async def test_generate_zero_val(self, two_wallet_nodes): num_blocks = 10 full_nodes, wallets = two_wallet_nodes full_node_1, server_1 = full_nodes[0] wallet_node, server_2 = wallets[0] wallet_node_2, server_3 = wallets[1] wallet = wallet_node.wallet_state_manager.main_wallet wallet2 = wallet_node_2.wallet_state_manager.main_wallet ph = await wallet.get_new_puzzlehash() await server_2.start_client(PeerInfo("localhost", uint16(server_1._port)), None) await server_3.start_client(PeerInfo("localhost", uint16(server_1._port)), None) for i in range(1, 4): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) funds = sum( [ calculate_base_fee(uint32(i)) + calculate_block_reward(uint32(i)) for i in range(1, 4 - 2) ] ) await time_out_assert(15, wallet.get_confirmed_balance, funds) cc_wallet: CCWallet = await CCWallet.create_new_cc( wallet_node.wallet_state_manager, wallet, uint64(100) ) ph = await wallet2.get_new_puzzlehash() for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await time_out_assert(15, cc_wallet.get_confirmed_balance, 100) await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100) assert cc_wallet.cc_info.my_core is not None colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core) cc_wallet_2: CCWallet = await CCWallet.create_wallet_for_cc( wallet_node_2.wallet_state_manager, wallet2, colour ) assert cc_wallet.cc_info.my_core == cc_wallet_2.cc_info.my_core await cc_wallet_2.generate_zero_val_coin() for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) unspent: List[WalletCoinRecord] = list( await cc_wallet_2.wallet_state_manager.get_spendable_coins_for_wallet( cc_wallet_2.wallet_info.id ) ) assert len(unspent) == 1 assert unspent.pop().coin.amount == 0
async def get_colour(self): colour = cc_wallet_puzzles.get_genesis_from_core(self.cc_info.my_core) return colour
async def test_cc_spend(self, two_wallet_nodes): num_blocks = 8 full_nodes, wallets = two_wallet_nodes full_node_1, server_1 = full_nodes[0] wallet_node, server_2 = wallets[0] wallet_node_2, server_3 = wallets[1] wallet = wallet_node.wallet_state_manager.main_wallet wallet2 = wallet_node_2.wallet_state_manager.main_wallet ph = await wallet.get_new_puzzlehash() await server_2.start_client(PeerInfo("localhost", uint16(server_1._port)), None) await server_3.start_client(PeerInfo("localhost", uint16(server_1._port)), None) for i in range(1, 4): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) funds = sum( [ calculate_base_fee(uint32(i)) + calculate_block_reward(uint32(i)) for i in range(1, 4 - 2) ] ) await time_out_assert(15, wallet.get_confirmed_balance, funds) cc_wallet: CCWallet = await CCWallet.create_new_cc( wallet_node.wallet_state_manager, wallet, uint64(100) ) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await time_out_assert(15, cc_wallet.get_confirmed_balance, 100) await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100) assert cc_wallet.cc_info.my_core is not None colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core) cc_wallet_2: CCWallet = await CCWallet.create_wallet_for_cc( wallet_node_2.wallet_state_manager, wallet2, colour ) assert cc_wallet.cc_info.my_core == cc_wallet_2.cc_info.my_core cc_2_hash = await cc_wallet_2.get_new_inner_hash() tx_record = await cc_wallet.generate_signed_transaction(uint64(60), cc_2_hash) await wallet.wallet_state_manager.add_pending_transaction(tx_record) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await time_out_assert(15, cc_wallet.get_confirmed_balance, 40) await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 40) await time_out_assert(30, cc_wallet_2.get_confirmed_balance, 60) await time_out_assert(30, cc_wallet_2.get_unconfirmed_balance, 60) cc_hash = await cc_wallet.get_new_inner_hash() tx_record = await cc_wallet_2.generate_signed_transaction(uint64(15), cc_hash) await wallet.wallet_state_manager.add_pending_transaction(tx_record) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await time_out_assert(15, cc_wallet.get_confirmed_balance, 55) await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 55)
async def test_create_offer_with_zero_val(self, two_wallet_nodes): num_blocks = 10 full_nodes, wallets = two_wallet_nodes full_node_1, server_1 = full_nodes[0] wallet_node, server_2 = wallets[0] wallet_node_2, server_3 = wallets[1] wallet = wallet_node.wallet_state_manager.main_wallet wallet2 = wallet_node_2.wallet_state_manager.main_wallet ph = await wallet.get_new_puzzlehash() ph2 = await wallet2.get_new_puzzlehash() await server_2.start_client( PeerInfo("localhost", uint16(server_1._port)), None) await server_3.start_client( PeerInfo("localhost", uint16(server_1._port)), None) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) funds = sum([ calculate_base_fee(uint32(i)) + calculate_block_reward(uint32(i)) for i in range(1, num_blocks - 2) ]) await self.time_out_assert(15, wallet.get_confirmed_balance, funds) cc_wallet: CCWallet = await CCWallet.create_new_cc( wallet_node.wallet_state_manager, wallet, uint64(100)) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100) await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100) assert cc_wallet.cc_info.my_core is not None colour = cc_wallet_puzzles.get_genesis_from_core( cc_wallet.cc_info.my_core) cc_wallet_2: CCWallet = await CCWallet.create_wallet_for_cc( wallet_node_2.wallet_state_manager, wallet2, colour) assert cc_wallet.cc_info.my_core == cc_wallet_2.cc_info.my_core await full_node_1.farm_new_block(FarmNewBlockProtocol(ph2)) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) trade_manager_1 = await TradeManager.create( wallet_node.wallet_state_manager) trade_manager_2 = await TradeManager.create( wallet_node_2.wallet_state_manager) file = "test_offer_file.offer" file_path = Path(file) if file_path.exists(): file_path.unlink() offer_dict = {1: -10, 2: 30} success, spend_bundle, error = await trade_manager_2.create_offer_for_ids( offer_dict) assert success is True assert spend_bundle is not None trade_manager_2.write_offer_to_disk(file_path, spend_bundle) success, offer, error = await trade_manager_1.get_discrepancies_for_offer( file_path) assert error is None assert success is True assert offer is not None assert offer["chia"] == 10 assert offer[colour] == -30 success, reason = await trade_manager_1.respond_to_offer(file_path) assert success is True for i in range(0, num_blocks): await full_node_1.farm_new_block( FarmNewBlockProtocol(token_bytes())) await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30) await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30)
async def test_cc_spend_uncoloured(self, two_wallet_nodes): num_blocks = 10 full_nodes, wallets = two_wallet_nodes full_node_1, server_1 = full_nodes[0] wallet_node, server_2 = wallets[0] wallet_node_2, server_3 = wallets[1] wallet = wallet_node.wallet_state_manager.main_wallet wallet2 = wallet_node_2.wallet_state_manager.main_wallet ph = await wallet.get_new_puzzlehash() await server_2.start_client( PeerInfo("localhost", uint16(server_1._port)), None) await server_3.start_client( PeerInfo("localhost", uint16(server_1._port)), None) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) funds = sum([ calculate_base_fee(uint32(i)) + calculate_block_reward(uint32(i)) for i in range(1, num_blocks - 2) ]) await self.time_out_assert(15, wallet.get_confirmed_balance, funds) cc_wallet: CCWallet = await CCWallet.create_new_cc( wallet_node.wallet_state_manager, wallet, uint64(100)) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 100) await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100) assert cc_wallet.cc_info.my_core is not None colour = cc_wallet_puzzles.get_genesis_from_core( cc_wallet.cc_info.my_core) cc_wallet_2: CCWallet = await CCWallet.create_wallet_for_cc( wallet_node_2.wallet_state_manager, wallet2, colour) assert cc_wallet.cc_info.my_core == cc_wallet_2.cc_info.my_core cc_2_hash = await cc_wallet_2.get_new_inner_hash() await cc_wallet.cc_spend(uint64(60), cc_2_hash) for i in range(1, num_blocks): await full_node_1.farm_new_block(FarmNewBlockProtocol(ph)) await self.time_out_assert(15, cc_wallet.get_confirmed_balance, 40) await self.time_out_assert(15, cc_wallet.get_unconfirmed_balance, 40) await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 60) await self.time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 60) cc2_ph = await cc_wallet_2.get_new_cc_puzzle_hash() spend_bundle = await wallet.wallet_state_manager.main_wallet.generate_signed_transaction( 10, cc2_ph, 0) await wallet.wallet_state_manager.main_wallet.push_transaction( spend_bundle) for i in range(0, num_blocks): await full_node_1.farm_new_block( FarmNewBlockProtocol(token_bytes())) id = cc_wallet_2.wallet_info.id wsm = cc_wallet_2.wallet_state_manager await self.time_out_assert(15, wsm.get_confirmed_balance_for_wallet, 70, id) await self.time_out_assert(15, cc_wallet_2.get_confirmed_balance, 60) await self.time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 60)
async def test_cc_trade(self, wallets_prefarm): wallet_node_0, wallet_node_1, full_node = wallets_prefarm wallet_0 = wallet_node_0.wallet_state_manager.main_wallet wallet_1 = wallet_node_1.wallet_state_manager.main_wallet cc_wallet: CCWallet = await CCWallet.create_new_cc( wallet_node_0.wallet_state_manager, wallet_0, uint64(100) ) for i in range(1, buffer_blocks): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) await time_out_assert(15, cc_wallet.get_confirmed_balance, 100) await time_out_assert(15, cc_wallet.get_unconfirmed_balance, 100) assert cc_wallet.cc_info.my_core is not None colour = cc_wallet_puzzles.get_genesis_from_core(cc_wallet.cc_info.my_core) cc_wallet_2: CCWallet = await CCWallet.create_wallet_for_cc( wallet_node_1.wallet_state_manager, wallet_1, colour ) assert cc_wallet.cc_info.my_core == cc_wallet_2.cc_info.my_core for i in range(0, buffer_blocks): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) trade_manager_0 = wallet_node_0.wallet_state_manager.trade_manager trade_manager_1 = wallet_node_1.wallet_state_manager.trade_manager file = "test_offer_file.offer" file_path = Path(file) if file_path.exists(): file_path.unlink() offer_dict = {1: 10, 2: -30} success, trade_offer, error = await trade_manager_0.create_offer_for_ids( offer_dict, file ) assert success is True assert trade_offer is not None success, offer, error = await trade_manager_1.get_discrepancies_for_offer( file_path ) assert error is None assert success is True assert offer is not None assert offer["chia"] == -10 assert offer[colour] == 30 success, trade, reason = await trade_manager_1.respond_to_offer(file_path) assert success is True for i in range(0, buffer_blocks): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) await time_out_assert(15, cc_wallet_2.get_confirmed_balance, 30) await time_out_assert(15, cc_wallet_2.get_unconfirmed_balance, 30) trade_2 = await trade_manager_0.get_trade_by_id(trade_offer.trade_id) assert TradeStatus(trade_2.status) is TradeStatus.CONFIRMED
async def test_cc_trade_with_multiple_colours(self, wallets_prefarm): # This test start with CCWallet in both wallets. wall # wallet1 {wallet_id: 2 = 70} # wallet2 {wallet_id: 2 = 30} wallet_node_a, wallet_node_b, full_node = wallets_prefarm wallet_a = wallet_node_a.wallet_state_manager.main_wallet wallet_b = wallet_node_b.wallet_state_manager.main_wallet # cc_a_2 = coloured coin, Alice, wallet id = 2 cc_a_2 = wallet_node_a.wallet_state_manager.wallets[2] cc_b_2 = wallet_node_b.wallet_state_manager.wallets[2] cc_a_3: CCWallet = await CCWallet.create_new_cc( wallet_node_a.wallet_state_manager, wallet_a, uint64(100) ) for i in range(0, buffer_blocks): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) await time_out_assert(15, cc_a_3.get_confirmed_balance, 100) await time_out_assert(15, cc_a_3.get_unconfirmed_balance, 100) assert cc_a_3.cc_info.my_core is not None red = cc_wallet_puzzles.get_genesis_from_core(cc_a_3.cc_info.my_core) for i in range(0, buffer_blocks): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) cc_b_3: CCWallet = await CCWallet.create_wallet_for_cc( wallet_node_b.wallet_state_manager, wallet_b, red ) assert cc_a_3.cc_info.my_core == cc_b_3.cc_info.my_core for i in range(0, buffer_blocks): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) trade_manager_0 = wallet_node_a.wallet_state_manager.trade_manager trade_manager_1 = wallet_node_b.wallet_state_manager.trade_manager file = "test_offer_file.offer" file_path = Path(file) if file_path.exists(): file_path.unlink() # Wallet offer_dict = {1: 1000, 2: -20, 3: -50} success, trade_offer, error = await trade_manager_0.create_offer_for_ids( offer_dict, file ) assert success is True assert trade_offer is not None success, offer, error = await trade_manager_1.get_discrepancies_for_offer( file_path ) assert error is None assert success is True assert offer is not None assert offer["chia"] == -1000 colour_2 = cc_a_2.get_colour() colour_3 = cc_a_3.get_colour() assert offer[colour_2] == 20 assert offer[colour_3] == 50 success, trade, reason = await trade_manager_1.respond_to_offer(file_path) assert success is True for i in range(0, 10): await full_node.farm_new_block(FarmNewBlockProtocol(token_bytes())) await time_out_assert(15, cc_b_3.get_confirmed_balance, 50) await time_out_assert(15, cc_b_3.get_unconfirmed_balance, 50) await time_out_assert(15, cc_a_3.get_confirmed_balance, 50) await time_out_assert(15, cc_a_3.get_unconfirmed_balance, 50) await time_out_assert(15, cc_a_2.get_unconfirmed_balance, 50) await time_out_assert(15, cc_b_2.get_unconfirmed_balance, 50) trade = await trade_manager_0.get_trade_by_id(trade_offer.trade_id) status: TradeStatus = TradeStatus(trade.status) assert status is TradeStatus.CONFIRMED