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