This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
/
tx_keeper.go
145 lines (125 loc) · 3.25 KB
/
tx_keeper.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 netsync
import (
"math/rand"
"time"
log "github.com/sirupsen/logrus"
core "github.com/bytom/bytom/protocol"
"github.com/bytom/bytom/protocol/bc/types"
)
const (
// This is the target size for the packs of transactions sent by txSyncLoop.
// A pack can get larger than this if a single transactions exceeds this size.
txSyncPackSize = 100 * 1024
)
type txSyncMsg struct {
peerID string
txs []*types.Tx
}
func (sm *SyncManager) syncTransactions(peerID string) {
pending := sm.txPool.GetTransactions()
if len(pending) == 0 {
return
}
txs := make([]*types.Tx, len(pending))
for i, batch := range pending {
txs[i] = batch.Tx
}
sm.txSyncCh <- &txSyncMsg{peerID, txs}
}
func (sm *SyncManager) txBroadcastLoop() {
for {
select {
case obj, ok := <-sm.txMsgSub.Chan():
if !ok {
log.WithFields(log.Fields{"module": logModule}).Warning("mempool tx msg subscription channel closed")
return
}
ev, ok := obj.Data.(core.TxMsgEvent)
if !ok {
log.WithFields(log.Fields{"module": logModule}).Error("event type error")
continue
}
if ev.TxMsg.MsgType == core.MsgNewTx {
if err := sm.peers.broadcastTx(ev.TxMsg.Tx); err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Error("fail on broadcast new tx.")
continue
}
}
case <-sm.quitSync:
return
}
}
}
// txSyncLoop takes care of the initial transaction sync for each new
// connection. When a new peer appears, we relay all currently pending
// transactions. In order to minimise egress bandwidth usage, we send
// the transactions in small packs to one peer at a time.
func (sm *SyncManager) txSyncLoop() {
pending := make(map[string]*txSyncMsg)
sending := false // whether a send is active
done := make(chan error, 1) // result of the send
// send starts a sending a pack of transactions from the sync.
send := func(msg *txSyncMsg) {
peer := sm.peers.getPeer(msg.peerID)
if peer == nil {
delete(pending, msg.peerID)
return
}
totalSize := uint64(0)
sendTxs := []*types.Tx{}
for i := 0; i < len(msg.txs) && totalSize < txSyncPackSize; i++ {
sendTxs = append(sendTxs, msg.txs[i])
totalSize += msg.txs[i].SerializedSize
}
if len(msg.txs) == len(sendTxs) {
delete(pending, msg.peerID)
} else {
msg.txs = msg.txs[len(sendTxs):]
}
// Send the pack in the background.
log.WithFields(log.Fields{
"module": logModule,
"count": len(sendTxs),
"bytes": totalSize,
"peer": msg.peerID,
}).Debug("txSyncLoop sending transactions")
sending = true
go func() {
ok, err := peer.sendTransactions(sendTxs)
if !ok {
sm.peers.removePeer(msg.peerID)
}
done <- err
}()
}
// pick chooses the next pending sync.
pick := func() *txSyncMsg {
if len(pending) == 0 {
return nil
}
n := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(pending)) + 1
for _, s := range pending {
if n--; n == 0 {
return s
}
}
return nil
}
for {
select {
case msg := <-sm.txSyncCh:
pending[msg.peerID] = msg
if !sending {
send(msg)
}
case err := <-done:
sending = false
if err != nil {
log.WithFields(log.Fields{"module": logModule, "err": err}).Warning("fail on txSyncLoop sending")
}
if s := pick(); s != nil {
send(s)
}
}
}
}