github.com/core-coin/go-core/v2@v2.1.9/cmd/checkpoint-admin/common.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of go-core.
     3  //
     4  // go-core 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-core 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-core. 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/core-coin/go-core/v2/accounts"
    25  	"github.com/core-coin/go-core/v2/accounts/abi/bind"
    26  	"github.com/core-coin/go-core/v2/accounts/external"
    27  	"github.com/core-coin/go-core/v2/cmd/utils"
    28  	"github.com/core-coin/go-core/v2/common"
    29  	"github.com/core-coin/go-core/v2/contracts/checkpointoracle"
    30  	"github.com/core-coin/go-core/v2/params"
    31  	"github.com/core-coin/go-core/v2/rpc"
    32  	"github.com/core-coin/go-core/v2/xcbclient"
    33  )
    34  
    35  // newClient creates a client with specified remote URL.
    36  func newClient(ctx *cli.Context) *xcbclient.Client {
    37  	client, err := xcbclient.Dial(ctx.GlobalString(nodeURLFlag.Name))
    38  	if err != nil {
    39  		utils.Fatalf("Failed to connect to Core 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 Core 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 addrStr string
    57  	if err := client.Call(&addrStr, "les_getCheckpointContractAddress"); err != nil {
    58  		utils.Fatalf("Failed to fetch checkpoint oracle address: %v", err)
    59  	}
    60  	addr, err := common.HexToAddress(addrStr)
    61  	if err != nil {
    62  		utils.Fatalf("invalid address %v, err: %v", addrStr, err)
    63  	}
    64  	return addr
    65  }
    66  
    67  // getCheckpoint retrieves the specified checkpoint or the latest one
    68  // through rpc request.
    69  func getCheckpoint(ctx *cli.Context, client *rpc.Client) *params.TrustedCheckpoint {
    70  	var checkpoint *params.TrustedCheckpoint
    71  
    72  	if ctx.GlobalIsSet(indexFlag.Name) {
    73  		var result [3]string
    74  		index := uint64(ctx.GlobalInt64(indexFlag.Name))
    75  		if err := client.Call(&result, "les_getCheckpoint", index); err != nil {
    76  			utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
    77  		}
    78  		checkpoint = &params.TrustedCheckpoint{
    79  			SectionIndex: index,
    80  			SectionHead:  common.HexToHash(result[0]),
    81  			CHTRoot:      common.HexToHash(result[1]),
    82  			BloomRoot:    common.HexToHash(result[2]),
    83  		}
    84  	} else {
    85  		var result [4]string
    86  		err := client.Call(&result, "les_latestCheckpoint")
    87  		if err != nil {
    88  			utils.Fatalf("Failed to get local checkpoint %v, please ensure the les API is exposed", err)
    89  		}
    90  		index, err := strconv.ParseUint(result[0], 0, 64)
    91  		if err != nil {
    92  			utils.Fatalf("Failed to parse checkpoint index %v", err)
    93  		}
    94  		checkpoint = &params.TrustedCheckpoint{
    95  			SectionIndex: index,
    96  			SectionHead:  common.HexToHash(result[1]),
    97  			CHTRoot:      common.HexToHash(result[2]),
    98  			BloomRoot:    common.HexToHash(result[3]),
    99  		}
   100  	}
   101  	return checkpoint
   102  }
   103  
   104  // newContract creates a registrar contract instance with specified
   105  // contract address or the default contracts for mainnet or testnet.
   106  func newContract(client *rpc.Client) (common.Address, *checkpointoracle.CheckpointOracle) {
   107  	addr := getContractAddr(client)
   108  	if addr == (common.Address{}) {
   109  		utils.Fatalf("No specified registrar contract address")
   110  	}
   111  	contract, err := checkpointoracle.NewCheckpointOracle(addr, xcbclient.NewClient(client))
   112  	if err != nil {
   113  		utils.Fatalf("Failed to setup registrar contract %s: %v", addr, err)
   114  	}
   115  	return addr, contract
   116  }
   117  
   118  // newClefSigner sets up a clef backend and returns a clef transaction signer.
   119  func newClefSigner(ctx *cli.Context) *bind.TransactOpts {
   120  	clef, err := external.NewExternalSigner(ctx.String(clefURLFlag.Name))
   121  	if err != nil {
   122  		utils.Fatalf("Failed to create clef signer %v", err)
   123  	}
   124  	addr, err := common.HexToAddress(ctx.String(signerFlag.Name))
   125  	if err != nil {
   126  		utils.Fatalf("invalid address %v, err: %v", addr, err)
   127  	}
   128  	return bind.NewClefTransactor(clef, accounts.Account{Address: addr})
   129  }