github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/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/ethxdao/go-ethereum/accounts"
    23  	"github.com/ethxdao/go-ethereum/accounts/abi/bind"
    24  	"github.com/ethxdao/go-ethereum/accounts/external"
    25  	"github.com/ethxdao/go-ethereum/cmd/utils"
    26  	"github.com/ethxdao/go-ethereum/common"
    27  	"github.com/ethxdao/go-ethereum/contracts/checkpointoracle"
    28  	"github.com/ethxdao/go-ethereum/ethclient"
    29  	"github.com/ethxdao/go-ethereum/params"
    30  	"github.com/ethxdao/go-ethereum/rpc"
    31  )
    32  
    33  // newClient creates a client with specified remote URL.
    34  func newClient(ctx *cli.Context) *ethclient.Client {
    35  	client, err := ethclient.Dial(ctx.String(nodeURLFlag.Name))
    36  	if err != nil {
    37  		utils.Fatalf("Failed to connect to Ethereum node: %v", err)
    38  	}
    39  	return client
    40  }
    41  
    42  // newRPCClient creates a rpc client with specified node URL.
    43  func newRPCClient(url string) *rpc.Client {
    44  	client, err := rpc.Dial(url)
    45  	if err != nil {
    46  		utils.Fatalf("Failed to connect to Ethereum node: %v", err)
    47  	}
    48  	return client
    49  }
    50  
    51  // getContractAddr retrieves the register contract address through
    52  // rpc request.
    53  func getContractAddr(client *rpc.Client) common.Address {
    54  	var addr string
    55  	if err := client.Call(&addr, "les_getCheckpointContractAddress"); err != nil {
    56  		utils.Fatalf("Failed to fetch checkpoint oracle address: %v", err)
    57  	}
    58  	return common.HexToAddress(addr)
    59  }
    60  
    61  // getCheckpoint retrieves the specified checkpoint or the latest one
    62  // through rpc request.
    63  func getCheckpoint(ctx *cli.Context, client *rpc.Client) *params.TrustedCheckpoint {
    64  	var checkpoint *params.TrustedCheckpoint
    65  
    66  	if ctx.IsSet(indexFlag.Name) {
    67  		var result [3]string
    68  		index := uint64(ctx.Int64(indexFlag.Name))
    69  		if err := client.Call(&result, "les_getCheckpoint", index); err != nil {
    70  			utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
    71  		}
    72  		checkpoint = &params.TrustedCheckpoint{
    73  			SectionIndex: index,
    74  			SectionHead:  common.HexToHash(result[0]),
    75  			CHTRoot:      common.HexToHash(result[1]),
    76  			BloomRoot:    common.HexToHash(result[2]),
    77  		}
    78  	} else {
    79  		var result [4]string
    80  		err := client.Call(&result, "les_latestCheckpoint")
    81  		if err != nil {
    82  			utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
    83  		}
    84  		index, err := strconv.ParseUint(result[0], 0, 64)
    85  		if err != nil {
    86  			utils.Fatalf("Failed to parse checkpoint index %v", err)
    87  		}
    88  		checkpoint = &params.TrustedCheckpoint{
    89  			SectionIndex: index,
    90  			SectionHead:  common.HexToHash(result[1]),
    91  			CHTRoot:      common.HexToHash(result[2]),
    92  			BloomRoot:    common.HexToHash(result[3]),
    93  		}
    94  	}
    95  	return checkpoint
    96  }
    97  
    98  // newContract creates a registrar contract instance with specified
    99  // contract address or the default contracts for mainnet or testnet.
   100  func newContract(client *rpc.Client) (common.Address, *checkpointoracle.CheckpointOracle) {
   101  	addr := getContractAddr(client)
   102  	if addr == (common.Address{}) {
   103  		utils.Fatalf("No specified registrar contract address")
   104  	}
   105  	contract, err := checkpointoracle.NewCheckpointOracle(addr, ethclient.NewClient(client))
   106  	if err != nil {
   107  		utils.Fatalf("Failed to setup registrar contract %s: %v", addr, err)
   108  	}
   109  	return addr, contract
   110  }
   111  
   112  // newClefSigner sets up a clef backend and returns a clef transaction signer.
   113  func newClefSigner(ctx *cli.Context) *bind.TransactOpts {
   114  	clef, err := external.NewExternalSigner(ctx.String(clefURLFlag.Name))
   115  	if err != nil {
   116  		utils.Fatalf("Failed to create clef signer %v", err)
   117  	}
   118  	return bind.NewClefTransactor(clef, accounts.Account{Address: common.HexToAddress(ctx.String(signerFlag.Name))})
   119  }