github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/core/rawdb/accessors_chain.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 "encoding/binary" 22 "math/big" 23 24 "github.com/PlatONnetwork/PlatON-Go/common" 25 "github.com/PlatONnetwork/PlatON-Go/core/types" 26 "github.com/PlatONnetwork/PlatON-Go/log" 27 "github.com/PlatONnetwork/PlatON-Go/rlp" 28 ) 29 30 // ReadCanonicalHash retrieves the hash assigned to a canonical block number. 31 func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash { 32 data, _ := db.Get(headerHashKey(number)) 33 if len(data) == 0 { 34 return common.Hash{} 35 } 36 return common.BytesToHash(data) 37 } 38 39 // WriteCanonicalHash stores the hash assigned to a canonical block number. 40 func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) { 41 if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil { 42 log.Crit("Failed to store number to hash mapping", "err", err) 43 } 44 } 45 46 // DeleteCanonicalHash removes the number to hash canonical mapping. 47 func DeleteCanonicalHash(db DatabaseDeleter, number uint64) { 48 if err := db.Delete(headerHashKey(number)); err != nil { 49 log.Crit("Failed to delete number to hash mapping", "err", err) 50 } 51 } 52 53 // ReadHeaderNumber returns the header number assigned to a hash. 54 func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 { 55 data, _ := db.Get(headerNumberKey(hash)) 56 if len(data) != 8 { 57 return nil 58 } 59 number := binary.BigEndian.Uint64(data) 60 return &number 61 } 62 63 // ReadHeadHeaderHash retrieves the hash of the current canonical head header. 64 func ReadHeadHeaderHash(db DatabaseReader) common.Hash { 65 data, _ := db.Get(headHeaderKey) 66 if len(data) == 0 { 67 return common.Hash{} 68 } 69 return common.BytesToHash(data) 70 } 71 72 // WriteHeadHeaderHash stores the hash of the current canonical head header. 73 func WriteHeadHeaderHash(db DatabaseWriter, hash common.Hash) { 74 if err := db.Put(headHeaderKey, hash.Bytes()); err != nil { 75 log.Crit("Failed to store last header's hash", "err", err) 76 } 77 } 78 79 // ReadHeadBlockHash retrieves the hash of the current canonical head block. 80 func ReadHeadBlockHash(db DatabaseReader) common.Hash { 81 data, _ := db.Get(headBlockKey) 82 if len(data) == 0 { 83 return common.Hash{} 84 } 85 return common.BytesToHash(data) 86 } 87 88 // WriteHeadBlockHash stores the head block's hash. 89 func WriteHeadBlockHash(db DatabaseWriter, hash common.Hash) { 90 if err := db.Put(headBlockKey, hash.Bytes()); err != nil { 91 log.Crit("Failed to store last block's hash", "err", err) 92 } 93 } 94 95 // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block. 96 func ReadHeadFastBlockHash(db DatabaseReader) common.Hash { 97 data, _ := db.Get(headFastBlockKey) 98 if len(data) == 0 { 99 return common.Hash{} 100 } 101 return common.BytesToHash(data) 102 } 103 104 // WriteHeadFastBlockHash stores the hash of the current fast-sync head block. 105 func WriteHeadFastBlockHash(db DatabaseWriter, hash common.Hash) { 106 if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil { 107 log.Crit("Failed to store last fast block's hash", "err", err) 108 } 109 } 110 111 // ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow 112 // reporting correct numbers across restarts. 113 func ReadFastTrieProgress(db DatabaseReader) uint64 { 114 data, _ := db.Get(fastTrieProgressKey) 115 if len(data) == 0 { 116 return 0 117 } 118 return new(big.Int).SetBytes(data).Uint64() 119 } 120 121 // WriteFastTrieProgress stores the fast sync trie process counter to support 122 // retrieving it across restarts. 123 func WriteFastTrieProgress(db DatabaseWriter, count uint64) { 124 if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil { 125 log.Crit("Failed to store fast sync trie progress", "err", err) 126 } 127 } 128 129 // ReadHeaderRLP retrieves a block header in its raw RLP database encoding. 130 func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { 131 data, _ := db.Get(headerKey(number, hash)) 132 return data 133 } 134 135 // HasHeader verifies the existence of a block header corresponding to the hash. 136 func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool { 137 if has, err := db.Has(headerKey(number, hash)); !has || err != nil { 138 return false 139 } 140 return true 141 } 142 143 // ReadHeader retrieves the block header corresponding to the hash. 144 func ReadHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header { 145 data := ReadHeaderRLP(db, hash, number) 146 if len(data) == 0 { 147 return nil 148 } 149 header := new(types.Header) 150 if err := rlp.Decode(bytes.NewReader(data), header); err != nil { 151 log.Error("Invalid block header RLP", "hash", hash, "err", err) 152 return nil 153 } 154 return header 155 } 156 157 // WriteHeader stores a block header into the database and also stores the hash- 158 // to-number mapping. 159 func WriteHeader(db DatabaseWriter, header *types.Header) { 160 // Write the hash -> number mapping 161 var ( 162 hash = header.Hash() 163 number = header.Number.Uint64() 164 encoded = encodeBlockNumber(number) 165 ) 166 key := headerNumberKey(hash) 167 if err := db.Put(key, encoded); err != nil { 168 log.Crit("Failed to store hash to number mapping", "err", err) 169 } 170 // Write the encoded header 171 data, err := rlp.EncodeToBytes(header) 172 if err != nil { 173 log.Crit("Failed to RLP encode header", "err", err) 174 } 175 key = headerKey(number, hash) 176 if err := db.Put(key, data); err != nil { 177 log.Crit("Failed to store header", "err", err) 178 } 179 } 180 181 // DeleteHeader removes all block header data associated with a hash. 182 func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) { 183 if err := db.Delete(headerKey(number, hash)); err != nil { 184 log.Crit("Failed to delete header", "err", err) 185 } 186 if err := db.Delete(headerNumberKey(hash)); err != nil { 187 log.Crit("Failed to delete hash to number mapping", "err", err) 188 } 189 } 190 191 // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. 192 func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { 193 data, _ := db.Get(blockBodyKey(number, hash)) 194 return data 195 } 196 197 // WriteBodyRLP stores an RLP encoded block body into the database. 198 func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) { 199 if err := db.Put(blockBodyKey(number, hash), rlp); err != nil { 200 log.Crit("Failed to store block body", "err", err) 201 } 202 } 203 204 // HasBody verifies the existence of a block body corresponding to the hash. 205 func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool { 206 if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil { 207 return false 208 } 209 return true 210 } 211 212 // ReadBody retrieves the block body corresponding to the hash. 213 func ReadBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body { 214 data := ReadBodyRLP(db, hash, number) 215 if len(data) == 0 { 216 return nil 217 } 218 body := new(types.Body) 219 if err := rlp.Decode(bytes.NewReader(data), body); err != nil { 220 log.Error("Invalid block body RLP", "hash", hash, "err", err) 221 return nil 222 } 223 return body 224 } 225 226 // WriteBody storea a block body into the database. 227 func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.Body) { 228 data, err := rlp.EncodeToBytes(body) 229 if err != nil { 230 log.Crit("Failed to RLP encode body", "err", err) 231 } 232 WriteBodyRLP(db, hash, number, data) 233 } 234 235 // DeleteBody removes all block body data associated with a hash. 236 func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) { 237 if err := db.Delete(blockBodyKey(number, hash)); err != nil { 238 log.Crit("Failed to delete block body", "err", err) 239 } 240 } 241 242 // ReadReceipts retrieves all the transaction receipts belonging to a block. 243 func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts { 244 // Retrieve the flattened receipt slice 245 data, _ := db.Get(blockReceiptsKey(number, hash)) 246 if len(data) == 0 { 247 return nil 248 } 249 // Convert the revceipts from their storage form to their internal representation 250 storageReceipts := []*types.ReceiptForStorage{} 251 if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { 252 log.Error("Invalid receipt array RLP", "hash", hash, "err", err) 253 return nil 254 } 255 receipts := make(types.Receipts, len(storageReceipts)) 256 for i, receipt := range storageReceipts { 257 receipts[i] = (*types.Receipt)(receipt) 258 } 259 return receipts 260 } 261 262 // WriteReceipts stores all the transaction receipts belonging to a block. 263 func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts types.Receipts) { 264 // Convert the receipts into their storage form and serialize them 265 storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) 266 for i, receipt := range receipts { 267 storageReceipts[i] = (*types.ReceiptForStorage)(receipt) 268 } 269 bytes, err := rlp.EncodeToBytes(storageReceipts) 270 if err != nil { 271 log.Crit("Failed to encode block receipts", "err", err) 272 } 273 // Store the flattened receipt slice 274 if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil { 275 log.Crit("Failed to store block receipts", "err", err) 276 } 277 } 278 279 // ReadBlockConfirmSigns retrieves all the block confirmSigns belonging to a block. 280 func ReadBlockConfirmSigns(db DatabaseReader, hash common.Hash, number uint64) []*common.BlockConfirmSign { 281 data, _ := db.Get(blockConfirmSignsKey(number, hash)) 282 if len(data) == 0 { 283 return nil 284 } 285 blockConfirmSigns := []*common.BlockConfirmSign{} 286 if err := rlp.DecodeBytes(data, &blockConfirmSigns); err != nil { 287 log.Error("Invalid block confirmSign array RLP", "hash", hash, "err", err) 288 return nil 289 } 290 return blockConfirmSigns 291 } 292 293 // WriteBlockConfirmSigns stores all the block confirmSigns belonging to a block. 294 func WriteBlockConfirmSigns(db DatabaseWriter, hash common.Hash, number uint64, blockConfirmSigns []*common.BlockConfirmSign) { 295 bytes, err := rlp.EncodeToBytes(blockConfirmSigns) 296 if err != nil { 297 log.Crit("Failed to encode block confirmSigns", "err", err) 298 } 299 // Store the flattened receipt slice 300 if err := db.Put(blockConfirmSignsKey(number, hash), bytes); err != nil { 301 log.Crit("Failed to store block confirmSigns", "err", err) 302 } 303 } 304 305 // DeleteReceipts removes all receipt data associated with a block hash. 306 func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) { 307 if err := db.Delete(blockReceiptsKey(number, hash)); err != nil { 308 log.Crit("Failed to delete block receipts", "err", err) 309 } 310 } 311 312 // ReadBlock retrieves an entire block corresponding to the hash, assembling it 313 // back from the stored header and body. If either the header or body could not 314 // be retrieved nil is returned. 315 // 316 // Note, due to concurrent download of header and block body the header and thus 317 // canonical hash can be stored in the database but the body data not (yet). 318 func ReadBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block { 319 header := ReadHeader(db, hash, number) 320 if header == nil { 321 return nil 322 } 323 body := ReadBody(db, hash, number) 324 if body == nil { 325 return nil 326 } 327 return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles, body.Signatures) 328 } 329 330 // WriteBlock serializes a block into the database, header and body separately. 331 func WriteBlock(db DatabaseWriter, block *types.Block) { 332 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 333 WriteHeader(db, block.Header()) 334 } 335 336 // DeleteBlock removes all block data associated with a hash. 337 func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) { 338 DeleteReceipts(db, hash, number) 339 DeleteHeader(db, hash, number) 340 DeleteBody(db, hash, number) 341 } 342 343 // FindCommonAncestor returns the last common ancestor of two block headers 344 func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header { 345 for bn := b.Number.Uint64(); a.Number.Uint64() > bn; { 346 a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) 347 if a == nil { 348 return nil 349 } 350 } 351 for an := a.Number.Uint64(); an < b.Number.Uint64(); { 352 b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) 353 if b == nil { 354 return nil 355 } 356 } 357 for a.Hash() != b.Hash() { 358 a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) 359 if a == nil { 360 return nil 361 } 362 b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) 363 if b == nil { 364 return nil 365 } 366 } 367 return a 368 } 369 370