-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestnet_node.py
181 lines (142 loc) · 5.8 KB
/
testnet_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from __future__ import annotations
import argparse
import asyncio
import shutil
import sys
import time
from typing import TYPE_CHECKING
import test_tools as tt
from clive.__private.core.accounts.accounts import WatchedAccount, WorkingAccount
from clive.__private.core.commands.create_wallet import CreateWallet
from clive.__private.core.constants.setting_identifiers import NODE_CHAIN_ID, SECRETS_NODE_ADDRESS
from clive.__private.core.keys.keys import PrivateKeyAliased
from clive.__private.core.profile import Profile
from clive.__private.core.world import World
from clive.__private.run_tui import run_tui
from clive.__private.settings import safe_settings, settings
from clive_local_tools.data.constants import (
ALT_WORKING_ACCOUNT1_KEY_ALIAS,
TESTNET_CHAIN_ID,
WORKING_ACCOUNT_KEY_ALIAS,
)
from clive_local_tools.testnet_block_log import run_node
from clive_local_tools.testnet_block_log.constants import (
ALT_WORKING_ACCOUNT1_DATA,
ALT_WORKING_ACCOUNT1_NAME,
WATCHED_ACCOUNTS_NAMES,
WORKING_ACCOUNT_DATA,
WORKING_ACCOUNT_NAME,
)
if TYPE_CHECKING:
from collections.abc import Sequence
def init_argparse(args: Sequence[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Clive testnet configurator")
add = parser.add_argument
add(
"-p",
"--prepare-profiles",
action="store_true",
default=False,
help="When set, pregenerated profiles will be created. Otherwise clive create_profile will be launched.",
)
add(
"-t",
"--tui",
action="store_true",
default=False,
help="When set, TUI will be launched. Otherwise only testnet node will be configured and launched.",
)
return parser.parse_args(args)
def prepare_node() -> tt.RawNode:
# TODO: time_offset/use_faketime option should be used there but faketime is not available in the embedded_testnet
# docker image yet
return run_node(webserver_http_endpoint="0.0.0.0:8090")
async def prepare_profiles(node: tt.RawNode) -> None:
tt.logger.info("Configuring profiles for clive")
settings.set(SECRETS_NODE_ADDRESS, node.http_endpoint.as_string())
settings.set(NODE_CHAIN_ID, TESTNET_CHAIN_ID)
_create_profile(
profile_name=WORKING_ACCOUNT_NAME,
working_account_name=WORKING_ACCOUNT_NAME,
watched_accounts_names=WATCHED_ACCOUNTS_NAMES,
)
_create_profile(
profile_name=ALT_WORKING_ACCOUNT1_NAME,
working_account_name=ALT_WORKING_ACCOUNT1_NAME,
watched_accounts_names=WATCHED_ACCOUNTS_NAMES,
)
await _create_wallet(
working_account_name=WORKING_ACCOUNT_NAME,
private_key=WORKING_ACCOUNT_DATA.account.private_key,
key_alias=WORKING_ACCOUNT_KEY_ALIAS,
)
await _create_wallet(
working_account_name=ALT_WORKING_ACCOUNT1_NAME,
private_key=ALT_WORKING_ACCOUNT1_DATA.account.private_key,
key_alias=ALT_WORKING_ACCOUNT1_KEY_ALIAS,
)
def _create_profile(profile_name: str, working_account_name: str, watched_accounts_names: list[str]) -> None:
Profile.create(
profile_name,
working_account=WorkingAccount(name=working_account_name),
watched_accounts=[WatchedAccount(name) for name in watched_accounts_names],
).save()
async def _create_wallet(working_account_name: str, private_key: str, key_alias: str) -> None:
async with World(working_account_name) as world_cm:
password = await CreateWallet(
app_state=world_cm.app_state,
beekeeper=world_cm.beekeeper,
wallet=working_account_name,
password=working_account_name * 2,
).execute_with_result()
tt.logger.info(f"password for profile `{working_account_name}` is: `{password}`")
world_cm.profile.keys.add_to_import(PrivateKeyAliased(value=private_key, alias=key_alias))
await world_cm.commands.sync_data_with_beekeeper()
def create_proposal(wallet: tt.Wallet) -> None:
proposal_authors = ["initminer", "alice", "bob", "john"]
for discriminator, author in enumerate(proposal_authors, start=1):
wallet.api.post_comment(
author=author,
permlink=f"test-permlink-{discriminator}",
parent_author="",
parent_permlink=f"test-parent-{discriminator}",
title=f"test-title-{discriminator}",
body=f"test-body-{discriminator}",
json="{}",
)
wallet.api.create_proposal(
creator=author,
receiver="alice",
start_date=tt.Time.now(),
end_date=tt.Time.from_now(weeks=discriminator),
daily_pay=tt.Asset.Tbd(discriminator).as_nai(),
subject=f"test-subject-{discriminator}",
permlink=f"test-permlink-{discriminator}",
)
def print_working_account_keys() -> None:
tt.logger.info(f"{WORKING_ACCOUNT_NAME} public key: {WORKING_ACCOUNT_DATA.account.public_key}")
tt.logger.info(f"{WORKING_ACCOUNT_NAME} private key: {WORKING_ACCOUNT_DATA.account.private_key}")
def launch_tui() -> None:
tt.logger.info("Attempting to start a clive interactive mode - exit to finish")
run_tui()
def serve_forever() -> None:
tt.logger.info("Serving forever... press Ctrl+C to exit")
while True:
time.sleep(1)
async def prepare(*, recreate_profiles: bool) -> None:
node = prepare_node()
print_working_account_keys()
if recreate_profiles:
shutil.rmtree(safe_settings.data_path, ignore_errors=True)
await prepare_profiles(node)
def main() -> None:
args = init_argparse(sys.argv[1:])
should_prepare_profiles = args.prepare_profiles
should_launch_tui = args.tui
asyncio.run(prepare(recreate_profiles=should_prepare_profiles))
if should_launch_tui:
launch_tui()
else:
serve_forever()
if __name__ == "__main__":
main()