Skip to content

Commit

Permalink
Merge branch 'main' into version-restore
Browse files Browse the repository at this point in the history
Signed-off-by: mmsqe <mavis@crypto.com>
  • Loading branch information
mmsqe authored Jul 25, 2023
2 parents 3415978 + 1bbf3af commit f447007
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- [#1083](https://github.com/crypto-org-chain/cronos/pull/1083) memiavl support both sdk 46 and 47 root hash rules.
- [#1091](https://github.com/crypto-org-chain/cronos/pull/1091) memiavl support rollback.
- [#1100](https://github.com/crypto-org-chain/cronos/pull/1100) memiavl support read-only mode, and grab exclusive lock for write mode.
- [#1103](https://github.com/crypto-org-chain/cronos/pull/1103) Add EventQueryTxFor cmd to subscribe and wait for transaction.
- [#1108](https://github.com/crypto-org-chain/cronos/pull/1108) versiondb support restore from local snapshot.

### Improvements
Expand Down
11 changes: 11 additions & 0 deletions integration_tests/configs/broadcast.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local config = import 'default.jsonnet';

config {
'cronos_777-1'+: {
validators: [validator {
client_config: {
'broadcast-mode': 'sync',
},
} for validator in super.validators],
},
}
13 changes: 13 additions & 0 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,3 +1682,16 @@ def bootstrap_state(self, height=None):
height=height,
home=self.data_dir,
)

def event_query_tx_for(self, hash):
return json.loads(
self.raw(
"tx",
"cronos",
"event-query-tx-for",
hash,
"-y",
home=self.data_dir,
stderr=subprocess.DEVNULL,
)
)
34 changes: 2 additions & 32 deletions integration_tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
KEYS,
Greeter,
RevertTestContract,
approve_proposal,
build_batch_tx,
contract_address,
contract_path,
Expand All @@ -27,6 +26,7 @@
modify_command_in_supervisor_config,
send_transaction,
send_txs,
submit_any_proposal,
wait_for_block,
wait_for_new_blocks,
wait_for_port,
Expand Down Expand Up @@ -844,34 +844,4 @@ def test_replay_protection(cronos):


def test_submit_any_proposal(cronos, tmp_path):
# governance module account as granter
cli = cronos.cosmos_cli()
granter_addr = "crc10d07y265gmmuvt4z0w9aw880jnsr700jdufnyd"
grantee_addr = cli.address("signer1")

# this json can be obtained with `--generate-only` flag for respective cli calls
proposal_json = {
"messages": [
{
"@type": "/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"granter": granter_addr,
"grantee": grantee_addr,
"allowance": {
"@type": "/cosmos.feegrant.v1beta1.BasicAllowance",
"spend_limit": [],
"expiration": None,
},
}
],
"deposit": "1basetcro",
}
proposal_file = tmp_path / "proposal.json"
proposal_file.write_text(json.dumps(proposal_json))
rsp = cli.submit_gov_proposal(proposal_file, from_="community")
assert rsp["code"] == 0, rsp["raw_log"]

approve_proposal(cronos, rsp)

grant_detail = cli.query_grant(granter_addr, grantee_addr)
assert grant_detail["granter"] == granter_addr
assert grant_detail["grantee"] == grantee_addr
submit_any_proposal(cronos, tmp_path)
18 changes: 18 additions & 0 deletions integration_tests/test_broadcast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pathlib import Path

import pytest

from .network import setup_custom_cronos
from .utils import submit_any_proposal


@pytest.fixture(scope="module")
def custom_cronos(tmp_path_factory):
path = tmp_path_factory.mktemp("cronos")
yield from setup_custom_cronos(
path, 26400, Path(__file__).parent / "configs/broadcast.jsonnet"
)


def test_submit_any_proposal(custom_cronos, tmp_path):
submit_any_proposal(custom_cronos, tmp_path, True)
38 changes: 37 additions & 1 deletion integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ def wait_for_block_time(cli, t):
time.sleep(0.5)


def approve_proposal(n, rsp):
def approve_proposal(n, rsp, event_query_tx=False):
cli = n.cosmos_cli()
if event_query_tx:
rsp = cli.event_query_tx_for(rsp["txhash"])
# get proposal_id
ev = parse_events(rsp["logs"])["submit_proposal"]
proposal_id = ev["proposal_id"]
Expand Down Expand Up @@ -597,3 +599,37 @@ def setup_token_mapping(cronos, name, symbol):
rsp = cronos_cli.query_denom_by_contract(contract.address)
assert rsp["denom"] == denom
return contract, denom


def submit_any_proposal(cronos, tmp_path, event_query_tx=False):
# governance module account as granter
cli = cronos.cosmos_cli()
granter_addr = "crc10d07y265gmmuvt4z0w9aw880jnsr700jdufnyd"
grantee_addr = cli.address("signer1")

# this json can be obtained with `--generate-only` flag for respective cli calls
proposal_json = {
"messages": [
{
"@type": "/cosmos.feegrant.v1beta1.MsgGrantAllowance",
"granter": granter_addr,
"grantee": grantee_addr,
"allowance": {
"@type": "/cosmos.feegrant.v1beta1.BasicAllowance",
"spend_limit": [],
"expiration": None,
},
}
],
"deposit": "1basetcro",
}
proposal_file = tmp_path / "proposal.json"
proposal_file.write_text(json.dumps(proposal_json))
rsp = cli.submit_gov_proposal(proposal_file, from_="community")
assert rsp["code"] == 0, rsp["raw_log"]

approve_proposal(cronos, rsp, event_query_tx)

grant_detail = cli.query_grant(granter_addr, grantee_addr)
assert grant_detail["granter"] == granter_addr
assert grant_detail["grantee"] == grantee_addr
63 changes: 62 additions & 1 deletion x/cronos/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cli

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -16,8 +19,11 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"

"github.com/crypto-org-chain/cronos/v2/x/cronos/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
)

// GetTxCmd returns the transaction commands for this module
Expand All @@ -37,6 +43,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdUpdateTokenMapping())
cmd.AddCommand(CmdTurnBridge())
cmd.AddCommand(CmdUpdatePermissions())
cmd.AddCommand(EventQueryTxFor())

return cmd
}
Expand Down Expand Up @@ -309,3 +316,57 @@ func CmdUpdatePermissions() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// EventQueryTxFor returns a CLI command that subscribes to a WebSocket connection and waits for a transaction event with the given hash.
func EventQueryTxFor() *cobra.Command {
cmd := &cobra.Command{
Use: "event-query-tx-for [hash]",
Short: "event-query-tx-for [hash]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
c, err := rpchttp.New(clientCtx.NodeURI, "/websocket")
if err != nil {
return err
}
if err := c.Start(); err != nil {
return err
}
defer c.Stop()

ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()

hash := args[0]
query := fmt.Sprintf("%s='%s' AND %s='%s'", tmtypes.EventTypeKey, tmtypes.EventTx, tmtypes.TxHashKey, hash)
const subscriber = "subscriber"
eventCh, err := c.Subscribe(ctx, subscriber, query)
if err != nil {
return fmt.Errorf("failed to subscribe to tx: %w", err)
}
defer c.UnsubscribeAll(context.Background(), subscriber)

select {
case evt := <-eventCh:
if txe, ok := evt.Data.(tmtypes.EventDataTx); ok {
res := &coretypes.ResultBroadcastTxCommit{
DeliverTx: txe.Result,
Hash: tmtypes.Tx(txe.Tx).Hash(),
Height: txe.Height,
}
return clientCtx.PrintProto(sdk.NewResponseFormatBroadcastTxCommit(res))
}
case <-ctx.Done():
return errors.New("timed out waiting for event")
}
return nil
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

0 comments on commit f447007

Please sign in to comment.