github.com/jartcoin/go-jartdiuma@v0.0.0-20210708013502-b71bfe42bfc3/les/commons.go (about)

     1  // Copyright 2018 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  	"fmt"
    21  	"math/big"
    22  
    23  	"github.com/jartcoin/go-jartdiuma/common"
    24  	"github.com/jartcoin/go-jartdiuma/core"
    25  	"github.com/jartcoin/go-jartdiuma/eth"
    26  	"github.com/jartcoin/go-jartdiuma/ethdb"
    27  	"github.com/jartcoin/go-jartdiuma/light"
    28  	"github.com/jartcoin/go-jartdiuma/p2p"
    29  	"github.com/jartcoin/go-jartdiuma/p2p/enode"
    30  	"github.com/jartcoin/go-jartdiuma/params"
    31  )
    32  
    33  // lesCommons contains fields needed by both server and client.
    34  type lesCommons struct {
    35  	config                       *eth.Config
    36  	iConfig                      *light.IndexerConfig
    37  	chainDb                      ethdb.Database
    38  	protocolManager              *ProtocolManager
    39  	chtIndexer, bloomTrieIndexer *core.ChainIndexer
    40  }
    41  
    42  // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
    43  // known about the host peer.
    44  type NodeInfo struct {
    45  	Network    uint64                   `json:"network"`    // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
    46  	Difficulty *big.Int                 `json:"difficulty"` // Total difficulty of the host's blockchain
    47  	Genesis    common.Hash              `json:"genesis"`    // SHA3 hash of the host's genesis block
    48  	Config     *params.ChainConfig      `json:"config"`     // Chain configuration for the fork rules
    49  	Head       common.Hash              `json:"head"`       // SHA3 hash of the host's best owned block
    50  	CHT        params.TrustedCheckpoint `json:"cht"`        // Trused CHT checkpoint for fast catchup
    51  }
    52  
    53  // makeProtocols creates protocol descriptors for the given LES versions.
    54  func (c *lesCommons) makeProtocols(versions []uint) []p2p.Protocol {
    55  	protos := make([]p2p.Protocol, len(versions))
    56  	for i, version := range versions {
    57  		version := version
    58  		protos[i] = p2p.Protocol{
    59  			Name:     "les",
    60  			Version:  version,
    61  			Length:   ProtocolLengths[version],
    62  			NodeInfo: c.nodeInfo,
    63  			Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
    64  				return c.protocolManager.runPeer(version, p, rw)
    65  			},
    66  			PeerInfo: func(id enode.ID) interface{} {
    67  				if p := c.protocolManager.peers.Peer(fmt.Sprintf("%x", id.Bytes())); p != nil {
    68  					return p.Info()
    69  				}
    70  				return nil
    71  			},
    72  		}
    73  	}
    74  	return protos
    75  }
    76  
    77  // nodeInfo retrieves some protocol metadata about the running host node.
    78  func (c *lesCommons) nodeInfo() interface{} {
    79  	chain := c.protocolManager.blockchain
    80  	head := chain.CurrentHeader()
    81  	hash := head.Hash()
    82  	return &NodeInfo{
    83  		Network:    c.config.NetworkId,
    84  		Difficulty: chain.GetTd(hash, head.Number.Uint64()),
    85  		Genesis:    chain.Genesis().Hash(),
    86  		Config:     chain.Config(),
    87  		Head:       chain.CurrentHeader().Hash(),
    88  		CHT:        c.latestLocalCheckpoint(),
    89  	}
    90  }
    91  
    92  // latestLocalCheckpoint finds the common stored section index and returns a set of
    93  // post-processed trie roots (CHT and BloomTrie) associated with
    94  // the appropriate section index and head hash as a local checkpoint package.
    95  func (c *lesCommons) latestLocalCheckpoint() params.TrustedCheckpoint {
    96  	sections, _, _ := c.chtIndexer.Sections()
    97  	sections2, _, _ := c.bloomTrieIndexer.Sections()
    98  	// Cap the section index if the two sections are not consistent.
    99  	if sections > sections2 {
   100  		sections = sections2
   101  	}
   102  	if sections == 0 {
   103  		// No checkpoint information can be provided.
   104  		return params.TrustedCheckpoint{}
   105  	}
   106  	return c.getLocalCheckpoint(sections - 1)
   107  }
   108  
   109  // getLocalCheckpoint returns a set of post-processed trie roots (CHT and BloomTrie)
   110  // associated with the appropriate head hash by specific section index.
   111  //
   112  // The returned checkpoint is only the checkpoint generated by the local indexers,
   113  // not the stable checkpoint registered in the registrar contract.
   114  func (c *lesCommons) getLocalCheckpoint(index uint64) params.TrustedCheckpoint {
   115  	sectionHead := c.chtIndexer.SectionHead(index)
   116  	return params.TrustedCheckpoint{
   117  		SectionIndex: index,
   118  		SectionHead:  sectionHead,
   119  		CHTRoot:      light.GetChtRoot(c.chainDb, index, sectionHead),
   120  		BloomRoot:    light.GetBloomTrieRoot(c.chainDb, index, sectionHead),
   121  	}
   122  }