-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathp2p.go
More file actions
173 lines (144 loc) · 4.48 KB
/
Copy pathp2p.go
File metadata and controls
173 lines (144 loc) · 4.48 KB
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
package celestia_core_v34
import (
"fmt"
bc "github.com/KYVENetwork/celestia-core/blockchain"
bcv0 "github.com/KYVENetwork/celestia-core/blockchain/v0"
tmLog "github.com/KYVENetwork/celestia-core/libs/log"
"github.com/KYVENetwork/celestia-core/p2p"
bcproto "github.com/KYVENetwork/celestia-core/proto/celestiacore/blockchain"
"github.com/KYVENetwork/celestia-core/version"
"reflect"
)
const (
BlockchainChannel = byte(0x40)
)
type BlockchainReactor struct {
p2p.BaseReactor
block *Block
nextBlock *Block
}
func NewBlockchainReactor(block *Block, nextBlock *Block) *BlockchainReactor {
bcR := &BlockchainReactor{
block: block,
nextBlock: nextBlock,
}
bcR.BaseReactor = *p2p.NewBaseReactor("BlockchainReactor", bcR)
return bcR
}
func (bcR *BlockchainReactor) GetChannels() []*p2p.ChannelDescriptor {
return []*p2p.ChannelDescriptor{
{
ID: BlockchainChannel,
Priority: 5,
SendQueueCapacity: 1000,
RecvBufferCapacity: 50 * 4096,
RecvMessageCapacity: bc.MaxMsgSize,
MessageType: &bcproto.Message{},
},
}
}
func (bcR *BlockchainReactor) sendStatusToPeer(src p2p.Peer) (queued bool) {
msgBytes, err := bc.EncodeMsg(&bcproto.StatusResponse{
Base: bcR.block.Height,
Height: bcR.block.Height + 1})
if err != nil {
bcR.Logger.Error("could not convert msg to protobuf", err.Error())
return
}
bcR.Logger.Info("Sent status to peer", "base", bcR.block.Height, "height", bcR.block.Height+1)
return src.Send(BlockchainChannel, msgBytes)
}
func (bcR *BlockchainReactor) sendBlockToPeer(msg *bcproto.BlockRequest, src p2p.Peer) (queued bool) {
if msg.Height == bcR.block.Height {
bl, err := bcR.block.ToProto()
if err != nil {
bcR.Logger.Error("could not convert msg to protobuf", err.Error())
return false
}
msgBytes, err := bc.EncodeMsg(&bcproto.BlockResponse{Block: bl})
if err != nil {
bcR.Logger.Error("could not marshal msg", err.Error())
return false
}
bcR.Logger.Info(fmt.Sprintf("sent block with height %d to peer", bcR.block.Height))
return src.TrySend(BlockchainChannel, msgBytes)
}
if msg.Height == bcR.nextBlock.Height {
bl, err := bcR.nextBlock.ToProto()
if err != nil {
bcR.Logger.Error("could not convert msg to protobuf", err.Error())
return false
}
msgBytes, err := bc.EncodeMsg(&bcproto.BlockResponse{Block: bl})
if err != nil {
bcR.Logger.Error("could not marshal msg", err.Error())
return false
}
bcR.Logger.Info(fmt.Sprintf("sent block with height %d to peer", bcR.nextBlock.Height))
return src.TrySend(BlockchainChannel, msgBytes)
}
bcR.Logger.Error(fmt.Sprintf("peer asked for different block, expected = %d,%d, requested %d", bcR.block.Height, bcR.nextBlock.Height, msg.Height))
return false
}
func (bcR *BlockchainReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte) {
msg, err := bc.DecodeMsg(msgBytes)
if err != nil {
bcR.Logger.Error("Error decoding message", fmt.Sprintf("src: %s", src), fmt.Sprintf("chId: %b", chID), err)
bcR.Switch.StopPeerForError(src, err)
return
}
switch msg := msg.(type) {
case *bcproto.StatusRequest:
bcR.Logger.Info("Incoming status request")
bcR.sendStatusToPeer(src)
case *bcproto.BlockRequest:
bcR.Logger.Info("Incoming block request", "height", msg.Height)
bcR.sendBlockToPeer(msg, src)
case *bcproto.StatusResponse:
bcR.Logger.Info("Incoming status response", "base", msg.Base, "height", msg.Height)
default:
bcR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg)))
}
}
func MakeNodeInfo(
config *Config,
nodeKey *p2p.NodeKey,
genDoc *GenesisDoc,
) (p2p.NodeInfo, error) {
nodeInfo := p2p.DefaultNodeInfo{
DefaultNodeID: nodeKey.ID(),
Network: genDoc.ChainID,
Version: version.TMCoreSemVer,
Channels: []byte{bcv0.BlockchainChannel},
Moniker: config.Moniker,
Other: p2p.DefaultNodeInfoOther{
TxIndex: "off",
RPCAddress: config.RPC.ListenAddress,
},
}
lAddr := config.P2P.ExternalAddress
if lAddr == "" {
lAddr = config.P2P.ListenAddress
}
nodeInfo.ListenAddr = lAddr
err := nodeInfo.Validate()
return nodeInfo, err
}
func CreateSwitch(config *Config,
transport p2p.Transport,
bcReactor p2p.Reactor,
nodeInfo p2p.NodeInfo,
nodeKey *p2p.NodeKey,
logger tmLog.Logger) *p2p.Switch {
sw := p2p.NewSwitch(
config.P2P,
transport,
)
sw.SetLogger(logger)
bcReactor.SetLogger(logger)
sw.AddReactor("BLOCKCHAIN", bcReactor)
sw.SetNodeInfo(nodeInfo)
sw.SetNodeKey(nodeKey)
logger.Info("P2P Node ID", "ID", nodeKey.ID())
return sw
}