def step_node_receive_message(context, node_name, message, neighbor_node_name): receive_task = context.async_loop.create_task( context.node[node_name].msg_receive()) msg = context.async_loop.run_until_complete(receive_task) assert receive_task.done() is True assert msg.command.name == message assert msg.from_addr == list(context.node[neighbor_node_name].addr) context.node[node_name].msg_received = msg if message == AnnounceBlockCommand.name: context.block_new[node_name] = Block.unserialize(msg.command.params['blk'])
def command_info_handler(self, blocks_insert: List[JsonDict]) -> None: logger.debug(f'command_info_handler {blocks_insert}') #merge blockchains parts blks_m = [Block.unserialize(blk_str) for blk_str in blocks_insert] head_idx = -1 for i in range(len(blks_m)): try: Rules.block_valid(self.__ledger[-1], blks_m[i]) #have head for merging head_idx = i break except (BlockRulesError, TransactionRulesError): pass if head_idx < 0 or head_idx > 10: logger.error('possible fork') return for i in range(head_idx, len(blks_m)): self.block_add_to_blockchain(blks_m[i])
def handler(ctx: Any, **kwargs: Any) -> None: logger.debug( f'running "{AnnounceBlockCommand.name}" command with {type(ctx)} {kwargs}' ) ctx.block_add_to_blockchain(Block.unserialize(kwargs['blk']))
def test_blk_from_json_obj(self) -> None: blk_new = Block.unserialize_json(str(self.blk)) self.assertIsInstance(blk_new, Serializable) self.assertIsInstance(blk_new, Block) self.assertIs(type(blk_new), Block) self.assertTrue(blk_new.id == self.blk.id)
def setUp(self) -> None: self.trx = Transaction(123, [Output(100, TEST_PUBADDR).id], [Output(10, TEST_PUBADDR), Output(90, TEST_PUBADDR)]) self.trx.sign(Privkey.from_pem(io.StringIO(PEM_FILE_DATA))) # self.blk = Block(345, GENESIS_BLOCK, [self.trx.id]) self.blk = Block(345, GENESIS_BLOCK, [self.trx])
def do_POST(self) -> None: content_length = int(self.headers['Content-Length']) post_data = self.rfile.read(content_length) self.__io_loop.run_until_complete( self.__mbq.put(Block.unserialize_json(post_data))) self._set_response(200)
def block_assemble_new(self, trxs_new: List[Transaction]) -> Block: logger.debug('assembling new block') # coinbase trx coinbase = Utils.coinbase_transaction() blk_new = Block(time(), self.__ledger[-1], [coinbase, *trxs_new]) return blk_new