示例#1
0
def test_exiting_1(setup_participate):
    '''
        Alice participated in plasma and has not done any transaction off-chain
        now she wants to exit due to suspected malicious behavior from validator/operator
        ... or any other ol' reason
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address

    # Exiting with a deposit transaction.
    token_uid = next(COIN_COUNTER)
    previous_block = 0
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, alice_addr, alice_addr)
    alice_alice = helpers.utils.generate_tx(*args)

    # alice start exit
    events.start_exit(
        deployed_contracts.plasma_instance,
        coins[token_uid],
        '0x',  # prevtx_bytes
        alice_alice["tx"],  # exitingtx_bytes
        '0x',  # prevtx_inclusion_proof
        '0x',  # exitingtx_inclusion_proof
        alice_alice["signature"],  # signature
        [0, 1],  # blocks
        alice_addr  # exiter
    )

    # alice finishes exit
    events.finish_exit(deployed_contracts, coins[token_uid], alice_addr)
示例#2
0
def test_exiting_3(setup_participate):
    '''
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address

    # alice deposit transaction
    token_uid = next(COIN_COUNTER)
    previous_block = 0
    alice_alice = utils.generate_tx(coins[token_uid], previous_block, COIN_DENOMINATION, alice_addr, alice_addr)

    # alice sends coin to bob
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(coins[token_uid]).call()[1]
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, bob_addr, alice_addr)
    alice_bob = helpers.utils.generate_tx(*args)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid], alice_bob["tx_hash"], block_height)
    alice_bob["proof"], alice_bob["block_number"] = helpers.utils.generate_block(*args)

    # bob sends coins to oscar
    bob_oscar = helpers.utils.generate_tx(
        coins[token_uid],
        alice_bob["block_number"],
        COIN_DENOMINATION,
        oscar_addr,
        bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid], bob_oscar["tx_hash"], block_height)
    bob_oscar["proof"], bob_oscar["block_number"] = helpers.utils.generate_block(*args)

    # oscar starts exit
    events.start_exit(
        deployed_contracts.plasma_instance,
        coins[token_uid],
        alice_bob["tx"],
        bob_oscar["tx"],
        alice_bob["proof"],
        bob_oscar["proof"],
        bob_oscar["signature"],
        [alice_bob["block_number"], bob_oscar["block_number"]],
        oscar_addr
    )

    # oscar finishes exit
    events.finish_exit(deployed_contracts, coins[token_uid], oscar_addr)
示例#3
0
def test_exiting_2(setup_participate):
    '''
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address

    token_uid = next(COIN_COUNTER)
    previous_block = 0
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, alice_addr, alice_addr)
    alice_alice = helpers.utils.generate_tx(*args)

    # alice sends coin to bob
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(coins[token_uid]).call()[1]
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, bob_addr, alice_addr)
    alice_bob = utils.generate_tx(*args)

    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid], alice_bob["tx_hash"], block_height)
    alice_bob["proof"], alice_bob["block_number"] = helpers.utils.generate_block(*args)

    # bob starts exit
    events.start_exit(
        deployed_contracts.plasma_instance,
        coins[token_uid],
        alice_alice["tx"],
        alice_bob["tx"],
        '0x',
        # prevtx_inclusion_proof is 0x0 since prevtx of bob is alice deposit tx
        # which has no merkle proof.
        alice_bob["proof"],
        alice_bob["signature"],
        # 2 is the prevBlock of prevTx | since prevtx is deposit and coins is
        # [1] it means that it is in the block 2 since this coin is the second
        # which was minted.
        [2, alice_bob["block_number"]],
        bob_addr
    )

    # bob finishes exit
    events.finish_exit(deployed_contracts, coins[token_uid], bob_addr)
def test_failing_challenge_5(setup_participate):
    '''
        Cannot challenge exit with an earlier spend
            bob sends coin to oscar
            bob sends coin to charlie
            charlie sends coin to peter
            alice challenges but fails due to providing earlier spend
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address
    charlie_addr = accounts[4].address

    previous_block = 0
    alice_alice = helpers.utils.generate_tx(coins[7], previous_block,
                                            COIN_DENOMINATION, alice_addr,
                                            alice_addr)

    # alice sends coin to bob
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(
        coins[0]).call()[1]
    alice_bob = helpers.utils.generate_tx(coins[7], previous_block,
                                          COIN_DENOMINATION, bob_addr,
                                          alice_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[7], alice_bob["tx_hash"],
            block_height)
    alice_bob["proof"], alice_bob[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to oscar
    bob_oscar = helpers.utils.generate_tx(coins[7], alice_bob["block_number"],
                                          COIN_DENOMINATION, oscar_addr,
                                          bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[7], bob_oscar["tx_hash"],
            block_height)
    bob_oscar["proof"], bob_oscar[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to charlie / charlie is a address owned by bob too.
    bob_charlie = helpers.utils.generate_tx(coins[7],
                                            alice_bob["block_number"],
                                            COIN_DENOMINATION, charlie_addr,
                                            bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[7],
            bob_charlie["tx_hash"], block_height)
    bob_charlie["proof"], bob_charlie[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to charlie / charlie is a address owned by bob too.
    charlie_peter = helpers.utils.generate_tx(coins[7],
                                              bob_charlie["block_number"],
                                              COIN_DENOMINATION, charlie_addr,
                                              charlie_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[7],
            charlie_peter["tx_hash"], block_height)
    charlie_peter["proof"], charlie_peter[
        "block_number"] = helpers.utils.generate_block(*args)

    # charlie starts exit.
    events.start_exit(deployed_contracts.plasma_instance, coins[7],
                      alice_bob["tx"], bob_charlie["tx"], alice_bob["proof"],
                      bob_charlie["proof"], bob_charlie["signature"],
                      [alice_bob["block_number"], bob_charlie["block_number"]],
                      charlie_addr)

    # alice challenges charlie
    with pytest.raises(Exception):
        helpers.events.challenge_between(deployed_contracts.plasma_instance,
                                         coins[7], alice_bob["block_number"],
                                         alice_bob["tx"], alice_bob["proof"],
                                         alice_bob["signature"], alice_addr)
        # Cannot challenge exit with an earlier spend

    # charlie finishes exit.
    events.finish_exit(deployed_contracts, coins[7], charlie_addr)
def test_successful_exit1(setup_participate):
    '''
        same challenge_1 but no one challenges and maturity period is achived and coin can be finalized
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address
    charlie_addr = accounts[4].address

    previous_block = 0
    alice_alice = helpers.utils.generate_tx(coins[5], previous_block,
                                            COIN_DENOMINATION, alice_addr,
                                            alice_addr)

    # alice sends coin to bob
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(
        coins[5]).call()[1]
    alice_bob = helpers.utils.generate_tx(coins[5], previous_block,
                                          COIN_DENOMINATION, bob_addr,
                                          alice_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[5], alice_bob["tx_hash"],
            block_height)
    alice_bob["proof"], alice_bob[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to oscar
    bob_oscar = helpers.utils.generate_tx(coins[5], alice_bob["block_number"],
                                          COIN_DENOMINATION, oscar_addr,
                                          bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[5], bob_oscar["tx_hash"],
            block_height)
    bob_oscar["proof"], bob_oscar[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to charlie
    bob_charlie = helpers.utils.generate_tx(coins[5],
                                            alice_bob["block_number"],
                                            COIN_DENOMINATION, charlie_addr,
                                            bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[5],
            bob_charlie["tx_hash"], block_height)
    bob_charlie["proof"], bob_charlie[
        "block_number"] = helpers.utils.generate_block(*args)

    # charlie starts exit
    events.start_exit(deployed_contracts.plasma_instance, coins[5],
                      alice_bob["tx"], bob_charlie["tx"], alice_bob["proof"],
                      bob_charlie["proof"], bob_charlie["signature"],
                      [alice_bob["block_number"], bob_charlie["block_number"]],
                      charlie_addr)

    # no one challenges thus charlie can finalize exit
    events.finish_exit(deployed_contracts, coins[5], charlie_addr)
def test_failing_challenge_3(setup_participate):
    '''
        same challenge_1  but with invalid merkle proof provided
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address
    charlie_addr = accounts[4].address

    previous_block = 0
    alice_alice = helpers.utils.generate_tx(coins[3], previous_block,
                                            COIN_DENOMINATION, alice_addr,
                                            alice_addr)

    # alice sends coin to bob
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(
        coins[3]).call()[1]
    alice_bob = helpers.utils.generate_tx(coins[3], previous_block,
                                          COIN_DENOMINATION, bob_addr,
                                          alice_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[3], alice_bob["tx_hash"],
            block_height)
    alice_bob["proof"], alice_bob[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to oscar
    bob_oscar = helpers.utils.generate_tx(coins[3], alice_bob["block_number"],
                                          COIN_DENOMINATION, oscar_addr,
                                          bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[3], bob_oscar["tx_hash"],
            block_height)
    bob_oscar["proof"], bob_oscar[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to charlie / charlie is a address owned by bob too.
    bob_charlie = helpers.utils.generate_tx(coins[3],
                                            alice_bob["block_number"],
                                            COIN_DENOMINATION, charlie_addr,
                                            bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[3],
            bob_charlie["tx_hash"], block_height)
    bob_charlie["proof"], bob_charlie[
        "block_number"] = helpers.utils.generate_block(*args)

    # oscar startes exit
    events.start_exit(deployed_contracts.plasma_instance, coins[3],
                      alice_bob["tx"], bob_charlie["tx"], alice_bob["proof"],
                      bob_charlie["proof"], bob_charlie["signature"],
                      [alice_bob["block_number"], bob_charlie["block_number"]],
                      charlie_addr)

    # oscar provides invalid merkle proof, he fails to stop charlie's exit
    with pytest.raises(Exception):
        helpers.events.challenge_between(deployed_contracts.plasma_instance,
                                         coins[3], bob_oscar["block_number"],
                                         bob_oscar["tx"], '0x12345678',
                                         bob_oscar["signature"], oscar_addr)
        # Invalid merkle proof provided!

    # charlie finishes exit.
    events.finish_exit(deployed_contracts, coins[3], charlie_addr)
示例#7
0
def test_challenge_after_9(setup_participate):
    '''
        Cannot challenge with non-direct spend
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address
    charlie_addr = accounts[4].address

    # alice deposit transaction
    token_uid = next(COIN_COUNTER)
    previous_block = 0
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, alice_addr,
            alice_addr)
    alice_alice = helpers.utils.generate_tx(*args)

    # alice sends coin to bob
    # previous_block = 9
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(
        coins[token_uid]).call()[1]
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, bob_addr,
            alice_addr)
    alice_bob = helpers.utils.generate_tx(*args)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            alice_bob["tx_hash"], block_height)
    alice_bob["proof"], alice_bob[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to oscar
    bob_oscar = helpers.utils.generate_tx(coins[token_uid],
                                          alice_bob["block_number"],
                                          COIN_DENOMINATION, oscar_addr,
                                          bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            bob_oscar["tx_hash"], block_height)
    bob_oscar["proof"], bob_oscar[
        "block_number"] = helpers.utils.generate_block(*args)

    # oscar sends coin to charlie
    oscar_charlie = helpers.utils.generate_tx(coins[token_uid],
                                              bob_oscar["block_number"],
                                              COIN_DENOMINATION, charlie_addr,
                                              oscar_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            oscar_charlie["tx_hash"], block_height)
    oscar_charlie["proof"], oscar_charlie[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob starts exit
    events.start_exit(deployed_contracts.plasma_instance, coins[token_uid],
                      alice_alice["tx"], alice_bob["tx"], '0x',
                      alice_bob["proof"], alice_bob["signature"],
                      [9, alice_bob["block_number"]], bob_addr)

    with pytest.raises(Exception):
        # charlie challenges bob
        helpers.events.challenge_after(deployed_contracts, coins[token_uid],
                                       oscar_charlie["block_number"],
                                       oscar_charlie["tx"],
                                       oscar_charlie["proof"],
                                       oscar_charlie["signature"],
                                       charlie_addr)
        # Cannot challenge with non-direct spend.

    # bob finishes exit
    events.finish_exit(deployed_contracts, coins[token_uid], bob_addr)
示例#8
0
def test_challenge_after_7(setup_participate):
    '''
        Cannot challenge with earlier tx
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address

    # alice deposit transaction
    token_uid = next(COIN_COUNTER)
    previous_block = 0
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, alice_addr,
            alice_addr)
    alice_alice = helpers.utils.generate_tx(*args)

    # alice sends coin to bob
    # previous_block = 7
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(
        coins[token_uid]).call()[1]
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, bob_addr,
            alice_addr)
    alice_bob = helpers.utils.generate_tx(*args)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            alice_bob["tx_hash"], block_height)
    alice_bob["proof"], alice_bob[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to oscar
    bob_oscar = helpers.utils.generate_tx(coins[token_uid],
                                          alice_bob["block_number"],
                                          COIN_DENOMINATION, oscar_addr,
                                          bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            bob_oscar["tx_hash"], block_height)
    bob_oscar["proof"], bob_oscar[
        "block_number"] = helpers.utils.generate_block(*args)

    # oscar starts exit...
    events.start_exit(
        deployed_contracts.plasma_instance,
        coins[token_uid],
        alice_bob["tx"],
        bob_oscar["tx"],
        alice_bob["proof"],
        bob_oscar["proof"],
        bob_oscar["signature"],
        [alice_bob["block_number"], bob_oscar["block_number"]],
        oscar_addr,
    )

    # it should fail
    with pytest.raises(Exception):
        # bob challenge oscar's exit
        helpers.events.challenge_after(deployed_contracts.plasma_instance,
                                       coins[token_uid],
                                       alice_bob["block_number"],
                                       alice_bob["tx"], alice_bob["proof"],
                                       alice_bob["signature"], bob_addr)
        # he fails because he challenged with earlier tx!

    # oscar finishes exit
    events.finish_exit(deployed_contracts, coins[token_uid], oscar_addr)
示例#9
0
def test_challenge_after_5(setup_participate):
    '''
        Same as above but with invalid tx provided...
        Thus oscar can exit a double spend coin.
    '''
    accounts, deployed_contracts, coins = setup_participate
    alice_addr = accounts[1].address
    bob_addr = accounts[2].address
    oscar_addr = accounts[3].address
    charlie_addr = accounts[4].address

    # alice deposit transaction
    token_uid = next(COIN_COUNTER)
    previous_block = 0
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, alice_addr,
            alice_addr)
    alice_alice = utils.generate_tx(*args)

    # alice sends coin to bob
    # previous_block = 5
    previous_block = deployed_contracts.plasma_instance.functions.getPlasmaCoin(
        coins[token_uid]).call()[1]
    args = (coins[token_uid], previous_block, COIN_DENOMINATION, bob_addr,
            alice_addr)
    alice_bob = helpers.utils.generate_tx(*args)

    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            alice_bob["tx_hash"], block_height)
    alice_bob["proof"], alice_bob[
        "block_number"] = helpers.utils.generate_block(*args)

    # bob sends coin to oscar
    bob_oscar = helpers.utils.generate_tx(coins[token_uid],
                                          alice_bob["block_number"],
                                          COIN_DENOMINATION, oscar_addr,
                                          bob_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            bob_oscar["tx_hash"], block_height)
    bob_oscar["proof"], bob_oscar[
        "block_number"] = helpers.utils.generate_block(*args)

    # oscar sends coin to charlie
    oscar_charlie = helpers.utils.generate_tx(coins[token_uid],
                                              bob_oscar["block_number"],
                                              COIN_DENOMINATION, charlie_addr,
                                              oscar_addr)

    # plasma block is generated and submited to mainnet
    block_height = next(BLOCK_COUNTER)
    args = (deployed_contracts.plasma_instance, coins[token_uid],
            oscar_charlie["tx_hash"], block_height)
    oscar_charlie["proof"], oscar_charlie[
        "block_number"] = helpers.utils.generate_block(*args)

    # oscar starts exit...
    events.start_exit(
        deployed_contracts.plasma_instance,
        coins[token_uid],
        alice_bob["tx"],
        bob_oscar["tx"],
        alice_bob["proof"],
        bob_oscar["proof"],
        bob_oscar["signature"],
        [alice_bob["block_number"], bob_oscar["block_number"]],
        oscar_addr,
    )

    # it should fail
    with pytest.raises(Exception):
        # charlie challenges oscar but fails
        helpers.events.challenge_after(
            deployed_contracts.plasma_instance,
            coins[token_uid],
            oscar_charlie["block_number"],
            bob_oscar[
                "tx"],  # invalid tx provided, it has to be oscar_charlie["tx"]
            oscar_charlie["proof"],
            oscar_charlie["signature"],
            charlie_addr)

    # oscar finishes exit
    events.finish_exit(deployed_contracts, coins[token_uid], oscar_addr)