-
Notifications
You must be signed in to change notification settings - Fork 177
/
emulator.go
120 lines (101 loc) · 3.15 KB
/
emulator.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package testutils
import (
cryptoRand "crypto/rand"
"math/big"
"math/rand"
"testing"
gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/fvm/evm/types"
)
type TestEmulator struct {
BalanceOfFunc func(address types.Address) (*big.Int, error)
NonceOfFunc func(address types.Address) (uint64, error)
CodeOfFunc func(address types.Address) (types.Code, error)
DirectCallFunc func(call *types.DirectCall) (*types.Result, error)
RunTransactionFunc func(tx *gethTypes.Transaction) (*types.Result, error)
}
var _ types.Emulator = &TestEmulator{}
// NewBlock returns a new block
func (em *TestEmulator) NewBlockView(_ types.BlockContext) (types.BlockView, error) {
return em, nil
}
// NewBlock returns a new block view
func (em *TestEmulator) NewReadOnlyBlockView(_ types.BlockContext) (types.ReadOnlyBlockView, error) {
return em, nil
}
// BalanceOf returns the balance of this address
func (em *TestEmulator) BalanceOf(address types.Address) (*big.Int, error) {
if em.BalanceOfFunc == nil {
panic("method not set")
}
return em.BalanceOfFunc(address)
}
// NonceOfFunc returns the nonce for this address
func (em *TestEmulator) NonceOf(address types.Address) (uint64, error) {
if em.NonceOfFunc == nil {
panic("method not set")
}
return em.NonceOfFunc(address)
}
// CodeOf returns the code for this address (if smart contract is deployed at this address)
func (em *TestEmulator) CodeOf(address types.Address) (types.Code, error) {
if em.CodeOfFunc == nil {
panic("method not set")
}
return em.CodeOfFunc(address)
}
// DirectCall executes a direct call
func (em *TestEmulator) DirectCall(call *types.DirectCall) (*types.Result, error) {
if em.DirectCallFunc == nil {
panic("method not set")
}
return em.DirectCallFunc(call)
}
// RunTransaction runs a transaction and collect gas fees to the coinbase account
func (em *TestEmulator) RunTransaction(tx *gethTypes.Transaction) (*types.Result, error) {
if em.RunTransactionFunc == nil {
panic("method not set")
}
return em.RunTransactionFunc(tx)
}
func RandomCommonHash(t testing.TB) gethCommon.Hash {
ret := gethCommon.Hash{}
_, err := cryptoRand.Read(ret[:gethCommon.HashLength])
require.NoError(t, err)
return ret
}
func RandomBigInt(limit int64) *big.Int {
return big.NewInt(rand.Int63n(limit) + 1)
}
func RandomAddress(t testing.TB) types.Address {
return types.NewAddress(RandomCommonAddress(t))
}
func RandomCommonAddress(t testing.TB) gethCommon.Address {
ret := gethCommon.Address{}
_, err := cryptoRand.Read(ret[:gethCommon.AddressLength])
require.NoError(t, err)
return ret
}
func RandomGas(limit int64) uint64 {
return uint64(rand.Int63n(limit) + 1)
}
func RandomData(t testing.TB) []byte {
// byte size [1, 100]
size := rand.Intn(100) + 1
ret := make([]byte, size)
_, err := cryptoRand.Read(ret[:])
require.NoError(t, err)
return ret
}
func GetRandomLogFixture(t testing.TB) *gethTypes.Log {
return &gethTypes.Log{
Address: RandomCommonAddress(t),
Topics: []gethCommon.Hash{
RandomCommonHash(t),
RandomCommonHash(t),
},
Data: RandomData(t),
}
}