github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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  	"testing"
    21  
    22  	"github.com/ethereum/go-ethereum/crypto"
    23  	"github.com/ethereum/go-ethereum/p2p/enr"
    24  )
    25  
    26  func newLocalNodeForTesting() (*LocalNode, *DB) {
    27  	db, _ := OpenDB("")
    28  	key, _ := crypto.GenerateKey()
    29  	return NewLocalNode(db, key), db
    30  }
    31  
    32  func TestLocalNode(t *testing.T) {
    33  	ln, db := newLocalNodeForTesting()
    34  	defer db.Close()
    35  
    36  	if ln.Node().ID() != ln.ID() {
    37  		t.Fatal("inconsistent ID")
    38  	}
    39  
    40  	ln.Set(enr.WithEntry("x", uint(3)))
    41  	var x uint
    42  	if err := ln.Node().Load(enr.WithEntry("x", &x)); err != nil {
    43  		t.Fatal("can't load entry 'x':", err)
    44  	} else if x != 3 {
    45  		t.Fatal("wrong value for entry 'x':", x)
    46  	}
    47  }
    48  
    49  func TestLocalNodeSeqPersist(t *testing.T) {
    50  	ln, db := newLocalNodeForTesting()
    51  	defer db.Close()
    52  
    53  	if s := ln.Node().Seq(); s != 1 {
    54  		t.Fatalf("wrong initial seq %d, want 1", s)
    55  	}
    56  	ln.Set(enr.WithEntry("x", uint(1)))
    57  	if s := ln.Node().Seq(); s != 2 {
    58  		t.Fatalf("wrong seq %d after set, want 2", s)
    59  	}
    60  
    61  	// Create a new instance, it should reload the sequence number.
    62  	// The number increases just after that because a new record is
    63  	// created without the "x" entry.
    64  	ln2 := NewLocalNode(db, ln.key)
    65  	if s := ln2.Node().Seq(); s != 3 {
    66  		t.Fatalf("wrong seq %d on new instance, want 3", s)
    67  	}
    68  
    69  	// Create a new instance with a different node key on the same database.
    70  	// This should reset the sequence number.
    71  	key, _ := crypto.GenerateKey()
    72  	ln3 := NewLocalNode(db, key)
    73  	if s := ln3.Node().Seq(); s != 1 {
    74  		t.Fatalf("wrong seq %d on instance with changed key, want 1", s)
    75  	}
    76  }