Skip to content

Commit

Permalink
Merge branch 'maciej-upgradetest' into 'master'
Browse files Browse the repository at this point in the history
feat(icp_ledger): update upgrade testing scripts to work with the new testnet infrastructure and add tests for icrc2 endpoints

I also updated the guide: https://www.notion.so/dfinityorg/Test-ICP-Ledger-Archive-Upgrade-1176e13ae416424a80ed19e186af4a74 

See merge request dfinity-lab/public/ic!15429
  • Loading branch information
maciejdfinity committed Nov 1, 2023
2 parents 25b42af + 04378bd commit a4b46b2
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 23 deletions.
8 changes: 1 addition & 7 deletions rs/rosetta-api/scripts/get_blocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ LEDGER_CANISTER_ID="ryjl3-tyaaa-aaaaa-aaaba-cai"
ARCHIVE_CANISTER_ID="qjdve-lqaaa-aaaaa-aaaeq-cai"
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
TMP_DIR="$SCRIPT_DIR/tmp" # need a persistent directory from within dev container
INFO_FILE=$(find "$TMP_DIR" -iname "*.sourceme")
ROSETTA_DATA_DIR_PARENT="$TMP_DIR/rosetta_data"

#shellcheck source=/dev/null
source "$INFO_FILE"

source "$SCRIPT_DIR/init_identity.sh"
init_identity # initialize the dfx identity
HOME="${DFX_HOME:-$HOME}"

# the directory used to store the ledger blocks is
# the hash of the ledger and the archive nodes
Expand Down
21 changes: 11 additions & 10 deletions rs/rosetta-api/scripts/run_upgrade_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,35 @@ set -euo pipefail
set -x

function help {
echo >&2 "Usage: $0 <test_net> <commit_id> (archive|ledger) <arg>?"
echo >&2 "Usage: $0 <commit_id> (icp-ledger-archive|ledger) <arg>?"
}

if (($# != 3)) && (($# != 4)); then
if (($# != 2)) && (($# != 3)); then
help
exit 1
fi

if [ "$3" != "archive" ] && [ "$3" != "ledger" ]; then
if [ "$2" != "icp-ledger-archive" ] && [ "$2" != "ledger" ]; then
help
exit 2
fi

rm -rf candid && git clone https://github.com/dfinity/candid.git && cd candid && cargo build && cd target/debug && sudo cp didc /ic/bin/ && sudo chmod +x /ic/bin/didc && cd ~
sudo apt update && sudo apt install sqlite3 xxd

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
TEST_NET="$1"
COMMIT_ID="$2"
LEDGER_OR_ARCHIVE="$3"
ARG="${4:-}"
COMMIT_ID="$1"
LEDGER_OR_ARCHIVE="$2"
ARG="${3:-}"

"$SCRIPT_DIR/setup_testnet.sh" "$TEST_NET"
ROSETTA_DB_OLD=$("$SCRIPT_DIR/get_blocks.sh" | tail -n1)
if [ -z "$ARG" ]; then
"$SCRIPT_DIR/submit_proposal.sh" "$COMMIT_ID" "$LEDGER_OR_ARCHIVE"
"$SCRIPT_DIR/../../../testnet/tools/nns-tools/test-canister-upgrade.sh" "$LEDGER_OR_ARCHIVE" "$COMMIT_ID"
else
"$SCRIPT_DIR/submit_proposal.sh" "$COMMIT_ID" "$LEDGER_OR_ARCHIVE" "$ARG"
"$SCRIPT_DIR/../../../testnet/tools/nns-tools/test-canister-upgrade.sh" "$LEDGER_OR_ARCHIVE" "$COMMIT_ID" "$ARG"
fi
ROSETTA_DB_NEW=$("$SCRIPT_DIR/get_blocks.sh" | tail -n1)
"$SCRIPT_DIR/diff_rosetta_data.sh" "$ROSETTA_DB_OLD" "$ROSETTA_DB_NEW"
"$SCRIPT_DIR/test_transfers.sh"
"$SCRIPT_DIR/test_transfer_from.sh"
"$SCRIPT_DIR/test_approve.sh"
76 changes: 76 additions & 0 deletions rs/rosetta-api/scripts/test_approve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bash

# Submit an upgrade proposal to the NNS.

set -euo pipefail
#set -x

if (($# != 0)); then
echo >&2 "Usage: $0"
exit 1
fi

LEDGER_CANISTER_ID="ryjl3-tyaaa-aaaaa-aaaba-cai"
ARCHIVE_CANISTER_ID="qjdve-lqaaa-aaaaa-aaaeq-cai"
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
LEDGER_DID="$SCRIPT_DIR/../icp_ledger/ledger.did"
TMP_DIR="$SCRIPT_DIR/tmp" # need a persistent directory from within dev container
NUM_OF_BATCHES=50
NUM_OF_APPROVALS_PER_BATCH=21 # 100 fails for some reason related to the testnet setup
HOME="${DFX_HOME:-$HOME}"

NNS_TEST_ID="nns_test_user_dfx_identity"
dfx identity use "$NNS_TEST_ID"

SPENDER_1_PRINCIPAL="pcwbg-y26mf-k62dw-7xo2m-jfv2n-un56b-pjkp7-oirek-obdjm-bklck-2ae"
SPENDER_2_PRINCIPAL="tcpbp-bppbk-ac6zu-smi7p-opmky-qjuez-ugxzv-v33ci-vod7y-kpktd-kae"
SPENDER_3_PRINCIPAL="xnrq6-gadm3-6ious-kx6kn-utuqf-iphoq-vrag4-rlfvt-5z7n7-52n4q-vae"

query_blocks_res=$(dfx canister --network $NNS_URL call --candid "$LEDGER_DID" --query "$LEDGER_CANISTER_ID" query_blocks '(record {start=0:nat64;length=1:nat64})')
BEFORE_CHAIN_LENGTH=$(echo $query_blocks_res | sed -n 's/.*chain_length *= *\([^ ]\+\).*/\1/p' | tr -d '_')
BEFORE_FIRST_BLOCK_INDEX=$(echo $query_blocks_res | sed -n 's/.*first_block_index *= *\([^ ]\+\).*/\1/p' | tr -d '_')

dfx canister call --candid "$LEDGER_DID" --network "$NNS_URL" "$LEDGER_CANISTER_ID" icrc2_approve "(record {spender=record {owner=principal \"$SPENDER_2_PRINCIPAL\"}; amount=200:nat},)"

EXPIRATION=2993656999000000000 # expiration far in the future

for ((batch = 0; batch < $NUM_OF_BATCHES; batch++)); do
echo "start sending batch $((batch + 1))/$NUM_OF_BATCHES of $NUM_OF_APPROVALS_PER_BATCH txs"
for ((t = 0; t < $NUM_OF_APPROVALS_PER_BATCH; t++)); do
case $(($t % 3)) in

0)
dfx canister call --candid "$LEDGER_DID" --network "$NNS_URL" "$LEDGER_CANISTER_ID" icrc2_approve "(record {spender=record {owner=principal \"$SPENDER_1_PRINCIPAL\"}; amount=100:nat},)" &
;;

1)
dfx canister call --candid "$LEDGER_DID" --network "$NNS_URL" "$LEDGER_CANISTER_ID" icrc2_approve "(record {spender=record {owner=principal \"$SPENDER_2_PRINCIPAL\"}; amount=200:nat; expected_allowance=opt 200},)" &
;;

2)
dfx canister call --candid "$LEDGER_DID" --network "$NNS_URL" "$LEDGER_CANISTER_ID" icrc2_approve "(record {spender=record {owner=principal \"$SPENDER_3_PRINCIPAL\"}; amount=300:nat; expires_at=opt $EXPIRATION},)" &
let "EXPIRATION += 1"
;;

esac
done
wait
echo "done sending batch $((batch + 1))/$NUM_OF_BATCHES of $NUM_OF_APPROVALS_PER_BATCH txs"
done

query_blocks_res=$(dfx canister --network $NNS_URL call --candid "$LEDGER_DID" --query "$LEDGER_CANISTER_ID" query_blocks '(record {start=0:nat64;length=1:nat64})')
AFTER_CHAIN_LENGTH=$(echo $query_blocks_res | sed -n 's/.*chain_length *= *\([^ ]\+\).*/\1/p' | tr -d '_')
AFTER_FIRST_BLOCK_INDEX=$(echo $query_blocks_res | sed -n 's/.*first_block_index *= *\([^ ]\+\).*/\1/p' | tr -d '_')

DELTA=$(($NUM_OF_BATCHES * $NUM_OF_APPROVALS_PER_BATCH + 1))
EXPECTED_AFTER_CHAIN_LENGTH=$(($BEFORE_CHAIN_LENGTH + $DELTA))
if (($AFTER_CHAIN_LENGTH < $EXPECTED_AFTER_CHAIN_LENGTH)); then
echo >&2 "The chain_length should have increased of $DELTA transactions but it is not. before:$BEFORE_CHAIN_LENGTH after:$AFTER_CHAIN_LENGTH"
exit 2
fi

if [ $BEFORE_FIRST_BLOCK_INDEX -eq $AFTER_FIRST_BLOCK_INDEX ]; then
echo "Warning: no archival happened"
fi

echo "Approval test completed successfully!"
73 changes: 73 additions & 0 deletions rs/rosetta-api/scripts/test_transfer_from.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash

# Submit an upgrade proposal to the NNS.

set -euo pipefail
#set -x

if (($# != 0)); then
echo >&2 "Usage: $0"
exit 1
fi

LEDGER_CANISTER_ID="ryjl3-tyaaa-aaaaa-aaaba-cai"
ARCHIVE_CANISTER_ID="qjdve-lqaaa-aaaaa-aaaeq-cai"
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
LEDGER_DID="$SCRIPT_DIR/../icp_ledger/ledger.did"
TMP_DIR="$SCRIPT_DIR/tmp" # need a persistent directory from within dev container
NUM_OF_BATCHES=3
NUM_OF_TRANSFERS_PER_BATCH=20 # 100 fails for some reason related to the testnet setup
AMOUNT_PER_TRANSFER="1:nat"
AMOUNT_PER_APPROVAL="1000000000:nat"
HOME="${DFX_HOME:-$HOME}"

NNS_TEST_ID="nns_test_user_dfx_identity"
dfx identity use "$NNS_TEST_ID"
NNS_TEST_PRINCIPAL=$(dfx identity get-principal)

query_blocks_res=$(dfx canister --network $NNS_URL call --candid "$LEDGER_DID" --query "$LEDGER_CANISTER_ID" query_blocks '(record {start=0:nat64;length=1:nat64})')
BEFORE_CHAIN_LENGTH=$(echo $query_blocks_res | sed -n 's/.*chain_length *= *\([^ ]\+\).*/\1/p' | tr -d '_')
BEFORE_FIRST_BLOCK_INDEX=$(echo $query_blocks_res | sed -n 's/.*first_block_index *= *\([^ ]\+\).*/\1/p' | tr -d '_')

SPENDER_ID="spender_id"
SPENDER_ID_HOME_DIR="$DFX_HOME/.config/dfx/identity/$SPENDER_ID"

if [ -d "$SPENDER_ID_HOME_DIR" ]; then
echo "Found $SPENDER_ID identity in $SPENDER_ID_HOME_DIR"
else
echo "No identity directory found, I'm going to create a new $SPENDER_ID identity and persist it"
dfx identity new "$SPENDER_ID" --disable-encryption
fi
dfx identity use "$SPENDER_ID"
SPENDER_PRINCIPAL=$(dfx identity get-principal)

dfx identity use "$NNS_TEST_ID"
dfx canister call --candid "$LEDGER_DID" --network "$NNS_URL" "$LEDGER_CANISTER_ID" icrc2_approve "(record {spender=record {owner=principal \"$SPENDER_PRINCIPAL\"}; amount=$AMOUNT_PER_APPROVAL},)"

dfx identity use "$SPENDER_ID"

for ((batch = 0; batch < $NUM_OF_BATCHES; batch++)); do
echo "start sending batch $((batch + 1))/$NUM_OF_BATCHES of $NUM_OF_TRANSFERS_PER_BATCH txs"
for ((t = 0; t < $NUM_OF_TRANSFERS_PER_BATCH; t++)); do
dfx canister call --candid "$LEDGER_DID" --network "$NNS_URL" "$LEDGER_CANISTER_ID" icrc2_transfer_from "(record {from=record {owner=principal \"$NNS_TEST_PRINCIPAL\"}; to=record {owner=principal \"$SPENDER_PRINCIPAL\"}; amount=$AMOUNT_PER_TRANSFER},)" &
done
wait
echo "done sending batch $((batch + 1))/$NUM_OF_BATCHES of $NUM_OF_TRANSFERS_PER_BATCH txs"
done

query_blocks_res=$(dfx canister --network $NNS_URL call --candid "$LEDGER_DID" --query "$LEDGER_CANISTER_ID" query_blocks '(record {start=0:nat64;length=1:nat64})')
AFTER_CHAIN_LENGTH=$(echo $query_blocks_res | sed -n 's/.*chain_length *= *\([^ ]\+\).*/\1/p' | tr -d '_')
AFTER_FIRST_BLOCK_INDEX=$(echo $query_blocks_res | sed -n 's/.*first_block_index *= *\([^ ]\+\).*/\1/p' | tr -d '_')

DELTA=$(($NUM_OF_BATCHES * $NUM_OF_TRANSFERS_PER_BATCH + 1))
EXPECTED_AFTER_CHAIN_LENGTH=$(($BEFORE_CHAIN_LENGTH + $DELTA))
if (($AFTER_CHAIN_LENGTH < $EXPECTED_AFTER_CHAIN_LENGTH)); then
echo >&2 "The chain_length should have increased of $DELTA transactions but it is not. before:$BEFORE_CHAIN_LENGTH after:$AFTER_CHAIN_LENGTH"
exit 2
fi

if [ $BEFORE_FIRST_BLOCK_INDEX -eq $AFTER_FIRST_BLOCK_INDEX ]; then
echo "Warning: no archival happened"
fi

echo "Transfers from test completed successfully!"
9 changes: 3 additions & 6 deletions rs/rosetta-api/scripts/test_transfers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ ARCHIVE_CANISTER_ID="qjdve-lqaaa-aaaaa-aaaeq-cai"
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
LEDGER_DID="$SCRIPT_DIR/../icp_ledger/ledger.did"
TMP_DIR="$SCRIPT_DIR/tmp" # need a persistent directory from within dev container
INFO_FILE=$(find "$TMP_DIR" -iname "*.sourceme")
NUM_OF_BATCHES=50
NUM_OF_TRANSFERS_PER_BATCH=20 # 100 fails for some reason related to the testnet setup
AMOUNT_PER_TRANSFER="1:nat"
HOME="${DFX_HOME:-$HOME}"

#shellcheck source=/dev/null
source "$INFO_FILE"

source "$SCRIPT_DIR/init_identity.sh"
init_identity # initialize the dfx identity
NNS_TEST_ID="nns_test_user_dfx_identity"
dfx identity use "$NNS_TEST_ID"

query_blocks_res=$(dfx canister --network $NNS_URL call --candid "$LEDGER_DID" --query "$LEDGER_CANISTER_ID" query_blocks '(record {start=0:nat64;length=1:nat64})')
BEFORE_CHAIN_LENGTH=$(echo $query_blocks_res | sed -n 's/.*chain_length *= *\([^ ]\+\).*/\1/p' | tr -d '_')
Expand Down

0 comments on commit a4b46b2

Please sign in to comment.