Commit f99fa484 authored by Nicolas "Norswap" Laurent's avatar Nicolas "Norswap" Laurent Committed by norswap

make demo scripts automatically run the mainnet fork

parent 89bf4922
...@@ -6,3 +6,4 @@ unicorn2 ...@@ -6,3 +6,4 @@ unicorn2
.*.swp .*.swp
venv venv
.idea .idea
*.log
#!/bin/bash #!/bin/bash
# -------------------------------------------------------------------------------- # --- DOC ----------------------------------------------------------------------
#
# !! Make sure to run forked_node.sh in another terminal before running this.
#
# Unlike the simple scenario (cf. challenge_simple.sh), in this # Unlike the simple scenario (cf. challenge_simple.sh), in this
# challenge-response scenario we use the correct block data (preimages) and # challenge-response scenario we use the correct block data (preimages) and
# instead use the `OUTPUTFAULT` environment variable to request a fault in the # instead use the `OUTPUTFAULT` environment variable to request a fault in the
...@@ -29,11 +27,9 @@ ...@@ -29,11 +27,9 @@
# #
# - The challenged block contains almost 4x as many transactions as the original # - The challenged block contains almost 4x as many transactions as the original
# (8.5M vs 30M gas). # (8.5M vs 30M gas).
#
# --------------------------------------------------------------------------------
# Exit if any command fails.
set -e # --- SCRIPT SETUP -------------------------------------------------------------
shout() { shout() {
echo "" echo ""
...@@ -43,14 +39,41 @@ shout() { ...@@ -43,14 +39,41 @@ shout() {
echo "" echo ""
} }
# Print an error if we exit before all commands have run. # Exit if any command fails.
set -e
exit_trap() { exit_trap() {
# Print an error if the last command failed
# (in which case the script is exiting because of set -e).
[[ $? == 0 ]] && return [[ $? == 0 ]] && return
echo "----------------------------------------" echo "----------------------------------------"
echo "EARLY EXIT: SCRIPT FAILED" echo "EARLY EXIT: SCRIPT FAILED"
echo "----------------------------------------" echo "----------------------------------------"
# Kill (send SIGTERM) to the whole process group, also killing
# any background processes.
# I think the trap command resets SIGTERM before resending it to the whole
# group. (cf. https://stackoverflow.com/a/2173421)
trap - SIGTERM && kill -- -$$
} }
trap "exit_trap" EXIT trap "exit_trap" SIGINT SIGTERM EXIT
# --- BOOT MAINNET FORK --------------------------------------------------------
NODE_LOG="challenge_fault_node.log"
shout "BOOTING MAINNET FORK NODE IN BACKGROUND (LOG: $NODE_LOG)"
# get directory containing this file
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
# run a hardhat mainnet fork node
"$SCRIPT_DIR/forked_node.sh" > "$NODE_LOG" 2>&1 &
# give the node some time to boot up
sleep 10
# --- CHALLENGE SETUP ----------------------------------------------------------
# block whose transition will be challenged # block whose transition will be challenged
# this variable is read by challenge.js, respond.js and assert.js # this variable is read by challenge.js, respond.js and assert.js
...@@ -90,6 +113,8 @@ OUTPUTFAULT=1 BASEDIR=/tmp/cannon_fault mipsevm/mipsevm $BLOCK ...@@ -90,6 +113,8 @@ OUTPUTFAULT=1 BASEDIR=/tmp/cannon_fault mipsevm/mipsevm $BLOCK
# alternatively, to inject a fault in registers instead of memory # alternatively, to inject a fault in registers instead of memory
# REGFAULT=13240000 BASEDIR=/tmp/cannon_fault mipsevm/mipsevm $BLOCK # REGFAULT=13240000 BASEDIR=/tmp/cannon_fault mipsevm/mipsevm $BLOCK
# --- BINARY SEARCH ------------------------------------------------------------
shout "STARTING CHALLENGE" shout "STARTING CHALLENGE"
BASEDIR=/tmp/cannon_fault npx hardhat run scripts/challenge.js --network hosthat BASEDIR=/tmp/cannon_fault npx hardhat run scripts/challenge.js --network hosthat
...@@ -102,6 +127,8 @@ for i in {1..25}; do ...@@ -102,6 +127,8 @@ for i in {1..25}; do
npx hardhat run scripts/respond.js --network hosthat npx hardhat run scripts/respond.js --network hosthat
done done
# --- SINGLE STEP EXECUTION ----------------------------------------------------
shout "ASSERTING AS CHALLENGER (should fail)" shout "ASSERTING AS CHALLENGER (should fail)"
set +e # this should fail! set +e # this should fail!
BASEDIR=/tmp/cannon_fault CHALLENGER=1 npx hardhat run scripts/assert.js --network hosthat BASEDIR=/tmp/cannon_fault CHALLENGER=1 npx hardhat run scripts/assert.js --network hosthat
......
#!/bin/bash #!/bin/bash
# -------------------------------------------------------------------------------- # --- DOC ----------------------------------------------------------------------
#
# !! Make sure to run forked_node.sh in another terminal before running this.
#
# In this example, the challenger will challenge the transition from a block # In this example, the challenger will challenge the transition from a block
# (`BLOCK`), but pretends that chain state before another block (`WRONG_BLOCK`) # (`BLOCK`), but pretends that chain state before another block (`WRONG_BLOCK`)
# is the state before the challenged block. Consequently, the challenger will # is the state before the challenged block. Consequently, the challenger will
...@@ -21,26 +19,53 @@ ...@@ -21,26 +19,53 @@
# Because the challenger uses the wrong inputs, it will assert a post-state # Because the challenger uses the wrong inputs, it will assert a post-state
# (Merkle root) for the first MIPS instruction that has the wrong input hash at # (Merkle root) for the first MIPS instruction that has the wrong input hash at
# 0x3000000. Hence, the challenge will fail. # 0x3000000. Hence, the challenge will fail.
#
# --------------------------------------------------------------------------------
# Exit if any command fails.
set -e # --- SCRIPT SETUP -------------------------------------------------------------
shout() { shout() {
echo "" echo ""
echo "------------------------------------------------------------" echo "----------------------------------------"
echo "$1" echo "$1"
echo "------------------------------------------------------------" echo "----------------------------------------"
echo "" echo ""
} }
# Print an error if we exit before all commands have run. # Exit if any command fails.
set -e
exit_trap() { exit_trap() {
# Print an error if the last command failed
# (in which case the script is exiting because of set -e).
[[ $? == 0 ]] && return [[ $? == 0 ]] && return
shout "EARLY EXIT: SCRIPT FAILED" echo "----------------------------------------"
echo "EARLY EXIT: SCRIPT FAILED"
echo "----------------------------------------"
# Kill (send SIGTERM) to the whole process group, also killing
# any background processes.
# I think the trap command resets SIGTERM before resending it to the whole
# group. (cf. https://stackoverflow.com/a/2173421)
trap - SIGTERM && kill -- -$$
} }
trap "exit_trap" EXIT trap "exit_trap" SIGINT SIGTERM EXIT
# --- BOOT MAINNET FORK --------------------------------------------------------
NODE_LOG="challenge_simple_node.log"
shout "BOOTING MAINNET FORK NODE IN BACKGROUND (LOG: $NODE_LOG)"
# get directory containing this file
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
# run a hardhat mainnet fork node
"$SCRIPT_DIR/forked_node.sh" > "$NODE_LOG" 2>&1 &
# give the node some time to boot up
sleep 10
# --- CHALLENGE SETUP ----------------------------------------------------------
# chain ID, read by challenge.js, respond.js and assert.js # chain ID, read by challenge.js, respond.js and assert.js
export ID=0 export ID=0
...@@ -80,6 +105,8 @@ BASEDIR=/tmp/cannon_fault mipsevm/mipsevm $WRONG_BLOCK ...@@ -80,6 +105,8 @@ BASEDIR=/tmp/cannon_fault mipsevm/mipsevm $WRONG_BLOCK
# pretend the wrong block's input, checkpoints and preimages are the right block's # pretend the wrong block's input, checkpoints and preimages are the right block's
ln -s /tmp/cannon_fault/0_$WRONG_BLOCK /tmp/cannon_fault/0_$BLOCK ln -s /tmp/cannon_fault/0_$WRONG_BLOCK /tmp/cannon_fault/0_$BLOCK
# --- BINARY SEARCH ------------------------------------------------------------
shout "STARTING CHALLENGE" shout "STARTING CHALLENGE"
BASEDIR=/tmp/cannon_fault npx hardhat run scripts/challenge.js --network hosthat BASEDIR=/tmp/cannon_fault npx hardhat run scripts/challenge.js --network hosthat
...@@ -92,6 +119,8 @@ for i in {1..23}; do ...@@ -92,6 +119,8 @@ for i in {1..23}; do
npx hardhat run scripts/respond.js --network hosthat npx hardhat run scripts/respond.js --network hosthat
done done
# --- SINGLE STEP EXECUTION ----------------------------------------------------
shout "ASSERTING AS CHALLENGER (should fail)" shout "ASSERTING AS CHALLENGER (should fail)"
set +e # this should fail! set +e # this should fail!
BASEDIR=/tmp/cannon_fault CHALLENGER=1 npx hardhat run scripts/assert.js --network hosthat BASEDIR=/tmp/cannon_fault CHALLENGER=1 npx hardhat run scripts/assert.js --network hosthat
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment