def test_standard_spend(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] commit_and_notify(remote, wallets, wallet_a) assert wallet_a.current_balance == 1000000000 assert len(wallet_a.my_utxos) == 2 assert wallet_b.current_balance == 0 assert len(wallet_b.my_utxos) == 0 # wallet a send to wallet b pubkey_puz_string = "(0x%s)" % hexlify( wallet_b.get_next_public_key().serialize()).decode('ascii') args = binutils.assemble(pubkey_puz_string) program = Program( clvm.eval_f( clvm.eval_f, binutils.assemble( wallet_a.generator_lookups[wallet_b.puzzle_generator_id]), args)) puzzlehash = ProgramHash(program) amount = 5000 spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) # give new wallet the reward to not complicate the one's we're tracking commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 999995000 assert wallet_b.current_balance == 5000 assert len(wallet_b.my_utxos) == 1 # wallet b sends back to wallet a pubkey_puz_string = "(0x%s)" % hexlify( wallet_a.get_next_public_key().serialize()).decode('ascii') args = binutils.assemble(pubkey_puz_string) program = Program( clvm.eval_f( clvm.eval_f, binutils.assemble( wallet_b.generator_lookups[wallet_a.puzzle_generator_id]), args)) puzzlehash = ProgramHash(program) amount = 5000 spend_bundle = wallet_b.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) # give new wallet the reward to not complicate the one's we're tracking commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000000000 assert wallet_b.current_balance == 0
def as_generate_secret_hash(self, secret): secret_hash_cl = "(sha256 (q %s))" % (secret) sec = "(%s)" % secret secret_hash_preformat = clvm.eval_f( clvm.eval_f, binutils.assemble("(sha256 (f (a)))"), binutils.assemble(sec)) secret_hash = binutils.disassemble(secret_hash_preformat) return secret_hash
def test_spend_failure(): remote = make_client_server() run = asyncio.get_event_loop().run_until_complete wallet_a = Wallet() wallet_b = Wallet() wallets = [wallet_a, wallet_b] amount = 5000 # wallet a send to wallet b pubkey_puz_string = "(0x%s)" % hexlify( wallet_b.get_next_public_key().serialize()).decode('ascii') args = binutils.assemble(pubkey_puz_string) program = Program( clvm.eval_f( clvm.eval_f, binutils.assemble( wallet_a.generator_lookups[wallet_b.puzzle_generator_id]), args)) puzzlehash = ProgramHash(program) spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None commit_and_notify(remote, wallets, wallet_a) amount = 50000000000000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None amount = 999995000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.temp_balance == 5000 amount = 6000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) assert spend_bundle is None amount = 4000 puzzlehash = wallet_b.get_new_puzzlehash() spend_bundle = wallet_a.generate_signed_transaction(amount, puzzlehash) _ = run(remote.push_tx(tx=spend_bundle)) assert wallet_a.temp_balance == 1000 commit_and_notify(remote, wallets, Wallet()) assert wallet_a.current_balance == 1000 assert wallet_a.temp_balance == 1000
def as_solution_list(self, body_program): try: sexp = clvm.eval_f(clvm.eval_f, body_program, []) except clvm.EvalError.EvalError: breakpoint() raise ConsensusError(Err.INVALID_BLOCK_SOLUTION, body_program) npc_list = [] for name_solution in sexp.as_iter(): _ = name_solution.as_python() if len(_) != 2: raise ConsensusError(Err.INVALID_COIN_SOLUTION, name_solution) if not isinstance(_[0], bytes) or len(_[0]) != 32: raise ConsensusError(Err.INVALID_COIN_SOLUTION, name_solution) if not isinstance(_[1], list) or len(_[1]) != 2: raise ConsensusError(Err.INVALID_COIN_SOLUTION, name_solution) puzzle_solution_program = name_solution.rest().first() puzzle_program = puzzle_solution_program.first() puzzle_hash = ProgramHash(Program(puzzle_program)) npc_list.append((puzzle_hash, puzzle_solution_program)) return npc_list
def puzzle_for_pk(self, pubkey): args = f"({pubkey_format(pubkey)})" puzzle = Program( clvm.eval_f(clvm.eval_f, binutils.assemble(self.puzzle_generator), binutils.assemble(args))) return puzzle