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