github.com/luckypickle/go-ethereum-vet@v1.14.2/light/odr.go (about)

     1  // Copyright 2015 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 light implements on-demand retrieval capable state and chain objects
    18  // for the Ethereum Light Client.
    19  package light
    20  
    21  import (
    22  	"context"
    23  	"errors"
    24  	"math/big"
    25  
    26  	"github.com/luckypickle/go-ethereum-vet/common"
    27  	"github.com/luckypickle/go-ethereum-vet/core"
    28  	"github.com/luckypickle/go-ethereum-vet/core/rawdb"
    29  	"github.com/luckypickle/go-ethereum-vet/core/types"
    30  	"github.com/luckypickle/go-ethereum-vet/ethdb"
    31  )
    32  
    33  // NoOdr is the default context passed to an ODR capable function when the ODR
    34  // service is not required.
    35  var NoOdr = context.Background()
    36  
    37  // ErrNoPeers is returned if no peers capable of serving a queued request are available
    38  var ErrNoPeers = errors.New("no suitable peers available")
    39  
    40  // OdrBackend is an interface to a backend service that handles ODR retrievals type
    41  type OdrBackend interface {
    42  	Database() ethdb.Database
    43  	ChtIndexer() *core.ChainIndexer
    44  	BloomTrieIndexer() *core.ChainIndexer
    45  	BloomIndexer() *core.ChainIndexer
    46  	Retrieve(ctx context.Context, req OdrRequest) error
    47  }
    48  
    49  // OdrRequest is an interface for retrieval requests
    50  type OdrRequest interface {
    51  	StoreResult(db ethdb.Database)
    52  }
    53  
    54  // TrieID identifies a state or account storage trie
    55  type TrieID struct {
    56  	BlockHash, Root common.Hash
    57  	BlockNumber     uint64
    58  	AccKey          []byte
    59  }
    60  
    61  // StateTrieID returns a TrieID for a state trie belonging to a certain block
    62  // header.
    63  func StateTrieID(header *types.Header) *TrieID {
    64  	return &TrieID{
    65  		BlockHash:   header.Hash(),
    66  		BlockNumber: header.Number.Uint64(),
    67  		AccKey:      nil,
    68  		Root:        header.Root,
    69  	}
    70  }
    71  
    72  // StorageTrieID returns a TrieID for a contract storage trie at a given account
    73  // of a given state trie. It also requires the root hash of the trie for
    74  // checking Merkle proofs.
    75  func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID {
    76  	return &TrieID{
    77  		BlockHash:   state.BlockHash,
    78  		BlockNumber: state.BlockNumber,
    79  		AccKey:      addrHash[:],
    80  		Root:        root,
    81  	}
    82  }
    83  
    84  // TrieRequest is the ODR request type for state/storage trie entries
    85  type TrieRequest struct {
    86  	OdrRequest
    87  	Id    *TrieID
    88  	Key   []byte
    89  	Proof *NodeSet
    90  }
    91  
    92  // StoreResult stores the retrieved data in local database
    93  func (req *TrieRequest) StoreResult(db ethdb.Database) {
    94  	req.Proof.Store(db)
    95  }
    96  
    97  // CodeRequest is the ODR request type for retrieving contract code
    98  type CodeRequest struct {
    99  	OdrRequest
   100  	Id   *TrieID // references storage trie of the account
   101  	Hash common.Hash
   102  	Data []byte
   103  }
   104  
   105  // StoreResult stores the retrieved data in local database
   106  func (req *CodeRequest) StoreResult(db ethdb.Database) {
   107  	db.Put(req.Hash[:], req.Data)
   108  }
   109  
   110  // BlockRequest is the ODR request type for retrieving block bodies
   111  type BlockRequest struct {
   112  	OdrRequest
   113  	Hash   common.Hash
   114  	Number uint64
   115  	Rlp    []byte
   116  }
   117  
   118  // StoreResult stores the retrieved data in local database
   119  func (req *BlockRequest) StoreResult(db ethdb.Database) {
   120  	rawdb.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp)
   121  }
   122  
   123  // ReceiptsRequest is the ODR request type for retrieving block bodies
   124  type ReceiptsRequest struct {
   125  	OdrRequest
   126  	Hash     common.Hash
   127  	Number   uint64
   128  	Receipts types.Receipts
   129  }
   130  
   131  // StoreResult stores the retrieved data in local database
   132  func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
   133  	rawdb.WriteReceipts(db, req.Hash, req.Number, req.Receipts)
   134  }
   135  
   136  // ChtRequest is the ODR request type for state/storage trie entries
   137  type ChtRequest struct {
   138  	OdrRequest
   139  	ChtNum, BlockNum uint64
   140  	ChtRoot          common.Hash
   141  	Header           *types.Header
   142  	Td               *big.Int
   143  	Proof            *NodeSet
   144  }
   145  
   146  // StoreResult stores the retrieved data in local database
   147  func (req *ChtRequest) StoreResult(db ethdb.Database) {
   148  	hash, num := req.Header.Hash(), req.Header.Number.Uint64()
   149  
   150  	rawdb.WriteHeader(db, req.Header)
   151  	rawdb.WriteTd(db, hash, num, req.Td)
   152  	rawdb.WriteCanonicalHash(db, hash, num)
   153  }
   154  
   155  // BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure
   156  type BloomRequest struct {
   157  	OdrRequest
   158  	BloomTrieNum   uint64
   159  	BitIdx         uint
   160  	SectionIdxList []uint64
   161  	BloomTrieRoot  common.Hash
   162  	BloomBits      [][]byte
   163  	Proofs         *NodeSet
   164  }
   165  
   166  // StoreResult stores the retrieved data in local database
   167  func (req *BloomRequest) StoreResult(db ethdb.Database) {
   168  	for i, sectionIdx := range req.SectionIdxList {
   169  		sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*BloomTrieFrequency-1)
   170  		// if we don't have the canonical hash stored for this section head number, we'll still store it under
   171  		// a key with a zero sectionHead. GetBloomBits will look there too if we still don't have the canonical
   172  		// hash. In the unlikely case we've retrieved the section head hash since then, we'll just retrieve the
   173  		// bit vector again from the network.
   174  		rawdb.WriteBloomBits(db, req.BitIdx, sectionIdx, sectionHead, req.BloomBits[i])
   175  	}
   176  }