def test_p2_delegated_puzzle_graftroot(self): payments, conditions = default_payments_and_conditions() delegated_puzzle = p2_delegated_conditions.puzzle_for_pk( public_key_bytes_for_index(8)) delegated_solution = p2_delegated_conditions.solution_for_conditions( delegated_puzzle, conditions) puzzle_program = p2_delegated_puzzle.puzzle_for_pk( public_key_bytes_for_index(1)) puzzle_hash = ProgramHash(puzzle_program) solution = p2_delegated_puzzle.solution_for_delegated_puzzle( puzzle_program, delegated_solution) run_test(puzzle_hash, solution, payments)
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)
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")
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")
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")
def run_test_p2_delegated_puzzle_or_hidden_puzzle_with_delegated_puzzle( self, hidden_pub_key_index): payments, conditions = default_payments_and_conditions() hidden_puzzle = p2_conditions.puzzle_for_conditions(conditions) hidden_public_key = public_key_bytes_for_index(hidden_pub_key_index) puzzle = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_public_key_and_hidden_puzzle( hidden_public_key, hidden_puzzle) puzzle_hash = ProgramHash(puzzle) payable_payments, payable_conditions = default_payments_and_conditions( 5) delegated_puzzle = p2_conditions.puzzle_for_conditions( payable_conditions) delegated_solution = [] synthetic_public_key = p2_delegated_puzzle_or_hidden_puzzle.calculate_synthetic_public_key( hidden_public_key, hidden_puzzle) solution = p2_delegated_puzzle_or_hidden_puzzle.solution_with_delegated_puzzle( synthetic_public_key, delegated_puzzle, delegated_solution) hidden_puzzle_hash = ProgramHash(hidden_puzzle) synthetic_offset = p2_delegated_puzzle_or_hidden_puzzle.calculate_synthetic_offset( hidden_public_key, hidden_puzzle_hash) private_key = bls_private_key_for_index(hidden_pub_key_index) assert private_key.public_key() == hidden_public_key secret_exponent = private_key.secret_exponent() synthetic_secret_exponent = secret_exponent + synthetic_offset DEFAULT_KEYCHAIN.add_secret_exponents([synthetic_secret_exponent]) run_test(puzzle_hash, solution, payable_payments)
def do_multisig(selectors, fuzz_coin=None, fuzz_signature=None): payments = [ (puzzle_hash_for_index(0), 1000), (puzzle_hash_for_index(1), 2000), ] conditions = conditions_for_payment(payments) pks = [public_key_bytes_for_index(_) for _ in range(1, 4)] M = 2 puzzle_program = p2_m_of_n_delegate_direct.puzzle_for_m_of_public_key_list( M, pks) puzzle_hash = ProgramHash(puzzle_program) def solution_maker(coin, remote): print(coin.name()) if fuzz_coin: coin = fuzz_coin(remote, puzzle_hash) print(coin.name()) id_condition = make_assert_my_coin_id_condition(coin.name()) delegated_puzzle = p2_conditions.puzzle_for_conditions( conditions + [id_condition]) delegated_solution = p2_delegated_conditions.solution_for_conditions( delegated_puzzle, []) solution = p2_m_of_n_delegate_direct.solution_for_delegated_puzzle( M, pks, selectors, delegated_puzzle, delegated_solution) return solution run_test(puzzle_hash, payments, solution_maker, fuzz_signature)
def test_p2_delegated_puzzle_simple(self): payments, conditions = default_payments_and_conditions() pk = public_key_bytes_for_index(1) puzzle_program = p2_delegated_puzzle.puzzle_for_pk(pk) puzzle_hash = ProgramHash(puzzle_program) solution = p2_delegated_puzzle.solution_for_conditions( puzzle_program, conditions) run_test(puzzle_hash, solution, payments)
def test_p2_delegated_puzzle_or_hidden_puzzle_with_hidden_puzzle(self): payments, conditions = default_payments_and_conditions() hidden_puzzle = p2_conditions.puzzle_for_conditions(conditions) hidden_public_key = public_key_bytes_for_index(10) puzzle = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_public_key_and_hidden_puzzle( hidden_public_key, hidden_puzzle) puzzle_hash = ProgramHash(puzzle) solution = p2_delegated_puzzle_or_hidden_puzzle.solution_with_hidden_puzzle( hidden_public_key, hidden_puzzle, []) run_test(puzzle_hash, solution, payments)
def test_p2_puzzle_hash(self): payments, conditions = default_payments_and_conditions() underlying_puzzle = p2_delegated_conditions.puzzle_for_pk( public_key_bytes_for_index(4)) underlying_solution = p2_delegated_conditions.solution_for_conditions( underlying_puzzle, conditions) underlying_puzzle_hash = ProgramHash(underlying_puzzle) puzzle_program = p2_puzzle_hash.puzzle_for_puzzle_hash( underlying_puzzle_hash) puzzle_hash = ProgramHash(puzzle_program) solution = p2_puzzle_hash.solution_for_puzzle_and_solution( underlying_puzzle, underlying_solution) run_test(puzzle_hash, solution, payments)
def test_p2_m_of_n_delegated_puzzle(self): payments, conditions = default_payments_and_conditions() pks = [public_key_bytes_for_index(_) for _ in range(1, 6)] M = 3 delegated_puzzle = p2_conditions.puzzle_for_conditions(conditions) delegated_solution = [] puzzle_program = p2_m_of_n_delegate_direct.puzzle_for_m_of_public_key_list( M, pks) selectors = [1, [], [], 1, 1] solution = p2_m_of_n_delegate_direct.solution_for_delegated_puzzle( M, pks, selectors, delegated_puzzle, delegated_solution) puzzle_hash = ProgramHash(puzzle_program) run_test(puzzle_hash, solution, payments)
def do_pubkey(fuzz_coin=None): payments = [ (puzzle_hash_for_index(0), 1000), (puzzle_hash_for_index(1), 2000), ] conditions = conditions_for_payment(payments) pk = public_key_bytes_for_index(1) puzzle_program = p2_delegated_conditions.puzzle_for_pk(pk) puzzle_hash = ProgramHash(puzzle_program) def solution_maker(coin, remote): if fuzz_coin: coin = fuzz_coin(remote, puzzle_hash) id_condition = make_assert_my_coin_id_condition(coin.name()) return p2_delegated_conditions.solution_for_conditions( puzzle_program, conditions + [id_condition]) run_test(puzzle_hash, payments, solution_maker)
def farm_block(previous_header, previous_signature, block_number, proof_of_space, spend_bundle, coinbase_puzzle_hash, reward): fees_puzzle_hash = puzzle_hash_for_index(3) coinbase_coin, coinbase_signature = create_coinbase_coin_and_signature( block_number, coinbase_puzzle_hash, reward, proof_of_space.pool_public_key) timestamp = int(1e10) + 300 * block_number header, body = farm_new_block(previous_header, previous_signature, block_number, proof_of_space, spend_bundle, coinbase_coin, coinbase_signature, fees_puzzle_hash, timestamp) header_signature = sign_header(header, proof_of_space.plot_public_key) bad_bls_public_key = BLSPublicKey.from_bytes(public_key_bytes_for_index(9)) bad_eor_public_key = EORPrivateKey(std_hash(bytes([5]))).public_key() hkp = header_signature.aggsig_pair(proof_of_space.plot_public_key, HeaderHash(header)) _ = header_signature.validate([hkp]) assert _ hkp = header_signature.aggsig_pair(bad_eor_public_key, HeaderHash(header)) assert not header_signature.validate([hkp]) hkp = body.coinbase_signature.aggsig_pair(proof_of_space.pool_public_key, body.coinbase_coin.name()) assert body.coinbase_signature.validate([hkp]) hkp = body.coinbase_signature.aggsig_pair(bad_bls_public_key, body.coinbase_coin.name()) assert not body.coinbase_signature.validate([hkp]) return header, header_signature, body
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")