common.go 4.2 KB

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