github.com/core-coin/go-core/v2@v2.1.9/les/odr.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package les
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/core-coin/go-core/v2/xcbdb"
    24  
    25  	"github.com/core-coin/go-core/v2/common/mclock"
    26  	"github.com/core-coin/go-core/v2/core"
    27  	"github.com/core-coin/go-core/v2/light"
    28  )
    29  
    30  // LesOdr implements light.OdrBackend
    31  type LesOdr struct {
    32  	db                                         xcbdb.Database
    33  	indexerConfig                              *light.IndexerConfig
    34  	chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer
    35  	retriever                                  *retrieveManager
    36  	stop                                       chan struct{}
    37  }
    38  
    39  func NewLesOdr(db xcbdb.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() xcbdb.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  	MsgBlockHeaders = iota
    87  	MsgBlockBodies
    88  	MsgCode
    89  	MsgReceipts
    90  	MsgProofsV2
    91  	MsgHelperTrieProofs
    92  	MsgTxStatus
    93  )
    94  
    95  // Msg encodes a LES message that delivers reply data for a request
    96  type Msg struct {
    97  	MsgType int
    98  	ReqID   uint64
    99  	Obj     interface{}
   100  }
   101  
   102  // Retrieve tries to fetch an object from the LES network.
   103  // If the network retrieval was successful, it stores the object in local db.
   104  func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) {
   105  	lreq := LesRequest(req)
   106  
   107  	reqID := genReqID()
   108  	rq := &distReq{
   109  		getCost: func(dp distPeer) uint64 {
   110  			return lreq.GetCost(dp.(*serverPeer))
   111  		},
   112  		canSend: func(dp distPeer) bool {
   113  			p := dp.(*serverPeer)
   114  			if !p.onlyAnnounce {
   115  				return lreq.CanSend(p)
   116  			}
   117  			return false
   118  		},
   119  		request: func(dp distPeer) func() {
   120  			p := dp.(*serverPeer)
   121  			cost := lreq.GetCost(p)
   122  			p.fcServer.QueuedRequest(reqID, cost)
   123  			return func() { lreq.Request(reqID, p) }
   124  		},
   125  	}
   126  
   127  	defer func(sent mclock.AbsTime) {
   128  		if err != nil {
   129  			return
   130  		}
   131  		requestRTT.Update(time.Duration(mclock.Now() - sent))
   132  	}(mclock.Now())
   133  
   134  	if err := odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err != nil {
   135  		return err
   136  	}
   137  	req.StoreResult(odr.db)
   138  	return nil
   139  }