/
processor.go
206 lines (177 loc) · 6.81 KB
/
processor.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
/**
* Copyright (c) 2018-present, MultiVAC Foundation.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package storagenode
import (
"github.com/multivactech/MultiVAC/interface/isync"
"github.com/multivactech/MultiVAC/logger/btclog"
"github.com/multivactech/MultiVAC/model/chaincfg/multivacaddress"
"github.com/multivactech/MultiVAC/model/shard"
"github.com/multivactech/MultiVAC/model/wire"
"github.com/multivactech/MultiVAC/p2p/connection"
"github.com/multivactech/MultiVAC/p2p/peer"
)
// Processor manages a storage node.
type Processor struct {
sn *threadedStorageNode
logger btclog.Logger
syncMgr isync.SyncManager
msgQueue chan *connection.MessageAndReply
}
// Start a storage node.
func (pcr *Processor) Start() {
// init msgQueue
pcr.msgQueue = make(chan *connection.MessageAndReply, 1000)
// register channel
tag := []connection.Tag{
{Msg: wire.CmdTx, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdBlock, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdTxBatch, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdFetchTxs, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdInitAbciData, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdFetchSmartContractInfo, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdSyncReq, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdSyncInv, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdSlimBlock, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdSyncBlock, Shard: pcr.sn.shardIndex},
{Msg: wire.CmdSyncSlimBlock, Shard: pcr.sn.shardIndex},
}
for _, shardIndex := range shard.ShardList {
tag = append(tag, connection.Tag{Msg: wire.CmdMsgBlockConfirmation, Shard: shardIndex})
}
connection.GlobalConnServer.RegisterChannels(&connection.MessagesAndReceiver{
Tags: tag,
Channels: pcr.msgQueue,
})
// handle msg
go pcr.msgHandler()
pcr.sn.Start()
}
func (pcr *Processor) msgHandler() {
for msg := range pcr.msgQueue {
pcr.HandleMsg(msg.Msg, msg.Reply)
}
}
// HandleMsg handles messages from peers.
func (pcr *Processor) HandleMsg(msg wire.Message, pr peer.Reply) {
switch msg := msg.(type) {
case *wire.MsgTx:
pcr.sn.OnTransactionReceived(msg)
case *wire.MsgBlock:
pcr.sn.OnBlockReceived(msg)
case *wire.MsgTxBatch:
for _, tx := range msg.Txs {
pcr.sn.OnTransactionReceived(&tx)
}
case *wire.MsgFetchTxs, *wire.MsgFetchInit, *wire.MsgFetchSmartContractInfo:
pcr.handleFetchMsg(msg, pr)
case *wire.MsgSyncReq:
pcr.handleSyncReq(msg, pr)
case *wire.MsgSyncInv:
pcr.handleSyncInv(msg, pr)
case *wire.SlimBlock:
pcr.sn.OnSlimBlockReceived(msg)
case *wire.MsgBlockConfirmation:
pcr.sn.OnConfirmationReceived(msg)
case *wire.MsgSyncBlock:
pcr.syncMgr.AcceptMsg(msg)
case *wire.MsgSyncSlimBlock:
pcr.syncMgr.AcceptMsg(msg)
}
}
// PendingTransactionNumber returns the number of transactions waiting to be packaged in the current trading pool.
func (pcr *Processor) PendingTransactionNumber() int {
return pcr.sn.PendingTransactionNumber()
}
func (pcr *Processor) handleFetchMsg(msg wire.Message, pr peer.Reply) {
pcr.sn.OnFetchMessage(msg, pr)
}
func (pcr *Processor) sendSyncData(msg *wire.MsgSyncInv, pr peer.Reply) {
// 不处理非本分片消息
if pcr.sn.shardIndex != msg.ReqShardIdx {
return
}
toShard := msg.ReqShardIdx
for _, group := range msg.InvGroups {
fromShard := group.Shard
for _, hash := range group.HeaderHashes {
slimBlock := pcr.sn.getSlimBlockInShard(fromShard, hash)
if slimBlock == nil {
pcr.logger.Errorf("Cannot find slim block for shard: %v with header hash: %v", fromShard, hash)
continue
}
pcr.logger.Infof("同步请求发送slimBlock(shard: %d,hgt: %d)",
fromShard, slimBlock.Header.Height)
pr(wire.NewMsgSyncSlimBlock(toShard, slimBlock), nil)
}
lastConfirmation := pcr.sn.getLastConfirmation(fromShard)
if lastConfirmation == nil {
pcr.logger.Errorf("failed to getLastConfirmation, fromShard: %d,toShard: %d", fromShard, toShard)
continue
}
pr(lastConfirmation, nil)
}
}
func (pcr *Processor) getHeaderHashes(locators []*wire.BlockLocator) []*wire.InvGroup {
return pcr.sn.GetHeaderHashes(locators)
}
// ShardHeight returns the current shard height.
func (pcr *Processor) ShardHeight() int64 {
return pcr.sn.ShardHeight()
}
// GetOutState returns the corresponding outstate according to the specified outpoint.
func (pcr *Processor) GetOutState(outPoint *wire.OutPoint) (*wire.OutState, error) {
return pcr.sn.GetOutState(outPoint)
}
// GetBlockInfo returns all blocks after the specified height.
func (pcr *Processor) GetBlockInfo(height int64) []*wire.MsgBlock {
return pcr.sn.GetBlockInfo(height)
}
// GetSlimBlockInfo returns all SlimBlocks after the specified height in the specified shard.
func (pcr *Processor) GetSlimBlockInfo(toShard shard.Index, height int64) []*wire.SlimBlock {
return pcr.sn.GetSlimBlockInfo(toShard, height)
}
// GetBlockSliceInfoSince returns all blocks after the specified height.
func (pcr *Processor) GetBlockSliceInfoSince(startHeight int64) []*wire.MsgBlock {
return pcr.sn.GetBlockSliceInfoSince(startHeight)
}
// GetBlockSliceInfoBetween returns all blocks between the specified heights.
func (pcr *Processor) GetBlockSliceInfoBetween(startHeight, endHeight int64) []*wire.MsgBlock {
return pcr.sn.GetBlockSliceInfoBetween(startHeight, endHeight)
}
// GetSlimBlockSliceInfoSince returns all slimBlocks after the specified height.
func (pcr *Processor) GetSlimBlockSliceInfoSince(toShard shard.Index, startHeight int64) []*wire.SlimBlock {
return pcr.sn.GetSlimBlockSliceInfoSince(toShard, startHeight)
}
// GetSlimBlockSliceInfoBetween returns all slimblocks between the specified heights.
func (pcr *Processor) GetSlimBlockSliceInfoBetween(toShard shard.Index, startHeight, endHeight int64) []*wire.SlimBlock {
return pcr.sn.GetSlimBlockSliceInfoBetween(toShard, startHeight, endHeight)
}
// GetAllShardHeights returns the current height of all shards.
func (pcr *Processor) GetAllShardHeights() []shard.IndexAndHeight {
return pcr.sn.GetAllShardHeights()
}
// SetSyncManager sets the storage node's sync manager.
func (pcr *Processor) SetSyncManager(mgr isync.SyncManager) {
pcr.syncMgr = mgr
pcr.sn.setSyncManager(mgr)
}
func (pcr *Processor) handleSyncReq(msg *wire.MsgSyncReq, pr peer.Reply) {
inv := wire.NewMsgSyncInv(msg.ReqShardIdx, wire.SyncInvTypeFullList)
inv.InvGroups = pcr.getHeaderHashes(msg.Locators)
pr(inv, nil)
}
func (pcr *Processor) handleSyncInv(msg *wire.MsgSyncInv, pr peer.Reply) {
if msg.Type == wire.SyncInvTypeGetData {
pcr.sendSyncData(msg, pr)
} else if msg.Type == wire.SyncInvTypeFullList {
pcr.syncMgr.AcceptMsg(msg)
}
}
// GetSmartContractInfo returns smartContractInfo for specified shard and contract address.
func (pcr *Processor) GetSmartContractInfo(contractAddr multivacaddress.Address, shardIdx shard.Index) *wire.SmartContractInfo {
return pcr.sn.GetSmartContractInfo(contractAddr, shardIdx)
}