forked from aergoio/aergo
/
actorwork.go
233 lines (208 loc) · 8.1 KB
/
actorwork.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package p2p
import (
"fmt"
"time"
"github.com/aergoio/aergo-actor/actor"
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/message"
"github.com/aergoio/aergo/p2p/p2putil"
"github.com/aergoio/aergo/p2p/subproto"
"github.com/aergoio/aergo/types"
peer "github.com/libp2p/go-libp2p-peer"
)
const (
// fetchTimeOut was copied from syncer package. it can be problem if these value become different
fetchTimeOut = time.Second * 100
)
// GetAddresses send getAddress request to other peer
func (p2ps *P2P) GetAddresses(peerID peer.ID, size uint32) bool {
remotePeer, ok := p2ps.pm.GetPeer(peerID)
if !ok {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Msg("Message addressRequest to Unknown peer, check if a bug")
return false
}
senderAddr := p2ps.pm.SelfMeta().ToPeerAddress()
// create message data
req := &types.AddressesRequest{Sender: &senderAddr, MaxSize: 50}
remotePeer.SendMessage(p2ps.mf.NewMsgRequestOrder(true, subproto.AddressesRequest, req))
return true
}
// GetBlockHeaders send request message to peer and
func (p2ps *P2P) GetBlockHeaders(msg *message.GetBlockHeaders) bool {
remotePeer, exists := p2ps.pm.GetPeer(msg.ToWhom)
if !exists {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(msg.ToWhom)).Msg("Request to invalid peer")
return false
}
p2ps.Debug().Str(p2putil.LogPeerName, remotePeer.Name()).Interface("msg", msg).Msg("Sending Get block Header request")
// create message data
reqMsg := &types.GetBlockHeadersRequest{Hash: msg.Hash,
Height: msg.Height, Offset: msg.Offset, Size: msg.MaxSize, Asc: msg.Asc,
}
remotePeer.SendMessage(p2ps.mf.NewMsgRequestOrder(true, subproto.GetBlockHeadersRequest, reqMsg))
return true
}
// GetBlocks send request message to peer and
func (p2ps *P2P) GetBlocks(peerID peer.ID, blockHashes []message.BlockHash) bool {
remotePeer, exists := p2ps.pm.GetPeer(peerID)
if !exists {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Str(p2putil.LogProtoID, string(subproto.GetBlocksRequest)).Msg("Message to Unknown peer, check if a bug")
return false
}
if len(blockHashes) == 0 {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Str(p2putil.LogProtoID, string(subproto.GetBlocksRequest)).Msg("meaningless GetBlocks request with zero hash")
return false
}
p2ps.Debug().Str(p2putil.LogPeerName, remotePeer.Name()).Int(p2putil.LogBlkCount, len(blockHashes)).Str("first_hash", enc.ToString(blockHashes[0])).Msg("Sending Get block request")
hashes := make([][]byte, len(blockHashes))
for i, hash := range blockHashes {
hashes[i] = ([]byte)(hash)
}
// create message data
req := &types.GetBlockRequest{Hashes: hashes}
remotePeer.SendMessage(p2ps.mf.NewMsgRequestOrder(true, subproto.GetBlocksRequest, req))
return true
}
// GetBlocksChunk send request message to peer and
func (p2ps *P2P) GetBlocksChunk(context actor.Context, msg *message.GetBlockChunks) {
peerID := msg.ToWhom
blockHashes := msg.Hashes
remotePeer, exists := p2ps.pm.GetPeer(peerID)
if !exists {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Str(p2putil.LogProtoID, subproto.GetBlocksRequest.String()).Msg("Message to Unknown peer, check if a bug")
context.Respond(&message.GetBlockChunksRsp{Seq:msg.Seq, ToWhom: peerID, Err: fmt.Errorf("invalid peer")})
return
}
receiver := NewBlockReceiver(p2ps, remotePeer, msg.Seq, blockHashes, msg.TTL)
receiver.StartGet()
}
// GetBlockHashes send request message to peer and make response message for block hashes
func (p2ps *P2P) GetBlockHashes(context actor.Context, msg *message.GetHashes) {
peerID := msg.ToWhom
// TODO
remotePeer, exists := p2ps.pm.GetPeer(peerID)
if !exists {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Str(p2putil.LogProtoID, subproto.GetHashesRequest.String()).Msg("Invalid peerID")
context.Respond(&message.GetHashesRsp{Seq:msg.Seq, Hashes: nil, PrevInfo: msg.PrevInfo, Count: 0, Err: message.PeerNotFoundError})
return
}
receiver := NewBlockHashesReceiver(p2ps, remotePeer, msg.Seq, msg, fetchTimeOut)
receiver.StartGet()
}
// GetBlockHashes send request message to peer and make response message for block hashes
func (p2ps *P2P) GetBlockHashByNo(context actor.Context, msg *message.GetHashByNo) {
peerID := msg.ToWhom
// TODO
remotePeer, exists := p2ps.pm.GetPeer(peerID)
if !exists {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Str(p2putil.LogProtoID, subproto.GetHashByNoRequest.String()).Msg("Invalid peerID")
context.Respond(&message.GetHashByNoRsp{Seq:msg.Seq, Err: message.PeerNotFoundError})
return
}
receiver := NewBlockHashByNoReceiver(p2ps, remotePeer, msg.Seq, msg.BlockNo, fetchTimeOut)
receiver.StartGet()
}
// NotifyNewBlock send notice message of new block to a peer
func (p2ps *P2P) NotifyNewBlock(newBlock message.NotifyNewBlock) bool {
req := &types.NewBlockNotice{
BlockHash: newBlock.Block.BlockHash(),
BlockNo: newBlock.BlockNo}
msg := p2ps.mf.NewMsgBlkBroadcastOrder(req)
skipped, sent := 0, 0
// create message data
for _, neighbor := range p2ps.pm.GetPeers() {
if neighbor != nil && neighbor.State() == types.RUNNING {
sent++
neighbor.SendMessage(msg)
} else {
skipped++
}
}
p2ps.Debug().Int("skipped_cnt", skipped).Int("sent_cnt", sent).Str("hash", enc.ToString(newBlock.Block.BlockHash())).Msg("Notifying new block")
return true
}
// NotifyNewBlock send notice message of new block to a peer
func (p2ps *P2P) NotifyBlockProduced(newBlock message.NotifyNewBlock) bool {
// TODO fill producerID
req := &types.BlockProducedNotice{ProducerID: nil, BlockNo: newBlock.BlockNo, Block: newBlock.Block}
msg := p2ps.mf.NewMsgBPBroadcastOrder(req)
skipped, sent := 0, 0
// TODO filter to only contain bp and trusted node.
for _, neighbor := range p2ps.pm.GetPeers() {
if neighbor != nil && neighbor.State() == types.RUNNING {
sent++
neighbor.SendMessage(msg)
} else {
skipped++
}
}
p2ps.Debug().Int("sent_cnt", sent).Str("hash", enc.ToString(newBlock.Block.BlockHash())).Uint64("block_no", req.BlockNo).Msg("Notifying block produced")
return true
}
// GetTXs send request message to peer and
func (p2ps *P2P) GetTXs(peerID peer.ID, txHashes []message.TXHash) bool {
remotePeer, ok := p2ps.pm.GetPeer(peerID)
if !ok {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Msg("Invalid peer. check for bug")
return false
}
if len(txHashes) == 0 {
p2ps.Warn().Msg("empty hash list")
return false
}
hashes := make([][]byte, len(txHashes))
for i, hash := range txHashes {
if len(hash) == 0 {
p2ps.Warn().Msg("empty hash value requested.")
return false
}
hashes[i] = ([]byte)(hash)
}
// create message data
req := &types.GetTransactionsRequest{Hashes: hashes}
remotePeer.SendMessage(p2ps.mf.NewMsgRequestOrder(true, subproto.GetTXsRequest, req))
return true
}
// NotifyNewTX notice tx(s) id created
func (p2ps *P2P) NotifyNewTX(newTXs message.NotifyNewTransactions) bool {
hashes := make([]types.TxID, len(newTXs.Txs))
for i, tx := range newTXs.Txs {
hashes[i] = types.ToTxID(tx.Hash)
}
// create message data
skipped, sent := 0, 0
// send to peers
for _, rPeer := range p2ps.pm.GetPeers() {
if rPeer != nil && rPeer.State() == types.RUNNING {
sent++
rPeer.PushTxsNotice(hashes)
} else {
skipped++
}
}
//p2ps.Debug().Int("skippeer_cnt", skipped).Int("sendpeer_cnt", sent).Int("hash_cnt", len(hashes)).Msg("Notifying newTXs to peers")
return true
}
// Syncer.finder request remote peer to find ancestor
func (p2ps *P2P) GetSyncAncestor(context actor.Context, msg *message.GetSyncAncestor) {
peerID := msg.ToWhom
remotePeer, exists := p2ps.pm.GetPeer(peerID)
if !exists {
p2ps.Warn().Str(p2putil.LogPeerID, p2putil.ShortForm(peerID)).Msg("invalid peer id")
context.Respond(&message.GetSyncAncestorRsp{Seq: msg.Seq, Ancestor:nil})
return
}
if len(msg.Hashes) == 0 {
p2ps.Warn().Str(p2putil.LogPeerName, remotePeer.Name()).Msg("empty hash list received")
context.Respond(&message.GetSyncAncestorRsp{Seq: msg.Seq, Ancestor:nil})
return
}
// create message data
receiver := NewAncestorReceiver(p2ps, remotePeer, msg.Seq, msg.Hashes, fetchTimeOut)
receiver.StartGet()
return
}