Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Nov 24, 2022
1 parent 2f9cd18 commit fb986d3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
26 changes: 26 additions & 0 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,29 @@ def query_grant(self, granter, grantee):
home=self.data_dir,
)
)

def query_batches(self):
"query all gravity batches"
return json.loads(
self.raw(
"query",
"gravity",
"batch-txs",
home=self.data_dir,
)
)

def turn_bridge(self, enable, **kwargs):
kwargs.setdefault("gas_prices", DEFAULT_GAS_PRICE)
kwargs.setdefault("gas", DEFAULT_GAS)
return json.loads(
self.raw(
"tx",
"cronos",
"turn-bridge",
enable,
"-y",
home=self.data_dir,
**kwargs,
)
)
71 changes: 71 additions & 0 deletions integration_tests/test_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,74 @@ def check():
gravity.contract.functions.redeemVoucher(
old_nonce, ADDRS["signer2"]
).build_transaction({"from": ADDRS["signer1"]})


def test_gravity_turn_bridge(gravity):
geth = gravity.geth
cli = gravity.cronos.cosmos_cli()
cronos_w3 = gravity.cronos.w3

# deploy test erc20 contract
erc20 = deploy_contract(
geth,
CONTRACTS["TestERC20A"],
)

balance = erc20.caller.balanceOf(ADDRS["validator"])
assert balance == 100000000000000000000000000
amount = 1000

print("send to cronos crc20")
recipient = HexBytes(ADDRS["community"])
txreceipt = send_to_cosmos(
gravity.contract, erc20, geth, recipient, amount, KEYS["validator"]
)
assert txreceipt.status == 1, "should success"
assert erc20.caller.balanceOf(ADDRS["validator"]) == balance - amount

denom = f"gravity{erc20.address}"

def check_gravity_native_tokens():
"check the balance of gravity native token"
return cli.balance(eth_to_bech32(recipient), denom=denom) == amount

if gravity.cronos.enable_auto_deployment:
crc21_contract = None

def local_check_auto_deployment():
nonlocal crc21_contract
crc21_contract = check_auto_deployment(
cli, denom, cronos_w3, recipient, amount
)
return crc21_contract

wait_for_fn("send-to-crc21", local_check_auto_deployment)
else:
wait_for_fn("send-to-gravity-native", check_gravity_native_tokens)

# turn off bridge
rsp = cli.turn_bridge("false", from_="community")
assert rsp["code"] != 0, "should not have the permission"

rsp = cli.turn_bridge("false", from_="validator")
assert rsp["code"] == 0, rsp["raw_log"]
wait_for_new_blocks(cli, 1)

if gravity.cronos.enable_auto_deployment:
# send it back to erc20, should fail
tx = crc21_contract.functions.send_to_evm_chain(
ADDRS["validator"], amount, 1, 0, b""
).build_transaction({"from": ADDRS["community"]})
txreceipt = send_transaction(cronos_w3, tx, KEYS["community"])
assert txreceipt.status == 0, "should fail"
else:
# send back the gravity native tokens, should fail
rsp = cli.send_to_ethereum(
ADDRS["validator"], f"{amount}{denom}", f"0{denom}", from_="community"
)
assert rsp["code"] == 3, rsp["raw_log"]

wait_for_new_blocks(cli, 10)
# check no new batch is created
rsp = cli.query_batches()
assert len(rsp["batches"]) == 0
15 changes: 4 additions & 11 deletions x/cronos/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -251,28 +252,22 @@ func CmdUpdateTokenMapping() *cobra.Command {
return cmd
}

// FlagEnable TurnBridgeCmd flags
const (
FlagEnable = "enable"
)

// CmdTurnBridge returns a CLI command handler for enable or disable the bridge
func CmdTurnBridge() *cobra.Command {
cmd := &cobra.Command{
Use: "turn-bridge",
Use: "turn-bridge [true/false]",
Short: "Turn Bridge",
Args: cobra.ExactArgs(0),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

enable, err := cmd.Flags().GetBool(FlagEnable)
enable, err := strconv.ParseBool(args[0])
if err != nil {
return err
}

msg := types.NewMsgTurnBridge(clientCtx.GetFromAddress().String(), enable)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -282,7 +277,5 @@ func CmdTurnBridge() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(FlagEnable, true, "")

return cmd
}

0 comments on commit fb986d3

Please sign in to comment.