2016-11-09 04:01:56 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-10-14 06:47:09 +03:00
|
|
|
// 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/>.
|
2016-11-09 04:01:56 +03:00
|
|
|
|
2016-10-14 06:47:09 +03:00
|
|
|
package light
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-22 20:20:33 +03:00
|
|
|
"context"
|
2020-12-10 16:33:52 +03:00
|
|
|
"errors"
|
2020-07-13 12:02:54 +03:00
|
|
|
"math/big"
|
2016-10-14 06:47:09 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-05-07 14:35:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2022-10-24 16:13:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2016-10-14 06:47:09 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2020-12-10 16:33:52 +03:00
|
|
|
// errNonCanonicalHash is returned if the requested chain data doesn't belong
|
|
|
|
// to the canonical chain. ODR can only retrieve the canonical chain data covered
|
|
|
|
// by the CHT or Bloom trie for verification.
|
|
|
|
var errNonCanonicalHash = errors.New("hash is not currently canonical")
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2020-07-13 12:02:54 +03:00
|
|
|
// GetHeaderByNumber retrieves the canonical block header corresponding to the
|
2020-12-10 16:33:52 +03:00
|
|
|
// given number. The returned header is proven by local CHT.
|
2016-10-14 06:47:09 +03:00
|
|
|
func GetHeaderByNumber(ctx context.Context, odr OdrBackend, number uint64) (*types.Header, error) {
|
2020-07-13 12:02:54 +03:00
|
|
|
// Try to find it in the local database first.
|
2016-10-14 06:47:09 +03:00
|
|
|
db := odr.Database()
|
2018-05-07 14:35:06 +03:00
|
|
|
hash := rawdb.ReadCanonicalHash(db, number)
|
2016-10-14 06:47:09 +03:00
|
|
|
|
2020-07-13 12:02:54 +03:00
|
|
|
// If there is a canonical hash, there should have a header too.
|
|
|
|
// But if it's pruned, re-fetch from network again.
|
|
|
|
if (hash != common.Hash{}) {
|
|
|
|
if header := rawdb.ReadHeader(db, hash, number); header != nil {
|
|
|
|
return header, nil
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// Retrieve the header via ODR, ensure the requested header is covered
|
|
|
|
// by local trusted CHT.
|
|
|
|
chts, _, chtHead := odr.ChtIndexer().Sections()
|
|
|
|
if number >= chts*odr.IndexerConfig().ChtSize {
|
2019-08-21 12:29:34 +03:00
|
|
|
return nil, errNoTrustedCht
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
r := &ChtRequest{
|
|
|
|
ChtRoot: GetChtRoot(db, chts-1, chtHead),
|
|
|
|
ChtNum: chts - 1,
|
|
|
|
BlockNum: number,
|
|
|
|
Config: odr.IndexerConfig(),
|
|
|
|
}
|
2016-10-14 06:47:09 +03:00
|
|
|
if err := odr.Retrieve(ctx, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-08 08:49:23 +03:00
|
|
|
return r.Header, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-13 12:02:54 +03:00
|
|
|
// GetCanonicalHash retrieves the canonical block hash corresponding to the number.
|
2016-10-14 06:47:09 +03:00
|
|
|
func GetCanonicalHash(ctx context.Context, odr OdrBackend, number uint64) (common.Hash, error) {
|
2018-05-07 14:35:06 +03:00
|
|
|
hash := rawdb.ReadCanonicalHash(odr.Database(), number)
|
2020-07-13 12:02:54 +03:00
|
|
|
if hash != (common.Hash{}) {
|
2016-10-14 06:47:09 +03:00
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
header, err := GetHeaderByNumber(ctx, odr, number)
|
2020-07-13 12:02:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
// number -> canonical mapping already be stored in db, get it.
|
|
|
|
return header.Hash(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTd retrieves the total difficulty corresponding to the number and hash.
|
|
|
|
func GetTd(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*big.Int, error) {
|
|
|
|
td := rawdb.ReadTd(odr.Database(), hash, number)
|
|
|
|
if td != nil {
|
|
|
|
return td, nil
|
|
|
|
}
|
2020-12-10 16:33:52 +03:00
|
|
|
header, err := GetHeaderByNumber(ctx, odr, number)
|
2020-07-13 12:02:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
2020-12-10 16:33:52 +03:00
|
|
|
if header.Hash() != hash {
|
|
|
|
return nil, errNonCanonicalHash
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// <hash, number> -> td mapping already be stored in db, get it.
|
|
|
|
return rawdb.ReadTd(odr.Database(), hash, number), nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
|
|
|
|
func GetBodyRLP(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (rlp.RawValue, error) {
|
2018-05-07 14:35:06 +03:00
|
|
|
if data := rawdb.ReadBodyRLP(odr.Database(), hash, number); data != nil {
|
2016-10-14 06:47:09 +03:00
|
|
|
return data, nil
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// Retrieve the block header first and pass it for verification.
|
|
|
|
header, err := GetHeaderByNumber(ctx, odr, number)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errNoHeader
|
|
|
|
}
|
2020-12-10 16:33:52 +03:00
|
|
|
if header.Hash() != hash {
|
|
|
|
return nil, errNonCanonicalHash
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
r := &BlockRequest{Hash: hash, Number: number, Header: header}
|
2016-10-14 06:47:09 +03:00
|
|
|
if err := odr.Retrieve(ctx, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
return r.Rlp, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-13 12:02:54 +03:00
|
|
|
// GetBody retrieves the block body (transactions, uncles) corresponding to the
|
2016-10-14 06:47:09 +03:00
|
|
|
// hash.
|
|
|
|
func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Body, error) {
|
|
|
|
data, err := GetBodyRLP(ctx, odr, hash, number)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body := new(types.Body)
|
|
|
|
if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlock retrieves an entire block corresponding to the hash, assembling it
|
|
|
|
// back from the stored header and body.
|
|
|
|
func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (*types.Block, error) {
|
|
|
|
// Retrieve the block header and body contents
|
2020-07-13 12:02:54 +03:00
|
|
|
header, err := GetHeaderByNumber(ctx, odr, number)
|
|
|
|
if err != nil {
|
2019-08-21 12:29:34 +03:00
|
|
|
return nil, errNoHeader
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
|
|
|
body, err := GetBody(ctx, odr, hash, number)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Reassemble the block and return
|
|
|
|
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockReceipts retrieves the receipts generated by the transactions included
|
2023-02-13 12:59:27 +03:00
|
|
|
// in a block given by its hash. Receipts will be filled in with context data.
|
2016-10-14 06:47:09 +03:00
|
|
|
func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) {
|
2019-03-27 19:11:24 +03:00
|
|
|
// Assume receipts are already stored locally and attempt to retrieve.
|
|
|
|
receipts := rawdb.ReadRawReceipts(odr.Database(), hash, number)
|
2018-02-22 13:48:14 +03:00
|
|
|
if receipts == nil {
|
2020-07-13 12:02:54 +03:00
|
|
|
header, err := GetHeaderByNumber(ctx, odr, number)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errNoHeader
|
|
|
|
}
|
2020-12-10 16:33:52 +03:00
|
|
|
if header.Hash() != hash {
|
|
|
|
return nil, errNonCanonicalHash
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
r := &ReceiptsRequest{Hash: hash, Number: number, Header: header}
|
2018-02-22 13:48:14 +03:00
|
|
|
if err := odr.Retrieve(ctx, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
receipts = r.Receipts
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
2018-02-22 13:48:14 +03:00
|
|
|
// If the receipts are incomplete, fill the derived fields
|
|
|
|
if len(receipts) > 0 && receipts[0].TxHash == (common.Hash{}) {
|
|
|
|
block, err := GetBlock(ctx, odr, hash, number)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-07 14:35:06 +03:00
|
|
|
genesis := rawdb.ReadCanonicalHash(odr.Database(), 0)
|
|
|
|
config := rawdb.ReadChainConfig(odr.Database(), genesis)
|
2018-02-22 13:48:14 +03:00
|
|
|
|
2023-04-21 12:52:02 +03:00
|
|
|
if err := receipts.DeriveFields(config, block.Hash(), block.NumberU64(), block.Time(), block.BaseFee(), block.Transactions()); err != nil {
|
2018-03-07 13:05:14 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-07 14:35:06 +03:00
|
|
|
rawdb.WriteReceipts(odr.Database(), hash, number, receipts)
|
2018-02-22 13:48:14 +03:00
|
|
|
}
|
|
|
|
return receipts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockLogs retrieves the logs generated by the transactions included in a
|
2023-02-13 12:59:27 +03:00
|
|
|
// block given by its hash. Logs will be filled in with context data.
|
2018-02-22 13:48:14 +03:00
|
|
|
func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error) {
|
2019-03-27 19:11:24 +03:00
|
|
|
receipts, err := GetBlockReceipts(ctx, odr, hash, number)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-02-22 13:48:14 +03:00
|
|
|
}
|
|
|
|
logs := make([][]*types.Log, len(receipts))
|
|
|
|
for i, receipt := range receipts {
|
|
|
|
logs[i] = receipt.Logs
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
2018-02-22 13:48:14 +03:00
|
|
|
return logs, nil
|
2016-10-14 06:47:09 +03:00
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2020-07-13 12:02:54 +03:00
|
|
|
// GetBloomBits retrieves a batch of compressed bloomBits vectors belonging to
|
|
|
|
// the given bit index and section indexes.
|
|
|
|
func GetBloomBits(ctx context.Context, odr OdrBackend, bit uint, sections []uint64) ([][]byte, error) {
|
2017-10-24 16:19:09 +03:00
|
|
|
var (
|
2020-07-13 12:02:54 +03:00
|
|
|
reqIndex []int
|
|
|
|
reqSections []uint64
|
|
|
|
db = odr.Database()
|
|
|
|
result = make([][]byte, len(sections))
|
2017-10-24 16:19:09 +03:00
|
|
|
)
|
2020-07-13 12:02:54 +03:00
|
|
|
blooms, _, sectionHead := odr.BloomTrieIndexer().Sections()
|
|
|
|
for i, section := range sections {
|
|
|
|
sectionHead := rawdb.ReadCanonicalHash(db, (section+1)*odr.IndexerConfig().BloomSize-1)
|
|
|
|
// If we don't have the canonical hash stored for this section head number,
|
|
|
|
// we'll still look for an entry with a zero sectionHead (we store it with
|
|
|
|
// zero section head too if we don't know it at the time of the retrieval)
|
|
|
|
if bloomBits, _ := rawdb.ReadBloomBits(db, bit, section, sectionHead); len(bloomBits) != 0 {
|
2017-10-24 16:19:09 +03:00
|
|
|
result[i] = bloomBits
|
2020-07-13 12:02:54 +03:00
|
|
|
continue
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// TODO(rjl493456442) Convert sectionIndex to BloomTrie relative index
|
|
|
|
if section >= blooms {
|
|
|
|
return nil, errNoTrustedBloomTrie
|
|
|
|
}
|
|
|
|
reqSections = append(reqSections, section)
|
|
|
|
reqIndex = append(reqIndex, i)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// Find all bloombits in database, nothing to query via odr, return.
|
|
|
|
if reqSections == nil {
|
2017-10-24 16:19:09 +03:00
|
|
|
return result, nil
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
// Send odr request to retrieve missing bloombits.
|
|
|
|
r := &BloomRequest{
|
|
|
|
BloomTrieRoot: GetBloomTrieRoot(db, blooms-1, sectionHead),
|
|
|
|
BloomTrieNum: blooms - 1,
|
|
|
|
BitIdx: bit,
|
|
|
|
SectionIndexList: reqSections,
|
|
|
|
Config: odr.IndexerConfig(),
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
if err := odr.Retrieve(ctx, r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
for i, idx := range reqIndex {
|
|
|
|
result[idx] = r.BloomBits[i]
|
|
|
|
}
|
|
|
|
return result, nil
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
2019-05-13 14:41:10 +03:00
|
|
|
|
2021-02-25 16:24:04 +03:00
|
|
|
// GetTransaction retrieves a canonical transaction by hash and also returns
|
|
|
|
// its position in the chain. There is no guarantee in the LES protocol that
|
|
|
|
// the mined transaction will be retrieved back for sure because of different
|
2022-08-19 09:00:21 +03:00
|
|
|
// reasons(the transaction is unindexed, the malicious server doesn't reply it
|
2021-02-25 16:24:04 +03:00
|
|
|
// deliberately, etc). Therefore, unretrieved transactions will receive a certain
|
2022-08-19 09:00:21 +03:00
|
|
|
// number of retries, thus giving a weak guarantee.
|
2019-05-13 14:41:10 +03:00
|
|
|
func GetTransaction(ctx context.Context, odr OdrBackend, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) {
|
|
|
|
r := &TxStatusRequest{Hashes: []common.Hash{txHash}}
|
2022-10-24 16:13:55 +03:00
|
|
|
if err := odr.RetrieveTxStatus(ctx, r); err != nil || r.Status[0].Status != txpool.TxStatusIncluded {
|
2019-05-13 14:41:10 +03:00
|
|
|
return nil, common.Hash{}, 0, 0, err
|
|
|
|
}
|
2020-07-13 12:02:54 +03:00
|
|
|
pos := r.Status[0].Lookup
|
|
|
|
// first ensure that we have the header, otherwise block body retrieval will fail
|
|
|
|
// also verify if this is a canonical block by getting the header by number and checking its hash
|
|
|
|
if header, err := GetHeaderByNumber(ctx, odr, pos.BlockIndex); err != nil || header.Hash() != pos.BlockHash {
|
|
|
|
return nil, common.Hash{}, 0, 0, err
|
|
|
|
}
|
|
|
|
body, err := GetBody(ctx, odr, pos.BlockHash, pos.BlockIndex)
|
|
|
|
if err != nil || uint64(len(body.Transactions)) <= pos.Index || body.Transactions[pos.Index].Hash() != txHash {
|
|
|
|
return nil, common.Hash{}, 0, 0, err
|
|
|
|
}
|
|
|
|
return body.Transactions[pos.Index], pos.BlockHash, pos.BlockIndex, pos.Index, nil
|
2019-05-13 14:41:10 +03:00
|
|
|
}
|