github.com/puffscoin/go-puffscoin@v0.0.0-20190701205704-e48ad5c90fa1/light/odr.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-puffscoin 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/puffscoin/go-puffscoin/common"
    25  	"github.com/puffscoin/go-puffscoin/core"
    26  	"github.com/puffscoin/go-puffscoin/core/rawdb"
    27  	"github.com/puffscoin/go-puffscoin/core/types"
    28  	"github.com/puffscoin/go-puffscoin/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  	OdrRequest
    86  	Id    *TrieID
    87  	Key   []byte
    88  	Proof *NodeSet
    89  }
    90  
    91  // StoreResult stores the retrieved data in local database
    92  func (req *TrieRequest) StoreResult(db ethdb.Database) {
    93  	req.Proof.Store(db)
    94  }
    95  
    96  // CodeRequest is the ODR request type for retrieving contract code
    97  type CodeRequest struct {
    98  	OdrRequest
    99  	Id   *TrieID // references storage trie of the account
   100  	Hash common.Hash
   101  	Data []byte
   102  }
   103  
   104  // StoreResult stores the retrieved data in local database
   105  func (req *CodeRequest) StoreResult(db ethdb.Database) {
   106  	db.Put(req.Hash[:], req.Data)
   107  }
   108  
   109  // BlockRequest is the ODR request type for retrieving block bodies
   110  type BlockRequest struct {
   111  	OdrRequest
   112  	Hash   common.Hash
   113  	Number uint64
   114  	Rlp    []byte
   115  }
   116  
   117  // StoreResult stores the retrieved data in local database
   118  func (req *BlockRequest) StoreResult(db ethdb.Database) {
   119  	rawdb.WriteBodyRLP(db, req.Hash, req.Number, req.Rlp)
   120  }
   121  
   122  // ReceiptsRequest is the ODR request type for retrieving block bodies
   123  type ReceiptsRequest struct {
   124  	OdrRequest
   125  	Hash     common.Hash
   126  	Number   uint64
   127  	Receipts types.Receipts
   128  }
   129  
   130  // StoreResult stores the retrieved data in local database
   131  func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
   132  	rawdb.WriteReceipts(db, req.Hash, req.Number, req.Receipts)
   133  }
   134  
   135  // ChtRequest is the ODR request type for state/storage trie entries
   136  type ChtRequest struct {
   137  	OdrRequest
   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  
   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  	Config           *IndexerConfig
   159  	BloomTrieNum     uint64
   160  	BitIdx           uint
   161  	SectionIndexList []uint64
   162  	BloomTrieRoot    common.Hash
   163  	BloomBits        [][]byte
   164  	Proofs           *NodeSet
   165  }
   166  
   167  // StoreResult stores the retrieved data in local database
   168  func (req *BloomRequest) StoreResult(db ethdb.Database) {
   169  	for i, sectionIdx := range req.SectionIndexList {
   170  		sectionHead := rawdb.ReadCanonicalHash(db, (sectionIdx+1)*req.Config.BloomTrieSize-1)
   171  		// if we don't have the canonical hash stored for this section head number, we'll still store it under
   172  		// a key with a zero sectionHead. GetBloomBits will look there too if we still don't have the canonical
   173  		// hash. In the unlikely case we've retrieved the section head hash since then, we'll just retrieve the
   174  		// bit vector again from the network.
   175  		rawdb.WriteBloomBits(db, req.BitIdx, sectionIdx, sectionHead, req.BloomBits[i])
   176  	}
   177  }