Skip to content

Commit

Permalink
Child chain mintable predicates E2E tests (#1560)
Browse files Browse the repository at this point in the history
* Simplify helper bridge commands

* Prevent mapping childchain mintable native token on non-mintable predicate

* Provide stake token address to rootchain deploy command

* WIP

* Revert not needed changes

* Mintable native token bridge transfer E2E test

* Wrap up bridge native erc20 mintable e2e test

* Update smart contracts spec

* Register bridge common flags at once

* Provide stake token address where missing

* Fix E2E tests

* Remove isExitEventProcessed helper function

* E2E tests for access list mintable predicates

* ERC-721 tokens bridging E2E test WIP

* Fix warnings

* Decouple minter and sender key in deposit commands

* Update README

* ERC-721 owner of helper function

* Fix deposit commands

* Update to the latest SC spec and remove not needed bindings

* Wrap up ERC721 tokens bridging E2E test

* Fix unit test

* Comment

* Comments

* Change exit event ids from string to big int in deposit and withdraw results

* Update to the latest SC spec

* Fix race condition

* Introduce buffered channel

* Consolidate bridge transaction result structs (#1584)

* Consolidate bridge transaction result into single struct

* Update SC to the latest spec

* Fix

* Add json tag to title field
  • Loading branch information
Stefan-Ethernal authored Jun 5, 2023
1 parent 406ea94 commit fc803f1
Show file tree
Hide file tree
Showing 34 changed files with 1,043 additions and 1,015 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.nightly.devnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ jobs:
polygon-edge rootchain deploy \
--deployer-key $(cat rootchain-wallet.json | jq -r '.HexPrivateKey') \
--stake-manager $(cat genesis.json | jq -r '.params.engine.polybft.bridge.stakeManagerAddr') \
--stake-token $(cat genesis.json | jq -r '.params.engine.polybft.bridge.stakeTokenAddr') \
--json-rpc {{ rootchain_json_rpc }}
{% for item in hostvars %}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test-e2e-polybft:
# We can not build with race because of a bug in boltdb dependency
go build -o artifacts/polygon-edge .
env EDGE_BINARY=${PWD}/artifacts/polygon-edge E2E_TESTS=true E2E_LOGS=true \
go test -v -timeout=1h ./e2e-polybft/e2e/...
go test -v -timeout=1h10m ./e2e-polybft/e2e/...

.PHONY: test-property-polybft
test-property-polybft:
Expand Down
9 changes: 5 additions & 4 deletions command/bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ $ polygon-edge bridge deposit-erc20 \
--amounts <amounts> \
--root-token <root_erc20_token_address> \
--root-predicate <root_erc20_predicate_address> \
--json-rpc <root_chain_json_rpc_endpoint>
--json-rpc <json_rpc_endpoint>
[--minter-key <hex_encoded_minter_account_private_key>]
```

**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.
**Note:** in case `minter-key` is provided, tokens are going to be minted to sender account. Note that provided minter private key must belong to the account which has minter role.

## Withdraw ERC20

Expand All @@ -27,9 +28,9 @@ $ 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-predicate <child_erc20_predicate_address> \
[--child-token <child_erc20_token_address>] \
--json-rpc <child_chain_json_rpc_endpoint>
--json-rpc <json_rpc_endpoint>
```

## Exit
Expand Down
157 changes: 142 additions & 15 deletions command/bridge/common/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package common

import (
"bytes"
"errors"
"fmt"
"math/big"
"strings"

"github.com/spf13/cobra"
"github.com/umbracle/ethgo"

cmdHelper "github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/txrelayer"
)

type TokenType int
Expand All @@ -13,16 +24,20 @@ const (
)

const (
SenderKeyFlag = "sender-key"
ReceiversFlag = "receivers"
AmountsFlag = "amounts"
TokenIDsFlag = "token-ids"

RootTokenFlag = "root-token"
RootPredicateFlag = "root-predicate"
ChildPredicateFlag = "child-predicate"
ChildTokenFlag = "child-token"
JSONRPCFlag = "json-rpc"
SenderKeyFlag = "sender-key"
ReceiversFlag = "receivers"
AmountsFlag = "amounts"
TokenIDsFlag = "token-ids"
RootTokenFlag = "root-token"
RootPredicateFlag = "root-predicate"
ChildPredicateFlag = "child-predicate"
ChildTokenFlag = "child-token"
JSONRPCFlag = "json-rpc"
ChildChainMintableFlag = "child-chain-mintable"

MinterKeyFlag = "minter-key"
MinterKeyFlagDesc = "minter key is the account which is able to mint tokens to sender account " +
"(if provided tokens are minted prior to depositing)"
)

var (
Expand All @@ -31,11 +46,43 @@ var (
)

type BridgeParams struct {
SenderKey string
Receivers []string
TokenAddr string
PredicateAddr string
JSONRPCAddr string
SenderKey string
Receivers []string
TokenAddr string
PredicateAddr string
JSONRPCAddr string
ChildChainMintable bool
}

// RegisterCommonFlags registers common bridge flags to a given command
func (p *BridgeParams) RegisterCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(
&p.SenderKey,
SenderKeyFlag,
"",
"hex encoded private key of the account which sends bridge transactions",
)

cmd.Flags().StringSliceVar(
&p.Receivers,
ReceiversFlag,
nil,
"receiving accounts addresses",
)

cmd.Flags().StringVar(
&p.JSONRPCAddr,
JSONRPCFlag,
txrelayer.DefaultRPCAddress,
"the JSON RPC endpoint",
)

cmd.Flags().BoolVar(
&p.ChildChainMintable,
ChildChainMintableFlag,
false,
"flag indicating whether tokens originate from child chain",
)
}

type ERC20BridgeParams struct {
Expand Down Expand Up @@ -93,3 +140,83 @@ func (bp *ERC1155BridgeParams) Validate() error {

return nil
}

// ExtractExitEventID tries to extract exit event id from provided receipt
func ExtractExitEventID(receipt *ethgo.Receipt) (*big.Int, error) {
var exitEvent contractsapi.L2StateSyncedEvent
for _, log := range receipt.Logs {
doesMatch, err := exitEvent.ParseLog(log)
if err != nil {
return nil, err
}

if !doesMatch {
continue
}

return exitEvent.ID, nil
}

return nil, errors.New("failed to find exit event log")
}

type BridgeTxResult struct {
Sender string `json:"sender"`
Receivers []string `json:"receivers"`
ExitEventIDs []*big.Int `json:"exitEventIDs"`
Amounts []string `json:"amounts"`
TokenIDs []string `json:"tokenIds"`
BlockNumbers []uint64 `json:"blockNumbers"`

Title string `json:"title"`
}

func (r *BridgeTxResult) GetOutput() string {
var buffer bytes.Buffer

vals := make([]string, 0, 5)
vals = append(vals, fmt.Sprintf("Sender|%s", r.Sender))
vals = append(vals, fmt.Sprintf("Receivers|%s", strings.Join(r.Receivers, ", ")))

if len(r.Amounts) > 0 {
vals = append(vals, fmt.Sprintf("Amounts|%s", strings.Join(r.Amounts, ", ")))
}

if len(r.TokenIDs) > 0 {
vals = append(vals, fmt.Sprintf("Token Ids|%s", strings.Join(r.TokenIDs, ", ")))
}

if len(r.ExitEventIDs) > 0 {
var buf bytes.Buffer

for i, id := range r.ExitEventIDs {
buf.WriteString(id.String())

if i != len(r.ExitEventIDs)-1 {
buf.WriteString(", ")
}
}

vals = append(vals, fmt.Sprintf("Exit Event IDs|%s", buf.String()))
}

if len(r.BlockNumbers) > 0 {
var buf bytes.Buffer

for i, blockNum := range r.BlockNumbers {
buf.WriteString(fmt.Sprintf("%d", blockNum))

if i != len(r.BlockNumbers)-1 {
buf.WriteString(", ")
}
}

vals = append(vals, fmt.Sprintf("Inclusion Block Numbers|%s", buf.String()))
}

_, _ = buffer.WriteString(fmt.Sprintf("\n[%s]\n", r.Title))
_, _ = buffer.WriteString(cmdHelper.FormatKV(vals))
_, _ = buffer.WriteString("\n")

return buffer.String()
}
Loading

0 comments on commit fc803f1

Please sign in to comment.