-
Notifications
You must be signed in to change notification settings - Fork 199
/
blockChainHook.go
527 lines (436 loc) · 14.9 KB
/
blockChainHook.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package hooks
import (
"encoding/binary"
"fmt"
"math/big"
"sync"
"time"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/data/typeConverters"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/hashing/keccak"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
var _ process.BlockChainHookHandler = (*BlockChainHookImpl)(nil)
var _ process.TemporaryAccountsHandler = (*BlockChainHookImpl)(nil)
var log = logger.GetOrCreate("process/smartcontract/blockchainhook")
const executeDurationAlarmThreshold = time.Duration(50) * time.Millisecond
// ArgBlockChainHook represents the arguments structure for the blockchain hook
type ArgBlockChainHook struct {
Accounts state.AccountsAdapter
PubkeyConv core.PubkeyConverter
StorageService dataRetriever.StorageService
BlockChain data.ChainHandler
ShardCoordinator sharding.Coordinator
Marshalizer marshal.Marshalizer
Uint64Converter typeConverters.Uint64ByteSliceConverter
BuiltInFunctions process.BuiltInFunctionContainer
}
// BlockChainHookImpl is a wrapper over AccountsAdapter that satisfy vmcommon.BlockchainHook interface
type BlockChainHookImpl struct {
accounts state.AccountsAdapter
pubkeyConv core.PubkeyConverter
storageService dataRetriever.StorageService
blockChain data.ChainHandler
shardCoordinator sharding.Coordinator
marshalizer marshal.Marshalizer
uint64Converter typeConverters.Uint64ByteSliceConverter
builtInFunctions process.BuiltInFunctionContainer
mutCurrentHdr sync.RWMutex
currentHdr data.HeaderHandler
mutTempAccounts sync.Mutex
tempAccounts map[string]state.AccountHandler
}
// NewBlockChainHookImpl creates a new BlockChainHookImpl instance
func NewBlockChainHookImpl(
args ArgBlockChainHook,
) (*BlockChainHookImpl, error) {
err := checkForNil(args)
if err != nil {
return nil, err
}
blockChainHookImpl := &BlockChainHookImpl{
accounts: args.Accounts,
pubkeyConv: args.PubkeyConv,
storageService: args.StorageService,
blockChain: args.BlockChain,
shardCoordinator: args.ShardCoordinator,
marshalizer: args.Marshalizer,
uint64Converter: args.Uint64Converter,
builtInFunctions: args.BuiltInFunctions,
}
blockChainHookImpl.currentHdr = &block.Header{}
blockChainHookImpl.tempAccounts = make(map[string]state.AccountHandler)
return blockChainHookImpl, nil
}
func checkForNil(args ArgBlockChainHook) error {
if check.IfNil(args.Accounts) {
return process.ErrNilAccountsAdapter
}
if check.IfNil(args.PubkeyConv) {
return process.ErrNilPubkeyConverter
}
if check.IfNil(args.StorageService) {
return process.ErrNilStorage
}
if check.IfNil(args.BlockChain) {
return process.ErrNilBlockChain
}
if check.IfNil(args.ShardCoordinator) {
return process.ErrNilShardCoordinator
}
if check.IfNil(args.Marshalizer) {
return process.ErrNilMarshalizer
}
if check.IfNil(args.Uint64Converter) {
return process.ErrNilUint64Converter
}
if check.IfNil(args.BuiltInFunctions) {
return process.ErrNilBuiltInFunction
}
return nil
}
// GetUserAccount returns the balance of a shard account
func (bh *BlockChainHookImpl) GetUserAccount(address []byte) (vmcommon.UserAccountHandler, error) {
defer stopMeasure(startMeasure("GetUserAccount"))
account, err := bh.getAccountFromAddressBytes(address)
if err != nil {
return nil, err
}
shardAccount, ok := account.(state.UserAccountHandler)
if !ok {
return nil, state.ErrWrongTypeAssertion
}
return shardAccount, nil
}
// GetStorageData returns the storage value of a variable held in account's data trie
func (bh *BlockChainHookImpl) GetStorageData(accountAddress []byte, index []byte) ([]byte, error) {
defer stopMeasure(startMeasure("GetStorageData"))
account, err := bh.GetUserAccount(accountAddress)
if err == state.ErrAccNotFound {
return make([]byte, 0), nil
}
if err != nil {
return nil, err
}
userAcc, ok := account.(state.UserAccountHandler)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
value, err := userAcc.DataTrieTracker().RetrieveValue(index)
messages := []interface{}{
"address", accountAddress,
"rootHash", userAcc.GetRootHash(),
"key", index,
"value", value,
}
if err != nil {
messages = append(messages, "error")
messages = append(messages, err)
}
log.Trace("GetStorageData ", messages...)
return value, err
}
// GetBlockhash returns the header hash for a requested nonce delta
func (bh *BlockChainHookImpl) GetBlockhash(nonce uint64) ([]byte, error) {
defer stopMeasure(startMeasure("GetBlockhash"))
hdr := bh.blockChain.GetCurrentBlockHeader()
if check.IfNil(hdr) {
return nil, process.ErrNilBlockHeader
}
if nonce > hdr.GetNonce() {
return nil, process.ErrInvalidNonceRequest
}
if nonce == hdr.GetNonce() {
return bh.blockChain.GetCurrentBlockHeaderHash(), nil
}
_, hash, err := process.GetHeaderFromStorageWithNonce(
nonce,
bh.shardCoordinator.SelfId(),
bh.storageService,
bh.uint64Converter,
bh.marshalizer,
)
if err != nil {
return nil, err
}
return hash, nil
}
// LastNonce returns the nonce from from the last committed block
func (bh *BlockChainHookImpl) LastNonce() uint64 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetNonce()
}
return 0
}
// LastRound returns the round from the last committed block
func (bh *BlockChainHookImpl) LastRound() uint64 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetRound()
}
return 0
}
// LastTimeStamp returns the timeStamp from the last committed block
func (bh *BlockChainHookImpl) LastTimeStamp() uint64 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetTimeStamp()
}
return 0
}
// LastRandomSeed returns the random seed from the last committed block
func (bh *BlockChainHookImpl) LastRandomSeed() []byte {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetRandSeed()
}
return []byte{}
}
// LastEpoch returns the epoch from the last committed block
func (bh *BlockChainHookImpl) LastEpoch() uint32 {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetEpoch()
}
return 0
}
// GetStateRootHash returns the state root hash from the last committed block
func (bh *BlockChainHookImpl) GetStateRootHash() []byte {
if !check.IfNil(bh.blockChain.GetCurrentBlockHeader()) {
return bh.blockChain.GetCurrentBlockHeader().GetRootHash()
}
return []byte{}
}
// CurrentNonce returns the nonce from the current block
func (bh *BlockChainHookImpl) CurrentNonce() uint64 {
bh.mutCurrentHdr.RLock()
defer bh.mutCurrentHdr.RUnlock()
return bh.currentHdr.GetNonce()
}
// CurrentRound returns the round from the current block
func (bh *BlockChainHookImpl) CurrentRound() uint64 {
bh.mutCurrentHdr.RLock()
defer bh.mutCurrentHdr.RUnlock()
return bh.currentHdr.GetRound()
}
// CurrentTimeStamp return the timestamp from the current block
func (bh *BlockChainHookImpl) CurrentTimeStamp() uint64 {
bh.mutCurrentHdr.RLock()
defer bh.mutCurrentHdr.RUnlock()
return bh.currentHdr.GetTimeStamp()
}
// CurrentRandomSeed returns the random seed from the current header
func (bh *BlockChainHookImpl) CurrentRandomSeed() []byte {
bh.mutCurrentHdr.RLock()
defer bh.mutCurrentHdr.RUnlock()
return bh.currentHdr.GetRandSeed()
}
// CurrentEpoch returns the current epoch
func (bh *BlockChainHookImpl) CurrentEpoch() uint32 {
bh.mutCurrentHdr.RLock()
defer bh.mutCurrentHdr.RUnlock()
return bh.currentHdr.GetEpoch()
}
// NewAddress is a hook which creates a new smart contract address from the creators address and nonce
// The address is created by applied keccak256 on the appended value off creator address and nonce
// Prefix mask is applied for first 8 bytes 0, and for bytes 9-10 - VM type
// Suffix mask is applied - last 2 bytes are for the shard ID - mask is applied as suffix mask
func (bh *BlockChainHookImpl) NewAddress(creatorAddress []byte, creatorNonce uint64, vmType []byte) ([]byte, error) {
addressLength := bh.pubkeyConv.Len()
if len(creatorAddress) != addressLength {
return nil, ErrAddressLengthNotCorrect
}
if len(vmType) != core.VMTypeLen {
return nil, ErrVMTypeLengthIsNotCorrect
}
base := hashFromAddressAndNonce(creatorAddress, creatorNonce)
prefixMask := createPrefixMask(vmType)
suffixMask := createSuffixMask(creatorAddress)
copy(base[:core.NumInitCharactersForScAddress], prefixMask)
copy(base[len(base)-core.ShardIdentiferLen:], suffixMask)
return base, nil
}
// ProcessBuiltInFunction is the hook through which a smart contract can execute a built in function
func (bh *BlockChainHookImpl) ProcessBuiltInFunction(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error) {
defer stopMeasure(startMeasure("ProcessBuiltInFunction"))
if input == nil {
return nil, process.ErrNilVmInput
}
function, err := bh.builtInFunctions.Get(input.Function)
if err != nil {
return nil, err
}
sndAccount, dstAccount, err := bh.getUserAccounts(input)
if err != nil {
return nil, err
}
vmOutput, err := function.ProcessBuiltinFunction(sndAccount, dstAccount, input)
if err != nil {
return nil, err
}
if !check.IfNil(sndAccount) {
err = bh.accounts.SaveAccount(sndAccount)
if err != nil {
return nil, err
}
}
if !check.IfNil(dstAccount) {
err = bh.accounts.SaveAccount(dstAccount)
if err != nil {
return nil, err
}
}
return vmOutput, nil
}
// GetShardOfAddress is the hook that returns the shard of a given address
func (bh *BlockChainHookImpl) GetShardOfAddress(address []byte) uint32 {
return bh.shardCoordinator.ComputeId(address)
}
// IsSmartContract returns whether the address points to a smart contract
func (bh *BlockChainHookImpl) IsSmartContract(address []byte) bool {
return core.IsSmartContractAddress(address)
}
func (bh *BlockChainHookImpl) getUserAccounts(
input *vmcommon.ContractCallInput,
) (state.UserAccountHandler, state.UserAccountHandler, error) {
var sndAccount state.UserAccountHandler
sndShardId := bh.shardCoordinator.ComputeId(input.CallerAddr)
if sndShardId == bh.shardCoordinator.SelfId() {
acc, err := bh.accounts.GetExistingAccount(input.CallerAddr)
if err != nil {
return nil, nil, err
}
var ok bool
sndAccount, ok = acc.(state.UserAccountHandler)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}
}
var dstAccount state.UserAccountHandler
dstShardId := bh.shardCoordinator.ComputeId(input.RecipientAddr)
if dstShardId == bh.shardCoordinator.SelfId() {
acc, err := bh.accounts.LoadAccount(input.RecipientAddr)
if err != nil {
return nil, nil, err
}
var ok bool
dstAccount, ok = acc.(state.UserAccountHandler)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}
}
return sndAccount, dstAccount, nil
}
// GetBuiltInFunctions returns the built in functions container
func (bh *BlockChainHookImpl) GetBuiltInFunctions() process.BuiltInFunctionContainer {
return bh.builtInFunctions
}
// GetBuiltinFunctionNames returns the built in function names
func (bh *BlockChainHookImpl) GetBuiltinFunctionNames() vmcommon.FunctionNames {
return bh.builtInFunctions.Keys()
}
// GetAllState returns the underlying state of a given account
func (bh *BlockChainHookImpl) GetAllState(address []byte) (map[string][]byte, error) {
defer stopMeasure(startMeasure("GetAllState"))
dstShardId := bh.shardCoordinator.ComputeId(address)
if dstShardId != bh.shardCoordinator.SelfId() {
return nil, process.ErrDestinationNotInSelfShard
}
acc, err := bh.accounts.GetExistingAccount(address)
if err != nil {
return nil, err
}
dstAccount, ok := acc.(state.UserAccountHandler)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
return dstAccount.DataTrie().GetAllLeaves()
}
func hashFromAddressAndNonce(creatorAddress []byte, creatorNonce uint64) []byte {
buffNonce := make([]byte, 8)
binary.LittleEndian.PutUint64(buffNonce, creatorNonce)
adrAndNonce := append(creatorAddress, buffNonce...)
scAddress := keccak.Keccak{}.Compute(string(adrAndNonce))
return scAddress
}
func createPrefixMask(vmType []byte) []byte {
prefixMask := make([]byte, core.NumInitCharactersForScAddress-core.VMTypeLen)
prefixMask = append(prefixMask, vmType...)
return prefixMask
}
func createSuffixMask(creatorAddress []byte) []byte {
return creatorAddress[len(creatorAddress)-2:]
}
func (bh *BlockChainHookImpl) getAccountFromAddressBytes(address []byte) (state.AccountHandler, error) {
tempAcc, success := bh.getAccountFromTemporaryAccounts(address)
if success {
return tempAcc, nil
}
return bh.accounts.GetExistingAccount(address)
}
func (bh *BlockChainHookImpl) getAccountFromTemporaryAccounts(address []byte) (state.AccountHandler, bool) {
bh.mutTempAccounts.Lock()
defer bh.mutTempAccounts.Unlock()
if tempAcc, ok := bh.tempAccounts[string(address)]; ok {
return tempAcc, true
}
return nil, false
}
// AddTempAccount will add a temporary account in temporary store
func (bh *BlockChainHookImpl) AddTempAccount(address []byte, balance *big.Int, nonce uint64) {
bh.mutTempAccounts.Lock()
defer bh.mutTempAccounts.Unlock()
account, err := state.NewUserAccount(address)
if err != nil {
return
}
account.Balance.Set(balance)
account.Nonce = nonce
bh.tempAccounts[string(address)] = account
}
// CleanTempAccounts cleans the map holding the temporary accounts
func (bh *BlockChainHookImpl) CleanTempAccounts() {
bh.mutTempAccounts.Lock()
bh.tempAccounts = make(map[string]state.AccountHandler)
bh.mutTempAccounts.Unlock()
}
// TempAccount can retrieve a temporary account from provided address
func (bh *BlockChainHookImpl) TempAccount(address []byte) state.AccountHandler {
tempAcc, success := bh.getAccountFromTemporaryAccounts(address)
if success {
return tempAcc
}
return nil
}
// SetCurrentHeader sets current header to be used by smart contracts
func (bh *BlockChainHookImpl) SetCurrentHeader(hdr data.HeaderHandler) {
if check.IfNil(hdr) {
return
}
bh.mutCurrentHdr.Lock()
bh.currentHdr = hdr
bh.mutCurrentHdr.Unlock()
}
// IsInterfaceNil returns true if there is no value under the interface
func (bh *BlockChainHookImpl) IsInterfaceNil() bool {
return bh == nil
}
func startMeasure(hook string) (string, *core.StopWatch) {
sw := core.NewStopWatch()
sw.Start(hook)
return hook, sw
}
func stopMeasure(hook string, sw *core.StopWatch) {
sw.Stop(hook)
duration := sw.GetMeasurement(hook)
if duration > executeDurationAlarmThreshold {
log.Debug(fmt.Sprintf("%s took > %s", hook, executeDurationAlarmThreshold), "duration", duration)
} else {
log.Trace(hook, "duration", duration)
}
}