database_util_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 core
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "io/ioutil"
  21. "math/big"
  22. "os"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/core/vm"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/crypto/sha3"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/rlp"
  31. )
  32. type diffTest struct {
  33. ParentTimestamp uint64
  34. ParentDifficulty *big.Int
  35. CurrentTimestamp uint64
  36. CurrentBlocknumber *big.Int
  37. CurrentDifficulty *big.Int
  38. }
  39. func (d *diffTest) UnmarshalJSON(b []byte) (err error) {
  40. var ext struct {
  41. ParentTimestamp string
  42. ParentDifficulty string
  43. CurrentTimestamp string
  44. CurrentBlocknumber string
  45. CurrentDifficulty string
  46. }
  47. if err := json.Unmarshal(b, &ext); err != nil {
  48. return err
  49. }
  50. d.ParentTimestamp = common.String2Big(ext.ParentTimestamp).Uint64()
  51. d.ParentDifficulty = common.String2Big(ext.ParentDifficulty)
  52. d.CurrentTimestamp = common.String2Big(ext.CurrentTimestamp).Uint64()
  53. d.CurrentBlocknumber = common.String2Big(ext.CurrentBlocknumber)
  54. d.CurrentDifficulty = common.String2Big(ext.CurrentDifficulty)
  55. return nil
  56. }
  57. func TestDifficultyFrontier(t *testing.T) {
  58. file, err := os.Open("../tests/files/BasicTests/difficulty.json")
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. defer file.Close()
  63. tests := make(map[string]diffTest)
  64. err = json.NewDecoder(file).Decode(&tests)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. for name, test := range tests {
  69. number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1))
  70. diff := calcDifficultyFrontier(test.CurrentTimestamp, test.ParentTimestamp, number, test.ParentDifficulty)
  71. if diff.Cmp(test.CurrentDifficulty) != 0 {
  72. t.Error(name, "failed. Expected", test.CurrentDifficulty, "and calculated", diff)
  73. }
  74. }
  75. }
  76. // Tests block header storage and retrieval operations.
  77. func TestHeaderStorage(t *testing.T) {
  78. db, _ := ethdb.NewMemDatabase()
  79. // Create a test header to move around the database and make sure it's really new
  80. header := &types.Header{Extra: []byte("test header")}
  81. if entry := GetHeader(db, header.Hash()); entry != nil {
  82. t.Fatalf("Non existent header returned: %v", entry)
  83. }
  84. // Write and verify the header in the database
  85. if err := WriteHeader(db, header); err != nil {
  86. t.Fatalf("Failed to write header into database: %v", err)
  87. }
  88. if entry := GetHeader(db, header.Hash()); entry == nil {
  89. t.Fatalf("Stored header not found")
  90. } else if entry.Hash() != header.Hash() {
  91. t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
  92. }
  93. if entry := GetHeaderRLP(db, header.Hash()); entry == nil {
  94. t.Fatalf("Stored header RLP not found")
  95. } else {
  96. hasher := sha3.NewKeccak256()
  97. hasher.Write(entry)
  98. if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
  99. t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
  100. }
  101. }
  102. // Delete the header and verify the execution
  103. DeleteHeader(db, header.Hash())
  104. if entry := GetHeader(db, header.Hash()); entry != nil {
  105. t.Fatalf("Deleted header returned: %v", entry)
  106. }
  107. }
  108. // Tests block body storage and retrieval operations.
  109. func TestBodyStorage(t *testing.T) {
  110. db, _ := ethdb.NewMemDatabase()
  111. // Create a test body to move around the database and make sure it's really new
  112. body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
  113. hasher := sha3.NewKeccak256()
  114. rlp.Encode(hasher, body)
  115. hash := common.BytesToHash(hasher.Sum(nil))
  116. if entry := GetBody(db, hash); entry != nil {
  117. t.Fatalf("Non existent body returned: %v", entry)
  118. }
  119. // Write and verify the body in the database
  120. if err := WriteBody(db, hash, body); err != nil {
  121. t.Fatalf("Failed to write body into database: %v", err)
  122. }
  123. if entry := GetBody(db, hash); entry == nil {
  124. t.Fatalf("Stored body not found")
  125. } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
  126. t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
  127. }
  128. if entry := GetBodyRLP(db, hash); entry == nil {
  129. t.Fatalf("Stored body RLP not found")
  130. } else {
  131. hasher := sha3.NewKeccak256()
  132. hasher.Write(entry)
  133. if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
  134. t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
  135. }
  136. }
  137. // Delete the body and verify the execution
  138. DeleteBody(db, hash)
  139. if entry := GetBody(db, hash); entry != nil {
  140. t.Fatalf("Deleted body returned: %v", entry)
  141. }
  142. }
  143. // Tests block storage and retrieval operations.
  144. func TestBlockStorage(t *testing.T) {
  145. db, _ := ethdb.NewMemDatabase()
  146. // Create a test block to move around the database and make sure it's really new
  147. block := types.NewBlockWithHeader(&types.Header{
  148. Extra: []byte("test block"),
  149. UncleHash: types.EmptyUncleHash,
  150. TxHash: types.EmptyRootHash,
  151. ReceiptHash: types.EmptyRootHash,
  152. })
  153. if entry := GetBlock(db, block.Hash()); entry != nil {
  154. t.Fatalf("Non existent block returned: %v", entry)
  155. }
  156. if entry := GetHeader(db, block.Hash()); entry != nil {
  157. t.Fatalf("Non existent header returned: %v", entry)
  158. }
  159. if entry := GetBody(db, block.Hash()); entry != nil {
  160. t.Fatalf("Non existent body returned: %v", entry)
  161. }
  162. // Write and verify the block in the database
  163. if err := WriteBlock(db, block); err != nil {
  164. t.Fatalf("Failed to write block into database: %v", err)
  165. }
  166. if entry := GetBlock(db, block.Hash()); entry == nil {
  167. t.Fatalf("Stored block not found")
  168. } else if entry.Hash() != block.Hash() {
  169. t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
  170. }
  171. if entry := GetHeader(db, block.Hash()); entry == nil {
  172. t.Fatalf("Stored header not found")
  173. } else if entry.Hash() != block.Header().Hash() {
  174. t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
  175. }
  176. if entry := GetBody(db, block.Hash()); entry == nil {
  177. t.Fatalf("Stored body not found")
  178. } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
  179. t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
  180. }
  181. // Delete the block and verify the execution
  182. DeleteBlock(db, block.Hash())
  183. if entry := GetBlock(db, block.Hash()); entry != nil {
  184. t.Fatalf("Deleted block returned: %v", entry)
  185. }
  186. if entry := GetHeader(db, block.Hash()); entry != nil {
  187. t.Fatalf("Deleted header returned: %v", entry)
  188. }
  189. if entry := GetBody(db, block.Hash()); entry != nil {
  190. t.Fatalf("Deleted body returned: %v", entry)
  191. }
  192. }
  193. // Tests that partial block contents don't get reassembled into full blocks.
  194. func TestPartialBlockStorage(t *testing.T) {
  195. db, _ := ethdb.NewMemDatabase()
  196. block := types.NewBlockWithHeader(&types.Header{
  197. Extra: []byte("test block"),
  198. UncleHash: types.EmptyUncleHash,
  199. TxHash: types.EmptyRootHash,
  200. ReceiptHash: types.EmptyRootHash,
  201. })
  202. // Store a header and check that it's not recognized as a block
  203. if err := WriteHeader(db, block.Header()); err != nil {
  204. t.Fatalf("Failed to write header into database: %v", err)
  205. }
  206. if entry := GetBlock(db, block.Hash()); entry != nil {
  207. t.Fatalf("Non existent block returned: %v", entry)
  208. }
  209. DeleteHeader(db, block.Hash())
  210. // Store a body and check that it's not recognized as a block
  211. if err := WriteBody(db, block.Hash(), block.Body()); err != nil {
  212. t.Fatalf("Failed to write body into database: %v", err)
  213. }
  214. if entry := GetBlock(db, block.Hash()); entry != nil {
  215. t.Fatalf("Non existent block returned: %v", entry)
  216. }
  217. DeleteBody(db, block.Hash())
  218. // Store a header and a body separately and check reassembly
  219. if err := WriteHeader(db, block.Header()); err != nil {
  220. t.Fatalf("Failed to write header into database: %v", err)
  221. }
  222. if err := WriteBody(db, block.Hash(), block.Body()); err != nil {
  223. t.Fatalf("Failed to write body into database: %v", err)
  224. }
  225. if entry := GetBlock(db, block.Hash()); entry == nil {
  226. t.Fatalf("Stored block not found")
  227. } else if entry.Hash() != block.Hash() {
  228. t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
  229. }
  230. }
  231. // Tests block total difficulty storage and retrieval operations.
  232. func TestTdStorage(t *testing.T) {
  233. db, _ := ethdb.NewMemDatabase()
  234. // Create a test TD to move around the database and make sure it's really new
  235. hash, td := common.Hash{}, big.NewInt(314)
  236. if entry := GetTd(db, hash); entry != nil {
  237. t.Fatalf("Non existent TD returned: %v", entry)
  238. }
  239. // Write and verify the TD in the database
  240. if err := WriteTd(db, hash, td); err != nil {
  241. t.Fatalf("Failed to write TD into database: %v", err)
  242. }
  243. if entry := GetTd(db, hash); entry == nil {
  244. t.Fatalf("Stored TD not found")
  245. } else if entry.Cmp(td) != 0 {
  246. t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
  247. }
  248. // Delete the TD and verify the execution
  249. DeleteTd(db, hash)
  250. if entry := GetTd(db, hash); entry != nil {
  251. t.Fatalf("Deleted TD returned: %v", entry)
  252. }
  253. }
  254. // Tests that canonical numbers can be mapped to hashes and retrieved.
  255. func TestCanonicalMappingStorage(t *testing.T) {
  256. db, _ := ethdb.NewMemDatabase()
  257. // Create a test canonical number and assinged hash to move around
  258. hash, number := common.Hash{0: 0xff}, uint64(314)
  259. if entry := GetCanonicalHash(db, number); entry != (common.Hash{}) {
  260. t.Fatalf("Non existent canonical mapping returned: %v", entry)
  261. }
  262. // Write and verify the TD in the database
  263. if err := WriteCanonicalHash(db, hash, number); err != nil {
  264. t.Fatalf("Failed to write canonical mapping into database: %v", err)
  265. }
  266. if entry := GetCanonicalHash(db, number); entry == (common.Hash{}) {
  267. t.Fatalf("Stored canonical mapping not found")
  268. } else if entry != hash {
  269. t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
  270. }
  271. // Delete the TD and verify the execution
  272. DeleteCanonicalHash(db, number)
  273. if entry := GetCanonicalHash(db, number); entry != (common.Hash{}) {
  274. t.Fatalf("Deleted canonical mapping returned: %v", entry)
  275. }
  276. }
  277. // Tests that head headers and head blocks can be assigned, individually.
  278. func TestHeadStorage(t *testing.T) {
  279. db, _ := ethdb.NewMemDatabase()
  280. blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
  281. blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
  282. blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
  283. // Check that no head entries are in a pristine database
  284. if entry := GetHeadHeaderHash(db); entry != (common.Hash{}) {
  285. t.Fatalf("Non head header entry returned: %v", entry)
  286. }
  287. if entry := GetHeadBlockHash(db); entry != (common.Hash{}) {
  288. t.Fatalf("Non head block entry returned: %v", entry)
  289. }
  290. if entry := GetHeadFastBlockHash(db); entry != (common.Hash{}) {
  291. t.Fatalf("Non fast head block entry returned: %v", entry)
  292. }
  293. // Assign separate entries for the head header and block
  294. if err := WriteHeadHeaderHash(db, blockHead.Hash()); err != nil {
  295. t.Fatalf("Failed to write head header hash: %v", err)
  296. }
  297. if err := WriteHeadBlockHash(db, blockFull.Hash()); err != nil {
  298. t.Fatalf("Failed to write head block hash: %v", err)
  299. }
  300. if err := WriteHeadFastBlockHash(db, blockFast.Hash()); err != nil {
  301. t.Fatalf("Failed to write fast head block hash: %v", err)
  302. }
  303. // Check that both heads are present, and different (i.e. two heads maintained)
  304. if entry := GetHeadHeaderHash(db); entry != blockHead.Hash() {
  305. t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
  306. }
  307. if entry := GetHeadBlockHash(db); entry != blockFull.Hash() {
  308. t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
  309. }
  310. if entry := GetHeadFastBlockHash(db); entry != blockFast.Hash() {
  311. t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
  312. }
  313. }
  314. // Tests that transactions and associated metadata can be stored and retrieved.
  315. func TestTransactionStorage(t *testing.T) {
  316. db, _ := ethdb.NewMemDatabase()
  317. tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), big.NewInt(1111), big.NewInt(11111), []byte{0x11, 0x11, 0x11})
  318. tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), big.NewInt(2222), big.NewInt(22222), []byte{0x22, 0x22, 0x22})
  319. tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), big.NewInt(3333), big.NewInt(33333), []byte{0x33, 0x33, 0x33})
  320. txs := []*types.Transaction{tx1, tx2, tx3}
  321. block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil)
  322. // Check that no transactions entries are in a pristine database
  323. for i, tx := range txs {
  324. if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
  325. t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn)
  326. }
  327. }
  328. // Insert all the transactions into the database, and verify contents
  329. if err := WriteTransactions(db, block); err != nil {
  330. t.Fatalf("failed to write transactions: %v", err)
  331. }
  332. for i, tx := range txs {
  333. if txn, hash, number, index := GetTransaction(db, tx.Hash()); txn == nil {
  334. t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
  335. } else {
  336. if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
  337. t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i)
  338. }
  339. if tx.String() != txn.String() {
  340. t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx)
  341. }
  342. }
  343. }
  344. // Delete the transactions and check purge
  345. for i, tx := range txs {
  346. DeleteTransaction(db, tx.Hash())
  347. if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
  348. t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
  349. }
  350. }
  351. }
  352. // Tests that receipts can be stored and retrieved.
  353. func TestReceiptStorage(t *testing.T) {
  354. db, _ := ethdb.NewMemDatabase()
  355. receipt1 := &types.Receipt{
  356. PostState: []byte{0x01},
  357. CumulativeGasUsed: big.NewInt(1),
  358. Logs: vm.Logs{
  359. &vm.Log{Address: common.BytesToAddress([]byte{0x11})},
  360. &vm.Log{Address: common.BytesToAddress([]byte{0x01, 0x11})},
  361. },
  362. TxHash: common.BytesToHash([]byte{0x11, 0x11}),
  363. ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
  364. GasUsed: big.NewInt(111111),
  365. }
  366. receipt2 := &types.Receipt{
  367. PostState: []byte{0x02},
  368. CumulativeGasUsed: big.NewInt(2),
  369. Logs: vm.Logs{
  370. &vm.Log{Address: common.BytesToAddress([]byte{0x22})},
  371. &vm.Log{Address: common.BytesToAddress([]byte{0x02, 0x22})},
  372. },
  373. TxHash: common.BytesToHash([]byte{0x22, 0x22}),
  374. ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
  375. GasUsed: big.NewInt(222222),
  376. }
  377. receipts := []*types.Receipt{receipt1, receipt2}
  378. // Check that no receipt entries are in a pristine database
  379. for i, receipt := range receipts {
  380. if r := GetReceipt(db, receipt.TxHash); r != nil {
  381. t.Fatalf("receipt #%d [%x]: non existent receipt returned: %v", i, receipt.TxHash, r)
  382. }
  383. }
  384. // Insert all the receipts into the database, and verify contents
  385. if err := WriteReceipts(db, receipts); err != nil {
  386. t.Fatalf("failed to write receipts: %v", err)
  387. }
  388. for i, receipt := range receipts {
  389. if r := GetReceipt(db, receipt.TxHash); r == nil {
  390. t.Fatalf("receipt #%d [%x]: receipt not found", i, receipt.TxHash)
  391. } else {
  392. rlpHave, _ := rlp.EncodeToBytes(r)
  393. rlpWant, _ := rlp.EncodeToBytes(receipt)
  394. if bytes.Compare(rlpHave, rlpWant) != 0 {
  395. t.Fatalf("receipt #%d [%x]: receipt mismatch: have %v, want %v", i, receipt.TxHash, r, receipt)
  396. }
  397. }
  398. }
  399. // Delete the receipts and check purge
  400. for i, receipt := range receipts {
  401. DeleteReceipt(db, receipt.TxHash)
  402. if r := GetReceipt(db, receipt.TxHash); r != nil {
  403. t.Fatalf("receipt #%d [%x]: deleted receipt returned: %v", i, receipt.TxHash, r)
  404. }
  405. }
  406. }
  407. // Tests that receipts associated with a single block can be stored and retrieved.
  408. func TestBlockReceiptStorage(t *testing.T) {
  409. db, _ := ethdb.NewMemDatabase()
  410. receipt1 := &types.Receipt{
  411. PostState: []byte{0x01},
  412. CumulativeGasUsed: big.NewInt(1),
  413. Logs: vm.Logs{
  414. &vm.Log{Address: common.BytesToAddress([]byte{0x11})},
  415. &vm.Log{Address: common.BytesToAddress([]byte{0x01, 0x11})},
  416. },
  417. TxHash: common.BytesToHash([]byte{0x11, 0x11}),
  418. ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
  419. GasUsed: big.NewInt(111111),
  420. }
  421. receipt2 := &types.Receipt{
  422. PostState: []byte{0x02},
  423. CumulativeGasUsed: big.NewInt(2),
  424. Logs: vm.Logs{
  425. &vm.Log{Address: common.BytesToAddress([]byte{0x22})},
  426. &vm.Log{Address: common.BytesToAddress([]byte{0x02, 0x22})},
  427. },
  428. TxHash: common.BytesToHash([]byte{0x22, 0x22}),
  429. ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
  430. GasUsed: big.NewInt(222222),
  431. }
  432. receipts := []*types.Receipt{receipt1, receipt2}
  433. // Check that no receipt entries are in a pristine database
  434. hash := common.BytesToHash([]byte{0x03, 0x14})
  435. if rs := GetBlockReceipts(db, hash); len(rs) != 0 {
  436. t.Fatalf("non existent receipts returned: %v", rs)
  437. }
  438. // Insert the receipt slice into the database and check presence
  439. if err := WriteBlockReceipts(db, hash, receipts); err != nil {
  440. t.Fatalf("failed to write block receipts: %v", err)
  441. }
  442. if rs := GetBlockReceipts(db, hash); len(rs) == 0 {
  443. t.Fatalf("no receipts returned")
  444. } else {
  445. for i := 0; i < len(receipts); i++ {
  446. rlpHave, _ := rlp.EncodeToBytes(rs[i])
  447. rlpWant, _ := rlp.EncodeToBytes(receipts[i])
  448. if bytes.Compare(rlpHave, rlpWant) != 0 {
  449. t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
  450. }
  451. }
  452. }
  453. // Delete the receipt slice and check purge
  454. DeleteBlockReceipts(db, hash)
  455. if rs := GetBlockReceipts(db, hash); len(rs) != 0 {
  456. t.Fatalf("deleted receipts returned: %v", rs)
  457. }
  458. }
  459. func TestMipmapBloom(t *testing.T) {
  460. db, _ := ethdb.NewMemDatabase()
  461. receipt1 := new(types.Receipt)
  462. receipt1.Logs = vm.Logs{
  463. &vm.Log{Address: common.BytesToAddress([]byte("test"))},
  464. &vm.Log{Address: common.BytesToAddress([]byte("address"))},
  465. }
  466. receipt2 := new(types.Receipt)
  467. receipt2.Logs = vm.Logs{
  468. &vm.Log{Address: common.BytesToAddress([]byte("test"))},
  469. &vm.Log{Address: common.BytesToAddress([]byte("address1"))},
  470. }
  471. WriteMipmapBloom(db, 1, types.Receipts{receipt1})
  472. WriteMipmapBloom(db, 2, types.Receipts{receipt2})
  473. for _, level := range MIPMapLevels {
  474. bloom := GetMipmapBloom(db, 2, level)
  475. if !bloom.Test(new(big.Int).SetBytes([]byte("address1"))) {
  476. t.Error("expected test to be included on level:", level)
  477. }
  478. }
  479. // reset
  480. db, _ = ethdb.NewMemDatabase()
  481. receipt := new(types.Receipt)
  482. receipt.Logs = vm.Logs{
  483. &vm.Log{Address: common.BytesToAddress([]byte("test"))},
  484. }
  485. WriteMipmapBloom(db, 999, types.Receipts{receipt1})
  486. receipt = new(types.Receipt)
  487. receipt.Logs = vm.Logs{
  488. &vm.Log{Address: common.BytesToAddress([]byte("test 1"))},
  489. }
  490. WriteMipmapBloom(db, 1000, types.Receipts{receipt})
  491. bloom := GetMipmapBloom(db, 1000, 1000)
  492. if bloom.TestBytes([]byte("test")) {
  493. t.Error("test should not have been included")
  494. }
  495. }
  496. func TestMipmapChain(t *testing.T) {
  497. dir, err := ioutil.TempDir("", "mipmap")
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. defer os.RemoveAll(dir)
  502. var (
  503. db, _ = ethdb.NewLDBDatabase(dir, 0, 0)
  504. key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  505. addr = crypto.PubkeyToAddress(key1.PublicKey)
  506. addr2 = common.BytesToAddress([]byte("jeff"))
  507. hash1 = common.BytesToHash([]byte("topic1"))
  508. )
  509. defer db.Close()
  510. genesis := WriteGenesisBlockForTesting(db, GenesisAccount{addr, big.NewInt(1000000)})
  511. chain, receipts := GenerateChain(genesis, db, 1010, func(i int, gen *BlockGen) {
  512. var receipts types.Receipts
  513. switch i {
  514. case 1:
  515. receipt := types.NewReceipt(nil, new(big.Int))
  516. receipt.Logs = vm.Logs{
  517. &vm.Log{
  518. Address: addr,
  519. Topics: []common.Hash{hash1},
  520. },
  521. }
  522. gen.AddUncheckedReceipt(receipt)
  523. receipts = types.Receipts{receipt}
  524. case 1000:
  525. receipt := types.NewReceipt(nil, new(big.Int))
  526. receipt.Logs = vm.Logs{&vm.Log{Address: addr2}}
  527. gen.AddUncheckedReceipt(receipt)
  528. receipts = types.Receipts{receipt}
  529. }
  530. // store the receipts
  531. err := WriteReceipts(db, receipts)
  532. if err != nil {
  533. t.Fatal(err)
  534. }
  535. WriteMipmapBloom(db, uint64(i+1), receipts)
  536. })
  537. for i, block := range chain {
  538. WriteBlock(db, block)
  539. if err := WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil {
  540. t.Fatalf("failed to insert block number: %v", err)
  541. }
  542. if err := WriteHeadBlockHash(db, block.Hash()); err != nil {
  543. t.Fatalf("failed to insert block number: %v", err)
  544. }
  545. if err := WriteBlockReceipts(db, block.Hash(), receipts[i]); err != nil {
  546. t.Fatal("error writing block receipts:", err)
  547. }
  548. }
  549. bloom := GetMipmapBloom(db, 0, 1000)
  550. if bloom.TestBytes(addr2[:]) {
  551. t.Error("address was included in bloom and should not have")
  552. }
  553. }