Skip to content

Commit

Permalink
Update smart contracts to include validators storage fix (#1325)
Browse files Browse the repository at this point in the history
* Provide hex encoded private key

* Update bridge framework

* Update withdraw command

* Simplify exit command

* Remove redundant comment

* Update smart contracts and filter out non active validators
  • Loading branch information
Stefan-Ethernal committed Mar 29, 2023
1 parent 7b89494 commit 99dffd5
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 283 deletions.
38 changes: 19 additions & 19 deletions command/bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ This is a helper command, which allows sending deposits from root to child chain
This is a helper command which deposits ERC20 tokens from the root chain to the child chain

```bash
$ polygon-edge bridge deposit-erc20
--data-dir <local_storage_secrets_path> | [--config <cloud_secrets_manager_config_path>]
--receivers <receivers_addresses>
--amounts <amounts>
--root-token <root_erc20_token_address>
--root-predicate <root_erc20_predicate_address>
$ polygon-edge bridge deposit-erc20 \
--sender-key <hex_encoded_depositor_private_key> \
--receivers <receivers_addresses> \
--amounts <amounts> \
--root-token <root_erc20_token_address> \
--root-predicate <root_erc20_predicate_address> \
--json-rpc <root_chain_json_rpc_endpoint>
```

**Note:** for using test account provided by Geth dev instance, use `--test` flag. In that case `--data-dir` and `--config` flags can be omitted and test account is used as depositor.
**Note:** for using test account provided by Geth dev instance, use `--test` flag. In that case `--sender-key` flag can be omitted and test account is used as a depositor.

## Withdraw ERC20

This is a helper command which withdraws ERC20 tokens from the child chain to the root chain

```bash
$ polygon-edge bridge withdraw-erc20
--data-dir <local_storage_secrets_path> | [--config <cloud_secrets_manager_config_path>]
--receivers <receivers_addresses>
--amounts <amounts>
--child-predicate <rchild_erc20_predicate_address>
[--child-token <child_erc20_token_address>]
$ polygon-edge bridge withdraw-erc20 \
--sender-key <hex_encoded_txn_sender_private_key> \
--receivers <receivers_addresses> \
--amounts <amounts> \
--child-predicate <rchild_erc20_predicate_address> \
[--child-token <child_erc20_token_address>] \
--json-rpc <child_chain_json_rpc_endpoint>
```

Expand All @@ -37,12 +37,12 @@ $ polygon-edge bridge withdraw-erc20
This is a helper command which qeuries child chain for exit event proof and sends an exit transaction to ExitHelper smart contract.

```bash
$ polygon-edge bridge exit
--data-dir <local_storage_secrets_path> | [--config <cloud_secrets_manager_config_path>]
--exit-helper <exit_helper_address>
--exit-id <exit_event_id>
--root-json-rpc <root_chain_json_rpc_endpoint>
$ polygon-edge bridge exit \
--sender-key <hex_encoded_txn_sender_private_key> \
--exit-helper <exit_helper_address> \
--exit-id <exit_event_id> \
--root-json-rpc <root_chain_json_rpc_endpoint> \
--child-json-rpc <child_chain_json_rpc_endpoint>
```

**Note:** for using test account provided by Geth dev instance, use `--test` flag. In that case `--data-dir` and `--config` flags can be omitted and test account is used as an exit transaction sender.
**Note:** for using test account provided by Geth dev instance, use `--test` flag. In that case `--sender-key` flag can be omitted and test account is used as an exit transaction sender.
17 changes: 5 additions & 12 deletions command/bridge/common/bridge_erc20_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package common

import (
"errors"

"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
)

const (
SenderKeyFlag = "sender-key"
ReceiversFlag = "receivers"
AmountsFlag = "amounts"
)
Expand All @@ -16,18 +15,12 @@ var (
)

type ERC20BridgeParams struct {
AccountDir string
AccountConfig string
Receivers []string
Amounts []string
SenderKey string
Receivers []string
Amounts []string
}

func (bp *ERC20BridgeParams) ValidateFlags(isTestMode bool) error {
// in case of test mode test rootchain account is being used as the rootchain transactions sender
if err := helper.ValidateSecretFlags(isTestMode, bp.AccountDir, bp.AccountConfig); err != nil {
return err
}

func (bp *ERC20BridgeParams) ValidateFlags() error {
if len(bp.Receivers) != len(bp.Amounts) {
return errInconsistentAccounts
}
Expand Down
53 changes: 12 additions & 41 deletions command/bridge/deposit/deposit_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/bridge/common"
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/polybftsecrets"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/command/sidechain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
Expand Down Expand Up @@ -50,17 +48,10 @@ func GetCommand() *cobra.Command {
}

depositCmd.Flags().StringVar(
&dp.AccountDir,
polybftsecrets.AccountDirFlag,
&dp.SenderKey,
common.SenderKeyFlag,
"",
polybftsecrets.AccountDirFlagDesc,
)

depositCmd.Flags().StringVar(
&dp.AccountConfig,
polybftsecrets.AccountConfigFlag,
"",
polybftsecrets.AccountConfigFlagDesc,
"hex encoded private key of the account which sends rootchain deposit transactions",
)

depositCmd.Flags().StringSliceVar(
Expand Down Expand Up @@ -106,21 +97,18 @@ func GetCommand() *cobra.Command {
"(in that case tokens are minted to it, so it is able to make deposits)",
)

depositCmd.MarkFlagRequired(common.ReceiversFlag) //nolint:errcheck
depositCmd.MarkFlagRequired(common.AmountsFlag) //nolint:errcheck
depositCmd.MarkFlagRequired(rootTokenFlag) //nolint:errcheck
depositCmd.MarkFlagRequired(rootPredicateFlag) //nolint:errcheck
_ = depositCmd.MarkFlagRequired(common.ReceiversFlag)
_ = depositCmd.MarkFlagRequired(common.AmountsFlag)
_ = depositCmd.MarkFlagRequired(rootTokenFlag)
_ = depositCmd.MarkFlagRequired(rootPredicateFlag)

depositCmd.MarkFlagsMutuallyExclusive(
helper.TestModeFlag,
polybftsecrets.AccountDirFlag,
polybftsecrets.AccountConfigFlag)
depositCmd.MarkFlagsMutuallyExclusive(helper.TestModeFlag, common.SenderKeyFlag)

return depositCmd
}

func runPreRun(cmd *cobra.Command, _ []string) error {
if err := dp.ValidateFlags(dp.testMode); err != nil {
if err := dp.ValidateFlags(); err != nil {
return err
}

Expand All @@ -131,26 +119,9 @@ func runCommand(cmd *cobra.Command, _ []string) {
outputter := command.InitializeOutputter(cmd)
defer outputter.WriteOutput()

var depositorKey ethgo.Key

if !dp.testMode {
depositorAccount, err := sidechain.GetAccount(dp.AccountDir, dp.AccountConfig)
if err != nil {
outputter.SetError(err)

return
}

depositorKey = depositorAccount.Ecdsa
} else {
rootchainKey, err := helper.GetRootchainTestPrivKey()
if err != nil {
outputter.SetError(fmt.Errorf("failed to initialize root chain private key: %w", err))

return
}

depositorKey = rootchainKey
depositorKey, err := helper.GetRootchainPrivateKey(dp.SenderKey)
if err != nil {
outputter.SetError(fmt.Errorf("failed to initialize depositor private key: %w", err))
}

depositorAddr := depositorKey.Address()
Expand Down
77 changes: 14 additions & 63 deletions command/bridge/exit/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import (
"github.com/umbracle/ethgo/jsonrpc"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/command/bridge/common"
cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/polybftsecrets"
"github.com/0xPolygon/polygon-edge/command/rootchain/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/txrelayer"
"github.com/0xPolygon/polygon-edge/types"
)
Expand All @@ -35,20 +34,14 @@ const (
)

type exitParams struct {
accountDir string
accountConfig string
senderKey string
exitHelperAddrRaw string
exitID uint64
rootJSONRPCAddr string
childJSONRPCAddr string
isTestMode bool
}

// validateFlags validates input values
func (ep *exitParams) validateFlags() error {
return helper.ValidateSecretFlags(ep.isTestMode, ep.accountDir, ep.accountConfig)
}

var (
// ep represents exit command parameters
ep *exitParams = &exitParams{}
Expand All @@ -57,24 +50,16 @@ var (
// GetCommand returns the bridge exit command
func GetCommand() *cobra.Command {
exitCmd := &cobra.Command{
Use: "exit",
Short: "Sends exit transaction to the Exit helper contract on the root chain",
PreRunE: preRun,
Run: run,
Use: "exit",
Short: "Sends exit transaction to the Exit helper contract on the root chain",
Run: run,
}

exitCmd.Flags().StringVar(
&ep.accountDir,
polybftsecrets.AccountDirFlag,
"",
polybftsecrets.AccountDirFlagDesc,
)

exitCmd.Flags().StringVar(
&ep.accountConfig,
polybftsecrets.AccountConfigFlag,
&ep.senderKey,
common.SenderKeyFlag,
"",
polybftsecrets.AccountConfigFlagDesc,
"hex encoded private key of the account which sends exit transaction to the root chain",
)

exitCmd.Flags().StringVar(
Expand Down Expand Up @@ -112,11 +97,8 @@ func GetCommand() *cobra.Command {
"test indicates whether exit transaction sender is hardcoded test account",
)

exitCmd.MarkFlagRequired(exitHelperFlag) //nolint:errcheck
exitCmd.MarkFlagsMutuallyExclusive(
helper.TestModeFlag,
polybftsecrets.AccountDirFlag,
polybftsecrets.AccountConfigFlag)
_ = exitCmd.MarkFlagRequired(exitHelperFlag)
exitCmd.MarkFlagsMutuallyExclusive(helper.TestModeFlag, common.SenderKeyFlag)

return exitCmd
}
Expand All @@ -125,33 +107,11 @@ func run(cmd *cobra.Command, _ []string) {
outputter := command.InitializeOutputter(cmd)
defer outputter.WriteOutput()

var senderKey ethgo.Key

if !ep.isTestMode {
secretsManager, err := polybftsecrets.GetSecretsManager(ep.accountDir, ep.accountConfig, true)
if err != nil {
outputter.SetError(err)

return
}

senderAccount, err := wallet.NewAccountFromSecret(secretsManager)
if err != nil {
outputter.SetError(err)

return
}

senderKey = senderAccount.Ecdsa
} else {
rootchainKey, err := helper.GetRootchainTestPrivKey()
if err != nil {
outputter.SetError(fmt.Errorf("failed to initialize root chain private key: %w", err))

return
}
senderKey, err := helper.GetRootchainPrivateKey(ep.senderKey)
if err != nil {
outputter.SetError(fmt.Errorf("failed to create wallet from private key: %w", err))

senderKey = rootchainKey
return
}

rootTxRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(ep.rootJSONRPCAddr))
Expand Down Expand Up @@ -207,15 +167,6 @@ func run(cmd *cobra.Command, _ []string) {
})
}

// preRun is used to validate input values
func preRun(_ *cobra.Command, _ []string) error {
if err := ep.validateFlags(); err != nil {
return err
}

return nil
}

// createExitTxn encodes parameters for exit function on root chain ExitHelper contract
func createExitTxn(sender ethgo.Address, proof types.Proof) (*ethgo.Transaction, *polybft.ExitEvent, error) {
exitEventMap, ok := proof.Metadata["ExitEvent"].(map[string]interface{})
Expand Down
Loading

0 comments on commit 99dffd5

Please sign in to comment.