github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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  	"sync"
    23  
    24  	"github.com/ethereum-optimism/optimism/l2geth/common"
    25  	"github.com/ethereum-optimism/optimism/l2geth/core"
    26  	"github.com/ethereum-optimism/optimism/l2geth/core/rawdb"
    27  	"github.com/ethereum-optimism/optimism/l2geth/core/types"
    28  	"github.com/ethereum-optimism/optimism/l2geth/eth"
    29  	"github.com/ethereum-optimism/optimism/l2geth/ethdb"
    30  	"github.com/ethereum-optimism/optimism/l2geth/les/checkpointoracle"
    31  	"github.com/ethereum-optimism/optimism/l2geth/light"
    32  	"github.com/ethereum-optimism/optimism/l2geth/p2p"
    33  	"github.com/ethereum-optimism/optimism/l2geth/p2p/discv5"
    34  	"github.com/ethereum-optimism/optimism/l2geth/p2p/enode"
    35  	"github.com/ethereum-optimism/optimism/l2geth/params"
    36  )
    37  
    38  func errResp(code errCode, format string, v ...interface{}) error {
    39  	return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
    40  }
    41  
    42  func lesTopic(genesisHash common.Hash, protocolVersion uint) discv5.Topic {
    43  	var name string
    44  	switch protocolVersion {
    45  	case lpv2:
    46  		name = "LES2"
    47  	default:
    48  		panic(nil)
    49  	}
    50  	return discv5.Topic(name + "@" + common.Bytes2Hex(genesisHash.Bytes()[0:8]))
    51  }
    52  
    53  type chainReader interface {
    54  	CurrentHeader() *types.Header
    55  }
    56  
    57  // lesCommons contains fields needed by both server and client.
    58  type lesCommons struct {
    59  	genesis                      common.Hash
    60  	config                       *eth.Config
    61  	chainConfig                  *params.ChainConfig
    62  	iConfig                      *light.IndexerConfig
    63  	chainDb                      ethdb.Database
    64  	peers                        *peerSet
    65  	chainReader                  chainReader
    66  	chtIndexer, bloomTrieIndexer *core.ChainIndexer
    67  	oracle                       *checkpointoracle.CheckpointOracle
    68  
    69  	closeCh chan struct{}
    70  	wg      sync.WaitGroup
    71  }
    72  
    73  // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
    74  // known about the host peer.
    75  type NodeInfo struct {
    76  	Network    uint64                   `json:"network"`    // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
    77  	Difficulty *big.Int                 `json:"difficulty"` // Total difficulty of the host's blockchain
    78  	Genesis    common.Hash              `json:"genesis"`    // SHA3 hash of the host's genesis block
    79  	Config     *params.ChainConfig      `json:"config"`     // Chain configuration for the fork rules
    80  	Head       common.Hash              `json:"head"`       // SHA3 hash of the host's best owned block
    81  	CHT        params.TrustedCheckpoint `json:"cht"`        // Trused CHT checkpoint for fast catchup
    82  }
    83  
    84  // makeProtocols creates protocol descriptors for the given LES versions.
    85  func (c *lesCommons) makeProtocols(versions []uint, runPeer func(version uint, p *p2p.Peer, rw p2p.MsgReadWriter) error, peerInfo func(id enode.ID) interface{}) []p2p.Protocol {
    86  	protos := make([]p2p.Protocol, len(versions))
    87  	for i, version := range versions {
    88  		version := version
    89  		protos[i] = p2p.Protocol{
    90  			Name:     "les",
    91  			Version:  version,
    92  			Length:   ProtocolLengths[version],
    93  			NodeInfo: c.nodeInfo,
    94  			Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
    95  				return runPeer(version, peer, rw)
    96  			},
    97  			PeerInfo: peerInfo,
    98  		}
    99  	}
   100  	return protos
   101  }
   102  
   103  // nodeInfo retrieves some protocol metadata about the running host node.
   104  func (c *lesCommons) nodeInfo() interface{} {
   105  	head := c.chainReader.CurrentHeader()
   106  	hash := head.Hash()
   107  	return &NodeInfo{
   108  		Network:    c.config.NetworkId,
   109  		Difficulty: rawdb.ReadTd(c.chainDb, hash, head.Number.Uint64()),
   110  		Genesis:    c.genesis,
   111  		Config:     c.chainConfig,
   112  		Head:       hash,
   113  		CHT:        c.latestLocalCheckpoint(),
   114  	}
   115  }
   116  
   117  // latestLocalCheckpoint finds the common stored section index and returns a set
   118  // of post-processed trie roots (CHT and BloomTrie) associated with the appropriate
   119  // section index and head hash as a local checkpoint package.
   120  func (c *lesCommons) latestLocalCheckpoint() params.TrustedCheckpoint {
   121  	sections, _, _ := c.chtIndexer.Sections()
   122  	sections2, _, _ := c.bloomTrieIndexer.Sections()
   123  	// Cap the section index if the two sections are not consistent.
   124  	if sections > sections2 {
   125  		sections = sections2
   126  	}
   127  	if sections == 0 {
   128  		// No checkpoint information can be provided.
   129  		return params.TrustedCheckpoint{}
   130  	}
   131  	return c.localCheckpoint(sections - 1)
   132  }
   133  
   134  // localCheckpoint returns a set of post-processed trie roots (CHT and BloomTrie)
   135  // associated with the appropriate head hash by specific section index.
   136  //
   137  // The returned checkpoint is only the checkpoint generated by the local indexers,
   138  // not the stable checkpoint registered in the registrar contract.
   139  func (c *lesCommons) localCheckpoint(index uint64) params.TrustedCheckpoint {
   140  	sectionHead := c.chtIndexer.SectionHead(index)
   141  	return params.TrustedCheckpoint{
   142  		SectionIndex: index,
   143  		SectionHead:  sectionHead,
   144  		CHTRoot:      light.GetChtRoot(c.chainDb, index, sectionHead),
   145  		BloomRoot:    light.GetBloomTrieRoot(c.chainDb, index, sectionHead),
   146  	}
   147  }