localnode_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // This test checks that the sequence number is persisted between restarts.
  45. func TestLocalNodeSeqPersist(t *testing.T) {
  46. timestamp := nowMilliseconds()
  47. ln, db := newLocalNodeForTesting()
  48. defer db.Close()
  49. initialSeq := ln.Node().Seq()
  50. if initialSeq < timestamp {
  51. t.Fatalf("wrong initial seq %d, want at least %d", initialSeq, timestamp)
  52. }
  53. ln.Set(enr.WithEntry("x", uint(1)))
  54. if s := ln.Node().Seq(); s != initialSeq+1 {
  55. t.Fatalf("wrong seq %d after set, want %d", s, initialSeq+1)
  56. }
  57. // Create a new instance, it should reload the sequence number.
  58. // The number increases just after that because a new record is
  59. // created without the "x" entry.
  60. ln2 := NewLocalNode(db, ln.key)
  61. if s := ln2.Node().Seq(); s != initialSeq+2 {
  62. t.Fatalf("wrong seq %d on new instance, want %d", s, initialSeq+2)
  63. }
  64. finalSeq := ln2.Node().Seq()
  65. // Create a new instance with a different node key on the same database.
  66. // This should reset the sequence number.
  67. key, _ := crypto.GenerateKey()
  68. ln3 := NewLocalNode(db, key)
  69. if s := ln3.Node().Seq(); s < finalSeq {
  70. t.Fatalf("wrong seq %d on instance with changed key, want >= %d", s, finalSeq)
  71. }
  72. }
  73. // This test checks behavior of the endpoint predictor.
  74. func TestLocalNodeEndpoint(t *testing.T) {
  75. var (
  76. fallback = &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 80}
  77. predicted = &net.UDPAddr{IP: net.IP{127, 0, 1, 2}, Port: 81}
  78. staticIP = net.IP{127, 0, 1, 2}
  79. )
  80. ln, db := newLocalNodeForTesting()
  81. defer db.Close()
  82. // Nothing is set initially.
  83. assert.Equal(t, net.IP(nil), ln.Node().IP())
  84. assert.Equal(t, 0, ln.Node().UDP())
  85. initialSeq := ln.Node().Seq()
  86. // Set up fallback address.
  87. ln.SetFallbackIP(fallback.IP)
  88. ln.SetFallbackUDP(fallback.Port)
  89. assert.Equal(t, fallback.IP, ln.Node().IP())
  90. assert.Equal(t, fallback.Port, ln.Node().UDP())
  91. assert.Equal(t, initialSeq+1, ln.Node().Seq())
  92. // Add endpoint statements from random hosts.
  93. for i := 0; i < iptrackMinStatements; i++ {
  94. assert.Equal(t, fallback.IP, ln.Node().IP())
  95. assert.Equal(t, fallback.Port, ln.Node().UDP())
  96. assert.Equal(t, initialSeq+1, ln.Node().Seq())
  97. from := &net.UDPAddr{IP: make(net.IP, 4), Port: 90}
  98. rand.Read(from.IP)
  99. ln.UDPEndpointStatement(from, predicted)
  100. }
  101. assert.Equal(t, predicted.IP, ln.Node().IP())
  102. assert.Equal(t, predicted.Port, ln.Node().UDP())
  103. assert.Equal(t, initialSeq+2, ln.Node().Seq())
  104. // Static IP overrides prediction.
  105. ln.SetStaticIP(staticIP)
  106. assert.Equal(t, staticIP, ln.Node().IP())
  107. assert.Equal(t, fallback.Port, ln.Node().UDP())
  108. assert.Equal(t, initialSeq+3, ln.Node().Seq())
  109. }