database_test.go 11 KB

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