-
Notifications
You must be signed in to change notification settings - Fork 199
/
oneMBPostProcessor.go
170 lines (139 loc) · 4.63 KB
/
oneMBPostProcessor.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
package postprocess
import (
"bytes"
"sort"
"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/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"
)
var _ process.IntermediateTransactionHandler = (*oneMBPostProcessor)(nil)
type oneMBPostProcessor struct {
blockType block.Type
*basePostProcessor
}
// NewOneMiniBlockPostProcessor creates a new intermediate results processor
func NewOneMiniBlockPostProcessor(
hasher hashing.Hasher,
marshalizer marshal.Marshalizer,
coordinator sharding.Coordinator,
store dataRetriever.StorageService,
blockType block.Type,
storageType dataRetriever.UnitType,
economicsFee process.FeeHandler,
) (*oneMBPostProcessor, 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(store) {
return nil, process.ErrNilStorage
}
if check.IfNil(economicsFee) {
return nil, process.ErrNilEconomicsFeeHandler
}
base := &basePostProcessor{
hasher: hasher,
marshalizer: marshalizer,
shardCoordinator: coordinator,
store: store,
storageType: storageType,
mapTxToResult: make(map[string][]string),
economicsFee: economicsFee,
}
opp := &oneMBPostProcessor{
basePostProcessor: base,
blockType: blockType,
}
opp.interResultsForBlock = make(map[string]*txInfo)
return opp, nil
}
// GetNumOfCrossInterMbsAndTxs returns the number of cross shard miniblocks and transactions for the current round,
// created from the receipts/bad transactions
func (opp *oneMBPostProcessor) GetNumOfCrossInterMbsAndTxs() (int, int) {
return 0, 0
}
// CreateAllInterMiniBlocks returns the miniblock for the current round created from the receipts/bad transactions
func (opp *oneMBPostProcessor) CreateAllInterMiniBlocks() []*block.MiniBlock {
selfId := opp.shardCoordinator.SelfId()
miniBlocks := make([]*block.MiniBlock, 0)
opp.mutInterResultsForBlock.Lock()
defer opp.mutInterResultsForBlock.Unlock()
if len(opp.interResultsForBlock) == 0 {
return miniBlocks
}
miniBlock := &block.MiniBlock{
Type: opp.blockType,
ReceiverShardID: selfId,
SenderShardID: selfId,
}
for key := range opp.interResultsForBlock {
miniBlock.TxHashes = append(miniBlock.TxHashes, []byte(key))
}
sort.Slice(miniBlock.TxHashes, func(a, b int) bool {
return bytes.Compare(miniBlock.TxHashes[a], miniBlock.TxHashes[b]) < 0
})
log.Trace("oneMBPostProcessor.CreateAllInterMiniBlocks",
"type", miniBlock.Type,
"senderShardID", miniBlock.SenderShardID,
"receiverShardID", miniBlock.ReceiverShardID,
)
for _, hash := range miniBlock.TxHashes {
log.Trace("tx", "hash", hash)
}
miniBlocks = append(miniBlocks, miniBlock)
opp.intraShardMiniBlock = miniBlock.Clone()
return miniBlocks
}
// VerifyInterMiniBlocks verifies if the receipts/bad transactions added to the block are valid
func (opp *oneMBPostProcessor) VerifyInterMiniBlocks(body *block.Body) error {
scrMbs := opp.CreateAllInterMiniBlocks()
createdMapMbs := createMiniBlocksMap(scrMbs)
verifiedOne := false
for i := 0; i < len(body.MiniBlocks); i++ {
mb := body.MiniBlocks[i]
if mb.Type != opp.blockType {
continue
}
if verifiedOne {
return process.ErrTooManyReceiptsMiniBlocks
}
err := opp.verifyMiniBlock(createdMapMbs, mb)
if err != nil {
return err
}
verifiedOne = true
}
return nil
}
// AddIntermediateTransactions adds receipts/bad transactions resulting from transaction processor
func (opp *oneMBPostProcessor) AddIntermediateTransactions(txs []data.TransactionHandler) error {
opp.mutInterResultsForBlock.Lock()
defer opp.mutInterResultsForBlock.Unlock()
selfId := opp.shardCoordinator.SelfId()
for i := 0; i < len(txs); i++ {
txHash, err := core.CalculateHash(opp.marshalizer, opp.hasher, txs[i])
if err != nil {
return err
}
addReceiptShardInfo := &txShardInfo{receiverShardID: selfId, senderShardID: selfId}
scrInfo := &txInfo{tx: txs[i], txShardInfo: addReceiptShardInfo}
opp.interResultsForBlock[string(txHash)] = scrInfo
opp.mapTxToResult[string(txHash)] = append(opp.mapTxToResult[string(txHash)], string(txHash))
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (opp *oneMBPostProcessor) IsInterfaceNil() bool {
return opp == nil
}