def test_challenge_exit(t, u, root_chain): owner, value_1, key = t.a1, 100, t.k1 null_address = b'\x00' * 20 tx1 = Transaction(0, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx_bytes1 = rlp.encode(tx1, UnsignedTransaction) dep1_blknum = root_chain.getDepositBlock() root_chain.deposit(tx_bytes1, value=value_1) merkle = FixedMerkle(16, [tx1.merkle_hash], True) proof = merkle.create_membership_proof(tx1.merkle_hash) confirmSig1 = confirm_tx(tx1, root_chain.getChildChain(dep1_blknum)[0], key) sigs = tx1.sig1 + tx1.sig2 + confirmSig1 exitId1 = dep1_blknum * 1000000000 + 10000 * 0 + 0 root_chain.startExit(exitId1, tx_bytes1, proof, sigs, sender=key) tx2 = Transaction(dep1_blknum, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx2.sign1(key) tx_bytes2 = rlp.encode(tx2, UnsignedTransaction) merkle = FixedMerkle(16, [tx2.merkle_hash], True) proof = merkle.create_membership_proof(tx2.merkle_hash) child_blknum = root_chain.currentChildBlock() root_chain.submitBlock(merkle.root) confirmSig = confirm_tx(tx2, root_chain.getChildChain(child_blknum)[0], key) sigs = tx2.sig1 + tx2.sig2 exitId2 = child_blknum * 1000000000 + 10000 * 0 + 0 assert root_chain.exits(exitId1) == ['0x' + owner.hex(), 100, exitId1] assert root_chain.exitIds(exitId1) == exitId1 root_chain.challengeExit(exitId2, exitId1, tx_bytes2, proof, sigs, confirmSig) assert root_chain.exits(exitId1) == [ '0x0000000000000000000000000000000000000000', 0, 0 ] assert root_chain.exitIds(exitId1) == 0
def test_challenge_exit(t, u, root_chain): owner, value_1, key = t.a1, 100, t.k1 null_address = b'\x00' * 20 tx1 = Transaction(0, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx_bytes1 = rlp.encode(tx1, UnsignedTransaction) root_chain.deposit(tx_bytes1, value=value_1) merkle = FixedMerkle(16, [tx1.merkle_hash], True) proof = merkle.create_membership_proof(tx1.merkle_hash) confirmSig1 = confirm_tx(tx1, root_chain.getChildChain(1)[0], key) sigs = tx1.sig1 + tx1.sig2 + confirmSig1 root_chain.startExit([1, 0, 0], tx_bytes1, proof, sigs, sender=key) tx2 = Transaction(1, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx2.sign1(key) tx_bytes2 = rlp.encode(tx2, UnsignedTransaction) merkle = FixedMerkle(16, [tx2.merkle_hash], True) proof = merkle.create_membership_proof(tx2.merkle_hash) t.chain.head_state.block_number += 7 root_chain.submitBlock(merkle.root) confirmSig = confirm_tx(tx2, root_chain.getChildChain(2)[0], key) sigs = tx2.sig1 + tx2.sig2 exit_id = 1 * 1000000000 + 10000 * 0 + 0 assert root_chain.exits(exit_id) == ['0x' + owner.hex(), 100] assert root_chain.exitIds(exit_id) == exit_id root_chain.challengeExit(exit_id, [2, 0, 0], tx_bytes2, proof, sigs, confirmSig) assert root_chain.exits(exit_id) == ['0x0000000000000000000000000000000000000000', 0] assert root_chain.exitIds(exit_id) == 0
def child_chain(): child_chain = ChildChain(AUTHORITY, Mock()) # Create some valid transations tx1 = Transaction(0, 0, 0, 0, 0, 0, newowner1, amount1, b'\x00' * 20, 0, 0) tx2 = Transaction(0, 0, 0, 0, 0, 0, newowner1, amount1, b'\x00' * 20, 0, 0) # Create a block with those transactions child_chain.blocks[1] = Block([tx1, tx2]) return child_chain
def child_chain(root_chain): child_chain = ChildChain(None, None, None) # Create some valid transations key = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' owner, amount1 = '0x8f2ecdd6103c0423f4c3e906666238936e81e538', 200 tx1 = Transaction(0, 0, 0, 0, 0, 0, owner, amount1, b'\x00' * 20, 0, 0) tx2 = Transaction(0, 0, 0, 0, 0, 0, owner, amount1, b'\x00' * 20, 0, 0) # Create a block with those transactions child_chain.blocks[1] = Block([tx1, tx2]) return child_chain
def test_challenge_exit(t, u, root_chain, assert_tx_failed): owner, value_1, key = t.a1, 100, t.k1 null_address = b'\x00' * 20 tx1 = Transaction(0, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) deposit_tx_hash = get_deposit_hash(owner, value_1) utxo_pos1 = root_chain.getDepositBlock() * 1000000000 + 1 root_chain.deposit(value=value_1, sender=key) utxo_pos2 = root_chain.getDepositBlock() * 1000000000 root_chain.deposit(value=value_1, sender=key) merkle = FixedMerkle(16, [deposit_tx_hash], True) proof = merkle.create_membership_proof(deposit_tx_hash) confirmSig1 = confirm_tx(tx1, root_chain.getChildChain(utxo_pos1)[0], key) sigs = tx1.sig1 + tx1.sig2 + confirmSig1 root_chain.startDepositExit(utxo_pos1, tx1.amount1, sender=key) tx3 = Transaction(utxo_pos2, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx3.sign1(key) tx_bytes3 = rlp.encode(tx3, UnsignedTransaction) merkle = FixedMerkle(16, [tx3.merkle_hash], True) proof = merkle.create_membership_proof(tx3.merkle_hash) child_blknum = root_chain.currentChildBlock() root_chain.submitBlock(merkle.root) confirmSig = confirm_tx(tx3, root_chain.getChildChain(child_blknum)[0], key) sigs = tx3.sig1 + tx3.sig2 utxo_pos3 = child_blknum * 1000000000 + 10000 * 0 + 0 tx4 = Transaction(utxo_pos1, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx4.sign1(key) tx_bytes4 = rlp.encode(tx4, UnsignedTransaction) merkle = FixedMerkle(16, [tx4.merkle_hash], True) proof = merkle.create_membership_proof(tx4.merkle_hash) child_blknum = root_chain.currentChildBlock() root_chain.submitBlock(merkle.root) confirmSig = confirm_tx(tx4, root_chain.getChildChain(child_blknum)[0], key) sigs = tx4.sig1 + tx4.sig2 utxo_pos4 = child_blknum * 1000000000 + 10000 * 0 + 0 oindex1 = 0 assert root_chain.exits(utxo_pos1) == ['0x' + owner.hex(), 100] # Fails if transaction after exit doesn't reference the utxo being exited assert_tx_failed(lambda: root_chain.challengeExit( utxo_pos3, utxo_pos1, tx_bytes3, proof, sigs, confirmSig)) # Fails if transaction proof is incorrect assert_tx_failed(lambda: root_chain.challengeExit( utxo_pos4, utxo_pos1, tx_bytes4, proof[::-1], sigs, confirmSig)) # Fails if transaction confirmation is incorrect assert_tx_failed(lambda: root_chain.challengeExit( utxo_pos4, utxo_pos1, tx_bytes4, proof, sigs, confirmSig[::-1])) root_chain.challengeExit(utxo_pos4, oindex1, tx_bytes4, proof, sigs, confirmSig) assert root_chain.exits(utxo_pos1) == [ '0x0000000000000000000000000000000000000000', value_1 ]
def test_finalize_exits(t, u, root_chain): two_weeks = 60 * 60 * 24 * 14 owner, value_1, key = t.a1, 100, t.k1 null_address = b'\x00' * 20 tx1 = Transaction(0, 0, 0, 0, 0, 0, null_address, owner, value_1, null_address, 0) dep1_blknum = root_chain.getDepositBlock() root_chain.deposit(value=value_1, sender=key) utxo_pos1 = dep1_blknum * 1000000000 + 10000 * 0 + 1 root_chain.startDepositExit(utxo_pos1, null_address, tx1.amount1, sender=key) t.chain.head_state.timestamp += two_weeks * 2 enc_null_address = "0x" + "00" * 20 assert root_chain.exits(utxo_pos1) == [ '0x' + owner.hex(), enc_null_address, 100 ] pre_balance = t.chain.head_state.get_balance(owner) root_chain.finalizeExits(sender=t.k2) post_balance = t.chain.head_state.get_balance(owner) assert post_balance == pre_balance + value_1 assert root_chain.exits(utxo_pos1) == [ '0x0000000000000000000000000000000000000000', enc_null_address, value_1 ]
def test_finalize_exits(t, u, root_chain): two_weeks = 60 * 60 * 24 * 14 owner, value_1, key = t.a1, 100, t.k1 null_address = b'\x00' * 20 tx1 = Transaction(0, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx_bytes1 = rlp.encode(tx1, UnsignedTransaction) dep1_blknum = root_chain.getDepositBlock() root_chain.deposit(tx_bytes1, value=value_1) merkle = FixedMerkle(16, [tx1.merkle_hash], True) proof = merkle.create_membership_proof(tx1.merkle_hash) confirmSig1 = confirm_tx(tx1, root_chain.getChildChain(dep1_blknum)[0], key) sigs = tx1.sig1 + tx1.sig2 + confirmSig1 exitId1 = dep1_blknum * 1000000000 + 10000 * 0 + 0 root_chain.startExit(exitId1, tx_bytes1, proof, sigs, sender=key) t.chain.head_state.timestamp += two_weeks * 2 assert root_chain.exits(exitId1) == ['0x' + owner.hex(), 100, exitId1] assert root_chain.exitIds(exitId1) == exitId1 pre_balance = t.chain.head_state.get_balance(owner) root_chain.finalizeExits(sender=t.k2) post_balance = t.chain.head_state.get_balance(owner) assert post_balance == pre_balance + value_1 assert root_chain.exits(exitId1) == [ '0x0000000000000000000000000000000000000000', 0, 0 ] assert root_chain.exitIds(exitId1) == 0
def create_transaction(self, blknum1=0, txindex1=0, oindex1=0, blknum2=0, txindex2=0, oindex2=0, newowner1=b'\x00' * 20, contractaddress1=b'\x00' * 20, amount1=0, tokenid1=0, newowner2=b'\x00' * 20, contractaddress2=b'\x00' * 20, amount2=0, tokenid2=0): return Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, newowner1, contractaddress1, amount1, tokenid1, newowner2, contractaddress2, amount2, tokenid2)
def create_partially_signed_transaction(self, from_contractaddress, to_contractaddress, amount): exchange_rate = self.get_exchange_rate(from_contractaddress, to_contractaddress, amount) if exchange_rate is None: return None provide_amount = amount * exchange_rate blknum1, txindex1, oindex1, contractaddress2, amount2, tokenid2 = self.create_utxo( to_contractaddress, provide_amount) ps_tx = Transaction( blknum1, txindex1, oindex1, 0, 0, 0, utils.normalize_address(self.address), b'\x00' * 20 if from_contractaddress == "0x0" else from_contractaddress, amount, 0, b'\x00' * 20, contractaddress2, amount2, tokenid2) ps_tx.sign1(utils.normalize_key(plasma_config["AUTHORITY_KEY"])) return ps_tx.to_json()
def create_transaction(self, blknum1=0, txindex1=0, oindex1=0, blknum2=0, txindex2=0, oindex2=0, newowner1=b'\x00' * 20, amount1=0, newowner2=b'\x00' * 20, amount2=0, fee=0): return Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, newowner1, amount1, newowner2, amount2, fee)
def test_deposit(t, root_chain): owner, value_1 = t.a1, 100 null_address = b'\x00' * 20 tx = Transaction(0, 0, 0, 0, 0, 0, owner, value_1, null_address, 0, 0) tx_bytes = rlp.encode(tx, UnsignedTransaction) root_chain.deposit(tx_bytes, value=value_1) assert root_chain.getChildChain(1)[0] == get_merkle_of_leaves(16, [tx.hash + tx.sig1 + tx.sig2]).root assert root_chain.getChildChain(1)[1] == t.chain.head_state.timestamp assert root_chain.currentChildBlock() == 2
def deposit(self): if len(self.inp) != 3: raise Exception("Wrong number of inputs for deposit") amount1 = int(self.inp[1]) key = utils.normalize_key(self.inp[2]) newOwner1 = utils.privtoaddr(key) newOwner2, amount2 = utils.normalize_address(b'\x00' * 20), 0 tx = Transaction(0, 0, 0, 0, 0, 0, newOwner1, amount1, newOwner2, amount2, 0) self.client.deposit(tx, key) print("Succesfully deposited %s to %s" % (amount1, newOwner1))
def test_transaction(t): blknum1, txindex1, oindex1 = 1, 1, 0 blknum2, txindex2, oindex2 = 2, 2, 1 newowner1, amount1 = t.a1, 100 newowner2, amount2 = t.a2, 150 fee = 5 oldowner1, oldowner2 = t.a1, t.a2 key1, key2 = t.k1, t.k2 tx = Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, newowner1, amount1, newowner2, amount2, fee) assert tx.blknum1 == blknum1 assert tx.txindex1 == txindex1 assert tx.oindex1 == oindex1 assert tx.blknum2 == blknum2 assert txindex2 == txindex2 assert tx.oindex2 == oindex2 assert tx.newowner1 == newowner1 assert tx.amount1 == amount1 assert tx.newowner2 == newowner2 assert tx.amount2 == amount2 assert tx.sig1 == b'\x00' * 65 assert tx.sig2 == b'\x00' * 65 tx.sign1(key1) assert tx.sender1 == oldowner1 tx.sign2(key2) assert tx.sender2 == oldowner2
def create_utxo(self, to_contractaddress, provide_amount): utxos = self.get_utxos() for blknum, txindex, oindex, contractaddress, amount, tokenid in utxos: if contractaddress.lower() == to_contractaddress[2:].lower(): if amount == provide_amount: return blknum, txindex, oindex, contractaddress, amount, tokenid for blknum, txindex, oindex, contractaddress, amount, tokenid in utxos: if contractaddress.lower() == to_contractaddress[2:].lower(): if amount > provide_amount: tx = Transaction(blknum, txindex, oindex, 0, 0, 0, utils.normalize_address(self.address), utils.normalize_address(contractaddress), provide_amount, 0, utils.normalize_address(self.address), utils.normalize_address(contractaddress), amount - provide_amount, 0) tx.sign1( utils.normalize_key(plasma_config["AUTHORITY_KEY"])) tx.sign2( utils.normalize_key(plasma_config["AUTHORITY_KEY"])) self.child_chain.apply_transaction( rlp.encode(tx, Transaction).hex()) break else: raise ValueError("no available utxo") utxos = self.get_utxos() for blknum, txindex, oindex, contractaddress, amount, tokenid in utxos: if contractaddress.lower() == to_contractaddress[2:].lower(): if amount == provide_amount: return blknum, txindex, oindex, contractaddress, amount, tokenid raise Exception("Something went wrong")
def makeorder(client, address, currency, amount, tokenprice, key): utxos = client_call(client.get_utxos, [address, currency]) amount = int(amount * 1e18) # Find a utxos with enough tokens for utxo in utxos: if utxo[3] >= amount: # generate the transaction object change_amount = utxo[3] - amount if change_amount: tx = Transaction(Transaction.TxnType.make_order, utxo[0], utxo[1], utxo[2], 0, 0, 0, Transaction.UTXOType.make_order, utils.normalize_address(address), amount, tokenprice, utils.normalize_address(currency), Transaction.UTXOType.transfer, utils.normalize_address(address), change_amount, 0, utils.normalize_address(currency), 0, NULL_ADDRESS, 0, 0, NULL_ADDRESS, 0, NULL_ADDRESS, 0, 0, NULL_ADDRESS) else: tx = Transaction(Transaction.TxnType.make_order, utxo[0], utxo[1], utxo[2], 0, 0, 0, Transaction.UTXOType.make_order, utils.normalize_address(address), amount, tokenprice, utils.normalize_address(currency), 0, NULL_ADDRESS, 0, 0, NULL_ADDRESS, 0, NULL_ADDRESS, 0, 0, NULL_ADDRESS, 0, NULL_ADDRESS, 0, 0, NULL_ADDRESS) tx.sign1(utils.normalize_key(key)) client_call(client.apply_transaction, [tx], "Sent transaction") break
def sendtx(client, blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, newowner1, contractaddress1, amount1, tokenid1, newowner2, contractaddress2, amount2, tokenid2, key1, key2): if newowner1 == "0x0": newowner1 = NULL_ADDRESS if newowner2 == "0x0": newowner2 = NULL_ADDRESS if contractaddress1 == "0x0": contractaddress1 = NULL_ADDRESS if contractaddress2 == "0x0": contractaddress2 = NULL_ADDRESS if key2 is None: key2 = key1 # Form a transaction tx = Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, utils.normalize_address(newowner1), utils.normalize_address(contractaddress1), amount1, tokenid1, utils.normalize_address(newowner2), utils.normalize_address(contractaddress2), amount2, tokenid2) # Sign it tx.sign1(utils.normalize_key(key1)) tx.sign2(utils.normalize_key(key2)) client.apply_transaction(tx) print("Sent transaction")
def test_transfer(test_lang): owner_1 = test_lang.get_account() owner_2 = test_lang.get_account() deposit_id = test_lang.deposit(owner_1, 100) transfer_id = test_lang.transfer(deposit_id, 0, owner_2, 100, owner_1) tx = Transaction(1, 0, 0, 0, 0, 0, NULL_ADDRESS, owner_2['address'], 100, NULL_ADDRESS, 0, 0) assert test_lang.transactions[transfer_id]['tx'].hash == tx.hash assert test_lang.child_chain.current_block.transaction_set[ 0].hash == tx.hash
def test_send_tx_with_sig(child_chain): tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner1, amount2, b'\x00' * 20, 0, 0) # Sign the transaction tx3.sign1(tx_key) tx3.sign2(tx_key) child_chain.apply_transaction(rlp.encode(tx3).hex())
def test_submit_block_invalid_tx_set(child_chain): block = Block() block.transaction_set = child_chain.current_block.transaction_set[:] unsubmitted_tx = Transaction(0, 0, 0, 0, 0, 0, newowner1, 1234, b'\x00' * 20, 0, 0) # Add an arbitrary transaction that hasn't been correctly submitted block.transaction_set.append(unsubmitted_tx) block.make_mutable() block.sign(block_key) block = rlp.encode(block).hex() with pytest.raises(InvalidBlockMerkleException): child_chain.submit_block(block)
def test_send_tx_invalid_sig(child_chain): tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner1, amount2, b'\x00' * 20, 0, 0) # Sign with an invalid key tx3.sign1(invalid_tx_key) tx3.sign2(invalid_tx_key) with pytest.raises(InvalidTxSignatureException): child_chain.apply_transaction(rlp.encode(tx3).hex())
def test_send_tx_with_sig(child_chain): # Valid key key = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' newowner, amount2 = '0x4b3ec6c9dc67079e82152d6d55d8dd96a8e6aa26', 400 tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner, amount2, b'\x00' * 20, 0, 0) # Sign the transaction tx3.sign1(key) tx3.sign2(key) child_chain.apply_transaction(rlp.encode(tx3).hex())
def test_send_tx_double_spend(child_chain, u): tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner1, amount2, b'\x00' * 20, 0, 0) tx3.sign1(key1) tx3.sign2(key1) # Submit once child_chain.apply_transaction(rlp.encode(tx3).hex()) # Try to submit again with pytest.raises(AssertionError): child_chain.apply_transaction(rlp.encode(tx3).hex())
def test_send_tx_double_spend(child_chain): tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner1, amount2, b'\x00' * 20, 0, 0) tx3.sign1(tx_key) tx3.sign2(tx_key) # Submit once child_chain.apply_transaction(rlp.encode(tx3).hex()) # Try to submit again with pytest.raises(TxAlreadySpentException): child_chain.apply_transaction(rlp.encode(tx3).hex())
def test_deposit(test_lang): owner_1 = test_lang.get_account() deposit_id = test_lang.deposit(owner_1, 100) tx = Transaction(0, 0, 0, 0, 0, 0, NULL_ADDRESS, owner_1['address'], 100, NULL_ADDRESS, 0, 0) deposit_hash = bytes.fromhex( Web3.soliditySha3(['address', 'address', 'uint256'], [owner_1['address'], '0x' + '00' * 20, 100])[2:]) assert test_lang.transactions[deposit_id]['tx'].hash == tx.hash deposit_blknum = 1 deposit_block = test_lang.child_chain.blocks[deposit_blknum] assert deposit_block.transaction_set[0].hash == tx.hash assert test_lang.root_chain.call().getChildChain(deposit_blknum)[0].encode( 'latin-1') == deposit_hash
def test_send_tx_invalid_sig(child_chain): # Invalid key key = b'8a76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' newowner, amount2 = '0x4b3ec6c9dc67079e82152d6d55d8dd96a8e6aa26', 400 tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner, amount2, b'\x00' * 20, 0, 0) # Sign with an invalid key tx3.sign1(key) tx3.sign2(key) with pytest.raises(AssertionError): child_chain.apply_transaction(rlp.encode(tx3).hex())
def transfer(self, input1, oindex1, newowner1, amount1, signatory1, input2=None, oindex2=0, newowner2=None, amount2=None, signatory2=None): newowner_address1 = newowner1['address'] amount1 = int(amount1) newowner_address2 = NULL_ADDRESS if newowner2 is not None: newowner_address2 = newowner2['address'] amount2 = int(amount2) if amount2 is not None else 0 encoded_input_tx1 = rlp.encode(self.transactions[input1]['tx']).hex() blknum1, txindex1 = self.child_chain.get_tx_pos(encoded_input_tx1) blknum2, txindex2 = 0, 0 if input2 is not None: encoded_input_tx2 = rlp.encode( self.transactions[input2]['tx']).hex() blknum2, txindex2 = self.child_chain.get_tx_pos(encoded_input_tx2) tx = Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, newowner_address1, amount1, newowner_address2, amount2) if signatory1 is not None: key1 = signatory1['key'] tx.sign1(key1) if signatory2 is not None: key2 = signatory2['key'] tx.sign2(key2) encoded_tx = rlp.encode(tx).hex() self.child_chain.apply_transaction(encoded_tx) self.transactions.append({ 'tx': tx, 'confirm_sigs': b'', 'is_deposit': False }) return len(self.transactions) - 1
def deposit(self, account, amount): amount = int(amount) self.root_chain.transact({ 'from': account['address'], 'value': amount }).deposit() # Wait for the Deposit event to be detected time.sleep(1) tx = Transaction(0, 0, 0, 0, 0, 0, account['address'], amount, NULL_ADDRESS, 0) self.transactions.append({ 'tx': tx, 'confirm_sigs': b'', 'is_deposit': True }) return len(self.transactions) - 1
def test_send_tx_double_spend(child_chain): key = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965' newowner, amount2 = '0x4b3ec6c9dc67079e82152d6d55d8dd96a8e6aa26', 400 tx3 = Transaction(1, 0, 0, 1, 1, 0, newowner, amount2, b'\x00' * 20, 0, 0) tx3.sign1(key) tx3.sign2(key) # Submit once child_chain.apply_transaction(rlp.encode(tx3).hex()) # Try to submit again with pytest.raises(AssertionError): child_chain.apply_transaction(rlp.encode(tx3).hex())
def sendtx(client, blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, amount1, newowner1, amount2, newowner2, fee, key1, key2): if newowner1 == "0x0": newowner1 = NULL_ADDRESS if newowner2 == "0x0": newowner2 = NULL_ADDRESS # Form a transaction tx = Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, utils.normalize_address(newowner1), amount1, utils.normalize_address(newowner2), amount2, fee) # Sign it if key1: tx.sign1(utils.normalize_key(key1)) if key2: tx.sign2(utils.normalize_key(key2)) client_call(client.apply_transaction, [tx], "Sent transaction")
def send_tx(self): if len(self.inp) != 14 and len(self.inp) != 13: raise Exception( "Wrong number of inputs for sending a transaction!") blknum1, tx_pos1, utxo_pos1 = int(self.inp[1]), int(self.inp[2]), int( self.inp[3]) blknum2, tx_pos2, utxo_pos2 = int(self.inp[4]), int(self.inp[5]), int( self.inp[6]) newowner1 = utils.normalize_address(self.inp[7]) amount1 = int(self.inp[8]) newowner2 = utils.normalize_address(self.inp[9]) amount2 = int(self.inp[10]) fee = int(self.inp[11]) key1 = utils.normalize_key(self.inp[12]) key2 = utils.normalize_key(self.inp[13]) if len( self.inp) == 14 else b'' tx = Transaction(blknum1, tx_pos1, utxo_pos1, blknum2, tx_pos2, utxo_pos2, newowner1, amount1, newowner2, amount2, fee) tx.sign1(key1) if key2: tx.sign2(key2) self.client.apply_transaction(tx) print("Succesfully added transaction!")