/
node_cross_shard.go
executable file
·135 lines (124 loc) · 4.82 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
130
131
132
133
134
135
package node
import (
"github.com/ethereum/go-ethereum/rlp"
proto_node "github.com/zennittians/intelchain/api/proto/node"
"github.com/zennittians/intelchain/block"
"github.com/zennittians/intelchain/consensus"
"github.com/zennittians/intelchain/core"
"github.com/zennittians/intelchain/core/types"
nodeconfig "github.com/zennittians/intelchain/internal/configs/node"
"github.com/zennittians/intelchain/internal/utils"
"github.com/zennittians/intelchain/p2p"
"github.com/zennittians/intelchain/shard"
)
// BroadcastCXReceipts broadcasts cross shard receipts to correspoding
// destination shards
func BroadcastCXReceipts(newBlock *types.Block, consensus *consensus.Consensus) {
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 := 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
}
BroadcastCXReceiptsWithShardID(newBlock.Header(), commitSig, commitBitmap, uint32(i), consensus)
}
}
// BroadcastCXReceiptsWithShardID broadcasts cross shard receipts to given ToShardID
func BroadcastCXReceiptsWithShardID(block *block.Header, commitSig []byte, commitBitmap []byte, toShardID uint32, consensus *consensus.Consensus) {
myShardID := consensus.ShardID
utils.Logger().Debug().
Uint32("toShardID", toShardID).
Uint32("myShardID", myShardID).
Uint64("blockNum", block.NumberU64()).
Msg("[BroadcastCXReceiptsWithShardID]")
cxReceipts, err := consensus.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 := consensus.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,
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 consensus.GetHost().SendMessageToGroups([]nodeconfig.GroupID{groupID},
p2p.ConstructMessage(proto_node.ConstructCXReceiptsProof(cxReceiptsProof)),
)
}
// BroadcastMissingCXReceipts broadcasts missing cross shard receipts per request
func BroadcastMissingCXReceipts(c *consensus.Consensus) {
var (
sendNextTime = make([]core.CxEntry, 0)
cxPool = c.Registry().GetCxPool()
blockchain = c.Blockchain()
)
it := cxPool.Pool().Iterator()
for entry := range it.C {
cxEntry := entry.(core.CxEntry)
toShardID := cxEntry.ToShardID
blk := blockchain.GetBlockByHash(cxEntry.BlockHash)
if blk == nil {
continue
}
blockNum := blk.NumberU64()
nextHeader := blockchain.GetHeaderByNumber(blockNum + 1)
if nextHeader == nil {
sendNextTime = append(sendNextTime, cxEntry)
continue
}
sig := nextHeader.LastCommitSignature()
bitmap := nextHeader.LastCommitBitmap()
BroadcastCXReceiptsWithShardID(blk.Header(), sig[:], bitmap, toShardID, c)
}
cxPool.Clear()
// this should not happen or maybe happen for impatient user
for _, entry := range sendNextTime {
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)
}