def __init__(self): self.block_sender = MockBlockSender() self.block_store = BlockStore(DictDatabase()) self.block_cache = BlockCache(self.block_store) self.state_db = {} # add the mock reference to the consensus self.state_db[_setting_address('sawtooth.consensus.algorithm')] = \ _setting_entry('sawtooth.consensus.algorithm', 'test_journal.mock_consensus') self.state_view_factory = MockStateViewFactory(self.state_db) self.signing_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.signing_key), "hex") self.identity_signing_key = signing.generate_privkey() self.genesis_block = self._generate_genesis_block() self.set_chain_head(self.genesis_block) self.block_publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, squash_handler=None, chain_head=self.genesis_block, identity_signing_key=self.identity_signing_key)
def setUp(self): self.block_tree_manager = BlockTreeManager(with_genesis=False) self.gossip = MockNetwork() self.executor = SynchronousExecutor() self.txn_executor = MockTransactionExecutor() self.block_sender = MockBlockSender() self.chain_id_manager = MockChainIdManager() self.state_delta_processor = MockStateDeltaProcessor() self.chain_head_lock = RLock() def chain_updated(head, committed_batches=None, uncommitted_batches=None): pass self.chain_ctrl = ChainController( block_cache=self.block_tree_manager.block_cache, state_view_factory=MockStateViewFactory( self.block_tree_manager.state_db), block_sender=self.block_sender, executor=self.executor, transaction_executor=MockTransactionExecutor(), chain_head_lock=self.chain_head_lock, on_chain_updated=chain_updated, squash_handler=None, chain_id_manager=self.chain_id_manager, state_delta_processor=self.state_delta_processor, identity_signing_key=self.block_tree_manager.identity_signing_key, data_dir=None, config_dir=None) self.assertIsNone(self.chain_ctrl.chain_head)
def __init__(self): self.block_tree_manager = BlockTreeManager() self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.state_view_factory = MockStateViewFactory({}) self.permission_verifier = MockPermissionVerifier() self.publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_tree_manager.block_cache, state_view_factory=self.state_view_factory, settings_cache=SettingsCache( SettingsViewFactory( self.block_tree_manager.state_view_factory), ), block_sender=self.block_sender, batch_sender=self.batch_sender, squash_handler=None, chain_head=self.block_tree_manager.chain_head, identity_signer=self.block_tree_manager.identity_signer, data_dir=None, config_dir=None, check_publish_block_frequency=0.1, batch_observers=[], permission_verifier=self.permission_verifier) self.init_chain_head = self.block_tree_manager.chain_head self.result_block = None # A list of batches is created at the beginning of each test. # The test assertions and the publisher function wrappers # take these batches as a default argument. self.batch_count = 8 self.batches = self.make_batches()
def test_max_block_size(self): ''' Test block publisher obeys the block size limits ''' # Create a publisher that has a state view # with a batch limit addr, value = CreateSetting( 'sawtooth.publisher.max_batches_per_block', 1) print('test', addr) self.state_view_factory = MockStateViewFactory( {addr: value}) self.publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_tree_manager.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.batch_sender, squash_handler=None, chain_head=self.block_tree_manager.chain_head, identity_signing_key=self.block_tree_manager.identity_signing_key, data_dir=None, config_dir=None) self.assert_no_block_published() # receive batches, then try again (succeeding) self.receive_batches() # try to publish with no pending queue (failing) for i in range(self.batch_count): self.publish_block() self.assert_block_published() self.update_chain_head(BlockWrapper(self.result_block)) self.verify_block([self.batches[i]])
def setUp(self): self.state_view_factory = MockStateViewFactory() self.block_tree_manager = BlockTreeManager() self.root = self.block_tree_manager.chain_head self.block_validation_handler = self.BlockValidationHandler()
def setUp(self, mock_batch_injector_factory): mock_batch_injector_factory.create_injectors.return_value = [] self.block_tree_manager = BlockTreeManager() self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.state_view_factory = MockStateViewFactory({}) self.permission_verifier = MockPermissionVerifier() self.publisher = BlockPublisher( block_store=self.block_tree_manager.block_store, block_manager=self.block_tree_manager.block_manager, transaction_executor=MockTransactionExecutor(), state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.batch_sender, identity_signer=self.block_tree_manager.identity_signer, data_dir=None, config_dir=None, batch_observers=[], permission_verifier=self.permission_verifier, batch_injector_factory=mock_batch_injector_factory) self.init_chain_head = self.block_tree_manager.chain_head self.result_block = None # A list of batches is created at the beginning of each test. # The test assertions and the publisher function wrappers # take these batches as a default argument. self.batch_count = 8 self.batches = self.make_batches()
def setUp(self): self.block_tree_manager = BlockTreeManager() self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.state_view_factory = MockStateViewFactory({}) self.publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_tree_manager.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.batch_sender, squash_handler=None, chain_head=self.block_tree_manager.chain_head, identity_signing_key=self.block_tree_manager.identity_signing_key, data_dir=None, config_dir=None) self.init_chain_head = self.block_tree_manager.chain_head self.result_block = None # A list of batches is created at the beginning of each test. # The test assertions and the publisher function wrappers # take these batches as a default argument. self.batch_count = 8 self.batches = self.make_batches()
def __init__(self, with_genesis=True): self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.block_store = BlockStore(DictDatabase()) self.block_cache = BlockCache(self.block_store) self.state_db = {} # add the mock reference to the consensus consensus_setting_addr = ConfigView.setting_address( 'sawtooth.consensus.algorithm') self.state_db[consensus_setting_addr] = _setting_entry( 'sawtooth.consensus.algorithm', 'test_journal.mock_consensus') self.state_view_factory = MockStateViewFactory(self.state_db) self.signing_key = signing.generate_privkey() self.public_key = signing.generate_pubkey(self.signing_key) self.identity_signing_key = signing.generate_privkey() chain_head = None if with_genesis: self.genesis_block = self.generate_genesis_block() self.set_chain_head(self.genesis_block) chain_head = self.genesis_block self.block_publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.block_sender, squash_handler=None, chain_head=chain_head, identity_signing_key=self.identity_signing_key, data_dir=None, config_dir=None)
def setUp(self): self.block_tree_manager = BlockTreeManager() self.gossip = MockNetwork() self.executor = SynchronousExecutor() self.txn_executor = MockTransactionExecutor() self.block_sender = MockBlockSender() self.chain_id_manager = MockChainIdManager() def chain_updated(head, committed_batches=None, uncommitted_batches=None): pass self.chain_ctrl = ChainController( block_cache=self.block_tree_manager.block_cache, state_view_factory=MockStateViewFactory( self.block_tree_manager.state_db), block_sender=self.block_sender, executor=self.executor, transaction_executor=MockTransactionExecutor(), on_chain_updated=chain_updated, squash_handler=None, chain_id_manager=self.chain_id_manager, identity_signing_key=self.block_tree_manager.identity_signing_key, data_dir=None) init_root = self.chain_ctrl.chain_head self.assert_is_chain_head(init_root) # create a chain of length 5 extending the root _, head = self.generate_chain(init_root, 5) self.receive_and_process_blocks(head) self.assert_is_chain_head(head) self.init_head = head
def create_state_view_factory(self, values): state_db = {} if values is not None: for key, value in values.items(): state_db[ConfigView.setting_address(key)] = \ TestCheckPublishBlock._setting_entry(key, repr(value)) return MockStateViewFactory(state_db)
def __init__(self): self.state_view_factory = MockStateViewFactory() self.block_tree_manager = BlockTreeManager() self.root = self.block_tree_manager.chain_head self.block_validation_handler = self.BlockValidationHandler() self.permission_verifier = MockPermissionVerifier()
def __init__(self): self.block_sender = MockBlockSender() self.block_store = BlockStoreAdapter({}) self.block_cache = BlockCache(self.block_store) self.state_db = {} self.state_view_factory = MockStateViewFactory(self.state_db) self.signing_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.signing_key), "hex") self.genesis_block = self._generate_genesis_block() self.block_store[self.genesis_block.identifier] = self.genesis_block self.set_chain_head(self.genesis_block) self.block_publisher = BlockPublisher( consensus_module=mock_consensus, transaction_executor=MockTransactionExecutor(), block_cache=self.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, squash_handler=None, chain_head=self.genesis_block)
def __init__(self): self.block_tree_manager = BlockTreeManager() self.gossip = MockNetwork() self.txn_executor = MockTransactionExecutor() self.block_sender = MockBlockSender() self.chain_id_manager = MockChainIdManager() self._chain_head_lock = RLock() self.permission_verifier = MockPermissionVerifier() self.state_view_factory = MockStateViewFactory( self.block_tree_manager.state_db) self.transaction_executor = MockTransactionExecutor( batch_execution_result=None) self.executor = SynchronousExecutor() self.block_validator = BlockValidator( state_view_factory=self.state_view_factory, block_cache=self.block_tree_manager.block_cache, transaction_executor=self.transaction_executor, squash_handler=None, identity_signer=self.block_tree_manager.identity_signer, data_dir=None, config_dir=None, permission_verifier=self.permission_verifier, thread_pool=self.executor) def chain_updated(head, committed_batches=None, uncommitted_batches=None): pass self.chain_ctrl = ChainController( block_cache=self.block_tree_manager.block_cache, block_validator=self.block_validator, state_view_factory=self.state_view_factory, chain_head_lock=self._chain_head_lock, on_chain_updated=chain_updated, chain_id_manager=self.chain_id_manager, data_dir=None, config_dir=None, chain_observers=[]) init_root = self.chain_ctrl.chain_head self.assert_is_chain_head(init_root) # create a chain of length 5 extending the root _, head = self.generate_chain(init_root, 5) self.receive_and_process_blocks(head) self.assert_is_chain_head(head) self.init_head = head
def __init__(self, with_genesis=True): self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.block_store = BlockStore( DictDatabase(indexes=BlockStore.create_index_configuration())) self.block_cache = BlockCache(self.block_store) self.state_db = {} self.block_manager = BlockManager() self.block_manager.add_store("commit_store", self.block_store) # add the mock reference to the consensus consensus_setting_addr = SettingsView.setting_address( 'sawtooth.consensus.algorithm') self.state_db[consensus_setting_addr] = _setting_entry( 'sawtooth.consensus.algorithm', 'test_journal.mock_consensus') self.state_view_factory = MockStateViewFactory(self.state_db) context = create_context('secp256k1') private_key = context.new_random_private_key() crypto_factory = CryptoFactory(context) self.signer = crypto_factory.new_signer(private_key) identity_private_key = context.new_random_private_key() self.identity_signer = crypto_factory.new_signer(identity_private_key) chain_head = None if with_genesis: self.genesis_block = self.generate_genesis_block() chain_head = self.genesis_block self.block_manager.put([chain_head.block]) self.block_manager.persist(chain_head.block.header_signature, "commit_store") self.block_publisher = BlockPublisher( block_manager=self.block_manager, transaction_executor=MockTransactionExecutor(), transaction_committed=self.block_store.has_transaction, batch_committed=self.block_store.has_batch, state_view_factory=self.state_view_factory, settings_cache=SettingsCache( SettingsViewFactory(self.state_view_factory), ), block_sender=self.block_sender, batch_sender=self.block_sender, chain_head=chain_head.block, identity_signer=self.identity_signer, data_dir=None, config_dir=None, permission_verifier=MockPermissionVerifier(), batch_observers=[])
def test_publish_block(self): """ Test that the Journal will produce blocks and consume those blocks to extend the chain. :return: """ # construction and wire the journal to the # gossip layer. btm = BlockTreeManager() try: journal = Journal( block_store=btm.block_store, block_cache=btm.block_cache, state_view_factory=MockStateViewFactory(btm.state_db), block_sender=self.block_sender, batch_sender=self.batch_sender, transaction_executor=self.txn_executor, squash_handler=None, identity_signing_key=btm.identity_signing_key, chain_id_manager=None, state_delta_processor=self.state_delta_processor, data_dir=None, config_dir=None, permission_verifier=self.permission_verifier ) self.gossip.on_batch_received = journal.on_batch_received self.gossip.on_block_received = journal.on_block_received journal.start() # feed it a batch batch = Batch() journal.on_batch_received(batch) wait_until(lambda: self.block_sender.new_block is not None, 2) self.assertTrue(self.block_sender.new_block is not None) block = BlockWrapper(self.block_sender.new_block) journal.on_block_received(block) # wait for the chain_head to be updated. wait_until(lambda: btm.chain_head.identifier == block.identifier, 2) self.assertTrue(btm.chain_head.identifier == block.identifier) finally: if journal is not None: journal.stop()
def do_test_injector_blockinfoinject_create_batch(): block_info = BlockInfo(block_num=100, signer_public_key=BLOCKINFO_SIGNER_PUBLIC_KEY, header_signature=BLOCKINFO_HEADER_SIGNATURE, timestamp=2364657, previous_block_id=BLOCKINFO_PREVIOUS_BLOCKID) btm = BlockTreeManager() batch_injector_factory=DefaultBatchInjectorFactory(\ block_store=btm.block_store,\ state_view_factory=MockStateViewFactory(btm.state_db),\ signer=btm.identity_signer) blockinjector = BlockInfoInjector(batch_injector_factory._block_store, \ batch_injector_factory._state_view_factory, batch_injector_factory._signer) blockinjector.create_batch(block_info=block_info)
def test_validation_rules_reject_batches(self, mock_batch_injector_factory): """Test that a batch is not added to the block if it will violate the block validation rules. It does the following: - Sets the block_validation_rules to limit the number of 'test' transactions to 1 - creates two batches, limited to 1 transaction each, and receives them - verifies that only the first batch was committed to the block """ addr, value = CreateSetting( 'sawtooth.validator.block_validation_rules', 'NofX:1,test') self.state_view_factory = MockStateViewFactory({addr: value}) mock_batch_injector_factory.create_injectors.return_value = [] batch1 = self.make_batch(txn_count=1) batch2 = self.make_batch(txn_count=1) self.publisher = BlockPublisher( block_manager=self.block_tree_manager.block_manager, transaction_executor=MockTransactionExecutor(), transaction_committed=( self.block_tree_manager.block_store.has_transaction), batch_committed=self.block_tree_manager.block_store.has_batch, state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.batch_sender, chain_head=self.block_tree_manager.chain_head.block, identity_signer=self.block_tree_manager.identity_signer, data_dir=None, config_dir=None, batch_observers=[], permission_verifier=self.permission_verifier, batch_injector_factory=mock_batch_injector_factory) self.receive_batches(batches=[batch1, batch2]) self.publish_block() self.assert_block_batch_count(1) self.assert_batch_in_block(batch1)
def __init__(self, with_genesis=True): self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.block_store = BlockStore(DictDatabase( indexes=BlockStore.create_index_configuration())) self.block_cache = BlockCache(self.block_store) self.state_db = {} # add the mock reference to the consensus consensus_setting_addr = SettingsView.setting_address( 'sawtooth.consensus.algorithm') self.state_db[consensus_setting_addr] = _setting_entry( 'sawtooth.consensus.algorithm', 'test_journal.mock_consensus') self.state_view_factory = MockStateViewFactory(self.state_db) context = create_context('secp256k1') private_key = context.new_random_private_key() crypto_factory = CryptoFactory(context) self.signer = crypto_factory.new_signer(private_key) identity_private_key = context.new_random_private_key() self.identity_signer = crypto_factory.new_signer(identity_private_key) chain_head = None if with_genesis: self.genesis_block = self.generate_genesis_block() self.set_chain_head(self.genesis_block) chain_head = self.genesis_block self.block_publisher = BlockPublisher( transaction_executor=MockTransactionExecutor(), block_cache=self.block_cache, state_view_factory=self.state_view_factory, settings_cache=SettingsCache( SettingsViewFactory(self.state_view_factory), ), block_sender=self.block_sender, batch_sender=self.block_sender, squash_handler=None, chain_head=chain_head, identity_signer=self.identity_signer, data_dir=None, config_dir=None, permission_verifier=MockPermissionVerifier(), check_publish_block_frequency=0.1, batch_observers=[])
def __init__(self): self.block_tree_manager = BlockTreeManager(with_genesis=False) self.gossip = MockNetwork() self.txn_executor = MockTransactionExecutor() self.block_sender = MockBlockSender() self.chain_id_manager = MockChainIdManager() self.chain_head_lock = RLock() self.permission_verifier = MockPermissionVerifier() self.state_view_factory = MockStateViewFactory( self.block_tree_manager.state_db) self.transaction_executor = MockTransactionExecutor( batch_execution_result=None) self.executor = SynchronousExecutor() self.block_validator = BlockValidator( state_view_factory=self.state_view_factory, block_cache=self.block_tree_manager.block_cache, transaction_executor=self.transaction_executor, squash_handler=None, identity_signer=self.block_tree_manager.identity_signer, data_dir=None, config_dir=None, permission_verifier=self.permission_verifier, thread_pool=self.executor) def chain_updated(head, committed_batches=None, uncommitted_batches=None): pass self.chain_ctrl = ChainController( block_cache=self.block_tree_manager.block_cache, block_validator=self.block_validator, state_view_factory=self.state_view_factory, chain_head_lock=self.chain_head_lock, on_chain_updated=chain_updated, chain_id_manager=self.chain_id_manager, data_dir=None, config_dir=None, chain_observers=[]) self.assertIsNone(self.chain_ctrl.chain_head)
def test_max_block_size(self, mock_batch_injector_factory): ''' Test block publisher obeys the block size limits ''' mock_batch_injector_factory.create_injectors.return_value = [] # Create a publisher that has a state view # with a batch limit addr, value = CreateSetting('sawtooth.publisher.max_batches_per_block', 1) self.state_view_factory = MockStateViewFactory({addr: value}) self.publisher = BlockPublisher( block_manager=self.block_tree_manager.block_manager, transaction_executor=MockTransactionExecutor(), transaction_committed=( self.block_tree_manager.block_store.has_transaction), batch_committed=self.block_tree_manager.block_store.has_batch, state_view_factory=self.state_view_factory, block_sender=self.block_sender, batch_sender=self.batch_sender, chain_head=self.block_tree_manager.chain_head.block, identity_signer=self.block_tree_manager.identity_signer, data_dir=None, config_dir=None, batch_observers=[], permission_verifier=self.permission_verifier, batch_injector_factory=mock_batch_injector_factory) self.assert_no_block_published() # receive batches, then try again (succeeding) self.receive_batches() # try to publish with no pending queue (failing) for i in range(self.batch_count): self.publish_block() self.assert_block_published() self.update_chain_head(BlockWrapper(self.result_block)) self.verify_block([self.batches[i]])
def setUp(self): self.blocks = BlockTreeManager() self.gossip = MockNetwork() self.executor = SynchronousExecutor() self.txn_executor = MockTransactionExecutor() self.block_sender = MockBlockSender() self.state_view_factory = MockStateViewFactory() def chain_updated(head, committed_batches=None, uncommitted_batches=None): pass self.chain_ctrl = ChainController( consensus_module=mock_consensus, block_cache=self.blocks.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, executor=self.executor, transaction_executor=MockTransactionExecutor(), on_chain_updated=chain_updated, squash_handler=None)
class BlockTreeManager(object): def block_def(self, add_to_store=False, add_to_cache=False, batch_count=0, status=BlockStatus.Unknown, invalid_consensus=False, invalid_batch=False, invalid_signature=False, weight=None): return { "add_to_cache": add_to_cache, "add_to_store": add_to_store, "batch_count": batch_count, "status": status, "invalid_consensus": invalid_consensus, "invalid_batch": invalid_batch, "invalid_signature": invalid_signature, "weight": weight } def __init__(self): self.block_sender = MockBlockSender() self.block_store = BlockStoreAdapter({}) self.block_cache = BlockCache(self.block_store) self.state_db = {} self.state_view_factory = MockStateViewFactory(self.state_db) self.signing_key = signing.generate_privkey() self.public_key = signing.encode_pubkey( signing.generate_pubkey(self.signing_key), "hex") self.genesis_block = self._generate_genesis_block() self.block_store[self.genesis_block.identifier] = self.genesis_block self.set_chain_head(self.genesis_block) self.block_publisher = BlockPublisher( consensus_module=mock_consensus, transaction_executor=MockTransactionExecutor(), block_cache=self.block_cache, state_view_factory=self.state_view_factory, block_sender=self.block_sender, squash_handler=None, chain_head=self.genesis_block) def _get_block_id(self, block): if (block is None): return None elif isinstance(block, Block) or\ isinstance(block, BlockWrapper): return block.header_signature elif isinstance(block, basestring): return block else: return str(block) def _get_block(self, block): if (block is None): return None elif isinstance(block, Block): return BlockWrapper(block) elif isinstance(block, BlockWrapper): return block elif isinstance(block, str): return self.block_cache[block] else: # WTF try something crazy return self.block_cache[str(block)] def set_chain_head(self, block): self.block_store.set_chain_head(self._get_block_id(block)) @property def chain_head(self): return self.block_store.chain_head def set_chain_head(self, block): self.block_store.set_chain_head(block.identifier) def generate_block(self, previous_block=None, add_to_store=False, add_to_cache=False, batch_count=0, status=BlockStatus.Unknown, invalid_consensus=False, invalid_batch=False, invalid_signature=False, weight=None): previous = self._get_block(previous_block) if previous is None: previous = self.chain_head self.block_publisher.on_chain_updated(previous) while self.block_sender.new_block is None: self.block_publisher.on_batch_received(Batch()) self.block_publisher.on_check_publish_block(True) block = self.block_sender.new_block self.block_sender.new_block = None block = BlockWrapper(block) if invalid_signature: block.block.header_signature = "BAD" block.weight = weight block.status = status if add_to_cache: self.block_cache[block.identifier] = block if add_to_store: if block.weight is None: state_view = self.state_view_factory.create_view(None) tmv = mock_consensus.\ BlockVerifier(block_cache=self.block_cache, state_view=state_view) block.weight = tmv.compute_block_weight(block) self.block_store[block.identifier] = block return block def _generate_batch(self, payload): payload_encoded = payload.encode('utf-8') hasher = hashlib.sha512() hasher.update(payload_encoded) header = TransactionHeader() header.batcher_pubkey = self.public_key # txn.dependencies not yet header.family_name = 'test' header.family_version = '1' header.nonce = _generate_id(16) header.payload_encoding = "text" header.payload_sha512 = hasher.hexdigest().encode() header.signer_pubkey = self.public_key txn = Transaction() header_bytes = header.SerializeToString() txn.header = header_bytes txn.header_signature = signing.sign(header_bytes, self.signing_key) txn.payload = payload_encoded batch_header = BatchHeader() batch_header.signer_pubkey = self.public_key batch_header.transaction_ids.extend([txn.header_signature]) batch = Batch() header_bytes = batch_header.SerializeToString() batch.header = header_bytes batch.header_signature = signing.sign(header_bytes, self.signing_key) batch.transactions.extend([txn]) return batch def _generate_genesis_block(self): genesis_header = BlockHeader(previous_block_id=NULL_BLOCK_IDENTIFIER, signer_pubkey=self.public_key, block_num=0) block = BlockBuilder(genesis_header) block.add_batches([self._generate_batch("Genesis")]) header_bytes = block.block_header.SerializeToString() signature = signing.sign(header_bytes, self.signing_key) block.set_signature(signature) return BlockWrapper(block.build_block()) def generate_chain(self, root_block, blocks, params=None): """ Generate a new chain based on the root block and place it in the block cache. """ if params is None: params = {} out = [] if not isinstance(blocks, list): blocks = self.generate_chain_definition(int(blocks), params) if root_block is None: previous = self._generate_genesis_block() self.block_store[previous.identifier] = previous else: previous = self._get_block(root_block) for block in blocks: new_block = self.generate_block(previous_block=previous, **block) out.append(new_block) previous = new_block return out def generate_chain_definition(self, count, params=None): if params is None: params = {} out = [] for _ in range(0, count): out.append(self.block_def(**params)) return out def __str__(self): return str(self.block_cache) def __repr__(self): return repr(self.block_cache)
def do_publish_block(): btm = BlockTreeManager() block_publisher = None chain_controller = None try: block_publisher = BlockPublisher( transaction_executor=testJournal.txn_executor, block_cache=btm.block_cache, state_view_factory=MockStateViewFactory(btm.state_db), settings_cache=SettingsCache( SettingsViewFactory(btm.state_view_factory), ), block_sender=testJournal.block_sender, batch_sender=testJournal.batch_sender, squash_handler=None, chain_head=btm.block_store.chain_head, identity_signer=btm.identity_signer, data_dir=None, config_dir=None, permission_verifier=testJournal.permission_verifier, check_publish_block_frequency=0.1, batch_observers=[], batch_injector_factory=DefaultBatchInjectorFactory( block_store=btm.block_store, state_view_factory=MockStateViewFactory(btm.state_db), signer=btm.identity_signer)) block_validator = BlockValidator( state_view_factory=MockStateViewFactory(btm.state_db), block_cache=btm.block_cache, transaction_executor=testJournal.txn_executor, squash_handler=None, identity_signer=btm.identity_signer, data_dir=None, config_dir=None, permission_verifier=testJournal.permission_verifier) chain_controller = ChainController( block_cache=btm.block_cache, block_validator=block_validator, state_view_factory=MockStateViewFactory(btm.state_db), chain_head_lock=block_publisher.chain_head_lock, on_chain_updated=block_publisher.on_chain_updated, chain_id_manager=None, data_dir=None, config_dir=None, chain_observers=[]) testJournal.gossip.on_batch_received = block_publisher.queue_batch testJournal.gossip.on_block_received = chain_controller.queue_block block_publisher.start() chain_controller.start() # feed it a batch batch = Batch() block_publisher.queue_batch(batch) wait_until(lambda: testJournal.block_sender.new_block is not None, 2) assert testJournal.block_sender.new_block != None block = BlockWrapper(testJournal.block_sender.new_block) chain_controller.queue_block(block) # wait for the chain_head to be updated. wait_until(lambda: btm.chain_head.identifier == block.identifier, 2) # assert btm.chain_head.identifier == block.identifier finally: if block_publisher is not None: block_publisher.stop() if chain_controller is not None: chain_controller.stop() if block_validator is not None: block_validator.stop()
def setUp(self): self.blocks = BlockTreeManager() self.block_sender = MockBlockSender() self.batch_sender = MockBatchSender() self.state_view_factory = MockStateViewFactory({})
def setUp(self): self.btm = BlockTreeManager() self.state_view_factory = MockStateViewFactory()