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