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

My/pay as bid #3

Merged
merged 23 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 18 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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NODE_URL=ws://127.0.0.1
NODE_PORT=9944
WORKER_URL=wss://127.0.0.1
TRUSTED_WORKER_PORT=2000
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
venv/**

# user data
my_keystore/**
my_keystore/**

# ignore the .env file
.env
165 changes: 152 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,179 @@
# Integritee Python Client
Use Pyo3 Lib to wrap the integritee-cli into a python lib.

## Getting Started
Use `Pyo3` Lib to wrap the `integritee-cli` into a Python lib.

## Installation

Get started with the project setup and initial configuration.

- ### Prerequisites

In order to use the features, the nightly version of the compiler has
to be setup beforehand:

```
$ rustup override set nightly
```

- ### Updating Git Submodules

`Cargo.toml` states the relative dependencies at the `shared` folder.
The submodules have to be initialized and updated before building:

```
git submodule update --init --recursive
$ git submodule update --init --recursive
```

Install `integritee_cli_py` as lip into virtual environment:
- ### Creating and Activating venv

Install `integritee_cli_py` as lib into virtual environment:

```
python -m venv venv/
source venv/bin/activate
$ python -m venv venv/
$ source venv/bin/activate
# unset conda_prefix - if existing - for maturin to work
conda deactivate
$ conda deactivate
```

- ### Managing Environment Variables

Please follow these steps to set up and use environment variables:

1. Copy the `.env.example` file and rename it to `.env`:

```bash
$ cp .env.example .env
```
pip install maturin

2. Open the `.env` file and provide values for the environment variables.

- ### Installing Dependencies

Install the necessary dependencies using the provided `requirements.txt` file:

```bash
$ pip install -r requirements.txt
```

- ### Using maturin

To install the lib to your current python environment use:

```
maturin develop
$ maturin develop
```
In order to use the features, the nightly version of the compiler has
to be setup beforehand: `rustup override set nightly`

## Run as Command Line Tool

Test import and execution of `integritee_cli_py` in python:

```
$ python run_integritee_cli.py --command <COMMAND-NAME> --params <PARAMETER-NAME>
```

The `integritee-cli-py` provides the following CLI commands:

- ### Get Help


**Syntax:**

```bash
$ python3 run_integritee_cli.py --help
```

**Output:**

```bash

usage: run_integritee_cli.py [-h] [--command {new_account_cmd,new_trusted_account_cmd,pay_as_bid_cmd,get_market_results_cmd,pay_as_bid_proof_cmd,verify_proof_cmd}] [--params PARAMS [PARAMS ...]]

Run Rust CLI with specific commands

options:
-h, --help show this help message and exit
--command {new_account_cmd,new_trusted_account_cmd,pay_as_bid_cmd,get_market_results_cmd,pay_as_bid_proof_cmd,verify_proof_cmd}
Please specify the command to run
--params PARAMS [PARAMS ...]
Parameters for the command
```

- ### Create a new account:

**Syntax:**

```bash
$ python3 run_integritee_cli.py --command new_account_cmd
```
python run_integritee_cli.py

- ### Create a new trusted account:

**Syntax:**

```bash
$ python3 run_integritee_cli.py --command new_trusted_account_cmd --params <MRENCLAVE>
```

- ### Pay As Bid:

**Syntax:**

```bash
$ python3 run_integritee_cli.py --command pay_as_bid_cmd --params <MRENCLAVE> <ACCOUNT> <ORDERS_STRING>
```

**Example:**

```bash
$ python3 run_integritee_cli.py --command pay_as_bid_cmd --params 9PPeGELLdD9Uw1mVJbUGTeRpGzPBGb1bdEk6TCL4pPCE 5Dsni69ozXZZwpxyCGjLq8KQnBpGrtPnbykepgst2Tbh7NuY "[{\"id\":0,\"order_type\":\"ask\",\"time_slot\":\"2022-10-04T05:06:07+00:00\",\"actor_id\":\"actor_0\",\"cluster_index\":0,\"energy_kwh\":5,\"price_euro_per_kwh\":0.19},{\"id\":1,\"order_type\":\"bid\",\"time_slot\":\"2022-10-04T05:06:07+00:00\",\"actor_id\":\"actor_1\",\"cluster_index\":0,\"energy_kwh\":8.8,\"price_euro_per_kwh\":0.23}]"
```

- ### Get Market Results:

**Syntax:**

```bash
$ python3 run_integritee_cli.py --command get_market_results_cmd --params <MRENCLAVE> <ACCOUNT> <TIMESTAMP>
```

**Example:**

```bash
$ python3 run_integritee_cli.py --command get_market_results_cmd --params 9PPeGELLdD9Uw1mVJbUGTeRpGzPBGb1bdEk6TCL4pPCE 5Dsni69ozXZZwpxyCGjLq8KQnBpGrtPnbykepgst2Tbh7NuY 2022-10-04T05:06:07+00:00
```

- ### Get Bid Proof:

**Syntax:**

```bash
$ python3 run_integritee_cli.py --command pay_as_bid_proof_cmd --params <MRENCLAVE> <ACCOUNT> <TIMESTAMP> <ACTOR_ID>
```

**Example:**

```bash
$ python3 run_integritee_cli.py --command pay_as_bid_proof_cmd --params 9PPeGELLdD9Uw1mVJbUGTeRpGzPBGb1bdEk6TCL4pPCE 5Dsni69ozXZZwpxyCGjLq8KQnBpGrtPnbykepgst2Tbh7NuY 2022-10-04T05:06:07+00:00 actor_0
```

- ### Verify Proof:

**Syntax:**

```bash
$ python3 run_integritee_cli.py --command verify_proof_cmd --params <MRENCLAVE> <ACCOUNT> <MERKLE_PROOF_JSON>
```

**Example:**

```bash
python3 run_integritee_cli.py --command verify_proof_cmd --params 9PPeGELLdD9Uw1mVJbUGTeRpGzPBGb1bdEk6TCL4pPCE 5Dsni69ozXZZwpxyCGjLq8KQnBpGrtPnbykepgst2Tbh7NuY "{\"root\":\"0xeae9131721b25db95622605c99a62f56f2e3b47e7f54f9b0653055b11b8d37b8\",\"proof\":[\"0xf147000d21d56a303b688da5bf1294d865518a8fb889af48ca21d12af5a6d823\"],\"number_of_leaves\":2,\"leaf_index\":0,\"leaf\":[0,0,0,0,0,0,0,0,1,100,50,48,50,50,45,49,48,45,48,52,84,48,53,58,48,54,58,48,55,43,48,48,58,48,48,28,97,99,116,111,114,95,48,1,0,0,0,0,0,0,0,0,0,0,20,64,82,184,30,133,235,81,200,63]}"
```

## Troubleshooting

* **wasmtime**: error[E0463]: can't find crate for `std`
- **wasmtime**: error[E0463]: can't find crate for `std`

```
rustup target add --toolchain nightly wasm32-unknown-unknown
```
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python-dotenv
argparse
maturin
82 changes: 76 additions & 6 deletions run_integritee_cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,81 @@
import os
from dotenv import load_dotenv
import argparse
import sys
import integritee_cli_py

node_url = "ws://127.0.0.1"
node_port = "9944"
worker_url = "wss://127.0.0.1"
trusted_worker_port = "2000"

# Load environment variables from .env file
load_dotenv()

# Access environment variables
NODE_URL = os.getenv("NODE_URL")
NODE_PORT = os.getenv("NODE_PORT")
WORKER_URL = os.getenv("WORKER_URL")
TRUSTED_WORKER_PORT = os.getenv("TRUSTED_WORKER_PORT")

# Mapping commands to their required parameter names
COMMAND_PARAMETER_INFO = {
"new_account_cmd": ([], 0),
"new_trusted_account_cmd": (
[
"mrenclave",
],
1,
),
"pay_as_bid_cmd": (["mrenclave", "account", "orders_string"], 3),
"get_market_results_cmd": (["mrenclave", "account", "timestamp"], 3),
"pay_as_bid_proof_cmd": (["mrenclave", "account", "timestamp", "actor_id"], 4),
"verify_proof_cmd": (["mrenclave", "account", "merkle_proof_json"], 3),
}


def validate_parameters(command, params):
required_params, required_count = COMMAND_PARAMETER_INFO.get(command, ([], 0))
if len(params) != required_count:
missing_params = required_count - len(params)
param_names = ", ".join(required_params)
raise argparse.ArgumentError(
None,
f"{command} requires {missing_params} more parameter(s): {param_names}",
)


if __name__ == "__main__":
integritee_cli_py.run_cli(node_url, node_port, worker_url, trusted_worker_port, "balanced")
print("TERMINATE")
parser = argparse.ArgumentParser(description="Run Rust CLI with specific commands")
parser.add_argument(
"--command",
type=str,
choices=COMMAND_PARAMETER_INFO.keys(),
help="Please specify the command to run",
)

parser.add_argument(
"--params",
nargs="+",
default=[],
help="Parameters for the command",
)

args = parser.parse_args()

if not args.command:
parser.print_help()
sys.exit(1)

try:
validate_parameters(args.command, args.params)

except argparse.ArgumentError as e:
print(str(e))
parser.print_help()
sys.exit(1)

try:
command_name = args.command
params = args.params
integritee_cli_py.run_cli(
NODE_URL, NODE_PORT, WORKER_URL, TRUSTED_WORKER_PORT, command_name, params
)
except Exception as e:
print("Encountered an error:", e)
Loading