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