github.com/simplechain-org/go-simplechain@v1.0.6/p2p/discv5/database_test.go (about) 1 // Copyright 2015 The go-simplechain Authors 2 // This file is part of the go-simplechain library. 3 // 4 // The go-simplechain 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-simplechain 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-simplechain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package discv5 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "net" 23 "os" 24 "path/filepath" 25 "reflect" 26 "testing" 27 "time" 28 ) 29 30 var nodeDBKeyTests = []struct { 31 id NodeID 32 field string 33 key []byte 34 }{ 35 { 36 id: NodeID{}, 37 field: "version", 38 key: []byte{0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e}, // field 39 }, 40 { 41 id: MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 42 field: ":discover", 43 key: []byte{0x6e, 0x3a, // prefix 44 0x1d, 0xd9, 0xd6, 0x5c, 0x45, 0x52, 0xb5, 0xeb, // node id 45 0x43, 0xd5, 0xad, 0x55, 0xa2, 0xee, 0x3f, 0x56, // 46 0xc6, 0xcb, 0xc1, 0xc6, 0x4a, 0x5c, 0x8d, 0x65, // 47 0x9f, 0x51, 0xfc, 0xd5, 0x1b, 0xac, 0xe2, 0x43, // 48 0x51, 0x23, 0x2b, 0x8d, 0x78, 0x21, 0x61, 0x7d, // 49 0x2b, 0x29, 0xb5, 0x4b, 0x81, 0xcd, 0xef, 0xb9, // 50 0xb3, 0xe9, 0xc3, 0x7d, 0x7f, 0xd5, 0xf6, 0x32, // 51 0x70, 0xbc, 0xc9, 0xe1, 0xa6, 0xf6, 0xa4, 0x39, // 52 0x3a, 0x64, 0x69, 0x73, 0x63, 0x6f, 0x76, 0x65, 0x72, // field 53 }, 54 }, 55 } 56 57 func TestNodeDBKeys(t *testing.T) { 58 for i, tt := range nodeDBKeyTests { 59 if key := makeKey(tt.id, tt.field); !bytes.Equal(key, tt.key) { 60 t.Errorf("make test %d: key mismatch: have 0x%x, want 0x%x", i, key, tt.key) 61 } 62 id, field := splitKey(tt.key) 63 if !bytes.Equal(id[:], tt.id[:]) { 64 t.Errorf("split test %d: id mismatch: have 0x%x, want 0x%x", i, id, tt.id) 65 } 66 if field != tt.field { 67 t.Errorf("split test %d: field mismatch: have 0x%x, want 0x%x", i, field, tt.field) 68 } 69 } 70 } 71 72 var nodeDBInt64Tests = []struct { 73 key []byte 74 value int64 75 }{ 76 {key: []byte{0x01}, value: 1}, 77 {key: []byte{0x02}, value: 2}, 78 {key: []byte{0x03}, value: 3}, 79 } 80 81 func TestNodeDBInt64(t *testing.T) { 82 db, _ := newNodeDB("", Version, NodeID{}) 83 defer db.close() 84 85 tests := nodeDBInt64Tests 86 for i := 0; i < len(tests); i++ { 87 // Insert the next value 88 if err := db.storeInt64(tests[i].key, tests[i].value); err != nil { 89 t.Errorf("test %d: failed to store value: %v", i, err) 90 } 91 // Check all existing and non existing values 92 for j := 0; j < len(tests); j++ { 93 num := db.fetchInt64(tests[j].key) 94 switch { 95 case j <= i && num != tests[j].value: 96 t.Errorf("test %d, item %d: value mismatch: have %v, want %v", i, j, num, tests[j].value) 97 case j > i && num != 0: 98 t.Errorf("test %d, item %d: value mismatch: have %v, want %v", i, j, num, 0) 99 } 100 } 101 } 102 } 103 104 func TestNodeDBFetchStore(t *testing.T) { 105 node := NewNode( 106 MustHexID("0x1dd9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 107 net.IP{192, 168, 0, 1}, 108 30303, 109 30303, 110 ) 111 inst := time.Now() 112 num := 314 113 114 db, _ := newNodeDB("", Version, NodeID{}) 115 defer db.close() 116 117 // Check fetch/store operations on a node ping object 118 if stored := db.lastPing(node.ID); stored.Unix() != 0 { 119 t.Errorf("ping: non-existing object: %v", stored) 120 } 121 if err := db.updateLastPing(node.ID, inst); err != nil { 122 t.Errorf("ping: failed to update: %v", err) 123 } 124 if stored := db.lastPing(node.ID); stored.Unix() != inst.Unix() { 125 t.Errorf("ping: value mismatch: have %v, want %v", stored, inst) 126 } 127 // Check fetch/store operations on a node pong object 128 if stored := db.lastPong(node.ID); stored.Unix() != 0 { 129 t.Errorf("pong: non-existing object: %v", stored) 130 } 131 if err := db.updateLastPong(node.ID, inst); err != nil { 132 t.Errorf("pong: failed to update: %v", err) 133 } 134 if stored := db.lastPong(node.ID); stored.Unix() != inst.Unix() { 135 t.Errorf("pong: value mismatch: have %v, want %v", stored, inst) 136 } 137 // Check fetch/store operations on a node findnode-failure object 138 if stored := db.findFails(node.ID); stored != 0 { 139 t.Errorf("find-node fails: non-existing object: %v", stored) 140 } 141 if err := db.updateFindFails(node.ID, num); err != nil { 142 t.Errorf("find-node fails: failed to update: %v", err) 143 } 144 if stored := db.findFails(node.ID); stored != num { 145 t.Errorf("find-node fails: value mismatch: have %v, want %v", stored, num) 146 } 147 // Check fetch/store operations on an actual node object 148 if stored := db.node(node.ID); stored != nil { 149 t.Errorf("node: non-existing object: %v", stored) 150 } 151 if err := db.updateNode(node); err != nil { 152 t.Errorf("node: failed to update: %v", err) 153 } 154 if stored := db.node(node.ID); stored == nil { 155 t.Errorf("node: not found") 156 } else if !reflect.DeepEqual(stored, node) { 157 t.Errorf("node: data mismatch: have %v, want %v", stored, node) 158 } 159 } 160 161 var nodeDBSeedQueryNodes = []struct { 162 node *Node 163 pong time.Time 164 }{ 165 // This one should not be in the result set because its last 166 // pong time is too far in the past. 167 { 168 node: NewNode( 169 MustHexID("0x84d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 170 net.IP{127, 0, 0, 3}, 171 30303, 172 30303, 173 ), 174 pong: time.Now().Add(-3 * time.Hour), 175 }, 176 // This one shouldn't be in the result set because its 177 // nodeID is the local node's ID. 178 { 179 node: NewNode( 180 MustHexID("0x57d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 181 net.IP{127, 0, 0, 3}, 182 30303, 183 30303, 184 ), 185 pong: time.Now().Add(-4 * time.Second), 186 }, 187 188 // These should be in the result set. 189 { 190 node: NewNode( 191 MustHexID("0x22d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 192 net.IP{127, 0, 0, 1}, 193 30303, 194 30303, 195 ), 196 pong: time.Now().Add(-2 * time.Second), 197 }, 198 { 199 node: NewNode( 200 MustHexID("0x44d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 201 net.IP{127, 0, 0, 2}, 202 30303, 203 30303, 204 ), 205 pong: time.Now().Add(-3 * time.Second), 206 }, 207 { 208 node: NewNode( 209 MustHexID("0xe2d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 210 net.IP{127, 0, 0, 3}, 211 30303, 212 30303, 213 ), 214 pong: time.Now().Add(-1 * time.Second), 215 }, 216 } 217 218 func TestNodeDBSeedQuery(t *testing.T) { 219 db, _ := newNodeDB("", Version, nodeDBSeedQueryNodes[1].node.ID) 220 defer db.close() 221 222 // Insert a batch of nodes for querying 223 for i, seed := range nodeDBSeedQueryNodes { 224 if err := db.updateNode(seed.node); err != nil { 225 t.Fatalf("node %d: failed to insert: %v", i, err) 226 } 227 if err := db.updateLastPong(seed.node.ID, seed.pong); err != nil { 228 t.Fatalf("node %d: failed to insert lastPong: %v", i, err) 229 } 230 } 231 232 // Retrieve the entire batch and check for duplicates 233 seeds := db.querySeeds(len(nodeDBSeedQueryNodes)*2, time.Hour) 234 have := make(map[NodeID]struct{}) 235 for _, seed := range seeds { 236 have[seed.ID] = struct{}{} 237 } 238 want := make(map[NodeID]struct{}) 239 for _, seed := range nodeDBSeedQueryNodes[2:] { 240 want[seed.node.ID] = struct{}{} 241 } 242 if len(seeds) != len(want) { 243 t.Errorf("seed count mismatch: have %v, want %v", len(seeds), len(want)) 244 } 245 for id := range have { 246 if _, ok := want[id]; !ok { 247 t.Errorf("extra seed: %v", id) 248 } 249 } 250 for id := range want { 251 if _, ok := have[id]; !ok { 252 t.Errorf("missing seed: %v", id) 253 } 254 } 255 } 256 257 func TestNodeDBPersistency(t *testing.T) { 258 root, err := ioutil.TempDir("", "nodedb-") 259 if err != nil { 260 t.Fatalf("failed to create temporary data folder: %v", err) 261 } 262 defer os.RemoveAll(root) 263 264 var ( 265 testKey = []byte("somekey") 266 testInt = int64(314) 267 ) 268 269 // Create a persistent database and store some values 270 db, err := newNodeDB(filepath.Join(root, "database"), Version, NodeID{}) 271 if err != nil { 272 t.Fatalf("failed to create persistent database: %v", err) 273 } 274 if err := db.storeInt64(testKey, testInt); err != nil { 275 t.Fatalf("failed to store value: %v.", err) 276 } 277 db.close() 278 279 // Reopen the database and check the value 280 db, err = newNodeDB(filepath.Join(root, "database"), Version, NodeID{}) 281 if err != nil { 282 t.Fatalf("failed to open persistent database: %v", err) 283 } 284 if val := db.fetchInt64(testKey); val != testInt { 285 t.Fatalf("value mismatch: have %v, want %v", val, testInt) 286 } 287 db.close() 288 289 // Change the database version and check flush 290 db, err = newNodeDB(filepath.Join(root, "database"), Version+1, NodeID{}) 291 if err != nil { 292 t.Fatalf("failed to open persistent database: %v", err) 293 } 294 if val := db.fetchInt64(testKey); val != 0 { 295 t.Fatalf("value mismatch: have %v, want %v", val, 0) 296 } 297 db.close() 298 } 299 300 var nodeDBExpirationNodes = []struct { 301 node *Node 302 pong time.Time 303 exp bool 304 }{ 305 { 306 node: NewNode( 307 MustHexID("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 308 net.IP{127, 0, 0, 1}, 309 30303, 310 30303, 311 ), 312 pong: time.Now().Add(-nodeDBNodeExpiration + time.Minute), 313 exp: false, 314 }, { 315 node: NewNode( 316 MustHexID("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439"), 317 net.IP{127, 0, 0, 2}, 318 30303, 319 30303, 320 ), 321 pong: time.Now().Add(-nodeDBNodeExpiration - time.Minute), 322 exp: true, 323 }, 324 } 325 326 func TestNodeDBExpiration(t *testing.T) { 327 db, _ := newNodeDB("", Version, NodeID{}) 328 defer db.close() 329 330 // Add all the test nodes and set their last pong time 331 for i, seed := range nodeDBExpirationNodes { 332 if err := db.updateNode(seed.node); err != nil { 333 t.Fatalf("node %d: failed to insert: %v", i, err) 334 } 335 if err := db.updateLastPong(seed.node.ID, seed.pong); err != nil { 336 t.Fatalf("node %d: failed to update pong: %v", i, err) 337 } 338 } 339 // Expire some of them, and check the rest 340 if err := db.expireNodes(); err != nil { 341 t.Fatalf("failed to expire nodes: %v", err) 342 } 343 for i, seed := range nodeDBExpirationNodes { 344 node := db.node(seed.node.ID) 345 if (node == nil && !seed.exp) || (node != nil && seed.exp) { 346 t.Errorf("node %d: expiration mismatch: have %v, want %v", i, node, seed.exp) 347 } 348 } 349 } 350 351 func TestNodeDBSelfExpiration(t *testing.T) { 352 // Find a node in the tests that shouldn't expire, and assign it as self 353 var self NodeID 354 for _, node := range nodeDBExpirationNodes { 355 if !node.exp { 356 self = node.node.ID 357 break 358 } 359 } 360 db, _ := newNodeDB("", Version, self) 361 defer db.close() 362 363 // Add all the test nodes and set their last pong time 364 for i, seed := range nodeDBExpirationNodes { 365 if err := db.updateNode(seed.node); err != nil { 366 t.Fatalf("node %d: failed to insert: %v", i, err) 367 } 368 if err := db.updateLastPong(seed.node.ID, seed.pong); err != nil { 369 t.Fatalf("node %d: failed to update pong: %v", i, err) 370 } 371 } 372 // Expire the nodes and make sure self has been evacuated too 373 if err := db.expireNodes(); err != nil { 374 t.Fatalf("failed to expire nodes: %v", err) 375 } 376 node := db.node(self) 377 if node != nil { 378 t.Errorf("self not evacuated") 379 } 380 }