/
netsync.go
511 lines (472 loc) · 16.4 KB
/
netsync.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package netsync
import (
"errors"
"sync"
"sync/atomic"
"time"
"github.com/incognitochain/incognito-chain/blockchain/types"
"github.com/incognitochain/incognito-chain/config"
"github.com/incognitochain/incognito-chain/metadata"
"github.com/incognitochain/incognito-chain/syncker"
"github.com/incognitochain/incognito-chain/blockchain"
"github.com/incognitochain/incognito-chain/common"
"github.com/incognitochain/incognito-chain/mempool"
"github.com/incognitochain/incognito-chain/peer"
"github.com/incognitochain/incognito-chain/pubsub"
"github.com/incognitochain/incognito-chain/wire"
libp2p "github.com/libp2p/go-libp2p-peer"
"github.com/patrickmn/go-cache"
)
// NetSync is a gate for message to enter node from network (after Peerconn),
// all message must be process by NetSync before proccessed by other package in node
// NetSync parses message from other peer, identifies type of message
// After parsing, it will detect if message is duplicate or not
// If message is duplicate then discard it, otherwise pass it to the right handler
// NetSync start when node start and run all the time while node is alive
// and it will stop when received quit signal
type NetSync struct {
started int32
shutdown int32
cMessage chan interface{}
cQuit chan struct{}
cTxMessage chan wire.Message
cpuUsage float64
config *NetSyncConfig
cache *NetSyncCache
usingNewPool bool
}
type NetSyncConfig struct {
Syncker *syncker.SynckerManager
BlockChain *blockchain.BlockChain
TxMemPool *mempool.TxPool
PubSubManager *pubsub.PubSubManager
TransactionEvent pubsub.EventChannel // transaction event
// RoleInCommitteesEvent pubsub.EventChannel // role in committees event
BeaconBlockEvent pubsub.EventChannel // beacon block event
ShardBlockEvent pubsub.EventChannel // shard block event
RelayShard []byte
// RoleInCommittees int
// roleInCommitteesMtx sync.RWMutex
Server interface {
// list functions callback which are assigned from Server struct
PushMessageToPeer(wire.Message, libp2p.ID) error
PushMessageToAll(wire.Message) error
PushMessageToShard(msg wire.Message, shard byte) error
}
Consensus interface {
OnBFTMsg(*wire.MessageBFT)
}
}
type NetSyncCache struct {
blockCache *cache.Cache
txCache *cache.Cache
txCacheMtx sync.Mutex
blockCacheMtx sync.Mutex
}
func (netSync *NetSync) Init(cfg *NetSyncConfig) {
netSync.config = cfg
netSync.cQuit = make(chan struct{})
netSync.cMessage = make(chan interface{}, 1000)
netSync.cTxMessage = make(chan wire.Message, 1000)
// init cache
blockCache := cache.New(messageLiveTime, messageCleanupInterval)
txCache := cache.New(messageLiveTime, messageCleanupInterval)
netSync.cache = &NetSyncCache{
txCache: txCache,
blockCache: blockCache,
}
txPoolVersion := config.Param().TxPoolVersion
if txPoolVersion == 0 {
netSync.usingNewPool = false
} else {
netSync.usingNewPool = true
}
// register pubsub channel
_, subChanTx, err := netSync.config.PubSubManager.RegisterNewSubscriber(pubsub.TransactionHashEnterNodeTopic)
if err != nil {
Logger.log.Error(err)
}
netSync.config.TransactionEvent = subChanTx
// _, subChanRole, err := netSync.config.PubSubManager.RegisterNewSubscriber(pubsub.ShardRoleTopic)
// if err != nil {
// Logger.log.Error(err)
// }
// netSync.config.RoleInCommitteesEvent = subChanRole
_, subChanBeaconBlock, err := netSync.config.PubSubManager.RegisterNewSubscriber(pubsub.NewBeaconBlockTopic)
if err != nil {
Logger.log.Error(err)
}
netSync.config.BeaconBlockEvent = subChanBeaconBlock
_, subChanShardBlock, err := netSync.config.PubSubManager.RegisterNewSubscriber(pubsub.NewShardblockTopic)
if err != nil {
Logger.log.Error(err)
}
netSync.config.ShardBlockEvent = subChanShardBlock
}
func (netSync *NetSync) Start() error {
// Already started?
if atomic.AddInt32(&netSync.started, 1) != 1 {
return NewNetSyncError(AlreadyStartError, errors.New("Already started"))
}
Logger.log.Debug("Starting sync manager")
//netSync.waitgroup.Add(1)
go netSync.transactionHandler()
go netSync.messageHandler()
go netSync.GetCPUUsage()
go netSync.cacheLoop()
return nil
}
// Stop gracefully shuts down the sync manager by stopping all asynchronous
// handlers and waiting for them to finish.
func (netSync *NetSync) Stop() {
if atomic.AddInt32(&netSync.shutdown, 1) != 1 {
Logger.log.Warn("Sync manager is already in the process of shutting down")
}
Logger.log.Warn("Sync manager shutting down")
close(netSync.cQuit)
}
// messageHandler is the main handler for the sync manager. It must be run as a
// goroutine. It processes block and inv messages in a separate goroutine
// from the peer handlers so the block (MsgBlock) messages are handled by a
// single thread without needing to lock memory data structures. This is
// important because the sync manager controls which blocks are needed and how
// the fetching should proceed.
func (netSync *NetSync) messageHandler() {
out:
for {
select {
case msgChan := <-netSync.cMessage:
{
go func(msgC interface{}) {
// go metrics.AnalyzeTimeSeriesMetricData(map[string]interface{}{
// metrics.Measurement: metrics.HandleAllMessage,
// metrics.MeasurementValue: float64(1),
// metrics.Tag: metrics.ShardIDTag,
// metrics.TagValue: fmt.Sprintf("shardid-%+v", netSync.config.RoleInCommittees)})
// go metrics.AnalyzeTimeSeriesMetricData(map[string]interface{}{
// metrics.Measurement: metrics.HandleAllMessageSize,
// metrics.MeasurementValue: float64(reflect.TypeOf(msgC).Size()),
// metrics.Tag: metrics.ShardIDTag,
// metrics.TagValue: fmt.Sprintf("shardid-%+v", netSync.config.RoleInCommittees)})
switch msg := msgC.(type) {
case *wire.MessageTx, *wire.MessageTxPrivacyToken:
{
beaconHeight := netSync.config.BlockChain.GetBeaconBestState().BestBlock.GetHeight()
var tx metadata.Transaction
switch msg := msgC.(type) {
case *wire.MessageTx:
tx = msg.Transaction
netSync.handleMessageTx(msg, tx, int64(beaconHeight))
case *wire.MessageTxPrivacyToken:
tx = msg.Transaction
netSync.handleMessageTx(msg, tx, int64(beaconHeight))
}
}
case *wire.MessageBFT:
{
netSync.handleMessageBFTMsg(msg)
}
default:
Logger.log.Debugf("Invalid message type in block "+"handler: %T", msg)
}
}(msgChan)
}
case msgChan := <-netSync.cQuit:
{
Logger.log.Warn(msgChan)
break out
}
}
}
//netSync.waitgroup.Done()
close(netSync.cMessage)
Logger.log.Debug("Block handler done")
}
func (netSync *NetSync) GetCPUUsage() {
ticker := time.NewTicker(2 * time.Second)
idle0, total0 := common.GetCPUSample()
defer ticker.Stop()
for _ = range ticker.C {
idle1, total1 := common.GetCPUSample()
idle, total := idle1-idle0, total1-total0
netSync.cpuUsage = 100 * float64(total-idle) / float64(total)
Logger.log.Debugf("TestCPU: CPU Usage %v %v %v", idle, total, netSync.cpuUsage)
idle0, total0 = common.GetCPUSample()
}
}
func (netSync *NetSync) transactionHandler() {
ticker := time.NewTicker(1 * time.Second)
txsrps := 100 // total transaction release per second
defer ticker.Stop()
queueTx := []wire.Message{}
for {
select {
case msgTx := <-netSync.cTxMessage:
queueTx = append(queueTx, msgTx)
case <-ticker.C:
i := 0
if netSync.cpuUsage < 80 {
for ; (i < txsrps) && (i < len(queueTx)); i++ {
Logger.log.Infof("netSync.cpuUsage %v is low now, send tx to mempool", netSync.cpuUsage)
netSync.cMessage <- queueTx[i]
}
queueTx = queueTx[i:]
}
}
}
}
func (netSync *NetSync) QueueTx(peer *peer.Peer, msg *wire.MessageTx, done chan struct{}) error {
// Don't accept more transactions if we're shutting down.
if atomic.LoadInt32(&netSync.shutdown) != 0 {
done <- struct{}{}
return NewNetSyncError(AlreadyShutdownError, errors.New("We're shutting down"))
}
if netSync.cpuUsage > 80 {
Logger.log.Infof("Received tx %v, cpuUsage too high %v, send to another pool", msg.Transaction.Hash().String(), netSync.cpuUsage)
go func(msg *wire.MessageTx) {
netSync.cTxMessage <- msg
}(msg)
} else {
Logger.log.Infof("Received tx %v, send to mempool", msg.Transaction.Hash().String(), netSync.cpuUsage)
netSync.cMessage <- msg
}
return nil
}
func (netSync *NetSync) QueueTxPrivacyToken(peer *peer.Peer, msg *wire.MessageTxPrivacyToken, done chan struct{}) error {
// Don't accept more transactions if we're shutting down.
if atomic.LoadInt32(&netSync.shutdown) != 0 {
done <- struct{}{}
return NewNetSyncError(AlreadyShutdownError, errors.New("We're shutting down"))
}
if netSync.cpuUsage > 80 {
go func(msg *wire.MessageTx) {
netSync.cTxMessage <- msg
}((*wire.MessageTx)(msg))
} else {
netSync.cMessage <- msg
}
return nil
}
// QueueBlock adds the passed block message and peer to the block handling
// queue. Responds to the done channel argument after the block message is
// processed.
func (netSync *NetSync) QueueBlock(_ *peer.Peer, msg wire.Message, done chan struct{}) {
// Don't accept more transactions if we're shutting down.
if atomic.LoadInt32(&netSync.shutdown) != 0 {
done <- struct{}{}
return
}
netSync.cMessage <- msg
}
func (netSync *NetSync) QueueGetBlockShard(peer *peer.Peer, msg *wire.MessageGetBlockShard, done chan struct{}) {
// Don't accept more transactions if we're shutting down.
if atomic.LoadInt32(&netSync.shutdown) != 0 {
done <- struct{}{}
return
}
netSync.cMessage <- msg
}
func (netSync *NetSync) QueueGetBlockBeacon(peer *peer.Peer, msg *wire.MessageGetBlockBeacon, done chan struct{}) {
// Don't accept more transactions if we're shutting down.
if atomic.LoadInt32(&netSync.shutdown) != 0 {
done <- struct{}{}
return
}
netSync.cMessage <- msg
}
func (netSync *NetSync) QueueMessage(peer *peer.Peer, msg wire.Message, done chan struct{}) {
// Don't accept more transactions if we're shutting down.
if atomic.LoadInt32(&netSync.shutdown) != 0 {
done <- struct{}{}
return
}
netSync.cMessage <- msg
}
// handleTxMsg handles transaction messages from all peers.
func (netSync *NetSync) handleMessageTx(msg wire.Message, tx metadata.Transaction, beaconHeight int64) {
txHash := tx.Hash().String()
Logger.log.Debugf(" Handling new message tx %v", txHash)
if isAdded := netSync.handleCacheTx(*tx.Hash()); !isAdded {
sID := common.GetShardIDFromLastByte(tx.GetSenderAddrLastByte())
sBState := netSync.config.BlockChain.GetBestStateShard(sID)
if sBState == nil {
Logger.log.Debugf("Received tx from sID %v but cannot get shard Beststate", sID)
return
}
if (sBState.BestBlock == nil) || (sBState.BestBlock.GetProposeTime() < (time.Now().Unix() - int64(time.Hour.Seconds()))) {
return
}
if !netSync.usingNewPool {
txDB := sBState.GetCopiedTransactionStateDB()
err := tx.LoadData(txDB)
if err == nil {
hash, _, err := netSync.config.TxMemPool.MaybeAcceptTransaction(tx, beaconHeight)
if err != nil {
Logger.log.Errorf("Validate tx %v return err %v", tx.Hash().String(), err)
} else {
Logger.log.Debugf("Node got hash of transaction %s", hash.String())
}
} else {
Logger.log.Errorf("Validate tx %v return err %v", tx.Hash().String(), err)
}
} else {
tp, err := netSync.config.BlockChain.GetConfig().PoolManager.GetShardTxsPool(sID)
if err != nil {
Logger.log.Errorf("Cannot get tx pool of shard %v, got err %v", sID, err)
} else {
if !tp.IsRunning() {
return
}
tp.GetInbox() <- tx
Logger.log.Infof("Sent transaction %+v to pool", txHash)
}
}
}
Logger.log.Infof("Transaction %+v found in cache", txHash)
}
// handleTxMsg handles transaction messages from all peers.
func (netSync *NetSync) handleMessageTxPrivacyToken(msg *wire.MessageTxPrivacyToken, beaconHeight int64) {
txHash := msg.Transaction.Hash().String()
Logger.log.Debugf("Handling new message tx %v", txHash)
// if !netSync.handleTxWithRole(msg.Transaction) {
// return
// }
if isAdded := netSync.handleCacheTx(*msg.Transaction.Hash()); !isAdded {
tx := msg.Transaction
sID := common.GetShardIDFromLastByte(tx.GetSenderAddrLastByte())
tp, err := netSync.config.BlockChain.GetConfig().PoolManager.GetShardTxsPool(sID)
if err != nil {
Logger.log.Errorf("Cannot get tx pool of shard %v, got err %v", sID, err)
} else {
if !tp.IsRunning() {
return
}
tp.GetInbox() <- tx
// Broadcast to network
/*go metrics.AnalyzeTimeSeriesMetricData(map[string]interface{}{
metrics.Measurement: metrics.TxEnterNetSyncSuccess,
metrics.MeasurementValue: float64(1),
metrics.Tag: metrics.TxHashTag,
metrics.TagValue: msg.Transaction.Hash().String(),
})*/
// Logger.log.Debugf("there is hash of transaction %s", tx.Hash().String())
// err := netSync.config.Server.PushMessageToAll(msg)
// if err != nil {
// Logger.log.Error(err)
// } else {
// netSync.config.TxMemPool.MarkForwardedTransaction(*msg.Transaction.Hash())
// }
}
}
Logger.log.Infof("Transaction %+v found in cache", txHash)
}
func (netSync *NetSync) handleMessageBFTMsg(msg *wire.MessageBFT) {
// go metrics.AnalyzeTimeSeriesMetricData(map[string]interface{}{
// metrics.Measurement: metrics.HandleMessageBFTMsg,
// metrics.MeasurementValue: float64(1),
// metrics.Tag: metrics.ShardIDTag,
// metrics.TagValue: fmt.Sprintf("shardid-%+v", netSync.config.RoleInCommittees),
// })
Logger.log.Info("Handling new message BFTMsg")
// startTime := time.Now()
if err := msg.VerifyMsgSanity(); err != nil {
Logger.log.Error(err)
return
}
netSync.config.Consensus.OnBFTMsg(msg)
// go metrics.AnalyzeTimeSeriesMetricData(map[string]interface{}{
// metrics.Measurement: metrics.HandleMessageBFTMsgTime,
// metrics.MeasurementValue: float64(time.Since(startTime).Seconds()),
// metrics.Tag: metrics.ShardIDTag,
// metrics.TagValue: fmt.Sprintf("shardid-%+v", netSync.config.RoleInCommittees),
// })
}
func (netSync *NetSync) handleCacheBlock(blockHash string) bool {
netSync.cache.blockCacheMtx.Lock()
defer netSync.cache.blockCacheMtx.Unlock()
_, ok := netSync.cache.blockCache.Get(blockHash)
if ok {
return true
}
err := netSync.cache.blockCache.Add(blockHash, 1, messageLiveTime)
if err != nil {
Logger.log.Error(err)
}
return false
}
// handleCacheTx - check txHash and cache
func (netSync *NetSync) handleCacheTx(txHash common.Hash) bool {
netSync.cache.txCacheMtx.Lock()
defer netSync.cache.txCacheMtx.Unlock()
_, ok := netSync.cache.txCache.Get(txHash.String())
if ok {
return true
}
err := netSync.cache.txCache.Add(txHash.String(), 1, messageLiveTime)
if err != nil {
Logger.log.Error(err)
}
return false
}
// handleTxWithRole - check tx and make decision is processed or not
// func (netSync *NetSync) handleTxWithRole(tx metadata.Transaction) bool {
// senderShardID := common.GetShardIDFromLastByte(tx.GetSenderAddrLastByte())
// for _, shardID := range netSync.config.RelayShard {
// if senderShardID == shardID {
// return true
// }
// }
// netSync.config.roleInCommitteesMtx.RLock()
// if netSync.config.RoleInCommittees > -1 && byte(netSync.config.RoleInCommittees) == senderShardID {
// netSync.config.roleInCommitteesMtx.RUnlock()
// return true
// } else {
// netSync.config.roleInCommitteesMtx.RUnlock()
// return false
// }
// }
func (netSync *NetSync) cacheLoop() {
for w := 0; w < workers; w++ {
go netSync.handleCacheTxHashWorker(netSync.config.TransactionEvent)
}
for {
select {
case msg := <-netSync.config.ShardBlockEvent:
{
if shardBlock, ok := msg.Value.(*types.ShardBlock); !ok {
continue
} else {
go netSync.handleCacheBlock("s" + shardBlock.Header.Hash().String())
}
}
case msg := <-netSync.config.BeaconBlockEvent:
{
if beaconBlock, ok := msg.Value.(*types.BeaconBlock); !ok {
continue
} else {
go netSync.handleCacheBlock("b" + beaconBlock.Header.Hash().String())
}
}
// case msg := <-netSync.config.RoleInCommitteesEvent:
// {
// if shardID, ok := msg.Value.(int); !ok {
// continue
// } else {
// netSync.config.roleInCommitteesMtx.Lock()
// netSync.config.RoleInCommittees = shardID
// netSync.config.roleInCommitteesMtx.Unlock()
// }
// }
}
}
}
func (netSync *NetSync) handleCacheTxHashWorker(event pubsub.EventChannel) {
for msg := range event {
value, ok := msg.Value.(common.Hash)
if !ok {
continue
}
go netSync.handleCacheTx(value)
time.Sleep(time.Nanosecond)
}
}