Skip to content

Commit

Permalink
Update p2p implementation according to recent spec (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Jul 8, 2024
1 parent 4022926 commit 65c0d6c
Show file tree
Hide file tree
Showing 43 changed files with 4,251 additions and 6,066 deletions.
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ clean: ## clean project builds
help: ## show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

feedernode:
feedernode: juno-cached
./build/juno \
--network=sepolia \
--log-level=debug \
Expand All @@ -98,7 +98,9 @@ feedernode:
--p2p-private-key="5f6cdc3aebcc74af494df054876100368ef6126e3a33fa65b90c765b381ffc37a0a63bbeeefab0740f24a6a38dabb513b9233254ad0020c721c23e69bc820089" \
--metrics-port=9090

node1:
node1: juno-cached
# todo remove rm before merge
rm -rf ./p2p-dbs/node1/ && \
./build/juno \
--network=sepolia \
--log-level=debug \
Expand Down Expand Up @@ -132,3 +134,12 @@ node3:
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9093

pathfinder: juno-cached
./build/juno \
--network=sepolia \
--log-level=debug \
--db-path=./p2p-dbs/node-pathfinder \
--p2p \
--p2p-peers=/ip4/127.0.0.1/tcp/8888/p2p/12D3KooWF1JrZWQoBiBSjsFSuLbDiDvqcmJQRLaFQLmpVkHA9duk \
--p2p-private-key="54a695e2a5d5717d5ba8730efcafe6f17251a1955733cffc55a4085fbf7f5d2c1b4009314092069ef7ca9b364ce3eb3072531c64dfb2799c6bad76720a5bdff0" \
--metrics-port=9094
48 changes: 31 additions & 17 deletions adapters/core2p2p/block.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package core2p2p

import (
"time"
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/utils"
"google.golang.org/protobuf/types/known/timestamppb"
)

func AdaptBlockID(header *core.Header) *spec.BlockID {
Expand All @@ -28,29 +27,44 @@ func AdaptSignature(sig []*felt.Felt) *spec.ConsensusSignature {
}
}

func AdaptHeader(header *core.Header, commitments *core.BlockCommitments) *spec.BlockHeader {
return &spec.BlockHeader{
func AdaptHeader(header *core.Header, commitments *core.BlockCommitments) *spec.SignedBlockHeader {
return &spec.SignedBlockHeader{
BlockHash: AdaptHash(header.Hash),
ParentHash: AdaptHash(header.ParentHash),
Number: header.Number,
Time: timestamppb.New(time.Unix(int64(header.Timestamp), 0)),
Time: header.Timestamp,
SequencerAddress: AdaptAddress(header.SequencerAddress),
ProofFact: nil, // not defined yet
Receipts: nil, // not defined yet
StateDiffs: nil, // not defined yet
State: &spec.Patricia{
Height: core.ContractStorageTrieHeight,
Root: AdaptHash(header.GlobalStateRoot),
},
Transactions: &spec.Merkle{
NLeaves: uint32(header.TransactionCount),
StateRoot: AdaptHash(header.GlobalStateRoot),
Transactions: &spec.Patricia{
NLeaves: header.TransactionCount,
Root: AdaptHash(commitments.TransactionCommitment),
},
Events: &spec.Merkle{
NLeaves: uint32(header.EventCount),
Events: &spec.Patricia{
NLeaves: header.EventCount,
Root: AdaptHash(commitments.EventCommitment),
},
// todo fill receipts
Receipts: nil,
ProtocolVersion: header.ProtocolVersion,
GasPrice: AdaptFelt(header.GasPrice),
GasPriceFri: AdaptUint128(header.GasPrice),
Signatures: utils.Map(header.Signatures, AdaptSignature),
// todo(kirill) set these fields
StateDiffCommitment: nil,
GasPriceWei: AdaptUint128(header.GasPriceSTRK),
DataGasPriceFri: AdaptUint128(header.L1DataGasPrice.PriceInFri),
DataGasPriceWei: AdaptUint128(header.L1DataGasPrice.PriceInWei),
L1DataAvailabilityMode: adaptL1DA(header.L1DAMode),
}
}

func adaptL1DA(da core.L1DAMode) spec.L1DataAvailabilityMode {
switch da {
case core.Calldata:
return spec.L1DataAvailabilityMode_Calldata
case core.Blob:
return spec.L1DataAvailabilityMode_Blob
default:
panic(fmt.Errorf("unknown L1DAMode %v", da))
}
}

Expand Down
18 changes: 6 additions & 12 deletions adapters/core2p2p/class.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core2p2p

import (
"encoding/json"
"fmt"

"github.com/NethermindEth/juno/core"
Expand All @@ -19,35 +18,30 @@ func AdaptClass(class core.Class) *spec.Class {
return &spec.Class{
Class: &spec.Class_Cairo0{
Cairo0: &spec.Cairo0Class{
Abi: v.Abi,
Abi: string(v.Abi),
Externals: utils.Map(v.Externals, adaptEntryPoint),
L1Handlers: utils.Map(v.L1Handlers, adaptEntryPoint),
Constructors: utils.Map(v.Constructors, adaptEntryPoint),
Program: []byte(v.Program),
Program: v.Program,
},
},
Domain: 0, // todo(kirill) recheck
}
case *core.Cairo1Class:
// Todo: Add compiled class struct to p2p spec. For now we will use json encoding
compiledBytes, err := json.Marshal(v.Compiled)
if err != nil {
panic(fmt.Errorf("unable to marshal compiled class hash to json: %v", err))
}

return &spec.Class{
Class: &spec.Class_Cairo1{
Cairo1: &spec.Cairo1Class{
Abi: []byte(v.Abi),
Abi: v.Abi,
EntryPoints: &spec.Cairo1EntryPoints{
Externals: utils.Map(v.EntryPoints.External, adaptSierra),
L1Handlers: utils.Map(v.EntryPoints.L1Handler, adaptSierra),
Constructors: utils.Map(v.EntryPoints.Constructor, adaptSierra),
},
Program: utils.Map(v.Program, AdaptFelt),
ContractClassVersion: v.SemanticVersion,
Compiled: compiledBytes,
},
},
Domain: 0, // todo(kirill) recheck
}
default:
panic(fmt.Errorf("unsupported cairo class %T (version=%d)", v, class.Version()))
Expand All @@ -64,6 +58,6 @@ func adaptSierra(e core.SierraEntryPoint) *spec.SierraEntryPoint {
func adaptEntryPoint(e core.EntryPoint) *spec.EntryPoint {
return &spec.EntryPoint{
Selector: AdaptFelt(e.Selector),
Offset: AdaptFelt(e.Offset),
Offset: e.Offset.Uint64(),
}
}
10 changes: 10 additions & 0 deletions adapters/core2p2p/felt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ func AdaptAddress(f *felt.Felt) *spec.Address {
Elements: f.Marshal(),
}
}

func AdaptUint128(f *felt.Felt) *spec.Uint128 {
// bits represents value in little endian byte order
// i.e. first is least significant byte
bits := f.Bits()
return &spec.Uint128{
Low: bits[0],
High: bits[1],
}
}
29 changes: 26 additions & 3 deletions adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core2p2p

import (
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/utils"
)
Expand Down Expand Up @@ -62,13 +63,28 @@ func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *spec.Receip
}

func receiptCommon(r *core.TransactionReceipt) *spec.Receipt_Common {
var revertReason *string
if r.RevertReason != "" {
revertReason = &r.RevertReason
}

return &spec.Receipt_Common{
TransactionHash: AdaptHash(r.TransactionHash),
ActualFee: AdaptFelt(r.Fee),
PriceUnit: adaptPriceUnit(r.FeeUnit),
MessagesSent: utils.Map(r.L2ToL1Message, AdaptMessageToL1),
ExecutionResources: AdaptExecutionResources(r.ExecutionResources),
RevertReason: r.RevertReason,
ConsumedMessage: nil, // todo(kirill) recheck
RevertReason: revertReason,
}
}

func adaptPriceUnit(unit core.FeeUnit) spec.PriceUnit {
switch unit {
case core.WEI:
return spec.PriceUnit_Wei
case core.STRK:
return spec.PriceUnit_Fri // todo(kirill) recheck
default:
panic("unreachable adaptPriceUnit")
}
}

Expand All @@ -85,6 +101,11 @@ func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_Executio
return nil
}

var l1Gas, l1DataGas *spec.Felt252
if da := er.DataAvailability; da != nil { // todo(kirill) check that it might be null
l1Gas = AdaptFelt(new(felt.Felt).SetUint64(da.L1Gas))
l1DataGas = AdaptFelt(new(felt.Felt).SetUint64(da.L1DataGas))
}
return &spec.Receipt_ExecutionResources{
Builtins: &spec.Receipt_ExecutionResources_BuiltinCounter{
Bitwise: uint32(er.BuiltinInstanceCounter.Bitwise),
Expand All @@ -98,5 +119,7 @@ func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_Executio
},
Steps: uint32(er.Steps),
MemoryHoles: uint32(er.MemoryHoles),
L1Gas: l1Gas,
L1DataGas: l1DataGas,
}
}
16 changes: 5 additions & 11 deletions adapters/core2p2p/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"github.com/NethermindEth/juno/utils"
)

func AdaptStateDiff(addr, nonce *felt.Felt, diff map[felt.Felt]*felt.Felt) *spec.StateDiff_ContractDiff {
return &spec.StateDiff_ContractDiff{
func AdaptContractDiff(addr, nonce, classHash *felt.Felt, storageDiff map[felt.Felt]*felt.Felt) *spec.ContractDiff {
return &spec.ContractDiff{
Address: AdaptAddress(addr),
Nonce: AdaptFelt(nonce),
ClassHash: nil, // This will need to be set if deployed_contracts and replaced_classes are removed from StateDiff
Values: AdaptStorageDiff(diff),
ClassHash: AdaptHash(classHash), // This will need to be set if deployed_contracts and replaced_classes are removed from StateDiff
Values: AdaptStorageDiff(storageDiff),
Domain: 0,
}
}

Expand All @@ -23,10 +24,3 @@ func AdaptStorageDiff(diff map[felt.Felt]*felt.Felt) []*spec.ContractStoredValue
}
})
}

func AdaptAddressClassHashPair(address felt.Felt, classHash *felt.Felt) *spec.StateDiff_ContractAddrToClassHash {
return &spec.StateDiff_ContractAddrToClassHash{
ContractAddr: AdaptAddress(&address),
ClassHash: AdaptHash(classHash),
}
}
Loading

0 comments on commit 65c0d6c

Please sign in to comment.