From 3747a9c0d60fae82633570e6c8c7b401cce50bdb Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Tue, 7 May 2024 18:52:38 -0300 Subject: [PATCH 1/6] replay changes for old replay data Signed-off-by: Ignacio Hagopian --- core/vm/contracts.go | 2 +- core/vm/evm.go | 4 ++-- core/vm/interpreter.go | 6 +++--- params/config.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 75fc80e3c18..0065757dd71 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -152,7 +152,7 @@ func ActivePrecompiles(rules params.Rules) []common.Address { switch { case rules.IsPrague: // Note: this config should be correctly set depending on replay starting point. - return PrecompiledAddressesBerlin + return PrecompiledAddressesByzantium case rules.IsCancun: return PrecompiledAddressesCancun case rules.IsBerlin: diff --git a/core/vm/evm.go b/core/vm/evm.go index 489697eec39..603ad5dc6ef 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -43,12 +43,12 @@ func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { switch { case evm.chainRules.IsCancun: precompiles = PrecompiledContractsCancun - case evm.chainRules.IsPrague: - precompiles = PrecompiledContractsBerlin case evm.chainRules.IsBerlin: precompiles = PrecompiledContractsBerlin case evm.chainRules.IsIstanbul: precompiles = PrecompiledContractsIstanbul + case evm.chainRules.IsPrague: + precompiles = PrecompiledContractsByzantium case evm.chainRules.IsByzantium: precompiles = PrecompiledContractsByzantium default: diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index e61b464c062..959650e63ea 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -58,9 +58,6 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { switch { case evm.chainRules.IsCancun: table = &cancunInstructionSet - case evm.chainRules.IsPrague: - // TODO replace with proper instruction set when fork is specified - table = &shanghaiInstructionSet case evm.chainRules.IsShanghai: table = &shanghaiInstructionSet case evm.chainRules.IsMerge: @@ -73,6 +70,9 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { table = &istanbulInstructionSet case evm.chainRules.IsConstantinople: table = &constantinopleInstructionSet + case evm.chainRules.IsPrague: + // TODO replace with proper instruction set when fork is specified + table = &byzantiumInstructionSet case evm.chainRules.IsByzantium: table = &byzantiumInstructionSet case evm.chainRules.IsEIP158: diff --git a/params/config.go b/params/config.go index 6c9e95197fd..5b55c519770 100644 --- a/params/config.go +++ b/params/config.go @@ -505,7 +505,7 @@ func (c *ChainConfig) IsCancun(num *big.Int, time uint64) bool { // IsPrague returns whether num is either equal to the Prague fork time or greater. func (c *ChainConfig) IsPrague(num *big.Int, time uint64) bool { - return c.IsShanghai(num, time) && isTimestampForked(c.PragueTime, time) + return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) } // CheckCompatible checks whether scheduled fork transitions have been imported From a190055ef73a4120ef34d810195d4931ca00df44 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Tue, 7 May 2024 19:05:34 -0300 Subject: [PATCH 2/6] more adjustements Signed-off-by: Ignacio Hagopian --- core/state/database.go | 2 +- params/config.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index 826c03cd9f0..7ffb7a4073d 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -415,7 +415,7 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre } } if db.InTransition() { - fmt.Printf("OpenStorageTrie during transition, state root=%x root=%x\n", stateRoot, root) + // fmt.Printf("OpenStorageTrie during transition, state root=%x root=%x\n", stateRoot, root) mpt, err := db.openStorageMPTrie(db.LastMerkleRoot, address, root, nil) if err != nil { return nil, err diff --git a/params/config.go b/params/config.go index 5b55c519770..d680c5bf0f5 100644 --- a/params/config.go +++ b/params/config.go @@ -505,7 +505,7 @@ func (c *ChainConfig) IsCancun(num *big.Int, time uint64) bool { // IsPrague returns whether num is either equal to the Prague fork time or greater. func (c *ChainConfig) IsPrague(num *big.Int, time uint64) bool { - return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) + return c.IsByzantium(num) && isTimestampForked(c.PragueTime, time) } // CheckCompatible checks whether scheduled fork transitions have been imported From 28865fedb05e2f7ced36e07dccf6fb45e3f8be7b Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Tue, 7 May 2024 20:17:05 -0300 Subject: [PATCH 3/6] fix removed gas cost rules Signed-off-by: Ignacio Hagopian --- core/vm/evm.go | 2 +- core/vm/gas_table.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index 603ad5dc6ef..cacf41150c7 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -511,7 +511,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, // be stored due to not enough gas set an error and let it be handled // by the error checking condition below. if err == nil { - if !evm.chainRules.IsEIP4762 { + if !evm.chainRules.IsEIP4762 || replayMode { createDataGas := uint64(len(ret)) * params.CreateDataGas if !contract.UseGas(createDataGas) { err = ErrCodeStoreOutOfGas diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 5f8183801f1..4f5b19e93c5 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -377,6 +377,8 @@ func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor return gas, nil } +var replayMode = true + func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { var ( gas uint64 @@ -391,7 +393,7 @@ func gasCall(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize } else if !evm.StateDB.Exist(address) { gas += params.CallNewAccountGas } - if transfersValue && !evm.chainRules.IsEIP4762 { + if transfersValue && (!evm.chainRules.IsEIP4762 || replayMode) { gas += params.CallValueTransferGas } memoryGas, err := memoryGasCost(mem, memorySize) @@ -431,7 +433,7 @@ func gasCallCode(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memory gas uint64 overflow bool ) - if stack.Back(2).Sign() != 0 && !evm.chainRules.IsEIP4762 { + if stack.Back(2).Sign() != 0 && (!evm.chainRules.IsEIP4762 || replayMode) { gas += params.CallValueTransferGas } if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { From b9e361c719d9d5a202e3c7718ec45e0f2ad8aeb5 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 8 May 2024 14:25:32 -0300 Subject: [PATCH 4/6] things working now Signed-off-by: Ignacio Hagopian --- core/blockchain.go | 4 ++-- core/state_processor.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index f92d174ccf1..c0a34c6ec62 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -322,8 +322,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis // for it to be able to recover if interrupted during the transition // but that's left out to a later PR since there's not really a need // right now. - bc.stateCache.InitTransitionStatus(true, true) - bc.stateCache.EndVerkleTransition() + bc.stateCache.InitTransitionStatus(false, false) + // bc.stateCache.EndVerkleTransition() } if !bc.HasState(head.Root) { diff --git a/core/state_processor.go b/core/state_processor.go index d6a01673c6a..3192692e379 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -185,7 +185,7 @@ func InsertBlockHashHistoryAtEip2935Fork(statedb *state.StateDB, prevNumber uint statedb.Witness().TouchFullAccount(params.HistoryStorageAddress[:], true) ancestor := chain.GetHeader(prevHash, prevNumber) - for i := prevNumber; i > 0 && i >= prevNumber-params.Eip2935BlockHashHistorySize; i-- { + for i := prevNumber; i > 0 && i > prevNumber-params.Eip2935BlockHashHistorySize; i-- { ProcessParentBlockHash(statedb, i, ancestor.Hash()) ancestor = chain.GetHeader(ancestor.ParentHash, ancestor.Number.Uint64()-1) } From cd92db32b64ff835988e5fbc18753e3318f267a9 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Thu, 30 May 2024 13:59:42 -0300 Subject: [PATCH 5/6] byzantium is not PoS Signed-off-by: Ignacio Hagopian --- consensus/beacon/consensus.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index e40c180aa42..d3e9be2c60a 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -28,7 +28,6 @@ import ( "github.com/ethereum/go-ethereum/core/overlay" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/trie" @@ -346,6 +345,14 @@ func (beacon *Beacon) Prepare(chain consensus.ChainHeaderReader, header *types.H // Finalize implements consensus.Engine and processes withdrawals on top. func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, withdrawals []*types.Withdrawal) { + if chain.Config().IsPrague(header.Number, header.Time) { + // uncomment when debugging + // fmt.Println("at block", header.Number, "performing transition?", state.Database().InTransition()) + parent := chain.GetHeaderByHash(header.ParentHash) + if err := overlay.OverlayVerkleTransition(state, parent.Root, chain.Config().OverlayStride); err != nil { + panic(err) + } + } if !beacon.IsPoSHeader(header) { beacon.ethone.Finalize(chain, header, state, txs, uncles, nil) return @@ -361,14 +368,6 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types. state.Witness().TouchFullAccount(w.Address[:], true) } - if chain.Config().IsPrague(header.Number, header.Time) { - // uncomment when debugging - // fmt.Println("at block", header.Number, "performing transition?", state.Database().InTransition()) - parent := chain.GetHeaderByHash(header.ParentHash) - if err := overlay.OverlayVerkleTransition(state, parent.Root, chain.Config().OverlayStride); err != nil { - log.Error("error performing the transition", "err", err) - } - } } // FinalizeAndAssemble implements consensus.Engine, setting the final state and From 3ee27100b4c29244a0e3740037d7d09e3d965f69 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 7 Jun 2024 11:33:25 -0300 Subject: [PATCH 6/6] add byzantium Seek trick to replay faster Signed-off-by: Ignacio Hagopian --- cmd/utils/cmd.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index de25fd1a146..9e8f5ffaa5f 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -170,6 +170,10 @@ func ImportChain(chain *core.BlockChain, fn string) error { } defer fh.Close() + if _, err := fh.Seek(18224628422, 0); err != nil { + panic(err) + } + var reader io.Reader = fh if strings.HasSuffix(fn, ".gz") { if reader, err = gzip.NewReader(reader); err != nil {