Skip to content

Commit

Permalink
accounts/abi/bind/backends: return hash of new blocks (ethereum#25163)
Browse files Browse the repository at this point in the history
Co-authored-by: Jens <jmw.1906@gmx.de>
  • Loading branch information
2 people authored and blakehhuynh committed Oct 3, 2022
1 parent 563d3b3 commit fad30bb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
6 changes: 5 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (b *SimulatedBackend) Close() error {

// Commit imports all the pending transactions as a single block and starts a
// fresh new state.
func (b *SimulatedBackend) Commit() {
func (b *SimulatedBackend) Commit() common.Hash {
b.mu.Lock()
defer b.mu.Unlock()

Expand All @@ -131,10 +131,14 @@ func (b *SimulatedBackend) Commit() {
if _, err := b.blockchain.InsertChain([]*types.Block{blocks[0]}); err != nil {
panic(err) // This cannot happen unless the simulator is wrong, fail in that case
}
blockHash := b.pendingBlock.Hash()

// Using the last inserted block here makes it possible to build on a side
// chain after a fork.
b.stuckTransactions = remainingTx
b.rollback(blocks[0])

return blockHash
}

// Rollback aborts all pending transactions, reverting to the last committed state.
Expand Down
60 changes: 49 additions & 11 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,15 +1354,15 @@ func TestLowGasTxNotBeingMined(t *testing.T) {
defer sim.Close()

var testCases = []struct {
name string
txType byte
name string
txType byte
}{
{
name: "LegacyTx",
name: "LegacyTx",
txType: types.LegacyTxType,
},
{
name: "dynamicTx",
name: "dynamicTx",
txType: types.DynamicFeeTxType,
},
}
Expand Down Expand Up @@ -1413,7 +1413,7 @@ func TestLowGasTxNotBeingMined(t *testing.T) {
// expect nonce be the same because low gas fee tx will not be mined
nonce, err := sim.PendingNonceAt(bgCtx, testAddr)
require.NoError(t, err)
assert.Equal(t, uint64(i), nonce)
assert.Equal(t, uint64(i), nonce)

// send tx with higher gas fee
sufficientGasFeeTx := types.NewTx(&types.LegacyTx{
Expand All @@ -1439,7 +1439,7 @@ func TestLowGasTxNotBeingMined(t *testing.T) {
// expect nonce has increased
nonce, err = sim.PendingNonceAt(bgCtx, testAddr)
require.NoError(t, err)
assert.Equal(t, uint64(i) + 1, nonce)
assert.Equal(t, uint64(i)+1, nonce)
})
}
}
Expand All @@ -1453,15 +1453,15 @@ func TestLowGasTxNotBeingMined(t *testing.T) {
// 4. Check tx get mined
func TestLowGasTxGetMinedOnceGasFeeDropped(t *testing.T) {
var testCases = []struct {
name string
txType byte
name string
txType byte
}{
{
name: "LegacyTx",
name: "LegacyTx",
txType: types.LegacyTxType,
},
{
name: "dynamicTx",
name: "dynamicTx",
txType: types.DynamicFeeTxType,
},
}
Expand Down Expand Up @@ -1527,7 +1527,7 @@ func TestLowGasTxGetMinedOnceGasFeeDropped(t *testing.T) {
// nonce has increased
pendingNonce, err = sim.PendingNonceAt(bgCtx, testAddr)
require.NoError(t, err)
assert.Equal(t, uint64(i) + 1, pendingNonce)
assert.Equal(t, uint64(i)+1, pendingNonce)

// the transaction should have been mined
_, isPending, err := sim.TransactionByHash(bgCtx, signedTx.Hash())
Expand All @@ -1536,3 +1536,41 @@ func TestLowGasTxGetMinedOnceGasFeeDropped(t *testing.T) {
})
}
}
func TestCommitReturnValue(t *testing.T) {
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
sim := simTestBackend(testAddr)
defer sim.Close()

startBlockHeight := sim.blockchain.CurrentBlock().NumberU64()

// Test if Commit returns the correct block hash
h1 := sim.Commit()
if h1 != sim.blockchain.CurrentBlock().Hash() {
t.Error("Commit did not return the hash of the last block.")
}

// Create a block in the original chain (containing a transaction to force different block hashes)
head, _ := sim.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1))
_tx := types.NewTransaction(0, testAddr, big.NewInt(1000), params.TxGas, gasPrice, nil)
tx, _ := types.SignTx(_tx, types.HomesteadSigner{}, testKey)
sim.SendTransaction(context.Background(), tx)
h2 := sim.Commit()

// Create another block in the original chain
sim.Commit()

// Fork at the first bock
if err := sim.Fork(context.Background(), h1); err != nil {
t.Errorf("forking: %v", err)
}

// Test if Commit returns the correct block hash after the reorg
h2fork := sim.Commit()
if h2 == h2fork {
t.Error("The block in the fork and the original block are the same block!")
}
if sim.blockchain.GetHeader(h2fork, startBlockHeight+2) == nil {
t.Error("Could not retrieve the just created block (side-chain)")
}
}

0 comments on commit fad30bb

Please sign in to comment.