def _split_batch_list(args, batch_list): new_list = [] for batch in batch_list.batches: new_list.append(batch) if len(new_list) == args.batch_size_limit: yield batch_pb2.BatchList(batches=new_list) new_list = [] if new_list: yield batch_pb2.BatchList(batches=new_list)
def load(self, data): batch_list = batch_pb2.BatchList() batch_list.ParseFromString(data) for batch in batch_list.batches: self._verify_batch(batch) self._dispatcher.on_batch_received(batch)
def _assert_validator_transaction(self, public_key, target_file): filename = os.path.join(self._temp_dir, target_file) batch_list = batch_pb.BatchList() with open(filename, 'rb') as batch_file: batch_list.ParseFromString(batch_file.read()) self.assertEqual(1, len(batch_list.batches)) batch = batch_list.batches[0] self.assertEqual(1, len(batch.transactions)) batch_header = batch_pb.BatchHeader() batch_header.ParseFromString(batch.header) self.assertEqual(public_key, batch_header.signer_public_key) txn = batch.transactions[0] txn_header = txn_pb.TransactionHeader() txn_header.ParseFromString(txn.header) self.assertEqual(public_key, txn_header.signer_public_key) self.assertEqual('sawtooth_validator_registry', txn_header.family_name) payload = vr_pb.ValidatorRegistryPayload() payload.ParseFromString(txn.payload) self._assert_key_state(payload.signup_info.poet_public_key)
def submit_next_batch(self): batch_list_bytes = self.make_batch(len(self.batches)) batch_list = batch_pb2.BatchList() batch_list.ParseFromString(batch_list_bytes) self.batches.append(batch_list.batches[0]) self._post_batch(batch_list_bytes) return len(self.batches) - 1
def do_batch_submit(args): try: with open(args.filename, mode='rb') as fd: batches = batch_pb2.BatchList() batches.ParseFromString(fd.read()) except IOError as e: raise CliException(e) rest_client = RestClient(args.url, args.user) start = time.time() for batch_list in _split_batch_list(args, batches): rest_client.send_batches(batch_list) stop = time.time() print('batches: {}, batch/sec: {}'.format( str(len(batches.batches)), len(batches.batches) / (stop - start))) if args.wait and args.wait > 0: batch_ids = [b.header_signature for b in batches.batches] wait_time = 0 start_time = time.time() while wait_time < args.wait: statuses = rest_client.get_statuses(batch_ids, args.wait - int(wait_time)) wait_time = time.time() - start_time if all(s['status'] == 'COMMITTED' for s in statuses): print('All batches committed in {:.6} sec'.format(wait_time)) return # Wait a moment so as not to send another request immediately time.sleep(0.2) print('Wait timed out! Some batches have not yet been committed...') for batch_id, status in statuses[0].items(): print('{} {}'.format(batch_id, status)) exit(1)
def do_create(args): """Executes the `poet registration` subcommand. This command generates a validator registry transaction and saves it to a file, whose location is determined by the args. The signup data, generated by the selected enclave, is also stored in a well-known location. """ signer = _read_signer(args.key) public_key = signer.get_public_key().as_hex() public_key_hash = sha256(public_key.encode()).hexdigest() with PoetEnclaveModuleWrapper( enclave_module=args.enclave_module, config_dir=config.get_config_dir(), data_dir=config.get_data_dir()) as poet_enclave_module: signup_info = SignupInfo.create_signup_info( poet_enclave_module=poet_enclave_module, originator_public_key_hash=public_key_hash, nonce=SignupInfo.block_id_to_nonce(args.block)) print('Writing key state for PoET public key: {}...{}'.format( signup_info.poet_public_key[:8], signup_info.poet_public_key[-8:])) # Store the newly-created PoET key state, associating it with its # corresponding public key poet_key_state_store = \ PoetKeyStateStore( data_dir=config.get_data_dir(), validator_id=public_key) poet_key_state_store[signup_info.poet_public_key] = \ PoetKeyState( sealed_signup_data=signup_info.sealed_signup_data, has_been_refreshed=False) # Create the validator registry payload payload = \ vr_pb.ValidatorRegistryPayload( verb='register', name='validator-{}'.format(public_key[:8]), id=public_key, signup_info=vr_pb.SignUpInfo( poet_public_key=signup_info.poet_public_key, proof_data=signup_info.proof_data, anti_sybil_id=signup_info.anti_sybil_id, nonce=SignupInfo.block_id_to_nonce(args.block))) serialized = payload.SerializeToString() # Create the address that will be used to look up this validator # registry transaction. Seems like a potential for refactoring.. validator_entry_address = \ VR_NAMESPACE + sha256(public_key.encode()).hexdigest() # Create a transaction header and transaction for the validator # registry update amd then hand it off to the batch publisher to # send out. output_addresses = [validator_entry_address, VALIDATOR_MAP_ADDRESS] input_addresses = \ output_addresses + \ [SettingsView.setting_address('sawtooth.poet.report_public_key_pem'), SettingsView.setting_address('sawtooth.poet.' 'valid_enclave_measurements'), SettingsView.setting_address('sawtooth.poet.valid_enclave_basenames')] header = \ txn_pb.TransactionHeader( signer_public_key=public_key, family_name='sawtooth_validator_registry', family_version='1.0', inputs=input_addresses, outputs=output_addresses, dependencies=[], payload_sha512=sha512(serialized).hexdigest(), batcher_public_key=public_key, nonce=time.time().hex().encode()).SerializeToString() signature = signer.sign(header) transaction = \ txn_pb.Transaction( header=header, payload=serialized, header_signature=signature) batch = _create_batch(signer, [transaction]) batch_list = batch_pb.BatchList(batches=[batch]) try: print('Generating {}'.format(args.output)) 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)))
def do_genesis(args): """Executes the `poet genesis` subcommand. This command generates a validator registry transaction and saves it to a file, whose location is determined by the args. The signup data, generated by the selected enclave, is also stored in a well-known location. """ if args.enclave_module == 'simulator': module_name = SIMUATOR_MODULE elif args.enclave_module == 'sgx': module_name = SGX_MODULE else: raise AssertionError('Unknown enclave module: {}'.format( args.enclave_module)) try: poet_enclave_module = importlib.import_module(module_name) except ImportError as e: raise AssertionError(str(e)) poet_enclave_module.initialize(**{}) pubkey, signing_key = _read_signing_keys(args.key) public_key_hash = sha256(pubkey.encode()).hexdigest() signup_info = SignupInfo.create_signup_info( poet_enclave_module=poet_enclave_module, validator_address=pubkey, originator_public_key_hash=public_key_hash, nonce=NULL_BLOCK_IDENTIFIER) print('Writing key state for PoET public key: {}...{}'.format( signup_info.poet_public_key[:8], signup_info.poet_public_key[-8:])) # Store the newly-created PoET key state, associating it with its # corresponding public key poet_key_state_store = \ PoetKeyStateStore( data_dir=config.get_data_dir(), validator_id=pubkey) poet_key_state_store[signup_info.poet_public_key] = \ PoetKeyState( sealed_signup_data=signup_info.sealed_signup_data, has_been_refreshed=False) # Create the validator registry payload payload = \ vr_pb.ValidatorRegistryPayload( verb='register', name='validator-{}'.format(pubkey[:8]), id=pubkey, signup_info=vr_pb.SignUpInfo( poet_public_key=signup_info.poet_public_key, proof_data=signup_info.proof_data, anti_sybil_id=signup_info.anti_sybil_id, nonce=NULL_BLOCK_IDENTIFIER)) serialized = payload.SerializeToString() # Create the address that will be used to look up this validator # registry transaction. Seems like a potential for refactoring.. validator_entry_address = \ VR_NAMESPACE + sha256(pubkey.encode()).hexdigest() # Create a transaction header and transaction for the validator # registry update amd then hand it off to the batch publisher to # send out. addresses = [validator_entry_address, VALIDATOR_MAP_ADDRESS] header = \ txn_pb.TransactionHeader( signer_pubkey=pubkey, family_name='sawtooth_validator_registry', family_version='1.0', inputs=addresses, outputs=addresses, dependencies=[], payload_encoding="application/protobuf", payload_sha512=sha512(serialized).hexdigest(), batcher_pubkey=pubkey, nonce=time.time().hex().encode()).SerializeToString() signature = signing.sign(header, signing_key) transaction = \ txn_pb.Transaction( header=header, payload=serialized, header_signature=signature) batch = _create_batch(pubkey, signing_key, [transaction]) batch_list = batch_pb.BatchList(batches=[batch]) try: print('Generating {}'.format(args.output)) 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)))