Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
add is-relayer cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
P1sar committed Aug 12, 2021
1 parent 73dedf2 commit 0d06b54
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 27 deletions.
31 changes: 31 additions & 0 deletions chains/evm/calls/bridge.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package calls

import (
"fmt"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog/log"
)

func PrepareSetBurnableInput(client ChainClient, handler, tokenAddress common.Address) ([]byte, error) {
Expand Down Expand Up @@ -55,3 +57,32 @@ func PrepareAddRelayerInput(relayer common.Address) ([]byte, error) {
}
return input, nil
}
func PrepareIsRelayerInput(address common.Address) ([]byte, error) {
a, err := abi.JSON(strings.NewReader(BridgeABI))
if err != nil {
return nil, err
}

data, err := a.Pack("isRelayer", address)
if err != nil {
log.Error().Err(fmt.Errorf("unpack output error: %v", err))
return nil, err
}
return data, nil
}

func ParseIsRelayerOutput(output []byte) (bool, error) {
a, err := abi.JSON(strings.NewReader(BridgeABI))
if err != nil {
return false, err
}

res, err := a.Unpack("isRelayer", output)
if err != nil {
log.Error().Err(fmt.Errorf("unpack output error: %v", err))
return false, err
}

b := abi.ConvertType(res[0], new(bool)).(*bool)
return *b, nil
}
100 changes: 73 additions & 27 deletions chains/evm/cli/admin/is-relayer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package admin

import (
"context"
"errors"
"fmt"

"github.com/ChainSafe/chainbridge-core/chains/evm/calls"
"github.com/ChainSafe/chainbridge-core/chains/evm/cli/flags"
"github.com/ChainSafe/chainbridge-core/chains/evm/evmclient"
"github.com/ChainSafe/chainbridge-core/chains/evm/evmtransaction"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
Expand All @@ -9,54 +19,90 @@ var isRelayerCmd = &cobra.Command{
Use: "is-relayer",
Short: "Check if an address is registered as a relayer",
Long: "Check if an address is registered as a relayer",
Run: isRelayer,
RunE: func(cmd *cobra.Command, args []string) error {
txFabric := evmtransaction.NewTransaction
return IsRelayer(cmd, args, txFabric)
},
}

func BindIsRelayerFlags(cli *cobra.Command) {
cli.Flags().String("relayer", "", "address to check")
cli.Flags().String("bridge", "", "bridge contract address")
}

func init() {
isRelayerCmd.Flags().String("relayer", "", "address to check")
isRelayerCmd.Flags().String("bridge", "", "bridge contract address")
BindIsRelayerFlags(isRelayerCmd)
}

func isRelayer(cmd *cobra.Command, args []string) {
relayerAddress := cmd.Flag("relayer").Value
bridgeAddress := cmd.Flag("bridge").Value
func IsRelayer(cmd *cobra.Command, args []string, txFabric calls.TxFabric) error {
relayerAddress := cmd.Flag("relayer").Value.String()
bridgeAddress := cmd.Flag("bridge").Value.String()
log.Debug().Msgf(`
Checking relayer
Relayer address: %s
Bridge address: %s`, relayerAddress, bridgeAddress)
}
Checking relayer
Relayer address: %s
Bridge address: %s`, relayerAddress, bridgeAddress)

/*
func isRelayer(cctx *cli.Context) error {
url := cctx.String("url")
gasLimit := cctx.Uint64("gasLimit")
gasPrice := cctx.Uint64("gasPrice")
sender, err := cliutils.DefineSender(cctx)
// fetch global flag values
url, _, gasPrice, senderKeyPair, err := flags.GlobalFlagValues(cmd)
if err != nil {
return fmt.Errorf("could not get global flags: %v", err)
}

if !common.IsHexAddress(relayerAddress) {
err := errors.New("handler address is incorrect format")
log.Error().Err(err)
return err
}

if !common.IsHexAddress(bridgeAddress) {
err := errors.New("tokenContract address is incorrect format")
log.Error().Err(err)
return err
}
bridgeAddress, err := cliutils.DefineBridgeAddress(cctx)
relayer := common.HexToAddress(relayerAddress)
bridge := common.HexToAddress(bridgeAddress)
ethClient, err := evmclient.NewEVMClientFromParams(url, senderKeyPair.PrivateKey(), gasPrice)
if err != nil {
log.Error().Err(err)
return err
}
relayer := cctx.String("relayer")
if !common.IsHexAddress(relayer) {
return fmt.Errorf("invalid relayer address %s", relayer)
// erc20Addr, accountAddr
input, err := calls.PrepareIsRelayerInput(relayer)
if err != nil {
log.Error().Err(fmt.Errorf("prepare input error: %v", err))
return err
}

msg := ethereum.CallMsg{
From: common.Address{},
To: &bridge,
Data: input,
}
relayerAddress := common.HexToAddress(relayer)
ethClient, err := client.NewClient(url, false, sender, big.NewInt(0).SetUint64(gasLimit), big.NewInt(0).SetUint64(gasPrice), big.NewFloat(1))

out, err := ethClient.CallContract(context.TODO(), calls.ToCallArg(msg), nil)
if err != nil {
log.Error().Err(fmt.Errorf("call contract error: %v", err))
return err
}
isRelayer, err := utils.AdminIsRelayer(ethClient, bridgeAddress, relayerAddress)

if len(out) == 0 {
// Make sure we have a contract to operate on, and bail out otherwise.
if code, err := ethClient.CodeAt(context.Background(), bridge, nil); err != nil {
return err
} else if len(code) == 0 {
return fmt.Errorf("no code at provided address %s", bridge.String())
}
}
b, err := calls.ParseIsRelayerOutput(out)
if err != nil {
log.Error().Err(fmt.Errorf("prepare output error: %v", err))
return err
}
if isRelayer {
log.Info().Msgf("Requested address %s is relayer", relayerAddress.String())
if b {
log.Info().Msgf("Address %s is not relayer", relayer.String())
} else {
log.Info().Msgf("Requested address %s is not a relayer", relayerAddress.String())
log.Info().Msgf("Address %s is relayer", relayer.String())
}
return nil
}
*/

0 comments on commit 0d06b54

Please sign in to comment.