forked from ledgerwatch/erigon
/
send.go
269 lines (246 loc) · 7.87 KB
/
send.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
/*
Copyright 2021 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package txpool
import (
"context"
"fmt"
"sync"
"github.com/nebojsa94/erigon/erigon-lib/direct"
"github.com/nebojsa94/erigon/erigon-lib/gointerfaces/sentry"
"github.com/nebojsa94/erigon/erigon-lib/rlp"
types2 "github.com/nebojsa94/erigon/erigon-lib/types"
"github.com/ledgerwatch/log/v3"
"google.golang.org/grpc"
)
type SentryClient interface {
sentry.SentryClient
Protocol() uint
}
// Send - does send concrete P2P messages to Sentry. Same as Fetch but for outbound traffic
// does not initiate any messages by self
type Send struct {
ctx context.Context
pool Pool
wg *sync.WaitGroup
sentryClients []direct.SentryClient // sentry clients that will be used for accessing the network
logger log.Logger
}
func NewSend(ctx context.Context, sentryClients []direct.SentryClient, pool Pool, logger log.Logger) *Send {
return &Send{
ctx: ctx,
pool: pool,
sentryClients: sentryClients,
logger: logger,
}
}
func (f *Send) SetWaitGroup(wg *sync.WaitGroup) {
f.wg = wg
}
const (
// This is the target size for the packs of transactions or announcements. A
// pack can get larger than this if a single transactions exceeds this size.
p2pTxPacketLimit = 100 * 1024
)
func (f *Send) notifyTests() {
if f.wg != nil {
f.wg.Done()
}
}
// Broadcast given RLPs to random peers
func (f *Send) BroadcastPooledTxs(rlps [][]byte, maxPeers uint64) (txSentTo []int) {
defer f.notifyTests()
if len(rlps) == 0 {
return
}
txSentTo = make([]int, len(rlps))
var prev, size int
for i, l := 0, len(rlps); i < len(rlps); i++ {
size += len(rlps[i])
// Wait till the combined size of rlps so far is greater than a threshold and
// send them all at once. Then wait till end of array or this threshold hits again
if i == l-1 || size >= p2pTxPacketLimit {
txsData := types2.EncodeTransactions(rlps[prev:i+1], nil)
var txs66 *sentry.SendMessageToRandomPeersRequest
for _, sentryClient := range f.sentryClients {
if !sentryClient.Ready() {
continue
}
if txs66 == nil {
txs66 = &sentry.SendMessageToRandomPeersRequest{
Data: &sentry.OutboundMessageData{
Id: sentry.MessageId_TRANSACTIONS_66,
Data: txsData,
},
MaxPeers: maxPeers,
}
}
peers, err := sentryClient.SendMessageToRandomPeers(f.ctx, txs66)
if err != nil {
f.logger.Debug("[txpool.send] BroadcastPooledTxs", "err", err)
}
if peers != nil {
for j := prev; j <= i; j++ {
txSentTo[j] = len(peers.Peers)
}
}
}
prev = i + 1
size = 0
}
}
return
}
func (f *Send) AnnouncePooledTxs(types []byte, sizes []uint32, hashes types2.Hashes, maxPeers uint64) (hashSentTo []int) {
defer f.notifyTests()
hashSentTo = make([]int, len(types))
if len(types) == 0 {
return
}
prevI := 0
prevJ := 0
for prevI < len(hashes) || prevJ < len(types) {
// Prepare two versions of the announcement message, one for pre-eth/68 peers, another for post-eth/68 peers
i := prevI
for i < len(hashes) && rlp.HashesLen(hashes[prevI:i+32]) < p2pTxPacketLimit {
i += 32
}
j := prevJ
for j < len(types) && rlp.AnnouncementsLen(types[prevJ:j+1], sizes[prevJ:j+1], hashes[32*prevJ:32*j+32]) < p2pTxPacketLimit {
j++
}
iSize := rlp.HashesLen(hashes[prevI:i])
jSize := rlp.AnnouncementsLen(types[prevJ:j], sizes[prevJ:j], hashes[32*prevJ:32*j])
iData := make([]byte, iSize)
jData := make([]byte, jSize)
if s := rlp.EncodeHashes(hashes[prevI:i], iData); s != iSize {
panic(fmt.Sprintf("Serialised hashes encoding len mismatch, expected %d, got %d", iSize, s))
}
if s := rlp.EncodeAnnouncements(types[prevJ:j], sizes[prevJ:j], hashes[32*prevJ:32*j], jData); s != jSize {
panic(fmt.Sprintf("Serialised announcements encoding len mismatch, expected %d, got %d", jSize, s))
}
for _, sentryClient := range f.sentryClients {
if !sentryClient.Ready() {
continue
}
switch sentryClient.Protocol() {
case direct.ETH66, direct.ETH67:
if i > prevI {
req := &sentry.SendMessageToRandomPeersRequest{
Data: &sentry.OutboundMessageData{
Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66,
Data: iData,
},
MaxPeers: maxPeers,
}
peers, err := sentryClient.SendMessageToRandomPeers(f.ctx, req)
if err != nil {
f.logger.Debug("[txpool.send] AnnouncePooledTxs", "err", err)
}
if peers != nil {
for k := prevI; k < i; k += 32 {
hashSentTo[k/32] += len(peers.Peers)
}
}
}
case direct.ETH68:
if j > prevJ {
req := &sentry.SendMessageToRandomPeersRequest{
Data: &sentry.OutboundMessageData{
Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_68,
Data: jData,
},
MaxPeers: maxPeers,
}
peers, err := sentryClient.SendMessageToRandomPeers(f.ctx, req)
if err != nil {
f.logger.Debug("[txpool.send] AnnouncePooledTxs68", "err", err)
}
if peers != nil {
for k := prevJ; k < j; k++ {
hashSentTo[k] += len(peers.Peers)
}
}
}
}
}
prevI = i
prevJ = j
}
return
}
func (f *Send) PropagatePooledTxsToPeersList(peers []types2.PeerID, types []byte, sizes []uint32, hashes []byte) {
defer f.notifyTests()
if len(types) == 0 {
return
}
prevI := 0
prevJ := 0
for prevI < len(hashes) || prevJ < len(types) {
// Prepare two versions of the annoucement message, one for pre-eth/68 peers, another for post-eth/68 peers
i := prevI
for i < len(hashes) && rlp.HashesLen(hashes[prevI:i+32]) < p2pTxPacketLimit {
i += 32
}
j := prevJ
for j < len(types) && rlp.AnnouncementsLen(types[prevJ:j+1], sizes[prevJ:j+1], hashes[32*prevJ:32*j+32]) < p2pTxPacketLimit {
j++
}
iSize := rlp.HashesLen(hashes[prevI:i])
jSize := rlp.AnnouncementsLen(types[prevJ:j], sizes[prevJ:j], hashes[32*prevJ:32*j])
iData := make([]byte, iSize)
jData := make([]byte, jSize)
if s := rlp.EncodeHashes(hashes[prevI:i], iData); s != iSize {
panic(fmt.Sprintf("Serialised hashes encoding len mismatch, expected %d, got %d", iSize, s))
}
if s := rlp.EncodeAnnouncements(types[prevJ:j], sizes[prevJ:j], hashes[32*prevJ:32*j], jData); s != jSize {
panic(fmt.Sprintf("Serialised annoucements encoding len mismatch, expected %d, got %d", jSize, s))
}
for _, sentryClient := range f.sentryClients {
if !sentryClient.Ready() {
continue
}
for _, peer := range peers {
switch sentryClient.Protocol() {
case direct.ETH66, direct.ETH67:
if i > prevI {
req := &sentry.SendMessageByIdRequest{
PeerId: peer,
Data: &sentry.OutboundMessageData{
Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_66,
Data: iData,
},
}
if _, err := sentryClient.SendMessageById(f.ctx, req, &grpc.EmptyCallOption{}); err != nil {
f.logger.Debug("[txpool.send] PropagatePooledTxsToPeersList", "err", err)
}
}
case direct.ETH68:
if j > prevJ {
req := &sentry.SendMessageByIdRequest{
PeerId: peer,
Data: &sentry.OutboundMessageData{
Id: sentry.MessageId_NEW_POOLED_TRANSACTION_HASHES_68,
Data: jData,
},
}
if _, err := sentryClient.SendMessageById(f.ctx, req, &grpc.EmptyCallOption{}); err != nil {
f.logger.Debug("[txpool.send] PropagatePooledTxsToPeersList68", "err", err)
}
}
}
}
}
prevI = i
prevJ = j
}
}