def _do_config_proposal_vote(args): """Executes the 'proposal vote' subcommand. Given a key file, a proposal id and a vote value, it generates a batch of sawtooth_config transactions in a BatchList instance. The BatchList is file or submitted to a validator. """ pubkey, signing_key = _read_signing_keys(args.key) rest_client = RestClient(args.url) proposals = _get_proposals(rest_client) proposal = None for candidate in proposals.candidates: if candidate.proposal_id == args.proposal_id: proposal = candidate break if proposal is None: raise CliException('No proposal exists with the given id') for vote_record in proposal.votes: if vote_record.public_key == pubkey: raise CliException( 'A vote has already been recorded with this signing key') txn = _create_vote_txn(pubkey, signing_key, args.proposal_id, proposal.proposal.setting, args.vote_value) batch = _create_batch(pubkey, signing_key, [txn]) batch_list = BatchList(batches=[batch]) rest_client.send_batches(batch_list)
def _do_config_set(args): """Executes the 'set' subcommand. Given a key file, and a series of key/value pairs, it generates batches of sawtooth_config transactions in a BatchList instance, and stores it in a file. """ settings = [s.split('=', 1) for s in args.setting] with open(args.key, 'r') as key_file: wif_key = key_file.read().strip() signing_key = signing.encode_privkey( signing.decode_privkey(wif_key, 'wif'), 'hex') pubkey = signing.encode_pubkey(signing.generate_pubkey(signing_key), 'hex') txns = [ _create_config_txn(pubkey, signing_key, setting) for setting in settings ] txn_ids = [txn.header_signature for txn in txns] batch_header = BatchHeader(signer_pubkey=pubkey, transaction_ids=txn_ids).SerializeToString() batch = Batch(header=batch_header, header_signature=signing.sign(batch_header, signing_key), transactions=txns) batch_list = BatchList(batches=[batch]).SerializeToString() try: with open(args.output, 'wb') as batch_file: batch_file.write(batch_list) except: raise CliException('Unable to write to {}'.format(args.output))
def do_genesis(args, data_dir=None): """Given the command args, take an series of input files containing GenesisData, combine all the batches into one GenesisData, and output the result into a new file. """ if data_dir is None: data_dir = get_data_dir() if not os.path.exists(data_dir): raise CliException( "Data directory does not exist: {}".format(data_dir)) genesis_batches = [] for input_file in args.input_file: print('Processing {}...'.format(input_file)) input_data = BatchList() try: with open(input_file, 'rb') as in_file: input_data.ParseFromString(in_file.read()) except: raise CliException('Unable to read {}'.format(input_file)) genesis_batches += input_data.batches _validate_depedencies(genesis_batches) if args.output: genesis_file = args.output else: genesis_file = os.path.join(data_dir, 'genesis.batch') print('Generating {}'.format(genesis_file)) output_data = GenesisData(batches=genesis_batches) with open(genesis_file, 'wb') as out_file: out_file.write(output_data.SerializeToString())
def _do_config_proposal_create(args): """Executes the 'proposal create' subcommand. Given a key file, and a series of key/value pairs, it generates batches of sawtooth_config transactions in a BatchList instance. The BatchList is either stored to a file or submitted to a validator, depending on the supplied CLI arguments. """ settings = [s.split('=', 1) for s in args.setting] pubkey, signing_key = _read_signing_keys(args.key) txns = [ _create_propose_txn(pubkey, signing_key, setting) for setting in settings ] batch = _create_batch(pubkey, signing_key, txns) batch_list = BatchList(batches=[batch]) if args.output is not None: try: with open(args.output, 'wb') as batch_file: batch_file.write(batch_list.SerializeToString()) except IOError as e: raise CliException('Unable to write to batch file: {}'.format( str(e))) elif args.url is not None: rest_client = RestClient(args.url) rest_client.send_batches(batch_list) else: raise AssertionError('No target for create set.')
def _do_identity_role_create(args): """Executes the 'role create' subcommand. Given a key file, a role name, and a policy name it generates a batch of sawtooth_identity transactions in a BatchList instance. The BatchList is either stored to a file or submitted to a validator, depending on the supplied CLI arguments. """ pubkey, signing_key = _read_signing_keys(args.key) txns = [_create_role_txn(pubkey, signing_key, args.name, args.policy)] batch = _create_batch(pubkey, signing_key, txns) batch_list = BatchList(batches=[batch]) if args.output is not None: try: with open(args.output, 'wb') as batch_file: batch_file.write(batch_list.SerializeToString()) except IOError as e: raise CliException('Unable to write to batch file: {}'.format( str(e))) elif args.url is not None: rest_client = RestClient(args.url) rest_client.send_batches(batch_list) else: raise AssertionError('No target for create set.')
def _do_identity_role_create(args): """Executes the 'role create' subcommand. Given a key file, a role name, and a policy name it generates a batch of sawtooth_identity transactions in a BatchList instance. The BatchList is either stored to a file or submitted to a validator, depending on the supplied CLI arguments. """ signer = _read_signer(args.key) txns = [_create_role_txn(signer, args.name, args.policy)] batch = _create_batch(signer, txns) batch_list = BatchList(batches=[batch]) if args.output is not None: try: with open(args.output, 'wb') as batch_file: batch_file.write(batch_list.SerializeToString()) except IOError as e: raise CliException( 'Unable to write to batch file: {}'.format(str(e))) elif args.url is not None: rest_client = RestClient(args.url) rest_client.send_batches(batch_list) if args.wait and args.wait > 0: batch_id = batch.header_signature wait_time = 0 start_time = time.time() while wait_time < args.wait: statuses = rest_client.get_statuses( [batch_id], args.wait - int(wait_time)) wait_time = time.time() - start_time if statuses[0]['status'] == 'COMMITTED': print( 'Role committed in {:.6} sec'.format(wait_time)) return # Wait a moment so as not to hammer the Rest Api time.sleep(0.2) print('Wait timed out! Role was not committed...') print('{:128.128} {}'.format( batch_id, statuses[0]['status'])) exit(1) else: raise AssertionError('No target for create set.')
def make_batch(self, batch_sig, *txns): txn_ids = [txn.header_signature for txn in txns] batch_header = BatchHeader( signer_pubkey='test_pubkey', transaction_ids=txn_ids).SerializeToString() batch = Batch(header=batch_header, header_signature=batch_sig, transactions=txns) batch_list = BatchList(batches=[batch]) target_path = os.path.join(self._temp_dir, batch_sig + ".batch") with open(target_path, "wb") as f: filename = f.name f.write(batch_list.SerializeToString()) return filename
def _do_config_genesis(args): signer = _read_signer(args.key) public_key = signer.get_public_key().as_hex() authorized_keys = args.authorized_key if args.authorized_key else \ [public_key] if public_key not in authorized_keys: authorized_keys.append(public_key) txns = [] txns.append(_create_propose_txn( signer, ('sawtooth.settings.vote.authorized_keys', ','.join(authorized_keys)))) if args.approval_threshold is not None: if args.approval_threshold < 1: raise CliException('approval threshold must not be less than 1') if args.approval_threshold > len(authorized_keys): raise CliException( 'approval threshold must not be greater than the number of ' 'authorized keys') txns.append(_create_propose_txn( signer, ('sawtooth.settings.vote.approval_threshold', str(args.approval_threshold)))) batch = _create_batch(signer, txns) batch_list = BatchList(batches=[batch]) try: with open(args.output, 'wb') as batch_file: batch_file.write(batch_list.SerializeToString()) print('Generated {}'.format(args.output)) except IOError as e: raise CliException( 'Unable to write to batch file: {}'.format(str(e)))
def do_genesis(args): """Given the command args, take an series of input files containing GenesisData, combine all the batches into one GenesisData, and output the result into a new file. """ genesis_batches = [] for input_file in args.input_file: print('Processing {}...'.format(input_file)) input_data = BatchList() try: with open(input_file, 'rb') as in_file: input_data.ParseFromString(in_file.read()) except: raise CliException('Unable to read {}'.format(input_file)) genesis_batches += input_data.batches _validate_depedencies(genesis_batches) print('Generating {}'.format(args.output)) output_data = GenesisData(batches=genesis_batches) with open(args.output, 'wb') as out_file: out_file.write(output_data.SerializeToString())