示例#1
0
    def test_assert_coin_consumed(self):
        run = asyncio.get_event_loop().run_until_complete

        remote = make_client_server()

        puzzle_program = p2_delegated_conditions.puzzle_for_pk(
            public_key_bytes_for_index(8))
        puzzle_hash = ProgramHash(puzzle_program)

        coin_1 = farm_spendable_coin(remote, puzzle_hash)
        coin_2 = farm_spendable_coin(remote, puzzle_hash)

        conditions_coin_1 = [
            make_assert_coin_consumed_condition(coin_2.name())
        ]
        solution_1 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_1)
        spend_bundle_1 = build_spend_bundle(coin_1, solution_1)

        # try to spend coin_1 without coin_2. Should fail.
        r = run(remote.push_tx(tx=spend_bundle_1))
        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_COIN_CONSUMED_FAILED")

        # try to spend coin_1 with coin_2. Should be okay
        spend_bundle_2 = build_spend_bundle(coin_2, solution_1)
        spend_bundle = spend_bundle_1.aggregate(
            [spend_bundle_1, spend_bundle_2])

        r = run(remote.push_tx(tx=spend_bundle))
        assert r["response"].startswith("accepted")
        spend_bundle_2 = build_spend_bundle(coin_2, solution_1)
示例#2
0
    def test_assert_my_id(self):
        run = asyncio.get_event_loop().run_until_complete

        remote = make_client_server()

        puzzle_program = p2_delegated_conditions.puzzle_for_pk(
            public_key_bytes_for_index(8))
        puzzle_hash = ProgramHash(puzzle_program)

        coin_1 = farm_spendable_coin(remote, puzzle_hash)
        coin_2 = farm_spendable_coin(remote, puzzle_hash)
        coin_3 = farm_spendable_coin(remote, puzzle_hash)

        conditions_coin_1 = [make_assert_my_coin_id_condition(coin_1.name())]
        solution_1 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_1)
        spend_bundle = build_spend_bundle(coin_1, solution_1)
        r = run(remote.push_tx(tx=spend_bundle))
        assert r["response"].startswith("accepted")

        spend_bundle = build_spend_bundle(coin_2, solution_1)
        r = run(remote.push_tx(tx=spend_bundle))
        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_MY_COIN_ID_FAILED")

        spend_bundle = build_spend_bundle(coin_3, solution_1)
        r = run(remote.push_tx(tx=spend_bundle))
        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_MY_COIN_ID_FAILED")
示例#3
0
    def test_assert_time_exceeds(self):
        run = asyncio.get_event_loop().run_until_complete

        remote = make_client_server()

        puzzle_program = p2_delegated_conditions.puzzle_for_pk(
            public_key_bytes_for_index(8))
        puzzle_hash = ProgramHash(puzzle_program)

        coin_1 = farm_spendable_coin(remote, puzzle_hash)

        now = run(remote.skip_milliseconds(ms=uint64(0).to_bytes(4, 'big')))
        assert (type(now) == int)
        min_time = now + 1000
        conditions_time_exceeds = [
            make_assert_time_exceeds_condition(min_time)
        ]

        # try to spend coin_1 with limit set to age 1. Should fail
        solution_1 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_time_exceeds)
        spend_bundle_1 = build_spend_bundle(coin_1, solution_1)
        r = run(remote.push_tx(tx=spend_bundle_1))

        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_TIME_EXCEEDS_FAILED")

        # wait a second, should succeed
        r = run(remote.skip_milliseconds(ms=uint64(1001)))

        r = run(remote.push_tx(tx=spend_bundle_1))
        assert r["response"].startswith("accepted")
示例#4
0
    def test_assert_block_age_exceeds(self):
        run = asyncio.get_event_loop().run_until_complete

        remote = make_client_server()

        puzzle_program = p2_delegated_conditions.puzzle_for_pk(
            public_key_bytes_for_index(8))
        puzzle_hash = ProgramHash(puzzle_program)

        # farm a bunch of blocks to start
        for _ in range(20):
            farm_spendable_coin(remote, puzzle_hash)

        coin_1 = farm_spendable_coin(remote, puzzle_hash)

        conditions_block_age_exceeds_1 = [
            make_assert_block_age_exceeds_condition(1)
        ]

        # try to spend coin_1 with limit set to age 1. Should fail
        solution_1 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_block_age_exceeds_1)
        spend_bundle_1 = build_spend_bundle(coin_1, solution_1)
        r = run(remote.push_tx(tx=spend_bundle_1))
        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_BLOCK_AGE_EXCEEDS_FAILED")

        # farm a block and try again. Should succeed
        farm_spendable_coin(remote, puzzle_hash)

        r = run(remote.push_tx(tx=spend_bundle_1))
        assert r["response"].startswith("accepted")
示例#5
0
def run_test(puzzle_hash, solution, payments):
    run = asyncio.get_event_loop().run_until_complete

    remote = make_client_server()

    coin = farm_spendable_coin(remote, puzzle_hash)
    spend_bundle = build_spend_bundle(coin, solution)

    # push it
    r = run(remote.push_tx(tx=spend_bundle))
    assert r["response"].startswith("accepted")
    print(r)

    # confirm it
    farm_spendable_coin(remote)

    # get unspents
    r = run(remote.all_unspents())
    print("unspents = %s" % r.get("unspents"))
    unspents = r["unspents"]

    # ensure all outputs are there
    for puzzle_hash, amount in payments:
        expected_coin = Coin(coin.name(), puzzle_hash, amount)
        name = expected_coin.name()
        assert name in unspents
        unspent = run(remote.unspent_for_coin_name(coin_name=name))
        assert unspent.confirmed_block_index == 2
        assert unspent.spent_block_index == 0
示例#6
0
async def client_test(path):

    remote = await proxy_for_unix_connection(path)

    payments = [
        (puzzle_hash_for_index(0), 1000),
        (puzzle_hash_for_index(1), 2000),
    ]

    conditions = conditions_for_payment(payments)
    coinbase_puzzle_hash = ProgramHash(puzzle_for_conditions(conditions))

    fees_puzzle_hash = puzzle_hash_for_index(6)

    r = await remote.next_block(coinbase_puzzle_hash=coinbase_puzzle_hash,
                                fees_puzzle_hash=fees_puzzle_hash)
    header = r.get("header")
    body = r.get("body")

    for _ in [header, body]:
        hh = std_hash(_)
        r1 = await remote.hash_preimage(hash=hh)
        assert r1 == bytes(_)

    coinbase_coin = body.coinbase_coin

    new_coinbase_puzzle_hash = puzzle_hash_for_index(5)

    # farm a few blocks
    for _ in range(5):
        r = await remote.next_block(
            coinbase_puzzle_hash=new_coinbase_puzzle_hash,
            fees_puzzle_hash=fees_puzzle_hash,
        )

        assert "header" in r

    # spend the coinbase coin

    solution = solution_for_conditions(conditions)
    spend_bundle = build_spend_bundle(coinbase_coin, solution)

    r = await remote.push_tx(tx=spend_bundle)
    assert r["response"].startswith("accepted")

    # farm a few blocks
    for _ in range(5):
        r = await remote.next_block(
            coinbase_puzzle_hash=new_coinbase_puzzle_hash,
            fees_puzzle_hash=fees_puzzle_hash,
        )

        assert "header" in r

    r = await remote.push_tx(tx=spend_bundle)
    assert r.args[0].startswith("exception: (<Err.DOUBLE_SPEND")

    r = await remote.all_unspents()
    coin_names = r["unspents"]
    coin_name = coin_names[0]
    assert coin_name == coinbase_coin.name()

    unspent = await remote.unspent_for_coin_name(coin_name=coin_name)
    assert unspent.spent_block_index == 7
示例#7
0
    def test_assert_block_index_exceeds(self):
        run = asyncio.get_event_loop().run_until_complete

        remote = make_client_server()

        puzzle_program = p2_delegated_conditions.puzzle_for_pk(
            public_key_bytes_for_index(8))
        puzzle_hash = ProgramHash(puzzle_program)

        coin_1 = farm_spendable_coin(remote, puzzle_hash)
        coin_2 = farm_spendable_coin(remote, puzzle_hash)

        conditions_coin_exceeds_block_1 = [
            make_assert_block_index_exceeds_condition(1)
        ]
        conditions_coin_exceeds_block_2 = [
            make_assert_block_index_exceeds_condition(2)
        ]
        conditions_coin_exceeds_block_3 = [
            make_assert_block_index_exceeds_condition(3)
        ]
        conditions_coin_exceeds_block_4 = [
            make_assert_block_index_exceeds_condition(4)
        ]

        # try to spend coin_1 with limit set to block 3. Should fail
        solution_1 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_exceeds_block_3)
        spend_bundle_1 = build_spend_bundle(coin_1, solution_1)
        r = run(remote.push_tx(tx=spend_bundle_1))
        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_BLOCK_INDEX_EXCEEDS_FAILED")

        # try to spend coin_1 with limit set to block 2. Should succeed
        solution_2 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_exceeds_block_2)
        spend_bundle_2 = build_spend_bundle(coin_1, solution_2)
        r = run(remote.push_tx(tx=spend_bundle_2))
        assert r["response"].startswith("accepted")

        # advance a block
        coin_3 = farm_spendable_coin(remote, puzzle_hash)

        # try to spend coin_2 with limit set to block 4. Should fail
        solution_3 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_exceeds_block_4)
        spend_bundle_3 = build_spend_bundle(coin_2, solution_3)
        r = run(remote.push_tx(tx=spend_bundle_3))
        assert r.args[0].startswith(
            "exception: (<Err.ASSERT_BLOCK_INDEX_EXCEEDS_FAILED")

        # try to spend coin_2 with limit set to block 4. Should succeed
        solution_4 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_exceeds_block_2)
        spend_bundle_4 = build_spend_bundle(coin_2, solution_4)
        r = run(remote.push_tx(tx=spend_bundle_4))
        assert r["response"].startswith("accepted")

        # try to spend coin_3 with limit set to block 1. Should succeed
        solution_5 = p2_delegated_conditions.solution_for_conditions(
            puzzle_program, conditions_coin_exceeds_block_1)
        spend_bundle_5 = build_spend_bundle(coin_3, solution_5)
        r = run(remote.push_tx(tx=spend_bundle_5))
        assert r["response"].startswith("accepted")