-
Notifications
You must be signed in to change notification settings - Fork 198
/
smartContractResults.go
542 lines (457 loc) · 16.8 KB
/
smartContractResults.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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package preprocess
import (
"time"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/core/sliceUtil"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/block"
"github.com/ElrondNetwork/elrond-go/data/smartContractResult"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/dataRetriever"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/storage"
)
var _ process.DataMarshalizer = (*smartContractResults)(nil)
var _ process.PreProcessor = (*smartContractResults)(nil)
type smartContractResults struct {
*basePreProcess
chRcvAllScrs chan bool
onRequestSmartContractResult func(shardID uint32, txHashes [][]byte)
scrForBlock txsForBlock
scrPool dataRetriever.ShardedDataCacherNotifier
storage dataRetriever.StorageService
scrProcessor process.SmartContractResultProcessor
}
// NewSmartContractResultPreprocessor creates a new smartContractResult preprocessor object
func NewSmartContractResultPreprocessor(
scrDataPool dataRetriever.ShardedDataCacherNotifier,
store dataRetriever.StorageService,
hasher hashing.Hasher,
marshalizer marshal.Marshalizer,
scrProcessor process.SmartContractResultProcessor,
shardCoordinator sharding.Coordinator,
accounts state.AccountsAdapter,
onRequestSmartContractResult func(shardID uint32, txHashes [][]byte),
gasHandler process.GasHandler,
economicsFee process.FeeHandler,
pubkeyConverter core.PubkeyConverter,
blockSizeComputation BlockSizeComputationHandler,
balanceComputation BalanceComputationHandler,
) (*smartContractResults, error) {
if check.IfNil(hasher) {
return nil, process.ErrNilHasher
}
if check.IfNil(marshalizer) {
return nil, process.ErrNilMarshalizer
}
if check.IfNil(scrDataPool) {
return nil, process.ErrNilUTxDataPool
}
if check.IfNil(store) {
return nil, process.ErrNilUTxStorage
}
if check.IfNil(scrProcessor) {
return nil, process.ErrNilTxProcessor
}
if check.IfNil(shardCoordinator) {
return nil, process.ErrNilShardCoordinator
}
if check.IfNil(accounts) {
return nil, process.ErrNilAccountsAdapter
}
if onRequestSmartContractResult == nil {
return nil, process.ErrNilRequestHandler
}
if check.IfNil(gasHandler) {
return nil, process.ErrNilGasHandler
}
if check.IfNil(economicsFee) {
return nil, process.ErrNilEconomicsFeeHandler
}
if check.IfNil(pubkeyConverter) {
return nil, process.ErrNilPubkeyConverter
}
if check.IfNil(blockSizeComputation) {
return nil, process.ErrNilBlockSizeComputationHandler
}
if check.IfNil(balanceComputation) {
return nil, process.ErrNilBalanceComputationHandler
}
bpp := &basePreProcess{
hasher: hasher,
marshalizer: marshalizer,
shardCoordinator: shardCoordinator,
gasHandler: gasHandler,
economicsFee: economicsFee,
blockSizeComputation: blockSizeComputation,
balanceComputation: balanceComputation,
accounts: accounts,
pubkeyConverter: pubkeyConverter,
}
scr := &smartContractResults{
basePreProcess: bpp,
storage: store,
scrPool: scrDataPool,
onRequestSmartContractResult: onRequestSmartContractResult,
scrProcessor: scrProcessor,
}
scr.chRcvAllScrs = make(chan bool)
scr.scrPool.RegisterOnAdded(scr.receivedSmartContractResult)
scr.scrForBlock.txHashAndInfo = make(map[string]*txInfo)
return scr, nil
}
// waitForScrHashes waits for a call whether all the requested smartContractResults appeared
func (scr *smartContractResults) waitForScrHashes(waitTime time.Duration) error {
select {
case <-scr.chRcvAllScrs:
return nil
case <-time.After(waitTime):
return process.ErrTimeIsOut
}
}
// IsDataPrepared returns non error if all the requested smartContractResults arrived and were saved into the pool
func (scr *smartContractResults) IsDataPrepared(requestedScrs int, haveTime func() time.Duration) error {
if requestedScrs > 0 {
log.Debug("requested missing scrs",
"num scrs", requestedScrs)
err := scr.waitForScrHashes(haveTime())
scr.scrForBlock.mutTxsForBlock.Lock()
missingScrs := scr.scrForBlock.missingTxs
scr.scrForBlock.missingTxs = 0
scr.scrForBlock.mutTxsForBlock.Unlock()
log.Debug("received missing scrs",
"num scrs", requestedScrs-missingScrs)
if err != nil {
return err
}
}
return nil
}
// RemoveTxBlockFromPools removes smartContractResults and miniblocks from associated pools
func (scr *smartContractResults) RemoveTxBlockFromPools(body *block.Body, miniBlockPool storage.Cacher) error {
return scr.removeDataFromPools(body, miniBlockPool, scr.scrPool, scr.isMiniBlockCorrect)
}
// RestoreTxBlockIntoPools restores the smartContractResults and miniblocks to associated pools
func (scr *smartContractResults) RestoreTxBlockIntoPools(
body *block.Body,
miniBlockPool storage.Cacher,
) (int, error) {
if check.IfNil(body) {
return 0, process.ErrNilBlockBody
}
if check.IfNil(miniBlockPool) {
return 0, process.ErrNilMiniBlockPool
}
scrRestored := 0
for i := 0; i < len(body.MiniBlocks); i++ {
miniBlock := body.MiniBlocks[i]
if miniBlock.Type != block.SmartContractResultBlock {
continue
}
strCache := process.ShardCacherIdentifier(miniBlock.SenderShardID, miniBlock.ReceiverShardID)
scrBuff, err := scr.storage.GetAll(dataRetriever.UnsignedTransactionUnit, miniBlock.TxHashes)
if err != nil {
log.Debug("unsigned tx from mini block was not found in UnsignedTransactionUnit",
"sender shard ID", miniBlock.SenderShardID,
"receiver shard ID", miniBlock.ReceiverShardID,
"num txs", len(miniBlock.TxHashes),
)
return scrRestored, err
}
for txHash, txBuff := range scrBuff {
tx := smartContractResult.SmartContractResult{}
err = scr.marshalizer.Unmarshal(&tx, txBuff)
if err != nil {
return scrRestored, err
}
scr.scrPool.AddData([]byte(txHash), &tx, tx.Size(), strCache)
}
//TODO: Should be analyzed if restoring into pool only cross-shard miniblocks with destination in self shard,
//would create problems or not
if miniBlock.SenderShardID != scr.shardCoordinator.SelfId() {
miniBlockHash, errHash := core.CalculateHash(scr.marshalizer, scr.hasher, miniBlock)
if errHash != nil {
return scrRestored, errHash
}
miniBlockPool.Put(miniBlockHash, miniBlock, miniBlock.Size())
}
scrRestored += len(miniBlock.TxHashes)
}
return scrRestored, nil
}
// ProcessBlockTransactions processes all the smartContractResult from the block.Body, updates the state
func (scr *smartContractResults) ProcessBlockTransactions(
body *block.Body,
haveTime func() bool,
) error {
if check.IfNil(body) {
return process.ErrNilBlockBody
}
// basic validation already done in interceptors
for i := 0; i < len(body.MiniBlocks); i++ {
miniBlock := body.MiniBlocks[i]
if miniBlock.Type != block.SmartContractResultBlock {
continue
}
// smart contract results are needed to be processed only at destination and only if they are cross shard
if miniBlock.ReceiverShardID != scr.shardCoordinator.SelfId() {
continue
}
if miniBlock.SenderShardID == scr.shardCoordinator.SelfId() {
continue
}
for j := 0; j < len(miniBlock.TxHashes); j++ {
if !haveTime() {
return process.ErrTimeIsOut
}
txHash := miniBlock.TxHashes[j]
scr.scrForBlock.mutTxsForBlock.RLock()
txInfoFromMap, ok := scr.scrForBlock.txHashAndInfo[string(txHash)]
scr.scrForBlock.mutTxsForBlock.RUnlock()
if !ok || check.IfNil(txInfoFromMap.tx) {
log.Warn("missing transaction in ProcessBlockTransactions ", "type", miniBlock.Type, "txHash", txHash)
return process.ErrMissingTransaction
}
currScr, ok := txInfoFromMap.tx.(*smartContractResult.SmartContractResult)
if !ok {
return process.ErrWrongTypeAssertion
}
scr.saveAccountBalanceForAddress(currScr.GetRcvAddr())
_, err := scr.scrProcessor.ProcessSmartContractResult(currScr)
if err != nil {
return err
}
}
}
return nil
}
// SaveTxBlockToStorage saves smartContractResults from body into storage
func (scr *smartContractResults) SaveTxBlockToStorage(body *block.Body) error {
if check.IfNil(body) {
return process.ErrNilBlockBody
}
for i := 0; i < len(body.MiniBlocks); i++ {
miniBlock := body.MiniBlocks[i]
if miniBlock.Type != block.SmartContractResultBlock {
continue
}
if miniBlock.ReceiverShardID != scr.shardCoordinator.SelfId() {
continue
}
if miniBlock.SenderShardID == scr.shardCoordinator.SelfId() {
continue
}
err := scr.saveTxsToStorage(miniBlock.TxHashes, &scr.scrForBlock, scr.storage, dataRetriever.UnsignedTransactionUnit)
if err != nil {
return err
}
}
return nil
}
// receivedSmartContractResult is a call back function which is called when a new smartContractResult
// is added in the smartContractResult pool
func (scr *smartContractResults) receivedSmartContractResult(key []byte, value interface{}) {
tx, ok := value.(data.TransactionHandler)
if !ok {
log.Warn("smartContractResults.receivedSmartContractResult", "error", process.ErrWrongTypeAssertion)
return
}
receivedAllMissing := scr.baseReceivedTransaction(key, tx, &scr.scrForBlock)
if receivedAllMissing {
scr.chRcvAllScrs <- true
}
}
// CreateBlockStarted cleans the local cache map for processed/created smartContractResults at this round
func (scr *smartContractResults) CreateBlockStarted() {
_ = core.EmptyChannel(scr.chRcvAllScrs)
scr.scrForBlock.mutTxsForBlock.Lock()
scr.scrForBlock.missingTxs = 0
scr.scrForBlock.txHashAndInfo = make(map[string]*txInfo)
scr.scrForBlock.mutTxsForBlock.Unlock()
}
// RequestBlockTransactions request for smartContractResults if missing from a block.Body
func (scr *smartContractResults) RequestBlockTransactions(body *block.Body) int {
if check.IfNil(body) {
return 0
}
return scr.computeExistingAndRequestMissingSCResultsForShards(body)
}
// computeExistingAndRequestMissingSCResultsForShards calculates what smartContractResults are available and requests
// what are missing from block.Body
func (scr *smartContractResults) computeExistingAndRequestMissingSCResultsForShards(body *block.Body) int {
scrTxs := block.Body{}
for _, mb := range body.MiniBlocks {
if mb.Type != block.SmartContractResultBlock {
continue
}
if mb.SenderShardID == scr.shardCoordinator.SelfId() {
continue
}
scrTxs.MiniBlocks = append(scrTxs.MiniBlocks, mb)
}
numMissingTxsForShard := scr.computeExistingAndRequestMissing(
&scrTxs,
&scr.scrForBlock,
scr.chRcvAllScrs,
scr.isMiniBlockCorrect,
scr.scrPool,
scr.onRequestSmartContractResult,
)
return numMissingTxsForShard
}
// RequestTransactionsForMiniBlock requests missing smartContractResults for a certain miniblock
func (scr *smartContractResults) RequestTransactionsForMiniBlock(miniBlock *block.MiniBlock) int {
if miniBlock == nil {
return 0
}
missingScrsForMiniBlock := scr.computeMissingScrsForMiniBlock(miniBlock)
if len(missingScrsForMiniBlock) > 0 {
scr.onRequestSmartContractResult(miniBlock.SenderShardID, missingScrsForMiniBlock)
}
return len(missingScrsForMiniBlock)
}
// computeMissingScrsForMiniBlock computes missing smartContractResults for a certain miniblock
func (scr *smartContractResults) computeMissingScrsForMiniBlock(miniBlock *block.MiniBlock) [][]byte {
if miniBlock.Type != block.SmartContractResultBlock {
return [][]byte{}
}
missingSmartContractResults := make([][]byte, 0, len(miniBlock.TxHashes))
for _, txHash := range miniBlock.TxHashes {
tx, _ := process.GetTransactionHandlerFromPool(
miniBlock.SenderShardID,
miniBlock.ReceiverShardID,
txHash,
scr.scrPool,
false)
if check.IfNil(tx) {
missingSmartContractResults = append(missingSmartContractResults, txHash)
}
}
return sliceUtil.TrimSliceSliceByte(missingSmartContractResults)
}
// getAllScrsFromMiniBlock gets all the smartContractResults from a miniblock into a new structure
func (scr *smartContractResults) getAllScrsFromMiniBlock(
mb *block.MiniBlock,
haveTime func() bool,
) ([]*smartContractResult.SmartContractResult, [][]byte, error) {
strCache := process.ShardCacherIdentifier(mb.SenderShardID, mb.ReceiverShardID)
txCache := scr.scrPool.ShardDataStore(strCache)
if check.IfNil(txCache) {
return nil, nil, process.ErrNilUTxDataPool
}
// verify if all smartContractResult exists
scResSlice := make([]*smartContractResult.SmartContractResult, 0, len(mb.TxHashes))
txHashes := make([][]byte, 0, len(mb.TxHashes))
for _, txHash := range mb.TxHashes {
if !haveTime() {
return nil, nil, process.ErrTimeIsOut
}
tmp, _ := txCache.Peek(txHash)
if tmp == nil {
return nil, nil, process.ErrNilSmartContractResult
}
tx, ok := tmp.(*smartContractResult.SmartContractResult)
if !ok {
return nil, nil, process.ErrWrongTypeAssertion
}
txHashes = append(txHashes, txHash)
scResSlice = append(scResSlice, tx)
}
return smartContractResult.TrimSlicePtr(scResSlice), sliceUtil.TrimSliceSliceByte(txHashes), nil
}
// CreateAndProcessMiniBlocks creates miniblocks from storage and processes the reward transactions added into the miniblocks
// as long as it has time
func (scr *smartContractResults) CreateAndProcessMiniBlocks(_ func() bool) (block.MiniBlockSlice, error) {
return make(block.MiniBlockSlice, 0), nil
}
// ProcessMiniBlock processes all the smartContractResults from a and saves the processed smartContractResults in local cache complete miniblock
func (scr *smartContractResults) ProcessMiniBlock(miniBlock *block.MiniBlock, haveTime func() bool, _ func() (int, int)) ([][]byte, error) {
if miniBlock.Type != block.SmartContractResultBlock {
return nil, process.ErrWrongTypeInMiniBlock
}
miniBlockScrs, miniBlockTxHashes, err := scr.getAllScrsFromMiniBlock(miniBlock, haveTime)
if err != nil {
return nil, err
}
if scr.blockSizeComputation.IsMaxBlockSizeWithoutThrottleReached(1, len(miniBlockScrs)) {
return nil, process.ErrMaxBlockSizeReached
}
processedTxHashes := make([][]byte, 0)
defer func() {
if err != nil {
scr.gasHandler.RemoveGasConsumed(processedTxHashes)
scr.gasHandler.RemoveGasRefunded(processedTxHashes)
}
}()
gasConsumedByMiniBlockInSenderShard := uint64(0)
gasConsumedByMiniBlockInReceiverShard := uint64(0)
totalGasConsumedInSelfShard := scr.gasHandler.TotalGasConsumed()
log.Trace("smartContractResults.ProcessMiniBlock", "totalGasConsumedInSelfShard", totalGasConsumedInSelfShard)
for index := range miniBlockScrs {
if !haveTime() {
err = process.ErrTimeIsOut
return processedTxHashes, err
}
err = scr.computeGasConsumed(
miniBlock.SenderShardID,
miniBlock.ReceiverShardID,
miniBlockScrs[index],
miniBlockTxHashes[index],
&gasConsumedByMiniBlockInSenderShard,
&gasConsumedByMiniBlockInReceiverShard,
&totalGasConsumedInSelfShard)
if err != nil {
return processedTxHashes, err
}
processedTxHashes = append(processedTxHashes, miniBlockTxHashes[index])
}
for index := range miniBlockScrs {
if !haveTime() {
err = process.ErrTimeIsOut
return processedTxHashes, err
}
scr.saveAccountBalanceForAddress(miniBlockScrs[index].GetRcvAddr())
_, err = scr.scrProcessor.ProcessSmartContractResult(miniBlockScrs[index])
if err != nil {
return processedTxHashes, err
}
}
txShardInfoToSet := &txShardInfo{senderShardID: miniBlock.SenderShardID, receiverShardID: miniBlock.ReceiverShardID}
scr.scrForBlock.mutTxsForBlock.Lock()
for index, txHash := range miniBlockTxHashes {
scr.scrForBlock.txHashAndInfo[string(txHash)] = &txInfo{tx: miniBlockScrs[index], txShardInfo: txShardInfoToSet}
}
scr.scrForBlock.mutTxsForBlock.Unlock()
scr.blockSizeComputation.AddNumMiniBlocks(1)
scr.blockSizeComputation.AddNumTxs(len(miniBlockScrs))
return nil, nil
}
// CreateMarshalizedData marshalizes smartContractResults and creates and saves them into a new structure
func (scr *smartContractResults) CreateMarshalizedData(txHashes [][]byte) ([][]byte, error) {
mrsScrs, err := scr.createMarshalizedData(txHashes, &scr.scrForBlock)
if err != nil {
return nil, err
}
return mrsScrs, nil
}
// GetAllCurrentUsedTxs returns all the smartContractResults used at current creation / processing
func (scr *smartContractResults) GetAllCurrentUsedTxs() map[string]data.TransactionHandler {
scr.scrForBlock.mutTxsForBlock.RLock()
scrPool := make(map[string]data.TransactionHandler, len(scr.scrForBlock.txHashAndInfo))
for txHash, txInfoFromMap := range scr.scrForBlock.txHashAndInfo {
scrPool[txHash] = txInfoFromMap.tx
}
scr.scrForBlock.mutTxsForBlock.RUnlock()
return scrPool
}
// IsInterfaceNil returns true if there is no value under the interface
func (scr *smartContractResults) IsInterfaceNil() bool {
return scr == nil
}
func (scr *smartContractResults) isMiniBlockCorrect(mbType block.Type) bool {
return mbType == block.SmartContractResultBlock
}