github.com/jimmyx0x/go-ethereum@v1.10.28/les/vflux/server/clientdb_test.go (about) 1 // Copyright 2020 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 server 18 19 import ( 20 "reflect" 21 "testing" 22 "time" 23 24 "github.com/ethereum/go-ethereum/common/mclock" 25 "github.com/ethereum/go-ethereum/core/rawdb" 26 "github.com/ethereum/go-ethereum/les/utils" 27 "github.com/ethereum/go-ethereum/p2p/enode" 28 ) 29 30 func expval(v uint64) utils.ExpiredValue { 31 return utils.ExpiredValue{Base: v} 32 } 33 34 func TestNodeDB(t *testing.T) { 35 ndb := newNodeDB(rawdb.NewMemoryDatabase(), mclock.System{}) 36 defer ndb.close() 37 38 var cases = []struct { 39 id enode.ID 40 ip string 41 balance utils.ExpiredValue 42 positive bool 43 }{ 44 {enode.ID{0x00, 0x01, 0x02}, "", expval(100), true}, 45 {enode.ID{0x00, 0x01, 0x02}, "", expval(200), true}, 46 {enode.ID{}, "127.0.0.1", expval(100), false}, 47 {enode.ID{}, "127.0.0.1", expval(200), false}, 48 } 49 for _, c := range cases { 50 if c.positive { 51 ndb.setBalance(c.id.Bytes(), false, c.balance) 52 if pb := ndb.getOrNewBalance(c.id.Bytes(), false); !reflect.DeepEqual(pb, c.balance) { 53 t.Fatalf("Positive balance mismatch, want %v, got %v", c.balance, pb) 54 } 55 } else { 56 ndb.setBalance([]byte(c.ip), true, c.balance) 57 if nb := ndb.getOrNewBalance([]byte(c.ip), true); !reflect.DeepEqual(nb, c.balance) { 58 t.Fatalf("Negative balance mismatch, want %v, got %v", c.balance, nb) 59 } 60 } 61 } 62 for _, c := range cases { 63 if c.positive { 64 ndb.delBalance(c.id.Bytes(), false) 65 if pb := ndb.getOrNewBalance(c.id.Bytes(), false); !reflect.DeepEqual(pb, utils.ExpiredValue{}) { 66 t.Fatalf("Positive balance mismatch, want %v, got %v", utils.ExpiredValue{}, pb) 67 } 68 } else { 69 ndb.delBalance([]byte(c.ip), true) 70 if nb := ndb.getOrNewBalance([]byte(c.ip), true); !reflect.DeepEqual(nb, utils.ExpiredValue{}) { 71 t.Fatalf("Negative balance mismatch, want %v, got %v", utils.ExpiredValue{}, nb) 72 } 73 } 74 } 75 posExp, negExp := utils.Fixed64(1000), utils.Fixed64(2000) 76 ndb.setExpiration(posExp, negExp) 77 if pos, neg := ndb.getExpiration(); pos != posExp || neg != negExp { 78 t.Fatalf("Expiration mismatch, want %v / %v, got %v / %v", posExp, negExp, pos, neg) 79 } 80 /* curBalance := currencyBalance{typ: "ETH", amount: 10000} 81 ndb.setCurrencyBalance(enode.ID{0x01, 0x02}, curBalance) 82 if got := ndb.getCurrencyBalance(enode.ID{0x01, 0x02}); !reflect.DeepEqual(got, curBalance) { 83 t.Fatalf("Currency balance mismatch, want %v, got %v", curBalance, got) 84 }*/ 85 } 86 87 func TestNodeDBExpiration(t *testing.T) { 88 var ( 89 iterated int 90 done = make(chan struct{}, 1) 91 ) 92 callback := func(now mclock.AbsTime, neg bool, b utils.ExpiredValue) bool { 93 iterated += 1 94 return true 95 } 96 clock := &mclock.Simulated{} 97 ndb := newNodeDB(rawdb.NewMemoryDatabase(), clock) 98 defer ndb.close() 99 ndb.evictCallBack = callback 100 ndb.cleanupHook = func() { done <- struct{}{} } 101 102 var cases = []struct { 103 id []byte 104 neg bool 105 balance utils.ExpiredValue 106 }{ 107 {[]byte{0x01, 0x02}, false, expval(1)}, 108 {[]byte{0x03, 0x04}, false, expval(1)}, 109 {[]byte{0x05, 0x06}, false, expval(1)}, 110 {[]byte{0x07, 0x08}, false, expval(1)}, 111 112 {[]byte("127.0.0.1"), true, expval(1)}, 113 {[]byte("127.0.0.2"), true, expval(1)}, 114 {[]byte("127.0.0.3"), true, expval(1)}, 115 {[]byte("127.0.0.4"), true, expval(1)}, 116 } 117 for _, c := range cases { 118 ndb.setBalance(c.id, c.neg, c.balance) 119 } 120 clock.WaitForTimers(1) 121 clock.Run(time.Hour + time.Minute) 122 select { 123 case <-done: 124 case <-time.NewTimer(time.Second).C: 125 t.Fatalf("timeout") 126 } 127 if iterated != 8 { 128 t.Fatalf("Failed to evict useless balances, want %v, got %d", 8, iterated) 129 } 130 131 for _, c := range cases { 132 ndb.setBalance(c.id, c.neg, c.balance) 133 } 134 clock.WaitForTimers(1) 135 clock.Run(time.Hour + time.Minute) 136 select { 137 case <-done: 138 case <-time.NewTimer(time.Second).C: 139 t.Fatalf("timeout") 140 } 141 if iterated != 16 { 142 t.Fatalf("Failed to evict useless balances, want %v, got %d", 16, iterated) 143 } 144 }