Skip to content

Commit fc20f20

Browse files
author
Evgeniy Zayats
committed
tests: add tests with main chain
closes #943 Signed-off-by: Evgeniy Zayats <[email protected]>
1 parent 32fba65 commit fc20f20

File tree

11 files changed

+341
-99
lines changed

11 files changed

+341
-99
lines changed

neofs-testlib/neofs_testlib/env/env.py

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -241,25 +241,49 @@ def deploy_neofs_contract(self):
241241
"There should be at least a single IR instance configured(not started) to deploy neofs contract"
242242
)
243243
neo_go = self.neo_go()
244+
neo_go.nep17.balance(
245+
self.main_chain.wallet.address,
246+
"GAS",
247+
f"http://{self.main_chain.rpc_address}",
248+
wallet_config=self.main_chain.neo_go_config,
249+
)
244250
neo_go.nep17.transfer(
245251
"GAS",
246252
self.default_wallet.address,
247253
f"http://{self.main_chain.rpc_address}",
248-
from_address=self.inner_ring_nodes[-1].alphabet_wallet.address,
254+
from_address=self.main_chain.wallet.address,
249255
amount=9000,
250256
force=True,
251257
wallet_config=self.main_chain.neo_go_config,
252258
await_=True,
253259
)
254-
ir_alphabet_pubkey_from_neogo = wallet_utils.get_last_public_key_from_wallet_with_neogo(
255-
self.neo_go(), self.inner_ring_nodes[-1].alphabet_wallet.path
260+
261+
for ir_node in self.inner_ring_nodes:
262+
accounts = wallet_utils.get_accounts_from_wallet(ir_node.alphabet_wallet.path, self.default_password)
263+
for acc in accounts:
264+
neo_go.nep17.transfer(
265+
"GAS",
266+
acc.address,
267+
f"http://{self.main_chain.rpc_address}",
268+
from_address=self.main_chain.wallet.address,
269+
amount=9000,
270+
force=True,
271+
wallet_config=self.main_chain.neo_go_config,
272+
await_=True,
273+
)
274+
275+
pub_keys_of_existing_ir_nodes = " ".join(
276+
wallet_utils.get_last_public_key_from_wallet_with_neogo(
277+
self.neo_go(),
278+
ir_node.alphabet_wallet.path,
279+
).splitlines()
256280
)
257281
result = neo_go.contract.deploy(
258282
input_file=f"{self.neofs_contract_dir}/neofs/neofs_contract.nef",
259283
manifest=f"{self.neofs_contract_dir}/neofs/config.json",
260284
force=True,
261285
rpc_endpoint=f"http://{self.main_chain.rpc_address}",
262-
post_data=f"[ true ffffffffffffffffffffffffffffffffffffffff [ {ir_alphabet_pubkey_from_neogo} ] [ InnerRingCandidateFee 10 WithdrawFee 10 ] ]",
286+
post_data=f"[ true ffffffffffffffffffffffffffffffffffffffff [ {pub_keys_of_existing_ir_nodes} ] [ InnerRingCandidateFee 10 WithdrawFee 10 ] ]",
263287
wallet_config=self.default_wallet_neogo_config,
264288
)
265289
contract_hash = result.stdout.split("Contract: ")[-1].strip()
@@ -306,34 +330,34 @@ def generate_storage_wallet(
306330

307331
@allure.step("Generate alphabet wallets")
308332
def generate_alphabet_wallets(
309-
self,
310-
network_config: Optional[str] = None,
311-
size: Optional[int] = 1,
333+
self, network_config: Optional[str] = None, size: Optional[int] = 1, alphabet_wallets_dir: Optional[str] = None
312334
) -> list[NodeWallet]:
313335
neofs_adm = self.neofs_adm(network_config)
314336

315-
neofs_adm.fschain.generate_alphabet(alphabet_wallets=self.alphabet_wallets_dir, size=size)
337+
if not alphabet_wallets_dir:
338+
alphabet_wallets_dir = self.alphabet_wallets_dir
339+
neofs_adm.fschain.generate_alphabet(alphabet_wallets=alphabet_wallets_dir, size=size)
316340

317341
generated_wallets = []
318342

319-
for generated_wallet in os.listdir(self.alphabet_wallets_dir):
343+
for generated_wallet in os.listdir(alphabet_wallets_dir):
320344
# neo3 package requires some attributes to be set
321-
with open(os.path.join(self.alphabet_wallets_dir, generated_wallet), "r") as wallet_file:
345+
with open(os.path.join(alphabet_wallets_dir, generated_wallet), "r") as wallet_file:
322346
wallet_json = json.load(wallet_file)
323347

324348
wallet_json["name"] = None
325349
for acc in wallet_json["accounts"]:
326350
acc["extra"] = None
327351

328-
with open(os.path.join(self.alphabet_wallets_dir, generated_wallet), "w") as wallet_file:
352+
with open(os.path.join(alphabet_wallets_dir, generated_wallet), "w") as wallet_file:
329353
json.dump(wallet_json, wallet_file)
330354

331355
generated_wallets.append(
332356
NodeWallet(
333-
path=os.path.join(self.alphabet_wallets_dir, generated_wallet),
357+
path=os.path.join(alphabet_wallets_dir, generated_wallet),
334358
password=self.default_password,
335359
address=wallet_utils.get_last_address_from_wallet(
336-
os.path.join(self.alphabet_wallets_dir, generated_wallet), self.default_password
360+
os.path.join(alphabet_wallets_dir, generated_wallet), self.default_password
337361
),
338362
)
339363
)
@@ -686,6 +710,8 @@ def __init__(self, neofs_env: NeoFSEnv):
686710
self.p2p_address = f"{self.neofs_env.domain}:{NeoFSEnv.get_available_port()}"
687711
self.pprof_address = f"{self.neofs_env.domain}:{NeoFSEnv.get_available_port()}"
688712
self.prometheus_address = f"{self.neofs_env.domain}:{NeoFSEnv.get_available_port()}"
713+
self.wallet_dir = self.neofs_env._generate_temp_dir(prefix="mainchain_wallet")
714+
self.wallet = None
689715
self.stdout = "Not initialized"
690716
self.stderr = "Not initialized"
691717
self.process = None
@@ -715,21 +741,31 @@ def start(self, wait_until_ready=True):
715741
if self.process is not None:
716742
raise RuntimeError("This main chain instance has already been started")
717743

718-
alphabet_wallet = self.neofs_env.inner_ring_nodes[-1].alphabet_wallet
744+
self.wallet = self.neofs_env.generate_alphabet_wallets(alphabet_wallets_dir=self.wallet_dir)[0]
745+
746+
standby_committee = wallet_utils.get_last_public_key_from_wallet_with_neogo(
747+
self.neofs_env.neo_go(), self.wallet.path
748+
)
719749

720750
ir_alphabet_pubkey_from_neogo = wallet_utils.get_last_public_key_from_wallet_with_neogo(
721-
self.neofs_env.neo_go(), alphabet_wallet.path
751+
self.neofs_env.neo_go(), self.neofs_env.inner_ring_nodes[-1].alphabet_wallet.path
722752
)
723753

754+
if len(self.neofs_env.inner_ring_nodes) > 1:
755+
ir_public_keys = ir_alphabet_pubkey_from_neogo.splitlines()
756+
else:
757+
ir_public_keys = [ir_alphabet_pubkey_from_neogo]
758+
724759
logger.info(f"Generating main chain config at: {self.main_chain_config_path}")
725760
main_chain_config_template = "main_chain.yaml"
726761

727762
NeoFSEnv.generate_config_file(
728763
config_template=main_chain_config_template,
729764
config_path=self.main_chain_config_path,
730765
custom=Path(main_chain_config_template).is_file(),
731-
wallet=alphabet_wallet,
732-
public_key=ir_alphabet_pubkey_from_neogo,
766+
wallet=self.wallet,
767+
standby_committee=standby_committee,
768+
ir_public_keys=ir_public_keys,
733769
main_chain_boltdb=self.main_chain_boltdb,
734770
p2p_address=self.p2p_address,
735771
rpc_address=self.rpc_address,
@@ -738,12 +774,10 @@ def start(self, wait_until_ready=True):
738774
prometheus_address=self.prometheus_address,
739775
)
740776
logger.info(f"Generating CLI config at: {self.cli_config}")
741-
NeoFSEnv.generate_config_file(
742-
config_template="cli_cfg.yaml", config_path=self.cli_config, wallet=alphabet_wallet
743-
)
777+
NeoFSEnv.generate_config_file(config_template="cli_cfg.yaml", config_path=self.cli_config, wallet=self.wallet)
744778
logger.info(f"Generating NEO GO config at: {self.neo_go_config}")
745779
NeoFSEnv.generate_config_file(
746-
config_template="neo_go_cfg.yaml", config_path=self.neo_go_config, wallet=alphabet_wallet
780+
config_template="neo_go_cfg.yaml", config_path=self.neo_go_config, wallet=self.wallet
747781
)
748782
logger.info(f"Launching Main Chain:{self}")
749783
self._launch_process()
@@ -839,18 +873,31 @@ def generate_cli_config(self):
839873
)
840874

841875
@allure.step("Start Inner Ring node")
842-
def start(self, wait_until_ready=True, with_main_chain=False):
876+
def start(
877+
self,
878+
wait_until_ready=True,
879+
with_main_chain=False,
880+
pub_keys_of_existing_ir_nodes=None,
881+
seed_node_addresses_of_existing_ir_nodes=None,
882+
fschain_autodeploy=True,
883+
):
843884
if self.process is not None:
844885
raise RuntimeError("This inner ring node instance has already been started")
845886
logger.info(f"Generating IR config at: {self.ir_node_config_path}")
846887
ir_config_template = "ir.yaml"
847888

848-
pub_keys_of_existing_ir_nodes = [
849-
wallet_utils.get_last_public_key_from_wallet(ir_node.alphabet_wallet.path, ir_node.alphabet_wallet.password)
850-
for ir_node in self.neofs_env.inner_ring_nodes
851-
]
889+
if not pub_keys_of_existing_ir_nodes:
890+
pub_keys_of_existing_ir_nodes = [
891+
wallet_utils.get_last_public_key_from_wallet(
892+
ir_node.alphabet_wallet.path, ir_node.alphabet_wallet.password
893+
)
894+
for ir_node in self.neofs_env.inner_ring_nodes
895+
]
852896

853-
seed_node_addresses_of_existing_ir_nodes = [ir_node.p2p_address for ir_node in self.neofs_env.inner_ring_nodes]
897+
if not seed_node_addresses_of_existing_ir_nodes:
898+
seed_node_addresses_of_existing_ir_nodes = [
899+
ir_node.p2p_address for ir_node in self.neofs_env.inner_ring_nodes
900+
]
854901

855902
NeoFSEnv.generate_config_file(
856903
config_template=ir_config_template,
@@ -868,6 +915,7 @@ def start(self, wait_until_ready=True, with_main_chain=False):
868915
len(self.neofs_env.inner_ring_nodes) - (len(self.neofs_env.inner_ring_nodes) - 1) / 3 - 1
869916
),
870917
set_roles_in_genesis=str(False if len(self.neofs_env.inner_ring_nodes) == 1 else True).lower(),
918+
fschain_autodeploy=fschain_autodeploy,
871919
control_public_key=wallet_utils.get_last_public_key_from_wallet(
872920
self.alphabet_wallet.path, self.alphabet_wallet.password
873921
),

neofs-testlib/neofs_testlib/env/templates/ir.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fschain:
5959
min: {{ peers_min_number }}
6060
set_roles_in_genesis: {{ set_roles_in_genesis }}
6161

62-
fschain_autodeploy: true
62+
fschain_autodeploy: {{ fschain_autodeploy }}
6363

6464
nns:
6565
system_email: [email protected]

neofs-testlib/neofs_testlib/env/templates/main_chain.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ ProtocolConfiguration:
44
TimePerBlock: 1s
55
MemPoolSize: 50000
66
StandbyCommittee:
7-
- {{ public_key }}
7+
- {{ standby_committee }}
88
ValidatorsCount: 1
99
VerifyTransactions: true
1010
P2PSigExtensions: false
11+
Genesis:
12+
Roles:
13+
NeoFSAlphabet:
14+
{%- for public_key in ir_public_keys %}
15+
- {{ public_key }}
16+
{%- endfor %}
1117

1218
ApplicationConfiguration:
1319
SkipBlockVerification: false

neofs-testlib/neofs_testlib/utils/wallet.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from neo3.wallet import account as neo3_account
55
from neo3.wallet import wallet as neo3_wallet
6+
67
from neofs_testlib.cli.neogo import NeoGo
78

89
logger = logging.getLogger("neofs.testlib.utils")
@@ -49,6 +50,29 @@ def get_last_address_from_wallet(
4950
return address
5051

5152

53+
def get_accounts_from_wallet(
54+
wallet_path: str, wallet_password: str | None = None, wallet_passwords: list[str] | None = None
55+
):
56+
"""
57+
Extracting the last address from the given wallet.
58+
Args:
59+
wallet_path: The path to the wallet to extract address from.
60+
wallet_password: The password for the given wallet.
61+
wallet_passwords: The password list for the given accounts in the wallet
62+
Returns:
63+
The address for the wallet.
64+
"""
65+
if wallet_password is None and wallet_passwords is None:
66+
raise ValueError("Either wallet_password or wallet_passwords should be specified")
67+
68+
with open(wallet_path) as wallet_file:
69+
wallet_json = json.load(wallet_file)
70+
if wallet_password is not None:
71+
wallet_passwords = [wallet_password] * len(wallet_json["accounts"])
72+
wallet = neo3_wallet.Wallet.from_json(wallet_json, passwords=wallet_passwords)
73+
return wallet.accounts
74+
75+
5276
def get_last_public_key_from_wallet(
5377
wallet_path: str, wallet_password: str | None = None, wallet_passwords: list[str] | None = None
5478
):

pytest_tests/lib/helpers/utility.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ def wait_for_gc_pass_on_storage_nodes() -> None:
6060
wait_time = parse_time(STORAGE_GC_TIME)
6161
with allure.step(f"Wait {wait_time}s until GC completes on storage nodes"):
6262
time.sleep(wait_time)
63+
64+
65+
def parse_node_height(stdout: str) -> tuple[float, float]:
66+
lines = stdout.strip().split("\n")
67+
block_height = float(lines[0].split(": ")[1].strip())
68+
state = float(lines[1].split(": ")[1].strip())
69+
return block_height, state

pytest_tests/lib/helpers/wallet_helpers.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,54 @@ def wait_for_correct_wallet_balance(
6262
assert compare_func(get_wallet_balance(neofs_env, neo_go, wallet, neo_go_wallet_config)), (
6363
"Wallet balance is not correct after"
6464
)
65+
66+
67+
def create_wallet_with_money(neofs_env_with_mainchain: NeoFSEnv) -> NodeWallet:
68+
neofs_env = neofs_env_with_mainchain
69+
70+
with allure.step("Create wallet for deposit"):
71+
wallet = NodeWallet(
72+
path=neofs_env_with_mainchain._generate_temp_file(
73+
neofs_env._env_dir, prefix="deposit_withdrawal_test_wallet"
74+
),
75+
address="",
76+
password=neofs_env.default_password,
77+
)
78+
init_wallet(wallet.path, wallet.password)
79+
wallet.address = get_last_address_from_wallet(wallet.path, wallet.password)
80+
wallet.neo_go_config = neofs_env.generate_neo_go_config(wallet)
81+
wallet.cli_config = neofs_env.generate_cli_config(wallet)
82+
83+
with allure.step("Transfer some money to created wallet"):
84+
neo_go = neofs_env.neo_go()
85+
neo_go.nep17.transfer(
86+
"GAS",
87+
wallet.address,
88+
f"http://{neofs_env.main_chain.rpc_address}",
89+
from_address=neofs_env.main_chain.wallet.address,
90+
amount=1000,
91+
force=True,
92+
wallet_config=neofs_env.main_chain.neo_go_config,
93+
await_=True,
94+
)
95+
assert get_wallet_balance(neofs_env, neo_go, wallet, wallet.neo_go_config) == 1000.0, (
96+
"Money transfer from alphabet to test wallet didn't succeed"
97+
)
98+
99+
with allure.step("Deposit money to neofs contract"):
100+
neo_go.nep17.transfer(
101+
"GAS",
102+
neofs_env.main_chain.neofs_contract_address,
103+
f"http://{neofs_env.main_chain.rpc_address}",
104+
from_address=wallet.address,
105+
amount=100,
106+
force=True,
107+
wallet_config=wallet.neo_go_config,
108+
await_=True,
109+
)
110+
assert get_wallet_balance(neofs_env, neo_go, wallet, wallet.neo_go_config) <= 900, (
111+
"Wallet balance is not correct after deposit"
112+
)
113+
wait_for_correct_neofs_balance(neofs_env, wallet, wallet.cli_config, lambda balance: balance == 100)
114+
115+
return wallet

pytest_tests/tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ def neofs_env_with_mainchain(request):
253253
NeoFSEnv.cleanup_unused_ports()
254254

255255

256+
@pytest.fixture
257+
def clear_neofs_env():
258+
neofs_env = NeoFSEnv(neofs_env_config=NeoFSEnv._generate_default_neofs_env_config())
259+
yield neofs_env
260+
neofs_env.kill()
261+
262+
256263
@pytest.fixture(scope="module", autouse=True)
257264
def cleanup_temp_files():
258265
yield

0 commit comments

Comments
 (0)