Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Adding the Anvil Fork on the multiples tasks. #438

Draft
wants to merge 19 commits into
base: feat/sim-stacked-tasks
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions anvil.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
ownerSafe="0x847B5c174615B1B7fDF770882256e2D3E95b9D92"
rpcUrl="http://localhost:8545"
echo ${ownersafe}
echo ${rpcUrl}
echo "============ OVERRIDE SAFE SETTINGS ============"
echo "1. Set the threshold to 1."
cast rpc anvil_setStorageAt ${ownerSafe} 0x0000000000000000000000000000000000000000000000000000000000000004 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${rpcUrl}

echo "2.set the owner count to 1."
## 2. Set the owner count to 1.
cast rpc anvil_setStorageAt ${ownerSafe} 0x0000000000000000000000000000000000000000000000000000000000000003 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${rpcUrl}

echo "3.Insert the address 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 as the sole owner of the safe."
## 3. Set the 0xf39 address to the sole signer of the safe.
# cast keccak 1 || 2 => 0xe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e0 expected owner mapping: {0x1 -> 0xf39..., 0xf39 -> 0x1}
cast rpc anvil_setStorageAt ${ownerSafe} 0xe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e0 0x000000000000000000000000f39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url ${rpcUrl}
## 4. Set the owner (abi.encode(owner, uint256(2)))-> to sentinel_address (0x1).
echo "4.Close the mapping of the owners to the sentinel address."
cast rpc anvil_setStorageAt ${ownerSafe} 0xbc40fbf4394cd00f78fae9763b0c2c71b21ea442c42fdadc5b720537240ebac1 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${rpcUrl}

cast call 0x847B5C174615B1B7FDF770882256E2D3E95B9D92 "getOwners()" --rpc-url http://localhost:8545
echo "================================================"

2 changes: 1 addition & 1 deletion lib/base-contracts
125 changes: 125 additions & 0 deletions nested.just
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,104 @@ sign whichSafe hdPath='0':
${bundlePath} \
"${safe}"

approvehash_in_anvil whichSafe hdPath='0':
#!/usr/bin/env bash
# set -x
localrpcUrl="http://localhost:8545"
# sender=$(cast wallet address --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80)
bundlePath=${taskPath}/${bundleName}.json
script=NestedSignFromJson
echo "simulating in anvil"
localrpcUrl="http://localhost:8545"

if [ -f "${taskPath}/NestedSignFromJson.s.sol" ]; then
script="${taskPath}/NestedSignFromJson.s.sol"
echo "Running script with assertions"
fi
echo "Using script ${script}"
if [ "{{whichSafe}}" == "foundation" ]; then
safe="{{foundationSafe}}"
echo "Using foundation safe at ${safe}"
fi
if [ "{{whichSafe}}" == "council" ]; then
safe="{{councilSafe}}"
echo "Using council safe at ${safe}"
fi
if [ "{{whichSafe}}" == "chain-governor" ]; then
if [ -z "{{chainGovernorSafe}}" ]; then
echo "Error: CHAIN_GOVERNOR_SAFE is not set for chain-governor." >&2
exit 1
fi
safe="{{chainGovernorSafe}}"
fi

echo "Using script ${script} with nested approvehash_in_anvil."
echo "Safe address: ${ownerSafe}"
echo "Getting signer address..."

if [ ! -z "$SIMULATE_WITHOUT_LEDGER" ]; then
signer=$(cast wallet address --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80) # corresponding to 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 private-key.
else
signer=$(cast wallet address --ledger --mnemonic-derivation-path "m/44'/60'/{{hdPath}}'/0/0")
fi

echo "Simulating with: ${signer}"
echo ""

echo "============ OVERRIDE SAFES SETTINGS '${ownerSafe}' & '${safe}' ============"
# echo "0. Set the threshold of the L1PAO to 1."
# cast rpc anvil_setStorageAt ${ownerSafe} 0x0000000000000000000000000000000000000000000000000000000000000004 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${localrpcUrl}
echo "1. Set the threshold to 1 of the child safe."
cast rpc anvil_setStorageAt ${safe} 0x0000000000000000000000000000000000000000000000000000000000000004 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${localrpcUrl}

echo "2. Set the owner count to 1."
## 2. Set the owner count to 1.
cast rpc anvil_setStorageAt ${safe} 0x0000000000000000000000000000000000000000000000000000000000000003 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${localrpcUrl}


echo "3. Insert the address 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 as the sole owner of the safe."
## 3. Set the 0xf39 address to the sole signer of the safe.
# cast keccak 1 || 2 => 0xe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e0 expected owner mapping: {0x1 -> 0xf39..., 0xf39 -> 0x1}
cast rpc anvil_setStorageAt ${safe} 0xe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e0 0x000000000000000000000000f39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --rpc-url ${localrpcUrl}
## 4. Set the owner (abi.encode(owner, uint256(2)))-> to sentinel_address (0x1).
echo "4. Close the mapping of the owners to the sentinel address."
cast rpc anvil_setStorageAt ${safe} 0xbc40fbf4394cd00f78fae9763b0c2c71b21ea442c42fdadc5b720537240ebac1 0x0000000000000000000000000000000000000000000000000000000000000001 --rpc-url ${localrpcUrl}

echo "the sole Owner of the \"${safe}\" is: $(cast call ${safe} "getOwners()(address[])" --rpc-url http://localhost:8545)"
echo "================================================"

forge build
outputforge=$(forge script ${script} \
--rpc-url ${localrpcUrl} \
--broadcast \
--private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
--sender ${signer} \
--sig "signJson(string,address)" \
${bundlePath} \
${safe}) &> /dev/null
echo "Outputforge: $outputforge" > /tmp/outputforge.txt
hash=$(cat /tmp/outputforge.txt | grep -A 1 "If submitting onchain, call Safe.approveHash on " | cut -d ':' -f 2 | tr -d '[:space:]')

# Check if a hash was found
if [[ -z "$hash" ]]; then
echo "Hash not found in the file."
exit 1
else
echo "Extracted Hash: $hash"
fi

## 5. Approve the hash with the well-known private key that is the first owner of anvil selection.
cast send ${safe} "approveHash(bytes32)" ${hash} --rpc-url ${localrpcUrl} --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
forge script ${script} \
--rpc-url ${localrpcUrl} \
--broadcast \
--private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
--sig "approveJson(string,address,bytes)" \
${bundlePath} \
${safe} \
1111 &> /dev/null # dummy signature to pass the check if the signature is not empty..


approve whichSafe hdPath='0':
#!/usr/bin/env bash
bundlePath=${taskPath}/${bundleName}.json
Expand Down Expand Up @@ -151,6 +249,33 @@ execute hdPath='0':
--sig "runJson(string)" \
${bundlePath}

execute_in_anvil hdPath='0':
#!/usr/bin/env bash
localrpcUrl="http://localhost:8545"
# set -x
bundlePath=${taskPath}/${bundleName}.json
script=NestedSignFromJson
if [ -f "${taskPath}/NestedSignFromJson.s.sol" ]; then
script="${taskPath}/NestedSignFromJson.s.sol"
echo "Running script with assertions"
fi
echo "Using script ${script}"

if [ ! -z "$SIMULATE_WITHOUT_LEDGER" ]; then
signer=$(cast wallet address --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80) # corresponding to 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 private-key.
else
signer=$(cast wallet address --ledger --mnemonic-derivation-path "m/44'/60'/{{hdPath}}'/0/0")
fi


forge build
forge script ${script} \
--rpc-url ${localrpcUrl} \
--broadcast --sender 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
--sig "runJson(string)" \
${bundlePath} &> /dev/null


simulated-run hdPath='0':
#!/usr/bin/env bash
bundlePath=${taskPath}/${bundleName}.json
Expand Down
73 changes: 64 additions & 9 deletions script/utils/sim-sequence.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/bin/bash


### ADDRESS SHOULD BE GET FROM SUPERCHAIN-OPS IN THE FUTURE ###


## ETHEREUM
Security_Council_Safe=0xc2819DC788505Aac350142A7A707BF9D03E3Bd03
Foundation_Upgrade_Safe=0x847B5c174615B1B7fDF770882256e2D3E95b9D92
Foundation_Operation_Safe=0x9BA6e03D8B90dE867373Db8cF1A58d2F7F006b3A
Proxy_Admin_Owner_Safe=0x5a0Aae59D09fccBdDb6C6CcEB07B7279367C3d2A
##############################################################
# Simulates a sequence of tasks for a given network by running them against an Anvil
# fork with state overrides disabled.
#
Expand Down Expand Up @@ -51,6 +61,30 @@ cleanup() {
if [[ -f "$backup_file" ]]; then
mv "$backup_file" "$simulation_file"
fi
# Kill the anvil fork at the end
ps aux | grep anvil | grep -v grep | awk '{print $2}' | xargs kill
}
createFork() {
# Start a fork
echo "Starting anvil fork..."
# check if the port is already open
if lsof -Pi :8545 -sTCP:LISTEN -t >/dev/null ; then
echo "Port 8545 is already in use, killing previous anvil fork..."
ps aux | grep anvil | grep -v grep | awk '{print $2}' | xargs kill


fi
anvil -f $RPC_URL --fork-block-number 21573136 >> /tmp/anvil.logs &
sleep 5
}

NonceDisplay(){
echo " $1 NONCES STATUS:"
echo "Foundation Upgrade Safe (FuS) nonce: "$(cast call $Foundation_Upgrade_Safe "nonce()(uint256)" --rpc-url http://localhost:8545)"."
echo "Foundation Operation Safe (FoS) nonce: "$(cast call $Foundation_Operation_Safe "nonce()(uint256)" --rpc-url http://localhost:8545)"."
echo "Security Council Safe (SC) nonce: "$(cast call $Security_Council_Safe "nonce()(uint256)" --rpc-url http://localhost:8545)"."
echo "L1ProxyAdminOwner (L1PAO) nonce: "$(cast call $Proxy_Admin_Owner_Safe "nonce()(uint256)" --rpc-url http://localhost:8545)"."
echo "==========================================================="
}

# Find unique task folder(s) for a given task ID
Expand Down Expand Up @@ -82,12 +116,10 @@ fi

network="$1"
task_ids="$2"

# Determine root directory.
base_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root_dir="$base_dir/../.."
task_base_dir="${root_dir}/tasks/${network}"

# Verify task_base_dir exists.
if [[ ! -d "$task_base_dir" ]]; then
error_exit "Task base directory '$task_base_dir' does not exist."
Expand All @@ -100,28 +132,51 @@ for task_id in $task_ids; do
task_folders+=("$matching_folders")
done


source ${task_folders[0]}/.env
echo "RPC: $ETH_RPC_URL"
RPC_URL=$ETH_RPC_URL
unset ETH_RPC_URL
echo "Simulating the following tasks in order:"
for task_folder in "${task_folders[@]}"; do
echo " $(realpath "$task_folder")"
done

# Create the anvil Fork
createFork
# Disable state overrides and execute tasks.
disable_state_overrides
export SIMULATE_WITHOUT_LEDGER=1
for task_folder in "${task_folders[@]}"; do
echo -e "\n---- Simulating task $task_folder ----"

NonceDisplay "(🟧) Before Simulation"
pushd "$task_folder" >/dev/null || error_exit "Failed to navigate to '$task_folder'."

# add the RPC_URL to the .env file
# echo "ETH_RPC_URL=http://localhost:8545" >> "${PWD}/.env" # Replace with the anvil fork URL
if [[ -f "${task_folder}/NestedSignFromJson.s.sol" ]]; then
echo "Task type: nested"
# TODO This currently hardcodes the council but we should also run as Foundation.
just --dotenv-path "${PWD}/.env" --justfile "${root_dir}/nested.just" simulate council
approvalhashcouncil=$(just \
--dotenv-path "${PWD}/.env" \
--justfile "${root_dir}/nested.just" \
approvehash_in_anvil council)
approvalhashcouncil=$(just \
--dotenv-path "${PWD}/.env" \
--justfile "${root_dir}/nested.just" \
approvehash_in_anvil foundation)

execution=$(just \
--dotenv-path "${PWD}/.env" \
--justfile "${root_dir}/nested.just" \
execute_in_anvil 0)

else
echo "Task type: single"
just --dotenv-path "${PWD}/.env" --justfile "${root_dir}/single.just" simulate
echo "Task type detected: single"
simulate=$(just --dotenv-path "${PWD}/.env" --justfile "${root_dir}/single.just" approvehash_in_anvil 0)
execution=$(just --dotenv-path "${PWD}/.env" --justfile "${root_dir}/single.just" execute_in_anvil 0)
echo ""
fi

sleep 5
NonceDisplay "(🟩) After Simulation"
popd >/dev/null || error_exit "Failed to return to previous directory."
done

Expand Down
Loading