Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: multi-sign batch command is not exposed and tested (#422) #438

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/chain-maind/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func txCommand() *cobra.Command {
authcmd.GetSignCommand(),
authcmd.GetSignBatchCommand(),
authcmd.GetMultiSignCommand(),
authcmd.GetMultiSignBatchCmd(),
authcmd.GetValidateSignaturesCommand(),
flags.LineBreak,
authcmd.GetBroadcastCommand(),
Expand Down
4 changes: 4 additions & 0 deletions integration_tests/configs/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ chaintest:
coins: 10000cro
- name: signer2
coins: 2000cro
- name: msigner1
coins: 2000cro
- name: msigner2
coins: 2000cro
genesis:
app_state:
staking:
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_liquid_supply(cluster):

liquid_supply = cluster.supply("liquid")["supply"]
assert liquid_supply[0]["denom"] == "basecro"
assert liquid_supply[0]["amount"] == "1240000000000"
assert liquid_supply[0]["amount"] == "1640000000000"


def test_statesync(cluster):
Expand Down
69 changes: 69 additions & 0 deletions integration_tests/test_multisig.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import json

from pystarport import ports
from pystarport.proto_python.api_util import ApiUtil

from .utils import wait_for_new_blocks


Expand Down Expand Up @@ -38,3 +41,69 @@ def test_multi_signature(cluster, tmp_path):
assert 125 == multi_balance
# check singer2 balance
assert cluster.balance(signer2_addr) == signer2_balance + 80


def test_multi_signature_batch(cluster, tmp_path):
# prepare files
m_txt = tmp_path / "bm.txt"
p1_txt = tmp_path / "bp1.txt"
p2_txt = tmp_path / "bp2.txt"
tx_txt = tmp_path / "btx.txt"
multi_wallet_name = "multitest2"

# make multi-sig
cluster.make_multisig(multi_wallet_name, "msigner1", "msigner2")
multi_addr = cluster.address(multi_wallet_name)
signer1_addr = cluster.address("msigner1")
signer2_addr = cluster.address("msigner2")
# send amount to multi-sig
cluster.transfer(signer1_addr, multi_addr, "500basecro")
wait_for_new_blocks(cluster, 1)
multi_balance = cluster.balance(multi_addr)
assert 500 == multi_balance
signer1_balance = cluster.balance(signer1_addr)
signer2_balance = cluster.balance(signer2_addr)
# send amount from multisig to msigner1 and msigner2
with open(m_txt, "a") as f:
multi_tx = cluster.transfer(
multi_addr, signer1_addr, "100basecro", generate_only=True
)
f.write(json.dumps(multi_tx))
f.write("\n")
multi_tx = cluster.transfer(
multi_addr, signer2_addr, "100basecro", generate_only=True
)
f.write(json.dumps(multi_tx))

# multisign the tx
port = ports.api_port(cluster.base_port(0))
api = ApiUtil(port)
multi_account_info = api.account_info(multi_addr)
account_num = multi_account_info["account_num"]
sequence = multi_account_info["sequence"]
signature2 = cluster.sign_batch_multisig_tx(
m_txt, multi_addr, "msigner1", account_num, sequence
)
with open(p1_txt, "w") as f:
f.write(signature2)
signature3 = cluster.sign_batch_multisig_tx(
m_txt, multi_addr, "msigner2", account_num, sequence
)
with open(p2_txt, "w") as f:
f.write(signature3)

final_multi_tx = cluster.combine_batch_multisig_tx(
m_txt, multi_wallet_name, p1_txt, p2_txt
)
for line in final_multi_tx.splitlines():
with open(tx_txt, "w") as f:
f.write(line)
assert len(line) > 0
cluster.broadcast_tx(tx_txt)
# check multisig balance
multi_balance = cluster.balance(multi_addr)
wait_for_new_blocks(cluster, 4)
assert 300 == multi_balance
# check singer2 balance
assert cluster.balance(signer1_addr) == signer1_balance + 100
assert cluster.balance(signer2_addr) == signer2_balance + 100
20 changes: 20 additions & 0 deletions pystarport/pystarport/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ def staking_params(self, i=0):
def staking_pool(self, bonded=True, i=0):
return self.cosmos_cli(i).staking_pool(bonded)

def transfer_offline(self, from_, to, coins, sequence, i=0, fees=None):
return self.cosmos_cli(i).transfer_offline(from_, to, coins, sequence, fees)

def transfer(self, from_, to, coins, i=0, generate_only=False, fees=None):
return self.cosmos_cli(i).transfer(from_, to, coins, generate_only, fees)

Expand Down Expand Up @@ -384,6 +387,13 @@ def make_multisig(self, name, signer1, signer2, i=0):
def sign_multisig_tx(self, tx_file, multi_addr, signer_name, i=0):
return self.cosmos_cli(i).sign_multisig_tx(tx_file, multi_addr, signer_name)

def sign_batch_multisig_tx(
self, tx_file, multi_addr, signer_name, account_num, sequence, i=0
):
return self.cosmos_cli(i).sign_batch_multisig_tx(
tx_file, multi_addr, signer_name, account_num, sequence
)

def encode_signed_tx(self, signed_tx, i=0):
return self.cosmos_cli(i).encode_signed_tx(signed_tx)

Expand All @@ -398,6 +408,16 @@ def combine_multisig_tx(self, tx_file, multi_name, signer1_file, signer2_file, i
signer2_file,
)

def combine_batch_multisig_tx(
self, tx_file, multi_name, signer1_file, signer2_file, i=0
):
return self.cosmos_cli(i).combine_batch_multisig_tx(
tx_file,
multi_name,
signer1_file,
signer2_file,
)

def broadcast_tx(self, tx_file, i=0):
return self.cosmos_cli(i).broadcast_tx(tx_file)

Expand Down
36 changes: 36 additions & 0 deletions pystarport/pystarport/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,25 @@ def sign_multisig_tx(self, tx_file, multi_addr, signer_name):
)
)

def sign_batch_multisig_tx(
self, tx_file, multi_addr, signer_name, account_number, sequence_number
):
r = self.raw(
"tx",
"sign-batch",
"--offline",
tx_file,
account_number=account_number,
sequence=sequence_number,
from_=signer_name,
multisig=multi_addr,
home=self.data_dir,
keyring_backend="test",
chain_id=self.chain_id,
node=self.node_rpc,
)
return r.decode("utf-8")

def encode_signed_tx(self, signed_tx):
return self.raw(
"tx",
Expand Down Expand Up @@ -495,6 +514,23 @@ def combine_multisig_tx(self, tx_file, multi_name, signer1_file, signer2_file):
)
)

def combine_batch_multisig_tx(
self, tx_file, multi_name, signer1_file, signer2_file
):
r = self.raw(
"tx",
"multisign-batch",
tx_file,
multi_name,
signer1_file,
signer2_file,
home=self.data_dir,
keyring_backend="test",
chain_id=self.chain_id,
node=self.node_rpc,
)
return r.decode("utf-8")

def broadcast_tx(self, tx_file):
return json.loads(self.raw("tx", "broadcast", tx_file, node=self.node_rpc))

Expand Down