-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_cross_link.go
145 lines (124 loc) · 4.87 KB
/
node_cross_link.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
package node
import (
"github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/shard"
common2 "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
const (
maxPendingCrossLinkSize = 1000
crossLinkBatchSize = 3
)
var (
errAlreadyExist = errors.New("crosslink already exist")
)
// VerifyBlockCrossLinks verifies the cross links of the block
func (node *Node) VerifyBlockCrossLinks(block *types.Block) error {
cxLinksData := block.Header().CrossLinks()
if len(cxLinksData) == 0 {
utils.Logger().Debug().Msgf("[CrossLinkVerification] Zero CrossLinks in the header")
return nil
}
crossLinks := types.CrossLinks{}
err := rlp.DecodeBytes(cxLinksData, &crossLinks)
if err != nil {
return errors.Wrapf(
err, "[CrossLinkVerification] failed to decode cross links",
)
}
if !crossLinks.IsSorted() {
return errors.New("[CrossLinkVerification] cross links are not sorted")
}
for _, crossLink := range crossLinks {
cl, err := node.Blockchain().ReadCrossLink(crossLink.ShardID(), crossLink.BlockNum())
if err == nil && cl != nil {
// Add slash for exist same blocknum but different crosslink
return errAlreadyExist
}
if err := node.VerifyCrossLink(crossLink); err != nil {
return errors.Wrapf(err, "cannot VerifyBlockCrossLinks")
}
}
return nil
}
// ProcessCrossLinkHeartbeatMessage process crosslink heart beat signal.
func (node *Node) ProcessCrossLinkHeartbeatMessage(msgPayload []byte) {
if node.IsRunningBeaconChain() {
return
}
// process in next pr.
}
// ProcessCrossLinkMessage verify and process Node/CrossLink message into crosslink when it's valid
func (node *Node) ProcessCrossLinkMessage(msgPayload []byte) {
if node.IsRunningBeaconChain() {
pendingCLs, err := node.Blockchain().ReadPendingCrossLinks()
if err == nil && len(pendingCLs) >= maxPendingCrossLinkSize {
utils.Logger().Debug().
Msgf("[ProcessingCrossLink] Pending Crosslink reach maximum size: %d", len(pendingCLs))
return
}
existingCLs := map[common2.Hash]struct{}{}
for _, pending := range pendingCLs {
existingCLs[pending.Hash()] = struct{}{}
}
var crosslinks []types.CrossLink
if err := rlp.DecodeBytes(msgPayload, &crosslinks); err != nil {
utils.Logger().Error().
Err(err).
Msg("[ProcessingCrossLink] Crosslink Message Broadcast Unable to Decode")
return
}
var candidates []types.CrossLink
utils.Logger().Debug().
Msgf("[ProcessingCrossLink] Received crosslinks: %d", len(crosslinks))
for i, cl := range crosslinks {
if i > crossLinkBatchSize*2 { // A sanity check to prevent spamming
break
}
if _, ok := existingCLs[cl.Hash()]; ok {
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "duplicate_crosslink"}).Inc()
utils.Logger().Debug().Err(err).
Msgf("[ProcessingCrossLink] Cross Link already exists in pending queue, pass. Beacon Epoch: %d, Block num: %d, Epoch: %d, shardID %d",
node.Blockchain().CurrentHeader().Epoch(), cl.Number(), cl.Epoch(), cl.ShardID())
continue
}
exist, err := node.Blockchain().ReadCrossLink(cl.ShardID(), cl.Number().Uint64())
if err == nil && exist != nil {
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "duplicate_crosslink"}).Inc()
utils.Logger().Debug().Err(err).
Msgf("[ProcessingCrossLink] Cross Link already exists, pass. Beacon Epoch: %d, Block num: %d, Epoch: %d, shardID %d", node.Blockchain().CurrentHeader().Epoch(), cl.Number(), cl.Epoch(), cl.ShardID())
continue
}
if err = node.VerifyCrossLink(cl); err != nil {
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "invalid_crosslink"}).Inc()
utils.Logger().Info().
Str("cross-link-issue", err.Error()).
Msgf("[ProcessingCrossLink] Failed to verify new cross link for blockNum %d epochNum %d shard %d skipped: %v", cl.BlockNum(), cl.Epoch().Uint64(), cl.ShardID(), cl)
continue
}
candidates = append(candidates, cl)
nodeCrossLinkMessageCounterVec.With(prometheus.Labels{"type": "new_crosslink"}).Inc()
utils.Logger().Debug().
Msgf("[ProcessingCrossLink] Committing for shardID %d, blockNum %d",
cl.ShardID(), cl.Number().Uint64(),
)
}
Len, _ := node.Blockchain().AddPendingCrossLinks(candidates)
utils.Logger().Debug().
Msgf("[ProcessingCrossLink] Add pending crosslinks, total pending: %d", Len)
}
}
// VerifyCrossLink verifies the header is valid
func (node *Node) VerifyCrossLink(cl types.CrossLink) error {
if node.Blockchain().ShardID() != shard.BeaconChainShardID {
return errors.New("[VerifyCrossLink] Shard chains should not verify cross links")
}
engine := node.Blockchain().Engine()
if err := engine.VerifyCrossLink(node.Blockchain(), cl); err != nil {
return errors.Wrap(err, "[VerifyCrossLink]")
}
return nil
}