go-ethereum icon indicating copy to clipboard operation
go-ethereum copied to clipboard

SimulatedBackend: Cannot use `AdjustTime` after `Fork`

Open DragonDev1906 opened this issue 3 years ago • 1 comments

System information

OS & Version: Linux Commit hash : de1cecb22e2a18ad70d4cb92bee122f4549c5b79 (master)

Expected behaviour

When using AdjustTime directly after a Fork, the following Commit should still create a block in the side-chain, the same goes for all other AdjustTime and Commit calls afterwards.

Actual behaviour

AdjustTime uses the current head (b.blockchain.CurrentBlock()), which is the canonical head and thus not the block we just rolled back to when calling Fork. Thus the following call to Commit creates a block with parent h3 instead of with parent h1, resulting in the same bahavior as if Fork was never called.

Fix

Use b.blockchain.GetBlockByHash(b.pendingBlock.ParentHash()) instead of b.blockchain.CurrentBlock() in SimulatedBackend.AdjustTime.

Steps to reproduce the behaviour

// Create a new SimulatedBackend (should be possible without allocations)
sb := backends.NewSimulatedBackend(
		core.GenesisAlloc{},
		30_000_000,
	)
sb.Commit() // h1
h1 := sb.Blockchain().CurrentHeader().Hash()
sb.Commit() // h2
sb.Fork(context.Background(), h1)
sb.AdjustTime(1 * time.Second)
sb.Commit() // Should have h1 as parent but has h2

DragonDev1906 avatar Jul 01 '22 13:07 DragonDev1906