Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend configure script #356

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/xian/tools/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"""


# TODO: Set chain_id through this too
class Configure:
"""
Snapshot should be a tar.gz file containing
Expand All @@ -43,9 +42,9 @@ def __init__(self):
)
parser.add_argument(
'--seed-node-address',
type=str,
help='Seed node address e.g. <node_id>@91.108.112.184 . For cold booting a test network.',
required=False
type=str,
help='Seed node address e.g. <node_id>@91.108.112.184 . For cold booting a test network.',
required=False
)
parser.add_argument(
'--moniker',
Expand Down Expand Up @@ -126,6 +125,19 @@ def __init__(self):
required=False,
default=100000
)
parser.add_argument(
'--single-node',
action=BooleanOptionalAction,
help='Run as a single node',
required=False
)
parser.add_argument(
'--chain-id',
type=str,
help='Chain ID for the network',
required=False,
default='xian-network'
)

self.args = parser.parse_args()

Expand All @@ -151,7 +163,7 @@ def get_node_info(self, seed_node):
sleep(1) # wait 1 second before trying again

return None # or raise an Exception indicating the request ultimately failed

def download_and_extract(self, url, target_path):
# Download the file from the URL
response = requests.get(url)
Expand All @@ -160,11 +172,11 @@ def download_and_extract(self, url, target_path):
tar_path = target_path / filename
# Ensure the target directory exists
os.makedirs(target_path, exist_ok=True)

# Save the downloaded file to disk
with open(tar_path, 'wb') as file:
file.write(response.content)

# Extract the tar.gz file
if tar_path.endswith(".tar.gz"):
with tarfile.open(tar_path, "r:gz") as tar:
Expand All @@ -174,7 +186,7 @@ def download_and_extract(self, url, target_path):
tar.extractall(path=target_path)
else:
print("File format not recognized. Please use a .tar.gz or .tar file.")

os.remove(tar_path)

def generate_keys(self):
Expand Down Expand Up @@ -215,7 +227,7 @@ def generate_keys(self):
def main(self):
# Make sure this is run in the tools directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))

if not os.path.exists(self.CONFIG_PATH):
print('Initialize CometBFT first')
return
Expand Down Expand Up @@ -278,11 +290,12 @@ def main(self):
sk = SigningKey(seed=seed)
vk = sk.verify_key

validator_pubkey = vk.encode().hex()
single_node_arg = '--single-node' if self.args.single_node else ''

os.system(f'python3 genesis_gen.py '
f'--validator-pubkey {validator_pubkey} '
f'--founder-privkey {self.args.founder_privkey}')
f'--founder-privkey {self.args.founder_privkey} '
f'--chain-id {self.args.chain_id} '
f'{single_node_arg}')

# Get generated genesis block JSON
with open(Path('genesis') / 'genesis_block.json') as first_file:
Expand Down
30 changes: 24 additions & 6 deletions src/xian/tools/genesis_gen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from argparse import ArgumentParser
from argparse import ArgumentParser, BooleanOptionalAction
from xian.utils.block import is_compiled_key
from contracting.client import ContractingClient
from contracting.storage.driver import Driver
Expand Down Expand Up @@ -50,6 +50,19 @@ def __init__(self):
default=None,
help='Path to existing cometbft genesis file to update the abci_genesis on.'
)
parser.add_argument(
'--single-node',
action=BooleanOptionalAction,
required=False,
help='If set, all contracts will be owned by the founder'
)
parser.add_argument(
'--chain-id',
type=str,
required=False,
default='xian-network',
help='Chain ID for the network'
)
self.args = parser.parse_args()

def hash_block_data(self, hlc_timestamp: str, block_number: str, previous_block_hash: str) -> str:
Expand All @@ -76,6 +89,9 @@ def build_genesis(self, founder_privkey: str):
contracting = ContractingClient(driver=Driver())
contracting.set_submission_contract(commit=False)

wallet = Wallet(seed=founder_privkey)
founder_public_key = wallet.public_key

con_cfg_path = self.CONTRACT_DIR / f'contracts_{self.args.network}.json'

with open(con_cfg_path) as f:
Expand All @@ -102,12 +118,15 @@ def build_genesis(self, founder_privkey: str):
for i, s in enumerate(v):
if type(s) is str:
v[i] = self.replace_arg(s, locals())

if contracting.get_contract(con_name) is None:
# If single-node mode is enabled, set the owner to the founder
owner = founder_public_key if self.args.single_node else contract['owner']

contracting.submit(
code,
name=con_name,
owner=contract['owner'],
owner=owner,
constructor_args=contract['constructor_args']
)

Expand Down Expand Up @@ -140,9 +159,7 @@ def build_genesis(self, founder_privkey: str):
})

# Signing genesis block with founder's wallet
wallet = Wallet(seed=founder_privkey)

genesis_block['origin']['sender'] = wallet.public_key
genesis_block['origin']['sender'] = founder_public_key
genesis_block['origin']['signature'] = wallet.sign_msg(
self.hash_state_changes(genesis_block['genesis'])
)
Expand All @@ -162,6 +179,7 @@ def main(self):
with open(self.args.genesis_to_update, 'r') as f:
existing_genesis = json.load(f)

existing_genesis['chain_id'] = self.args.chain_id
existing_genesis['abci_genesis'] = genesis
with open(self.args.genesis_to_update, 'w') as f:
f.write(encode(existing_genesis))
Expand Down
Loading