|
| 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