Skip to content

Commit

Permalink
p2p restructure (#2342)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AnkushinDaniil authored Jan 8, 2025
1 parent 28e4512 commit 2653ae9
Show file tree
Hide file tree
Showing 49 changed files with 6,568 additions and 7,468 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ core-rust:
compiler:
$(MAKE) -C starknet/compiler/rust $(VM_TARGET)

generate-buf: ## Generate protobuf files
@buf generate

generate: ## Generate mocks and code
mkdir -p mocks
generate-buf
go generate ./...

clean-testcache: ## Clean Go test cache
Expand Down
30 changes: 15 additions & 15 deletions adapters/core2p2p/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@ import (

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/p2p/gen"
"github.com/NethermindEth/juno/utils"
)

func AdaptBlockID(header *core.Header) *spec.BlockID {
func AdaptBlockID(header *core.Header) *gen.BlockID {
if header == nil {
return nil
}

return &spec.BlockID{
return &gen.BlockID{
Number: header.Number,
Header: AdaptHash(header.Hash),
}
}

func AdaptSignature(sig []*felt.Felt) *spec.ConsensusSignature {
return &spec.ConsensusSignature{
func AdaptSignature(sig []*felt.Felt) *gen.ConsensusSignature {
return &gen.ConsensusSignature{
R: AdaptFelt(sig[0]),
S: AdaptFelt(sig[1]),
}
}

func AdaptHeader(header *core.Header, commitments *core.BlockCommitments,
stateDiffCommitment *felt.Felt, stateDiffLength uint64,
) *spec.SignedBlockHeader {
return &spec.SignedBlockHeader{
) *gen.SignedBlockHeader {
return &gen.SignedBlockHeader{
BlockHash: AdaptHash(header.Hash),
ParentHash: AdaptHash(header.ParentHash),
Number: header.Number,
Time: header.Timestamp,
SequencerAddress: AdaptAddress(header.SequencerAddress),
StateRoot: AdaptHash(header.GlobalStateRoot),
Transactions: &spec.Patricia{
Transactions: &gen.Patricia{
NLeaves: header.TransactionCount,
Root: AdaptHash(commitments.TransactionCommitment),
},
Events: &spec.Patricia{
Events: &gen.Patricia{
NLeaves: header.EventCount,
Root: AdaptHash(commitments.EventCommitment),
},
Receipts: AdaptHash(commitments.ReceiptCommitment),
ProtocolVersion: header.ProtocolVersion,
GasPriceFri: AdaptUint128(header.GasPriceSTRK),
Signatures: utils.Map(header.Signatures, AdaptSignature),
StateDiffCommitment: &spec.StateDiffCommitment{
StateDiffCommitment: &gen.StateDiffCommitment{
StateDiffLength: stateDiffLength,
Root: AdaptHash(stateDiffCommitment),
},
Expand All @@ -60,23 +60,23 @@ func AdaptHeader(header *core.Header, commitments *core.BlockCommitments,
}
}

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

func AdaptEvent(e *core.Event, txH *felt.Felt) *spec.Event {
func AdaptEvent(e *core.Event, txH *felt.Felt) *gen.Event {
if e == nil {
return nil
}

return &spec.Event{
return &gen.Event{
TransactionHash: AdaptHash(txH),
FromAddress: AdaptFelt(e.From),
Keys: utils.Map(e.Keys, AdaptFelt),
Expand Down
26 changes: 13 additions & 13 deletions adapters/core2p2p/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/p2p/gen"
"github.com/NethermindEth/juno/utils"
)

func AdaptClass(class core.Class) *spec.Class {
func AdaptClass(class core.Class) *gen.Class {
if class == nil {
return nil
}
Expand All @@ -20,9 +20,9 @@ func AdaptClass(class core.Class) *spec.Class {

switch v := class.(type) {
case *core.Cairo0Class:
return &spec.Class{
Class: &spec.Class_Cairo0{
Cairo0: &spec.Cairo0Class{
return &gen.Class{
Class: &gen.Class_Cairo0{
Cairo0: &gen.Cairo0Class{
Abi: string(v.Abi),
Externals: utils.Map(v.Externals, adaptEntryPoint),
L1Handlers: utils.Map(v.L1Handlers, adaptEntryPoint),
Expand All @@ -34,11 +34,11 @@ func AdaptClass(class core.Class) *spec.Class {
ClassHash: AdaptHash(hash),
}
case *core.Cairo1Class:
return &spec.Class{
Class: &spec.Class_Cairo1{
Cairo1: &spec.Cairo1Class{
return &gen.Class{
Class: &gen.Class_Cairo1{
Cairo1: &gen.Cairo1Class{
Abi: v.Abi,
EntryPoints: &spec.Cairo1EntryPoints{
EntryPoints: &gen.Cairo1EntryPoints{
Externals: utils.Map(v.EntryPoints.External, adaptSierra),
L1Handlers: utils.Map(v.EntryPoints.L1Handler, adaptSierra),
Constructors: utils.Map(v.EntryPoints.Constructor, adaptSierra),
Expand All @@ -55,15 +55,15 @@ func AdaptClass(class core.Class) *spec.Class {
}
}

func adaptSierra(e core.SierraEntryPoint) *spec.SierraEntryPoint {
return &spec.SierraEntryPoint{
func adaptSierra(e core.SierraEntryPoint) *gen.SierraEntryPoint {
return &gen.SierraEntryPoint{
Index: e.Index,
Selector: AdaptFelt(e.Selector),
}
}

func adaptEntryPoint(e core.EntryPoint) *spec.EntryPoint {
return &spec.EntryPoint{
func adaptEntryPoint(e core.EntryPoint) *gen.EntryPoint {
return &gen.EntryPoint{
Selector: AdaptFelt(e.Selector),
Offset: e.Offset.Uint64(),
}
Expand Down
24 changes: 12 additions & 12 deletions adapters/core2p2p/felt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,55 @@ package core2p2p

import (
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/p2p/starknet/spec"
"github.com/NethermindEth/juno/p2p/gen"
"github.com/NethermindEth/juno/utils"
)

func AdaptHash(f *felt.Felt) *spec.Hash {
func AdaptHash(f *felt.Felt) *gen.Hash {
if f == nil {
return nil
}

return &spec.Hash{
return &gen.Hash{
Elements: f.Marshal(),
}
}

func AdaptAccountSignature(signature []*felt.Felt) *spec.AccountSignature {
return &spec.AccountSignature{
func AdaptAccountSignature(signature []*felt.Felt) *gen.AccountSignature {
return &gen.AccountSignature{
Parts: utils.Map(signature, AdaptFelt),
}
}

func AdaptFelt(f *felt.Felt) *spec.Felt252 {
func AdaptFelt(f *felt.Felt) *gen.Felt252 {
if f == nil {
return nil
}

return &spec.Felt252{
return &gen.Felt252{
Elements: f.Marshal(),
}
}

func AdaptFeltSlice(sl []*felt.Felt) []*spec.Felt252 {
func AdaptFeltSlice(sl []*felt.Felt) []*gen.Felt252 {
return utils.Map(sl, AdaptFelt)
}

func AdaptAddress(f *felt.Felt) *spec.Address {
func AdaptAddress(f *felt.Felt) *gen.Address {
if f == nil {
return nil
}

return &spec.Address{
return &gen.Address{
Elements: f.Marshal(),
}
}

func AdaptUint128(f *felt.Felt) *spec.Uint128 {
func AdaptUint128(f *felt.Felt) *gen.Uint128 {
// bits represents value in little endian byte order
// i.e. first is least significant byte
bits := f.Bits()
return &spec.Uint128{
return &gen.Uint128{
Low: bits[0],
High: bits[1],
}
Expand Down
58 changes: 29 additions & 29 deletions adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@ 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/p2p/gen"
"github.com/NethermindEth/juno/utils"
)

// Core Transaction receipt does not contain all the information required to create p2p spec Receipt, therefore,
// we have to pass the transaction as well.
func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *spec.Receipt {
func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *gen.Receipt {
if r == nil || txn == nil {
return nil
}
switch t := txn.(type) {
case *core.InvokeTransaction:
return &spec.Receipt{
Type: &spec.Receipt_Invoke_{
Invoke: &spec.Receipt_Invoke{
return &gen.Receipt{
Type: &gen.Receipt_Invoke_{
Invoke: &gen.Receipt_Invoke{
Common: receiptCommon(r),
},
},
}
case *core.L1HandlerTransaction:
return &spec.Receipt{
Type: &spec.Receipt_L1Handler_{
L1Handler: &spec.Receipt_L1Handler{
return &gen.Receipt{
Type: &gen.Receipt_L1Handler_{
L1Handler: &gen.Receipt_L1Handler{
Common: receiptCommon(r),
MsgHash: &spec.Hash256{Elements: t.MessageHash()},
MsgHash: &gen.Hash256{Elements: t.MessageHash()},
},
},
}
case *core.DeclareTransaction:
return &spec.Receipt{
Type: &spec.Receipt_Declare_{
Declare: &spec.Receipt_Declare{
return &gen.Receipt{
Type: &gen.Receipt_Declare_{
Declare: &gen.Receipt_Declare{
Common: receiptCommon(r),
},
},
}
case *core.DeployTransaction:
return &spec.Receipt{
Type: &spec.Receipt_DeprecatedDeploy{
DeprecatedDeploy: &spec.Receipt_Deploy{
return &gen.Receipt{
Type: &gen.Receipt_DeprecatedDeploy{
DeprecatedDeploy: &gen.Receipt_Deploy{
Common: receiptCommon(r),
ContractAddress: AdaptFelt(t.ContractAddress),
},
},
}
case *core.DeployAccountTransaction:
return &spec.Receipt{
Type: &spec.Receipt_DeployAccount_{
DeployAccount: &spec.Receipt_DeployAccount{
return &gen.Receipt{
Type: &gen.Receipt_DeployAccount_{
DeployAccount: &gen.Receipt_DeployAccount{
Common: receiptCommon(r),
ContractAddress: AdaptFelt(t.ContractAddress),
},
Expand All @@ -62,15 +62,15 @@ func AdaptReceipt(r *core.TransactionReceipt, txn core.Transaction) *spec.Receip
}
}

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

return &spec.Receipt_Common{
return &gen.Receipt_Common{
ActualFee: AdaptFelt(r.Fee),
PriceUnit: adaptPriceUnit(r.FeeUnit),
MessagesSent: utils.Map(r.L2ToL1Message, AdaptMessageToL1),
Expand All @@ -79,26 +79,26 @@ func receiptCommon(r *core.TransactionReceipt) *spec.Receipt_Common {
}
}

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

func AdaptMessageToL1(mL1 *core.L2ToL1Message) *spec.MessageToL1 {
return &spec.MessageToL1{
func AdaptMessageToL1(mL1 *core.L2ToL1Message) *gen.MessageToL1 {
return &gen.MessageToL1{
FromAddress: AdaptFelt(mL1.From),
Payload: utils.Map(mL1.Payload, AdaptFelt),
ToAddress: &spec.EthereumAddress{Elements: mL1.To.Bytes()},
ToAddress: &gen.EthereumAddress{Elements: mL1.To.Bytes()},
}
}

func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_ExecutionResources {
func AdaptExecutionResources(er *core.ExecutionResources) *gen.Receipt_ExecutionResources {
if er == nil {
return nil
}
Expand All @@ -112,8 +112,8 @@ func AdaptExecutionResources(er *core.ExecutionResources) *spec.Receipt_Executio
totalL1Gas = new(felt.Felt).SetUint64(tgs.L1Gas)
}

return &spec.Receipt_ExecutionResources{
Builtins: &spec.Receipt_ExecutionResources_BuiltinCounter{
return &gen.Receipt_ExecutionResources{
Builtins: &gen.Receipt_ExecutionResources_BuiltinCounter{
Bitwise: uint32(er.BuiltinInstanceCounter.Bitwise),
Ecdsa: uint32(er.BuiltinInstanceCounter.Ecsda),
EcOp: uint32(er.BuiltinInstanceCounter.EcOp),
Expand Down
Loading

0 comments on commit 2653ae9

Please sign in to comment.