@@ -95,7 +95,10 @@ func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.Genesis
9595 backend .filterSystem = filters .NewFilterSystem (filterBackend , filters.Config {})
9696 backend .events = filters .NewEventSystem (backend .filterSystem , false )
9797
98- backend .rollback (blockchain .CurrentBlock ())
98+ header := backend .blockchain .CurrentBlock ()
99+ block := backend .blockchain .GetBlock (header .Hash (), header .Number .Uint64 ())
100+
101+ backend .rollback (block )
99102 return backend
100103}
101104
@@ -135,7 +138,10 @@ func (b *SimulatedBackend) Rollback() {
135138 b .mu .Lock ()
136139 defer b .mu .Unlock ()
137140
138- b .rollback (b .blockchain .CurrentBlock ())
141+ header := b .blockchain .CurrentBlock ()
142+ block := b .blockchain .GetBlock (header .Hash (), header .Number .Uint64 ())
143+
144+ b .rollback (block )
139145}
140146
141147func (b * SimulatedBackend ) rollback (parent * types.Block ) {
@@ -174,7 +180,7 @@ func (b *SimulatedBackend) Fork(ctx context.Context, parent common.Hash) error {
174180
175181// stateByBlockNumber retrieves a state by a given blocknumber.
176182func (b * SimulatedBackend ) stateByBlockNumber (ctx context.Context , blockNumber * big.Int ) (* state.StateDB , error ) {
177- if blockNumber == nil || blockNumber .Cmp (b .blockchain .CurrentBlock ().Number () ) == 0 {
183+ if blockNumber == nil || blockNumber .Cmp (b .blockchain .CurrentBlock ().Number ) == 0 {
178184 return b .blockchain .State ()
179185 }
180186 block , err := b .blockByNumber (ctx , blockNumber )
@@ -303,7 +309,7 @@ func (b *SimulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (
303309// (associated with its hash) if found without Lock.
304310func (b * SimulatedBackend ) blockByNumber (ctx context.Context , number * big.Int ) (* types.Block , error ) {
305311 if number == nil || number .Cmp (b .pendingBlock .Number ()) == 0 {
306- return b .blockchain .CurrentBlock (), nil
312+ return b .blockByHash ( ctx , b . blockchain .CurrentBlock (). Hash ())
307313 }
308314
309315 block := b .blockchain .GetBlockByNumber (uint64 (number .Int64 ()))
@@ -431,7 +437,7 @@ func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallM
431437 b .mu .Lock ()
432438 defer b .mu .Unlock ()
433439
434- if blockNumber != nil && blockNumber .Cmp (b .blockchain .CurrentBlock ().Number () ) != 0 {
440+ if blockNumber != nil && blockNumber .Cmp (b .blockchain .CurrentBlock ().Number ) != 0 {
435441 return nil , errBlockNumberUnsupported
436442 }
437443 stateDB , err := b .blockchain .State ()
@@ -455,7 +461,7 @@ func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereu
455461 defer b .mu .Unlock ()
456462 defer b .pendingState .RevertToSnapshot (b .pendingState .Snapshot ())
457463
458- res , err := b .callContract (ctx , call , b .pendingBlock , b .pendingState )
464+ res , err := b .callContract (ctx , call , b .pendingBlock . Header () , b .pendingState )
459465 if err != nil {
460466 return nil , err
461467 }
@@ -549,7 +555,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
549555 call .Gas = gas
550556
551557 snapshot := b .pendingState .Snapshot ()
552- res , err := b .callContract (ctx , call , b .pendingBlock , b .pendingState )
558+ res , err := b .callContract (ctx , call , b .pendingBlock . Header () , b .pendingState )
553559 b .pendingState .RevertToSnapshot (snapshot )
554560
555561 if err != nil {
@@ -599,7 +605,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
599605
600606// callContract implements common code between normal and pending contract calls.
601607// state is modified during execution, make sure to copy it if necessary.
602- func (b * SimulatedBackend ) callContract (ctx context.Context , call ethereum.CallMsg , block * types.Block , stateDB * state.StateDB ) (* core.ExecutionResult , error ) {
608+ func (b * SimulatedBackend ) callContract (ctx context.Context , call ethereum.CallMsg , header * types.Header , stateDB * state.StateDB ) (* core.ExecutionResult , error ) {
603609 // Gas prices post 1559 need to be initialized
604610 if call .GasPrice != nil && (call .GasFeeCap != nil || call .GasTipCap != nil ) {
605611 return nil , errors .New ("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified" )
@@ -645,7 +651,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
645651 msg := callMsg {call }
646652
647653 txContext := core .NewEVMTxContext (msg )
648- evmContext := core .NewEVMBlockContext (block . Header () , b .blockchain , nil )
654+ evmContext := core .NewEVMBlockContext (header , b .blockchain , nil )
649655 // Create a new environment which holds all relevant information
650656 // about the transaction and calling mechanisms.
651657 vmEnv := vm .NewEVM (evmContext , txContext , stateDB , b .config , vm.Config {NoBaseFee : true })
@@ -854,15 +860,9 @@ func (fb *filterBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNum
854860 case rpc .LatestBlockNumber :
855861 return fb .bc .CurrentHeader (), nil
856862 case rpc .FinalizedBlockNumber :
857- if block := fb .bc .CurrentFinalizedBlock (); block != nil {
858- return block .Header (), nil
859- }
860- return nil , errors .New ("finalized block not found" )
863+ return fb .bc .CurrentFinalBlock (), nil
861864 case rpc .SafeBlockNumber :
862- if block := fb .bc .CurrentSafeBlock (); block != nil {
863- return block .Header (), nil
864- }
865- return nil , errors .New ("safe block not found" )
865+ return fb .bc .CurrentSafeBlock (), nil
866866 default :
867867 return fb .bc .GetHeaderByNumber (uint64 (number .Int64 ())), nil
868868 }
0 commit comments