-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_cross_shard.go
129 lines (118 loc) · 4.63 KB
/
node_cross_shard.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
package node
import (
proto_node "github.com/PositionExchange/posichain/api/proto/node"
"github.com/PositionExchange/posichain/core"
"github.com/PositionExchange/posichain/core/types"
nodeconfig "github.com/PositionExchange/posichain/internal/configs/node"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/p2p"
"github.com/PositionExchange/posichain/shard"
"github.com/ethereum/go-ethereum/rlp"
)
// BroadcastCXReceipts broadcasts cross shard receipts to correspoding
// destination shards
func (node *Node) BroadcastCXReceipts(newBlock *types.Block) {
commitSigAndBitmap := newBlock.GetCurrentCommitSig()
//#### Read payload data from committed msg
if len(commitSigAndBitmap) <= 96 {
utils.Logger().Debug().Int("commitSigAndBitmapLen", len(commitSigAndBitmap)).Msg("[BroadcastCXReceipts] commitSigAndBitmap Not Enough Length")
return
}
commitSig := make([]byte, 96)
commitBitmap := make([]byte, len(commitSigAndBitmap)-96)
offset := 0
copy(commitSig[:], commitSigAndBitmap[offset:offset+96])
offset += 96
copy(commitBitmap[:], commitSigAndBitmap[offset:])
//#### END Read payload data from committed msg
epoch := newBlock.Header().Epoch()
shardingConfig := shard.Schedule.InstanceForEpoch(epoch)
shardNum := int(shardingConfig.NumShards())
myShardID := node.Consensus.ShardID
utils.Logger().Info().Int("shardNum", shardNum).Uint32("myShardID", myShardID).Uint64("blockNum", newBlock.NumberU64()).Msg("[BroadcastCXReceipts]")
for i := 0; i < shardNum; i++ {
if i == int(myShardID) {
continue
}
node.BroadcastCXReceiptsWithShardID(newBlock, commitSig, commitBitmap, uint32(i))
}
}
// BroadcastCXReceiptsWithShardID broadcasts cross shard receipts to given ToShardID
func (node *Node) BroadcastCXReceiptsWithShardID(block *types.Block, commitSig []byte, commitBitmap []byte, toShardID uint32) {
myShardID := node.Consensus.ShardID
utils.Logger().Debug().
Uint32("toShardID", toShardID).
Uint32("myShardID", myShardID).
Uint64("blockNum", block.NumberU64()).
Msg("[BroadcastCXReceiptsWithShardID]")
cxReceipts, err := node.Blockchain().ReadCXReceipts(toShardID, block.NumberU64(), block.Hash())
if err != nil || len(cxReceipts) == 0 {
utils.Logger().Debug().Uint32("ToShardID", toShardID).
Int("numCXReceipts", len(cxReceipts)).
Msg("[CXMerkleProof] No receipts found for the destination shard")
return
}
merkleProof, err := node.Blockchain().CXMerkleProof(toShardID, block)
if err != nil {
utils.Logger().Warn().
Uint32("ToShardID", toShardID).
Msg("[BroadcastCXReceiptsWithShardID] Unable to get merkleProof")
return
}
cxReceiptsProof := &types.CXReceiptsProof{
Receipts: cxReceipts,
MerkleProof: merkleProof,
Header: block.Header(),
CommitSig: commitSig,
CommitBitmap: commitBitmap,
}
groupID := nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(toShardID))
utils.Logger().Info().Uint32("ToShardID", toShardID).
Str("GroupID", string(groupID)).
Interface("cxp", cxReceiptsProof).
Msg("[BroadcastCXReceiptsWithShardID] ReadCXReceipts and MerkleProof ready. Sending CX receipts...")
// TODO ek – limit concurrency
go node.host.SendMessageToGroups([]nodeconfig.GroupID{groupID},
p2p.ConstructMessage(proto_node.ConstructCXReceiptsProof(cxReceiptsProof)),
)
}
// BroadcastMissingCXReceipts broadcasts missing cross shard receipts per request
func (node *Node) BroadcastMissingCXReceipts() {
sendNextTime := []core.CxEntry{}
it := node.CxPool.Pool().Iterator()
for entry := range it.C {
cxEntry := entry.(core.CxEntry)
toShardID := cxEntry.ToShardID
blk := node.Blockchain().GetBlockByHash(cxEntry.BlockHash)
if blk == nil {
continue
}
blockNum := blk.NumberU64()
nextHeader := node.Blockchain().GetHeaderByNumber(blockNum + 1)
if nextHeader == nil {
sendNextTime = append(sendNextTime, cxEntry)
continue
}
sig := nextHeader.LastCommitSignature()
bitmap := nextHeader.LastCommitBitmap()
node.BroadcastCXReceiptsWithShardID(blk, sig[:], bitmap, toShardID)
}
node.CxPool.Clear()
// this should not happen or maybe happen for impatient user
for _, entry := range sendNextTime {
node.CxPool.Add(entry)
}
}
// ProcessReceiptMessage store the receipts and merkle proof in local data store
func (node *Node) ProcessReceiptMessage(msgPayload []byte) {
cxp := types.CXReceiptsProof{}
if err := rlp.DecodeBytes(msgPayload, &cxp); err != nil {
utils.Logger().Error().Err(err).
Msg("[ProcessReceiptMessage] Unable to Decode message Payload")
return
}
utils.Logger().Debug().Interface("cxp", cxp).
Msg("[ProcessReceiptMessage] Add CXReceiptsProof to pending Receipts")
// TODO: integrate with txpool
node.AddPendingReceipts(&cxp)
}