Skip to content

Commit

Permalink
feat: add scripts for some anvil json-rpc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelstanley committed Dec 14, 2023
1 parent b78891a commit 0dd4bdb
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 15 deletions.
14 changes: 14 additions & 0 deletions scripts/devnet/cmd/anvil_dumpState.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
# (c) Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}

hex_state_file="$1"
curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":"1","method":"anvil_dumpState","params":[]}' \
"http://$ANVIL_IP_ADDR:8545" \
| awk 'NR==1 {print $1}' \
| jq -r '.result' > "$hex_state_file"
13 changes: 13 additions & 0 deletions scripts/devnet/cmd/anvil_loadState.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# (c) Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}

hex_state_file="$1"
hex_state_str="\"$(cat $hex_state_file)\""
curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"anvil_loadState\",\"params\":[${hex_state_str}]}" \
"http://$ANVIL_IP_ADDR:8545"
11 changes: 11 additions & 0 deletions scripts/devnet/cmd/anvil_net_listening.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# (c) Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}

curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":"1","method":"net_listening","params":[]}' \
"http://$ANVIL_IP_ADDR:8545"
17 changes: 17 additions & 0 deletions scripts/devnet/cmd/anvil_up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# (c) Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}

hex_state_file="$1"

anvil \
--block-time 5 &

hex_state_str="\"$(cat $hex_state_file)\""
curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d "{\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"anvil_loadState\",\"params\":[${hex_state_str}]}" \
"http://$ANVIL_IP_ADDR:8545"
17 changes: 13 additions & 4 deletions scripts/devnet/gen-devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS="0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
DEVNET_FOUNDRY_ACCOUNT_0_PRIVATE_KEY="0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
# keccak256("salt")
DEVNET_DEFAULT_SALT="0xa05e334153147e75f3f416139b5109d1179cb56fef6a4ecb4c4cbc92a7c37b70"
DEVNET_ANVIL_TIMEOUT=5
readonly ROLLUPS_CONTRACTS_VERSION \
DEVNET_RPC_URL \
DEVNET_FOUNDRY_ACCOUNT_0_ADDRESS \
DEVNET_FOUNDRY_ACCOUNT_0_PRIVATE_KEY \
DEVNET_DEFAULT_SALT
DEVNET_DEFAULT_SALT \
DEVNET_ANVIL_TIMEOUT

# Defaults
devnet_anvil_state_file=$(realpath "./anvil_state.json")
devnet_anvil_state_file=$(realpath "./anvil_state.hex")
devnet_deployment_file=$(realpath "./deployment.json")
template_hash_file=""
VERBOSE=""
Expand All @@ -51,7 +53,7 @@ usage()
echo " -t template-hash-file"
echo " Mandatory Cartesi Machine template hash file"
echo " -a"
echo " Path for output anvil state file"
echo " Path for output anvil hex state file"
echo " -d"
echo " Path for deployment information file"
echo " -v"
Expand Down Expand Up @@ -88,6 +90,13 @@ print_report() {
log "deployment file location : $devnet_deployment_file"
}

################################################################################
# Dump anvil state
anvil_dump_state() {
local anvil_state_file="$1"
"$script_dir"/cmd/anvil_dumpState.sh "$anvil_state_file"
}

################################################################################
# Generate deployment file
generate_deployment_file() {
Expand Down Expand Up @@ -325,7 +334,7 @@ trap finish EXIT ERR
log "starting devnet creation"
work_dir=$(mktemp -d)
readonly work_dir
check_error $? "faile to create temp dir"
check_error $? "failed to create temp dir"
verbose "created $work_dir"

anvil_pid=""
Expand Down
15 changes: 4 additions & 11 deletions scripts/devnet/lib/anvil.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
# (c) Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: Apache-2.0 (see LICENSE)

readonly DEVNET_ANVIL_IP="0.0.0.0"
readonly DEVNET_ANVIL_TIMEOUT=5
ANVIL_IP_ADDR=${ANVIL_IP_ADDR:-"0.0.0.0"}
lib_dir="$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

_is_anvil_up() {
local -n ready=$1

local result="$(
curl -X \
POST \
-s \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","id":"1","method":"net_listening","params":[]}' \
"http://$DEVNET_ANVIL_IP:8545"
)"
local result=$(. $lib_dir/../cmd/anvil_net_listening.sh)

ready="false"
if [[ -n "$result" ]]; then
Expand All @@ -36,7 +29,7 @@ anvil_up() {
fi

anvil \
--host $DEVNET_ANVIL_IP \
--host $ANVIL_IP_ADDR \
--dump-state "$anvil_state_file" \
> /dev/null &
local pid=$!
Expand Down

0 comments on commit 0dd4bdb

Please sign in to comment.