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_apply_transaction_with_invalid_sig(self, child_chain): DUMMY_INVALID_TX_KEY = b'7a76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' tx = Transaction(prev_block=1, uid=1, amount=10, new_owner=self.DUMMY_TX_NEW_OWNER) tx.sign(eth_utils.normalize_key(DUMMY_INVALID_TX_KEY)) with pytest.raises(InvalidTxSignatureException): child_chain.apply_transaction(rlp.encode(tx).hex())
def send_transaction(self, prev_block, uid, amount, new_owner): new_owner = utils.normalize_address(new_owner) tx = Transaction(prev_block, uid, amount, new_owner) tx.sign(self.key) encoded_tx = '0' + rlp.encode(tx, Transaction).hex() ret = requests.get('http://{}:{}/broadcast_tx_commit?tx="{}"'.format( config['tdm_IP'], config['tdm_RPC'], encoded_tx)) print(ret.text)
def userA_tries_to_double_spend_some_eth_to_userC(context, amount): 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}) root_chain = container.get_root_chain() root_chain.functions.submitBlock(invalid_tx_merkle.root, TRANSFER_TX_2_BLOCK).transact({ 'from': operator })
def test_apply_transaction_with_mismatch_amount(self, child_chain): DUMMY_TX_KEY = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' # token with uid 1 doesn't have 20 tx = Transaction(prev_block=1, uid=1, amount=20, new_owner=self.DUMMY_TX_NEW_OWNER) tx.sign(eth_utils.normalize_key(DUMMY_TX_KEY)) with pytest.raises(TxAmountMismatchException): child_chain.apply_transaction(rlp.encode(tx).hex())
def test_apply_transaction_with_previous_tx_not_exist(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 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
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.blocks[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 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 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 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())