From 26b64ed32d968e6376915e814454272baa498b1b Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Tue, 29 Oct 2024 18:41:36 +0530 Subject: [PATCH 1/5] wip --- core/blockchain_zkevm.go | 34 +++++++++--------- core/state/intra_block_state_zkevm.go | 42 ++++++++++++---------- zk/stages/stage_sequence_execute_blocks.go | 4 ++- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/core/blockchain_zkevm.go b/core/blockchain_zkevm.go index de39378f17b..7fe1d98a971 100644 --- a/core/blockchain_zkevm.go +++ b/core/blockchain_zkevm.go @@ -158,24 +158,26 @@ func ExecuteBlockEphemerallyZk( } var l2InfoRoot *libcommon.Hash - if chainConfig.IsForkID7Etrog(blockNum) { - l2InfoRoot, err = blockinfo.BuildBlockInfoTree( - &header.Coinbase, - header.Number.Uint64(), - header.Time, - blockGasLimit, - *usedGas, - *ger, - *l1Blockhash, - *prevBlockRoot, - &txInfos, - ) - if err != nil { - return nil, err + if !chainConfig.IsNormalcy(blockNum) { + if chainConfig.IsForkID7Etrog(blockNum) { + l2InfoRoot, err = blockinfo.BuildBlockInfoTree( + &header.Coinbase, + header.Number.Uint64(), + header.Time, + blockGasLimit, + *usedGas, + *ger, + *l1Blockhash, + *prevBlockRoot, + &txInfos, + ) + if err != nil { + return nil, err + } } - } - ibs.PostExecuteStateSet(chainConfig, block.NumberU64(), l2InfoRoot) + ibs.PostExecuteStateSet(chainConfig, block.NumberU64(), l2InfoRoot) + } receiptSha := types.DeriveSha(receipts) // [zkevm] todo diff --git a/core/state/intra_block_state_zkevm.go b/core/state/intra_block_state_zkevm.go index 22a44c769ac..88bdea9632a 100644 --- a/core/state/intra_block_state_zkevm.go +++ b/core/state/intra_block_state_zkevm.go @@ -59,7 +59,7 @@ func (sdb *IntraBlockState) GetTxCount() (uint64, error) { func (sdb *IntraBlockState) PostExecuteStateSet(chainConfig *chain.Config, blockNum uint64, blockInfoRoot *libcommon.Hash) { //ETROG - if chainConfig.IsForkID7Etrog(blockNum) { + if chainConfig.IsForkID7Etrog(blockNum) && !chainConfig.IsNormalcy(blockNum) { sdb.scalableSetBlockInfoRoot(blockInfoRoot) } } @@ -70,18 +70,20 @@ func (sdb *IntraBlockState) PreExecuteStateSet(chainConfig *chain.Config, blockN sdb.CreateAccount(ADDRESS_SCALABLE_L2, true) } - //save block number - sdb.scalableSetBlockNum(blockNumber) + if !chainConfig.IsNormalcy(blockNumber) { + //save block number + sdb.scalableSetBlockNum(blockNumber) - //ETROG - if chainConfig.IsForkID7Etrog(blockNumber) { - currentTimestamp := sdb.ScalableGetTimestamp() - if blockTimestamp > currentTimestamp { - sdb.ScalableSetTimestamp(blockTimestamp) - } + //ETROG + if chainConfig.IsForkID7Etrog(blockNumber) { + currentTimestamp := sdb.ScalableGetTimestamp() + if blockTimestamp > currentTimestamp { + sdb.ScalableSetTimestamp(blockTimestamp) + } - //save prev block hash - sdb.scalableSetBlockHash(blockNumber-1, stateRoot) + //save prev block hash + sdb.scalableSetBlockHash(blockNumber-1, stateRoot) + } } } @@ -99,18 +101,22 @@ func (sdb *IntraBlockState) SyncerPreExecuteStateSet( } //save block number - sdb.scalableSetBlockNum(blockNumber) + if chainConfig.IsNormalcy(blockNumber) { + sdb.scalableSetBlockNum(blockNumber) + } emptyHash := libcommon.Hash{} //ETROG if chainConfig.IsForkID7Etrog(blockNumber) { - currentTimestamp := sdb.ScalableGetTimestamp() - if blockTimestamp > currentTimestamp { - sdb.ScalableSetTimestamp(blockTimestamp) - } + if chainConfig.IsNormalcy(blockNumber) { + currentTimestamp := sdb.ScalableGetTimestamp() + if blockTimestamp > currentTimestamp { + sdb.ScalableSetTimestamp(blockTimestamp) + } - //save prev block hash - sdb.scalableSetBlockHash(blockNumber-1, prevBlockHash) + //save prev block hash + sdb.scalableSetBlockHash(blockNumber-1, prevBlockHash) + } //save ger with l1blockhash - but only in the case that the l1 info tree index hasn't been // re-used. If it has been re-used we never write this to the contract storage diff --git a/zk/stages/stage_sequence_execute_blocks.go b/zk/stages/stage_sequence_execute_blocks.go index 282adc7c41b..e56fa841369 100644 --- a/zk/stages/stage_sequence_execute_blocks.go +++ b/zk/stages/stage_sequence_execute_blocks.go @@ -283,7 +283,9 @@ func postBlockStateHandling( return err } - ibs.PostExecuteStateSet(cfg.chainConfig, header.Number.Uint64(), blockInfoRootHash) + if !cfg.chainConfig.IsNormalcy(header.Number.Uint64()) { + ibs.PostExecuteStateSet(cfg.chainConfig, header.Number.Uint64(), blockInfoRootHash) + } // store a reference to this block info root against the block number return hermezDb.WriteBlockInfoRoot(header.Number.Uint64(), *blockInfoRootHash) From 903d4c33a109e21f7db790dea7d66534279b8dde Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Wed, 30 Oct 2024 00:19:56 +0530 Subject: [PATCH 2/5] wip --- core/state/intra_block_state_zkevm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/intra_block_state_zkevm.go b/core/state/intra_block_state_zkevm.go index 88bdea9632a..691a02faf5c 100644 --- a/core/state/intra_block_state_zkevm.go +++ b/core/state/intra_block_state_zkevm.go @@ -101,14 +101,14 @@ func (sdb *IntraBlockState) SyncerPreExecuteStateSet( } //save block number - if chainConfig.IsNormalcy(blockNumber) { + if !chainConfig.IsNormalcy(blockNumber) { sdb.scalableSetBlockNum(blockNumber) } emptyHash := libcommon.Hash{} //ETROG if chainConfig.IsForkID7Etrog(blockNumber) { - if chainConfig.IsNormalcy(blockNumber) { + if !chainConfig.IsNormalcy(blockNumber) { currentTimestamp := sdb.ScalableGetTimestamp() if blockTimestamp > currentTimestamp { sdb.ScalableSetTimestamp(blockTimestamp) From 8bb79b24475f7178911d0be7d6725b1dedcf54f7 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Tue, 5 Nov 2024 15:26:11 +0530 Subject: [PATCH 3/5] apply feedback --- core/blockchain_zkevm.go | 34 ++++++++++------------ zk/stages/stage_sequence_execute_blocks.go | 4 +-- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/core/blockchain_zkevm.go b/core/blockchain_zkevm.go index 7fe1d98a971..de39378f17b 100644 --- a/core/blockchain_zkevm.go +++ b/core/blockchain_zkevm.go @@ -158,27 +158,25 @@ func ExecuteBlockEphemerallyZk( } var l2InfoRoot *libcommon.Hash - if !chainConfig.IsNormalcy(blockNum) { - if chainConfig.IsForkID7Etrog(blockNum) { - l2InfoRoot, err = blockinfo.BuildBlockInfoTree( - &header.Coinbase, - header.Number.Uint64(), - header.Time, - blockGasLimit, - *usedGas, - *ger, - *l1Blockhash, - *prevBlockRoot, - &txInfos, - ) - if err != nil { - return nil, err - } + if chainConfig.IsForkID7Etrog(blockNum) { + l2InfoRoot, err = blockinfo.BuildBlockInfoTree( + &header.Coinbase, + header.Number.Uint64(), + header.Time, + blockGasLimit, + *usedGas, + *ger, + *l1Blockhash, + *prevBlockRoot, + &txInfos, + ) + if err != nil { + return nil, err } - - ibs.PostExecuteStateSet(chainConfig, block.NumberU64(), l2InfoRoot) } + ibs.PostExecuteStateSet(chainConfig, block.NumberU64(), l2InfoRoot) + receiptSha := types.DeriveSha(receipts) // [zkevm] todo //if !vmConfig.StatelessExec && chainConfig.IsByzantium(header.Number.Uint64()) && !vmConfig.NoReceipts && receiptSha != block.ReceiptHash() { diff --git a/zk/stages/stage_sequence_execute_blocks.go b/zk/stages/stage_sequence_execute_blocks.go index e56fa841369..282adc7c41b 100644 --- a/zk/stages/stage_sequence_execute_blocks.go +++ b/zk/stages/stage_sequence_execute_blocks.go @@ -283,9 +283,7 @@ func postBlockStateHandling( return err } - if !cfg.chainConfig.IsNormalcy(header.Number.Uint64()) { - ibs.PostExecuteStateSet(cfg.chainConfig, header.Number.Uint64(), blockInfoRootHash) - } + ibs.PostExecuteStateSet(cfg.chainConfig, header.Number.Uint64(), blockInfoRootHash) // store a reference to this block info root against the block number return hermezDb.WriteBlockInfoRoot(header.Number.Uint64(), *blockInfoRootHash) From 8b205f37c8317ab02c48697d684ce2b0e14c2092 Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 5 Nov 2024 16:12:07 -0800 Subject: [PATCH 4/5] Fix nil state root when there isn't changes in-between blocks --- consensus/ethash/consensus.go | 3 +++ core/blockchain_zkevm.go | 2 +- smt/pkg/smt/entity_storage.go | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index a6bfdf617df..77fa625b411 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -663,6 +663,9 @@ func AccumulateRewards(config *chain.Config, header *types.Header, uncles []*typ // accumulateRewards retrieves rewards for a block and applies them to the coinbase accounts for miner and uncle miners func accumulateRewards(config *chain.Config, state *state.IntraBlockState, header *types.Header, uncles []*types.Header) { + if config.IsNormalcy(header.Number.Uint64()) { + return + } minerReward, uncleRewards := AccumulateRewards(config, header, uncles) for i, uncle := range uncles { if i < len(uncleRewards) { diff --git a/core/blockchain_zkevm.go b/core/blockchain_zkevm.go index de39378f17b..10b61a2972a 100644 --- a/core/blockchain_zkevm.go +++ b/core/blockchain_zkevm.go @@ -134,7 +134,7 @@ func ExecuteBlockEphemerallyZk( receipts = append(receipts, receipt) } } - if !chainConfig.IsForkID7Etrog(block.NumberU64()) { + if !chainConfig.IsForkID7Etrog(block.NumberU64()) && !chainConfig.IsNormalcy(block.NumberU64()) { if err := ibs.ScalableSetSmtRootHash(roHermezDb); err != nil { return nil, err } diff --git a/smt/pkg/smt/entity_storage.go b/smt/pkg/smt/entity_storage.go index e33a6d06357..0751bae92e8 100644 --- a/smt/pkg/smt/entity_storage.go +++ b/smt/pkg/smt/entity_storage.go @@ -188,6 +188,10 @@ func (s *SMT) SetContractStorage(ethAddr string, storage map[string]string, prog } func (s *SMT) SetStorage(ctx context.Context, logPrefix string, accChanges map[libcommon.Address]*accounts.Account, codeChanges map[libcommon.Address]string, storageChanges map[libcommon.Address]map[string]string) ([]*utils.NodeKey, []*utils.NodeValue8, error) { + if len(storageChanges) == 0 && len(accChanges) == 0 && len(codeChanges) == 0 { + return nil, nil, nil + } + var isDelete bool var err error From 0f9da4c09cef170ecdba15620aa81dc6ff375326 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Wed, 6 Nov 2024 17:40:18 +0530 Subject: [PATCH 5/5] apply feedback --- core/state/intra_block_state_zkevm.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/state/intra_block_state_zkevm.go b/core/state/intra_block_state_zkevm.go index 691a02faf5c..36eb275f270 100644 --- a/core/state/intra_block_state_zkevm.go +++ b/core/state/intra_block_state_zkevm.go @@ -58,8 +58,12 @@ func (sdb *IntraBlockState) GetTxCount() (uint64, error) { } func (sdb *IntraBlockState) PostExecuteStateSet(chainConfig *chain.Config, blockNum uint64, blockInfoRoot *libcommon.Hash) { + if chainConfig.IsNormalcy(blockNum) { + return + } + //ETROG - if chainConfig.IsForkID7Etrog(blockNum) && !chainConfig.IsNormalcy(blockNum) { + if chainConfig.IsForkID7Etrog(blockNum) { sdb.scalableSetBlockInfoRoot(blockInfoRoot) } }