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