def run_bad_nodes(env): script_path = os.path.realpath( os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "test", "integration_test", "tester", "start_bad_network.sh")) process = subprocess.Popen([f"{script_path}"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) with process.stdout: log_subprocess_output(process.stdout, env) exitcode = process.wait() if exitcode != 0: env.logger.debug(f"Script exit code = {exitcode}") return 1 bad_client_pool = [] bad_node_ids = [] bad_node_ids.append( Id(20203, grpc_port=50051, absolute_address="192.168.100.141")) bad_node_ids.append( Id(20203, grpc_port=50051, absolute_address="192.168.100.142")) bad_node_ids.append( Id(20203, grpc_port=50051, absolute_address="192.168.100.143")) env.logger.info("Get client from bad network pool:") for id in bad_node_ids: bad_client_pool.append(env.get_grpc_client_to_outside_node(id)) return bad_client_pool, bad_node_ids
def main(env: Env) -> int: node_id = Id(20106, http_port=50106) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.PYTHON_HTTP, node_id) info = client.node_info() TEST_CHECK_EQUAL(info.top_block_number, 0) return 0
def main(env: Env) -> int: node_id = Id(20104, grpc_port=50104) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) info = client.node_info() TEST_CHECK_EQUAL(info.top_block_number, 0) return 0
def main(env: Env) -> int: amount = 100 node = Id(20101, grpc_port=50101) env.logger.info("Start node") env.start_node(NodeConfig(node, nodes=[node])) client = env.get_client(ClientType.LEGACY_GRPC, node) env.logger.info("Test node") TEST_CHECK(client.connection_test()) distributor_address = client.load_address( keys_path=get_distributor_address_path()) test_address = client.generate_keys(keys_path="test_keys") env.logger.info("Start transaction") transaction = client.transfer(to_address=test_address.address, amount=amount, from_address=distributor_address, fee=0, wait=1, timeout=2) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(client.transaction_success_wait(transaction=transaction)) env.logger.info("Check balance") TEST_CHECK_EQUAL( client.get_balance(address=test_address.address, timeout=2, wait=1), amount) return 0
def main(env: Env) -> int: sync_port = 20100 grpc_port = 50100 amount = randrange(1000) update_time = 0.5 timeout = 2 wait_time = 1 transaction_update_time = 2 max_update_request = 10 env.logger.debug(f"Random amount for test = {amount}") bad_client_pool, bad_node_ids = run_bad_nodes(env) main_id = Id(sync_port, grpc_port=grpc_port) env.logger.info("Start main node with connecting to bad network nodes:") # connect to only first node form bad pool, becouse it's IP from good network. # If connect to this ids, nodes in bad pool synchron across 2 network card env.start_node(NodeConfig(main_id, nodes=[ bad_node_ids[0], ])) main_client = env.get_client(ClientType.LEGACY_GRPC, main_id) env.logger.info("Check all nodes:") TEST_CHECK(main_client.connection_test()) for client in bad_client_pool: TEST_CHECK(client.connection_test()) env.logger.info("All nodes started success.") address = main_client.generate_keys(keys_path=f"keys") distributor_address = main_client.load_address( keys_path=get_distributor_address_path()) TEST_CHECK_EQUAL( main_client.get_balance(address=address.address, timeout=timeout, wait=wait_time), 0) env.logger.info("New address created.") transaction = main_client.transfer(to_address=address.address, amount=amount, from_address=distributor_address, fee=0, wait=wait_time, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(main_client.transaction_success_wait(transaction=transaction)) TEST_CHECK_EQUAL( main_client.get_balance(address=address.address, timeout=timeout, wait=wait_time), amount) env.logger.info("Main client transaction checked success.") for client in bad_client_pool: TEST_CHECK_EQUAL( client.get_balance(address=address.address, timeout=timeout, wait=wait_time), amount) env.logger.info("Test ended success.") return 0
def main(env: Env) -> int: amount = 100 node_1_id = Id(20101, grpc_port=50101) node_2_id = Id(20102, grpc_port=50201) node_3_id = Id(20103, grpc_port=50301) env.start_node(NodeConfig(node_1_id)) env.start_node(NodeConfig(node_2_id, nodes=[node_1_id])) env.start_node(NodeConfig(node_3_id, nodes=[node_1_id])) client_1_grpc = env.get_client(ClientType.LEGACY_GRPC, node_1_id) client_2_grpc = env.get_client(ClientType.LEGACY_GRPC, node_2_id) client_3_grpc = env.get_client(ClientType.LEGACY_GRPC, node_3_id) TEST_CHECK(client_1_grpc.connection_test()) TEST_CHECK(client_2_grpc.connection_test()) TEST_CHECK(client_3_grpc.connection_test()) distributor_address = client_1_grpc.load_address( keys_path=get_distributor_address_path()) test_address = client_1_grpc.generate_keys(keys_path="test_keys") transaction = client_1_grpc.transfer(to_address=test_address.address, amount=amount, from_address=distributor_address, fee=0, wait=1, timeout=2) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) stat = client_1_grpc.get_transaction_status(tx_hash=transaction.tx_hash) TEST_CHECK_EQUAL(stat.status_code, TransactionStatusCode.SUCCESS) TEST_CHECK_EQUAL( client_1_grpc.get_balance(address=test_address.address, timeout=2, wait=1), amount) TEST_CHECK_EQUAL( client_2_grpc.get_balance(address=test_address.address, timeout=2, wait=1), amount) TEST_CHECK_EQUAL( client_3_grpc.get_balance(address=test_address.address, timeout=2, wait=1), amount) return 0
def main(env: Env) -> int: sync_port = 20100 grpc_port = 50100 amount = randrange(1000) update_time = 0.5 timeout = 2 wait_time = 1 transaction_update_time=2 max_update_request=10 env.logger.debug(f"Random amount for test = {amount}") id = Id(sync_port, grpc_port = grpc_port) env.start_node(NodeConfig(id)) client = env.get_client(ClientType.LEGACY_GRPC, id) TEST_CHECK(client.connection_test()) env.logger.info("Node started success.") address = client.generate_keys(keys_path=f"keys") distributor_address = client.load_address(keys_path=get_distributor_address_path()) TEST_CHECK_EQUAL(client.get_balance(address=address.address, timeout=timeout, wait=wait_time), 0) env.logger.info("New address created.") transaction = client.transfer(to_address=address.address, amount=amount, from_address=distributor_address, fee=0, wait=wait_time, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(client.transaction_success_wait(transaction=transaction)) TEST_CHECK_EQUAL(client.get_balance(address=address.address, timeout=timeout, wait=wait_time), amount) env.logger.info("Initialaze transaction success.") transaction = client.transfer(to_address=address.address, amount=amount, from_address=address, fee=0, wait=wait_time, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(client.transaction_success_wait(transaction=transaction)) TEST_CHECK_EQUAL(client.get_balance(address=address.address, timeout=timeout, wait=wait_time), amount) env.logger.info("Transaction to myself success.") return 0
def main(env: Env) -> int: amount = 100 sync_time = 1 n = 3 node_ids = [] clients = [] for i in range(0, n): node_ids.append(Id(20100 + i, grpc_port=50100 + i)) for i in range(0, n): env.logger.info(f"Start node {i}") env.start_node(NodeConfig(node_ids[i], nodes=node_ids)) clients.append(env.get_client(ClientType.LEGACY_GRPC, node_ids[i])) TEST_CHECK(clients[i].connection_test()) env.logger.info("All nodes started") distributor_address = clients[0].load_address( keys_path=get_distributor_address_path()) test_address = clients[0].generate_keys(keys_path="test_keys") env.logger.info("Start transaction") transaction = clients[0].transfer(to_address=test_address.address, amount=amount, from_address=distributor_address, fee=0, wait=1, timeout=2) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(clients[0].transaction_success_wait(transaction=transaction)) env.logger.info("Transaction success") sleep(sync_time) for i in range(0, n): env.logger.info(f"Check balance (node {i})") TEST_CHECK_EQUAL( clients[i].get_balance(address=test_address.address, timeout=2, wait=1), amount) return 0
def main(env: Env) -> int: amount = 100 wait = 1 timeout = 2 sync_time = 1 node_1_id = Id(20101, grpc_port=50101, http_port=50102) node_2_id = Id(20102, grpc_port=50201, http_port=50202) env.start_node(NodeConfig(node_1_id)) env.start_node(NodeConfig(node_2_id, nodes=[node_1_id])) client_1_grpc = env.get_client(ClientType.LEGACY_GRPC, node_1_id) client_1_http = env.get_client(ClientType.LEGACY_HTTP, node_1_id) client_2_grpc = env.get_client(ClientType.LEGACY_GRPC, node_2_id) client_2_http = env.get_client(ClientType.LEGACY_HTTP, node_2_id) TEST_CHECK(client_1_grpc.connection_test()) TEST_CHECK(client_2_grpc.connection_test()) TEST_CHECK(client_1_http.connection_test()) TEST_CHECK(client_2_http.connection_test()) env.logger.info(f"All clients checked") distributor_address = client_1_grpc.load_address( keys_path=get_distributor_address_path()) test_address = client_1_grpc.generate_keys(keys_path="test_keys") env.logger.info(f"Start transaction 1") transaction = client_1_grpc.transfer(to_address=test_address.address, amount=amount, from_address=distributor_address, fee=0, wait=wait, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(client_1_grpc.transaction_success_wait(transaction=transaction)) env.logger.info(f"Start transaction 2") transaction = client_2_http.transfer(to_address=test_address.address, amount=amount, from_address=distributor_address, fee=0, wait=wait, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(client_2_http.transaction_success_wait(transaction=transaction)) sleep(sync_time) env.logger.info(f"Check balance on client 1") TEST_CHECK_EQUAL( client_1_grpc.get_balance(address=test_address.address, timeout=timeout, wait=wait), 2 * amount) TEST_CHECK_EQUAL( client_1_http.get_balance(address=test_address.address, timeout=timeout, wait=wait), 2 * amount) env.logger.info(f"Check balance on client 2") TEST_CHECK_EQUAL( client_2_grpc.get_balance(address=test_address.address, timeout=timeout, wait=wait), 2 * amount) TEST_CHECK_EQUAL( client_2_http.get_balance(address=test_address.address, timeout=timeout, wait=wait), 2 * amount) return 0
def main(env: Env) -> int: start_sync_port = 20230 start_rpc_port = 50080 waiting_time = 20 count_threads = 5 count_nodes_per_thread = 5 amount = 1000 transaction_wait = 20 transaction_timeout = 40 node_ids = list() pool = list() for i in range(count_threads * count_nodes_per_thread + 1): node_ids.append(Id(start_sync_port + i, grpc_port=start_rpc_port + i)) # start first node env.start_node(NodeConfig(node_ids[0])) first_node = env.get_client(ClientType.LEGACY_GRPC, node_ids[0]) pool.append(first_node) TEST_CHECK(first_node.connection_test()) # parallel initialize nodes with concurrent.futures.ThreadPoolExecutor(count_threads) as executor: threads = [] for i in range(count_threads): threads.append( executor.submit( init_nodes, env, node_ids[(count_nodes_per_thread * i) + 1:(count_nodes_per_thread * (i + 1)) + 1], node_ids[0])) # test for node initialization for node in pool: TEST_CHECK(node.connection_test()) addresses = [ first_node.generate_keys(keys_path=f"keys{i}") for i in range(1, len(pool)) ] distributor_address = first_node.load_address( keys_path=get_distributor_address_path()) for to_address, node in zip(addresses, pool): for other_node in pool: TEST_CHECK_EQUAL( other_node.get_balance(address=to_address.address, timeout=2, wait=1), 0) transaction = node.transfer(to_address=to_address.address, amount=amount, from_address=distributor_address, fee=0, wait=transaction_wait, timeout=transaction_timeout) for other_node in pool: TEST_CHECK_EQUAL( other_node.get_balance(address=to_address.address, timeout=2, wait=1), amount) return 0
def main(env: Env) -> int: count_nodes = 10 start_sync_port = 20310 start_rpc_port = 50160 waiting_time = 5 transaction_timeout = 7 transaction_wait = 4 pool = [] node_ids = [] id = Id(start_sync_port, grpc_port = start_rpc_port) env.start_node(NodeConfig(id)) node_ids.append(id) pool.append(env.get_client(ClientType.LEGACY_GRPC, id)) TEST_CHECK(pool[0].connection_test()) # initializing connections with nodes for i in range(1, count_nodes): current_sync_port = start_sync_port + i current_rpc_port = start_rpc_port + i last_id = id id = Id(current_sync_port, grpc_port = current_rpc_port) env.start_node(NodeConfig(id, nodes = [last_id, ])) node_ids.append(id) pool.append(env.get_client(ClientType.LEGACY_GRPC, id)) for node in pool: TEST_CHECK(node.connection_test()) addresses = [pool[-1].generate_keys(keys_path=f"keys{i}") for i in range(1, len(pool))] init_amount = 1000 distributor_address = pool[-1].load_address(keys_path=get_distributor_address_path()) # init addresses with amount for to_address in addresses: TEST_CHECK_EQUAL(pool[-1].get_balance(address=to_address.address, timeout=2, wait=1), 0) transaction = pool[-1].transfer(to_address=to_address.address, amount=init_amount, from_address=distributor_address, fee=0, wait=transaction_wait, timeout=transaction_timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) stat = pool[-1].get_transaction_status(tx_hash=transaction.tx_hash) TEST_CHECK_EQUAL(stat.status_code, TransactionStatusCode.SUCCESS) for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=to_address.address, timeout=2, wait=1), init_amount) for i in range(1, len(addresses) - 1): from_address = addresses[i] to_address = addresses[i + 1] amount = i * 100 transaction = pool[-1].transfer(to_address=to_address.address, amount=amount, from_address=from_address, fee=0, wait=transaction_wait, timeout=transaction_timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) stat = pool[-1].get_transaction_status(tx_hash=transaction.tx_hash) TEST_CHECK_EQUAL(stat.status_code, TransactionStatusCode.SUCCESS) for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=to_address.address, timeout=2, wait=1), amount + init_amount) first_address = addresses[0] first_address_balance = init_amount for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=first_address.address, timeout=2, wait=1), first_address_balance) return 0
def main(env: Env) -> int: init_amount = 1000 count_nodes = 10 start_sync_port = 20302 start_rpc_port = 50152 waiting_time = 0.5 timeout = 2 transaction_wait = 1 transaction_update_time = 1 max_update_request = 10 sync_time = 3 pool = [] node_ids = [] id = Id(start_sync_port, grpc_port = start_rpc_port) env.start_node(NodeConfig(id)) node_ids.append(id) pool.append(env.get_client(ClientType.LEGACY_GRPC, Id(start_sync_port, grpc_port = start_rpc_port))) TEST_CHECK(pool[0].connection_test()) # initializing connections with nodes for i in range(1, count_nodes): current_sync_port = start_sync_port + i current_rpc_port = start_rpc_port + i id = Id(current_sync_port, grpc_port = current_rpc_port) env.start_node(NodeConfig(id, nodes = node_ids)) pool.append(env.get_client(ClientType.LEGACY_GRPC, id)) node_ids.append(id) for node in pool: TEST_CHECK(node.connection_test()) addresses = [pool[-1].generate_keys(keys_path=f"keys{i}") for i in range(1, len(pool))] distributor_address = pool[-1].load_address(keys_path=get_distributor_address_path()) init_transactions = [] # init addresses with amount for to_address in addresses: TEST_CHECK_EQUAL(pool[-1].get_balance(address=to_address.address, timeout=timeout, wait=waiting_time), 0) transaction = pool[-1].transfer(to_address=to_address.address, amount=init_amount, from_address=distributor_address, fee=0, wait=transaction_wait, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) init_transactions.append(transaction) for transaction in init_transactions: TEST_CHECK(pool[-1].transaction_success_wait(transaction=transaction)) env.logger.info("Init transactions success. Wait synchronization") sleep(sync_time) for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=to_address.address, timeout=timeout, wait=waiting_time), init_amount) env.logger.info("Init check balance success.") for i in range(1, len(addresses) - 1): from_address = addresses[i] to_address = addresses[i + 1] amount = i * 100 transaction = pool[-1].transfer(to_address=to_address.address, amount=amount, from_address=from_address, fee=0, wait=transaction_wait, timeout=timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) TEST_CHECK(pool[-1].transaction_success_wait(transaction=transaction)) env.logger.info(f"Transaction success, check balance {to_address.address}") for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=to_address.address, timeout=timeout, wait=waiting_time), amount + init_amount) first_address = addresses[0] first_address_balance = init_amount env.logger.info(f"Check balance of first address {first_address.address}") for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=first_address.address, timeout=timeout, wait=waiting_time), first_address_balance) return 0
def main(env: Env) -> int: count_nodes = 5 start_sync_port = 20330 start_rpc_port = 50180 node_startup_time = 5 transaction_wait = 2 transaction_timeout = 10 init_amount = 1000 address_per_nodes = 3 pool = [] node_ids = [] id = Id(start_sync_port, grpc_port = start_rpc_port) env.start_node(NodeConfig(id)) node_ids.append(id) pool.append(env.get_client(ClientType.LEGACY_GRPC, id)) TEST_CHECK(pool[0].connection_test()) env.logger.info(f"First node started success") # initializing connections with nodes for i in range(1, count_nodes): current_sync_port = start_sync_port + i current_rpc_port = start_rpc_port + i id = Id(current_sync_port, grpc_port = current_rpc_port) env.start_node(NodeConfig(id, nodes = node_ids)) node_ids.append(id) pool.append(env.get_client(ClientType.LEGACY_GRPC, id)) for node in pool: TEST_CHECK(node.connection_test()) env.logger.info(f"All nodes in pool checked") addresses = [pool[-1].generate_keys(keys_path=f"keys{i}") for i in range(1, count_nodes * address_per_nodes + 1)] distributor_address = pool[-1].load_address(keys_path=get_distributor_address_path()) # init addresses with amount for to_address in addresses: TEST_CHECK_EQUAL(pool[-1].get_balance(address=to_address.address, timeout=2, wait=1), 0) transaction = pool[-1].transfer(to_address=to_address.address, amount=init_amount, from_address=distributor_address, fee=0, wait=transaction_wait, timeout=transaction_timeout) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) stat = pool[-1].get_transaction_status(tx_hash=transaction.tx_hash) TEST_CHECK_EQUAL(stat.status_code, TransactionStatusCode.SUCCESS) for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=to_address.address, timeout=2, wait=1), init_amount) env.logger.info(f"Init balance checked") with concurrent.futures.ThreadPoolExecutor(len(pool)) as executor: threads = [] for i in range(len(pool)): first_address_number = i * address_per_nodes last_address_number = (i * address_per_nodes) + address_per_nodes threads.append( executor.submit(node_transfers, pool[i], addresses[first_address_number:last_address_number], transaction_wait, env)) for i in threads: i.result() env.logger.info(f"All thread finished") for address in addresses: for node in pool: TEST_CHECK_EQUAL(node.get_balance(address=address.address, timeout=2, wait=1), init_amount) return 0
def main(env: Env) -> int: contract_text = ''' pragma solidity >=0.4.0 <0.7.0; contract Balance { function getBalance() public view returns (uint256 balance){ return msg.sender.balance; } } ''' contract_file_path = os.path.abspath("contract.sol") with open(contract_file_path, "wt", encoding='utf8') as f: f.write(contract_text) node_id = Id(20301, grpc_port=50301) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) TEST_CHECK(client.connection_test()) contracts = client.compile_file(code=contract_file_path) test_contract = contracts[0] distributor_address = client.load_address( keys_path=get_distributor_address_path()) deployed_contract_status = client.push_contract( from_address=distributor_address, code=test_contract, amount=0, fee=100000, init_message="") TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) current_balance = client.get_balance(address=distributor_address.address) gas_for_call = 10000000 call_message = client.encode_message(code=test_contract, message="getBalance()") contract_address = deployed_contract_status.data message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=gas_for_call, amount=0, message=call_message) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract, message=contract_data_message) TEST_CHECK(res['balance'] + gas_for_call == current_balance) return 0
def main(env: Env) -> int: node_id = Id(20101, grpc_port=50101) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) TEST_CHECK(client.connection_test()) return 0
def main(env: Env): contract_text = ''' pragma solidity >=0.4.0 <0.7.0; contract SimpleStorage { uint storedData; constructor(uint x) public{ storedData = x; } function set(uint x) public { storedData = x; } function get() public view returns (uint data) { return storedData; } } ''' contract_file_path = os.path.abspath("contract.sol") with open(contract_file_path, "wt", encoding='utf8') as f: f.write(contract_text) node_id = Id(20302, http_port=50302) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_HTTP, node_id) TEST_CHECK(client.connection_test()) contracts = client.compile_file(code=contract_file_path) test_contract = contracts[0] target_value = 8888 test_contract_init_message = client.encode_message( code=test_contract, message=f"constructor({target_value})") distributor_address = client.load_address( keys_path=get_distributor_address_path()) deployed_contract_status = client.push_contract( from_address=distributor_address, code=test_contract, amount=0, fee=100000, init_message=test_contract_init_message) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) TEST_CHECK( client.transaction_success_wait(transaction=deployed_contract_status)) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) # TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) call_message = client.encode_message(code=test_contract, message="get()") contract_address = deployed_contract_status.data message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=100000, amount=0, message=call_message) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) TEST_CHECK( client.transaction_success_wait(transaction=message_call_status)) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) # TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract, method="get", message=contract_data_message) TEST_CHECK(res['data'] == target_value) message_result = client.call_view(from_address=distributor_address, to_address=contract_address, message=call_message) res = client.decode_message(code=test_contract, method="get", message=message_result) TEST_CHECK(res['data'] == target_value) return 0
def main(env: Env) -> int: contract_text = ''' pragma solidity >=0.4.0 <0.7.0; contract AddressSend { uint256 coins_store; address payable minter; constructor() public { minter = msg.sender; } function testAddressSend(uint256 amount) public payable returns(bool is_success) { coins_store += msg.value; if(coins_store >= amount){ bool result = minter.send(coins_store); if(result){ coins_store = 0; } return result; } return true; } } ''' contract_file_path = os.path.abspath("contract.sol") with open(contract_file_path, "wt", encoding='utf8') as f: f.write(contract_text) node_id = Id(20217, grpc_port=50067) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) TEST_CHECK(client.connection_test()) contracts = client.compile_file(code=contract_file_path) test_contract = contracts[0] distributor_address = client.load_address( keys_path=get_distributor_address_path()) deployed_contract_status = client.push_contract( from_address=distributor_address, code=test_contract, amount=0, fee=100000, init_message="") TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) current_balance = client.get_balance(address=distributor_address.address) new_test_account = client.generate_keys(keys_path="account_1") initializing_amount = 100000000 transaction = client.transfer(to_address=new_test_account.address, amount=initializing_amount, from_address=distributor_address, fee=0) TEST_CHECK_EQUAL(transaction.status_code, TransactionStatusCode.PENDING) stat = client.get_transaction_status(tx_hash=transaction.tx_hash) TEST_CHECK_EQUAL(stat.status_code, TransactionStatusCode.SUCCESS) TEST_CHECK_EQUAL(client.get_balance(address=new_test_account.address), initializing_amount) contracts = client.compile_file(code=contract_file_path) test_contract = contracts[0] deployed_contract_status = client.push_contract( from_address=new_test_account, code=test_contract, amount=0, fee=100000, init_message="", timeout=5) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) current_balance = client.get_balance(address=new_test_account.address) gas_for_call = 10000000 amount_for_call = 5000000 call_message = client.encode_message( code=test_contract, message=f"testAddressSend({amount_for_call})") contract_address = deployed_contract_status.data message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=gas_for_call, amount=amount_for_call, message=call_message, timeout=5) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) res = client.decode_message(code=test_contract, message=message_call_status.data) TEST_CHECK(res['is_success']) TEST_CHECK_EQUAL( client.get_balance(address=new_test_account.address, timeout=2, wait=1), current_balance + amount_for_call) return 0
def main(env: Env) -> int: contract_a_text = ''' pragma solidity >=0.4.0 <0.7.0; import "./abstract_a.sol"; contract A is AbstractA { function sum(uint arg1, uint arg2) external override returns(uint) { return arg1 + arg2; } } ''' contract_b_text = ''' pragma solidity >=0.4.0 <0.7.0; import "./abstract_a.sol"; contract B { function doYourThing(address addressOfA, uint arg1, uint arg2) public returns (uint result) { AbstractA my_a = AbstractA(addressOfA); return my_a.sum(arg1, arg2); } } ''' contract_abstract_a_text = ''' pragma solidity >=0.4.0 <0.7.0; interface AbstractA { function sum(uint arg1, uint arg2) external returns(uint); } ''' contract_a_file_path = os.path.abspath("a.sol") with open(contract_a_file_path, "wt", encoding='utf8') as f: f.write(contract_a_text) contract_b_file_path = os.path.abspath("b.sol") with open(contract_b_file_path, "wt", encoding='utf8') as f: f.write(contract_b_text) contract_abstract_a_file_path = os.path.abspath("abstract_a.sol") with open(contract_abstract_a_file_path, "wt", encoding='utf8') as f: f.write(contract_abstract_a_text) node_id = Id(20216, grpc_port=50066) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) TEST_CHECK(client.connection_test()) contracts_a = client.compile_file(code=contract_a_file_path) test_contract_a = "A" contracts_b = client.compile_file(code=contract_b_file_path) test_contract_b = "B" distributor_address = client.load_address( keys_path=get_distributor_address_path()) deployed_contract_a_status = client.push_contract( from_address=distributor_address, code=test_contract_a, amount=0, fee=10000000, init_message="", timeout=5) TEST_CHECK_EQUAL(deployed_contract_a_status.status_code, TransactionStatusCode.PENDING) deployed_contract_a_status = client.get_transaction_status( tx_hash=deployed_contract_a_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_a_status.status_code, TransactionStatusCode.SUCCESS) deployed_contract_b_status = client.push_contract( from_address=distributor_address, code=test_contract_b, amount=0, fee=10000000, init_message="", timeout=5) TEST_CHECK_EQUAL(deployed_contract_b_status.status_code, TransactionStatusCode.PENDING) deployed_contract_b_status = client.get_transaction_status( tx_hash=deployed_contract_b_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_b_status.status_code, TransactionStatusCode.SUCCESS) arg1 = 5 arg2 = 6 call_message = client.encode_message( code=test_contract_b, message= f"doYourThing(Address({deployed_contract_a_status.data}), {arg1}, {arg2})" ) message_call_status = client.message_call( from_address=distributor_address, to_address=deployed_contract_b_status.data, fee=10000000, amount=0, message=call_message, timeout=5) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract_b, message=contract_data_message) TEST_CHECK(res['result'] == (arg1 + arg2)) return 0
def main(env: Env) -> int: contract_text = ''' pragma solidity >=0.4.0 <0.7.0; contract PayMe { uint256 coins_store; address payable target; constructor(address payable addr) public payable{ coins_store = msg.value; target = addr; } function payMe() public payable returns(uint256 current_balance){ coins_store += msg.value; return coins_store; } function initDelete() public { selfdestruct(target); } } ''' contract_file_path = os.path.abspath("contract.sol") with open(contract_file_path, "wt", encoding='utf8') as f: f.write(contract_text) node_id = Id(20219, grpc_port=50069) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) TEST_CHECK(client.connection_test()) contracts = client.compile_file(code=contract_file_path) test_contract = contracts[0] distributor_address = client.load_address( keys_path=get_distributor_address_path()) new_account = client.generate_keys(keys_path="new_account") init_message = client.encode_message( code=test_contract, message=f"constructor(Address({new_account.address}))") deployed_contract_status = client.push_contract( from_address=distributor_address, code=test_contract, amount=50, fee=10000000, init_message=init_message, timeout=7) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) amount_for_call = 100000 pay_message = client.encode_message(code=test_contract, message="payMe()") contract_address = deployed_contract_status.data message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=10000000, amount=amount_for_call, message=pay_message, timeout=7) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract, message=contract_data_message) TEST_CHECK( client.get_balance(address=contract_address, timeout=2, wait=1) == res["current_balance"]) message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=10000000, amount=amount_for_call, message=pay_message, timeout=7) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract, message=contract_data_message) TEST_CHECK( client.get_balance(address=contract_address, timeout=2, wait=1) == res["current_balance"]) TEST_CHECK(0 == client.get_balance( address=new_account.address, timeout=2, wait=1)) last_balance = client.get_balance(address=contract_address) delete_message = client.encode_message(code=test_contract, message="initDelete()") message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=10000000, amount=0, message=delete_message, timeout=7) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) TEST_CHECK(last_balance == client.get_balance(address=new_account.address)) TEST_CHECK(0 == client.get_balance(address=contract_address)) message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=10000000, amount=1, message=pay_message, timeout=7) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) return 0
def main(env: Env) -> int: contract_text = ''' pragma solidity >=0.4.0 <0.7.0; contract GetPreviousBlock { function get() public view returns (bytes32 previous_block_hash) { return blockhash(block.number - 1); } } ''' contract_file_path = os.path.abspath("contract.sol") with open(contract_file_path, "wt", encoding='utf8') as f: f.write(contract_text) node_id = Id(20218, grpc_port=50068) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.LEGACY_GRPC, node_id) TEST_CHECK(client.connection_test()) contracts = client.compile_file(code=contract_file_path) test_contract = contracts[0] distributor_address = client.load_address( keys_path=get_distributor_address_path()) deployed_contract_status = client.push_contract( from_address=distributor_address, code=test_contract, amount=0, fee=10000000, init_message="") TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) for i in range(0, 1): current_block_hash = client.node_info().top_block_hash contract_address = deployed_contract_status.data call_message = client.encode_message(code=test_contract, message="get()") message_call_status = client.message_call( from_address=distributor_address, to_address=contract_address, fee=10000000, amount=0, message=call_message, timeout=5) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract, message=contract_data_message) TEST_CHECK(res["previous_block_hash"] == current_block_hash) call_message = client.encode_message(code=test_contract, message="get()") message_call_status = client.message_call(from_address=distributor_address, to_address=contract_address, fee=10000000, amount=0, message=call_message, timeout=5) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.PENDING) message_call_status = client.get_transaction_status( tx_hash=message_call_status.tx_hash) TEST_CHECK_EQUAL(message_call_status.status_code, TransactionStatusCode.SUCCESS) contract_data_message = message_call_status.data res = client.decode_message(code=test_contract, message=contract_data_message) TEST_CHECK(res["previous_block_hash"] != current_block_hash) return 0
def main(env: Env): contract_file_path = os.path.join(CONTRACTS_FOLDER, "auction", "contract.sol") if not os.path.exists(contract_file_path): TEST_CHECK(False, message="Contracts file was not found") node_id = Id(20403, http_port=50403) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.PYTHON_HTTP, node_id) TEST_CHECK(client.connection_test()) contracts = client.compile_file(code=contract_file_path) auction_compiled_contract = contracts[0] beneficiary_keys = client.generate_keys(keys_path="user_1") beneficiary_init_balance = client.get_balance( address=beneficiary_keys.address) account_seconds_duration = 200 auction_init_message = client.encode_message( code=auction_compiled_contract, message= f"constructor({account_seconds_duration}, Address({beneficiary_keys.address}))" ) start_time = datetime.datetime.now() distributor_keys = client.load_address( keys_path=get_distributor_address_path()) deployed_contract_status = client.push_contract( from_address=distributor_keys, code=auction_compiled_contract, amount=0, fee=10000000, init_message=auction_init_message, wait=TRANSACTION_WAIT) TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.PENDING) TEST_CHECK( client.transaction_success_wait(transaction=deployed_contract_status)) deployed_contract_status = client.get_transaction_status( tx_hash=deployed_contract_status.tx_hash) # TEST_CHECK_EQUAL(deployed_contract_status.status_code, TransactionStatusCode.SUCCESS) deployed_contract = deployed_contract_status.data client_1 = client.generate_keys(keys_path="client_1") client_2 = client.generate_keys(keys_path="client_2") client_1_init_amount = 5000000 client_2_init_amount = 5000000 client.transfer(from_address=distributor_keys, to_address=client_1.address, amount=client_1_init_amount, fee=5, wait=TRANSACTION_WAIT) client.transfer(from_address=distributor_keys, to_address=client_2.address, amount=client_2_init_amount, fee=5, wait=TRANSACTION_WAIT) bid_message = client.encode_message(code=auction_compiled_contract, message="bid()") max_bid_message = client.encode_message(code=auction_compiled_contract, message="highestBid()") client_1_bid_1 = 10000 client_1_bid_1_gas = 100000 bid_status = client.message_call(from_address=client_1, to_address=deployed_contract, fee=client_1_bid_1_gas, amount=client_1_bid_1, message=bid_message, wait=TRANSACTION_WAIT) TEST_CHECK(client.transaction_success_wait(transaction=bid_status)) # bid_status = client.get_transaction_status(tx_hash=bid_status.tx_hash) # TEST_CHECK_EQUAL(bid_status.status_code, TransactionStatusCode.SUCCESS) client_1_bid_2 = 15000 client_1_bid_2_gas = 100000 bid_status = client.message_call(from_address=client_1, to_address=deployed_contract, fee=client_1_bid_2_gas, amount=client_1_bid_2, message=bid_message, wait=TRANSACTION_WAIT) TEST_CHECK(client.transaction_success_wait(transaction=bid_status)) # bid_status = client.get_transaction_status(tx_hash=bid_status.tx_hash) # TEST_CHECK_EQUAL(bid_status.status_code, TransactionStatusCode.SUCCESS) result = client.call_view(from_address=distributor_keys, to_address=deployed_contract, message=max_bid_message) current_max_bid = client.decode_message(code=auction_compiled_contract, method="highestBid", message=result) TEST_CHECK(current_max_bid[''] == client_1_bid_2) client_2_bid_1 = 10000 client_2_bid_1_gas = 100000 bid_status = client.message_call(from_address=client_2, to_address=deployed_contract, fee=client_2_bid_1_gas, amount=client_2_bid_1, message=bid_message, wait=TRANSACTION_WAIT) TEST_CHECK(client.transaction_success_wait(transaction=bid_status)) # bid_status = client.get_transaction_status(tx_hash=bid_status.tx_hash) # TEST_CHECK_NOT_EQUAL(bid_status.status_code, TransactionStatusCode.SUCCESS) result = client.call_view(from_address=distributor_keys, to_address=deployed_contract, message=max_bid_message) current_max_bid = client.decode_message(code=auction_compiled_contract, method="highestBid", message=result) TEST_CHECK(current_max_bid[''] == client_1_bid_2) client_2_bid_2 = 20000 client_2_bid_2_gas = 100000 bid_status = client.message_call(from_address=client_2, to_address=deployed_contract, fee=client_2_bid_2_gas, amount=client_2_bid_2, message=bid_message, wait=TRANSACTION_WAIT) TEST_CHECK(client.transaction_success_wait(transaction=bid_status)) # bid_status = client.get_transaction_status(tx_hash=bid_status.tx_hash) # TEST_CHECK_EQUAL(bid_status.status_code, TransactionStatusCode.SUCCESS) result = client.call_view(from_address=distributor_keys, to_address=deployed_contract, message=max_bid_message) current_max_bid = client.decode_message(code=auction_compiled_contract, method="highestBid", message=result) TEST_CHECK(current_max_bid[''] == client_2_bid_2) time.sleep(account_seconds_duration - (datetime.datetime.now() - start_time).total_seconds()) result = client.call_view(from_address=distributor_keys, to_address=deployed_contract, message=max_bid_message) current_max_bid = client.decode_message(code=auction_compiled_contract, method="highestBid", message=result) auction_end_message = client.encode_message(code=auction_compiled_contract, message="auctionEnd()") auction_end_status = client.message_call(from_address=distributor_keys, to_address=deployed_contract, fee=100000, amount=0, message=auction_end_message, wait=TRANSACTION_WAIT) TEST_CHECK(client.transaction_success_wait(transaction=auction_end_status)) # auction_end_status = client.get_transaction_status(tx_hash=auction_end_status.tx_hash) # TEST_CHECK_EQUAL(auction_end_status.status_code, TransactionStatusCode.SUCCESS) TEST_CHECK( client.decode_message(code=auction_compiled_contract, method="auctionEnd", message=auction_end_status.data)['is_end']) beneficiary_current_balance = client.get_balance( address=beneficiary_keys.address) TEST_CHECK(beneficiary_init_balance + current_max_bid[''] == beneficiary_current_balance) return 0
def main(env: Env) -> int: node_id = Id(20103, http_port=50103) env.start_node(NodeConfig(node_id)) client = env.get_client(ClientType.PYTHON_HTTP, node_id) TEST_CHECK(client.connection_test()) return 0