def vote(param, address, offline, config, style): """ this is government submodule vote command. """ if not os.path.isfile(param): cust_print('file {} not exits! please check!'.format(param), fg='r') sys.exit(1) params = read_json_file(param) try: _params = {'verifier': params['verifier'], 'proposalId': params['proposalId'], 'option': int(params['option'])} except KeyError as e: cust_print('vote need params {},but it does not exist,please check!'.format(e), fg='r') sys.exit(1) wallet_dir = g_dict_dir_config["wallet_dir"] _, private_key, _, _ = verify_password(address, wallet_dir) ppos = get_eth_obj(config, 'ppos') pip = get_eth_obj(config, 'pip') msg = ppos.admin.getProgramVersion() program_version = msg['Version'] version_sign = msg['Sign'] try: if offline: data = rlp.encode( [rlp.encode(int(2003)), rlp.encode(bytes.fromhex(params['verifier'])), rlp.encode(bytes.fromhex(params['proposalId'])), rlp.encode(params['option']), rlp.encode(int(program_version)), rlp.encode(bytes.fromhex(version_sign))]) _params['transaction_cfg'] = params.get('transaction_cfg', None) _params['to_type'] = 'pip' transaction_dict = un_sign_data(data, _params, pip, private_key[2:]) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_government_vote_{}.csv".format(get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: _params['program_version'] = program_version _params['version_sign'] = version_sign _params['pri_key'] = private_key[2:] _params['transaction_cfg'] = params.get('transaction_cfg', None) txhash = pip.vote(*_params.values()) cust_print('send raw transfer transaction successful, tx hash:{}.'.format(txhash), fg='g') except Exception as e: cust_print('vote send transfer transaction fail,error info:{}'.format(e), fg='r') sys.exit(1)
def withdrawDelegateReward(address, offline, config, style): """ this is delegate submodule withdrawDelegateReward command. """ wallet_dir = g_dict_dir_config["wallet_dir"] wallet_file_path, private_key, hrp, _ = verify_password(address, wallet_dir) ppos = get_eth_obj(config, 'ppos') _params = {'pri_key': private_key[2:], 'transaction_cfg': None} try: if offline: data = rlp.encode([rlp.encode(int(5000))]) _params['to_type'] = 'delegateReward' transaction_dict = un_sign_data(data, _params, ppos, _params['pri_key']) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_delegate_withdrawDelegateReward_{}.csv".format(get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) else: tx_hash = ppos.withdrawDelegateReward(*_params.values()) cust_print('delegate withdrawDelegateReward send transfer transaction successful, tx hash:{}.'.format(tx_hash),fg='g') except ValueError as e: cust_print('delegate withdrawDelegateReward send transfer transaction fail,error info:{}'.format(e), fg='r') sys.exit(1)
def blockNumber(config): """ this is query submodule get blockNumber command. """ platon = get_eth_obj(config) block_number = platon.blockNumber cust_print('{}'.format(block_number), fg='g')
def getBlockByHash(hash, config): """ this is query submodule get blockNumber info by block hash command. """ platon = get_eth_obj(config) block_info = dict(platon.getBlock(hash)) HexBytes_to_str(block_info) cust_print('{}'.format(json.dumps(block_info, indent=2)), fg='g')
def getPackageReward(config): """ this is query submodule get Package Reward command. """ ppos = get_eth_obj(config, 'ppos') package_reward = ppos.getPackageReward() result = '{} {}'.format(ppos.w3.fromWei(package_reward['Ret'], "ether"), ppos.hrp.upper()) cust_print('Block rewards for the current settlement cycle:{}'.format(result), fg='g')
def reportDoubleSign(param, address, offline, config, style): """ this is government submodule reportDoubleSign command. """ if not os.path.isfile(param): cust_print('file {} not exits! please check!'.format(param), fg='r') sys.exit(1) params = read_json_file(param) wallet_dir = g_dict_dir_config["wallet_dir"] _, private_key, _, _ = verify_password(address, wallet_dir) ppos = get_eth_obj(config, 'ppos') try: _params = {'typ': params['type'], 'data': params['data']} except KeyError as e: cust_print( 'reportDoubleSign need params {},but it does not exist,please check!' .format(e), fg='r') sys.exit(1) try: if offline: data = rlp.encode([ rlp.encode(int(3000)), rlp.encode(params['type']), rlp.encode(params['data']) ]) _params['transaction_cfg'] = params.get('transaction_cfg', None) _params['to_type'] = 'penalty' transaction_dict = un_sign_data(data, _params, ppos, private_key[2:]) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_government_reportDoubleSign_{}.csv".format( get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: _params['pri_key'] = private_key[2:] _params['transaction_cfg'] = params.get('transaction_cfg', None) txhash = ppos.reportDuplicateSign(*_params.values()) cust_print( 'send raw transfer transaction successful, tx hash:{}.'.format( txhash), fg='g') except Exception as e: cust_print( 'government send transfer transaction fail,error info:{}'.format( e), fg='r') sys.exit(1)
def getAvgPackTime(config): """ this is query submodule getAvgPackTime command. """ ppos = get_eth_obj(config, 'ppos') average_time = ppos.getAvgPackTime() cust_print('Average time for block packaging:{}s'.format( average_time['Ret'] / 1000), fg='g')
def getProposal(proposal_id, config, fromaddress): """ this is government submodule getProposal command. """ try: fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress pip = get_eth_obj(config, 'pip') result = pip.getProposal(proposal_id, fromaddress) cust_print('{}'.format(json.dumps(result['Ret'], indent=2)), fg='g') except Exception as e: cust_print('government getProposal exception,error info:{}'.format(e), fg='r') sys.exit(1)
def getRelatedListByDelAddress(address, config, fromaddress): """ this is delegate submodule getRelatedListByDelAddr command. """ try: ppos = get_eth_obj(config, 'ppos') fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress result = ppos.getRelatedListByDelAddr(address, fromaddress) for obj in result['Ret']: cust_print('{}'.format(json.dumps(obj, indent=2)), fg='g') except Exception as e: cust_print('Query exception,{}'.format(e), fg='r') sys.exit(1)
def getAccuVerifiersCount(proposal_id, hash, config, fromaddress): """ this is government submodule getAccuVerifiersCount command. """ try: fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress pip = get_eth_obj(config, 'pip') result = pip.getAccuVerifiersCount(proposal_id, hash, fromaddress) cust_print('{}'.format(result['Ret']), fg='g') except Exception as e: cust_print( 'government getGovernParamValue exception,error info:{}'.format(e), fg='r') sys.exit(1)
def getActiveVersion(config, fromaddress): """ this is government submodule getActiveVersion command. """ try: fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress pip = get_eth_obj(config, 'pip') result = pip.getActiveVersion(fromaddress) cust_print('{}'.format(result['Ret']), fg='g') except Exception as e: cust_print( 'government getActiveVersion exception,error info:{}'.format(e), fg='r') sys.exit(1)
def GetRestrictingInfo(address, config, fromaddress): """ this is hedge submodule GetRestrictingInfo command. """ try: fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress ppos = get_eth_obj(config, 'ppos') result = ppos.getRestrictingInfo(address, fromaddress) cust_print('{}'.format(json.dumps(result['Ret'], indent=2)), fg='g') except Exception as e: cust_print( 'hedge GetRestrictingInfo exception,error info:{}'.format(e), fg='r') sys.exit(1)
def listGovernParam(module, config, fromaddress): """ this is government submodule listGovernParam command. """ try: fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress pip = get_eth_obj(config, 'pip') result = pip.listGovernParam(module, fromaddress) for obj in result['Ret']: cust_print('{}'.format(json.dumps(obj, indent=2)), fg='g') except Exception as e: cust_print( 'government listGovernParam exception,error info:{}'.format(e), fg='r') sys.exit(1)
def checkDuplicateSign(type, nodeid, blocknumber, config, fromaddress): """ this is government submodule checkDuplicateSign command. """ try: fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress ppos = get_eth_obj(config, 'ppos') result = ppos.checkDuplicateSign(int(type), nodeid, blocknumber, fromaddress) cust_print('{}'.format(result), fg='g') except Exception as e: cust_print( 'government checkDuplicateSign exception,error info:{}'.format(e), fg='r') sys.exit(1)
def unStaking(address, file, config, offline, style): """ this is staking submodule unStaking command. """ params = read_json_file(file) node_id = params['node_id'] transaction_cfg = params['transaction_cfg'] check_node_id(node_id) wallet_dir = g_dict_dir_config["wallet_dir"] _, private_key, _, _ = verify_password(address, wallet_dir) private_key = private_key[2:] ppos = get_eth_obj(config, 'ppos') try: if offline: unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_staking_unStaking_{}.csv".format( get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) data = rlp.encode( [rlp.encode(int(1003)), rlp.encode(bytes.fromhex(node_id))]) params['to_type'] = 'staking' transaction_dict = un_sign_data(data, params, ppos, private_key) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: tx_hash = ppos.withdrewStaking(node_id, private_key, transaction_cfg) cust_print( 'withdrewStaking send transfer transaction successful, tx hash:{}.' .format(tx_hash), fg='g') except ValueError as e: cust_print( 'unStaking send transfer transaction fail,error info:{}'.format(e), fg='r') sys.exit(1)
def send_offline(data, config): """ this is tx submodule send_offline command. """ signed_tx_dir = g_dict_dir_config["signed_tx_dir"] abspath = os.path.join(signed_tx_dir, data) if not os.path.isfile(abspath): cust_print('{} not exists,please check!'.format(abspath), fg='r') sys.exit(1) try: with open(abspath, 'r') as f: result = f.read() sign_data = ast.literal_eval(result) except: cust_print('analytical exception,please check!', fg='r') sys.exit(1) try: # 发送交易 platon = get_eth_obj(config) tx_hash_list = [] for data in sign_data: txhash, _ = send_transaction(platon, data) tx_hash_list.append(txhash) txhash = ','.join(tx_hash_list) except Exception as e: cust_print( 'send raw transfer transaction failure, error message:{}!!!'. format(e), fg='r') sys.exit(1) else: dir_name = os.path.dirname(abspath) base_name = os.path.basename(abspath) new_base_name = base_name + '.done' os.rename(abspath, os.path.join(dir_name, new_base_name)) cust_print( 'send raw transfer transaction successful, tx hash:{}.'.format( txhash), fg='g') cust_print('send raw transfer transaction successful, {} to {}'.format( base_name, new_base_name), fg='g')
def getTransaction(hash, config): """ this is tx submodule get transaction command. """ platon = get_eth_obj(config) try: # 交易信息 transaction = dict(platon.getTransaction(hash)) HexBytes_to_str(transaction) # 交易回执信息 transaction_receipt = dict(platon.getTransactionReceipt(hash)) HexBytes_to_str(transaction_receipt) except Exception as e: cust_print( 'Failed to query transaction information,error message:{}.'.format( e)) sys.exit(1) cust_print('query transaction information successful!', fg='g') info = "transaction:\n" info += "{}\n".format(json.dumps(dict(transaction), indent=2)) info += "\n\ntransaction receipt:\n" info += "{}".format(json.dumps(dict(transaction_receipt), indent=2)) cust_print('{}'.format(info), fg='g')
def getDelegateReward(param, config): """ this is delegate submodule getDelegateReward command. """ ppos = get_eth_obj(config, 'ppos') if not os.path.isfile(param): cust_print('file {} not exits! please check!'.format(param), fg='r') sys.exit(1) params = read_json_file(param) _params = {} try: _params['address'] = params['address'] _params['node_ids'] = params['nodeIDs'] except KeyError as e: cust_print('Key {} does not exist in file {},please check!'.format(e, params), fg='r') sys.exit(1) try: receive = ppos.getDelegateReward(*_params.values()) for node in receive['Ret']: cust_print('{}'.format(json.dumps(node, indent=2)), fg='g') except ValueError as e: cust_print('delegate getDelegateReward send transfer transaction fail,error info:{}'.format(e), fg='r') sys.exit(1)
def query(function, nodeid, config, fromaddress): """ this is staking submodule query command. """ fromaddress = None if fromaddress.replace(" ", "") == "" else fromaddress ppos = get_eth_obj(config, 'ppos') try: if function == 'getCandidateInfo': info = getattr(ppos, function)(nodeid, fromaddress) cust_print('node info:\n{}'.format( json.dumps(info['Ret'], indent=2)), fg='g') elif function == 'getStakingReward': reward = ppos.w3.fromWei(getattr(ppos, function)()['Ret'], "ether") cust_print( 'Pledge rewards for the current settlement cycle is:{}'.format( reward), fg='g') else: for obj in getattr(ppos, function)(fromaddress)['Ret']: cust_print('{}'.format(json.dumps(obj, indent=2)), fg='g') except Exception as e: cust_print('Query exception,{}'.format(e), fg='r') sys.exit(1)
def create(file, address, template, config, offline, style): """ this is staking submodule create command. """ if template: show_params() return if not os.path.isfile(file): cust_print('file {} not exits! please check!'.format(file), fg='r') sys.exit(1) params = read_json_file(file) wallet_dir = g_dict_dir_config["wallet_dir"] wallet_file_path, private_key, hrp, _ = verify_password(address, wallet_dir) ppos = get_eth_obj(config, 'ppos') msg = ppos.admin.getProgramVersion() bls_pubkey = ppos.admin.nodeInfo.blsPubKey bls_proof = ppos.admin.getSchnorrNIZKProve() program_version = msg['Version'] program_version_sign = msg['Sign'] _params = {} try: _params['typ'] = params['typ'] _params['benifit_address'] = params['benifit_address'] _params['node_id'] = params['node_id'] or ppos.admin.nodeInfo.id _params['external_id'] = params['external_id'] _params['node_name'] = params['node_name'] _params['website'] = params['website'] _params['details'] = params['details'] _params['amount'] = ppos.w3.toWei(str(params['amount']), "ether") _params['program_version'] = params['program_version'] or program_version _params['program_version_sign'] = params['program_version_sign'] or program_version_sign _params['bls_pubkey'] = params['bls_pubkey'] or bls_pubkey _params['bls_proof'] = params['bls_proof'] or bls_proof _params['pri_key'] = private_key[2:] _params['reward_per'] = params['reward_per'] if isinstance(params['transaction_cfg'], dict): _params['transaction_cfg'] = params['transaction_cfg'] except KeyError as e: cust_print('Key {} does not exist in file {},please check!'.format(e, file), fg='r') sys.exit(1) try: if offline: data = rlp_params(tuple(_params.values()), hrp) params['to_type'] = 'staking' transaction_dict = un_sign_data(data, params, ppos, _params['pri_key']) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_staking_create_{}.csv".format(get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: tx_hash = ppos.createStaking(*_params.values()) cust_print('createStaking send transfer transaction successful, tx hash:{}.'.format(tx_hash), fg='g') except ValueError as e: cust_print('createStaking send transfer transaction fail,error info:{}'.format(e)) sys.exit(1)
def submitProposal(param, address, offline, module, config, style): """ this is government submodule submitProposal command. """ if not os.path.isfile(param): cust_print('file {} not exits! please check!'.format(param), fg='r') sys.exit(1) params = read_json_file(param) check_params(params) _params = {'verifier': params['verifier'], 'pip_id': params['pIDID']} _module, func = method_module[module] try: if _module == 'submitVersion': _params['new_version'] = params['newVersion'] _params['end_voting_rounds'] = params['endVotingRound'] if _module == 'submitCancel': _params['end_voting_rounds'] = params['endVotingRound'] _params['tobe_canceled_proposal_id'] = params['canceledProposalID'] if _module == 'submitParam': _params['module'] = params['module'] _params['name'] = params['name'] _params['new_value'] = params['newValue'] except KeyError as e: cust_print( '{} need params {},but it does not exist,please check!'.format( module, e), fg='r') sys.exit(1) wallet_dir = g_dict_dir_config["wallet_dir"] _, private_key, _, _ = verify_password(address, wallet_dir) pip = get_eth_obj(config, 'pip') module, func = method_module[module] try: if offline: data = rlp_params(func, *_params.values()) _params['transaction_cfg'] = params.get('transaction_cfg', None) _params['to_type'] = 'pip' transaction_dict = un_sign_data(data, _params, pip, private_key[2:]) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_submitProposal_{}_{}.csv".format( module, get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: _params['pri_key'] = private_key[2:] _params['transaction_cfg'] = params.get('transaction_cfg', None) txhash = getattr(pip, module)(*_params.values()) cust_print( 'send raw transfer transaction successful, tx hash:{}.'.format( txhash), fg='g') except Exception as e: cust_print( 'submitProposal {} send transfer transaction fail,error info:{}'. format(module, e), fg='r') sys.exit(1)
def increase(file, address, template, config, offline, style): """ this is staking submodule increase command. """ if template: show_params() return if not os.path.isfile(file): cust_print('file {} not exits! please check!'.format(file), fg='r') sys.exit(1) params = read_json_file(file) wallet_dir = g_dict_dir_config["wallet_dir"] wallet_file_path, private_key, hrp, _ = verify_password( address, wallet_dir) ppos = get_eth_obj(config, 'ppos') _params = {} try: _params['typ'] = params['typ'] _params['node_id'] = params['node_id'] or ppos.admin.nodeInfo.id _params['amount'] = ppos.w3.toWei(str(params['amount']), "ether") _params['pri_key'] = private_key[2:] if isinstance(params['transaction_cfg'], dict): _params['transaction_cfg'] = params['transaction_cfg'] except KeyError as e: cust_print('Key {} does not exist in file {},please check!'.format( e, file), fg='r') sys.exit(1) try: if offline: data = HexBytes( rlp.encode([ rlp.encode(int(1002)), rlp.encode(bytes.fromhex(_params['node_id'])), rlp.encode(_params['typ']), rlp.encode(_params['amount']) ])).hex() params['to_type'] = 'staking' transaction_dict = un_sign_data(data, params, ppos, _params['pri_key']) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_staking_increase_{}.csv".format( get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: tx_hash = ppos.increaseStaking(*_params.values()) cust_print( 'increase staking send transfer transaction successful, tx hash:{}.' .format(tx_hash), fg='g') except ValueError as e: cust_print( 'increase staking send transfer transaction fail,error info:{}'. format(e), fg='r') sys.exit(1)
def createRestrictingPlan(param, address, offline, config, style): """ this is government submodule vote command. """ if not os.path.isfile(param): cust_print('file {} not exits! please check!'.format(param), fg='r') sys.exit(1) params = read_json_file(param) ppos = get_eth_obj(config, 'ppos') try: params['plans'] = [{ 'epoch': plan['epoch'], 'amount': ppos.web3.toWei(str(plan['amount']), "ether") } for plan in params['plans']] _params = {'account': params['account'], 'plans': params['plans']} except KeyError as e: cust_print( 'createRestrictingPlan need params {},but it does not exist,please check!' .format(e), fg='r') sys.exit(1) wallet_dir = g_dict_dir_config["wallet_dir"] _, private_key, hrp, _ = verify_password(address, wallet_dir) try: if offline: account = bech32_address_bytes(hrp)(params['account']) plan_list = [[plan[k] for k in plan] for plan in params['plans']] data = rlp.encode([ rlp.encode(int(4000)), rlp.encode(account), rlp.encode(plan_list) ]) _params['transaction_cfg'] = params.get('transaction_cfg', None) _params['to_type'] = 'restricting' transaction_dict = un_sign_data(data, _params, ppos, private_key[2:]) unsigned_tx_dir = g_dict_dir_config["unsigned_tx_dir"] check_dir_exits(unsigned_tx_dir) unsigned_file_csv_name = "unsigned_hedge_createRestrictingPlan_{}.csv".format( get_time_stamp()) unsigned_file_path = os.path.join(unsigned_tx_dir, unsigned_file_csv_name) if style == '': write_csv(unsigned_file_path, [transaction_dict]) else: unsigned_file_path = unsigned_file_path.replace('csv', 'jpg') write_QRCode(transaction_dict, unsigned_file_path) cust_print('unsigned_file save to:{}'.format(unsigned_file_path), fg='g') else: _params['pri_key'] = private_key[2:] _params['transaction_cfg'] = params.get('transaction_cfg', None) txhash = ppos.createRestrictingPlan(*_params.values()) cust_print( 'send raw transfer transaction successful, tx hash:{}.'.format( txhash), fg='g') except Exception as e: cust_print( 'createRestrictingPlan send transfer transaction fail,error info:{}' .format(e), fg='r') sys.exit(1)