Skip to content

Commit

Permalink
chore: amend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pik694 committed Nov 6, 2019
1 parent b3cfe6e commit 4b6f86f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
15 changes: 7 additions & 8 deletions plasma_framework/python_tests/testlang/testlang.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from plasma_core.child_chain import ChildChain
from plasma_core.block import Block
from plasma_core.transaction import Transaction, TransactionOutput
from plasma_core.constants import MIN_EXIT_PERIOD, NULL_SIGNATURE, NULL_ADDRESS
from plasma_core.constants import MIN_EXIT_PERIOD, NULL_ADDRESS
from plasma_core.utils.transactions import decode_utxo_id, encode_utxo_id
from plasma_core.utils.merkle.fixed_merkle import FixedMerkle

Expand Down Expand Up @@ -214,19 +214,18 @@ def start_standard_exit_with_tx_body(self, output_id, output_tx, account, bond=N
self.root_chain.startStandardExit(output_id, output_tx.encoded, proof,
**{'value': bond, 'from': account.address})

def challenge_standard_exit(self, output_id, spend_id, input_index=None):
def challenge_standard_exit(self, output_id, spend_id, input_index=None, signature=None):
spend_tx = self.child_chain.get_transaction(spend_id)
exiting_tx = self.child_chain.get_transaction(output_id)

signature = NULL_SIGNATURE
if input_index is None:
for i in range(0, 4):
signature = spend_tx.signatures[i]
if spend_tx.inputs[i].identifier == output_id and signature != NULL_SIGNATURE:
for i in range(len(spend_tx.inputs)):
if spend_tx.inputs[i].identifier == output_id:
input_index = i
break
if input_index is None:
input_index = 3
if signature is None:
signature = spend_tx.signatures[input_index]

exit_id = self.get_standard_exit_id(output_id)
self.root_chain.challengeStandardExit(exit_id, spend_tx.encoded, input_index, signature, exiting_tx.encoded)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_challenge_standard_exit_valid_spend_should_succeed(testlang):
doublespend_id = testlang.spend_utxo([spend_id], [owner], outputs=[(owner.address, NULL_ADDRESS, amount)])
testlang.challenge_standard_exit(spend_id, doublespend_id)

assert testlang.get_standard_exit(spend_id) == [NULL_ADDRESS_HEX, NULL_ADDRESS_HEX, 0]
assert testlang.get_standard_exit(spend_id) == [NULL_ADDRESS_HEX, 0, 0, False]


def test_challenge_standard_exit_if_successful_awards_the_bond(testlang):
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_challenge_standard_exit_mature_valid_spend_should_succeed(testlang):
testlang.forward_timestamp(2 * MIN_EXIT_PERIOD + 1)

testlang.challenge_standard_exit(spend_id, doublespend_id)
assert testlang.get_standard_exit(spend_id) == [NULL_ADDRESS_HEX, NULL_ADDRESS_HEX, 0]
assert testlang.get_standard_exit(spend_id) == [NULL_ADDRESS_HEX, 0, 0, False]


def test_challenge_standard_exit_invalid_spend_should_fail(testlang):
Expand All @@ -64,18 +64,21 @@ def test_challenge_standard_exit_unrelated_spend_should_fail(testlang):
spend_id = testlang.spend_utxo([deposit_id_2], [owner])

with pytest.raises(TransactionFailed):
testlang.challenge_standard_exit(deposit_id_1, spend_id)
testlang.challenge_standard_exit(deposit_id_1, spend_id, input_index=0)


def test_challenge_standard_exit_uninitialized_memory_and_zero_sig_should_fail(testlang):
bond = testlang.root_chain.standardExitBond()
owner, amount = testlang.accounts[0], 100 * bond

deposit_id = testlang.deposit(owner, amount)
deposit_tx = testlang.child_chain.get_transaction(deposit_id)

spend_id = testlang.spend_utxo([deposit_id], [owner])
tx = testlang.child_chain.get_transaction(spend_id)
spend_tx = testlang.child_chain.get_transaction(spend_id)

with pytest.raises(TransactionFailed):
testlang.root_chain.challengeStandardExit(0, tx.encoded, 3, NULL_SIGNATURE)
testlang.root_chain.challengeStandardExit(0, spend_tx.encoded, 3, NULL_SIGNATURE, deposit_tx.encoded)


def test_challenge_standard_exit_not_started_should_fail(testlang):
Expand All @@ -97,7 +100,7 @@ def test_challenge_standard_exit_wrong_oindex_should_fail(testlang):

spend_tx = Transaction(inputs=[decode_utxo_id(deposit_id)],
outputs=[(alice.address, NULL_ADDRESS, alice_money), (bob.address, NULL_ADDRESS, bob_money)])
spend_tx.sign(0, alice, verifyingContract=testlang.root_chain)
spend_tx.sign(0, alice, verifyingContract=testlang.root_chain.plasma_framework)
blknum = testlang.submit_block([spend_tx])
alice_utxo = encode_utxo_id(blknum, 0, 0)
bob_utxo = encode_utxo_id(blknum, 0, 1)
Expand All @@ -109,22 +112,23 @@ def test_challenge_standard_exit_wrong_oindex_should_fail(testlang):
alice_spend_id = testlang.spend_utxo([alice_utxo], [alice], outputs=[(alice.address, NULL_ADDRESS, alice_money)])

with pytest.raises(TransactionFailed):
testlang.challenge_standard_exit(alice_utxo, bob_spend_id)
testlang.challenge_standard_exit(alice_utxo, bob_spend_id, input_index=0)

with pytest.raises(TransactionFailed):
testlang.challenge_standard_exit(bob_utxo, alice_spend_id)
testlang.challenge_standard_exit(bob_utxo, alice_spend_id, input_index=0)

testlang.challenge_standard_exit(alice_utxo, alice_spend_id)


@pytest.mark.skip("Includes starting an IFE")
def test_challenge_standard_exit_with_in_flight_exit_tx_should_succeed(testlang):
# exit cross-spend test, cases 3 and 4
owner, amount = testlang.accounts[0], 100
deposit_id = testlang.deposit(owner, amount)
spend_id = testlang.spend_utxo([deposit_id], [owner], outputs=[(owner.address, NULL_ADDRESS, amount)])

ife_tx = Transaction(inputs=[decode_utxo_id(spend_id)], outputs=[(owner.address, NULL_ADDRESS, amount)])
ife_tx.sign(0, owner, verifyingContract=testlang.root_chain)
ife_tx.sign(0, owner, verifyingContract=testlang.root_chain.plasma_framework)

(encoded_spend, encoded_inputs, proofs, signatures) = testlang.get_in_flight_exit_info(None, spend_tx=ife_tx)
bond = testlang.root_chain.inFlightExitBond()
Expand All @@ -135,7 +139,8 @@ def test_challenge_standard_exit_with_in_flight_exit_tx_should_succeed(testlang)
assert testlang.get_standard_exit(spend_id).amount == 100

exit_id = testlang.get_standard_exit_id(spend_id)
exiting_tx = testlang.get_transaction(spend_id)
# FIXME a proper way of getting encoded body of IFE tx is to get it out of generated events
testlang.root_chain.challengeStandardExit(exit_id, ife_tx.encoded, 0, ife_tx.signatures[0])
testlang.root_chain.challengeStandardExit(exit_id, ife_tx.encoded, 0, ife_tx.signatures[0], exiting_tx.encoded)

assert testlang.get_standard_exit(spend_id) == [NULL_ADDRESS_HEX, NULL_ADDRESS_HEX, 0, 0]
assert testlang.get_standard_exit(spend_id) == [NULL_ADDRESS_HEX, 0, 0, False]

0 comments on commit 4b6f86f

Please sign in to comment.