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

fix: Improve Code Readability and Performance for Contract Hash Gener… #1275

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
69 changes: 25 additions & 44 deletions scripts/get_hashes_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,46 @@
]

def main():
# Required compiler version argument
cmp_version = sys.argv[1]

# Read class hashes from stdin
contracts = json.load(sys.stdin)

cmp_version = sys.argv[1] # Compiler version argument
contracts = json.load(sys.stdin) # Read JSON from stdin
print(generate_doc_file(cmp_version, contracts))


def generate_doc_file(cmp_version, contracts):
header = f"""// Version
:class-hash-cairo-version: \
https://crates.io/crates/cairo-lang-compiler/{cmp_version}[cairo {cmp_version}]
"""
hashes = "// Class Hashes\n"
contracts['contracts'] = remove_prefix_from_names(contracts['contracts'])
contracts['contracts'].sort(key=lambda x: x['name'])

hashes += get_known_order_hashes(contracts['contracts'])
for contract in contracts['contracts']:
# Avoid the already added contracts in the known order
if contract['name'] in KNOWN_ORDER:
continue
# Avoid adding mocks
# TODO: remove this after mocks are removed from the artifacts built outside tests
if "Mock" in contract['name']:
continue
hashes += f":{contract['name']}-class-hash: {normalize_len(contract['sierra'])}\n"
# Remove prefixes and sort contracts by name
contracts['contracts'] = [
{**contract, 'name': contract['name'].removeprefix('openzeppelin_presets_')}
for contract in contracts['contracts']
]
contracts['contracts'].sort(key=lambda c: c['name'])

# Generate known order hashes and other hashes
known_hashes = get_known_order_hashes(contracts['contracts'])
other_hashes = [
f":{contract['name']}-class-hash: {normalize_len(contract['sierra'])}"
for contract in contracts['contracts']
if contract['name'] not in KNOWN_ORDER and "Mock" not in contract['name']
]

footer = """// Presets page
:presets-page: xref:presets.adoc[Sierra class hash]"""

return f"{header}\n{hashes}\n{footer}\n"


def remove_prefix_from_names(contracts):
for contract in contracts:
contract.update([("name", remove_prefix(contract['name'], 'openzeppelin_presets_'))])
return contracts


def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text

return f"{header}\n// Class Hashes\n{known_hashes}{'\n'.join(other_hashes)}\n\n{footer}\n"

def get_known_order_hashes(contracts):
known_order_hashes = [""] * len(KNOWN_ORDER)
for contract in contracts:
if contract['name'] in KNOWN_ORDER:
index = KNOWN_ORDER.index(contract['name'])
known_order_hashes[index] = f":{contract['name']}-class-hash: {normalize_len(contract['sierra'])}\n"
return ''.join(known_order_hashes)

known_hashes = [
f":{contract['name']}-class-hash: {normalize_len(contract['sierra'])}"
for contract in contracts
if contract['name'] in KNOWN_ORDER
]
# Ensure the hashes are sorted according to KNOWN_ORDER
return '\n'.join(sorted(known_hashes, key=lambda h: KNOWN_ORDER.index(h.split('-class-hash:')[0][1:])))

def normalize_len(sierra_hash):
return "0x" + "0" * (66 - len(sierra_hash)) + sierra_hash[2:]

return "0x" + sierra_hash[2:].zfill(64)

if __name__ == '__main__':
main()
Loading