github.com/aitimate-0/go-ethereum@v1.9.7/cmd/checkpoint-admin/common.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU 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  // go-ethereum 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 General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"strconv"
    21  
    22  	"github.com/ethereum/go-ethereum/accounts"
    23  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    24  	"github.com/ethereum/go-ethereum/accounts/external"
    25  	"github.com/ethereum/go-ethereum/cmd/utils"
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/contracts/checkpointoracle"
    28  	"github.com/ethereum/go-ethereum/ethclient"
    29  	"github.com/ethereum/go-ethereum/params"
    30  	"github.com/ethereum/go-ethereum/rpc"
    31  	"gopkg.in/urfave/cli.v1"
    32  )
    33  
    34  // newClient creates a client with specified remote URL.
    35  func newClient(ctx *cli.Context) *ethclient.Client {
    36  	client, err := ethclient.Dial(ctx.GlobalString(nodeURLFlag.Name))
    37  	if err != nil {
    38  		utils.Fatalf("Failed to connect to Ethereum node: %v", err)
    39  	}
    40  	return client
    41  }
    42  
    43  // newRPCClient creates a rpc client with specified node URL.
    44  func newRPCClient(url string) *rpc.Client {
    45  	client, err := rpc.Dial(url)
    46  	if err != nil {
    47  		utils.Fatalf("Failed to connect to Ethereum node: %v", err)
    48  	}
    49  	return client
    50  }
    51  
    52  // getContractAddr retrieves the register contract address through
    53  // rpc request.
    54  func getContractAddr(client *rpc.Client) common.Address {
    55  	var addr string
    56  	if err := client.Call(&addr, "les_getCheckpointContractAddress"); err != nil {
    57  		utils.Fatalf("Failed to fetch checkpoint oracle address: %v", err)
    58  	}
    59  	return common.HexToAddress(addr)
    60  }
    61  
    62  // getCheckpoint retrieves the specified checkpoint or the latest one
    63  // through rpc request.
    64  func getCheckpoint(ctx *cli.Context, client *rpc.Client) *params.TrustedCheckpoint {
    65  	var checkpoint *params.TrustedCheckpoint
    66  
    67  	if ctx.GlobalIsSet(indexFlag.Name) {
    68  		var result [3]string
    69  		index := uint64(ctx.GlobalInt64(indexFlag.Name))
    70  		if err := client.Call(&result, "les_getCheckpoint", index); err != nil {
    71  			utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
    72  		}
    73  		checkpoint = &params.TrustedCheckpoint{
    74  			SectionIndex: index,
    75  			SectionHead:  common.HexToHash(result[0]),
    76  			CHTRoot:      common.HexToHash(result[1]),
    77  			BloomRoot:    common.HexToHash(result[2]),
    78  		}
    79  	} else {
    80  		var result [4]string
    81  		err := client.Call(&result, "les_latestCheckpoint")
    82  		if err != nil {
    83  			utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
    84  		}
    85  		index, err := strconv.ParseUint(result[0], 0, 64)
    86  		if err != nil {
    87  			utils.Fatalf("Failed to parse checkpoint index %v", err)
    88  		}
    89  		checkpoint = &params.TrustedCheckpoint{
    90  			SectionIndex: index,
    91  			SectionHead:  common.HexToHash(result[1]),
    92  			CHTRoot:      common.HexToHash(result[2]),
    93  			BloomRoot:    common.HexToHash(result[3]),
    94  		}
    95  	}
    96  	return checkpoint
    97  }
    98  
    99  // newContract creates a registrar contract instance with specified
   100  // contract address or the default contracts for mainnet or testnet.
   101  func newContract(client *rpc.Client) (common.Address, *checkpointoracle.CheckpointOracle) {
   102  	addr := getContractAddr(client)
   103  	if addr == (common.Address{}) {
   104  		utils.Fatalf("No specified registrar contract address")
   105  	}
   106  	contract, err := checkpointoracle.NewCheckpointOracle(addr, ethclient.NewClient(client))
   107  	if err != nil {
   108  		utils.Fatalf("Failed to setup registrar contract %s: %v", addr, err)
   109  	}
   110  	return addr, contract
   111  }
   112  
   113  // newClefSigner sets up a clef backend and returns a clef transaction signer.
   114  func newClefSigner(ctx *cli.Context) *bind.TransactOpts {
   115  	clef, err := external.NewExternalSigner(ctx.String(clefURLFlag.Name))
   116  	if err != nil {
   117  		utils.Fatalf("Failed to create clef signer %v", err)
   118  	}
   119  	return bind.NewClefTransactor(clef, accounts.Account{Address: common.HexToAddress(ctx.String(signerFlag.Name))})
   120  }