Skip to content

Instantly share code, notes, and snippets.

@koxu1996
Last active May 14, 2024 10:06
Show Gist options
  • Save koxu1996/c53011eb69e22561aff76388b1190864 to your computer and use it in GitHub Desktop.
Save koxu1996/c53011eb69e22561aff76388b1190864 to your computer and use it in GitHub Desktop.
[Kairos] Test environment for periodic L1 synchronization
#!/usr/bin/env sh
set -e
CONTRACT_HASH_RAW="$1"
echo "> Calling 'batch' entrypoint."
DEPLOY_HASH=$(casper-client put-deploy \
--node-address http://127.0.0.1:11101/rpc \
--chain-name casper-net-1 \
--payment-amount 9991000000000 \
--secret-key /tmp/casper-users/user-1/secret_key.pem \
--session-hash $CONTRACT_HASH_RAW \
--session-entry-point "emit_batch_event" \
--session-version "1.0" | jq -r ".result.deploy_hash")
echo " - $DEPLOY_HASH"
echo
echo "> Waiting for execution."
max_retries=20
retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
raw_output=$(casper-client get-deploy \
--node-address http://127.0.0.1:11101/rpc \
$DEPLOY_HASH)
if ! echo "$raw_output" | jq -e . >/dev/null 2>&1; then
echo "Invalid JSON received:"
echo "$raw_output"
exit 1
fi
# Extract the data and compare
data=$(echo "$raw_output" | jq -r '.result.execution_results')
if [[ $data != "[]" ]]; then
break
fi
# If the result is still an empty array, increment the counter and wait
((retry_count++)) || true
echo "- attempt: $retry_count/$max_retries"
sleep 1
done
echo ""
echo "*-------*"
echo "| DONE! |"
echo "*-------*"
echo ""
#!/usr/bin/env sh
set -e
CONTRACT_HASH_RAW="$1"
echo "> Calling 'deposit' entrypoint..."
DEPLOY_HASH=$(casper-client put-deploy \
--node-address http://127.0.0.1:11101/rpc \
--chain-name casper-net-1 \
--payment-amount 9991000000000 \
--secret-key /tmp/casper-users/user-1/secret_key.pem \
--session-hash $CONTRACT_HASH_RAW \
--session-entry-point "emit_deposit_event" \
--session-version "1.0" | jq -r ".result.deploy_hash")
echo " - $DEPLOY_HASH"
echo
echo "> Waiting for execution."
max_retries=20
retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
raw_output=$(casper-client get-deploy \
--node-address http://127.0.0.1:11101/rpc \
$DEPLOY_HASH)
if ! echo "$raw_output" | jq -e . >/dev/null 2>&1; then
echo "Invalid JSON received:"
echo "$raw_output"
exit 1
fi
# Extract the data and compare
data=$(echo "$raw_output" | jq -r '.result.execution_results')
if [[ $data != "[]" ]]; then
break
fi
# If the result is still an empty array, increment the counter and wait
((retry_count++)) || true
echo "- attempt: $retry_count/$max_retries"
sleep 1
done
echo ""
echo "*-------*"
echo "| DONE! |"
echo "*-------*"
echo ""
#!/usr/bin/env sh
set -e
CONTRACT_HASH_RAW="$1"
echo "> Calling 'init' entrypoint."
DEPLOY_HASH=$(casper-client put-deploy \
--node-address http://127.0.0.1:11101/rpc \
--chain-name casper-net-1 \
--payment-amount 9991000000000 \
--secret-key /tmp/casper-users/user-1/secret_key.pem \
--session-hash $CONTRACT_HASH_RAW \
--session-entry-point "init" \
--session-version "1.0" | jq -r ".result.deploy_hash")
echo " - $DEPLOY_HASH"
echo
echo "> Waiting for execution."
max_retries=20
retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
raw_output=$(casper-client get-deploy \
--node-address http://127.0.0.1:11101/rpc \
$DEPLOY_HASH)
if ! echo "$raw_output" | jq -e . >/dev/null 2>&1; then
echo "Invalid JSON received:"
echo "$raw_output"
exit 1
fi
# Extract the data and compare
data=$(echo "$raw_output" | jq -r '.result.execution_results')
if [[ $data != "[]" ]]; then
break
fi
# If the result is still an empty array, increment the counter and wait
((retry_count++)) || true
echo "- attempt: $retry_count/$max_retries"
sleep 1
done
echo ""
echo "*-------*"
echo "| DONE! |"
echo "*-------*"
echo ""
#!/usr/bin/env sh
set -e
# Container cleanup.
if [ $(docker ps -q -f name=casper-nctl) ]; then
echo "> Stopping previous NCTL container."
docker stop casper-nctl
docker rm casper-nctl
fi
# Keys cleanup.
echo "> Removing wallet keys."
rm -rf /tmp/casper-users || true
# Contract cleanup.
echo "> Removing contract files."
rm -rf /tmp/l1-event-contract || true
rm /tmp/l1-event-contract.wasm || true
echo ""
echo "*-------*"
echo "| DONE! |"
echo "*-------*"
echo ""
#!/usr/bin/env sh
set -e
# Fetching NCTL image.
echo "> Downloading NCTL image."
docker pull makesoftware/casper-nctl:v156
# Creating smart contract at `/tmp/l1-event-contract.wasm`.
(
echo "> Preparing empty project."
cd /tmp
cargo new --bin l1-event-contract
cd /tmp/l1-event-contract
echo "> Configure wasm32-unknown-unknown target."
mkdir ./.cargo
cat << EOF >> ./.cargo/config.toml
[build]
target = "wasm32-unknown-unknown"
EOF
rustup target add wasm32-unknown-unknown
echo "> Add basic dependencies."
cargo add casper-event-standard@0.4.1
cargo add casper-contract@4.0.0 --no-default-features
cargo add casper-types@4.0.1 --no-default-features
echo "> Setting up code."
cat << EOF > ./src/main.rs
#![no_main]
use casper_contract::contract_api::{runtime, storage};
use casper_event_standard::{Event, Schemas};
use casper_types::{CLType, EntryPoint, EntryPointAccess, EntryPointType, EntryPoints};
#[derive(Event)]
struct DepositEvent {
amount: u64,
}
#[derive(Event)]
struct BatchEvent {
dummy: bool,
id: u64,
}
#[no_mangle]
pub fn init() {
let schemas = Schemas::new()
.with::<DepositEvent>()
.with::<BatchEvent>();
casper_event_standard::init(schemas);
}
#[no_mangle]
pub fn emit_deposit_event() {
let event = DepositEvent { amount: 1000 };
casper_event_standard::emit(event);
}
#[no_mangle]
pub fn emit_batch_event() {
let event = BatchEvent {
dummy: false,
id: 7,
};
casper_event_standard::emit(event);
}
#[no_mangle]
pub extern "C" fn call() {
let mut entry_points = EntryPoints::new();
entry_points.add_entry_point(EntryPoint::new(
"init",
Vec::new(),
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract,
));
entry_points.add_entry_point(EntryPoint::new(
"emit_deposit_event",
Vec::new(),
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract,
));
entry_points.add_entry_point(EntryPoint::new(
"emit_batch_event",
Vec::new(),
CLType::Unit,
EntryPointAccess::Public,
EntryPointType::Contract,
));
storage::new_locked_contract(entry_points, None, Some("l1-event-contract-hash".to_string()), None);
}
EOF
echo "> Compiling."
cargo build --release
echo "> Postprocessing WASM."
wasm-opt --strip-debug --signext-lowering ./target/wasm32-unknown-unknown/release/l1-event-contract.wasm -o /tmp/l1-event-contract.wasm
)
echo ""
echo "*-------*"
echo "| DONE! |"
echo "*-------*"
echo ""
#!/usr/bin/env sh
set -e
echo "> Running NCTL container."
docker run -d --rm -it --name casper-nctl -p 11101:11101 makesoftware/casper-nctl:v156
echo
sleep 60
echo "> Copying user keys."
docker cp casper-nctl:/home/casper/casper-node/utils/nctl/assets/net-1/users /tmp/casper-users
echo
echo "> Deploying session code."
RESULT=$(casper-client put-deploy \
--id 1 \
--node-address http://127.0.0.1:11101/rpc \
--chain-name casper-net-1 \
--payment-amount 333000000000 \
--secret-key /tmp/casper-users/user-1/secret_key.pem \
--session-path /tmp/l1-event-contract.wasm
)
DEPLOY_HASH=$(echo $RESULT | jq -r ".result.deploy_hash")
echo "- hash: $DEPLOY_HASH"
echo
echo "> Waiting for execution."
max_retries=10
retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
raw_output=$(casper-client get-deploy \
--node-address http://127.0.0.1:11101/rpc \
$DEPLOY_HASH)
if ! echo "$raw_output" | jq -e . >/dev/null 2>&1; then
echo "Invalid JSON received:"
echo "$raw_output"
exit 1
fi
# Extract the data and compare
data=$(echo "$raw_output" | jq -r '.result.execution_results')
if [[ $data != "[]" ]]; then
break
fi
# If the result is still an empty array, increment the counter and wait
((retry_count++)) || true
echo "- attempt: $retry_count/$max_retries"
sleep 3
done
echo "> Getting state root hash."
STATE_ROOT_HASH=$(casper-client get-state-root-hash --node-address http://127.0.0.1:11101/rpc | jq -r ".result.state_root_hash")
echo " - $STATE_ROOT_HASH"
echo
echo "> Getting account hash."
ACCOUNT_HASH=$(casper-client account-address --public-key /tmp/casper-users/user-1/public_key.pem)
ACCOUNT_HASH_RAW=$(echo $ACCOUNT_HASH | awk -F"-" '{print $3}')
echo " - $ACCOUNT_HASH_RAW"
echo
echo "> Getting contract hash."
CONTRACT_HASH=$(casper-client query-global-state \
--node-address http://127.0.0.1:11101/rpc \
--state-root-hash $STATE_ROOT_HASH \
--key account-hash-$ACCOUNT_HASH_RAW \
-q "l1-event-contract-hash" | jq -r ".result.stored_value.ContractPackage.versions[0].contract_hash")
CONTRACT_HASH_RAW=$(echo $CONTRACT_HASH | awk -F"-" '{print $2}')
echo " - $CONTRACT_HASH_RAW"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment