forked from ethersphere/swarm
/
testbackend.go
38 lines (31 loc) · 1.13 KB
/
testbackend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package mock
import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/core/types"
)
// TestBackend is the backend to use for tests with a simulated backend
type TestBackend struct {
*backends.SimulatedBackend
}
// SendTransaction adds a commit after a successful send
func (b *TestBackend) SendTransaction(ctx context.Context, tx *types.Transaction) (err error) {
err = b.SimulatedBackend.SendTransaction(ctx, tx)
if err == nil {
b.SimulatedBackend.Commit()
}
return err
}
// Close overrides the Close function of the underlying SimulatedBackend so that it does nothing
// This allows the same SimulatedBackend backend to be reused across tests
// This is necessary due to some memory leakage issues with the used version of the SimulatedBackend
func (b *TestBackend) Close() {
}
// NewTestBackend returns a new TestBackend for the given SimulatedBackend
// It also causes an initial commit to make sure that genesis accounts are set up
func NewTestBackend(backend *backends.SimulatedBackend) *TestBackend {
backend.Commit()
return &TestBackend{
SimulatedBackend: backend,
}
}