github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/p2p/enode/localnode_test.go (about)

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