Skip to content

Commit 2bcd6ba

Browse files
committed
feature: implemet p2p2consensus adapters for proposal types
1 parent 0dc9888 commit 2bcd6ba

File tree

5 files changed

+459
-0
lines changed

5 files changed

+459
-0
lines changed

adapters/p2p2consensus/class.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package p2p2consensus
2+
3+
import (
4+
"github.com/NethermindEth/juno/adapters/sn2core"
5+
"github.com/NethermindEth/juno/core"
6+
"github.com/NethermindEth/juno/core/crypto"
7+
"github.com/NethermindEth/juno/starknet"
8+
"github.com/NethermindEth/juno/starknet/compiler"
9+
"github.com/NethermindEth/juno/utils"
10+
"github.com/starknet-io/starknet-p2pspecs/p2p/proto/class"
11+
)
12+
13+
func AdaptClass(cls *class.Cairo1Class) core.Class {
14+
if cls == nil {
15+
return nil
16+
}
17+
18+
abiHash := crypto.StarknetKeccak([]byte(cls.Abi))
19+
20+
program := utils.Map(cls.Program, AdaptFelt)
21+
compiled, err := createCompiledClass(cls)
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
adaptEP := func(points []*class.SierraEntryPoint) []core.SierraEntryPoint {
27+
// usage of NonNilSlice is essential because relevant core class fields are non nil
28+
return utils.Map(utils.NonNilSlice(points), adaptSierra)
29+
}
30+
31+
entryPoints := cls.EntryPoints
32+
return &core.Cairo1Class{
33+
Abi: cls.Abi,
34+
AbiHash: abiHash,
35+
EntryPoints: struct {
36+
Constructor []core.SierraEntryPoint
37+
External []core.SierraEntryPoint
38+
L1Handler []core.SierraEntryPoint
39+
}{
40+
Constructor: adaptEP(entryPoints.Constructors),
41+
External: adaptEP(entryPoints.Externals),
42+
L1Handler: adaptEP(entryPoints.L1Handlers),
43+
},
44+
Program: program,
45+
ProgramHash: crypto.PoseidonArray(program...),
46+
SemanticVersion: cls.ContractClassVersion,
47+
Compiled: compiled,
48+
}
49+
}
50+
51+
func adaptSierra(e *class.SierraEntryPoint) core.SierraEntryPoint {
52+
return core.SierraEntryPoint{
53+
Index: e.Index,
54+
Selector: AdaptFelt(e.Selector),
55+
}
56+
}
57+
58+
func createCompiledClass(cairo1 *class.Cairo1Class) (*core.CompiledClass, error) {
59+
if cairo1 == nil {
60+
return nil, nil
61+
}
62+
63+
adapt := func(ep *class.SierraEntryPoint) starknet.SierraEntryPoint {
64+
return starknet.SierraEntryPoint{
65+
Index: ep.Index,
66+
Selector: AdaptFelt(ep.Selector),
67+
}
68+
}
69+
ep := cairo1.EntryPoints
70+
def := &starknet.SierraDefinition{
71+
Abi: cairo1.Abi,
72+
EntryPoints: starknet.SierraEntryPoints{
73+
// WARNING: usage of utils.NonNilSlice is essential, otherwise compilation will finish with errors
74+
// todo move NonNilSlice to Compile ?
75+
Constructor: utils.Map(utils.NonNilSlice(ep.Constructors), adapt),
76+
External: utils.Map(utils.NonNilSlice(ep.Externals), adapt),
77+
L1Handler: utils.Map(utils.NonNilSlice(ep.L1Handlers), adapt),
78+
},
79+
Program: utils.Map(cairo1.Program, AdaptFelt),
80+
Version: cairo1.ContractClassVersion,
81+
}
82+
83+
compiledClass, err := compiler.Compile(def)
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
return sn2core.AdaptCompiledClass(compiledClass)
89+
}

adapters/p2p2consensus/felt.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package p2p2consensus
2+
3+
import (
4+
"encoding/binary"
5+
6+
"github.com/NethermindEth/juno/core/felt"
7+
"github.com/NethermindEth/juno/utils"
8+
ethcommon "github.com/ethereum/go-ethereum/common"
9+
"github.com/starknet-io/starknet-p2pspecs/p2p/proto/common"
10+
"github.com/starknet-io/starknet-p2pspecs/p2p/proto/sync/receipt"
11+
)
12+
13+
func AdaptHash(h *common.Hash) *felt.Felt {
14+
return adapt(h)
15+
}
16+
17+
func AdaptAddress(h *common.Address) *felt.Felt {
18+
return adapt(h)
19+
}
20+
21+
func AdaptEthAddress(h *receipt.EthereumAddress) ethcommon.Address {
22+
return ethcommon.BytesToAddress(h.Elements)
23+
}
24+
25+
func AdaptFelt(f *common.Felt252) *felt.Felt {
26+
return adapt(f)
27+
}
28+
29+
func adapt(v interface{ GetElements() []byte }) *felt.Felt {
30+
if utils.IsNil(v) {
31+
return nil
32+
}
33+
34+
return new(felt.Felt).SetBytes(v.GetElements())
35+
}
36+
37+
func AdaptUint128(u *common.Uint128) *felt.Felt {
38+
if u == nil {
39+
return nil
40+
}
41+
42+
bytes := make([]byte, 16) //nolint:mnd
43+
44+
binary.BigEndian.PutUint64(bytes[:8], u.High)
45+
binary.BigEndian.PutUint64(bytes[8:], u.Low)
46+
47+
return new(felt.Felt).SetBytes(bytes)
48+
}

adapters/p2p2consensus/proposal.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package p2p2consensus
2+
3+
import (
4+
"math/big"
5+
6+
"github.com/Masterminds/semver/v3"
7+
consensus "github.com/NethermindEth/juno/consensus/types"
8+
"github.com/NethermindEth/juno/core"
9+
"github.com/NethermindEth/juno/core/felt"
10+
p2pconsensus "github.com/NethermindEth/juno/p2p/proto/consensus/consensus"
11+
"github.com/NethermindEth/juno/utils"
12+
common "github.com/starknet-io/starknet-p2pspecs/p2p/proto/common"
13+
)
14+
15+
func U128ToFelt(u *common.Uint128) *felt.Felt {
16+
lowBig := new(big.Int).SetUint64(u.Low)
17+
highBig := new(big.Int).SetUint64(u.High)
18+
highBig.Lsh(highBig, 64) //nolint:mnd
19+
return new(felt.Felt).SetBigInt(highBig.Or(highBig, lowBig))
20+
}
21+
22+
func AdaptProposalInit(msg *p2pconsensus.ProposalInit) consensus.ProposalInit {
23+
return consensus.ProposalInit{
24+
BlockNum: msg.BlockNumber,
25+
Proposer: *new(felt.Felt).SetBytes(msg.Proposer.Elements),
26+
}
27+
}
28+
29+
func AdaptBlockInfo(msg *p2pconsensus.BlockInfo) *consensus.BlockInfo {
30+
return &consensus.BlockInfo{
31+
BlockNumber: msg.BlockNumber,
32+
Builder: *new(felt.Felt).SetBytes(msg.Builder.Elements),
33+
Timestamp: msg.Timestamp,
34+
L2GasPriceFRI: *U128ToFelt(msg.L2GasPriceFri),
35+
L1GasPriceWEI: *U128ToFelt(msg.L1DataGasPriceWei),
36+
L1DataGasPriceWEI: *U128ToFelt(msg.L1DataGasPriceWei),
37+
EthToStrkRate: *U128ToFelt(msg.EthToStrkRate),
38+
L1DAMode: core.L1DAMode(msg.L1DaMode),
39+
}
40+
}
41+
42+
func AdaptProposalCommitment(msg *p2pconsensus.ProposalCommitment) consensus.ProposalCommitment {
43+
return consensus.ProposalCommitment{
44+
BlockNumber: msg.BlockNumber,
45+
Builder: *new(felt.Felt).SetBytes(msg.Builder.Elements),
46+
47+
ParentCommitment: *new(felt.Felt).SetBytes(msg.ParentCommitment.Elements),
48+
Timestamp: msg.Timestamp,
49+
ProtocolVersion: *semver.MustParse(msg.ProtocolVersion),
50+
51+
OldStateRoot: *new(felt.Felt).SetBytes(msg.OldStateRoot.Elements),
52+
VersionConstantCommitment: *new(felt.Felt).SetBytes(msg.VersionConstantCommitment.Elements),
53+
NextL2GasPriceFRI: *U128ToFelt(msg.NextL2GasPriceFri),
54+
55+
StateDiffCommitment: *new(felt.Felt).SetBytes(msg.StateDiffCommitment.Elements),
56+
TransactionCommitment: *new(felt.Felt).SetBytes(msg.TransactionCommitment.Elements),
57+
EventCommitment: *new(felt.Felt).SetBytes(msg.EventCommitment.Elements),
58+
ReceiptCommitment: *new(felt.Felt).SetBytes(msg.ReceiptCommitment.Elements),
59+
ConcatenatedCounts: *new(felt.Felt).SetBytes(msg.ConcatenatedCounts.Elements),
60+
L1GasPriceFRI: *U128ToFelt(msg.L1GasPriceFri),
61+
L1DataGasPriceFRI: *U128ToFelt(msg.L1DataGasPriceFri),
62+
L2GasPriceFRI: *U128ToFelt(msg.L2GasPriceFri),
63+
L2GasUsed: *U128ToFelt(msg.L2GasUsed),
64+
L1DAMode: core.L1DAMode(msg.L1DaMode),
65+
}
66+
}
67+
68+
func AdaptProposalTransaction(msg *p2pconsensus.TransactionBatch, network *utils.Network) []consensus.Transaction {
69+
txns := make([]consensus.Transaction, len(msg.Transactions))
70+
for i := range msg.Transactions {
71+
txn, class := AdaptTransaction(msg.Transactions[i], network)
72+
txns[i] = consensus.Transaction{
73+
Transaction: txn,
74+
Class: class,
75+
PaidFeeOnL1: nil, // Todo: this value is not passed in the spec.
76+
}
77+
}
78+
return txns
79+
}
80+
81+
func AdaptProposalFin(msg *p2pconsensus.ProposalFin) consensus.ProposalFin {
82+
return consensus.ProposalFin(*new(felt.Felt).SetBytes(msg.ProposalCommitment.Elements))
83+
}

0 commit comments

Comments
 (0)