Skip to content

Commit

Permalink
refactor create_validator
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jul 22, 2024
1 parent 217da5d commit 11b044b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 55 deletions.
32 changes: 14 additions & 18 deletions pystarport/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,29 +548,25 @@ def unjail(self, addr, i=0, event_query_tx=True):
def create_validator(
self,
amount,
options,
i,
moniker=None,
commission_max_change_rate="0.01",
commission_rate="0.1",
commission_max_rate="0.2",
min_self_delegation="1",
event_query_tx=True,
sdk47_compact=True,
**kwargs,
):
"""MsgCreateValidator
create the node with create_node before call this"""
return self.cosmos_cli(i).create_validator(
amount,
moniker or self.config["validators"][i]["moniker"],
commission_max_change_rate,
commission_rate,
commission_max_rate,
min_self_delegation,
event_query_tx=event_query_tx,
sdk47_compact=sdk47_compact,
**kwargs,
)
options.setdefault("moniker", self.config["validators"][i]["moniker"])
return self.cosmos_cli(i).create_validator(amount, options, **kwargs)

def create_validator_legacy(
self,
amount,
i,
**kwargs,
):
"""MsgCreateValidator
create the node with create_node before call this"""
kwargs.setdefault("moniker", self.config["validators"][i]["moniker"])
return self.cosmos_cli(i).create_validator_legacy(amount, **kwargs)

def edit_validator(
self,
Expand Down
101 changes: 64 additions & 37 deletions pystarport/cosmoscli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import enum
import hashlib
import json
import re
import subprocess
import tempfile
import threading
Expand Down Expand Up @@ -662,6 +661,55 @@ def unjail(self, addr, event_query_tx=True):
return rsp

def create_validator(
self,
amount,
options,
event_query_tx=True,
**kwargs,
):
options = {
"commission-max-change-rate": "0.01",
"commission-rate": "0.1",
"commission-max-rate": "0.2",
"min-self-delegation": "1",
"amount": amount,
} | options

if "pubkey" not in options:
pubkey = (
self.raw(
"tendermint",
"show-validator",
home=self.data_dir,
)
.strip()
.decode()
)
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

def create_validator_legacy(
self,
amount,
moniker=None,
Expand All @@ -670,7 +718,6 @@ def create_validator(
commission_max_rate="0.2",
min_self_delegation="1",
event_query_tx=True,
sdk47_compact=True,
**kwargs,
):
"""MsgCreateValidator
Expand All @@ -692,41 +739,21 @@ def create_validator(
"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"),
# 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,
)
options["pubkey"] = "'" + pubkey + "'"
raw = self.raw(
"tx",
"staking",
"create-validator",
"-y",
from_=self.address("validator"),
# 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,
)
rsp = json.loads(raw)
if rsp["code"] == 0 and event_query_tx:
rsp = self.event_query_tx_for(rsp["txhash"])
Expand Down

0 comments on commit 11b044b

Please sign in to comment.