async def wallet_nodes(): async_gen = setup_simulators_and_wallets(1, 1, { "MEMPOOL_BLOCK_BUFFER": 1, "MAX_BLOCK_COST_CLVM": 11000000000 }) nodes, wallets = await async_gen.__anext__() full_node_1 = nodes[0] server_1 = full_node_1.full_node.server wallet_a = bt.get_pool_wallet_tool() wallet_receiver = WalletTool(full_node_1.full_node.constants) yield full_node_1, server_1, wallet_a, wallet_receiver async for _ in async_gen: yield _
from chia.consensus.blockchain import ReceiveBlockResult from chia.protocols import full_node_protocol from chia.types.announcement import Announcement from chia.types.condition_opcodes import ConditionOpcode from chia.types.condition_with_args import ConditionWithArgs from chia.types.spend_bundle import SpendBundle from chia.util.errors import ConsensusError, Err from chia.util.ints import uint64 from chia.util.wallet_tools import WalletTool from tests.core.full_node.test_full_node import connect_and_get_peer from tests.setup_nodes import bt, setup_two_nodes, test_constants BURN_PUZZLE_HASH = b"0" * 32 WALLET_A = WalletTool() WALLET_A_PUZZLE_HASHES = [WALLET_A.get_new_puzzlehash() for _ in range(5)] log = logging.getLogger(__name__) @pytest.fixture(scope="session") def event_loop(): loop = asyncio.get_event_loop() yield loop class TestBlockchainTransactions: @pytest.fixture(scope="function") async def two_nodes(self): async for _ in setup_two_nodes(test_constants):
from chia.util.db_wrapper import DBWrapper from chia.util.generator_tools import run_and_get_removals_and_additions from chia.util.ints import uint64 from chia.util.wallet_tools import WalletTool from tests.setup_nodes import bt, test_constants @pytest.fixture(scope="module") def event_loop(): loop = asyncio.get_event_loop() yield loop constants = test_constants WALLET_A = WalletTool(constants) def get_future_reward_coins(block: FullBlock) -> Tuple[Coin, Coin]: pool_amount = calculate_pool_reward(block.height) farmer_amount = calculate_base_farmer_reward(block.height) if block.is_transaction_block(): assert block.transactions_info is not None farmer_amount = uint64(farmer_amount + block.transactions_info.fees) pool_coin: Coin = create_pool_coin( block.height, block.foliage.foliage_block_data.pool_target.puzzle_hash, pool_amount, constants.GENESIS_CHALLENGE) farmer_coin: Coin = create_farmer_coin( block.height, block.foliage.foliage_block_data.farmer_reward_puzzle_hash, farmer_amount,
import pytest from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward from chia.consensus.blockchain import Blockchain, ReceiveBlockResult from chia.consensus.coinbase import create_farmer_coin, create_pool_coin from chia.full_node.block_store import BlockStore from chia.full_node.coin_store import CoinStore from chia.types.blockchain_format.coin import Coin from chia.types.coin_record import CoinRecord from chia.types.full_block import FullBlock from chia.util.db_wrapper import DBWrapper from chia.util.ints import uint64 from chia.util.wallet_tools import WalletTool from tests.setup_nodes import bt, test_constants WALLET_A = WalletTool() @pytest.fixture(scope="module") def event_loop(): loop = asyncio.get_event_loop() yield loop constants = test_constants def get_future_reward_coins(block: FullBlock) -> Tuple[Coin, Coin]: pool_amount = calculate_pool_reward(block.height) farmer_amount = calculate_base_farmer_reward(block.height) if block.is_transaction_block():
print(f"cost of one_substraction is: {one_substraction}") print(f"cost of one_multiply is: {one_multiply}") print(f"cost of one_greater is: {one_greater}") print(f"cost of one_equal is: {one_equal}") print(f"cost of one_if is: {one_if}") print(f"cost of one_sha256 is: {one_sha256}") print(f"cost of one_pubkey_for_exp is: {one_pubkey_for_exp}") print(f"cost of one_point_add is: {one_point_add}") if __name__ == "__main__": """ Naive way to calculate cost ratio between vByte and CLVM cost unit. AggSig has assigned cost of 20vBytes, simple CLVM program is benchmarked against it. """ wallet_tool = WalletTool() benchmark_all_operators() secret_key: PrivateKey = AugSchemeMPL.key_gen(bytes([2] * 32)) puzzles = [] solutions = [] private_keys = [] public_keys = [] for i in range(0, 1000): private_key: PrivateKey = master_sk_to_wallet_sk(secret_key, uint32(i)) public_key = private_key.public_key() solution = wallet_tool.make_solution( {ConditionOpcode.ASSERT_MY_COIN_ID: [ConditionWithArgs(ConditionOpcode.ASSERT_MY_COIN_ID, [token_bytes()])]} ) puzzle = puzzle_for_pk(bytes(public_key)) puzzles.append(puzzle)
async def test1(self, two_nodes): num_blocks = 5 test_rpc_port = uint16(21522) nodes, _ = two_nodes full_node_api_1, full_node_api_2 = nodes server_1 = full_node_api_1.full_node.server server_2 = full_node_api_2.full_node.server def stop_node_cb(): full_node_api_1._close() server_1.close_all() full_node_rpc_api = FullNodeRpcApi(full_node_api_1.full_node) config = bt.config hostname = config["self_hostname"] daemon_port = config["daemon_port"] rpc_cleanup = await start_rpc_server( full_node_rpc_api, hostname, daemon_port, test_rpc_port, stop_node_cb, bt.root_path, config, connect_to_daemon=False, ) try: client = await FullNodeRpcClient.create(self_hostname, test_rpc_port, bt.root_path, config) state = await client.get_blockchain_state() assert state["peak"] is None assert not state["sync"]["sync_mode"] assert state["difficulty"] > 0 assert state["sub_slot_iters"] > 0 blocks = bt.get_consecutive_blocks(num_blocks) blocks = bt.get_consecutive_blocks(num_blocks, block_list_input=blocks, guarantee_transaction_block=True) assert len(await client.get_unfinished_block_headers()) == 0 assert len((await client.get_block_records(0, 100))) == 0 for block in blocks: if is_overflow_block(test_constants, block.reward_chain_block.signage_point_index): finished_ss = block.finished_sub_slots[:-1] else: finished_ss = block.finished_sub_slots unf = UnfinishedBlock( finished_ss, block.reward_chain_block.get_unfinished(), block.challenge_chain_sp_proof, block.reward_chain_sp_proof, block.foliage, block.foliage_transaction_block, block.transactions_info, block.transactions_generator, [], ) await full_node_api_1.full_node.respond_unfinished_block( full_node_protocol.RespondUnfinishedBlock(unf), None ) await full_node_api_1.full_node.respond_block(full_node_protocol.RespondBlock(block), None) assert len(await client.get_unfinished_block_headers()) > 0 assert len(await client.get_all_block(0, 2)) == 2 state = await client.get_blockchain_state() block = await client.get_block(state["peak"].header_hash) assert block == blocks[-1] assert (await client.get_block(bytes([1] * 32))) is None assert (await client.get_block_record_by_height(2)).header_hash == blocks[2].header_hash assert len((await client.get_block_records(0, 100))) == num_blocks * 2 assert (await client.get_block_record_by_height(100)) is None ph = list(blocks[-1].get_included_reward_coins())[0].puzzle_hash coins = await client.get_coin_records_by_puzzle_hash(ph) print(coins) assert len(coins) >= 1 additions, removals = await client.get_additions_and_removals(blocks[-1].header_hash) assert len(additions) >= 2 and len(removals) == 0 wallet = WalletTool(full_node_api_1.full_node.constants) wallet_receiver = WalletTool(full_node_api_1.full_node.constants, AugSchemeMPL.key_gen(std_hash(b"123123"))) ph = wallet.get_new_puzzlehash() ph_2 = wallet.get_new_puzzlehash() ph_receiver = wallet_receiver.get_new_puzzlehash() assert len(await client.get_coin_records_by_puzzle_hash(ph)) == 0 assert len(await client.get_coin_records_by_puzzle_hash(ph_receiver)) == 0 blocks = bt.get_consecutive_blocks( 2, block_list_input=blocks, guarantee_transaction_block=True, farmer_reward_puzzle_hash=ph, pool_reward_puzzle_hash=ph, ) for block in blocks[-2:]: await full_node_api_1.full_node.respond_block(full_node_protocol.RespondBlock(block)) assert len(await client.get_coin_records_by_puzzle_hash(ph)) == 2 assert len(await client.get_coin_records_by_puzzle_hash(ph_receiver)) == 0 coin_to_spend = list(blocks[-1].get_included_reward_coins())[0] spend_bundle = wallet.generate_signed_transaction(coin_to_spend.amount, ph_receiver, coin_to_spend) assert len(await client.get_all_mempool_items()) == 0 assert len(await client.get_all_mempool_tx_ids()) == 0 assert (await client.get_mempool_item_by_tx_id(spend_bundle.name())) is None await client.push_tx(spend_bundle) assert len(await client.get_all_mempool_items()) == 1 assert len(await client.get_all_mempool_tx_ids()) == 1 assert ( SpendBundle.from_json_dict(list((await client.get_all_mempool_items()).values())[0]["spend_bundle"]) == spend_bundle ) assert (await client.get_all_mempool_tx_ids())[0] == spend_bundle.name() assert ( SpendBundle.from_json_dict( (await client.get_mempool_item_by_tx_id(spend_bundle.name()))["spend_bundle"] ) == spend_bundle ) await full_node_api_1.farm_new_transaction_block(FarmNewBlockProtocol(ph_2)) assert len(await client.get_coin_records_by_puzzle_hash(ph_receiver)) == 1 assert len(list(filter(lambda cr: not cr.spent, (await client.get_coin_records_by_puzzle_hash(ph))))) == 3 assert len(await client.get_coin_records_by_puzzle_hash(ph, False)) == 3 assert len(await client.get_coin_records_by_puzzle_hash(ph, True)) == 4 assert len(await client.get_coin_records_by_puzzle_hash(ph, True, 0, 100)) == 4 assert len(await client.get_coin_records_by_puzzle_hash(ph, True, 50, 100)) == 0 assert len(await client.get_coin_records_by_puzzle_hash(ph, True, 0, blocks[-1].height + 1)) == 2 assert len(await client.get_coin_records_by_puzzle_hash(ph, True, 0, 1)) == 0 assert len(await client.get_connections()) == 0 await client.open_connection(self_hostname, server_2._port) async def num_connections(): return len(await client.get_connections()) await time_out_assert(10, num_connections, 1) connections = await client.get_connections() await client.close_connection(connections[0]["node_id"]) await time_out_assert(10, num_connections, 0) finally: # Checks that the RPC manages to stop the node client.close() await client.await_closed() await rpc_cleanup()