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