framework.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package v4test
  17. import (
  18. "crypto/ecdsa"
  19. "fmt"
  20. "net"
  21. "time"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/p2p/discover/v4wire"
  24. "github.com/ethereum/go-ethereum/p2p/enode"
  25. )
  26. const waitTime = 300 * time.Millisecond
  27. type testenv struct {
  28. l1, l2 net.PacketConn
  29. key *ecdsa.PrivateKey
  30. remote *enode.Node
  31. remoteAddr *net.UDPAddr
  32. }
  33. func newTestEnv(remote string, listen1, listen2 string) *testenv {
  34. l1, err := net.ListenPacket("udp", fmt.Sprintf("%v:0", listen1))
  35. if err != nil {
  36. panic(err)
  37. }
  38. l2, err := net.ListenPacket("udp", fmt.Sprintf("%v:0", listen2))
  39. if err != nil {
  40. panic(err)
  41. }
  42. key, err := crypto.GenerateKey()
  43. if err != nil {
  44. panic(err)
  45. }
  46. node, err := enode.Parse(enode.ValidSchemes, remote)
  47. if err != nil {
  48. panic(err)
  49. }
  50. if node.IP() == nil || node.UDP() == 0 {
  51. var ip net.IP
  52. var tcpPort, udpPort int
  53. if ip = node.IP(); ip == nil {
  54. ip = net.ParseIP("127.0.0.1")
  55. }
  56. if tcpPort = node.TCP(); tcpPort == 0 {
  57. tcpPort = 30303
  58. }
  59. if udpPort = node.TCP(); udpPort == 0 {
  60. udpPort = 30303
  61. }
  62. node = enode.NewV4(node.Pubkey(), ip, tcpPort, udpPort)
  63. }
  64. addr := &net.UDPAddr{IP: node.IP(), Port: node.UDP()}
  65. return &testenv{l1, l2, key, node, addr}
  66. }
  67. func (te *testenv) close() {
  68. te.l1.Close()
  69. te.l2.Close()
  70. }
  71. func (te *testenv) send(c net.PacketConn, req v4wire.Packet) []byte {
  72. packet, hash, err := v4wire.Encode(te.key, req)
  73. if err != nil {
  74. panic(fmt.Errorf("can't encode %v packet: %v", req.Name(), err))
  75. }
  76. if _, err := c.WriteTo(packet, te.remoteAddr); err != nil {
  77. panic(fmt.Errorf("can't send %v: %v", req.Name(), err))
  78. }
  79. return hash
  80. }
  81. func (te *testenv) read(c net.PacketConn) (v4wire.Packet, []byte, error) {
  82. buf := make([]byte, 2048)
  83. if err := c.SetReadDeadline(time.Now().Add(waitTime)); err != nil {
  84. return nil, nil, err
  85. }
  86. n, _, err := c.ReadFrom(buf)
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. p, _, hash, err := v4wire.Decode(buf[:n])
  91. return p, hash, err
  92. }
  93. func (te *testenv) localEndpoint(c net.PacketConn) v4wire.Endpoint {
  94. addr := c.LocalAddr().(*net.UDPAddr)
  95. return v4wire.Endpoint{
  96. IP: addr.IP.To4(),
  97. UDP: uint16(addr.Port),
  98. TCP: 0,
  99. }
  100. }
  101. func (te *testenv) remoteEndpoint() v4wire.Endpoint {
  102. return v4wire.NewEndpoint(te.remoteAddr, 0)
  103. }
  104. func contains(ns []v4wire.Node, key v4wire.Pubkey) bool {
  105. for _, n := range ns {
  106. if n.ID == key {
  107. return true
  108. }
  109. }
  110. return false
  111. }