peer_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "math/big"
  19. "net"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/mclock"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/eth"
  26. "github.com/ethereum/go-ethereum/les/flowcontrol"
  27. "github.com/ethereum/go-ethereum/p2p"
  28. "github.com/ethereum/go-ethereum/p2p/enode"
  29. "github.com/ethereum/go-ethereum/rlp"
  30. )
  31. const protocolVersion = lpv2
  32. var (
  33. hash = common.HexToHash("deadbeef")
  34. genesis = common.HexToHash("cafebabe")
  35. headNum = uint64(1234)
  36. td = big.NewInt(123)
  37. )
  38. func newNodeID(t *testing.T) *enode.Node {
  39. key, err := crypto.GenerateKey()
  40. if err != nil {
  41. t.Fatal("generate key err:", err)
  42. }
  43. return enode.NewV4(&key.PublicKey, net.IP{}, 35000, 35000)
  44. }
  45. // ulc connects to trusted peer and send announceType=announceTypeSigned
  46. func TestPeerHandshakeSetAnnounceTypeToAnnounceTypeSignedForTrustedPeer(t *testing.T) {
  47. id := newNodeID(t).ID()
  48. // peer to connect(on ulc side)
  49. p := peer{
  50. Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
  51. version: protocolVersion,
  52. trusted: true,
  53. rw: &rwStub{
  54. WriteHook: func(recvList keyValueList) {
  55. recv, _ := recvList.decode()
  56. var reqType uint64
  57. err := recv.get("announceType", &reqType)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if reqType != announceTypeSigned {
  62. t.Fatal("Expected announceTypeSigned")
  63. }
  64. },
  65. ReadHook: func(l keyValueList) keyValueList {
  66. l = l.add("serveHeaders", nil)
  67. l = l.add("serveChainSince", uint64(0))
  68. l = l.add("serveStateSince", uint64(0))
  69. l = l.add("txRelay", nil)
  70. l = l.add("flowControl/BL", uint64(0))
  71. l = l.add("flowControl/MRR", uint64(0))
  72. l = l.add("flowControl/MRC", testCostList(0))
  73. return l
  74. },
  75. },
  76. network: NetworkId,
  77. }
  78. err := p.Handshake(td, hash, headNum, genesis, nil)
  79. if err != nil {
  80. t.Fatalf("Handshake error: %s", err)
  81. }
  82. if p.announceType != announceTypeSigned {
  83. t.Fatal("Incorrect announceType")
  84. }
  85. }
  86. func TestPeerHandshakeAnnounceTypeSignedForTrustedPeersPeerNotInTrusted(t *testing.T) {
  87. id := newNodeID(t).ID()
  88. p := peer{
  89. Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
  90. version: protocolVersion,
  91. rw: &rwStub{
  92. WriteHook: func(recvList keyValueList) {
  93. // checking that ulc sends to peer allowedRequests=noRequests and announceType != announceTypeSigned
  94. recv, _ := recvList.decode()
  95. var reqType uint64
  96. err := recv.get("announceType", &reqType)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if reqType == announceTypeSigned {
  101. t.Fatal("Expected not announceTypeSigned")
  102. }
  103. },
  104. ReadHook: func(l keyValueList) keyValueList {
  105. l = l.add("serveHeaders", nil)
  106. l = l.add("serveChainSince", uint64(0))
  107. l = l.add("serveStateSince", uint64(0))
  108. l = l.add("txRelay", nil)
  109. l = l.add("flowControl/BL", uint64(0))
  110. l = l.add("flowControl/MRR", uint64(0))
  111. l = l.add("flowControl/MRC", testCostList(0))
  112. return l
  113. },
  114. },
  115. network: NetworkId,
  116. }
  117. err := p.Handshake(td, hash, headNum, genesis, nil)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. if p.announceType == announceTypeSigned {
  122. t.Fatal("Incorrect announceType")
  123. }
  124. }
  125. func TestPeerHandshakeDefaultAllRequests(t *testing.T) {
  126. id := newNodeID(t).ID()
  127. s := generateLesServer()
  128. p := peer{
  129. Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
  130. version: protocolVersion,
  131. rw: &rwStub{
  132. ReadHook: func(l keyValueList) keyValueList {
  133. l = l.add("announceType", uint64(announceTypeSigned))
  134. l = l.add("allowedRequests", uint64(0))
  135. return l
  136. },
  137. },
  138. network: NetworkId,
  139. }
  140. err := p.Handshake(td, hash, headNum, genesis, s)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if p.onlyAnnounce {
  145. t.Fatal("Incorrect announceType")
  146. }
  147. }
  148. func TestPeerHandshakeServerSendOnlyAnnounceRequestsHeaders(t *testing.T) {
  149. id := newNodeID(t).ID()
  150. s := generateLesServer()
  151. s.config.UltraLightOnlyAnnounce = true
  152. p := peer{
  153. Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
  154. version: protocolVersion,
  155. rw: &rwStub{
  156. ReadHook: func(l keyValueList) keyValueList {
  157. l = l.add("announceType", uint64(announceTypeSigned))
  158. return l
  159. },
  160. WriteHook: func(l keyValueList) {
  161. for _, v := range l {
  162. if v.Key == "serveHeaders" ||
  163. v.Key == "serveChainSince" ||
  164. v.Key == "serveStateSince" ||
  165. v.Key == "txRelay" {
  166. t.Fatalf("%v exists", v.Key)
  167. }
  168. }
  169. },
  170. },
  171. network: NetworkId,
  172. }
  173. err := p.Handshake(td, hash, headNum, genesis, s)
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. }
  178. func TestPeerHandshakeClientReceiveOnlyAnnounceRequestsHeaders(t *testing.T) {
  179. id := newNodeID(t).ID()
  180. p := peer{
  181. Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
  182. version: protocolVersion,
  183. rw: &rwStub{
  184. ReadHook: func(l keyValueList) keyValueList {
  185. l = l.add("flowControl/BL", uint64(0))
  186. l = l.add("flowControl/MRR", uint64(0))
  187. l = l.add("flowControl/MRC", RequestCostList{})
  188. l = l.add("announceType", uint64(announceTypeSigned))
  189. return l
  190. },
  191. },
  192. network: NetworkId,
  193. trusted: true,
  194. }
  195. err := p.Handshake(td, hash, headNum, genesis, nil)
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. if !p.onlyAnnounce {
  200. t.Fatal("onlyAnnounce must be true")
  201. }
  202. }
  203. func TestPeerHandshakeClientReturnErrorOnUselessPeer(t *testing.T) {
  204. id := newNodeID(t).ID()
  205. p := peer{
  206. Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
  207. version: protocolVersion,
  208. rw: &rwStub{
  209. ReadHook: func(l keyValueList) keyValueList {
  210. l = l.add("flowControl/BL", uint64(0))
  211. l = l.add("flowControl/MRR", uint64(0))
  212. l = l.add("flowControl/MRC", RequestCostList{})
  213. l = l.add("announceType", uint64(announceTypeSigned))
  214. return l
  215. },
  216. },
  217. network: NetworkId,
  218. }
  219. err := p.Handshake(td, hash, headNum, genesis, nil)
  220. if err == nil {
  221. t.FailNow()
  222. }
  223. }
  224. func generateLesServer() *LesServer {
  225. s := &LesServer{
  226. lesCommons: lesCommons{
  227. config: &eth.Config{UltraLightOnlyAnnounce: true},
  228. },
  229. defParams: flowcontrol.ServerParams{
  230. BufLimit: uint64(300000000),
  231. MinRecharge: uint64(50000),
  232. },
  233. fcManager: flowcontrol.NewClientManager(nil, &mclock.System{}),
  234. }
  235. s.costTracker, _ = newCostTracker(rawdb.NewMemoryDatabase(), s.config)
  236. return s
  237. }
  238. type rwStub struct {
  239. ReadHook func(l keyValueList) keyValueList
  240. WriteHook func(l keyValueList)
  241. }
  242. func (s *rwStub) ReadMsg() (p2p.Msg, error) {
  243. payload := keyValueList{}
  244. payload = payload.add("protocolVersion", uint64(protocolVersion))
  245. payload = payload.add("networkId", uint64(NetworkId))
  246. payload = payload.add("headTd", td)
  247. payload = payload.add("headHash", hash)
  248. payload = payload.add("headNum", headNum)
  249. payload = payload.add("genesisHash", genesis)
  250. if s.ReadHook != nil {
  251. payload = s.ReadHook(payload)
  252. }
  253. size, p, err := rlp.EncodeToReader(payload)
  254. if err != nil {
  255. return p2p.Msg{}, err
  256. }
  257. return p2p.Msg{
  258. Size: uint32(size),
  259. Payload: p,
  260. }, nil
  261. }
  262. func (s *rwStub) WriteMsg(m p2p.Msg) error {
  263. recvList := keyValueList{}
  264. if err := m.Decode(&recvList); err != nil {
  265. return err
  266. }
  267. if s.WriteHook != nil {
  268. s.WriteHook(recvList)
  269. }
  270. return nil
  271. }