Skip to content

Commit 9ece3e7

Browse files
MariusVanDerWijdenenriquefynn
authored andcommitted
accounts/abi/bind/backends: Disallow AdjustTime for non-empty blocks (ethereum#21334)
* accounts/abi/bind/backends: Disallow timeshift for non-empty blocks * accounts/abi/bind/backends: added tests for adjust time * accounts/abi/bind/simulated: added comments, fixed test for AdjustTime * accounts/abi/bind/backends: updated comment
1 parent 01f3482 commit 9ece3e7

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,14 +675,16 @@ func (b *SimulatedBackend) SubscribeNewHead(ctx context.Context, ch chan<- *type
675675
}
676676

677677
// AdjustTime adds a time shift to the simulated clock.
678+
// It can only be called on empty blocks.
678679
func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
679680
b.mu.Lock()
680681
defer b.mu.Unlock()
681682

683+
if len(b.pendingBlock.Transactions()) != 0 {
684+
return errors.New("Could not adjust time on non-empty block")
685+
}
686+
682687
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {
683-
for _, tx := range b.pendingBlock.Transactions() {
684-
block.AddTx(tx)
685-
}
686688
block.OffsetTime(int64(adjustment.Seconds()))
687689
})
688690
statedb, _ := b.blockchain.State()

accounts/abi/bind/backends/simulated_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ func TestSimulatedBackend_AdjustTime(t *testing.T) {
143143
defer sim.Close()
144144

145145
prevTime := sim.pendingBlock.Time()
146-
err := sim.AdjustTime(time.Second)
147-
if err != nil {
146+
if err := sim.AdjustTime(time.Second); err != nil {
148147
t.Error(err)
149148
}
150149
newTime := sim.pendingBlock.Time()
@@ -154,6 +153,44 @@ func TestSimulatedBackend_AdjustTime(t *testing.T) {
154153
}
155154
}
156155

156+
func TestNewSimulatedBackend_AdjustTimeFail(t *testing.T) {
157+
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
158+
sim := simTestBackend(testAddr)
159+
// Create tx and send
160+
tx := types.NewTransaction(0, testAddr, big.NewInt(1000), params.TxGas, big.NewInt(1), nil)
161+
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, testKey)
162+
if err != nil {
163+
t.Errorf("could not sign tx: %v", err)
164+
}
165+
sim.SendTransaction(context.Background(), signedTx)
166+
// AdjustTime should fail on non-empty block
167+
if err := sim.AdjustTime(time.Second); err == nil {
168+
t.Error("Expected adjust time to error on non-empty block")
169+
}
170+
sim.Commit()
171+
172+
prevTime := sim.pendingBlock.Time()
173+
if err := sim.AdjustTime(time.Minute); err != nil {
174+
t.Error(err)
175+
}
176+
newTime := sim.pendingBlock.Time()
177+
if newTime-prevTime != uint64(time.Minute.Seconds()) {
178+
t.Errorf("adjusted time not equal to a minute. prev: %v, new: %v", prevTime, newTime)
179+
}
180+
// Put a transaction after adjusting time
181+
tx2 := types.NewTransaction(1, testAddr, big.NewInt(1000), params.TxGas, big.NewInt(1), nil)
182+
signedTx2, err := types.SignTx(tx2, types.HomesteadSigner{}, testKey)
183+
if err != nil {
184+
t.Errorf("could not sign tx: %v", err)
185+
}
186+
sim.SendTransaction(context.Background(), signedTx2)
187+
sim.Commit()
188+
newTime = sim.pendingBlock.Time()
189+
if newTime-prevTime >= uint64(time.Minute.Seconds()) {
190+
t.Errorf("time adjusted, but shouldn't be: prev: %v, new: %v", prevTime, newTime)
191+
}
192+
}
193+
157194
func TestSimulatedBackend_BalanceAt(t *testing.T) {
158195
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
159196
expectedBal := big.NewInt(10000000000)

0 commit comments

Comments
 (0)