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

Update streamlit app config #35

Merged
merged 18 commits into from
Sep 16, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ cython_debug/
#.idea/

.streamlit/secrets.toml
.devcontainer
2 changes: 1 addition & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tasks:
source /home/gitpod/.bashrc && export PATH=$PATH:/home/gitpod/.nilup/sdks/latest
nillion -V
pip install -r requirements.txt
nada build
nada build --mir-json
echo "✅ Compiled all programs to binary (.nada.bin) in target/ directory"
echo "🖥️ Check out Nada programs in src/ directory"
echo "🧪 Check out Nada program test files in tests/ directory"
Expand Down
9 changes: 8 additions & 1 deletion .streamlit/secrets.toml.example
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
nilchain_private_key = "YOUR_PRIVATE_KEY"
nilchain_private_key = "YOUR_PRIVATE_KEY"

# optionally add custom Nillion Network params here

# cluster_id = ""
# grpc_endpoint = ""
# chain_id = ""
# bootnode = ""
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nada run [test-name]
2. Run a demo

```
streamlit run streamlit.py [test-name]
streamlit run streamlit_app.py [test-name]
```

### Local Installation Instructions
Expand Down Expand Up @@ -55,13 +55,13 @@ pip install -r requirements.txt
nilup instrumentation enable --wallet <your-eth-wallet-address>
```

### Build (compile) all Nada programs
### Build (compile) all Nada programs with the --mir-json flag

```
nada build
nada build --mir-json
```

This creates one compiled binary (.nada.bin) file per program listed in the `nada-project.toml` file in the target/ directory.
This creates one compiled binary (.nada.bin) file and one json file (.nada.json) per program listed in the `nada-project.toml` file in the target/ directory. The json file is needed to get inputs, outputs, types, and parties for any Streamlit demo apps.

### Test a Nada program

Expand Down Expand Up @@ -129,11 +129,11 @@ cp .streamlit/secrets.toml.example .streamlit/secrets.toml
### 3. Run the `addition` test with starter values from `addition_test`

```
streamlit run streamlit.py addition_test
streamlit run streamlit_app.py addition_test
```

### 4. Run any other example that isn't using na.numpy or loops to define input and output parties

```
streamlit run streamlit.py [test-name]
streamlit run streamlit_app.py [test-name]
```
7 changes: 0 additions & 7 deletions demos/addition_test_demo.py

This file was deleted.

110 changes: 68 additions & 42 deletions nillion_client_script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import py_nillion_client as nillion
import uuid
import os

from py_nillion_client import NodeKey, UserKey
from nillion_python_helpers import get_quote_and_pay, create_nillion_client, create_payments_config
Expand All @@ -10,13 +11,32 @@
from cosmpy.crypto.keypairs import PrivateKey

# Nillion Testnet Config: https://docs.nillion.com/network-configuration#testnet
cluster_id = 'b13880d3-dde8-4a75-a171-8a1a9d985e6c'
grpc_endpoint = '65.109.228.73:9090'
chain_id = 'nillion-chain-testnet-1'
bootnode = '/dns/node-1.testnet-photon.nillion-network.nilogy.xyz/tcp/14111/p2p/12D3KooWCfFYAb77NCjEk711e9BVe2E6mrasPZTtAjJAPtVAdbye'
bootnodes = [bootnode]

async def store_inputs_and_run_blind_computation(input_data, program_name, output_parties, nilchain_private_key):
nillion_testnet_default_config = {
"cluster_id": 'b13880d3-dde8-4a75-a171-8a1a9d985e6c',
"grpc_endpoint": 'https://testnet-nillion-grpc.lavenderfive.com',
"chain_id": 'nillion-chain-testnet-1',
"bootnodes": ['/dns/node-1.testnet-photon.nillion-network.nilogy.xyz/tcp/14111/p2p/12D3KooWCfFYAb77NCjEk711e9BVe2E6mrasPZTtAjJAPtVAdbye']
}

async def store_inputs_and_run_blind_computation(
input_data,
program_name,
output_parties,
nilchain_private_key,
compiled_nada_program_path=None,
cluster_id=None,
grpc_endpoint=None,
chain_id=None,
bootnodes=None,
should_store_inputs=False
):

# Set fallback values if params are None
cluster_id = cluster_id or nillion_testnet_default_config["cluster_id"]
grpc_endpoint = grpc_endpoint or nillion_testnet_default_config["grpc_endpoint"]
chain_id = chain_id or nillion_testnet_default_config["chain_id"]
bootnodes = bootnodes or nillion_testnet_default_config["bootnodes"]

# Create Nillion Client for user
seed=str(uuid.uuid4())
userkey = UserKey.from_seed(f"nada-by-example-{seed}")
Expand All @@ -29,7 +49,12 @@ async def store_inputs_and_run_blind_computation(input_data, program_name, outpu

# Pay for and store the program
# Set the program name and path to the compiled program
program_mir_path = f"./target/{program_name}.nada.bin"
# Convert the relative path to an absolute path
if compiled_nada_program_path is None:
compiled_nada_program_path = os.path.abspath(os.path.join("target", f"{program_name}.nada.bin"))

print(f"compiled nada program: {compiled_nada_program_path}")

# Create payments config, client and wallet
payments_config = create_payments_config(chain_id, grpc_endpoint)
payments_client = LedgerClient(payments_config)
Expand All @@ -50,7 +75,7 @@ async def store_inputs_and_run_blind_computation(input_data, program_name, outpu
memo_store_program = f"petnet operation: store_program; program_name: {program_name}; user_id: {user_id}"
receipt_store_program = await get_quote_and_pay(
client,
nillion.Operation.store_program(program_mir_path),
nillion.Operation.store_program(compiled_nada_program_path),
payments_wallet,
payments_client,
cluster_id,
Expand All @@ -60,7 +85,7 @@ async def store_inputs_and_run_blind_computation(input_data, program_name, outpu

# Store the program
program_id = await client.store_program(
cluster_id, program_name, program_mir_path, receipt_store_program
cluster_id, program_name, compiled_nada_program_path, receipt_store_program
)

print(program_id)
Expand All @@ -71,7 +96,9 @@ async def store_inputs_and_run_blind_computation(input_data, program_name, outpu
compute_bindings.add_output_party(party_name, party_id)

store_ids=[]
# Store each input
compute_time_values = {}

# Iterate through each input to create a secret
for input_name, details in input_data.items():
value, party_name, input_type = details
print(f"Processing Input: {input_name}, Value: {value}, Party: {party_name}, Type: {input_type}")
Expand All @@ -89,40 +116,40 @@ async def store_inputs_and_run_blind_computation(input_data, program_name, outpu
'PublicBoolean': nillion.Boolean,
}

nada_val = types_mapping.get(input_type, nillion.Integer)(value)

new_secret = nillion.NadaValues(
{
input_name: nada_val
}
)

memo_store_values = f"petnet operation: store_values; name: {input_name}; user_id: {user_id}"
receipt_store = await get_quote_and_pay(
client,
nillion.Operation.store_values(new_secret, ttl_days=5),
payments_wallet,
payments_client,
cluster_id,
memo_store_values,
)
print(f"🧾 RECEIPT MEMO: {memo_store_values}")

# Set permissions for the client to compute on the program
permissions = nillion.Permissions.default_for_user(client.user_id)
permissions.add_compute_permissions({client.user_id: {program_id}})
# Add compute bindings for secret
compute_bindings.add_input_party(party_name, party_id)

store_id = await client.store_values(
cluster_id, new_secret, permissions, receipt_store
)
print(input_name, store_id)
store_ids.append(store_id)
print(store_ids)
# Create secret
nada_val = types_mapping.get(input_type, nillion.Integer)(value)

await asyncio.sleep(1) # Simulate async delay for each input
if should_store_inputs:
new_secret = nillion.NadaValues({
input_name: nada_val
})
memo_store_values = f"petnet operation: store_values; name: {input_name}; user_id: {user_id}"
receipt_store = await get_quote_and_pay(
client,
nillion.Operation.store_values(new_secret, ttl_days=5),
payments_wallet,
payments_client,
cluster_id,
memo_store_values,
)
print(f"🧾 RECEIPT MEMO: {memo_store_values}")

# Set permissions for the client to compute on the program
permissions = nillion.Permissions.default_for_user(client.user_id)
permissions.add_compute_permissions({client.user_id: {program_id}})

store_id = await client.store_values(
cluster_id, new_secret, permissions, receipt_store
)
store_ids.append(store_id)
else:
# Add the secret to compute_time_values instead of storing it
compute_time_values[input_name] = nada_val

computation_time_secrets = nillion.NadaValues({})
computation_time_secrets = nillion.NadaValues(compute_time_values)

memo_compute = f"petnet operation: compute; program_id: {program_id}; user_id: {user_id}"
# Pay for the compute
Expand Down Expand Up @@ -160,4 +187,3 @@ async def store_inputs_and_run_blind_computation(input_data, program_name, outpu
'output': blind_computation_results,
'nillion_address': payments_wallet,
}

Loading
Loading