github.com/theQRL/go-zond@v0.1.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/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/core"
    26  	"github.com/theQRL/go-zond/core/rawdb"
    27  	"github.com/theQRL/go-zond/core/types"
    28  	"github.com/theQRL/go-zond/zond/ethconfig"
    29  	"github.com/theQRL/go-zond/zonddb"
    30  	"github.com/theQRL/go-zond/light"
    31  	"github.com/theQRL/go-zond/p2p"
    32  	"github.com/theQRL/go-zond/p2p/enode"
    33  	"github.com/theQRL/go-zond/params"
    34  )
    35  
    36  func errResp(code errCode, format string, v ...interface{}) error {
    37  	return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
    38  }
    39  
    40  type chainReader interface {
    41  	CurrentHeader() *types.Header
    42  }
    43  
    44  // lesCommons contains fields needed by both server and client.
    45  type lesCommons struct {
    46  	genesis                      common.Hash
    47  	config                       *ethconfig.Config
    48  	chainConfig                  *params.ChainConfig
    49  	iConfig                      *light.IndexerConfig
    50  	chainDb, lesDb               zonddb.Database
    51  	chainReader                  chainReader
    52  	chtIndexer, bloomTrieIndexer *core.ChainIndexer
    53  
    54  	closeCh chan struct{}
    55  	wg      sync.WaitGroup
    56  }
    57  
    58  // NodeInfo represents a short summary of the Ethereum sub-protocol metadata
    59  // known about the host peer.
    60  type NodeInfo struct {
    61  	Network    uint64              `json:"network"`    // Ethereum network ID (1=Mainnet, Goerli=5)
    62  	Difficulty *big.Int            `json:"difficulty"` // Total difficulty of the host's blockchain
    63  	Genesis    common.Hash         `json:"genesis"`    // SHA3 hash of the host's genesis block
    64  	Config     *params.ChainConfig `json:"config"`     // Chain configuration for the fork rules
    65  	Head       common.Hash         `json:"head"`       // SHA3 hash of the host's best owned block
    66  }
    67  
    68  // makeProtocols creates protocol descriptors for the given LES versions.
    69  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 {
    70  	protos := make([]p2p.Protocol, len(versions))
    71  	for i, version := range versions {
    72  		version := version
    73  		protos[i] = p2p.Protocol{
    74  			Name:     "les",
    75  			Version:  version,
    76  			Length:   ProtocolLengths[version],
    77  			NodeInfo: c.nodeInfo,
    78  			Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
    79  				return runPeer(version, peer, rw)
    80  			},
    81  			PeerInfo:       peerInfo,
    82  			DialCandidates: dialCandidates,
    83  		}
    84  	}
    85  	return protos
    86  }
    87  
    88  // nodeInfo retrieves some protocol metadata about the running host node.
    89  func (c *lesCommons) nodeInfo() interface{} {
    90  	head := c.chainReader.CurrentHeader()
    91  	hash := head.Hash()
    92  	return &NodeInfo{
    93  		Network:    c.config.NetworkId,
    94  		Difficulty: rawdb.ReadTd(c.chainDb, hash, head.Number.Uint64()),
    95  		Genesis:    c.genesis,
    96  		Config:     c.chainConfig,
    97  		Head:       hash,
    98  	}
    99  }