Skip to content

Commit 7f33625

Browse files
authored
Merge pull request #19902 from karalabe/simulated-close
accounts/abi/bind: support closing a simulated backend
2 parents 96ab8e1 + 140a7e9 commit 7f33625

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBac
8888
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit)
8989
}
9090

91+
// Close terminates the underlying blockchain's update loop.
92+
func (b *SimulatedBackend) Close() error {
93+
b.blockchain.Stop()
94+
return nil
95+
}
96+
9197
// Commit imports all the pending transactions as a single block and starts a
9298
// fresh new state.
9399
func (b *SimulatedBackend) Commit() {

accounts/abi/bind/backends/simulated_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestSimulatedBackend(t *testing.T) {
3838
genAlloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(9223372036854775807)}
3939

4040
sim := backends.NewSimulatedBackend(genAlloc, gasLimit)
41+
defer sim.Close()
4142

4243
// should return an error if the tx is not found
4344
txHash := common.HexToHash("2")

accounts/abi/bind/bind_test.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ var bindTests = []struct {
282282
// Generate a new random account and a funded simulator
283283
key, _ := crypto.GenerateKey()
284284
auth := bind.NewKeyedTransactor(key)
285+
285286
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
287+
defer sim.Close()
286288
287289
// Deploy an interaction tester contract and call a transaction on it
288290
_, _, interactor, err := DeployInteractor(auth, sim, "Deploy string")
@@ -334,7 +336,9 @@ var bindTests = []struct {
334336
// Generate a new random account and a funded simulator
335337
key, _ := crypto.GenerateKey()
336338
auth := bind.NewKeyedTransactor(key)
339+
337340
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
341+
defer sim.Close()
338342
339343
// Deploy a tuple tester contract and execute a structured call on it
340344
_, _, getter, err := DeployGetter(auth, sim)
@@ -377,7 +381,9 @@ var bindTests = []struct {
377381
// Generate a new random account and a funded simulator
378382
key, _ := crypto.GenerateKey()
379383
auth := bind.NewKeyedTransactor(key)
384+
380385
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
386+
defer sim.Close()
381387
382388
// Deploy a tuple tester contract and execute a structured call on it
383389
_, _, tupler, err := DeployTupler(auth, sim)
@@ -432,7 +438,9 @@ var bindTests = []struct {
432438
// Generate a new random account and a funded simulator
433439
key, _ := crypto.GenerateKey()
434440
auth := bind.NewKeyedTransactor(key)
441+
435442
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
443+
defer sim.Close()
436444
437445
// Deploy a slice tester contract and execute a n array call on it
438446
_, _, slicer, err := DeploySlicer(auth, sim)
@@ -477,7 +485,9 @@ var bindTests = []struct {
477485
// Generate a new random account and a funded simulator
478486
key, _ := crypto.GenerateKey()
479487
auth := bind.NewKeyedTransactor(key)
488+
480489
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
490+
defer sim.Close()
481491
482492
// Deploy a default method invoker contract and execute its default method
483493
_, _, defaulter, err := DeployDefaulter(auth, sim)
@@ -519,7 +529,9 @@ var bindTests = []struct {
519529
`,
520530
`
521531
// Create a simulator and wrap a non-deployed contract
532+
522533
sim := backends.NewSimulatedBackend(core.GenesisAlloc{}, uint64(10000000000))
534+
defer sim.Close()
523535
524536
nonexistent, err := NewNonExistent(common.Address{}, sim)
525537
if err != nil {
@@ -566,7 +578,9 @@ var bindTests = []struct {
566578
// Generate a new random account and a funded simulator
567579
key, _ := crypto.GenerateKey()
568580
auth := bind.NewKeyedTransactor(key)
581+
569582
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
583+
defer sim.Close()
570584
571585
// Deploy a funky gas pattern contract
572586
_, _, limiter, err := DeployFunkyGasPattern(auth, sim)
@@ -613,7 +627,9 @@ var bindTests = []struct {
613627
// Generate a new random account and a funded simulator
614628
key, _ := crypto.GenerateKey()
615629
auth := bind.NewKeyedTransactor(key)
630+
616631
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
632+
defer sim.Close()
617633
618634
// Deploy a sender tester contract and execute a structured call on it
619635
_, _, callfrom, err := DeployCallFrom(auth, sim)
@@ -685,7 +701,9 @@ var bindTests = []struct {
685701
// Generate a new random account and a funded simulator
686702
key, _ := crypto.GenerateKey()
687703
auth := bind.NewKeyedTransactor(key)
704+
688705
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
706+
defer sim.Close()
689707
690708
// Deploy a underscorer tester contract and execute a structured call on it
691709
_, _, underscorer, err := DeployUnderscorer(auth, sim)
@@ -776,7 +794,9 @@ var bindTests = []struct {
776794
// Generate a new random account and a funded simulator
777795
key, _ := crypto.GenerateKey()
778796
auth := bind.NewKeyedTransactor(key)
797+
779798
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
799+
defer sim.Close()
780800
781801
// Deploy an eventer contract
782802
_, _, eventer, err := DeployEventer(auth, sim)
@@ -963,7 +983,9 @@ var bindTests = []struct {
963983
// Generate a new random account and a funded simulator
964984
key, _ := crypto.GenerateKey()
965985
auth := bind.NewKeyedTransactor(key)
986+
966987
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
988+
defer sim.Close()
967989
968990
//deploy the test contract
969991
_, _, testContract, err := DeployDeeplyNestedArray(auth, sim)
@@ -1059,12 +1081,12 @@ var bindTests = []struct {
10591081
`
10601082
pragma solidity >=0.4.19 <0.6.0;
10611083
pragma experimental ABIEncoderV2;
1062-
1084+
10631085
contract Tuple {
10641086
struct S { uint a; uint[] b; T[] c; }
10651087
struct T { uint x; uint y; }
10661088
event TupleEvent(S a, T[2][] b, T[][2] c, S[] d, uint[] e);
1067-
1089+
10681090
function func1(S memory a, T[2][] memory b, T[][2] memory c, S[] memory d, uint[] memory e) public pure returns (S memory, T[2][] memory, T[][2] memory, S[] memory, uint[] memory) {
10691091
return (a, b, c, d, e);
10701092
}
@@ -1081,7 +1103,7 @@ var bindTests = []struct {
10811103
`
10821104
"math/big"
10831105
"reflect"
1084-
1106+
10851107
"github.com/ethereum/go-ethereum/accounts/abi/bind"
10861108
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
10871109
"github.com/ethereum/go-ethereum/core"
@@ -1091,13 +1113,15 @@ var bindTests = []struct {
10911113
`
10921114
key, _ := crypto.GenerateKey()
10931115
auth := bind.NewKeyedTransactor(key)
1094-
backend := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
10951116
1096-
_, _, contract, err := DeployTuple(auth, backend)
1117+
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
1118+
defer sim.Close()
1119+
1120+
_, _, contract, err := DeployTuple(auth, sim)
10971121
if err != nil {
10981122
t.Fatalf("deploy contract failed %v", err)
10991123
}
1100-
backend.Commit()
1124+
sim.Commit()
11011125
11021126
check := func(a, b interface{}, errMsg string) {
11031127
if !reflect.DeepEqual(a, b) {
@@ -1169,7 +1193,7 @@ var bindTests = []struct {
11691193
if err != nil {
11701194
t.Fatalf("invoke contract failed, err %v", err)
11711195
}
1172-
backend.Commit()
1196+
sim.Commit()
11731197
11741198
iter, err := contract.FilterTupleEvent(nil)
11751199
if err != nil {
@@ -1225,7 +1249,9 @@ var bindTests = []struct {
12251249
// Generate a new random account and a funded simulator
12261250
key, _ := crypto.GenerateKey()
12271251
auth := bind.NewKeyedTransactor(key)
1252+
12281253
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
1254+
defer sim.Close()
12291255
12301256
//deploy the test contract
12311257
_, _, testContract, err := DeployUseLibrary(auth, sim)

accounts/abi/bind/util_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestWaitDeployed(t *testing.T) {
5959
},
6060
10000000,
6161
)
62+
defer backend.Close()
6263

6364
// Create the transaction.
6465
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, big.NewInt(1), common.FromHex(test.code))

contracts/checkpointoracle/oracle_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,11 @@ func TestCheckpointRegister(t *testing.T) {
175175
sort.Sort(accounts)
176176

177177
// Deploy registrar contract
178-
transactOpts := bind.NewKeyedTransactor(accounts[0].key)
179178
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{accounts[0].addr: {Balance: big.NewInt(1000000000)}, accounts[1].addr: {Balance: big.NewInt(1000000000)}, accounts[2].addr: {Balance: big.NewInt(1000000000)}}, 10000000)
179+
defer contractBackend.Close()
180+
181+
transactOpts := bind.NewKeyedTransactor(accounts[0].key)
182+
180183
// 3 trusted signers, threshold 2
181184
contractAddr, _, c, err := contract.DeployCheckpointOracle(transactOpts, contractBackend, []common.Address{accounts[0].addr, accounts[1].addr, accounts[2].addr}, sectionSize, processConfirms, big.NewInt(2))
182185
if err != nil {

les/helper_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ func newServerEnv(t *testing.T, blocks int, protocol int, waitIndexers func(*cor
422422
// Note bloom trie indexer will be closed by it parent recursively.
423423
cIndexer.Close()
424424
bIndexer.Close()
425+
b.Close()
425426
}
426427
}
427428

@@ -503,5 +504,7 @@ func newClientServerEnv(t *testing.T, blocks int, protocol int, waitIndexers fun
503504
bIndexer.Close()
504505
lcIndexer.Close()
505506
lbIndexer.Close()
507+
b.Close()
508+
lb.Close()
506509
}
507510
}

0 commit comments

Comments
 (0)