def test_submit_block(self, child_chain, root_chain): DUMMY_MERKLE = 'merkle hash' MOCK_FUNCTION = mock() DUMMY_NONCE = 100 DUMMY_TX = {'nonce': DUMMY_NONCE, 'gas': 100, 'gasPrice': 100} block_number = child_chain.current_block_number block = child_chain.current_block when(child_chain.current_block).merklize_transaction_set().thenReturn( DUMMY_MERKLE) (when(root_chain.functions).submitBlock(DUMMY_MERKLE, block_number, ANY, ANY, ANY).thenReturn(MOCK_FUNCTION)) when(MOCK_FUNCTION).buildTransaction(ANY).thenReturn(DUMMY_TX) (when('plasma_cash.child_chain.child_chain').get_sender( ANY, ANY).thenReturn(child_chain.authority)) (when( 'plasma_cash.child_chain.child_chain.w3.eth').getTransactionCount( ANY, ANY).thenReturn(DUMMY_NONCE)) (when('plasma_cash.child_chain.child_chain.w3.eth').sendRawTransaction( ANY).thenReturn(None)) DUMMY_SIG = '01' * 65 child_chain.submit_block(DUMMY_SIG) assert child_chain.current_block_number == block_number + 1 assert child_chain.db.get_block(block_number) == block assert child_chain.current_block == Block()
def test_apply_transaction_with_same_uid_tx_already_in_block_should_fail( self, child_chain): # create a another (invalid) transaction with same uid in block 2 # this transaction would be used as the prev_tx of second same uid tx DUMMY_TX_OWNER = b'\x8cT\xa4\xa0\x17\x9f$\x80\x1fI\xf92-\xab<\x87\xeb\x19L\x9b' tx = Transaction(prev_block=0, uid=1, amount=10, new_owner=DUMMY_TX_OWNER) child_chain.db.save_block(Block([tx]), 2) child_chain.current_block_number = 3 child_chain.db.increment_current_block_num() # first apply a tx with the uid DUMMY_TX_KEY = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' tx = Transaction(prev_block=1, uid=1, amount=10, new_owner=self.DUMMY_TX_NEW_OWNER) tx.sign(eth_utils.normalize_key(DUMMY_TX_KEY)) child_chain.apply_transaction(rlp.encode(tx).hex()) # apply another tx with the same uid should fail tx = Transaction(prev_block=2, uid=1, amount=10, new_owner=self.DUMMY_TX_NEW_OWNER) tx.sign(eth_utils.normalize_key(DUMMY_TX_KEY)) with pytest.raises(TxWithSameUidAlreadyExists): child_chain.apply_transaction(rlp.encode(tx).hex())
def test_submit_block(self, child_chain): DUMMY_MERKLE = 'merkle hash' MOCK_TRANSACT = mock() block_number = child_chain.current_block_number block = child_chain.current_block when(child_chain.current_block).merklize_transaction_set().thenReturn(DUMMY_MERKLE) when(self.ROOT_CHAIN).transact(any).thenReturn(MOCK_TRANSACT) child_chain.submit_block(self.DUMMY_SIG) verify(MOCK_TRANSACT).submitBlock(DUMMY_MERKLE, block_number) assert child_chain.current_block_number == block_number + 1 assert child_chain.blocks[block_number] == block assert child_chain.current_block == Block()
def child_chain(self): DUMMY_TX_OWNER = b'\x8cT\xa4\xa0\x17\x9f$\x80\x1fI\xf92-\xab<\x87\xeb\x19L\x9b' deposit_filter = mock() when(self.ROOT_CHAIN).on('Deposit').thenReturn(deposit_filter) child_chain = ChildChain(authority=self.DUMMY_AUTHORITY, root_chain=self.ROOT_CHAIN) # create a dummy transaction tx = Transaction(prev_block=0, uid=1, amount=10, new_owner=DUMMY_TX_OWNER) # create a block with the dummy transaction child_chain.blocks[1] = Block([tx]) child_chain.current_block_number = 2 return child_chain
def test_submit_block(self, child_chain, root_chain): DUMMY_MERKLE = 'merkle hash' MOCK_TRANSACT = mock() block_number = child_chain.current_block_number block = child_chain.current_block when(child_chain.current_block).merklize_transaction_set().thenReturn( DUMMY_MERKLE) (when(root_chain.functions).submitBlock( DUMMY_MERKLE, block_number).thenReturn(MOCK_TRANSACT)) child_chain.submit_block(self.DUMMY_SIG) verify(MOCK_TRANSACT).transact(ANY) assert child_chain.current_block_number == block_number + 1 assert child_chain.db.get_block(block_number) == block assert child_chain.current_block == Block()
def child_chain(self, root_chain, db): DUMMY_TX_OWNER = b'\x8cT\xa4\xa0\x17\x9f$\x80\x1fI\xf92-\xab<\x87\xeb\x19L\x9b' expect(root_chain).eventFilter('Deposit', {'fromBlock': 0}) expect(Thread).start() child_chain = ChildChain(self.DUMMY_AUTHORITY, root_chain, db) # create a dummy transaction tx = Transaction(prev_block=0, uid=1, amount=10, new_owner=DUMMY_TX_OWNER) # create a block with the dummy transaction db.save_block(Block([tx]), 1) child_chain.current_block_number = 2 db.increment_current_block_num() return child_chain
def test_save_block_already_exists(self, db): DUMMY_BLOCK = Block() DUMMY_BLK_NUM = 1 db.save_block(DUMMY_BLOCK, DUMMY_BLK_NUM) with pytest.raises(BlockAlreadyExistsException): db.save_block('second block should fail', DUMMY_BLK_NUM)
def test_block_normal_case(self, db): DUMMY_BLOCK = Block() DUMMY_BLK_NUM = 1 db.save_block(DUMMY_BLOCK, DUMMY_BLK_NUM) assert db.get_block(DUMMY_BLK_NUM) == DUMMY_BLOCK
def _generate_dummy_block(self): owner = b'\x8cT\xa4\xa0\x17\x9f$\x80\x1fI\xf92-\xab<\x87\xeb\x19L\x9b' tx = Transaction(prev_block=0, uid=1, amount=10, new_owner=owner) block = Block(transaction_set=[tx]) return block
def test_constructor_with_default_param(self): block = Block() assert block.transaction_set == [] assert block.merkle is None
def test_constructor(self): DUMMY_TX_SET = ['tx set'] block = Block(transaction_set=DUMMY_TX_SET) assert block.transaction_set == DUMMY_TX_SET assert block.merkle is None
def block(self): tx = Transaction(prev_block=0, uid=self.UID, amount=10, new_owner=self.DUMMY_TX_OWNER) return Block(transaction_set=[tx])