github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/core/rawdb/accessors_chain_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2018 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package rawdb 26 27 import ( 28 "bytes" 29 "math/big" 30 "testing" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/crypto/sha3" 35 "github.com/ethereum/go-ethereum/ethdb" 36 "github.com/ethereum/go-ethereum/rlp" 37 ) 38 39 //测试块头存储和检索操作。 40 func TestHeaderStorage(t *testing.T) { 41 db := ethdb.NewMemDatabase() 42 43 //创建一个测试头以在数据库中移动,并确保它确实是新的 44 header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")} 45 if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil { 46 t.Fatalf("Non existent header returned: %v", entry) 47 } 48 //在数据库中写入并验证头 49 WriteHeader(db, header) 50 if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil { 51 t.Fatalf("Stored header not found") 52 } else if entry.Hash() != header.Hash() { 53 t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header) 54 } 55 if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil { 56 t.Fatalf("Stored header RLP not found") 57 } else { 58 hasher := sha3.NewKeccak256() 59 hasher.Write(entry) 60 61 if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() { 62 t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header) 63 } 64 } 65 //删除头并验证执行 66 DeleteHeader(db, header.Hash(), header.Number.Uint64()) 67 if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil { 68 t.Fatalf("Deleted header returned: %v", entry) 69 } 70 } 71 72 //测试块体存储和检索操作。 73 func TestBodyStorage(t *testing.T) { 74 db := ethdb.NewMemDatabase() 75 76 //创建一个测试体以在数据库中移动,并确保它确实是新的 77 body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}} 78 79 hasher := sha3.NewKeccak256() 80 rlp.Encode(hasher, body) 81 hash := common.BytesToHash(hasher.Sum(nil)) 82 83 if entry := ReadBody(db, hash, 0); entry != nil { 84 t.Fatalf("Non existent body returned: %v", entry) 85 } 86 //在数据库中写入并验证正文 87 WriteBody(db, hash, 0, body) 88 if entry := ReadBody(db, hash, 0); entry == nil { 89 t.Fatalf("Stored body not found") 90 } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) { 91 t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body) 92 } 93 if entry := ReadBodyRLP(db, hash, 0); entry == nil { 94 t.Fatalf("Stored body RLP not found") 95 } else { 96 hasher := sha3.NewKeccak256() 97 hasher.Write(entry) 98 99 if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash { 100 t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body) 101 } 102 } 103 //删除主体并验证执行 104 DeleteBody(db, hash, 0) 105 if entry := ReadBody(db, hash, 0); entry != nil { 106 t.Fatalf("Deleted body returned: %v", entry) 107 } 108 } 109 110 //测试块存储和检索操作。 111 func TestBlockStorage(t *testing.T) { 112 db := ethdb.NewMemDatabase() 113 114 //创建一个测试块来移动数据库并确保它真的是新的 115 block := types.NewBlockWithHeader(&types.Header{ 116 Extra: []byte("test block"), 117 UncleHash: types.EmptyUncleHash, 118 TxHash: types.EmptyRootHash, 119 ReceiptHash: types.EmptyRootHash, 120 }) 121 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 122 t.Fatalf("Non existent block returned: %v", entry) 123 } 124 if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil { 125 t.Fatalf("Non existent header returned: %v", entry) 126 } 127 if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil { 128 t.Fatalf("Non existent body returned: %v", entry) 129 } 130 //写入并验证数据库中的块 131 WriteBlock(db, block) 132 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil { 133 t.Fatalf("Stored block not found") 134 } else if entry.Hash() != block.Hash() { 135 t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block) 136 } 137 if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil { 138 t.Fatalf("Stored header not found") 139 } else if entry.Hash() != block.Header().Hash() { 140 t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header()) 141 } 142 if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil { 143 t.Fatalf("Stored body not found") 144 } else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) { 145 t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body()) 146 } 147 //删除块并验证执行 148 DeleteBlock(db, block.Hash(), block.NumberU64()) 149 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 150 t.Fatalf("Deleted block returned: %v", entry) 151 } 152 if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil { 153 t.Fatalf("Deleted header returned: %v", entry) 154 } 155 if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil { 156 t.Fatalf("Deleted body returned: %v", entry) 157 } 158 } 159 160 //测试部分块内容不能重新组装成完整块。 161 func TestPartialBlockStorage(t *testing.T) { 162 db := ethdb.NewMemDatabase() 163 block := types.NewBlockWithHeader(&types.Header{ 164 Extra: []byte("test block"), 165 UncleHash: types.EmptyUncleHash, 166 TxHash: types.EmptyRootHash, 167 ReceiptHash: types.EmptyRootHash, 168 }) 169 //存储一个头并检查它是否未被识别为块 170 WriteHeader(db, block.Header()) 171 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 172 t.Fatalf("Non existent block returned: %v", entry) 173 } 174 DeleteHeader(db, block.Hash(), block.NumberU64()) 175 176 //存储正文并检查它是否未被识别为块 177 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 178 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil { 179 t.Fatalf("Non existent block returned: %v", entry) 180 } 181 DeleteBody(db, block.Hash(), block.NumberU64()) 182 183 //单独存放一个收割台和一个车斗,并检查重新组装情况。 184 WriteHeader(db, block.Header()) 185 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 186 187 if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil { 188 t.Fatalf("Stored block not found") 189 } else if entry.Hash() != block.Hash() { 190 t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block) 191 } 192 } 193 194 //测试块总难度存储和检索操作。 195 func TestTdStorage(t *testing.T) { 196 db := ethdb.NewMemDatabase() 197 198 //创建一个测试td来移动数据库,并确保它是新的 199 hash, td := common.Hash{}, big.NewInt(314) 200 if entry := ReadTd(db, hash, 0); entry != nil { 201 t.Fatalf("Non existent TD returned: %v", entry) 202 } 203 //在数据库中编写并验证TD 204 WriteTd(db, hash, 0, td) 205 if entry := ReadTd(db, hash, 0); entry == nil { 206 t.Fatalf("Stored TD not found") 207 } else if entry.Cmp(td) != 0 { 208 t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td) 209 } 210 //删除TD并验证执行情况 211 DeleteTd(db, hash, 0) 212 if entry := ReadTd(db, hash, 0); entry != nil { 213 t.Fatalf("Deleted TD returned: %v", entry) 214 } 215 } 216 217 //测试规范数字是否可以映射到哈希并进行检索。 218 func TestCanonicalMappingStorage(t *testing.T) { 219 db := ethdb.NewMemDatabase() 220 221 //创建一个测试规范编号和分配的哈希以四处移动 222 hash, number := common.Hash{0: 0xff}, uint64(314) 223 if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) { 224 t.Fatalf("Non existent canonical mapping returned: %v", entry) 225 } 226 //在数据库中编写并验证TD 227 WriteCanonicalHash(db, hash, number) 228 if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) { 229 t.Fatalf("Stored canonical mapping not found") 230 } else if entry != hash { 231 t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash) 232 } 233 //删除TD并验证执行情况 234 DeleteCanonicalHash(db, number) 235 if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) { 236 t.Fatalf("Deleted canonical mapping returned: %v", entry) 237 } 238 } 239 240 //测试头标题和头块可以单独分配。 241 func TestHeadStorage(t *testing.T) { 242 db := ethdb.NewMemDatabase() 243 244 blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")}) 245 blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")}) 246 blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")}) 247 248 //检查原始数据库中是否没有头条目 249 if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) { 250 t.Fatalf("Non head header entry returned: %v", entry) 251 } 252 if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) { 253 t.Fatalf("Non head block entry returned: %v", entry) 254 } 255 if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) { 256 t.Fatalf("Non fast head block entry returned: %v", entry) 257 } 258 //为标题和块分配单独的条目 259 WriteHeadHeaderHash(db, blockHead.Hash()) 260 WriteHeadBlockHash(db, blockFull.Hash()) 261 WriteHeadFastBlockHash(db, blockFast.Hash()) 262 263 //检查两个磁头是否存在以及是否不同(即保持两个磁头) 264 if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() { 265 t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash()) 266 } 267 if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() { 268 t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash()) 269 } 270 if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() { 271 t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash()) 272 } 273 } 274 275 //测试与单个块关联的收据是否可以存储和检索。 276 func TestBlockReceiptStorage(t *testing.T) { 277 db := ethdb.NewMemDatabase() 278 279 receipt1 := &types.Receipt{ 280 Status: types.ReceiptStatusFailed, 281 CumulativeGasUsed: 1, 282 Logs: []*types.Log{ 283 {Address: common.BytesToAddress([]byte{0x11})}, 284 {Address: common.BytesToAddress([]byte{0x01, 0x11})}, 285 }, 286 TxHash: common.BytesToHash([]byte{0x11, 0x11}), 287 ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), 288 GasUsed: 111111, 289 } 290 receipt2 := &types.Receipt{ 291 PostState: common.Hash{2}.Bytes(), 292 CumulativeGasUsed: 2, 293 Logs: []*types.Log{ 294 {Address: common.BytesToAddress([]byte{0x22})}, 295 {Address: common.BytesToAddress([]byte{0x02, 0x22})}, 296 }, 297 TxHash: common.BytesToHash([]byte{0x22, 0x22}), 298 ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), 299 GasUsed: 222222, 300 } 301 receipts := []*types.Receipt{receipt1, receipt2} 302 303 //检查原始数据库中是否没有收据条目 304 hash := common.BytesToHash([]byte{0x03, 0x14}) 305 if rs := ReadReceipts(db, hash, 0); len(rs) != 0 { 306 t.Fatalf("non existent receipts returned: %v", rs) 307 } 308 //将收据切片插入数据库并检查是否存在 309 WriteReceipts(db, hash, 0, receipts) 310 if rs := ReadReceipts(db, hash, 0); len(rs) == 0 { 311 t.Fatalf("no receipts returned") 312 } else { 313 for i := 0; i < len(receipts); i++ { 314 rlpHave, _ := rlp.EncodeToBytes(rs[i]) 315 rlpWant, _ := rlp.EncodeToBytes(receipts[i]) 316 317 if !bytes.Equal(rlpHave, rlpWant) { 318 t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i]) 319 } 320 } 321 } 322 //删除收据切片并检查清除 323 DeleteReceipts(db, hash, 0) 324 if rs := ReadReceipts(db, hash, 0); len(rs) != 0 { 325 t.Fatalf("deleted receipts returned: %v", rs) 326 } 327 }