github.com/tdcblockchain/tdcblockchain@v0.0.0-20191111034745-805c65ade158/les/odr.go (about)

     1  // Copyright 2016 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 les
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/common/mclock"
    24  	"github.com/ethereum/go-ethereum/core"
    25  	"github.com/ethereum/go-ethereum/ethdb"
    26  	"github.com/ethereum/go-ethereum/light"
    27  	"github.com/ethereum/go-ethereum/log"
    28  )
    29  
    30  // LesOdr implements light.OdrBackend
    31  type LesOdr struct {
    32  	db                                         ethdb.Database
    33  	indexerConfig                              *light.IndexerConfig
    34  	chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer
    35  	retriever                                  *retrieveManager
    36  	stop                                       chan struct{}
    37  }
    38  
    39  func NewLesOdr(db ethdb.Database, config *light.IndexerConfig, retriever *retrieveManager) *LesOdr {
    40  	return &LesOdr{
    41  		db:            db,
    42  		indexerConfig: config,
    43  		retriever:     retriever,
    44  		stop:          make(chan struct{}),
    45  	}
    46  }
    47  
    48  // Stop cancels all pending retrievals
    49  func (odr *LesOdr) Stop() {
    50  	close(odr.stop)
    51  }
    52  
    53  // Database returns the backing database
    54  func (odr *LesOdr) Database() ethdb.Database {
    55  	return odr.db
    56  }
    57  
    58  // SetIndexers adds the necessary chain indexers to the ODR backend
    59  func (odr *LesOdr) SetIndexers(chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer) {
    60  	odr.chtIndexer = chtIndexer
    61  	odr.bloomTrieIndexer = bloomTrieIndexer
    62  	odr.bloomIndexer = bloomIndexer
    63  }
    64  
    65  // ChtIndexer returns the CHT chain indexer
    66  func (odr *LesOdr) ChtIndexer() *core.ChainIndexer {
    67  	return odr.chtIndexer
    68  }
    69  
    70  // BloomTrieIndexer returns the bloom trie chain indexer
    71  func (odr *LesOdr) BloomTrieIndexer() *core.ChainIndexer {
    72  	return odr.bloomTrieIndexer
    73  }
    74  
    75  // BloomIndexer returns the bloombits chain indexer
    76  func (odr *LesOdr) BloomIndexer() *core.ChainIndexer {
    77  	return odr.bloomIndexer
    78  }
    79  
    80  // IndexerConfig returns the indexer config.
    81  func (odr *LesOdr) IndexerConfig() *light.IndexerConfig {
    82  	return odr.indexerConfig
    83  }
    84  
    85  const (
    86  	MsgBlockBodies = iota
    87  	MsgCode
    88  	MsgReceipts
    89  	MsgProofsV2
    90  	MsgHelperTrieProofs
    91  	MsgTxStatus
    92  )
    93  
    94  // Msg encodes a LES message that delivers reply data for a request
    95  type Msg struct {
    96  	MsgType int
    97  	ReqID   uint64
    98  	Obj     interface{}
    99  }
   100  
   101  // Retrieve tries to fetch an object from the LES network.
   102  // If the network retrieval was successful, it stores the object in local db.
   103  func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) {
   104  	lreq := LesRequest(req)
   105  
   106  	reqID := genReqID()
   107  	rq := &distReq{
   108  		getCost: func(dp distPeer) uint64 {
   109  			return lreq.GetCost(dp.(*peer))
   110  		},
   111  		canSend: func(dp distPeer) bool {
   112  			p := dp.(*peer)
   113  			if !p.onlyAnnounce {
   114  				return lreq.CanSend(p)
   115  			}
   116  			return false
   117  		},
   118  		request: func(dp distPeer) func() {
   119  			p := dp.(*peer)
   120  			cost := lreq.GetCost(p)
   121  			p.fcServer.QueuedRequest(reqID, cost)
   122  			return func() { lreq.Request(reqID, p) }
   123  		},
   124  	}
   125  	sent := mclock.Now()
   126  	if err = odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err == nil {
   127  		// retrieved from network, store in db
   128  		req.StoreResult(odr.db)
   129  		requestRTT.Update(time.Duration(mclock.Now() - sent))
   130  	} else {
   131  		log.Debug("Failed to retrieve data from network", "err", err)
   132  	}
   133  	return
   134  }