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