github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/p2p/enode/node_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  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math/big"
    24  	"testing"
    25  	"testing/quick"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  
    29  	"github.com/scroll-tech/go-ethereum/p2p/enr"
    30  	"github.com/scroll-tech/go-ethereum/rlp"
    31  )
    32  
    33  var pyRecord, _ = hex.DecodeString("f884b8407098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c01826964827634826970847f00000189736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31388375647082765f")
    34  
    35  // TestPythonInterop checks that we can decode and verify a record produced by the Python
    36  // implementation.
    37  func TestPythonInterop(t *testing.T) {
    38  	var r enr.Record
    39  	if err := rlp.DecodeBytes(pyRecord, &r); err != nil {
    40  		t.Fatalf("can't decode: %v", err)
    41  	}
    42  	n, err := New(ValidSchemes, &r)
    43  	if err != nil {
    44  		t.Fatalf("can't verify record: %v", err)
    45  	}
    46  
    47  	var (
    48  		wantID  = HexID("a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7")
    49  		wantSeq = uint64(1)
    50  		wantIP  = enr.IPv4{127, 0, 0, 1}
    51  		wantUDP = enr.UDP(30303)
    52  	)
    53  	if n.Seq() != wantSeq {
    54  		t.Errorf("wrong seq: got %d, want %d", n.Seq(), wantSeq)
    55  	}
    56  	if n.ID() != wantID {
    57  		t.Errorf("wrong id: got %x, want %x", n.ID(), wantID)
    58  	}
    59  	want := map[enr.Entry]interface{}{new(enr.IPv4): &wantIP, new(enr.UDP): &wantUDP}
    60  	for k, v := range want {
    61  		desc := fmt.Sprintf("loading key %q", k.ENRKey())
    62  		if assert.NoError(t, n.Load(k), desc) {
    63  			assert.Equal(t, k, v, desc)
    64  		}
    65  	}
    66  }
    67  
    68  func TestHexID(t *testing.T) {
    69  	ref := ID{0, 0, 0, 0, 0, 0, 0, 128, 106, 217, 182, 31, 165, 174, 1, 67, 7, 235, 220, 150, 66, 83, 173, 205, 159, 44, 10, 57, 42, 161, 26, 188}
    70  	id1 := HexID("0x00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
    71  	id2 := HexID("00000000000000806ad9b61fa5ae014307ebdc964253adcd9f2c0a392aa11abc")
    72  
    73  	if id1 != ref {
    74  		t.Errorf("wrong id1\ngot  %v\nwant %v", id1[:], ref[:])
    75  	}
    76  	if id2 != ref {
    77  		t.Errorf("wrong id2\ngot  %v\nwant %v", id2[:], ref[:])
    78  	}
    79  }
    80  
    81  func TestID_textEncoding(t *testing.T) {
    82  	ref := ID{
    83  		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
    84  		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
    85  		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
    86  		0x31, 0x32,
    87  	}
    88  	hex := "0102030405060708091011121314151617181920212223242526272829303132"
    89  
    90  	text, err := ref.MarshalText()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	if !bytes.Equal(text, []byte(hex)) {
    95  		t.Fatalf("text encoding did not match\nexpected: %s\ngot:      %s", hex, text)
    96  	}
    97  
    98  	id := new(ID)
    99  	if err := id.UnmarshalText(text); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	if *id != ref {
   103  		t.Fatalf("text decoding did not match\nexpected: %s\ngot:      %s", ref, id)
   104  	}
   105  }
   106  
   107  func TestID_distcmp(t *testing.T) {
   108  	distcmpBig := func(target, a, b ID) int {
   109  		tbig := new(big.Int).SetBytes(target[:])
   110  		abig := new(big.Int).SetBytes(a[:])
   111  		bbig := new(big.Int).SetBytes(b[:])
   112  		return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig))
   113  	}
   114  	if err := quick.CheckEqual(DistCmp, distcmpBig, nil); err != nil {
   115  		t.Error(err)
   116  	}
   117  }
   118  
   119  // The random tests is likely to miss the case where a and b are equal,
   120  // this test checks it explicitly.
   121  func TestID_distcmpEqual(t *testing.T) {
   122  	base := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   123  	x := ID{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
   124  	if DistCmp(base, x, x) != 0 {
   125  		t.Errorf("DistCmp(base, x, x) != 0")
   126  	}
   127  }
   128  
   129  func TestID_logdist(t *testing.T) {
   130  	logdistBig := func(a, b ID) int {
   131  		abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:])
   132  		return new(big.Int).Xor(abig, bbig).BitLen()
   133  	}
   134  	if err := quick.CheckEqual(LogDist, logdistBig, nil); err != nil {
   135  		t.Error(err)
   136  	}
   137  }
   138  
   139  // The random tests is likely to miss the case where a and b are equal,
   140  // this test checks it explicitly.
   141  func TestID_logdistEqual(t *testing.T) {
   142  	x := ID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   143  	if LogDist(x, x) != 0 {
   144  		t.Errorf("LogDist(x, x) != 0")
   145  	}
   146  }