localnode_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 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 enode
  17. import (
  18. "math/rand"
  19. "net"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/p2p/enr"
  23. "github.com/stretchr/testify/assert"
  24. )
  25. func newLocalNodeForTesting() (*LocalNode, *DB) {
  26. db, _ := OpenDB("")
  27. key, _ := crypto.GenerateKey()
  28. return NewLocalNode(db, key), db
  29. }
  30. func TestLocalNode(t *testing.T) {
  31. ln, db := newLocalNodeForTesting()
  32. defer db.Close()
  33. if ln.Node().ID() != ln.ID() {
  34. t.Fatal("inconsistent ID")
  35. }
  36. ln.Set(enr.WithEntry("x", uint(3)))
  37. var x uint
  38. if err := ln.Node().Load(enr.WithEntry("x", &x)); err != nil {
  39. t.Fatal("can't load entry 'x':", err)
  40. } else if x != 3 {
  41. t.Fatal("wrong value for entry 'x':", x)
  42. }
  43. }
  44. func TestLocalNodeSeqPersist(t *testing.T) {
  45. ln, db := newLocalNodeForTesting()
  46. defer db.Close()
  47. if s := ln.Node().Seq(); s != 1 {
  48. t.Fatalf("wrong initial seq %d, want 1", s)
  49. }
  50. ln.Set(enr.WithEntry("x", uint(1)))
  51. if s := ln.Node().Seq(); s != 2 {
  52. t.Fatalf("wrong seq %d after set, want 2", s)
  53. }
  54. // Create a new instance, it should reload the sequence number.
  55. // The number increases just after that because a new record is
  56. // created without the "x" entry.
  57. ln2 := NewLocalNode(db, ln.key)
  58. if s := ln2.Node().Seq(); s != 3 {
  59. t.Fatalf("wrong seq %d on new instance, want 3", s)
  60. }
  61. // Create a new instance with a different node key on the same database.
  62. // This should reset the sequence number.
  63. key, _ := crypto.GenerateKey()
  64. ln3 := NewLocalNode(db, key)
  65. if s := ln3.Node().Seq(); s != 1 {
  66. t.Fatalf("wrong seq %d on instance with changed key, want 1", s)
  67. }
  68. }
  69. // This test checks behavior of the endpoint predictor.
  70. func TestLocalNodeEndpoint(t *testing.T) {
  71. var (
  72. fallback = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80}
  73. predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81}
  74. staticIP = net.IP{127, 0, 1, 2}
  75. )
  76. ln, db := newLocalNodeForTesting()
  77. defer db.Close()
  78. // Nothing is set initially.
  79. assert.Equal(t, net.IP(nil), ln.Node().IP())
  80. assert.Equal(t, 0, ln.Node().UDP())
  81. assert.Equal(t, uint64(1), ln.Node().Seq())
  82. // Set up fallback address.
  83. ln.SetFallbackIP(fallback.IP)
  84. ln.SetFallbackUDP(fallback.Port)
  85. assert.Equal(t, fallback.IP, ln.Node().IP())
  86. assert.Equal(t, fallback.Port, ln.Node().UDP())
  87. assert.Equal(t, uint64(2), ln.Node().Seq())
  88. // Add endpoint statements from random hosts.
  89. for i := 0; i < iptrackMinStatements; i++ {
  90. assert.Equal(t, fallback.IP, ln.Node().IP())
  91. assert.Equal(t, fallback.Port, ln.Node().UDP())
  92. assert.Equal(t, uint64(2), ln.Node().Seq())
  93. from := &net.UDPAddr{IP: make(net.IP, 4), Port: 90}
  94. rand.Read(from.IP)
  95. ln.UDPEndpointStatement(from, predicted)
  96. }
  97. assert.Equal(t, predicted.IP, ln.Node().IP())
  98. assert.Equal(t, predicted.Port, ln.Node().UDP())
  99. assert.Equal(t, uint64(3), ln.Node().Seq())
  100. // Static IP overrides prediction.
  101. ln.SetStaticIP(staticIP)
  102. assert.Equal(t, staticIP, ln.Node().IP())
  103. assert.Equal(t, fallback.Port, ln.Node().UDP())
  104. assert.Equal(t, uint64(4), ln.Node().Seq())
  105. }