accessors_chain_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. "bytes"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/crypto/sha3"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // Tests block header storage and retrieval operations.
  28. func TestHeaderStorage(t *testing.T) {
  29. db := ethdb.NewMemDatabase()
  30. // Create a test header to move around the database and make sure it's really new
  31. header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
  32. if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
  33. t.Fatalf("Non existent header returned: %v", entry)
  34. }
  35. // Write and verify the header in the database
  36. WriteHeader(db, header)
  37. if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
  38. t.Fatalf("Stored header not found")
  39. } else if entry.Hash() != header.Hash() {
  40. t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
  41. }
  42. if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
  43. t.Fatalf("Stored header RLP not found")
  44. } else {
  45. hasher := sha3.NewKeccak256()
  46. hasher.Write(entry)
  47. if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
  48. t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
  49. }
  50. }
  51. // Delete the header and verify the execution
  52. DeleteHeader(db, header.Hash(), header.Number.Uint64())
  53. if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
  54. t.Fatalf("Deleted header returned: %v", entry)
  55. }
  56. }
  57. // Tests block body storage and retrieval operations.
  58. func TestBodyStorage(t *testing.T) {
  59. db := ethdb.NewMemDatabase()
  60. // Create a test body to move around the database and make sure it's really new
  61. body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
  62. hasher := sha3.NewKeccak256()
  63. rlp.Encode(hasher, body)
  64. hash := common.BytesToHash(hasher.Sum(nil))
  65. if entry := ReadBody(db, hash, 0); entry != nil {
  66. t.Fatalf("Non existent body returned: %v", entry)
  67. }
  68. // Write and verify the body in the database
  69. WriteBody(db, hash, 0, body)
  70. if entry := ReadBody(db, hash, 0); entry == nil {
  71. t.Fatalf("Stored body not found")
  72. } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
  73. t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
  74. }
  75. if entry := ReadBodyRLP(db, hash, 0); entry == nil {
  76. t.Fatalf("Stored body RLP not found")
  77. } else {
  78. hasher := sha3.NewKeccak256()
  79. hasher.Write(entry)
  80. if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
  81. t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
  82. }
  83. }
  84. // Delete the body and verify the execution
  85. DeleteBody(db, hash, 0)
  86. if entry := ReadBody(db, hash, 0); entry != nil {
  87. t.Fatalf("Deleted body returned: %v", entry)
  88. }
  89. }
  90. // Tests block storage and retrieval operations.
  91. func TestBlockStorage(t *testing.T) {
  92. db := ethdb.NewMemDatabase()
  93. // Create a test block to move around the database and make sure it's really new
  94. block := types.NewBlockWithHeader(&types.Header{
  95. Extra: []byte("test block"),
  96. UncleHash: types.EmptyUncleHash,
  97. TxHash: types.EmptyRootHash,
  98. ReceiptHash: types.EmptyRootHash,
  99. })
  100. if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
  101. t.Fatalf("Non existent block returned: %v", entry)
  102. }
  103. if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
  104. t.Fatalf("Non existent header returned: %v", entry)
  105. }
  106. if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
  107. t.Fatalf("Non existent body returned: %v", entry)
  108. }
  109. // Write and verify the block in the database
  110. WriteBlock(db, block)
  111. if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
  112. t.Fatalf("Stored block not found")
  113. } else if entry.Hash() != block.Hash() {
  114. t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
  115. }
  116. if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil {
  117. t.Fatalf("Stored header not found")
  118. } else if entry.Hash() != block.Header().Hash() {
  119. t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
  120. }
  121. if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
  122. t.Fatalf("Stored body not found")
  123. } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
  124. t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
  125. }
  126. // Delete the block and verify the execution
  127. DeleteBlock(db, block.Hash(), block.NumberU64())
  128. if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
  129. t.Fatalf("Deleted block returned: %v", entry)
  130. }
  131. if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
  132. t.Fatalf("Deleted header returned: %v", entry)
  133. }
  134. if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
  135. t.Fatalf("Deleted body returned: %v", entry)
  136. }
  137. }
  138. // Tests that partial block contents don't get reassembled into full blocks.
  139. func TestPartialBlockStorage(t *testing.T) {
  140. db := ethdb.NewMemDatabase()
  141. block := types.NewBlockWithHeader(&types.Header{
  142. Extra: []byte("test block"),
  143. UncleHash: types.EmptyUncleHash,
  144. TxHash: types.EmptyRootHash,
  145. ReceiptHash: types.EmptyRootHash,
  146. })
  147. // Store a header and check that it's not recognized as a block
  148. WriteHeader(db, block.Header())
  149. if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
  150. t.Fatalf("Non existent block returned: %v", entry)
  151. }
  152. DeleteHeader(db, block.Hash(), block.NumberU64())
  153. // Store a body and check that it's not recognized as a block
  154. WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
  155. if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
  156. t.Fatalf("Non existent block returned: %v", entry)
  157. }
  158. DeleteBody(db, block.Hash(), block.NumberU64())
  159. // Store a header and a body separately and check reassembly
  160. WriteHeader(db, block.Header())
  161. WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
  162. if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
  163. t.Fatalf("Stored block not found")
  164. } else if entry.Hash() != block.Hash() {
  165. t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
  166. }
  167. }
  168. // Tests block total difficulty storage and retrieval operations.
  169. func TestTdStorage(t *testing.T) {
  170. db := ethdb.NewMemDatabase()
  171. // Create a test TD to move around the database and make sure it's really new
  172. hash, td := common.Hash{}, big.NewInt(314)
  173. if entry := ReadTd(db, hash, 0); entry != nil {
  174. t.Fatalf("Non existent TD returned: %v", entry)
  175. }
  176. // Write and verify the TD in the database
  177. WriteTd(db, hash, 0, td)
  178. if entry := ReadTd(db, hash, 0); entry == nil {
  179. t.Fatalf("Stored TD not found")
  180. } else if entry.Cmp(td) != 0 {
  181. t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
  182. }
  183. // Delete the TD and verify the execution
  184. DeleteTd(db, hash, 0)
  185. if entry := ReadTd(db, hash, 0); entry != nil {
  186. t.Fatalf("Deleted TD returned: %v", entry)
  187. }
  188. }
  189. // Tests that canonical numbers can be mapped to hashes and retrieved.
  190. func TestCanonicalMappingStorage(t *testing.T) {
  191. db := ethdb.NewMemDatabase()
  192. // Create a test canonical number and assinged hash to move around
  193. hash, number := common.Hash{0: 0xff}, uint64(314)
  194. if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
  195. t.Fatalf("Non existent canonical mapping returned: %v", entry)
  196. }
  197. // Write and verify the TD in the database
  198. WriteCanonicalHash(db, hash, number)
  199. if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) {
  200. t.Fatalf("Stored canonical mapping not found")
  201. } else if entry != hash {
  202. t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
  203. }
  204. // Delete the TD and verify the execution
  205. DeleteCanonicalHash(db, number)
  206. if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
  207. t.Fatalf("Deleted canonical mapping returned: %v", entry)
  208. }
  209. }
  210. // Tests that head headers and head blocks can be assigned, individually.
  211. func TestHeadStorage(t *testing.T) {
  212. db := ethdb.NewMemDatabase()
  213. blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
  214. blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
  215. blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
  216. // Check that no head entries are in a pristine database
  217. if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) {
  218. t.Fatalf("Non head header entry returned: %v", entry)
  219. }
  220. if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) {
  221. t.Fatalf("Non head block entry returned: %v", entry)
  222. }
  223. if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) {
  224. t.Fatalf("Non fast head block entry returned: %v", entry)
  225. }
  226. // Assign separate entries for the head header and block
  227. WriteHeadHeaderHash(db, blockHead.Hash())
  228. WriteHeadBlockHash(db, blockFull.Hash())
  229. WriteHeadFastBlockHash(db, blockFast.Hash())
  230. // Check that both heads are present, and different (i.e. two heads maintained)
  231. if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() {
  232. t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
  233. }
  234. if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() {
  235. t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
  236. }
  237. if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() {
  238. t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
  239. }
  240. }
  241. // Tests that receipts associated with a single block can be stored and retrieved.
  242. func TestBlockReceiptStorage(t *testing.T) {
  243. db := ethdb.NewMemDatabase()
  244. receipt1 := &types.Receipt{
  245. Status: types.ReceiptStatusFailed,
  246. CumulativeGasUsed: 1,
  247. Logs: []*types.Log{
  248. {Address: common.BytesToAddress([]byte{0x11})},
  249. {Address: common.BytesToAddress([]byte{0x01, 0x11})},
  250. },
  251. TxHash: common.BytesToHash([]byte{0x11, 0x11}),
  252. ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
  253. GasUsed: 111111,
  254. }
  255. receipt2 := &types.Receipt{
  256. PostState: common.Hash{2}.Bytes(),
  257. CumulativeGasUsed: 2,
  258. Logs: []*types.Log{
  259. {Address: common.BytesToAddress([]byte{0x22})},
  260. {Address: common.BytesToAddress([]byte{0x02, 0x22})},
  261. },
  262. TxHash: common.BytesToHash([]byte{0x22, 0x22}),
  263. ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
  264. GasUsed: 222222,
  265. }
  266. receipts := []*types.Receipt{receipt1, receipt2}
  267. // Check that no receipt entries are in a pristine database
  268. hash := common.BytesToHash([]byte{0x03, 0x14})
  269. if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
  270. t.Fatalf("non existent receipts returned: %v", rs)
  271. }
  272. // Insert the receipt slice into the database and check presence
  273. WriteReceipts(db, hash, 0, receipts)
  274. if rs := ReadReceipts(db, hash, 0); len(rs) == 0 {
  275. t.Fatalf("no receipts returned")
  276. } else {
  277. for i := 0; i < len(receipts); i++ {
  278. rlpHave, _ := rlp.EncodeToBytes(rs[i])
  279. rlpWant, _ := rlp.EncodeToBytes(receipts[i])
  280. if !bytes.Equal(rlpHave, rlpWant) {
  281. t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
  282. }
  283. }
  284. }
  285. // Delete the receipt slice and check purge
  286. DeleteReceipts(db, hash, 0)
  287. if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
  288. t.Fatalf("deleted receipts returned: %v", rs)
  289. }
  290. }