github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/core/rawdb/accessors_chain_test.go (about) 1 // Copyright 2019 The ebakus/go-ebakus Authors 2 // This file is part of the ebakus/go-ebakus library. 3 // 4 // The ebakus/go-ebakus 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 ebakus/go-ebakus 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 ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rawdb 18 19 import ( 20 "bytes" 21 "encoding/hex" 22 "fmt" 23 "math/big" 24 "testing" 25 26 "github.com/ebakus/go-ebakus-old/ethdb" 27 "github.com/ebakus/go-ebakus/common" 28 "github.com/ebakus/go-ebakus/core/types" 29 "github.com/ebakus/go-ebakus/params" 30 "github.com/ebakus/go-ebakus/rlp" 31 "golang.org/x/crypto/sha3" 32 ) 33 34 // Tests block header storage and retrieval operations. 35 func TestHeaderStorage(t *testing.T) { 36 db := NewMemoryDatabase() 37 38 // Create a test header to move around the database and make sure it's really new 39 header := &types.Header{Number: big.NewInt(42)} 40 if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil { 41 t.Fatalf("Non existent header returned: %v", entry) 42 } 43 // Write and verify the header in the database 44 WriteHeader(db, header) 45 if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil { 46 t.Fatalf("Stored header not found") 47 } else if entry.Hash() != header.Hash() { 48 t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header) 49 } 50 if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil { 51 t.Fatalf("Stored header RLP not found") 52 } else { 53 hasher := sha3.NewLegacyKeccak256() 54 hasher.Write(entry) 55 56 if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() { 57 t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header) 58 } 59 } 60 // Delete the header and verify the execution 61 DeleteHeader(db, header.Hash(), header.Number.Uint64()) 62 if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil { 63 t.Fatalf("Deleted header returned: %v", entry) 64 } 65 } 66 67 // Tests block body storage and retrieval operations. 68 func TestBodyStorage(t *testing.T) { 69 db := NewMemoryDatabase() 70 71 // Create a test body to move around the database and make sure it's really new 72 body := &types.Body{} 73 74 hasher := sha3.NewLegacyKeccak256() 75 rlp.Encode(hasher, body) 76 hash := common.BytesToHash(hasher.Sum(nil)) 77 78 if entry := ReadBody(db, hash, 0); entry != nil { 79 t.Fatalf("Non existent body returned: %v", entry) 80 } 81 // Write and verify the body in the database 82 WriteBody(db, hash, 0, body) 83 if entry := ReadBody(db, hash, 0); entry == nil { 84 t.Fatalf("Stored body not found") 85 } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) { 86 t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body) 87 } 88 if entry := ReadBodyRLP(db, hash, 0); entry == nil { 89 t.Fatalf("Stored body RLP not found") 90 } else { 91 hasher := sha3.NewLegacyKeccak256() 92 hasher.Write(entry) 93 94 if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash { 95 t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body) 96 } 97 } 98 // Delete the body and verify the execution 99 DeleteBody(db, hash, 0) 100 if entry := ReadBody(db, hash, 0); entry != nil { 101 t.Fatalf("Deleted body returned: %v", entry) 102 } 103 } 104 105 // Tests block storage and retrieval operations. 106 func TestBlockStorage(t *testing.T) { 107 db := NewMemoryDatabase() 108 109 // Create a test block to move around the database and make sure it's really new 110 block := types.NewBlockWithHeader(&types.Header{ 111 TxHash: types.EmptyRootHash, 112 ReceiptHash: types.EmptyRootHash, 113 }) 114 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 115 t.Fatalf("Non existent block returned: %v", entry) 116 } 117 if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil { 118 t.Fatalf("Non existent header returned: %v", entry) 119 } 120 if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil { 121 t.Fatalf("Non existent body returned: %v", entry) 122 } 123 // Write and verify the block in the database 124 WriteBlock(db, block) 125 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil { 126 t.Fatalf("Stored block not found") 127 } else if entry.Hash() != block.Hash() { 128 t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block) 129 } 130 if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil { 131 t.Fatalf("Stored header not found") 132 } else if entry.Hash() != block.Header().Hash() { 133 t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header()) 134 } 135 if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil { 136 t.Fatalf("Stored body not found") 137 } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) { 138 t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body()) 139 } 140 // Delete the block and verify the execution 141 DeleteBlock(db, block.Hash(), block.NumberU64()) 142 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 143 t.Fatalf("Deleted block returned: %v", entry) 144 } 145 if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil { 146 t.Fatalf("Deleted header returned: %v", entry) 147 } 148 if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil { 149 t.Fatalf("Deleted body returned: %v", entry) 150 } 151 } 152 153 // Tests that partial block contents don't get reassembled into full blocks. 154 func TestPartialBlockStorage(t *testing.T) { 155 db := NewMemoryDatabase() 156 block := types.NewBlockWithHeader(&types.Header{ 157 TxHash: types.EmptyRootHash, 158 ReceiptHash: types.EmptyRootHash, 159 }) 160 // Store a header and check that it's not recognized as a block 161 WriteHeader(db, block.Header()) 162 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 163 t.Fatalf("Non existent block returned: %v", entry) 164 } 165 DeleteHeader(db, block.Hash(), block.NumberU64()) 166 167 // Store a body and check that it's not recognized as a block 168 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 169 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 170 t.Fatalf("Non existent block returned: %v", entry) 171 } 172 DeleteBody(db, block.Hash(), block.NumberU64()) 173 174 // Store a header and a body separately and check reassembly 175 WriteHeader(db, block.Header()) 176 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 177 178 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil { 179 t.Fatalf("Stored block not found") 180 } else if entry.Hash() != block.Hash() { 181 t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block) 182 } 183 } 184 185 // Tests ebakus state snapshot storage and retrieval operations. 186 func TestSnapshotStorage(t *testing.T) { 187 db := ethdb.NewMemDatabase() 188 189 // Create a test TD to move around the database and make sure it's really new 190 hash, number, snapshot := common.Hash{}, uint64(1), uint64(314) 191 if entry := ReadSnapshot(db, hash, number); entry != nil { 192 t.Fatalf("Non existent Snapshot returned: %v", entry) 193 } 194 // Write and verify the Snapshot in the database 195 WriteSnapshot(db, hash, snapshot) 196 if entry := ReadSnapshot(db, hash, number); entry == nil { 197 t.Fatalf("Stored Snapshot not found") 198 } 199 // Delete the Snapshot and verify the execution 200 DeleteSnapshot(db, hash) 201 if entry := ReadSnapshot(db, hash, number); entry != nil { 202 t.Fatalf("Deleted Snapshot returned: %v", entry) 203 } 204 } 205 206 // Tests that canonical numbers can be mapped to hashes and retrieved. 207 func TestCanonicalMappingStorage(t *testing.T) { 208 db := NewMemoryDatabase() 209 210 // Create a test canonical number and assinged hash to move around 211 hash, number := common.Hash{0: 0xff}, uint64(314) 212 if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) { 213 t.Fatalf("Non existent canonical mapping returned: %v", entry) 214 } 215 // Write and verify the TD in the database 216 WriteCanonicalHash(db, hash, number) 217 if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) { 218 t.Fatalf("Stored canonical mapping not found") 219 } else if entry != hash { 220 t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash) 221 } 222 // Delete the TD and verify the execution 223 DeleteCanonicalHash(db, number) 224 if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) { 225 t.Fatalf("Deleted canonical mapping returned: %v", entry) 226 } 227 } 228 229 // Tests that head headers and head blocks can be assigned, individually. 230 func TestHeadStorage(t *testing.T) { 231 db := NewMemoryDatabase() 232 233 blockHead := types.NewBlockWithHeader(&types.Header{}) 234 blockFull := types.NewBlockWithHeader(&types.Header{}) 235 blockFast := types.NewBlockWithHeader(&types.Header{}) 236 237 // Check that no head entries are in a pristine database 238 if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) { 239 t.Fatalf("Non head header entry returned: %v", entry) 240 } 241 if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) { 242 t.Fatalf("Non head block entry returned: %v", entry) 243 } 244 if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) { 245 t.Fatalf("Non fast head block entry returned: %v", entry) 246 } 247 // Assign separate entries for the head header and block 248 WriteHeadHeaderHash(db, blockHead.Hash()) 249 WriteHeadBlockHash(db, blockFull.Hash()) 250 WriteHeadFastBlockHash(db, blockFast.Hash()) 251 252 // Check that both heads are present, and different (i.e. two heads maintained) 253 if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() { 254 t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash()) 255 } 256 if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() { 257 t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash()) 258 } 259 if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() { 260 t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash()) 261 } 262 } 263 264 // Tests that receipts associated with a single block can be stored and retrieved. 265 func TestBlockReceiptStorage(t *testing.T) { 266 db := NewMemoryDatabase() 267 268 // Create a live block since we need metadata to reconstruct the receipt 269 tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil) 270 tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil) 271 272 body := &types.Body{Transactions: types.Transactions{tx1, tx2}} 273 274 // Create the two receipts to manage afterwards 275 receipt1 := &types.Receipt{ 276 Status: types.ReceiptStatusFailed, 277 CumulativeGasUsed: 1, 278 Logs: []*types.Log{ 279 {Address: common.BytesToAddress([]byte{0x11})}, 280 {Address: common.BytesToAddress([]byte{0x01, 0x11})}, 281 }, 282 TxHash: tx1.Hash(), 283 ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), 284 GasUsed: 111111, 285 } 286 receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1}) 287 288 receipt2 := &types.Receipt{ 289 PostState: common.Hash{2}.Bytes(), 290 CumulativeGasUsed: 2, 291 Logs: []*types.Log{ 292 {Address: common.BytesToAddress([]byte{0x22})}, 293 {Address: common.BytesToAddress([]byte{0x02, 0x22})}, 294 }, 295 TxHash: tx2.Hash(), 296 ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), 297 GasUsed: 222222, 298 } 299 receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2}) 300 receipts := []*types.Receipt{receipt1, receipt2} 301 302 // Check that no receipt entries are in a pristine database 303 hash := common.BytesToHash([]byte{0x03, 0x14}) 304 if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 { 305 t.Fatalf("non existent receipts returned: %v", rs) 306 } 307 // Insert the body that corresponds to the receipts 308 WriteBody(db, hash, 0, body) 309 310 // Insert the receipt slice into the database and check presence 311 WriteReceipts(db, hash, 0, receipts) 312 if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 { 313 t.Fatalf("no receipts returned") 314 } else { 315 if err := checkReceiptsRLP(rs, receipts); err != nil { 316 t.Fatalf(err.Error()) 317 } 318 } 319 // Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed) 320 DeleteBody(db, hash, 0) 321 if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil { 322 t.Fatalf("receipts returned when body was deleted: %v", rs) 323 } 324 // Ensure that receipts without metadata can be returned without the block body too 325 if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil { 326 t.Fatalf(err.Error()) 327 } 328 // Sanity check that body alone without the receipt is a full purge 329 WriteBody(db, hash, 0, body) 330 331 DeleteReceipts(db, hash, 0) 332 if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 { 333 t.Fatalf("deleted receipts returned: %v", rs) 334 } 335 } 336 337 func checkReceiptsRLP(have, want types.Receipts) error { 338 if len(have) != len(want) { 339 return fmt.Errorf("receipts sizes mismatch: have %d, want %d", len(have), len(want)) 340 } 341 for i := 0; i < len(want); i++ { 342 rlpHave, err := rlp.EncodeToBytes(have[i]) 343 if err != nil { 344 return err 345 } 346 rlpWant, err := rlp.EncodeToBytes(want[i]) 347 if err != nil { 348 return err 349 } 350 if !bytes.Equal(rlpHave, rlpWant) { 351 return fmt.Errorf("receipt #%d: receipt mismatch: have %s, want %s", i, hex.EncodeToString(rlpHave), hex.EncodeToString(rlpWant)) 352 } 353 } 354 return nil 355 }