Skip to content

Commit

Permalink
Update Raito client script (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus authored Sep 9, 2024
1 parent 6ee3fca commit 1ac9523
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ Cargo.lock
**/.vscode

.env
.env.*
.venv
.python-version
__pycache__

.light_client
.raito
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2024_07"
[scripts]
regenerate_tests= "./scripts/data/regenerate_tests.sh"
integration_tests = "scarb build && ./scripts/data/integration_tests.sh"
light_client= "scarb build && ./scripts/data/light_client.sh"
client= "scarb build && ./scripts/data/client.sh"
test = "scarb cairo-test && scarb run integration_tests"

[dependencies]
Expand Down
50 changes: 50 additions & 0 deletions scripts/data/client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

set -e;
set -o pipefail;

base_dir=".raito"

start=${1:-0}
end=${2:-100}
step=${3:-1}
mode=${4:-"light"}
strategy=${5:-"sequential"}

mkdir $base_dir || true

run_client() {
local initial_height=$1
local num_blocks=$2

first=$((initial_height+1))
second=$((initial_height+num_blocks))
echo -n "Running $mode client on blocks $first$second ..."

batch_file=${base_dir}/${mode}_${initial_height}_${num_blocks}.json
if [ ! -f "$batch_file" ]; then
python scripts/data/generate_data.py $mode $initial_height $num_blocks true $batch_file
fi

arguments=$(python scripts/data/format_args.py $batch_file)
output=$(scarb cairo-run --no-build --function test "$arguments")
if [[ "$output" == *"FAIL"* ]]; then
echo " fail"
echo $output
exit 1
else
echo " ok"
fi
}

for (( height=start; height<end; height+=step )); do
if [[ "$strategy" == "sequential" ]]; then
run_client $height $step
elif [[ "$strategy" == "random" ]]; then
random_height=$(( RANDOM * 25 + RANDOM % 25 ))
run_client $random_height 1
else
echo "Unsupported strategy"
exit 1
fi
done
10 changes: 5 additions & 5 deletions scripts/data/format_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ def append_obj(obj, to):

def format_cairo1_run(args: list) -> str:
"""Formats arguments for usage with cairo1-run.
Example: [0, 1, [2, 3, 4]] -> "0 1 [2 3 4]"
Example: [0, 1, [2, 3, 4]] -> "[0 1 [2 3 4]]"
:param args: Python object containing already processed arguments.
:return: Removes outer array brackets [] and commas, returns string.
:return: Returns string with removed commas.
"""
def format_item(item, root=False):
def format_item(item):
if isinstance(item, list):
arr = " ".join(map(format_item, item))
return arr if root else f'[{arr}]'
return f'[{arr}]'
else:
return str(item)
return format_item(args, root=True)
return format_item(args)


def format_args():
Expand Down
11 changes: 6 additions & 5 deletions scripts/data/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ def request_rpc(method: str, params: list):
:return: parsed JSON result as Python object
"""
url = BITCOIN_RPC or DEFAULT_URL
auth = USERPWD.split(":") if USERPWD else None
auth = tuple(USERPWD.split(":")) if USERPWD else None
headers = {'content-type': 'application/json'}
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": 0,
}
data = requests.post(url, auth=auth, headers=headers, json=payload).json()
if data['result'] is None:
raise ConnectionError("RPC response is null")
return data['result']
res = requests.post(url, auth=auth, headers=headers, json=payload)
try:
return res.json()['result']
except Exception:
raise ConnectionError(f"Unexpected RPC response:\n{res.text}")


def fetch_chain_state(block_height: int):
Expand Down
9 changes: 5 additions & 4 deletions scripts/data/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ failures=()
test_files="tests/data"/*

ignored_files=(
"tests/data/light_2015.json"
"tests/data/light_481823.json"
"tests/data/light_709631.json"
"tests/data/full_757738.json"
Expand All @@ -36,14 +37,14 @@ for test_file in $test_files; do
output=$(scarb cairo-run --no-build --function test "$arguments")
gas_spent=$(echo $output | grep -o 'gas_spent=[0-9]*' | sed 's/gas_spent=//')

if [[ "$output" == *"OK"* ]]; then
echo -e "${GREEN} ok ${RESET}(gas usage est.: $gas_spent)"
num_ok=$((num_ok + 1))
elif [[ "$output" == *"FAIL"* ]]; then
if [[ "$output" == *"FAIL"* ]]; then
echo -e "${RED} fail ${RESET}(gas usage est.: $gas_spent)"
num_fail=$((num_fail + 1))
error=$(echo $output | grep -o "error='[^']*'" | sed "s/error=//")
failures+="\te2e:$test_file — Panicked with $error\n"
elif [[ "$output" == *"OK"* ]]; then
echo -e "${GREEN} ok ${RESET}(gas usage est.: $gas_spent)"
num_ok=$((num_ok + 1))
else
echo -e "${RED} fail ${RESET}(gas usage est.: 0)"
num_fail=$((num_fail + 1))
Expand Down
20 changes: 0 additions & 20 deletions scripts/data/light_client.sh

This file was deleted.

1 change: 1 addition & 0 deletions scripts/data/regenerate_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fi

light_test_cases=(
169 # Block containing first P2P tx to Hal Finney (170)
2015
24834 # Block containing first off ramp tx from Martti Malmi (24835)
57042 # Block containing pizza tx (57043)
150012 # Small Block (150013)
Expand Down
56 changes: 56 additions & 0 deletions tests/data/light_2015.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"chain_state": {
"block_height": 2015,
"total_work": "8658786191328",
"best_block_hash": "00000000693067b0e6b440bc51450b9f3850561b07f6d3c021c54fbd6abb9763",
"current_target": "26959535291011309493156476344723991336010898738574164086137773096960",
"epoch_start_time": 1231006505,
"prev_timestamps": [
1233051397,
1233051777,
1233052325,
1233053576,
1233055146,
1233056326,
1233057128,
1233058577,
1233060081,
1233061610,
1233061996
]
},
"blocks": [
{
"header": {
"version": 1,
"time": 1233063531,
"bits": 486604799,
"nonce": 790229043
},
"data": {
"variant_id": 0,
"merkle_root": "572c6d6b54dda72004df004a95575a2b772acd8876e58f8c81c8a9cdae70e4ac"
}
}
],
"expected": {
"block_height": 2016,
"total_work": "8663081224161",
"best_block_hash": "00000000a141216a896c54f211301c436e557a8d55900637bbdce14c6c7bddef",
"current_target": "26959535291011309493156476344723991336010898738574164086137773096960",
"epoch_start_time": 1233063531,
"prev_timestamps": [
1233051777,
1233052325,
1233053576,
1233055146,
1233056326,
1233057128,
1233058577,
1233060081,
1233061610,
1233061996,
1233063531
]
}
}

0 comments on commit 1ac9523

Please sign in to comment.