-
Notifications
You must be signed in to change notification settings - Fork 199
/
intermediateResults.go
258 lines (213 loc) · 7.53 KB
/
intermediateResults.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
package postprocess
import (
"bytes"
"sort"
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/smartContractResult"
"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/davecgh/go-spew/spew"
)
var _ process.IntermediateTransactionHandler = (*intermediateResultsProcessor)(nil)
type intermediateResultsProcessor struct {
pubkeyConv core.PubkeyConverter
blockType block.Type
currTxs dataRetriever.TransactionCacher
*basePostProcessor
}
// NewIntermediateResultsProcessor creates a new intermediate results processor
func NewIntermediateResultsProcessor(
hasher hashing.Hasher,
marshalizer marshal.Marshalizer,
coordinator sharding.Coordinator,
pubkeyConv core.PubkeyConverter,
store dataRetriever.StorageService,
blockType block.Type,
currTxs dataRetriever.TransactionCacher,
) (*intermediateResultsProcessor, error) {
if check.IfNil(hasher) {
return nil, process.ErrNilHasher
}
if check.IfNil(marshalizer) {
return nil, process.ErrNilMarshalizer
}
if check.IfNil(coordinator) {
return nil, process.ErrNilShardCoordinator
}
if check.IfNil(pubkeyConv) {
return nil, process.ErrNilPubkeyConverter
}
if check.IfNil(store) {
return nil, process.ErrNilStorage
}
if check.IfNil(currTxs) {
return nil, process.ErrNilTxForCurrentBlockHandler
}
base := &basePostProcessor{
hasher: hasher,
marshalizer: marshalizer,
shardCoordinator: coordinator,
store: store,
storageType: dataRetriever.UnsignedTransactionUnit,
mapTxToResult: make(map[string][]string),
}
irp := &intermediateResultsProcessor{
basePostProcessor: base,
pubkeyConv: pubkeyConv,
blockType: blockType,
currTxs: currTxs,
}
irp.interResultsForBlock = make(map[string]*txInfo)
return irp, nil
}
// GetNumOfCrossInterMbsAndTxs returns the number of cross shard miniblocks and transactions for the current round,
// created from the smart contract results
func (irp *intermediateResultsProcessor) GetNumOfCrossInterMbsAndTxs() (int, int) {
miniBlocks := make(map[uint32]int)
irp.mutInterResultsForBlock.Lock()
for _, tx := range irp.interResultsForBlock {
if tx.receiverShardID == irp.shardCoordinator.SelfId() {
continue
}
miniBlocks[tx.receiverShardID]++
}
irp.mutInterResultsForBlock.Unlock()
numMbs := len(miniBlocks)
numTxs := 0
for _, numTxsInMb := range miniBlocks {
numTxs += numTxsInMb
}
return numMbs, numTxs
}
// CreateAllInterMiniBlocks returns the miniblocks for the current round created from the smart contract results
func (irp *intermediateResultsProcessor) CreateAllInterMiniBlocks() []*block.MiniBlock {
miniBlocks := make(map[uint32]*block.MiniBlock, int(irp.shardCoordinator.NumberOfShards())+1)
for i := uint32(0); i < irp.shardCoordinator.NumberOfShards(); i++ {
miniBlocks[i] = &block.MiniBlock{}
}
miniBlocks[core.MetachainShardId] = &block.MiniBlock{}
irp.currTxs.Clean()
irp.mutInterResultsForBlock.Lock()
for key, value := range irp.interResultsForBlock {
recvShId := value.receiverShardID
miniBlocks[recvShId].TxHashes = append(miniBlocks[recvShId].TxHashes, []byte(key))
irp.currTxs.AddTx([]byte(key), value.tx)
}
finalMBs := make([]*block.MiniBlock, 0)
for shId, miniblock := range miniBlocks {
if len(miniblock.TxHashes) > 0 {
miniblock.SenderShardID = irp.shardCoordinator.SelfId()
miniblock.ReceiverShardID = shId
miniblock.Type = irp.blockType
sort.Slice(miniblock.TxHashes, func(a, b int) bool {
return bytes.Compare(miniblock.TxHashes[a], miniblock.TxHashes[b]) < 0
})
log.Trace("intermediateResultsProcessor.CreateAllInterMiniBlocks",
"type", miniblock.Type,
"senderShardID", miniblock.SenderShardID,
"receiverShardID", miniblock.ReceiverShardID,
"numTxs", len(miniblock.TxHashes),
)
finalMBs = append(finalMBs, miniblock)
if miniblock.ReceiverShardID == irp.shardCoordinator.SelfId() {
irp.intraShardMiniBlock = miniblock.Clone()
}
}
}
sort.Slice(finalMBs, func(i, j int) bool {
return finalMBs[i].ReceiverShardID < finalMBs[j].ReceiverShardID
})
irp.mutInterResultsForBlock.Unlock()
return finalMBs
}
// VerifyInterMiniBlocks verifies if the smart contract results added to the block are valid
func (irp *intermediateResultsProcessor) VerifyInterMiniBlocks(body *block.Body) error {
scrMbs := irp.CreateAllInterMiniBlocks()
createdMapMbs := make(map[uint32]*block.MiniBlock)
for _, mb := range scrMbs {
createdMapMbs[mb.ReceiverShardID] = mb
}
countedCrossShard := 0
for i := 0; i < len(body.MiniBlocks); i++ {
mb := body.MiniBlocks[i]
if mb.Type != irp.blockType {
continue
}
if mb.ReceiverShardID == irp.shardCoordinator.SelfId() {
continue
}
countedCrossShard++
err := irp.verifyMiniBlock(createdMapMbs, mb)
if err != nil {
return err
}
}
createCrossShard := 0
for _, mb := range scrMbs {
if mb.Type != irp.blockType {
continue
}
if mb.ReceiverShardID == irp.shardCoordinator.SelfId() {
continue
}
createCrossShard++
}
if createCrossShard != countedCrossShard {
return process.ErrMiniBlockNumMissMatch
}
return nil
}
// AddIntermediateTransactions adds smart contract results from smart contract processing for cross-shard calls
func (irp *intermediateResultsProcessor) AddIntermediateTransactions(txs []data.TransactionHandler) error {
irp.mutInterResultsForBlock.Lock()
defer irp.mutInterResultsForBlock.Unlock()
log.Trace("intermediateResultsProcessor.AddIntermediateTransactions()", "txs", len(txs))
for i := 0; i < len(txs); i++ {
addScr, ok := txs[i].(*smartContractResult.SmartContractResult)
if !ok {
return process.ErrWrongTypeAssertion
}
scrHash, err := core.CalculateHash(irp.marshalizer, irp.hasher, addScr)
if err != nil {
return err
}
if log.GetLevel() == logger.LogTrace {
//spew.Sdump is very useful when debugging errors like `receipts hash mismatch`
log.Trace("scr added", "txHash", addScr.PrevTxHash, "hash", scrHash, "nonce", addScr.Nonce, "gasLimit", addScr.GasLimit, "value", addScr.Value,
"dump", spew.Sdump(addScr))
}
sndShId, dstShId, err := irp.getShardIdsFromAddresses(addScr.SndAddr, addScr.RcvAddr)
if err != nil {
return err
}
addScrShardInfo := &txShardInfo{receiverShardID: dstShId, senderShardID: sndShId}
scrInfo := &txInfo{tx: addScr, txShardInfo: addScrShardInfo}
irp.interResultsForBlock[string(scrHash)] = scrInfo
irp.mapTxToResult[string(addScr.PrevTxHash)] = append(irp.mapTxToResult[string(addScr.PrevTxHash)], string(scrHash))
}
return nil
}
func (irp *intermediateResultsProcessor) getShardIdsFromAddresses(sndAddr []byte, rcvAddr []byte) (uint32, uint32, error) {
shardForSrc := irp.shardCoordinator.ComputeId(sndAddr)
shardForDst := irp.shardCoordinator.ComputeId(rcvAddr)
isEmptyAddress := bytes.Equal(sndAddr, make([]byte, irp.pubkeyConv.Len()))
if isEmptyAddress {
shardForSrc = irp.shardCoordinator.SelfId()
}
isEmptyAddress = bytes.Equal(rcvAddr, make([]byte, irp.pubkeyConv.Len()))
if isEmptyAddress {
shardForDst = irp.shardCoordinator.SelfId()
}
return shardForSrc, shardForDst, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (irp *intermediateResultsProcessor) IsInterfaceNil() bool {
return irp == nil
}