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