accessors_indexes_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. package rawdb
  17. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Tests that positional lookup metadata can be stored and retrieved.
  26. func TestLookupStorage(t *testing.T) {
  27. tests := []struct {
  28. name string
  29. writeTxLookupEntries func(ethdb.Writer, *types.Block)
  30. }{
  31. {
  32. "DatabaseV6",
  33. func(db ethdb.Writer, block *types.Block) {
  34. WriteTxLookupEntries(db, block)
  35. },
  36. },
  37. {
  38. "DatabaseV4-V5",
  39. func(db ethdb.Writer, block *types.Block) {
  40. for _, tx := range block.Transactions() {
  41. db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes())
  42. }
  43. },
  44. },
  45. {
  46. "DatabaseV3",
  47. func(db ethdb.Writer, block *types.Block) {
  48. for index, tx := range block.Transactions() {
  49. entry := LegacyTxLookupEntry{
  50. BlockHash: block.Hash(),
  51. BlockIndex: block.NumberU64(),
  52. Index: uint64(index),
  53. }
  54. data, _ := rlp.EncodeToBytes(entry)
  55. db.Put(txLookupKey(tx.Hash()), data)
  56. }
  57. },
  58. },
  59. }
  60. for _, tc := range tests {
  61. t.Run(tc.name, func(t *testing.T) {
  62. db := NewMemoryDatabase()
  63. tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11})
  64. tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22})
  65. tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33})
  66. txs := []*types.Transaction{tx1, tx2, tx3}
  67. block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil)
  68. // Check that no transactions entries are in a pristine database
  69. for i, tx := range txs {
  70. if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil {
  71. t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn)
  72. }
  73. }
  74. // Insert all the transactions into the database, and verify contents
  75. WriteCanonicalHash(db, block.Hash(), block.NumberU64())
  76. WriteBlock(db, block)
  77. tc.writeTxLookupEntries(db, block)
  78. for i, tx := range txs {
  79. if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil {
  80. t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
  81. } else {
  82. if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
  83. 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)
  84. }
  85. if tx.Hash() != txn.Hash() {
  86. t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx)
  87. }
  88. }
  89. }
  90. // Delete the transactions and check purge
  91. for i, tx := range txs {
  92. DeleteTxLookupEntry(db, tx.Hash())
  93. if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil {
  94. t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
  95. }
  96. }
  97. })
  98. }
  99. }