/
worldCallbacks.go
284 lines (238 loc) · 7.51 KB
/
worldCallbacks.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package worldmock
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"math/big"
"github.com/ElrondNetwork/elrond-go/core/vmcommon"
"github.com/ElrondNetwork/elrond-go/data/esdt"
)
var _ vmcommon.BlockchainHook = (*MockWorld)(nil)
// ErrBuiltinFuncWrapperNotInitialized means that the builtin function wrapper was used before initialization.
var ErrBuiltinFuncWrapperNotInitialized = errors.New("builtin function not found or container not initialized")
var zero = big.NewInt(0)
// NewAddress provides the address for a new account.
// It looks up the explicit new address mocks, if none found generates one using a fake but realistic algorithm.
func (b *MockWorld) NewAddress(creatorAddress []byte, creatorNonce uint64, _ []byte) ([]byte, error) {
// custom error
if b.Err != nil {
return nil, b.Err
}
// explicit new address mocks
// matched by creator address and nonce
for _, newAddressMock := range b.NewAddressMocks {
if bytes.Equal(creatorAddress, newAddressMock.CreatorAddress) && creatorNonce == newAddressMock.CreatorNonce {
b.LastCreatedContractAddress = newAddressMock.NewAddress
return newAddressMock.NewAddress, nil
}
}
// If a mock address wasn't registered for the specified creatorAddress, generate one automatically.
// This is not the real algorithm but it's simple and close enough.
result := GenerateMockAddress(creatorAddress, creatorNonce)
b.LastCreatedContractAddress = result
return result, nil
}
// GetStorageData yields the storage value for a certain account and storage key.
// Should return an empty byte array if the key is missing from the account storage
func (b *MockWorld) GetStorageData(accountAddress []byte, key []byte) ([]byte, error) {
// custom error
if b.Err != nil {
return nil, b.Err
}
acct := b.AcctMap.GetAccount(accountAddress)
if acct == nil {
return []byte{}, nil
}
return acct.StorageValue(string(key)), nil
}
// GetBlockhash should return the hash of the nth previous blockchain.
// Offset specifies how many blocks we need to look back.
func (b *MockWorld) GetBlockhash(nonce uint64) ([]byte, error) {
if b.Err != nil {
return nil, b.Err
}
currentNonce := b.CurrentNonce()
if nonce > currentNonce {
return nil, errors.New("requested nonce is greater than current nonce")
}
offsetInt32 := int(currentNonce - nonce)
if offsetInt32 >= len(b.Blockhashes) {
return nil, errors.New("requested nonce is older than the oldest available block nonce")
}
return b.Blockhashes[offsetInt32], nil
}
// LastNonce returns the nonce from from the last committed block
func (b *MockWorld) LastNonce() uint64 {
if b.PreviousBlockInfo == nil {
return 0
}
return b.PreviousBlockInfo.BlockNonce
}
// LastRound returns the round from the last committed block
func (b *MockWorld) LastRound() uint64 {
if b.PreviousBlockInfo == nil {
return 0
}
return b.PreviousBlockInfo.BlockRound
}
// LastTimeStamp returns the timeStamp from the last committed block
func (b *MockWorld) LastTimeStamp() uint64 {
if b.PreviousBlockInfo == nil {
return 0
}
return b.PreviousBlockInfo.BlockTimestamp
}
// LastRandomSeed returns the random seed from the last committed block
func (b *MockWorld) LastRandomSeed() []byte {
if b.PreviousBlockInfo == nil {
return nil
}
return b.PreviousBlockInfo.RandomSeed[:]
}
// LastEpoch returns the epoch from the last committed block
func (b *MockWorld) LastEpoch() uint32 {
if b.PreviousBlockInfo == nil {
return 0
}
return b.PreviousBlockInfo.BlockEpoch
}
// GetStateRootHash returns the state root hash from the last committed block
func (b *MockWorld) GetStateRootHash() []byte {
return b.StateRootHash
}
// CurrentNonce returns the nonce from the current block
func (b *MockWorld) CurrentNonce() uint64 {
if b.CurrentBlockInfo == nil {
return 0
}
return b.CurrentBlockInfo.BlockNonce
}
// CurrentRound returns the round from the current block
func (b *MockWorld) CurrentRound() uint64 {
if b.CurrentBlockInfo == nil {
return 0
}
return b.CurrentBlockInfo.BlockRound
}
// CurrentTimeStamp return the timestamp from the current block
func (b *MockWorld) CurrentTimeStamp() uint64 {
if b.CurrentBlockInfo == nil {
return 0
}
return b.CurrentBlockInfo.BlockTimestamp
}
// CurrentRandomSeed returns the random seed from the current header
func (b *MockWorld) CurrentRandomSeed() []byte {
if b.CurrentBlockInfo == nil {
return nil
}
return b.CurrentBlockInfo.RandomSeed[:]
}
// CurrentEpoch returns the current epoch
func (b *MockWorld) CurrentEpoch() uint32 {
if b.CurrentBlockInfo == nil {
return 0
}
return b.CurrentBlockInfo.BlockEpoch
}
// ProcessBuiltInFunction -
func (b *MockWorld) ProcessBuiltInFunction(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) {
// custom error
if b.Err != nil {
return nil, b.Err
}
if b.BuiltinFuncs == nil {
return nil, ErrBuiltinFuncWrapperNotInitialized
}
return b.BuiltinFuncs.ProcessBuiltInFunction(input)
}
// GetESDTToken -
func (b *MockWorld) GetESDTToken(address []byte, tokenName []byte, nonce uint64) (*esdt.ESDigitalToken, error) {
// custom error
if b.Err != nil {
return nil, b.Err
}
if b.BuiltinFuncs == nil {
return nil, ErrBuiltinFuncWrapperNotInitialized
}
tokenKey := MakeTokenKey(tokenName, nonce)
return b.BuiltinFuncs.GetTokenData(address, tokenKey)
}
// GetBuiltinFunctionNames -
func (b *MockWorld) GetBuiltinFunctionNames() vmcommon.FunctionNames {
return b.BuiltinFuncs.GetBuiltinFunctionNames()
}
// GetAllState simply returns the storage as-is.
func (b *MockWorld) GetAllState(accountAddress []byte) (map[string][]byte, error) {
account := b.AcctMap.GetAccount(accountAddress)
if account == nil {
return nil, fmt.Errorf("account not found: %s", hex.EncodeToString(accountAddress))
}
return account.Storage, nil
}
// GetUserAccount retrieves account info from map, or error if not found.
func (b *MockWorld) GetUserAccount(address []byte) (vmcommon.UserAccountHandler, error) {
// custom error
if b.Err != nil {
return nil, b.Err
}
account := b.AcctMap.GetAccount(address)
if account == nil {
return nil, fmt.Errorf("account not found: %s", hex.EncodeToString(address))
}
return account, nil
}
// GetCode retrieves the code from the given account, or nil if not found
func (b *MockWorld) GetCode(acc vmcommon.UserAccountHandler) []byte {
account := b.AcctMap.GetAccount(acc.AddressBytes())
if account == nil {
return nil
}
return account.Code
}
// GetShardOfAddress -
func (b *MockWorld) GetShardOfAddress(address []byte) uint32 {
account := b.AcctMap.GetAccount(address)
if account == nil {
return 0
}
return account.ShardID
}
// IsSmartContract -
func (b *MockWorld) IsSmartContract(address []byte) bool {
account := b.AcctMap.GetAccount(address)
if account == nil {
return false
}
return account.IsSmartContract
}
// IsPayable -
func (b *MockWorld) IsPayable(address []byte) (bool, error) {
account := b.AcctMap.GetAccount(address)
if account == nil {
return true, nil
}
if !account.IsSmartContract {
return true, nil
}
metadata := vmcommon.CodeMetadataFromBytes(account.CodeMetadata)
return metadata.Payable, nil
}
// SaveCompiledCode -
func (b *MockWorld) SaveCompiledCode(codeHash []byte, code []byte) {
b.CompiledCode[string(codeHash)] = code
}
// GetCompiledCode -
func (b *MockWorld) GetCompiledCode(codeHash []byte) (bool, []byte) {
code, found := b.CompiledCode[string(codeHash)]
return found, code
}
// ClearCompiledCodes -
func (b *MockWorld) ClearCompiledCodes() {
b.CompiledCode = make(map[string][]byte)
}
// IsInterfaceNil returns true if underlying implementation is nil
func (b *MockWorld) IsInterfaceNil() bool {
return b == nil
}