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 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_start_exit(t, root_chain, assert_tx_failed): week_and_a_half = 60 * 60 * 24 * 13 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) priority1 = 1 * 1000000000 + 10000 * 0 + 0 snapshot = t.chain.snapshot() sigs = tx1.sig1 + tx1.sig2 + confirmSig1 # Deposit exit root_chain.startExit([1, 0, 0], tx_bytes1, proof, sigs, sender=key) t.chain.head_state.timestamp += week_and_a_half # Cannot exit twice off of the same utxo assert_tx_failed(lambda: root_chain.startExit( [1, 0, 0], tx_bytes1, proof, sigs, sender=key)) assert root_chain.getExit(priority1) == [ '0x' + owner.hex(), 100, [1, 0, 0] ] t.chain.revert(snapshot) 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) confirmSig1 = confirm_tx(tx2, root_chain.getChildChain(2)[0], key) priority2 = 2 * 1000000000 + 10000 * 0 + 0 sigs = tx2.sig1 + tx2.sig2 + confirmSig1 snapshot = t.chain.snapshot() # Single input exit root_chain.startExit([2, 0, 0], tx_bytes2, proof, sigs, sender=key) assert root_chain.getExit(priority2) == [ '0x' + owner.hex(), 100, [2, 0, 0] ] t.chain.revert(snapshot) root_chain.deposit(tx_bytes1, value=value_1) tx3 = Transaction(2, 0, 0, 3, 0, 0, owner, value_1, null_address, 0, 0) tx3.sign1(key) tx3.sign2(key) tx_bytes3 = rlp.encode(tx3, UnsignedTransaction) merkle = FixedMerkle(16, [tx3.merkle_hash], True) proof = merkle.create_membership_proof(tx3.merkle_hash) t.chain.head_state.block_number += 7 root_chain.submitBlock(merkle.root) confirmSig1 = confirm_tx(tx3, root_chain.getChildChain(4)[0], key) confirmSig2 = confirm_tx(tx3, root_chain.getChildChain(4)[0], key) priority3 = 4 * 1000000000 + 10000 * 0 + 0 sigs = tx3.sig1 + tx3.sig2 + confirmSig1 + confirmSig2 # Double input exit root_chain.startExit([4, 0, 0], tx_bytes3, proof, sigs, sender=key) assert root_chain.getExit(priority3) == [ '0x' + owner.hex(), 100, [4, 0, 0] ]
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, 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 withdraw(self): blknum, txindex, oindex = int(self.inp[1]), int(self.inp[2]), int( self.inp[3]) txPos = [blknum, txindex, oindex] key1 = utils.normalize_key(self.inp[4]) key2 = utils.normalize_key(self.inp[5]) if len(self.inp) == 6 else b'' block = self.client.get_block(blknum) block = rlp.decode(utils.decode_hex(block), Block) tx = block.transaction_set[txindex] block.merkilize_transaction_set proof = block.merkle.create_membership_proof(tx.merkle_hash) confirmSig1 = confirm_tx(tx, block.merkle.root, key1) confirmSig2 = b'' if key2: confirmSig2 = confirm_tx(tx, block.merkle.root, key2) sigs = tx.sig1 + tx.sig2 + confirmSig1 + confirmSig2 self.client.withdraw(txPos, tx, proof, sigs) print("Successfully submitted a withdrawal")
def withdraw(client, blknum, txindex, oindex, key1, key2): # Get the transaction's block, already decoded by client block = client.get_block(blknum) # Create a Merkle proof tx = block.transaction_set[txindex] block.merklize_transaction_set() proof = block.merkle.create_membership_proof(tx.merkle_hash) # Create the confirmation signatures confirmSig1, confirmSig2 = b'', b'' if key1: confirmSig1 = confirm_tx(tx, block.merkle.root, utils.normalize_key(key1)) if key2: confirmSig2 = confirm_tx(tx, block.merkle.root, utils.normalize_key(key2)) sigs = tx.sig1 + tx.sig2 + confirmSig1 + confirmSig2 client.withdraw(blknum, txindex, oindex, tx, proof, sigs) print('Submitted withdrawal')
def confirm(self, tx_id, signatory1, signatory2=None): transaction = self.transactions[tx_id] tx = transaction['tx'] encoded_tx = rlp.encode(tx).hex() blknum, _ = self.child_chain.get_tx_pos(encoded_tx) block_root = self.child_chain.blocks[blknum].merkle.root confirm_sigs = b'' for signatory in [x for x in [signatory1, signatory2] if x is not None]: confirm_sigs += confirm_tx(tx, block_root, signatory['key']) self.transactions[tx_id]['confirm_sigs'] = confirm_sigs
def test_start_exit(t, root_chain, assert_tx_failed): week_and_a_half = 60 * 60 * 24 * 13 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) deposit_tx_hash = get_deposit_hash(owner, null_address, value_1) dep_blknum = root_chain.getDepositBlock() assert dep_blknum == 1 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(dep_blknum)[0], key) priority1 = dep_blknum * 1000000000 + 10000 * 0 + 1 snapshot = t.chain.snapshot() sigs = tx1.sig1 + tx1.sig2 + confirmSig1 utxoId = dep_blknum * 1000000000 + 10000 * 0 + 1 # Deposit exit root_chain.startDepositExit(utxoId, null_address, tx1.amount1, sender=key) t.chain.head_state.timestamp += week_and_a_half # Cannot exit twice off of the same utxo utxo_pos1 = dep_blknum * 1000000000 + 10000 * 0 + 1 assert_tx_failed(lambda: root_chain.startExit( utxo_pos1, deposit_tx_hash, proof, sigs, sender=key)) enc_null_address = "0x" + "00" * 20 assert root_chain.getExit(priority1) == [ '0x' + owner.hex(), enc_null_address, 100 ] t.chain.revert(snapshot) tx2 = Transaction(dep_blknum, 0, 0, 0, 0, 0, null_address, owner, value_1, null_address, 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() assert child_blknum == 1000 root_chain.submitBlock(merkle.root) confirmSig1 = confirm_tx(tx2, root_chain.getChildChain(child_blknum)[0], key) priority2 = child_blknum * 1000000000 + 10000 * 0 + 0 sigs = tx2.sig1 + tx2.sig2 + confirmSig1 snapshot = t.chain.snapshot() # # Single input exit utxo_pos2 = child_blknum * 1000000000 + 10000 * 0 + 0 root_chain.startExit(utxo_pos2, tx_bytes2, proof, sigs, sender=key) assert root_chain.getExit(priority2) == [ '0x' + owner.hex(), enc_null_address, 100 ] t.chain.revert(snapshot) dep2_blknum = root_chain.getDepositBlock() assert dep2_blknum == 1001 root_chain.deposit(value=value_1, sender=key) tx3 = Transaction(child_blknum, 0, 0, dep2_blknum, 0, 0, null_address, owner, value_1, null_address, 0, 0) tx3.sign1(key) tx3.sign2(key) tx_bytes3 = rlp.encode(tx3, UnsignedTransaction) merkle = FixedMerkle(16, [tx3.merkle_hash], True) proof = merkle.create_membership_proof(tx3.merkle_hash) child2_blknum = root_chain.currentChildBlock() assert child2_blknum == 2000 root_chain.submitBlock(merkle.root) confirmSig1 = confirm_tx(tx3, root_chain.getChildChain(child2_blknum)[0], key) confirmSig2 = confirm_tx(tx3, root_chain.getChildChain(child2_blknum)[0], key) priority3 = child2_blknum * 1000000000 + 10000 * 0 + 0 sigs = tx3.sig1 + tx3.sig2 + confirmSig1 + confirmSig2 # Double input exit utxoPos3 = child2_blknum * 1000000000 + 10000 * 0 + 0 root_chain.startExit(utxoPos3, tx_bytes3, proof, sigs, sender=key) assert root_chain.getExit(priority3) == [ '0x' + owner.hex(), enc_null_address, 100 ]