def getRestrictingInfo(self, account, from_address=None): """ Get the lock position information. :param account: Locked account release account :param from_address: Used to call the rpc call method :return: todo fill """ # if account[:2] == '0x': # account = account[2:] account = bech32_address_bytes(account) data = rlp.encode([rlp.encode(int(4100)), rlp.encode(account)]) raw_data = call_obj(self, from_address, self.web3.restrictingAddress, data) receive = json.loads(str(raw_data, encoding="ISO-8859-1")) try: raw_data_dict = receive["Ret"] raw_data_dict["balance"] = int(raw_data_dict["balance"], 16) raw_data_dict["Pledge"] = int(raw_data_dict["Pledge"], 16) raw_data_dict["debt"] = int(raw_data_dict["debt"], 16) if raw_data_dict["plans"]: for i in raw_data_dict["plans"]: i["amount"] = int(i["amount"], 16) receive["Ret"] = raw_data_dict except: ... return receive
def test_staking_gas(client_new_node): external_id = "external_id" node_name = "node_name" website = "website" details = "details" node = client_new_node.node economic = client_new_node.economic benifit_address1, pri_key = economic.account.generate_account( node.web3, economic.create_staking_limit * 2) balance1 = node.eth.getBalance(benifit_address1) log.info(balance1) program_version_sign_ = node.program_version_sign[2:] benifit_address = bech32_address_bytes(benifit_address1) # result = client_new_node.ppos.createStaking(0, benifit_address, node.node_id, external_id, # node_name, website, # details, economic.create_staking_limit, # node.program_version, node.program_version_sign, node.blspubkey, # node.schnorr_NIZK_prove, # pri_key, reward_per=0) # # assert_code(result, 0) data = HexBytes( rlp.encode([ rlp.encode(int(1000)), rlp.encode(0), rlp.encode(benifit_address), rlp.encode(bytes.fromhex(node.node_id)), rlp.encode(external_id), rlp.encode(node_name), rlp.encode(website), rlp.encode(details), rlp.encode(economic.create_staking_limit), rlp.encode(600), rlp.encode(node.program_version), rlp.encode(bytes.fromhex(program_version_sign_)), rlp.encode(bytes.fromhex(node.blspubkey)), rlp.encode(bytes.fromhex(node.schnorr_NIZK_prove)) ])) esgas = node.eth.estimateGas({ "from": benifit_address1, "to": node.ppos.stakingAddress, "data": data }) print('esgas', esgas) gas = get_the_dynamic_parameter_gas_fee(data) + 21000 + 6000 + 32000 log.info('gas: {}'.format(gas)) gasPrice = node.eth.gasPrice log.info(gasPrice) time.sleep(2) balance2 = node.eth.getBalance(benifit_address1) log.info(balance2) assert esgas == gas
def getRelatedListByDelAddr(self, del_addr, from_address=None): """ Query the NodeID and pledge ID of the node entrusted by the current account address :param del_addr: Client's account address :param from_address: Used to call the rpc call method :return: todo fill """ del_addr = bech32_address_bytes(del_addr) data = rlp.encode([rlp.encode(int(1103)), rlp.encode(del_addr)]) raw_data = call_obj(self, from_address, self.web3.stakingAddress, data) return parse_str(raw_data)
def test_edit_candidate_gas(client_new_node): external_id = "external_id" node_name = "node_name" website = "website" details = "details" reward_per = 0 client = client_new_node node = client.node economic = client.economic client.ppos.need_quota_gas = False benifit_address, pri_key = economic.account.generate_account( node.web3, economic.create_staking_limit * 2) result = client.staking.create_staking(0, benifit_address, benifit_address) assert_code(result, 0) balance1 = node.eth.getBalance(benifit_address) log.info(balance1) result = client.ppos.editCandidate(benifit_address, node.node_id, external_id, node_name, website, details, pri_key, reward_per=reward_per) assert_code(result, 0) balance2 = node.eth.getBalance(benifit_address) log.info(balance2) rlp_benifit_address = rlp.encode( bech32_address_bytes(benifit_address)) #if benifit_address else b'' rlp_external_id = rlp.encode(external_id) #if external_id else b'' rlp_node_name = rlp.encode(node_name) #if node_name else b'' rlp_website = rlp.encode(website) #if website else b'' rlp_details = rlp.encode(details) #if details else b'' rlp_reward_per = rlp.encode(reward_per) if reward_per else b'' data = HexBytes( rlp.encode([ rlp.encode(int(1001)), rlp_benifit_address, rlp.encode(bytes.fromhex(node.node_id)), rlp_reward_per, rlp_external_id, rlp_node_name, rlp_website, rlp_details ])) estimated_gas = node.eth.estimateGas({ "from": benifit_address, "to": node.ppos.stakingAddress, "data": data }) print(estimated_gas) gas = get_the_dynamic_parameter_gas_fee(data) + 21000 + 6000 + 12000 log.info(gas) gasPrice = node.eth.gasPrice log.info(gasPrice) assert balance1 - gas * gasPrice == balance2
def editCandidate(self, benifit_address, node_id, external_id, node_name, website, details, pri_key, reward_per, transaction_cfg=None): """ Modify staking information :param benifit_address: Income account for accepting block rewards and staking rewards :param node_id: The idled node Id (also called the candidate's node Id) :param external_id: External Id (with length limit, Id for the third party to pull the node description) :param node_name: The name of the staking node (with a length limit indicating the name of the node) :param website: The third-party home page of the node (with a length limit indicating the home page of the node) :param details: Description of the node (with a length limit indicating the description of the node) :param pri_key: Private key for transaction :param reward_per: Proportion of the reward share obtained from the commission, using BasePoint 1BP = 0.01% :param transaction_cfg: Transaction basic configuration type: dict example:cfg = { "gas":100000000, "gasPrice":2000000000000, "nonce":1, } :return: if is need analyze return transaction result dict if is not need analyze return transaction hash """ # if benifit_address[:2] == '0x': # benifit_address = benifit_address[2:] rlp_benifit_address = rlp.encode( bech32_address_bytes(benifit_address)) if benifit_address else b'' rlp_external_id = rlp.encode(external_id) if external_id else b'' rlp_node_name = rlp.encode(node_name) if node_name else b'' rlp_website = rlp.encode(website) if website else b'' rlp_details = rlp.encode(details) if details else b'' rlp_reward_per = rlp.encode(reward_per) if reward_per else b'' data = HexBytes( rlp.encode([ rlp.encode(int(1001)), rlp_benifit_address, rlp.encode(bytes.fromhex(node_id)), rlp_reward_per, rlp_external_id, rlp_node_name, rlp_website, rlp_details ])).hex() return send_obj_transaction(self, data, self.stakingAddress, pri_key, transaction_cfg)
def createRestrictingPlan(self, account, plan, pri_key, transaction_cfg=None): """ Create a lockout plan :param account: Locked account release account :param plan: An is a list of RestrictingPlan types (array), and RestrictingPlan is defined as follows: type RestrictingPlan struct { Epoch uint64 Amount *big.Int } where Epoch: represents a multiple of the billing period. The product of the number of blocks per billing cycle indicates that the locked fund s are released at the target block height. Epoch * The number of blocks per cycle is at least greater than the maximum irreversible block height. Amount: indicates the amount to be released on the target block. :param pri_key: Private key for transaction :param transaction_cfg: Transaction basic configuration type: dict example:cfg = { "gas":100000000, "gasPrice":2000000000000, "nonce":1, } :return: if is need analyze return transaction result dict if is not need analyze return transaction hash """ # if account[:2] == '0x': # account = account[2:] account = bech32_address_bytes(account) plan_list = [] for dict_ in plan: v = [dict_[k] for k in dict_] plan_list.append(v) rlp_list = rlp.encode(plan_list) data = rlp.encode( [rlp.encode(int(4000)), rlp.encode(account), rlp_list]) return send_obj_transaction(self, data, self.web3.restrictingAddress, pri_key, transaction_cfg)
def getDelegateReward(self, from_address, node_ids=[]): node_id_bytes = [bytes.fromhex(node_id) for node_id in node_ids] tmp_from_address = bech32_address_bytes(from_address) data = [ rlp.encode(int(5100)), rlp.encode(tmp_from_address), rlp.encode(node_id_bytes) ] data = rlp.encode(data) raw_data = call_obj(self, from_address, self.web3.delegateRewardAddress, data) receive = json.loads(str(raw_data, encoding="ISO-8859-1")) try: raw_data_dict = receive["Ret"] result = [] for d in raw_data_dict: d["reward"] = int(d["reward"], 16) result.append(d) receive["Ret"] = result except: ... return receive
def getDelegateInfo(self, staking_blocknum, del_address, node_id, from_address=None): """ Query current single delegation information :param staking_blocknum: Block height at the time of staking :param del_address: Client's account address :param node_id: Verifier's node ID :param from_address: Used to call the rpc call method :return: todo fill """ del_address = bech32_address_bytes(del_address) data = rlp.encode([ rlp.encode(int(1104)), rlp.encode(staking_blocknum), rlp.encode(del_address), rlp.encode(bytes.fromhex(node_id)) ]) raw_data = call_obj(self, from_address, self.web3.stakingAddress, data) receive = json.loads(str(raw_data, encoding="utf8")) try: raw_data_dict = receive["Ret"] raw_data_dict["Released"] = int(raw_data_dict["Released"], 16) raw_data_dict["ReleasedHes"] = int(raw_data_dict["ReleasedHes"], 16) raw_data_dict["RestrictingPlan"] = int( raw_data_dict["RestrictingPlan"], 16) raw_data_dict["RestrictingPlanHes"] = int( raw_data_dict["RestrictingPlanHes"], 16) raw_data_dict["CumulativeIncome"] = int( raw_data_dict["CumulativeIncome"], 16) # raw_data_dict["Reduction"] = int(raw_data_dict["Reduction"], 16) receive["Ret"] = raw_data_dict except: ... return receive
def ecrecover(self, block_identifier): block = self.getBlock(block_identifier) extra = block.proofOfAuthorityData[0:32] sign = block.proofOfAuthorityData[32:] miner = bech32_address_bytes(remove_0x_prefix(block.miner)) raw_data = [ bytes.fromhex(remove_0x_prefix(block.parentHash.hex())), miner, bytes.fromhex(remove_0x_prefix(block.stateRoot.hex())), bytes.fromhex(remove_0x_prefix(block.transactionsRoot.hex())), bytes.fromhex(remove_0x_prefix(block.receiptsRoot.hex())), bytes.fromhex(remove_0x_prefix(block.logsBloom.hex())), block.number, block.gasLimit, block.gasUsed, block.timestamp, extra, bytes.fromhex(remove_0x_prefix(block.nonce)) ] message_hash = sha3.keccak_256(rlp.encode(raw_data)).digest() hash_bytes = HexBytes(message_hash) signature_bytes = HexBytes(sign) signature_bytes_standard = to_standard_signature_bytes(signature_bytes) signature_obj = self.account._keys.Signature( signature_bytes=signature_bytes_standard) return remove_0x_prefix( signature_obj.recover_public_key_from_msg_hash( hash_bytes).to_hex())
def createStaking(self, typ, benifit_address, node_id, external_id, node_name, website, details, amount, program_version, program_version_sign, bls_pubkey, bls_proof, pri_key, reward_per, transaction_cfg=None): """ Initiate Staking :param typ: Indicates whether the account free amount or the account's lock amount is used for staking, 0: free amount; 1: lock amount :param benifit_address: Income account for accepting block rewards and staking rewards :param node_id: The idled node Id (also called the candidate's node Id) :param external_id: External Id (with length limit, Id for the third party to pull the node description) :param node_name: The name of the staking node (with a length limit indicating the name of the node) :param website: The third-party home page of the node (with a length limit indicating the home page of the node) :param details: Description of the node (with a length limit indicating the description of the node) :param amount: staking von (unit:von, 1LAT = 10**18 von) :param program_version: The real version of the program, admin_getProgramVersion :param program_version_sign: The real version of the program is signed, admin_getProgramVersion :param bls_pubkey: Bls public key :param bls_proof: Proof of bls, obtained by pulling the proof interface, admin_getSchnorrNIZKProve :param pri_key: Private key for transaction :param reward_per: Proportion of the reward share obtained from the commission, using BasePoint 1BP = 0.01% :param transaction_cfg: Transaction basic configuration type: dict example:cfg = { "gas":100000000, "gasPrice":2000000000000, "nonce":1, } :return: if is need analyze return transaction result dict if is not need analyze return transaction hash """ benifit_address = bech32_address_bytes(benifit_address) if program_version_sign[:2] == '0x': program_version_sign = program_version_sign[2:] data = HexBytes( rlp.encode([ rlp.encode(int(1000)), rlp.encode(typ), rlp.encode(benifit_address), rlp.encode(bytes.fromhex(node_id)), rlp.encode(external_id), rlp.encode(node_name), rlp.encode(website), rlp.encode(details), rlp.encode(amount), rlp.encode(reward_per), rlp.encode(program_version), rlp.encode(bytes.fromhex(program_version_sign)), rlp.encode(bytes.fromhex(bls_pubkey)), rlp.encode(bytes.fromhex(bls_proof)) ])).hex() return send_obj_transaction(self, data, self.web3.stakingAddress, pri_key, transaction_cfg)