-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprotocol.go
150 lines (131 loc) · 3.62 KB
/
protocol.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
package main
import (
"fmt"
"io"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/rlp"
)
func newManspreadingProtocol() p2p.Protocol {
return p2p.Protocol{
Name: eth.ProtocolName,
Version: eth.ProtocolVersions[0],
Length: eth.ProtocolLengths[0],
Run: handle,
NodeInfo: func() interface{} {
fmt.Println("Noop: NodeInfo called")
return nil
},
PeerInfo: func(id discover.NodeID) interface{} {
fmt.Println("Noop: PeerInfo called")
return nil
},
}
}
func handle(p *p2p.Peer, rw p2p.MsgReadWriter) error {
fmt.Println("Run called")
for {
fmt.Println("Waiting for msg...")
msg, err := rw.ReadMsg()
fmt.Println("Got a msg from: ", fromWhom(p.ID().String()))
if err != nil {
fmt.Println("readMsg err: ", err)
if err == io.EOF {
fmt.Println(fromWhom(p.ID().String()), " has dropped its connection...")
pxy.lock.Lock()
if p.ID() == pxy.upstreamNode.ID {
pxy.upstreamConn = nil
} else {
pxy.downstreamConn = nil
}
pxy.lock.Unlock()
}
return err
}
fmt.Println("msg.Code: ", msg.Code)
if msg.Code == eth.StatusMsg { // handshake
var myMessage statusData
err = msg.Decode(&myMessage)
if err != nil {
fmt.Println("decode statusData err: ", err)
return err
}
fmt.Println("ProtocolVersion: ", myMessage.ProtocolVersion)
fmt.Println("NetworkId: ", myMessage.NetworkId)
fmt.Println("TD: ", myMessage.TD)
fmt.Println("CurrentBlock: ", myMessage.CurrentBlock.Hex())
fmt.Println("GenesisBlock: ", myMessage.GenesisBlock.Hex())
pxy.lock.Lock()
if p.ID() == pxy.upstreamNode.ID {
pxy.upstreamState = myMessage
pxy.upstreamConn = &conn{p, rw}
} else {
pxy.downstreamConn = &conn{p, rw}
}
pxy.lock.Unlock()
err = p2p.Send(rw, eth.StatusMsg, &statusData{
ProtocolVersion: myMessage.ProtocolVersion,
NetworkId: myMessage.NetworkId,
TD: pxy.upstreamState.TD,
CurrentBlock: pxy.upstreamState.CurrentBlock,
GenesisBlock: myMessage.GenesisBlock,
})
if err != nil {
fmt.Println("handshake err: ", err)
return err
}
} else if msg.Code == eth.NewBlockMsg {
var myMessage newBlockData
err = msg.Decode(&myMessage)
if err != nil {
fmt.Println("decode newBlockMsg err: ", err)
}
pxy.lock.Lock()
if p.ID() == pxy.upstreamNode.ID {
pxy.upstreamState.CurrentBlock = myMessage.Block.Hash()
pxy.upstreamState.TD = myMessage.TD
} //TODO: handle newBlock from downstream
pxy.lock.Unlock()
// need to re-encode msg
size, r, err := rlp.EncodeToReader(myMessage)
if err != nil {
fmt.Println("encoding newBlockMsg err: ", err)
}
relay(p, p2p.Msg{Code: eth.NewBlockMsg, Size: uint32(size), Payload: r})
} else {
relay(p, msg)
}
}
return nil
}
func relay(p *p2p.Peer, msg p2p.Msg) {
var err error
pxy.lock.RLock()
defer pxy.lock.RUnlock()
if p.ID() != pxy.upstreamNode.ID && pxy.upstreamConn != nil {
err = pxy.upstreamConn.rw.WriteMsg(msg)
} else if p.ID() == pxy.upstreamNode.ID && pxy.downstreamConn != nil {
err = pxy.downstreamConn.rw.WriteMsg(msg)
} else {
fmt.Println("One of upstream/downstream isn't alive: ", pxy.srv.Peers())
}
if err != nil {
fmt.Println("relaying err: ", err)
}
}
func (pxy *proxy) upstreamAlive() bool {
for _, peer := range pxy.srv.Peers() {
if peer.ID() == pxy.upstreamNode.ID {
return true
}
}
return false
}
func fromWhom(nodeId string) string {
if nodeId == pxy.upstreamNode.ID.String() {
return "upstream"
} else {
return "downstream"
}
}