Skip to content

Commit 2653ae9

Browse files
p2p restructure (#2342)
Refactor: Restructure `p2p` package. This commit focuses on restructuring the `p2p` package to improve organization and maintainability. Key updates include: 1. **Package Restructure**: - Renamed `peers.go` to `codec.go`. - Refactored the folder structure: - Moved generated protobuf files to a new `gen` directory. - Grouped related files into subdirectories (`peers`, `sync`, etc.). - Simplified and clarified the organization of the `p2p` package. 2. **Adapter Function Updates**: - Updated adapter functions in multiple files (e.g., `block.go`, `class.go`, `felt.go`, `receipt.go`, `state.go`, and `transaction.go`) to reference the `gen` package instead of `spec`. 3. **Import Statement Updates**: - Updated all import statements to replace `spec` with `gen`. 4. **Makefile Changes**: - Added a new `generate-buf` target to support protobuf generation using the `buf` tool. 5. **Protobuf Tooling**: - Switched from `protoc` to `buf` for managing protobuf files. This restructuring streamlines the `p2p` package, enhancing its readability and modularity while transitioning to the `gen` package for consistency across the codebase.
1 parent 28e4512 commit 2653ae9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+6568
-7468
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ core-rust:
5757
compiler:
5858
$(MAKE) -C starknet/compiler/rust $(VM_TARGET)
5959

60+
generate-buf: ## Generate protobuf files
61+
@buf generate
62+
6063
generate: ## Generate mocks and code
6164
mkdir -p mocks
65+
generate-buf
6266
go generate ./...
6367

6468
clean-testcache: ## Clean Go test cache

adapters/core2p2p/block.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,51 @@ import (
55

66
"github.com/NethermindEth/juno/core"
77
"github.com/NethermindEth/juno/core/felt"
8-
"github.com/NethermindEth/juno/p2p/starknet/spec"
8+
"github.com/NethermindEth/juno/p2p/gen"
99
"github.com/NethermindEth/juno/utils"
1010
)
1111

12-
func AdaptBlockID(header *core.Header) *spec.BlockID {
12+
func AdaptBlockID(header *core.Header) *gen.BlockID {
1313
if header == nil {
1414
return nil
1515
}
1616

17-
return &spec.BlockID{
17+
return &gen.BlockID{
1818
Number: header.Number,
1919
Header: AdaptHash(header.Hash),
2020
}
2121
}
2222

23-
func AdaptSignature(sig []*felt.Felt) *spec.ConsensusSignature {
24-
return &spec.ConsensusSignature{
23+
func AdaptSignature(sig []*felt.Felt) *gen.ConsensusSignature {
24+
return &gen.ConsensusSignature{
2525
R: AdaptFelt(sig[0]),
2626
S: AdaptFelt(sig[1]),
2727
}
2828
}
2929

3030
func AdaptHeader(header *core.Header, commitments *core.BlockCommitments,
3131
stateDiffCommitment *felt.Felt, stateDiffLength uint64,
32-
) *spec.SignedBlockHeader {
33-
return &spec.SignedBlockHeader{
32+
) *gen.SignedBlockHeader {
33+
return &gen.SignedBlockHeader{
3434
BlockHash: AdaptHash(header.Hash),
3535
ParentHash: AdaptHash(header.ParentHash),
3636
Number: header.Number,
3737
Time: header.Timestamp,
3838
SequencerAddress: AdaptAddress(header.SequencerAddress),
3939
StateRoot: AdaptHash(header.GlobalStateRoot),
40-
Transactions: &spec.Patricia{
40+
Transactions: &gen.Patricia{
4141
NLeaves: header.TransactionCount,
4242
Root: AdaptHash(commitments.TransactionCommitment),
4343
},
44-
Events: &spec.Patricia{
44+
Events: &gen.Patricia{
4545
NLeaves: header.EventCount,
4646
Root: AdaptHash(commitments.EventCommitment),
4747
},
4848
Receipts: AdaptHash(commitments.ReceiptCommitment),
4949
ProtocolVersion: header.ProtocolVersion,
5050
GasPriceFri: AdaptUint128(header.GasPriceSTRK),
5151
Signatures: utils.Map(header.Signatures, AdaptSignature),
52-
StateDiffCommitment: &spec.StateDiffCommitment{
52+
StateDiffCommitment: &gen.StateDiffCommitment{
5353
StateDiffLength: stateDiffLength,
5454
Root: AdaptHash(stateDiffCommitment),
5555
},
@@ -60,23 +60,23 @@ func AdaptHeader(header *core.Header, commitments *core.BlockCommitments,
6060
}
6161
}
6262

63-
func adaptL1DA(da core.L1DAMode) spec.L1DataAvailabilityMode {
63+
func adaptL1DA(da core.L1DAMode) gen.L1DataAvailabilityMode {
6464
switch da {
6565
case core.Calldata:
66-
return spec.L1DataAvailabilityMode_Calldata
66+
return gen.L1DataAvailabilityMode_Calldata
6767
case core.Blob:
68-
return spec.L1DataAvailabilityMode_Blob
68+
return gen.L1DataAvailabilityMode_Blob
6969
default:
7070
panic(fmt.Errorf("unknown L1DAMode %v", da))
7171
}
7272
}
7373

74-
func AdaptEvent(e *core.Event, txH *felt.Felt) *spec.Event {
74+
func AdaptEvent(e *core.Event, txH *felt.Felt) *gen.Event {
7575
if e == nil {
7676
return nil
7777
}
7878

79-
return &spec.Event{
79+
return &gen.Event{
8080
TransactionHash: AdaptHash(txH),
8181
FromAddress: AdaptFelt(e.From),
8282
Keys: utils.Map(e.Keys, AdaptFelt),

adapters/core2p2p/class.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"fmt"
55

66
"github.com/NethermindEth/juno/core"
7-
"github.com/NethermindEth/juno/p2p/starknet/spec"
7+
"github.com/NethermindEth/juno/p2p/gen"
88
"github.com/NethermindEth/juno/utils"
99
)
1010

11-
func AdaptClass(class core.Class) *spec.Class {
11+
func AdaptClass(class core.Class) *gen.Class {
1212
if class == nil {
1313
return nil
1414
}
@@ -20,9 +20,9 @@ func AdaptClass(class core.Class) *spec.Class {
2020

2121
switch v := class.(type) {
2222
case *core.Cairo0Class:
23-
return &spec.Class{
24-
Class: &spec.Class_Cairo0{
25-
Cairo0: &spec.Cairo0Class{
23+
return &gen.Class{
24+
Class: &gen.Class_Cairo0{
25+
Cairo0: &gen.Cairo0Class{
2626
Abi: string(v.Abi),
2727
Externals: utils.Map(v.Externals, adaptEntryPoint),
2828
L1Handlers: utils.Map(v.L1Handlers, adaptEntryPoint),
@@ -34,11 +34,11 @@ func AdaptClass(class core.Class) *spec.Class {
3434
ClassHash: AdaptHash(hash),
3535
}
3636
case *core.Cairo1Class:
37-
return &spec.Class{
38-
Class: &spec.Class_Cairo1{
39-
Cairo1: &spec.Cairo1Class{
37+
return &gen.Class{
38+
Class: &gen.Class_Cairo1{
39+
Cairo1: &gen.Cairo1Class{
4040
Abi: v.Abi,
41-
EntryPoints: &spec.Cairo1EntryPoints{
41+
EntryPoints: &gen.Cairo1EntryPoints{
4242
Externals: utils.Map(v.EntryPoints.External, adaptSierra),
4343
L1Handlers: utils.Map(v.EntryPoints.L1Handler, adaptSierra),
4444
Constructors: utils.Map(v.EntryPoints.Constructor, adaptSierra),
@@ -55,15 +55,15 @@ func AdaptClass(class core.Class) *spec.Class {
5555
}
5656
}
5757

58-
func adaptSierra(e core.SierraEntryPoint) *spec.SierraEntryPoint {
59-
return &spec.SierraEntryPoint{
58+
func adaptSierra(e core.SierraEntryPoint) *gen.SierraEntryPoint {
59+
return &gen.SierraEntryPoint{
6060
Index: e.Index,
6161
Selector: AdaptFelt(e.Selector),
6262
}
6363
}
6464

65-
func adaptEntryPoint(e core.EntryPoint) *spec.EntryPoint {
66-
return &spec.EntryPoint{
65+
func adaptEntryPoint(e core.EntryPoint) *gen.EntryPoint {
66+
return &gen.EntryPoint{
6767
Selector: AdaptFelt(e.Selector),
6868
Offset: e.Offset.Uint64(),
6969
}

adapters/core2p2p/felt.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,55 @@ package core2p2p
22

33
import (
44
"github.com/NethermindEth/juno/core/felt"
5-
"github.com/NethermindEth/juno/p2p/starknet/spec"
5+
"github.com/NethermindEth/juno/p2p/gen"
66
"github.com/NethermindEth/juno/utils"
77
)
88

9-
func AdaptHash(f *felt.Felt) *spec.Hash {
9+
func AdaptHash(f *felt.Felt) *gen.Hash {
1010
if f == nil {
1111
return nil
1212
}
1313

14-
return &spec.Hash{
14+
return &gen.Hash{
1515
Elements: f.Marshal(),
1616
}
1717
}
1818

19-
func AdaptAccountSignature(signature []*felt.Felt) *spec.AccountSignature {
20-
return &spec.AccountSignature{
19+
func AdaptAccountSignature(signature []*felt.Felt) *gen.AccountSignature {
20+
return &gen.AccountSignature{
2121
Parts: utils.Map(signature, AdaptFelt),
2222
}
2323
}
2424

25-
func AdaptFelt(f *felt.Felt) *spec.Felt252 {
25+
func AdaptFelt(f *felt.Felt) *gen.Felt252 {
2626
if f == nil {
2727
return nil
2828
}
2929

30-
return &spec.Felt252{
30+
return &gen.Felt252{
3131
Elements: f.Marshal(),
3232
}
3333
}
3434

35-
func AdaptFeltSlice(sl []*felt.Felt) []*spec.Felt252 {
35+
func AdaptFeltSlice(sl []*felt.Felt) []*gen.Felt252 {
3636
return utils.Map(sl, AdaptFelt)
3737
}
3838

39-
func AdaptAddress(f *felt.Felt) *spec.Address {
39+
func AdaptAddress(f *felt.Felt) *gen.Address {
4040
if f == nil {
4141
return nil
4242
}
4343

44-
return &spec.Address{
44+
return &gen.Address{
4545
Elements: f.Marshal(),
4646
}
4747
}
4848

49-
func AdaptUint128(f *felt.Felt) *spec.Uint128 {
49+
func AdaptUint128(f *felt.Felt) *gen.Uint128 {
5050
// bits represents value in little endian byte order
5151
// i.e. first is least significant byte
5252
bits := f.Bits()
53-
return &spec.Uint128{
53+
return &gen.Uint128{
5454
Low: bits[0],
5555
High: bits[1],
5656
}

adapters/core2p2p/receipt.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,55 @@ package core2p2p
33
import (
44
"github.com/NethermindEth/juno/core"
55
"github.com/NethermindEth/juno/core/felt"
6-
"github.com/NethermindEth/juno/p2p/starknet/spec"
6+
"github.com/NethermindEth/juno/p2p/gen"
77
"github.com/NethermindEth/juno/utils"
88
)
99

1010
// Core Transaction receipt does not contain all the information required to create p2p spec Receipt, therefore,
1111
// we have to pass the transaction as well.
12-
func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *spec.Receipt {
12+
func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *gen.Receipt {
1313
if r == nil || txn == nil {
1414
return nil
1515
}
1616
switch t := txn.(type) {
1717
case *core.InvokeTransaction:
18-
return &spec.Receipt{
19-
Type: &spec.Receipt_Invoke_{
20-
Invoke: &spec.Receipt_Invoke{
18+
return &gen.Receipt{
19+
Type: &gen.Receipt_Invoke_{
20+
Invoke: &gen.Receipt_Invoke{
2121
Common: receiptCommon(r),
2222
},
2323
},
2424
}
2525
case *core.L1HandlerTransaction:
26-
return &spec.Receipt{
27-
Type: &spec.Receipt_L1Handler_{
28-
L1Handler: &spec.Receipt_L1Handler{
26+
return &gen.Receipt{
27+
Type: &gen.Receipt_L1Handler_{
28+
L1Handler: &gen.Receipt_L1Handler{
2929
Common: receiptCommon(r),
30-
MsgHash: &spec.Hash256{Elements: t.MessageHash()},
30+
MsgHash: &gen.Hash256{Elements: t.MessageHash()},
3131
},
3232
},
3333
}
3434
case *core.DeclareTransaction:
35-
return &spec.Receipt{
36-
Type: &spec.Receipt_Declare_{
37-
Declare: &spec.Receipt_Declare{
35+
return &gen.Receipt{
36+
Type: &gen.Receipt_Declare_{
37+
Declare: &gen.Receipt_Declare{
3838
Common: receiptCommon(r),
3939
},
4040
},
4141
}
4242
case *core.DeployTransaction:
43-
return &spec.Receipt{
44-
Type: &spec.Receipt_DeprecatedDeploy{
45-
DeprecatedDeploy: &spec.Receipt_Deploy{
43+
return &gen.Receipt{
44+
Type: &gen.Receipt_DeprecatedDeploy{
45+
DeprecatedDeploy: &gen.Receipt_Deploy{
4646
Common: receiptCommon(r),
4747
ContractAddress: AdaptFelt(t.ContractAddress),
4848
},
4949
},
5050
}
5151
case *core.DeployAccountTransaction:
52-
return &spec.Receipt{
53-
Type: &spec.Receipt_DeployAccount_{
54-
DeployAccount: &spec.Receipt_DeployAccount{
52+
return &gen.Receipt{
53+
Type: &gen.Receipt_DeployAccount_{
54+
DeployAccount: &gen.Receipt_DeployAccount{
5555
Common: receiptCommon(r),
5656
ContractAddress: AdaptFelt(t.ContractAddress),
5757
},
@@ -62,15 +62,15 @@ func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *spec.Receip
6262
}
6363
}
6464

65-
func receiptCommon(r *core.TransactionReceipt) *spec.Receipt_Common {
65+
func receiptCommon(r *core.TransactionReceipt) *gen.Receipt_Common {
6666
var revertReason *string
6767
if r.RevertReason != "" {
6868
revertReason = &r.RevertReason
6969
} else if r.Reverted {
7070
revertReason = utils.Ptr("")
7171
}
7272

73-
return &spec.Receipt_Common{
73+
return &gen.Receipt_Common{
7474
ActualFee: AdaptFelt(r.Fee),
7575
PriceUnit: adaptPriceUnit(r.FeeUnit),
7676
MessagesSent: utils.Map(r.L2ToL1Message, AdaptMessageToL1),
@@ -79,26 +79,26 @@ func receiptCommon(r *core.TransactionReceipt) *spec.Receipt_Common {
7979
}
8080
}
8181

82-
func adaptPriceUnit(unit core.FeeUnit) spec.PriceUnit {
82+
func adaptPriceUnit(unit core.FeeUnit) gen.PriceUnit {
8383
switch unit {
8484
case core.WEI:
85-
return spec.PriceUnit_Wei
85+
return gen.PriceUnit_Wei
8686
case core.STRK:
87-
return spec.PriceUnit_Fri // todo(kirill) recheck
87+
return gen.PriceUnit_Fri // todo(kirill) recheck
8888
default:
8989
panic("unreachable adaptPriceUnit")
9090
}
9191
}
9292

93-
func AdaptMessageToL1(mL1 *core.L2ToL1Message) *spec.MessageToL1 {
94-
return &spec.MessageToL1{
93+
func AdaptMessageToL1(mL1 *core.L2ToL1Message) *gen.MessageToL1 {
94+
return &gen.MessageToL1{
9595
FromAddress: AdaptFelt(mL1.From),
9696
Payload: utils.Map(mL1.Payload, AdaptFelt),
97-
ToAddress: &spec.EthereumAddress{Elements: mL1.To.Bytes()},
97+
ToAddress: &gen.EthereumAddress{Elements: mL1.To.Bytes()},
9898
}
9999
}
100100

101-
func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_ExecutionResources {
101+
func AdaptExecutionResources(er *core.ExecutionResources) *gen.Receipt_ExecutionResources {
102102
if er == nil {
103103
return nil
104104
}
@@ -112,8 +112,8 @@ func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_Executio
112112
totalL1Gas = new(felt.Felt).SetUint64(tgs.L1Gas)
113113
}
114114

115-
return &spec.Receipt_ExecutionResources{
116-
Builtins: &spec.Receipt_ExecutionResources_BuiltinCounter{
115+
return &gen.Receipt_ExecutionResources{
116+
Builtins: &gen.Receipt_ExecutionResources_BuiltinCounter{
117117
Bitwise: uint32(er.BuiltinInstanceCounter.Bitwise),
118118
Ecdsa: uint32(er.BuiltinInstanceCounter.Ecsda),
119119
EcOp: uint32(er.BuiltinInstanceCounter.EcOp),

0 commit comments

Comments
 (0)