Skip to content

Commit

Permalink
fix: spec builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Supremesource committed Nov 13, 2024
1 parent ca48985 commit 2d87795
Show file tree
Hide file tree
Showing 5 changed files with 262,941 additions and 114 deletions.
28 changes: 14 additions & 14 deletions pallets/offworker/src/dispatches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use frame_support::pallet_macros::pallet_section;
pub mod dispatches {
#[pallet::call]
impl<T: Config> Pallet<T> {
/// TODO: v2 of DEW will involve offworker sending potential zk proofs of encryption
/// correctness (proof that he can not decrypt certain weights)
///
/// # References
/// - [CS03] Practical Verifiable Encryption and Decryption of Discrete Logarithms
/// Jan Camenisch and Victor Shoup, CRYPTO 2003
/// https://link.springer.com/content/pdf/10.1007/978-3-540-45146-4_8.pdf
///
/// - [BBBPWM] Bulletproofs: Short Proofs for Confidential Transactions and More
/// Benedikt Bünz, Jonathan Bootle, Dan Boneh, Andrew Poelstra, Pieter Wuille and Greg Maxwell, IEEE
/// https://eprint.iacr.org/2017/1066.pdf
///
/// # Implementation
/// S&P 2018 validaity https://github.com/ZenGo-X/dlog-verifiable-enc
// TODO: v2 of DEW will involve offworker sending potential zk proofs of encryption
// correctness (proof that he can not decrypt certain weights)

// # References
// - [CS03] Practical Verifiable Encryption and Decryption of Discrete Logarithms
// Jan Camenisch and Victor Shoup, CRYPTO 2003
// https://link.springer.com/content/pdf/10.1007/978-3-540-45146-4_8.pdf

// - [BBBPWM] Bulletproofs: Short Proofs for Confidential Transactions and More
// Benedikt Bünz, Jonathan Bootle, Dan Boneh, Andrew Poelstra, Pieter Wuille and Greg Maxwell, IEEE
// https://eprint.iacr.org/2017/1066.pdf

// # Implementation
// S&P 2018 validaity https://github.com/ZenGo-X/dlog-verifiable-enc
#[pallet::call_index(0)]
#[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::No))]
pub fn send_decrypted_weights(
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 461,
spec_version: 462,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
141 changes: 43 additions & 98 deletions scripts/python/builder.py
Original file line number Diff line number Diff line change
@@ -1,128 +1,88 @@
"""
Builds a genesis snapshot of current mainnet state
"""

import json
import argparse
import os
import codecs
from typing import Any
import logging

from substrateinterface.base import SubstrateInterface
from communex.client import CommuneClient

QUERY_URL = "wss://commune-api-node-1.communeai.net"
QUERY_URL = "wss://api.communeai.net"
STANDARD_MODULE = "SubspaceModule"

EXISTENTIAL_DEPOSIT = 500
MAX_NAME_LENGTH = 32

SUDO = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
SUDO = "5Dy6aBqv2MQEVpSAKqB147uQUZrAqK18JjFWs2jnzSXHn6Lh"

def query_map_values(
client: SubstrateInterface, module: str, storage_function: str, params: list = []
) -> dict:
logging.info(f"Querying {module}.{storage_function} with params {params}")
result = client.query_map(
module=module, storage_function=storage_function, params=params
)
return {k.value: v.value for k, v in result}
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')


def get_subnets(client: SubstrateInterface) -> dict[str, Any]:
def get_subnets(client: CommuneClient) -> dict[str, Any]:
logging.info("Fetching subnet information")
N = query_map_values(
client=client, storage_function="N", module=STANDARD_MODULE)
subnets: dict[Any, Any] = {"subnets": []}

subnet_names = query_map_values(
client=client, storage_function="SubnetNames", module=STANDARD_MODULE
)
founder_addys = query_map_values(
client=client, storage_function="Founder", module=STANDARD_MODULE
)

# due to potential name cutting we might repeat module names, which is not allowed.
subnets: dict[Any, Any] = {
"subnets": []
}

netuids = client.query_map("N", extract_value=False)["N"]
founder_addys = client.query_map_founder()
subnet_names = client.query_map_subnet_names()
stake_froms = client.query_map_stakefrom()

encountered_names = set()
for netuid in N:
for netuid in netuids:
logging.info(f"Processing subnet with netuid: {netuid}")
subnet = {
"name": subnet_names[netuid],
"founder": founder_addys[netuid],
"modules": [],
"modules": []
}
name = subnet_names[netuid]

keys = query_map_values(
client=client,
storage_function="Keys",
module=STANDARD_MODULE,
params=[netuid],
)
names = query_map_values(
client=client,
storage_function="Name",
module=STANDARD_MODULE,
params=[netuid],
)
addresses = query_map_values(
client=client,
storage_function="Address",
module=STANDARD_MODULE,
params=[netuid],
)
stake_froms = query_map_values(
client=client,
storage_function="StakeFrom",
module=STANDARD_MODULE,
params=[netuid],
)
keys = client.query_map_key(netuid=netuid)
names = client.query_map_name(netuid=netuid)
addresses = client.query_map_address(netuid=netuid)

for index, key in keys.items():
name = names[index][:MAX_NAME_LENGTH]
if name in encountered_names:
continue
encountered_names.add(name)

# Convert the list of tuples to a dictionary
stake_from_list = stake_froms.get(key, [])
stake_from_dict = {addr: amount for addr, amount in stake_from_list}

module = {
"key": key,
"name": name,
"address": addresses[index][:MAX_NAME_LENGTH],
"stake_from": {stake[0]: stake[1] for stake in stake_froms.get(key, 0)},
"stake_from": stake_from_dict
}
subnet["modules"].append(module)

subnets["subnets"].append(subnet)
return subnets


def get_balances(client: SubstrateInterface) -> dict[str, dict[str, int]]:
def get_balances(client: CommuneClient) -> dict[str, dict[str, int]]:
logging.info("Fetching account balances")
balances = query_map_values(
client=client, module="System", storage_function="Account", params=[]
)
result = {
k: v["data"]["free"]
for k, v in balances.items()
if v["data"]["free"] > EXISTENTIAL_DEPOSIT
}
return {"balances": result}
balances = client.query_map_balances()
result = {k: v["data"]["free"] for k, v in balances.items( # type: ignore
) if v["data"]["free"] > EXISTENTIAL_DEPOSIT} # type: ignore
return {"balances": result} # type: ignore


def get_code(client: SubstrateInterface) -> dict[str, str]:
def get_code(client: CommuneClient) -> dict[str, str]:
logging.info("Fetching code")
return {"code": str(client.query("Substrate", "Code"))}

return {"code": str(client.query(module="Substrate", name="Code"))}

def get_sudo(key: str) -> dict[str, str]:
return {"sudo": key}


def build_snap(
code: dict[str, str], balances: dict[str, dict[str, int]], subnets: dict[str, Any]
) -> dict[str, Any]:
def build_snap(code: dict[str, str], balances: dict[str, dict[str, int]], subnets: dict[str, Any]) -> dict[str, Any]:
"""
Returns:
snapshot spec with keys, in the following order:
Expand All @@ -137,35 +97,21 @@ def build_snap(
spec.update(subnets)
return spec


def main():
parser = argparse.ArgumentParser(
description="Generate a snapshot of balances and subnets."
)
parser.add_argument(
"-o",
"--output",
default="local.json",
help="Output file name (default: local.json)",
)
parser.add_argument(
"-d",
"--directory",
default=".",
help="Output directory (default: current directory)",
)
parser.add_argument(
"-c",
"--code",
default=False,
help="If the generated spec file should contain the mainnet runtime code (default: false)",
)
description="Generate a snapshot of balances and subnets.")
parser.add_argument("-o", "--output", default="local.json",
help="Output file name (default: local.json)")
parser.add_argument("-d", "--directory", default=".",
help="Output directory (default: current directory)")
parser.add_argument("-c", "--code", default=False,
help="If the generated spec file should contain the mainnet runtime code (default: false)")
args = parser.parse_args()

output_path = os.path.join(args.directory, args.output)

logging.info("Starting snapshot generation, might take up to 10 minutes.")
client = SubstrateInterface(QUERY_URL)
client = CommuneClient(QUERY_URL)
logging.info(f"Connected to {QUERY_URL}")

if args.code:
Expand All @@ -185,6 +131,5 @@ def main():

logging.info("Snapshot generation complete")


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

0 comments on commit 2d87795

Please sign in to comment.