github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"math/big"
    23  
    24  	"github.com/kisexp/xdchain/common"
    25  	"github.com/kisexp/xdchain/core"
    26  	"github.com/kisexp/xdchain/core/rawdb"
    27  	"github.com/kisexp/xdchain/core/types"
    28  	"github.com/kisexp/xdchain/ethdb"
    29  )
    30  
    31  // NoOdr is the default context passed to an ODR capable function when the ODR
    32  // service is not required.
    33  var NoOdr = context.Background()
    34  
    35  // ErrNoPeers is returned if no peers capable of serving a queued request are available
    36  var ErrNoPeers = errors.New("no suitable peers available")
    37  
    38  // OdrBackend is an interface to a backend service that handles ODR retrievals type
    39  type OdrBackend interface {
    40  	Database() ethdb.Database
    41  	ChtIndexer() *core.ChainIndexer
    42  	BloomTrieIndexer() *core.ChainIndexer
    43  	BloomIndexer() *core.ChainIndexer
    44  	Retrieve(ctx context.Context, req OdrRequest) error
    45  	IndexerConfig() *IndexerConfig
    46  }
    47  
    48  // OdrRequest is an interface for retrieval requests
    49  type OdrRequest interface {
    50  	StoreResult(db ethdb.Database)
    51  }
    52  
    53  // TrieID identifies a state or account storage trie
    54  type TrieID struct {
    55  	BlockHash, Root common.Hash
    56  	BlockNumber     uint64
    57  	AccKey          []byte
    58  }
    59  
    60  // StateTrieID returns a TrieID for a state trie belonging to a certain block
    61  // header.
    62  func StateTrieID(header *types.Header) *TrieID {
    63  	return &TrieID{
    64  		BlockHash:   header.Hash(),
    65  		BlockNumber: header.Number.Uint64(),
    66  		AccKey:      nil,
    67  		Root:        header.Root,
    68  	}
    69  }
    70  
    71  // StorageTrieID returns a TrieID for a contract storage trie at a given account
    72  // of a given state trie. It also requires the root hash of the trie for
    73  // checking Merkle proofs.
    74  func StorageTrieID(state *TrieID, addrHash, root common.Hash) *TrieID {
    75  	return &TrieID{
    76  		BlockHash:   state.BlockHash,
    77  		BlockNumber: state.BlockNumber,
    78  		AccKey:      addrHash[:],
    79  		Root:        root,
    80  	}
    81  }
    82  
    83  // TrieRequest is the ODR request type for state/storage trie entries
    84  type TrieRequest struct {
    85  	Id    *TrieID
    86  	Key   []byte
    87  	Proof *NodeSet
    88  }
    89  
    90  // StoreResult stores the retrieved data in local database
    91  func (req *TrieRequest) StoreResult(db ethdb.Database) {
    92  	req.Proof.Store(db)
    93  }
    94  
    95  // CodeRequest is the ODR request type for retrieving contract code
    96  type CodeRequest struct {
    97  	Id   *TrieID // references storage trie of the account
    98  	Hash common.Hash
    99  	Data []byte
   100  }
   101  
   102  // StoreResult stores the retrieved data in local database
   103  func (req *CodeRequest) StoreResult(db ethdb.Database) {
   104  	rawdb.WriteCode(db, req.Hash, req.Data)
   105  }
   106  
   107  // BlockRequest is the ODR request type for retrieving block bodies
   108  type BlockRequest struct {
   109  	Hash   common.Hash
   110  	Number uint64
   111  	Header *types.Header
   112  	Rlp    []byte
   113  }
   114  
   115  // StoreResult stores the retrieved data in local database
   116  func (req *BlockRequest) StoreResult(db ethdb.Database) {
   117  	rawdb.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp)
   118  }
   119  
   120  // ReceiptsRequest is the ODR request type for retrieving receipts.
   121  type ReceiptsRequest struct {
   122  	Untrusted bool // Indicator whether the result retrieved is trusted or not
   123  	Hash      common.Hash
   124  	Number    uint64
   125  	Header    *types.Header
   126  	Receipts  types.Receipts
   127  }
   128  
   129  // StoreResult stores the retrieved data in local database
   130  func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
   131  	if !req.Untrusted {
   132  		rawdb.WriteReceipts(db, req.Hash, req.Number, req.Receipts)
   133  	}
   134  }
   135  
   136  // ChtRequest is the ODR request type for retrieving header by Canonical Hash Trie
   137  type ChtRequest struct {
   138  	Config           *IndexerConfig
   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  	rawdb.WriteHeader(db, req.Header)
   150  	rawdb.WriteTd(db, hash, num, req.Td)
   151  	rawdb.WriteCanonicalHash(db, hash, num)
   152  }
   153  
   154  // BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure
   155  type BloomRequest struct {
   156  	OdrRequest
   157  	Config           *IndexerConfig
   158  	BloomTrieNum     uint64
   159  	BitIdx           uint
   160  	SectionIndexList []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.SectionIndexList {
   169  		sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*req.Config.BloomTrieSize-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  }
   177  
   178  // TxStatus describes the status of a transaction
   179  type TxStatus struct {
   180  	Status core.TxStatus
   181  	Lookup *rawdb.LegacyTxLookupEntry `rlp:"nil"`
   182  	Error  string
   183  }
   184  
   185  // TxStatusRequest is the ODR request type for retrieving transaction status
   186  type TxStatusRequest struct {
   187  	Hashes []common.Hash
   188  	Status []TxStatus
   189  }
   190  
   191  // StoreResult stores the retrieved data in local database
   192  func (req *TxStatusRequest) StoreResult(db ethdb.Database) {}