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: create and get validator are incompatible #129

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [#121](https://github.com/crypto-com/pystarport/pull/121), [#122](https://github.com/crypto-com/pystarport/pull/122), [#125](https://github.com/crypto-com/pystarport/pull/125) Support sdk 0.50.
- [#127](https://github.com/crypto-com/pystarport/pull/127) Support adding new key when patching config
- [#128](https://github.com/crypto-com/pystarport/pull/128) fix wrong description on empty flag when create validator and align flags for edit validator.
- [#129](https://github.com/crypto-com/pystarport/pull/129) create and get validator are incompatible.

*Feb 7, 2023*

Expand Down
2 changes: 2 additions & 0 deletions pystarport/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ def create_validator(
commission_max_rate="0.2",
min_self_delegation="1",
event_query_tx=True,
sdk47_compact=True,
**kwargs,
):
"""MsgCreateValidator
Expand All @@ -567,6 +568,7 @@ def create_validator(
commission_max_rate,
min_self_delegation,
event_query_tx=event_query_tx,
sdk47_compact=sdk47_compact,
**kwargs,
)

Expand Down
94 changes: 60 additions & 34 deletions pystarport/cosmoscli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import hashlib
import json
import re

Check failure on line 4 in pystarport/cosmoscli.py

View workflow job for this annotation

GitHub Actions / lint

./pystarport/cosmoscli.py:4:1: F401 're' imported but unused

Check notice

Code scanning / CodeQL

Unused import Note

Import of 're' is not used.
import subprocess
import tempfile
import threading
Expand All @@ -11,7 +12,13 @@

from .app import CHAIN
from .ledger import ZEMU_BUTTON_PORT, ZEMU_HOST, LedgerButton
from .utils import build_cli_args_safe, format_doc_string, get_sync_info, interact
from .utils import (
build_cli_args_safe,
format_doc_string,
get_sync_info,
interact,
parse_amount,
)


class ModuleAccount(enum.Enum):
Expand Down Expand Up @@ -249,7 +256,7 @@
return json.loads(txs)

def distribution_commission(self, addr):
coin = json.loads(
res = json.loads(
self.raw(
"query",
"distribution",
Expand All @@ -258,23 +265,23 @@
output="json",
node=self.node_rpc,
)
)["commission"][0]
return float(coin["amount"])
)["commission"]
return parse_amount((res.get("commission") or res)[0])

def distribution_community(self):
coin = json.loads(
res = json.loads(
self.raw(
"query",
"distribution",
"community-pool",
output="json",
node=self.node_rpc,
)
)["pool"][0]
return float(coin["amount"])
)
return parse_amount(res["pool"][0])

def distribution_reward(self, delegator_addr):
coin = json.loads(
res = json.loads(
self.raw(
"query",
"distribution",
Expand All @@ -283,8 +290,8 @@
output="json",
node=self.node_rpc,
)
)["total"][0]
return float(coin["amount"])
)
return parse_amount(res["total"][0])

def address(self, name, bech="acc"):
output = self.raw(
Expand All @@ -311,7 +318,7 @@
)

def validator(self, addr):
return json.loads(
res = json.loads(
self.raw(
"query",
"staking",
Expand All @@ -321,6 +328,7 @@
node=self.node_rpc,
)
)
return res.get("validator") or res

def validators(self):
return json.loads(
Expand All @@ -330,9 +338,10 @@
)["validators"]

def staking_params(self):
return json.loads(
res = json.loads(
self.raw("query", "staking", "params", output="json", node=self.node_rpc)
)
return res.get("params") or res

def staking_pool(self, bonded=True):
res = self.raw("query", "staking", "pool", output="json", node=self.node_rpc)
Expand Down Expand Up @@ -661,47 +670,64 @@
commission_max_rate="0.2",
min_self_delegation="1",
event_query_tx=True,
sdk47_compact=True,
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
**kwargs,
):
"""MsgCreateValidator
create the node with create_node before call this"""
pubkey = (
"'"
+ (
self.raw(
"tendermint",
"show-validator",
home=self.data_dir,
)
.strip()
.decode()
self.raw(
"tendermint",
"show-validator",
home=self.data_dir,
)
+ "'"
.strip()
.decode()
)
rsp = json.loads(
self.raw(
options = {
"amount": amount,
"min-self-delegation": min_self_delegation,
"commission-rate": commission_rate,
"commission-max-rate": commission_max_rate,
"commission-max-change-rate": commission_max_change_rate,
"moniker": moniker,
}
if sdk47_compact:
options["pubkey"] = "'" + pubkey + "'"
raw = self.raw(
"tx",
"staking",
"create-validator",
"-y",
from_=self.address("validator"),
amount=amount,
pubkey=pubkey,
min_self_delegation=min_self_delegation,
# commision
commission_rate=commission_rate,
commission_max_rate=commission_max_rate,
commission_max_change_rate=commission_max_change_rate,
# description
moniker=moniker,
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**{k: v for k, v in options.items() if v is not None},
**kwargs,
)
)
else:
options["pubkey"] = json.loads(pubkey)
with tempfile.NamedTemporaryFile("w") as fp:
json.dump(options, fp)
fp.flush()
raw = self.raw(
"tx",
"staking",
"create-validator",
fp.name,
"-y",
from_=self.address("validator"),
# basic
home=self.data_dir,
node=self.node_rpc,
keyring_backend="test",
chain_id=self.chain_id,
**kwargs,
)
rsp = json.loads(raw)
if rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp
Expand Down
6 changes: 6 additions & 0 deletions pystarport/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pystarport.utils import parse_amount


def test_parse_amount():
assert parse_amount("1000000.01uatom") == 1000000.01
assert parse_amount({"amount": "1000000.01", "denom": "uatom"}) == 1000000.01
17 changes: 17 additions & 0 deletions pystarport/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import configparser
from itertools import takewhile
import subprocess

Check failure on line 3 in pystarport/utils.py

View workflow job for this annotation

GitHub Actions / lint

./pystarport/utils.py:3:1: I001 isort found an import in the wrong position


def interact(cmd, ignore_error=False, input=None, **kwargs):
Expand Down Expand Up @@ -65,3 +66,19 @@

def get_sync_info(s):
return s.get("SyncInfo") or s.get("sync_info")


def parse_amount(coin):
"""
parse amount from coin representation, compatible with multiple sdk versions:
- pre-sdk-50: {"denom": "uatom", "amount": "1000000.00"}
- post-sdk-50: "1000000.00uatom"
"""
if isinstance(coin, dict):
return float(coin["amount"])
else:
return float("".join(takewhile(is_float, coin)))


def is_float(s):
return str.isdigit(s) or s == "."
Loading