-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_newblock.go
393 lines (345 loc) · 13.2 KB
/
node_newblock.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
package node
import (
"errors"
"sort"
"strings"
"time"
"github.com/PositionExchange/posichain/consensus"
"github.com/PositionExchange/posichain/crypto/bls"
staking "github.com/PositionExchange/posichain/staking/types"
"github.com/PositionExchange/posichain/core/rawdb"
"github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/shard"
"github.com/ethereum/go-ethereum/common"
)
// Constants of proposing a new block
const (
SleepPeriod = 20 * time.Millisecond
IncomingReceiptsLimit = 6000 // 2000 * (numShards - 1)
)
// WaitForConsensusReadyV2 listen for the readiness signal from consensus and generate new block for consensus.
// only leader will receive the ready signal
func (node *Node) WaitForConsensusReadyV2(readySignal chan consensus.ProposalType, commitSigsChan chan []byte, stopChan chan struct{}, stoppedChan chan struct{}) {
go func() {
// Setup stoppedChan
defer close(stoppedChan)
utils.Logger().Debug().
Msg("Waiting for Consensus ready")
select {
case <-time.After(30 * time.Second):
case <-stopChan:
return
}
for {
// keep waiting for Consensus ready
select {
case <-stopChan:
utils.Logger().Warn().
Msg("Consensus new block proposal: STOPPED!")
return
case proposalType := <-readySignal:
retryCount := 0
for node.Consensus != nil && node.Consensus.IsLeader() {
time.Sleep(SleepPeriod)
utils.Logger().Info().
Uint64("blockNum", node.Blockchain().CurrentBlock().NumberU64()+1).
Bool("asyncProposal", proposalType == consensus.AsyncProposal).
Msg("PROPOSING NEW BLOCK ------------------------------------------------")
// Prepare last commit signatures
newCommitSigsChan := make(chan []byte)
go func() {
waitTime := 0 * time.Second
if proposalType == consensus.AsyncProposal {
waitTime = consensus.CommitSigReceiverTimeout
}
select {
case <-time.After(waitTime):
if waitTime == 0 {
utils.Logger().Info().Msg("[ProposeNewBlock] Sync block proposal, reading commit sigs directly from DB")
} else {
utils.Logger().Info().Msg("[ProposeNewBlock] Timeout waiting for commit sigs, reading directly from DB")
}
sigs, err := node.Consensus.BlockCommitSigs(node.Blockchain().CurrentBlock().NumberU64())
if err != nil {
utils.Logger().Error().Err(err).Msg("[ProposeNewBlock] Cannot get commit signatures from last block")
} else {
newCommitSigsChan <- sigs
}
case commitSigs := <-commitSigsChan:
utils.Logger().Info().Msg("[ProposeNewBlock] received commit sigs asynchronously")
if len(commitSigs) > bls.BLSSignatureSizeInBytes {
newCommitSigsChan <- commitSigs
}
}
}()
newBlock, err := node.ProposeNewBlock(newCommitSigsChan)
if err == nil {
if blk, ok := node.proposedBlock[newBlock.NumberU64()]; ok {
utils.Logger().Info().Uint64("blockNum", newBlock.NumberU64()).Str("blockHash", blk.Hash().Hex()).
Msg("Block with the same number was already proposed, abort.")
}
utils.Logger().Info().
Uint64("blockNum", newBlock.NumberU64()).
Uint64("epoch", newBlock.Epoch().Uint64()).
Uint64("viewID", newBlock.Header().ViewID().Uint64()).
Int("numTxs", newBlock.Transactions().Len()).
Int("numStakingTxs", newBlock.StakingTransactions().Len()).
Int("crossShardReceipts", newBlock.IncomingReceipts().Len()).
Msg("=========Successfully Proposed New Block==========")
// Send the new block to Consensus so it can be confirmed.
node.proposedBlock[newBlock.NumberU64()] = newBlock
delete(node.proposedBlock, newBlock.NumberU64()-10)
node.BlockChannel <- newBlock
break
} else {
retryCount++
utils.Logger().Err(err).Int("retryCount", retryCount).
Msg("!!!!!!!!!Failed Proposing New Block!!!!!!!!!")
if retryCount > 3 {
// break to avoid repeated failures
break
}
continue
}
}
}
}
}()
}
// ProposeNewBlock proposes a new block...
func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error) {
currentHeader := node.Blockchain().CurrentHeader()
nowEpoch, blockNow := currentHeader.Epoch(), currentHeader.Number()
utils.AnalysisStart("ProposeNewBlock", nowEpoch, blockNow)
defer utils.AnalysisEnd("ProposeNewBlock", nowEpoch, blockNow)
node.Worker.UpdateCurrent()
header := node.Worker.GetCurrentHeader()
// Update worker's current header and
// state data in preparation to propose/process new transactions
leaderKey := node.Consensus.GetLeaderPubKey()
var (
coinbase = node.GetAddressForBLSKey(leaderKey.Object, header.Epoch())
beneficiary = coinbase
err error
)
// After staking, all coinbase will be the address of bls pub key
if node.Blockchain().Config().IsStaking(header.Epoch()) {
blsPubKeyBytes := leaderKey.Object.GetAddress()
coinbase.SetBytes(blsPubKeyBytes[:])
}
emptyAddr := common.Address{}
if coinbase == emptyAddr {
return nil, errors.New("[ProposeNewBlock] Failed setting coinbase")
}
// Must set coinbase here because the operations below depend on it
header.SetCoinbase(coinbase)
// Get beneficiary based on coinbase
// Before staking, coinbase itself is the beneficial
// After staking, beneficial is the corresponding ECDSA address of the bls key
beneficiary, err = node.Blockchain().GetECDSAFromCoinbase(header)
if err != nil {
return nil, err
}
// Add VRF
if node.Blockchain().Config().IsVRF(header.Epoch()) {
//generate a new VRF for the current block
if err := node.Consensus.GenerateVrfAndProof(header); err != nil {
return nil, err
}
}
if !shard.Schedule.IsLastBlock(header.Number().Uint64()) {
// Prepare normal and staking transactions retrieved from transaction pool
utils.AnalysisStart("proposeNewBlockChooseFromTxnPool")
pendingPoolTxs, err := node.TxPool.Pending()
if err != nil {
utils.Logger().Err(err).Msg("Failed to fetch pending transactions")
return nil, err
}
pendingPlainTxs := map[common.Address]types.Transactions{}
pendingStakingTxs := staking.StakingTransactions{}
for addr, poolTxs := range pendingPoolTxs {
plainTxsPerAcc := types.Transactions{}
for _, tx := range poolTxs {
if plainTx, ok := tx.(*types.Transaction); ok {
plainTxsPerAcc = append(plainTxsPerAcc, plainTx)
} else if stakingTx, ok := tx.(*staking.StakingTransaction); ok {
// Only process staking transactions after pre-staking epoch happened.
if node.Blockchain().Config().IsPreStaking(node.Worker.GetCurrentHeader().Epoch()) {
pendingStakingTxs = append(pendingStakingTxs, stakingTx)
}
} else {
utils.Logger().Err(types.ErrUnknownPoolTxType).
Msg("Failed to parse pending transactions")
return nil, types.ErrUnknownPoolTxType
}
}
if plainTxsPerAcc.Len() > 0 {
pendingPlainTxs[addr] = plainTxsPerAcc
}
}
// Try commit normal and staking transactions based on the current state
// The successfully committed transactions will be put in the proposed block
if err := node.Worker.CommitTransactions(
pendingPlainTxs, pendingStakingTxs, beneficiary,
); err != nil {
utils.Logger().Error().Err(err).Msg("cannot commit transactions")
return nil, err
}
utils.AnalysisEnd("proposeNewBlockChooseFromTxnPool")
}
// Prepare cross shard transaction receipts
receiptsList := node.proposeReceiptsProof()
if len(receiptsList) != 0 {
if err := node.Worker.CommitReceipts(receiptsList); err != nil {
return nil, err
}
}
isBeaconchainInCrossLinkEra := node.NodeConfig.ShardID == shard.BeaconChainShardID &&
node.Blockchain().Config().IsCrossLink(node.Worker.GetCurrentHeader().Epoch())
isBeaconchainInStakingEra := node.NodeConfig.ShardID == shard.BeaconChainShardID &&
node.Blockchain().Config().IsStaking(node.Worker.GetCurrentHeader().Epoch())
utils.AnalysisStart("proposeNewBlockVerifyCrossLinks")
// Prepare cross links and slashing messages
var crossLinksToPropose types.CrossLinks
if isBeaconchainInCrossLinkEra {
allPending, err := node.Blockchain().ReadPendingCrossLinks()
invalidToDelete := []types.CrossLink{}
if err == nil {
for _, pending := range allPending {
// ReadCrossLink beacon chain usage.
exist, err := node.Blockchain().ReadCrossLink(pending.ShardID(), pending.BlockNum())
if err == nil || exist != nil {
invalidToDelete = append(invalidToDelete, pending)
utils.Logger().Debug().
AnErr("[ProposeNewBlock] pending crosslink is already committed onchain", err)
continue
}
// Crosslink is already verified before it's accepted to pending,
// no need to verify again in proposal.
if !node.Blockchain().Config().IsCrossLink(pending.Epoch()) {
utils.Logger().Debug().
AnErr("[ProposeNewBlock] pending crosslink that's before crosslink epoch", err)
continue
}
crossLinksToPropose = append(crossLinksToPropose, pending)
if len(crossLinksToPropose) > 15 {
break
}
}
utils.Logger().Info().
Msgf("[ProposeNewBlock] Proposed %d crosslinks from %d pending crosslinks",
len(crossLinksToPropose), len(allPending),
)
} else {
utils.Logger().Error().Err(err).Msgf(
"[ProposeNewBlock] Unable to Read PendingCrossLinks, number of crosslinks: %d",
len(allPending),
)
}
node.Blockchain().DeleteFromPendingCrossLinks(invalidToDelete)
}
utils.AnalysisEnd("proposeNewBlockVerifyCrossLinks")
if isBeaconchainInStakingEra {
// this will set a meaningful w.current.slashes
if err := node.Worker.CollectVerifiedSlashes(); err != nil {
return nil, err
}
}
// Prepare shard state
var shardState *shard.State
if shardState, err = node.Blockchain().SuperCommitteeForNextEpoch(
node.Beaconchain(), node.Worker.GetCurrentHeader(), false,
); err != nil {
return nil, err
}
viewIDFunc := func() uint64 {
return node.Consensus.GetCurBlockViewID()
}
finalizedBlock, err := node.Worker.FinalizeNewBlock(
commitSigs, viewIDFunc,
coinbase, crossLinksToPropose, shardState,
)
if err != nil {
utils.Logger().Error().Err(err).Msg("[ProposeNewBlock] Failed finalizing the new block")
return nil, err
}
utils.Logger().Info().Msg("[ProposeNewBlock] verifying the new block header")
err = node.Blockchain().Validator().ValidateHeader(finalizedBlock, true)
if err != nil {
utils.Logger().Error().Err(err).Msg("[ProposeNewBlock] Failed verifying the new block header")
return nil, err
}
// Save process result in the cache for later use for faster block commitment to db.
result := node.Worker.GetCurrentResult()
node.Blockchain().Processor().CacheProcessorResult(finalizedBlock.Hash(), result)
return finalizedBlock, nil
}
func (node *Node) proposeReceiptsProof() []*types.CXReceiptsProof {
if !node.Blockchain().Config().HasCrossTxFields(node.Worker.GetCurrentHeader().Epoch()) {
return []*types.CXReceiptsProof{}
}
numProposed := 0
validReceiptsList := []*types.CXReceiptsProof{}
pendingReceiptsList := []*types.CXReceiptsProof{}
node.pendingCXMutex.Lock()
defer node.pendingCXMutex.Unlock()
// not necessary to sort the list, but we just prefer to process the list ordered by shard and blocknum
pendingCXReceipts := []*types.CXReceiptsProof{}
for _, v := range node.pendingCXReceipts {
pendingCXReceipts = append(pendingCXReceipts, v)
}
sort.SliceStable(pendingCXReceipts, func(i, j int) bool {
shardCMP := pendingCXReceipts[i].MerkleProof.ShardID < pendingCXReceipts[j].MerkleProof.ShardID
shardEQ := pendingCXReceipts[i].MerkleProof.ShardID == pendingCXReceipts[j].MerkleProof.ShardID
blockCMP := pendingCXReceipts[i].MerkleProof.BlockNum.Cmp(
pendingCXReceipts[j].MerkleProof.BlockNum,
) == -1
return shardCMP || (shardEQ && blockCMP)
})
m := map[common.Hash]struct{}{}
Loop:
for _, cxp := range node.pendingCXReceipts {
if numProposed > IncomingReceiptsLimit {
pendingReceiptsList = append(pendingReceiptsList, cxp)
continue
}
// check double spent
if node.Blockchain().IsSpent(cxp) {
utils.Logger().Debug().Interface("cxp", cxp).Msg("[proposeReceiptsProof] CXReceipt is spent")
continue
}
hash := cxp.MerkleProof.BlockHash
// ignore duplicated receipts
if _, ok := m[hash]; ok {
continue
} else {
m[hash] = struct{}{}
}
for _, item := range cxp.Receipts {
if item.ToShardID != node.Blockchain().ShardID() {
continue Loop
}
}
if err := node.Blockchain().Validator().ValidateCXReceiptsProof(cxp); err != nil {
if strings.Contains(err.Error(), rawdb.MsgNoShardStateFromDB) {
pendingReceiptsList = append(pendingReceiptsList, cxp)
} else {
utils.Logger().Error().Err(err).Msg("[proposeReceiptsProof] Invalid CXReceiptsProof")
}
continue
}
utils.Logger().Debug().Interface("cxp", cxp).Msg("[proposeReceiptsProof] CXReceipts Added")
validReceiptsList = append(validReceiptsList, cxp)
numProposed = numProposed + len(cxp.Receipts)
}
node.pendingCXReceipts = make(map[string]*types.CXReceiptsProof)
for _, v := range pendingReceiptsList {
blockNum := v.Header.Number().Uint64()
shardID := v.Header.ShardID()
key := utils.GetPendingCXKey(shardID, blockNum)
node.pendingCXReceipts[key] = v
}
utils.Logger().Debug().Msgf("[proposeReceiptsProof] number of validReceipts %d", len(validReceiptsList))
return validReceiptsList
}