| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | // Copyright 2018 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package rawdb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"encoding/binary" | 
					
						
							|  |  |  | 	"math/big" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | 	"github.com/ethereum/go-ethereum/ethdb" | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	"github.com/ethereum/go-ethereum/log" | 
					
						
							| 
									
										
										
										
											2019-03-27 09:11:24 -07:00
										 |  |  | 	"github.com/ethereum/go-ethereum/params" | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	"github.com/ethereum/go-ethereum/rlp" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadCanonicalHash retrieves the hash assigned to a canonical block number. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	data, _ := db.Get(headerHashKey(number)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return common.Hash{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return common.BytesToHash(data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteCanonicalHash stores the hash assigned to a canonical block number. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteCanonicalHash(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to store number to hash mapping", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteCanonicalHash removes the number to hash canonical mapping. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func DeleteCanonicalHash(db ethdb.Writer, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Delete(headerHashKey(number)); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to delete number to hash mapping", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadHeaderNumber returns the header number assigned to a hash. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadHeaderNumber(db ethdb.Reader, hash common.Hash) *uint64 { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	data, _ := db.Get(headerNumberKey(hash)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if len(data) != 8 { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	number := binary.BigEndian.Uint64(data) | 
					
						
							|  |  |  | 	return &number | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadHeadHeaderHash retrieves the hash of the current canonical head header. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadHeadHeaderHash(db ethdb.Reader) common.Hash { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data, _ := db.Get(headHeaderKey) | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return common.Hash{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return common.BytesToHash(data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteHeadHeaderHash stores the hash of the current canonical head header. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteHeadHeaderHash(db ethdb.Writer, hash common.Hash) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := db.Put(headHeaderKey, hash.Bytes()); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to store last header's hash", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadHeadBlockHash retrieves the hash of the current canonical head block. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadHeadBlockHash(db ethdb.Reader) common.Hash { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data, _ := db.Get(headBlockKey) | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return common.Hash{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return common.BytesToHash(data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteHeadBlockHash stores the head block's hash. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteHeadBlockHash(db ethdb.Writer, hash common.Hash) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := db.Put(headBlockKey, hash.Bytes()); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to store last block's hash", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadHeadFastBlockHash(db ethdb.Reader) common.Hash { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data, _ := db.Get(headFastBlockKey) | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return common.Hash{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return common.BytesToHash(data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteHeadFastBlockHash stores the hash of the current fast-sync head block. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteHeadFastBlockHash(db ethdb.Writer, hash common.Hash) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to store last fast block's hash", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow | 
					
						
							|  |  |  | // reporting correct numbers across restarts. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadFastTrieProgress(db ethdb.Reader) uint64 { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data, _ := db.Get(fastTrieProgressKey) | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return new(big.Int).SetBytes(data).Uint64() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteFastTrieProgress stores the fast sync trie process counter to support | 
					
						
							|  |  |  | // retrieving it across restarts. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteFastTrieProgress(db ethdb.Writer, count uint64) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to store fast sync trie progress", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadHeaderRLP retrieves a block header in its raw RLP database encoding. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	data, _ := db.Get(headerKey(number, hash)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	return data | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // HasHeader verifies the existence of a block header corresponding to the hash. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if has, err := db.Has(headerKey(number, hash)); !has || err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadHeader retrieves the block header corresponding to the hash. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data := ReadHeaderRLP(db, hash, number) | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	header := new(types.Header) | 
					
						
							|  |  |  | 	if err := rlp.Decode(bytes.NewReader(data), header); err != nil { | 
					
						
							|  |  |  | 		log.Error("Invalid block header RLP", "hash", hash, "err", err) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return header | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteHeader stores a block header into the database and also stores the hash- | 
					
						
							|  |  |  | // to-number mapping. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteHeader(db ethdb.Writer, header *types.Header) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	// Write the hash -> number mapping | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 		hash    = header.Hash() | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		number  = header.Number.Uint64() | 
					
						
							|  |  |  | 		encoded = encodeBlockNumber(number) | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	key := headerNumberKey(hash) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := db.Put(key, encoded); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to store hash to number mapping", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Write the encoded header | 
					
						
							|  |  |  | 	data, err := rlp.EncodeToBytes(header) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to RLP encode header", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	key = headerKey(number, hash) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := db.Put(key, data); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to store header", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteHeader removes all block header data associated with a hash. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func DeleteHeader(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | 	deleteHeaderWithoutNumber(db, hash, number) | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Delete(headerNumberKey(hash)); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to delete hash to number mapping", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | // deleteHeaderWithoutNumber removes only the block header but does not remove | 
					
						
							|  |  |  | // the hash to number mapping. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func deleteHeaderWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | 	if err := db.Delete(headerKey(number, hash)); err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to delete header", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	data, _ := db.Get(blockBodyKey(number, hash)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	return data | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteBodyRLP stores an RLP encoded block body into the database. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteBodyRLP(db ethdb.Writer, hash common.Hash, number uint64, rlp rlp.RawValue) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Put(blockBodyKey(number, hash), rlp); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to store block body", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // HasBody verifies the existence of a block body corresponding to the hash. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadBody retrieves the block body corresponding to the hash. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data := ReadBodyRLP(db, hash, number) | 
					
						
							|  |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	body := new(types.Body) | 
					
						
							|  |  |  | 	if err := rlp.Decode(bytes.NewReader(data), body); err != nil { | 
					
						
							|  |  |  | 		log.Error("Invalid block body RLP", "hash", hash, "err", err) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return body | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 17:22:21 +08:00
										 |  |  | // WriteBody stores a block body into the database. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteBody(db ethdb.Writer, hash common.Hash, number uint64, body *types.Body) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data, err := rlp.EncodeToBytes(body) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to RLP encode body", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	WriteBodyRLP(db, hash, number, data) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteBody removes all block body data associated with a hash. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func DeleteBody(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Delete(blockBodyKey(number, hash)); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to delete block body", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | // ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding. | 
					
						
							|  |  |  | func ReadTdRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	data, _ := db.Get(headerTDKey(number, hash)) | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | 	return data | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadTd retrieves a block's total difficulty corresponding to the hash. | 
					
						
							|  |  |  | func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int { | 
					
						
							|  |  |  | 	data := ReadTdRLP(db, hash, number) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	td := new(big.Int) | 
					
						
							|  |  |  | 	if err := rlp.Decode(bytes.NewReader(data), td); err != nil { | 
					
						
							|  |  |  | 		log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return td | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteTd stores the total difficulty of a block into the database. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteTd(db ethdb.Writer, hash common.Hash, number uint64, td *big.Int) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	data, err := rlp.EncodeToBytes(td) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to RLP encode block total difficulty", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Put(headerTDKey(number, hash), data); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to store block total difficulty", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteTd removes all block total difficulty data associated with a hash. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func DeleteTd(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Delete(headerTDKey(number, hash)); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to delete block total difficulty", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 13:15:05 +02:00
										 |  |  | // HasReceipts verifies the existence of all the transaction receipts belonging | 
					
						
							|  |  |  | // to a block. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool { | 
					
						
							| 
									
										
										
										
											2018-11-16 13:15:05 +02:00
										 |  |  | 	if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | // ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding. | 
					
						
							|  |  |  | func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { | 
					
						
							|  |  |  | 	data, _ := db.Get(blockReceiptsKey(number, hash)) | 
					
						
							|  |  |  | 	return data | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-27 09:11:24 -07:00
										 |  |  | // ReadRawReceipts retrieves all the transaction receipts belonging to a block. | 
					
						
							|  |  |  | // The receipt metadata fields are not guaranteed to be populated, so they | 
					
						
							|  |  |  | // should not be used. Use ReadReceipts instead if the metadata is needed. | 
					
						
							|  |  |  | func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	// Retrieve the flattened receipt slice | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | 	data := ReadReceiptsRLP(db, hash, number) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if len(data) == 0 { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-19 21:33:27 +08:00
										 |  |  | 	// Convert the receipts from their storage form to their internal representation | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	storageReceipts := []*types.ReceiptForStorage{} | 
					
						
							|  |  |  | 	if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { | 
					
						
							|  |  |  | 		log.Error("Invalid receipt array RLP", "hash", hash, "err", err) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-15 12:36:27 +03:00
										 |  |  | 	receipts := make(types.Receipts, len(storageReceipts)) | 
					
						
							|  |  |  | 	for i, storageReceipt := range storageReceipts { | 
					
						
							|  |  |  | 		receipts[i] = (*types.Receipt)(storageReceipt) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return receipts | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-27 09:11:24 -07:00
										 |  |  | // ReadReceipts retrieves all the transaction receipts belonging to a block, including | 
					
						
							|  |  |  | // its correspoinding metadata fields. If it is unable to populate these metadata | 
					
						
							|  |  |  | // fields then nil is returned. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The current implementation populates these metadata fields by reading the receipts' | 
					
						
							|  |  |  | // corresponding block body, so if the block body is not found it will return nil even | 
					
						
							|  |  |  | // if the receipt itself is stored. | 
					
						
							| 
									
										
										
										
											2019-04-15 12:36:27 +03:00
										 |  |  | func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts { | 
					
						
							|  |  |  | 	// We're deriving many fields from the block body, retrieve beside the receipt | 
					
						
							| 
									
										
										
										
											2019-03-27 09:11:24 -07:00
										 |  |  | 	receipts := ReadRawReceipts(db, hash, number) | 
					
						
							|  |  |  | 	if receipts == nil { | 
					
						
							| 
									
										
										
										
											2019-04-15 12:36:27 +03:00
										 |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2019-03-27 09:11:24 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	body := ReadBody(db, hash, number) | 
					
						
							|  |  |  | 	if body == nil { | 
					
						
							|  |  |  | 		log.Error("Missing body but have receipt", "hash", hash, "number", number) | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-15 12:36:27 +03:00
										 |  |  | 	if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil { | 
					
						
							|  |  |  | 		log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err) | 
					
						
							| 
									
										
										
										
											2019-03-27 09:11:24 -07:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return receipts | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | // WriteReceipts stores all the transaction receipts belonging to a block. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts types.Receipts) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	// Convert the receipts into their storage form and serialize them | 
					
						
							|  |  |  | 	storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) | 
					
						
							|  |  |  | 	for i, receipt := range receipts { | 
					
						
							|  |  |  | 		storageReceipts[i] = (*types.ReceiptForStorage)(receipt) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	bytes, err := rlp.EncodeToBytes(storageReceipts) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Crit("Failed to encode block receipts", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Store the flattened receipt slice | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to store block receipts", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteReceipts removes all receipt data associated with a block hash. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func DeleteReceipts(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Delete(blockReceiptsKey(number, hash)); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to delete block receipts", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadBlock retrieves an entire block corresponding to the hash, assembling it | 
					
						
							|  |  |  | // back from the stored header and body. If either the header or body could not | 
					
						
							|  |  |  | // be retrieved nil is returned. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Note, due to concurrent download of header and block body the header and thus | 
					
						
							|  |  |  | // canonical hash can be stored in the database but the body data not (yet). | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	header := ReadHeader(db, hash, number) | 
					
						
							|  |  |  | 	if header == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	body := ReadBody(db, hash, number) | 
					
						
							|  |  |  | 	if body == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteBlock serializes a block into the database, header and body separately. | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func WriteBlock(db ethdb.Writer, block *types.Block) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) | 
					
						
							|  |  |  | 	WriteHeader(db, block.Header()) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteBlock removes all block data associated with a hash. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func DeleteBlock(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	DeleteReceipts(db, hash, number) | 
					
						
							|  |  |  | 	DeleteHeader(db, hash, number) | 
					
						
							|  |  |  | 	DeleteBody(db, hash, number) | 
					
						
							|  |  |  | 	DeleteTd(db, hash, number) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | // deleteBlockWithoutNumber removes all block data associated with a hash, except | 
					
						
							|  |  |  | // the hash to number mapping. | 
					
						
							| 
									
										
										
										
											2019-03-26 15:48:31 +01:00
										 |  |  | func deleteBlockWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64) { | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | 	DeleteReceipts(db, hash, number) | 
					
						
							|  |  |  | 	deleteHeaderWithoutNumber(db, hash, number) | 
					
						
							|  |  |  | 	DeleteBody(db, hash, number) | 
					
						
							|  |  |  | 	DeleteTd(db, hash, number) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | // FindCommonAncestor returns the last common ancestor of two block headers | 
					
						
							| 
									
										
										
										
											2018-09-24 15:57:49 +03:00
										 |  |  | func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	for bn := b.Number.Uint64(); a.Number.Uint64() > bn; { | 
					
						
							|  |  |  | 		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) | 
					
						
							|  |  |  | 		if a == nil { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for an := a.Number.Uint64(); an < b.Number.Uint64(); { | 
					
						
							|  |  |  | 		b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) | 
					
						
							|  |  |  | 		if b == nil { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for a.Hash() != b.Hash() { | 
					
						
							|  |  |  | 		a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) | 
					
						
							|  |  |  | 		if a == nil { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) | 
					
						
							|  |  |  | 		if b == nil { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return a | 
					
						
							|  |  |  | } |