github.com/platonnetwork/platon-go@v0.7.6/cases/tests/lib/staking.py (about) 1 from environment.env import TestEnvironment 2 from environment.node import Node 3 from .config import StakingConfig 4 from .economic import Economic 5 import time 6 7 8 class Staking: 9 """ 10 Used to initiate a Staking transaction, 11 if you need to use the call method, please call ppos 12 example: 13 >>>staking=Staking(env, node) 14 >>>staking.ppos.getValidatorList() 15 """ 16 17 def __init__(self, env: TestEnvironment, node: Node, cfg: StakingConfig): 18 self.cfg = cfg 19 self.node = node 20 self.economic = Economic(env) 21 22 @property 23 def ppos(self): 24 return self.node.ppos 25 26 def create_staking(self, typ, benifit_address, from_address, node_id=None, amount=None, program_version=None, 27 program_version_sign=None, bls_pubkey=None, bls_proof=None, transaction_cfg=None, reward_per=0): 28 """ 29 Initiate Staking 30 :param typ: Indicates whether the account free amount or the account's lock amount is used for staking, 0: free amount; 1: lock amount 31 :param benifit_address: Income account for accepting block rewards and staking rewards 32 :param node_id: The idled node Id (also called the candidate's node Id) 33 :param amount: staking von (unit:von, 1LAT = 10**18 von) 34 :param program_version: The real version of the program, admin_getProgramVersion 35 :param program_version_sign: The real version of the program is signed, admin_getProgramVersion 36 :param bls_pubkey: Bls public key 37 :param bls_proof: Proof of bls, obtained by pulling the proof interface, admin_getSchnorrNIZKProve 38 :param from_address: address for transaction 39 :param transaction_cfg: Transaction basic configuration 40 type: dict 41 example:cfg = { 42 "gas":100000000, 43 "gasPrice":2000000000000, 44 "nonce":1, 45 } 46 :param reward_per: Proportion of the reward share obtained from the commission, using BasePoint 1BP = 0.01% 47 :return: if is need analyze return transaction result dict 48 if is not need analyze return transaction hash 49 """ 50 if node_id is None: 51 node_id = self.node.node_id 52 if amount is None: 53 amount = self.economic.create_staking_limit 54 if program_version is None: 55 program_version = self.node.program_version 56 if program_version_sign is None: 57 program_version_sign = self.node.program_version_sign 58 if bls_pubkey is None: 59 bls_pubkey = self.node.blspubkey 60 if bls_proof is None: 61 bls_proof = self.node.schnorr_NIZK_prove 62 pri_key = self.economic.account.find_pri_key(from_address) 63 return self.ppos.createStaking(typ, benifit_address, node_id, self.cfg.external_id, self.cfg.node_name, 64 self.cfg.website, self.cfg.details, amount, program_version, program_version_sign, 65 bls_pubkey, bls_proof, pri_key, reward_per, transaction_cfg=transaction_cfg) 66 67 def edit_candidate(self, from_address, benifit_address, node_id=None, transaction_cfg=None, reward_per=0): 68 """ 69 Modify staking information 70 :param benifit_address: Income account for accepting block rewards and staking rewards 71 :param node_id: The idled node Id (also called the candidate's node Id) 72 :param from_address: address for transaction 73 :param transaction_cfg: Transaction basic configuration 74 type: dict 75 example:cfg = { 76 "gas":100000000, 77 "gasPrice":2000000000000, 78 "nonce":1, 79 } 80 :param reward_per: Proportion of the reward share obtained from the commission, using BasePoint 1BP = 0.01% 81 :return: if is need analyze return transaction result dict 82 if is not need analyze return transaction hash 83 """ 84 if node_id is None: 85 node_id = self.node.node_id 86 pri_key = self.economic.account.find_pri_key(from_address) 87 return self.ppos.editCandidate(benifit_address, node_id, self.cfg.external_id, self.cfg.node_name, self.cfg.website, self.cfg.details, 88 pri_key, reward_per, transaction_cfg=transaction_cfg) 89 90 def increase_staking(self, typ, from_address, node_id=None, amount=None, transaction_cfg=None): 91 """ 92 Increase staking 93 :param typ: Indicates whether the account free amount or the account's lock amount is used for staking, 0: free amount; 1: lock amount 94 :param node_id: The idled node Id (also called the candidate's node Id) 95 :param amount: staking von (unit:von, 1LAT = 10**18 von) 96 :param from_address: address for transaction 97 :param transaction_cfg: Transaction basic configuration 98 type: dict 99 example:cfg = { 100 "gas":100000000, 101 "gasPrice":2000000000000, 102 "nonce":1, 103 } 104 :return: if is need analyze return transaction result dict 105 if is not need analyze return transaction hash 106 """ 107 if node_id is None: 108 node_id = self.node.node_id 109 if amount is None: 110 amount = self.economic.add_staking_limit 111 pri_key = self.economic.account.find_pri_key(from_address) 112 return self.ppos.increaseStaking(typ, node_id, amount, pri_key, transaction_cfg=transaction_cfg) 113 114 def withdrew_staking(self, from_address, node_id=None, transaction_cfg=None): 115 """ 116 Withdrawal of staking (one-time initiation of all cancellations, multiple arrivals) 117 :param node_id: The idled node Id (also called the candidate's node Id) 118 :param from_address: address for transaction 119 :param transaction_cfg: Transaction basic configuration 120 type: dict 121 example:cfg = { 122 "gas":100000000, 123 "gasPrice":2000000000000, 124 "nonce":1, 125 } 126 :return: if is need analyze return transaction result dict 127 if is not need analyze return transaction hash 128 """ 129 if node_id is None: 130 node_id = self.node.node_id 131 pri_key = self.economic.account.find_pri_key(from_address) 132 return self.ppos.withdrewStaking(node_id, pri_key, transaction_cfg=transaction_cfg) 133 134 def get_staking_address(self): 135 """ 136 Get the pledge wallet address 137 """ 138 result = self.ppos.getCandidateInfo(self.node.node_id) 139 candidate_info = result.get('Ret') 140 address = candidate_info.get('StakingAddress') 141 return self.node.web3.toChecksumAddress(address) 142 143 def get_candidate_list_not_verifier(self): 144 """ 145 Get a list of candidates for non-verifiers for the current billing cycle 146 """ 147 candidate_list = self.ppos.getCandidateList().get('Ret') 148 verifier_list = self.ppos.getVerifierList().get('Ret') 149 if verifier_list == "Getting verifierList is failed:The validator is not exist": 150 time.sleep(10) 151 verifier_list = self.ppos.getVerifierList().get('Ret') 152 candidate_no_verify_list = [] 153 verifier_node_list = [node_info.get("NodeId") for node_info in verifier_list] 154 for node_info in candidate_list: 155 node_id = node_info.get("NodeId") 156 if node_id not in verifier_node_list: 157 candidate_no_verify_list.append(node_id) 158 return candidate_no_verify_list 159 160 def get_staking_amount(self, node=None, flag=0): 161 """ 162 According to the node to obtain the amount of the deposit 163 """ 164 if node is None: 165 node = self.node 166 flag = int(flag) 167 stakinginfo = node.ppos.getCandidateInfo(node.node_id) 168 staking_data = stakinginfo.get('Ret') 169 shares = int(staking_data.get('Shares')) 170 released = int(staking_data.get('Released')) 171 restrictingplan = int(staking_data.get('RestrictingPlan')) 172 return [shares, released, restrictingplan][flag] 173 174 def get_version(self, node=None): 175 """ 176 According to the node to obtain the amount of the deposit 177 """ 178 if node is None: 179 node = self.node 180 stakinginfo = node.ppos.getCandidateInfo(node.node_id) 181 staking_data = stakinginfo.get('Ret') 182 programversion = staking_data.get('ProgramVersion') 183 return programversion