From 14cbc01683e64e3a8934cfe4471556b575af29be Mon Sep 17 00:00:00 2001 From: ledgerwatch <akhounov@gmail.com> Date: Sun, 1 May 2022 08:44:47 +0100 Subject: [PATCH] Fix for Bor (Polygon) (#4044) * print branchHash * Print state changes * Print val * Fix for author * Remove prints Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local> --- core/blockchain.go | 27 +++++++++++++++++---------- core/state/plain_state_writer.go | 2 ++ eth/stagedsync/stage_mining_exec.go | 4 +++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 23a67c4c36..7e123eefc5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -114,7 +114,7 @@ func ExecuteBlockEphemerallyForBSC( gp.AddGas(block.GasLimit()) if !vmConfig.ReadOnly { - if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs); err != nil { + if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs, false /* isBor */); err != nil { return nil, err } } @@ -175,7 +175,7 @@ func ExecuteBlockEphemerallyForBSC( // otherwise it causes block verification error. header.GasUsed = *usedGas syscall := func(contract common.Address, data []byte) ([]byte, error) { - return SysCallContract(contract, data, *chainConfig, ibs, header, engine) + return SysCallContract(contract, data, *chainConfig, ibs, header, engine, false /* isBor */) } outTxs, outReceipts, err := engine.Finalize(chainConfig, header, ibs, block.Transactions(), block.Uncles(), receipts, epochReader, chainReader, syscall) if err != nil { @@ -239,9 +239,10 @@ func ExecuteBlockEphemerally( usedGas := new(uint64) gp := new(GasPool) gp.AddGas(block.GasLimit()) + isBor := chainConfig.Bor != nil if !vmConfig.ReadOnly { - if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs); err != nil { + if err := InitializeBlockExecution(engine, chainReader, epochReader, block.Header(), block.Transactions(), block.Uncles(), chainConfig, ibs, isBor); err != nil { return nil, nil, err } } @@ -301,7 +302,7 @@ func ExecuteBlockEphemerally( } if !vmConfig.ReadOnly { txs := block.Transactions() - if _, err := FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, epochReader, chainReader, false); err != nil { + if _, err := FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, epochReader, chainReader, false, isBor); err != nil { return nil, nil, err } } @@ -334,7 +335,7 @@ func ExecuteBlockEphemerally( return receipts, stateSyncReceipt, nil } -func SysCallContract(contract common.Address, data []byte, chainConfig params.ChainConfig, ibs *state.IntraBlockState, header *types.Header, engine consensus.Engine) (result []byte, err error) { +func SysCallContract(contract common.Address, data []byte, chainConfig params.ChainConfig, ibs *state.IntraBlockState, header *types.Header, engine consensus.Engine, isBor bool) (result []byte, err error) { gp := new(GasPool).AddGas(50_000_000) if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(header.Number) == 0 { @@ -351,7 +352,13 @@ func SysCallContract(contract common.Address, data []byte, chainConfig params.Ch ) vmConfig := vm.Config{NoReceipts: true} // Create a new context to be used in the EVM environment - blockContext := NewEVMBlockContext(header, nil, engine, &state.SystemAddress, nil) + var author *common.Address + if isBor { + author = &header.Coinbase + } else { + author = &state.SystemAddress + } + blockContext := NewEVMBlockContext(header, nil, engine, author, nil) evm := vm.NewEVM(blockContext, NewEVMTxContext(msg), ibs, &chainConfig, vmConfig) if chainConfig.Bor != nil { ret, _, err := evm.Call( @@ -412,10 +419,10 @@ func CallContractTx(contract common.Address, data []byte, ibs *state.IntraBlockS func FinalizeBlockExecution(engine consensus.Engine, stateReader state.StateReader, header *types.Header, txs types.Transactions, uncles []*types.Header, stateWriter state.WriterWithChangeSets, cc *params.ChainConfig, ibs *state.IntraBlockState, - receipts types.Receipts, e consensus.EpochReader, headerReader consensus.ChainHeaderReader, isMining bool, + receipts types.Receipts, e consensus.EpochReader, headerReader consensus.ChainHeaderReader, isMining bool, isBor bool, ) (newBlock *types.Block, err error) { syscall := func(contract common.Address, data []byte) ([]byte, error) { - return SysCallContract(contract, data, *cc, ibs, header, engine) + return SysCallContract(contract, data, *cc, ibs, header, engine, isBor) } if isMining { newBlock, _, _, err = engine.FinalizeAndAssemble(cc, header, ibs, txs, uncles, receipts, e, headerReader, syscall, nil) @@ -456,10 +463,10 @@ func FinalizeBlockExecution(engine consensus.Engine, stateReader state.StateRead return newBlock, nil } -func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHeaderReader, epochReader consensus.EpochReader, header *types.Header, txs types.Transactions, uncles []*types.Header, cc *params.ChainConfig, ibs *state.IntraBlockState) error { +func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHeaderReader, epochReader consensus.EpochReader, header *types.Header, txs types.Transactions, uncles []*types.Header, cc *params.ChainConfig, ibs *state.IntraBlockState, isBor bool) error { // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) engine.Initialize(cc, chain, epochReader, header, txs, uncles, func(contract common.Address, data []byte) ([]byte, error) { - return SysCallContract(contract, data, *cc, ibs, header, engine) + return SysCallContract(contract, data, *cc, ibs, header, engine, isBor) }) return nil } diff --git a/core/state/plain_state_writer.go b/core/state/plain_state_writer.go index 68b4a05908..c8c7d3f7c8 100644 --- a/core/state/plain_state_writer.go +++ b/core/state/plain_state_writer.go @@ -42,6 +42,7 @@ func (w *PlainStateWriter) SetAccumulator(accumulator *shards.Accumulator) *Plai } func (w *PlainStateWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error { + //fmt.Printf("UpdateAccount [%x] hashed [%x]\n", address, crypto.Keccak256(address[:])) if w.csw != nil { if err := w.csw.UpdateAccountData(address, original, account); err != nil { return err @@ -93,6 +94,7 @@ func (w *PlainStateWriter) DeleteAccount(address common.Address, original *accou } func (w *PlainStateWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error { + //fmt.Printf("WriteAccountStorage [%x] hashed [%x],loc [%x] hashed [%x], val [%x]\n", address, crypto.Keccak256(address[:]), *key, crypto.Keccak256((*key)[:]), value.Bytes()) if w.csw != nil { if err := w.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil { return err diff --git a/eth/stagedsync/stage_mining_exec.go b/eth/stagedsync/stage_mining_exec.go index ee605bfc4e..0f608705ad 100644 --- a/eth/stagedsync/stage_mining_exec.go +++ b/eth/stagedsync/stage_mining_exec.go @@ -124,7 +124,9 @@ func SpawnMiningExecStage(s *StageState, tx kv.RwTx, cfg MiningExecCfg, quit <-c current.Receipts = types.Receipts{} } - _, err := core.FinalizeBlockExecution(cfg.engine, stateReader, current.Header, current.Txs, current.Uncles, stateWriter, &cfg.chainConfig, ibs, current.Receipts, epochReader{tx: tx}, chainReader{config: &cfg.chainConfig, tx: tx, blockReader: cfg.blockReader}, true) + _, err := core.FinalizeBlockExecution(cfg.engine, stateReader, current.Header, current.Txs, current.Uncles, stateWriter, + &cfg.chainConfig, ibs, current.Receipts, epochReader{tx: tx}, chainReader{config: &cfg.chainConfig, tx: tx, blockReader: cfg.blockReader}, true, + cfg.chainConfig.Bor != nil) if err != nil { return err } -- GitLab