From 123a3e589fbd29b025fbea4c35ef8b5e548df674 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 17 Jan 2024 20:22:11 +0330 Subject: [PATCH 1/7] eth/catalyst: update simulated beacon for cancun --- eth/catalyst/simulated_beacon.go | 41 ++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index 3c081074cc52..dc1b792440a3 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -148,26 +148,41 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u c.feeRecipientLock.Unlock() // Reset to CurrentBlock in case of the chain was rewound - if header := c.eth.BlockChain().CurrentBlock(); c.curForkchoiceState.HeadBlockHash != header.Hash() { + var ( + header = c.eth.BlockChain().CurrentBlock() + number = new(big.Int).Add(header.Number, big.NewInt(1)) + isCancun = c.eth.BlockChain().Config().IsCancun(number, timestamp) + ) + if c.curForkchoiceState.HeadBlockHash != header.Hash() { finalizedHash := c.finalizedBlockHash(header.Number.Uint64()) c.setCurrentState(header.Hash(), *finalizedHash) } var random [32]byte rand.Read(random[:]) - fcResponse, err := c.engineAPI.ForkchoiceUpdatedV2(c.curForkchoiceState, &engine.PayloadAttributes{ - Timestamp: timestamp, - SuggestedFeeRecipient: feeRecipient, - Withdrawals: withdrawals, - Random: random, - }) + + var ( + attrs = &engine.PayloadAttributes{ + Timestamp: timestamp, + SuggestedFeeRecipient: feeRecipient, + Withdrawals: withdrawals, + Random: random, + } + fcResponse engine.ForkChoiceResponse + err error + ) + if isCancun { + attrs.BeaconRoot = &common.Hash{} + fcResponse, err = c.engineAPI.ForkchoiceUpdatedV3(c.curForkchoiceState, attrs) + } else { + fcResponse, err = c.engineAPI.ForkchoiceUpdatedV2(c.curForkchoiceState, attrs) + } if err != nil { return err } if fcResponse == engine.STATUS_SYNCING { return errors.New("chain rewind prevented invocation of payload creation") } - envelope, err := c.engineAPI.getPayload(*fcResponse.PayloadID, true) if err != nil { return err @@ -186,8 +201,14 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u } // Mark the payload as canon - if _, err = c.engineAPI.NewPayloadV2(*payload); err != nil { - return err + if isCancun { + if _, err = c.engineAPI.NewPayloadV3(*payload, []common.Hash{}, &common.Hash{}); err != nil { + return err + } + } else { + if _, err = c.engineAPI.NewPayloadV2(*payload); err != nil { + return err + } } c.setCurrentState(payload.BlockHash, finalizedHash) From adc10d23a54d48908ebc05fc30fbe1dc6aa48859 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 18 Jan 2024 15:17:23 +0330 Subject: [PATCH 2/7] validate blob hashes --- eth/catalyst/simulated_beacon.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index dc1b792440a3..fc2cd1179fb6 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -19,6 +19,7 @@ package catalyst import ( "crypto/rand" "errors" + "fmt" "math/big" "sync" "time" @@ -202,6 +203,14 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u // Mark the payload as canon if isCancun { + txes, err := decodeTransactions(payload.Transactions) + if err != nil { + return err + } + blobHashes := make([]common.Hash, 0) + for _, tx := range txes { + blobHashes = append(blobHashes, tx.BlobHashes()...) + } if _, err = c.engineAPI.NewPayloadV3(*payload, []common.Hash{}, &common.Hash{}); err != nil { return err } @@ -321,3 +330,15 @@ func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) { }, }) } + +func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) { + var txs = make([]*types.Transaction, len(enc)) + for i, encTx := range enc { + var tx types.Transaction + if err := tx.UnmarshalBinary(encTx); err != nil { + return nil, fmt.Errorf("invalid transaction %d: %v", i, err) + } + txs[i] = &tx + } + return txs, nil +} From dbfe2be321f60b65eb9ad0835709a494353739d0 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Fri, 26 Jan 2024 13:51:02 +0100 Subject: [PATCH 3/7] compute hashes from commitment --- eth/catalyst/simulated_beacon.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index bb7848e87ded..d1473d583976 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -18,8 +18,8 @@ package catalyst import ( "crypto/rand" + "crypto/sha256" "errors" - "fmt" "math/big" "sync" "time" @@ -27,6 +27,7 @@ import ( "github.com/ethereum/go-ethereum/beacon/engine" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/node" @@ -192,15 +193,15 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u // Mark the payload as canon if isCancun { - txes, err := decodeTransactions(payload.Transactions) - if err != nil { - return err - } blobHashes := make([]common.Hash, 0) - for _, tx := range txes { - blobHashes = append(blobHashes, tx.BlobHashes()...) + if envelope.BlobsBundle != nil { + hasher := sha256.New() + for _, commit := range envelope.BlobsBundle.Commitments { + c := kzg4844.Commitment(commit[:]) + blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c)) + } } - if _, err = c.engineAPI.NewPayloadV3(*payload, []common.Hash{}, &common.Hash{}); err != nil { + if _, err = c.engineAPI.NewPayloadV3(*payload, blobHashes, &common.Hash{}); err != nil { return err } } else { @@ -319,15 +320,3 @@ func RegisterSimulatedBeaconAPIs(stack *node.Node, sim *SimulatedBeacon) { }, }) } - -func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) { - var txs = make([]*types.Transaction, len(enc)) - for i, encTx := range enc { - var tx types.Transaction - if err := tx.UnmarshalBinary(encTx); err != nil { - return nil, fmt.Errorf("invalid transaction %d: %v", i, err) - } - txs[i] = &tx - } - return txs, nil -} From 4cc0f14c90a55ee79e06b47807cadaf0bd5075b2 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 22 Feb 2024 18:35:36 +0100 Subject: [PATCH 4/7] fix beacon root and payload version --- eth/catalyst/simulated_beacon.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index b2e7dd24ab26..42d596d0006f 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -161,14 +161,22 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u c.setCurrentState(header.Hash(), *finalizedHash) } - var random [32]byte + var ( + random [32]byte + payloadVersion = engine.PayloadV2 + attrs = &engine.PayloadAttributes{ + Timestamp: timestamp, + SuggestedFeeRecipient: feeRecipient, + Withdrawals: withdrawals, + Random: random, + } + ) rand.Read(random[:]) - fcResponse, err := c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, &engine.PayloadAttributes{ - Timestamp: timestamp, - SuggestedFeeRecipient: feeRecipient, - Withdrawals: withdrawals, - Random: random, - }, engine.PayloadV2, true) + if isCancun { + payloadVersion = engine.PayloadV3 + attrs.BeaconRoot = &common.Hash{} + } + fcResponse, err := c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, attrs, payloadVersion, true) if err != nil { return err } From 0c58f4a5596d349c63c9633884c6537dea8b1f00 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Wed, 28 Feb 2024 15:46:59 +0100 Subject: [PATCH 5/7] check commitment conversion --- eth/catalyst/simulated_beacon.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index 42d596d0006f..021ce23795a0 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -206,7 +206,11 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u if envelope.BlobsBundle != nil { hasher := sha256.New() for _, commit := range envelope.BlobsBundle.Commitments { - c := kzg4844.Commitment(commit[:]) + var c kzg4844.Commitment + if len(commit) != len(c) { + return errors.New("invalid commitment length") + } + copy(c[:], commit) blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c)) } } From 0b4adbc4a6dacec6018bf1917f3b6484bdf5d0e2 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 29 Feb 2024 12:32:10 +0100 Subject: [PATCH 6/7] fix random attr --- eth/catalyst/simulated_beacon.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index 021ce23795a0..7bf547f6c179 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -161,8 +161,9 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u c.setCurrentState(header.Hash(), *finalizedHash) } + var random [32]byte + rand.Read(random[:]) var ( - random [32]byte payloadVersion = engine.PayloadV2 attrs = &engine.PayloadAttributes{ Timestamp: timestamp, @@ -171,7 +172,6 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u Random: random, } ) - rand.Read(random[:]) if isCancun { payloadVersion = engine.PayloadV3 attrs.BeaconRoot = &common.Hash{} From a8b9fd90d2443b88b1dbda109f19211009c3426b Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 29 Feb 2024 12:46:19 +0100 Subject: [PATCH 7/7] flip dev to cancun --- eth/catalyst/simulated_beacon.go | 61 ++++++++++++-------------------- params/config.go | 1 + 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/eth/catalyst/simulated_beacon.go b/eth/catalyst/simulated_beacon.go index 7bf547f6c179..4ae60ed4907c 100644 --- a/eth/catalyst/simulated_beacon.go +++ b/eth/catalyst/simulated_beacon.go @@ -151,32 +151,20 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u c.feeRecipientLock.Unlock() // Reset to CurrentBlock in case of the chain was rewound - var ( - header = c.eth.BlockChain().CurrentBlock() - number = new(big.Int).Add(header.Number, big.NewInt(1)) - isCancun = c.eth.BlockChain().Config().IsCancun(number, timestamp) - ) - if c.curForkchoiceState.HeadBlockHash != header.Hash() { + if header := c.eth.BlockChain().CurrentBlock(); c.curForkchoiceState.HeadBlockHash != header.Hash() { finalizedHash := c.finalizedBlockHash(header.Number.Uint64()) c.setCurrentState(header.Hash(), *finalizedHash) } var random [32]byte rand.Read(random[:]) - var ( - payloadVersion = engine.PayloadV2 - attrs = &engine.PayloadAttributes{ - Timestamp: timestamp, - SuggestedFeeRecipient: feeRecipient, - Withdrawals: withdrawals, - Random: random, - } - ) - if isCancun { - payloadVersion = engine.PayloadV3 - attrs.BeaconRoot = &common.Hash{} - } - fcResponse, err := c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, attrs, payloadVersion, true) + fcResponse, err := c.engineAPI.forkchoiceUpdated(c.curForkchoiceState, &engine.PayloadAttributes{ + Timestamp: timestamp, + SuggestedFeeRecipient: feeRecipient, + Withdrawals: withdrawals, + Random: random, + BeaconRoot: &common.Hash{}, + }, engine.PayloadV3, true) if err != nil { return err } @@ -200,27 +188,22 @@ func (c *SimulatedBeacon) sealBlock(withdrawals []*types.Withdrawal, timestamp u } } - // Mark the payload as canon - if isCancun { - blobHashes := make([]common.Hash, 0) - if envelope.BlobsBundle != nil { - hasher := sha256.New() - for _, commit := range envelope.BlobsBundle.Commitments { - var c kzg4844.Commitment - if len(commit) != len(c) { - return errors.New("invalid commitment length") - } - copy(c[:], commit) - blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c)) + // Independently calculate the blob hashes from sidecars. + blobHashes := make([]common.Hash, 0) + if envelope.BlobsBundle != nil { + hasher := sha256.New() + for _, commit := range envelope.BlobsBundle.Commitments { + var c kzg4844.Commitment + if len(commit) != len(c) { + return errors.New("invalid commitment length") } + copy(c[:], commit) + blobHashes = append(blobHashes, kzg4844.CalcBlobHashV1(hasher, &c)) } - if _, err = c.engineAPI.NewPayloadV3(*payload, blobHashes, &common.Hash{}); err != nil { - return err - } - } else { - if _, err = c.engineAPI.NewPayloadV2(*payload); err != nil { - return err - } + } + // Mark the payload as canon + if _, err = c.engineAPI.NewPayloadV3(*payload, blobHashes, &common.Hash{}); err != nil { + return err } c.setCurrentState(payload.BlockHash, finalizedHash) diff --git a/params/config.go b/params/config.go index 21ede457fd68..b24e782b8d96 100644 --- a/params/config.go +++ b/params/config.go @@ -183,6 +183,7 @@ var ( ArrowGlacierBlock: big.NewInt(0), GrayGlacierBlock: big.NewInt(0), ShanghaiTime: newUint64(0), + CancunTime: newUint64(0), TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, }