def test_apply_transaction_with_previous_tx_not_exist_should_fail( self, child_chain): DUMMY_TX_KEY = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' # token with uid 3 doesn't exist tx = Transaction(prev_block=1, uid=3, amount=10, new_owner=self.DUMMY_TX_NEW_OWNER) tx.sign(eth_utils.normalize_key(DUMMY_TX_KEY)) with pytest.raises(PreviousTxNotFoundException): child_chain.apply_transaction(rlp.encode(tx).hex())
def test_apply_transaction_with_double_spending(self, child_chain): 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()) # try to spend a spent transaction with pytest.raises(TxAlreadySpentException): child_chain.apply_transaction(rlp.encode(tx).hex())
def transferToken(prev_block, uid, amount, new_owner, owner_key): # client = Client(container.get_root_chain(), container.get_child_chain_client(), owner_key) # client.send_transaction(prev_block, uid, amount, new_owner) new_owner = utils.normalize_address(new_owner) tx = Transaction(prev_block, uid, amount, new_owner) owner_key = utils.normalize_key(owner_key) tx.sign(owner_key) # self.child_chain.send_transaction(rlp.encode(tx, Transaction).hex()) encoded_txn = rlp.encode(tx, Transaction).hex() payload = {'tx': encoded_txn} response = requests.post("http://localhost:8546/send_tx", data=payload) print(response.status_code) if response.status_code == 200: print(response.content)
def make_transfer(self, prev_block, uid, amount, new_owner): try: new_owner = utils.normalize_address(new_owner) tx = Transaction(prev_block, uid, amount, new_owner) tx.sign(self.key) encoded_txn = rlp.encode(tx, Transaction).hex() payload = {'tx': encoded_txn} response = requests.post(f"{self.child_chain}/send_tx", data=payload) if response.status_code == 200: return response.status_code, response.content.hex() else: return response.status_code, None except: print("exception occur in make_transfer function") return None, None
def test_apply_transaction(self, child_chain): 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()) prev_tx = child_chain.db.get_block(1).transaction_set[0] assert child_chain.current_block.transaction_set[0] == tx assert prev_tx.new_owner == tx.sender assert prev_tx.amount == tx.amount assert prev_tx.spent is True
def test_constructor_default_empty_sig(self): 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) assert tx.sig == b'\x00' * 65
def test_add_tx(self, block): dummy_tx = Transaction(prev_block=0, uid=2, amount=10, new_owner=self.DUMMY_TX_OWNER) block.add_tx(dummy_tx) assert len(block.transaction_set) == 2 assert dummy_tx in block.transaction_set
def userC_starts_to_exit_some_eth_from_plasma_cash(context, amount): client = container.get_client() deposit_block = client.get_block(DEPOSIT_TX_BLOCK) deposit_tx = deposit_block.get_tx_by_uid(uid) deposit_block.merklize_transaction_set() deposit_tx_proof = deposit_block.merkle.create_merkle_proof(uid) # invalid tx doesn't exist in child chain invalid_tx = Transaction(DEPOSIT_TX_BLOCK, uid, 1, utils.normalize_address(userC)) invalid_tx.sign(utils.normalize_key(userA_key)) invalid_tx_merkle = SparseMerkleTree(257, {uid: invalid_tx.merkle_hash}) invalid_tx_proof = invalid_tx_merkle.create_merkle_proof(uid) root_chain = container.get_root_chain() root_chain.functions.startExit( rlp.encode(deposit_tx), deposit_tx_proof, DEPOSIT_TX_BLOCK, rlp.encode(invalid_tx), invalid_tx_proof, TRANSFER_TX_2_BLOCK).transact({'from': userC}) time.sleep(5)
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 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_constructor(self): PREV_BLOCK = 1 UID = 'uid' AMOUNT = 1 NEW_OWNER = 'new owner' SIG = 'sig' tx = Transaction(prev_block=PREV_BLOCK, uid=UID, amount=AMOUNT, new_owner=NEW_OWNER, sig=SIG) assert tx.prev_block == PREV_BLOCK assert tx.uid == UID assert tx.amount == AMOUNT assert tx.new_owner == NEW_OWNER assert tx.sig == SIG assert tx.spent is False
def send_transaction(self, prev_block, uid, amount, new_owner, key): new_owner = utils.normalize_address(new_owner) key = utils.normalize_key(key) tx = Transaction(prev_block, uid, amount, new_owner) tx.sign(key) self.child_chain.send_transaction(rlp.encode(tx, Transaction).hex())
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 block(self): tx = Transaction(prev_block=0, uid=self.UID, amount=10, new_owner=self.DUMMY_TX_OWNER) return Block(transaction_set=[tx])
def tx(self): DUMMY_TX_OWNER = b'\x8cT\xa4\xa0\x17\x9f$\x80\x1fI\xf92-\xab<\x87\xeb\x19L\x9b' return Transaction(prev_block=0, uid=1, amount=10, new_owner=DUMMY_TX_OWNER)