| 
									
										
										
										
											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 ( | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/log" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/rlp" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadTxLookupEntry retrieves the positional metadata associated with a transaction | 
					
						
							|  |  |  | // hash to allow retrieving the transaction or receipt by hash. | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) common.Hash { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	data, _ := db.Get(txLookupKey(hash)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if len(data) == 0 { | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 		return common.Hash{} | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	if len(data) == common.HashLength { | 
					
						
							|  |  |  | 		return common.BytesToHash(data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Probably it's legacy txlookup entry data, try to decode it. | 
					
						
							|  |  |  | 	var entry LegacyTxLookupEntry | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if err := rlp.DecodeBytes(data, &entry); err != nil { | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 		log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err) | 
					
						
							|  |  |  | 		return common.Hash{} | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	return entry.BlockHash | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteTxLookupEntries stores a positional metadata for every transaction from | 
					
						
							|  |  |  | // a block, enabling hash based transaction and receipt lookups. | 
					
						
							|  |  |  | func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) { | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	for _, tx := range block.Transactions() { | 
					
						
							|  |  |  | 		if err := db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 			log.Crit("Failed to store transaction lookup entry", "err", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // DeleteTxLookupEntry removes all transaction data associated with a hash. | 
					
						
							|  |  |  | func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	db.Delete(txLookupKey(hash)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadTransaction retrieves a specific transaction from the database, along with | 
					
						
							|  |  |  | // its added positional metadata. | 
					
						
							|  |  |  | func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	blockHash := ReadTxLookupEntry(db, hash) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if blockHash == (common.Hash{}) { | 
					
						
							|  |  |  | 		return nil, common.Hash{}, 0, 0 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	blockNumber := ReadHeaderNumber(db, blockHash) | 
					
						
							|  |  |  | 	if blockNumber == nil { | 
					
						
							|  |  |  | 		return nil, common.Hash{}, 0, 0 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	body := ReadBody(db, blockHash, *blockNumber) | 
					
						
							|  |  |  | 	if body == nil { | 
					
						
							|  |  |  | 		log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		return nil, common.Hash{}, 0, 0 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	for txIndex, tx := range body.Transactions { | 
					
						
							|  |  |  | 		if tx.Hash() == hash { | 
					
						
							|  |  |  | 			return tx, blockHash, *blockNumber, uint64(txIndex) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash) | 
					
						
							|  |  |  | 	return nil, common.Hash{}, 0, 0 | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadReceipt retrieves a specific transaction receipt from the database, along with | 
					
						
							|  |  |  | // its added positional metadata. | 
					
						
							|  |  |  | func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	blockHash := ReadTxLookupEntry(db, hash) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 	if blockHash == (common.Hash{}) { | 
					
						
							|  |  |  | 		return nil, common.Hash{}, 0, 0 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	blockNumber := ReadHeaderNumber(db, blockHash) | 
					
						
							|  |  |  | 	if blockNumber == nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		return nil, common.Hash{}, 0, 0 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-21 21:14:35 +08:00
										 |  |  | 	receipts := ReadReceipts(db, blockHash, *blockNumber) | 
					
						
							|  |  |  | 	for receiptIndex, receipt := range receipts { | 
					
						
							|  |  |  | 		if receipt.TxHash == hash { | 
					
						
							|  |  |  | 			return receipt, blockHash, *blockNumber, uint64(receiptIndex) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash) | 
					
						
							|  |  |  | 	return nil, common.Hash{}, 0, 0 | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given | 
					
						
							|  |  |  | // section and bit index from the. | 
					
						
							|  |  |  | func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	return db.Get(bloomBitsKey(bit, section, head)) | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // WriteBloomBits stores the compressed bloom bits vector belonging to the given | 
					
						
							|  |  |  | // section and bit index. | 
					
						
							|  |  |  | func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) { | 
					
						
							| 
									
										
										
										
											2018-06-11 21:06:26 +08:00
										 |  |  | 	if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil { | 
					
						
							| 
									
										
										
										
											2018-05-07 14:35:06 +03:00
										 |  |  | 		log.Crit("Failed to store bloom bits", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |