diff --git a/packages/protocol/foundry.toml b/packages/protocol/foundry.toml index 95fe05401e1..f309779040c 100644 --- a/packages/protocol/foundry.toml +++ b/packages/protocol/foundry.toml @@ -14,7 +14,7 @@ remappings = [ 'forge-std-8/=lib/celo-foundry-8/lib/forge-std/src/', '@celo-contracts-8=contracts-0.8/', '@openzeppelin/contracts8/=lib/openzeppelin-contracts8/contracts/', - '@celo-contracts=contracts/' + '@celo-contracts/=contracts/' ] no_match_contract = "RandomTest" diff --git a/packages/protocol/migrations_sol/create_and_migrate_anvil_devchain.sh b/packages/protocol/migrations_sol/create_and_migrate_anvil_devchain.sh index fbbda837206..dc49fc4af1f 100755 --- a/packages/protocol/migrations_sol/create_and_migrate_anvil_devchain.sh +++ b/packages/protocol/migrations_sol/create_and_migrate_anvil_devchain.sh @@ -1,20 +1,34 @@ #!/usr/bin/env bash set -euo pipefail -# Compile everything -forge build +# Keeping track of start time to measure how long it takes to run the script entirely +START_TIME=$SECONDS export ANVIL_PORT=8546 # TODO make this configurable FROM_ACCOUNT_NO_ZERO="f39Fd6e51aad88F6F4ce6aB8827279cffFb92266" # This is Anvil's default account (1) -FROM_ACCOUNT="0x$FROM_ACCOUNT_NO_ZERO" +export FROM_ACCOUNT="0x$FROM_ACCOUNT_NO_ZERO" + +# Create temporary directory TEMP_FOLDER="$PWD/.tmp" +mkdir -p $TEMP_FOLDER +# Start a local anvil instance source $PWD/migrations_sol/start_anvil.sh -source $PWD/migrations_sol/deploy_precompiles.sh +# Deploy libraries to the anvil instance +source $PWD/migrations_sol/deploy_libraries.sh +echo "Library flags are: $LIBRARY_FLAGS" + +# Build all contracts with deployed libraries +# Including contracts that depend on libraries. This step replaces the library placeholder +# in the bytecode with the address of the actually deployed library. +echo "Compiling with libraries... " +time forge build $LIBRARY_FLAGS +# Deploy precompile contracts +source $PWD/migrations_sol/deploy_precompiles.sh echo "Setting Registry Proxy" REGISTRY_ADDRESS="0x000000000000000000000000000000000000ce10" @@ -24,50 +38,19 @@ REGISTRY_OWNER_ADDRESS=$FROM_ACCOUNT_NO_ZERO echo "Setting Registry owner" # Sets the storage of the registry so that it has an owner we control -# pasition is bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1); +# position is bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1); cast rpc anvil_setStorageAt --rpc-url http://127.0.0.1:$ANVIL_PORT $REGISTRY_ADDRESS 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103 "0x000000000000000000000000$REGISTRY_OWNER_ADDRESS" - -echo "Deploying libraries" -LIBRARIES_PATH=("contracts/common/linkedlists/AddressSortedLinkedListWithMedian.sol:AddressSortedLinkedListWithMedian" - "contracts/common/Signatures.sol:Signatures" - "contracts/common/linkedlists/AddressLinkedList.sol:AddressLinkedList" - "contracts/common/linkedlists/AddressSortedLinkedList.sol:AddressSortedLinkedList" - "contracts/common/linkedlists/IntegerSortedLinkedList.sol:IntegerSortedLinkedList" - "contracts/governance/Proposals.sol:Proposals" -) - -LIBRARIES="" - -for library in "${LIBRARIES_PATH[@]}"; do - library_name="${library#*:}" - echo "Deploying library: $library_name" - create_library_out=`forge create $library --from $FROM_ACCOUNT --rpc-url http://127.0.0.1:$ANVIL_PORT --unlocked --json` - library_address=`echo $create_library_out | jq -r '.deployedTo'` - - LIBRARIES="$LIBRARIES --libraries $library:$library_address" -done - -echo "Library flags are: $LIBRARIES" -echo "Backing up libraries" - -mkdir -p $TEMP_FOLDER - -LIBRARIES_FILE="$TEMP_FOLDER/libraries.tx" -rm -f $LIBRARIES_FILE -touch $LIBRARIES_FILE - -echo "$LIBRARIES" > $LIBRARIES_FILE - +# run migrations +echo "Running migration script... " # helpers to disable broadcast and simulation # TODO move to configuration BROADCAST="--broadcast" -SKIP_SUMULATION="" -# SKIP_SUMULATION="--skip-simulation" +SKIP_SIMULATION="" +# SKIP_SIMULATION="--skip-simulation" # BROADCAST="" +time forge script migrations_sol/Migration.s.sol --tc Migration --rpc-url http://127.0.0.1:$ANVIL_PORT -vvv $BROADCAST --non-interactive --sender $FROM_ACCOUNT --unlocked $LIBRARY_FLAGS || echo "Migration script failed" -echo "Compiling with libraries... " -time forge build $LIBRARIES - -# run migrations -time forge script migrations_sol/Migration.s.sol --tc Migration --rpc-url http://127.0.0.1:$ANVIL_PORT -vvv $BROADCAST --non-interactive --sender $FROM_ACCOUNT --unlocked $LIBRARIES || echo "Migration script failed" \ No newline at end of file +# Keeping track of the finish time to measure how long it takes to run the script entirely +ELAPSED_TIME=$(($SECONDS - $START_TIME)) +echo "Total elapsed time: $ELAPSED_TIME seconds" diff --git a/packages/protocol/migrations_sol/deploy_libraries.sh b/packages/protocol/migrations_sol/deploy_libraries.sh new file mode 100644 index 00000000000..4a9c5d5ac82 --- /dev/null +++ b/packages/protocol/migrations_sol/deploy_libraries.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Name of temporary directory +TEMP_DIR_NAME=".tmp/libraries" +TEMP_DIR="$PWD/$TEMP_DIR_NAME" + +# Create a temporary directory or remove it first it if exists +if [ -d "$TEMP_DIR" ]; then + echo "Removing existing temporary folder..." + rm -rf $TEMP_DIR +fi +mkdir $TEMP_DIR + +# Copy libraries to the directory +LIBRARIES_PATH=("contracts/common/linkedlists/AddressSortedLinkedListWithMedian.sol:AddressSortedLinkedListWithMedian" + "contracts/common/Signatures.sol:Signatures" + "contracts/common/linkedlists/AddressLinkedList.sol:AddressLinkedList" + "contracts/common/linkedlists/AddressSortedLinkedList.sol:AddressSortedLinkedList" + "contracts/common/linkedlists/IntegerSortedLinkedList.sol:IntegerSortedLinkedList" + "contracts/governance/Proposals.sol:Proposals" +) + +for LIB_PATH in "${LIBRARIES_PATH[@]}"; do + IFS=":" read -r SOURCE DEST <<< "$LIB_PATH" + DEST_FILE="$TEMP_DIR/$SOURCE" + DEST_DIR=$(dirname "$DEST_FILE") + mkdir -p "$DEST_DIR" + echo "Copying file $SOURCE to $DEST_FILE" + cp "$SOURCE" "$DEST_FILE" +done + +# Copy dependencies of the libraries to the directory +LIBRARY_DEPENDENCIES_PATH=( + "contracts/common/FixidityLib.sol" + "contracts/common/linkedlists/LinkedList.sol" + "contracts/common/linkedlists/SortedLinkedList.sol" + "contracts/common/linkedlists/SortedLinkedListWithMedian.sol" + "lib/openzeppelin-contracts/contracts/math/SafeMath.sol" + "lib/openzeppelin-contracts/contracts/math/Math.sol" + "lib/openzeppelin-contracts/contracts/cryptography/ECDSA.sol" + "lib/openzeppelin-contracts/contracts/utils/Address.sol" + "lib/solidity-bytes-utils/contracts/BytesLib.sol" +) + +# Creating two variables for better readability +SOURCE_DIR=$PWD +DEST_DIR=$TEMP_DIR + +for LIB_PATH in "${LIBRARY_DEPENDENCIES_PATH[@]}"; do + # Creates directory for the dependency, including any necessary parent directories + mkdir -p $DEST_DIR/$(dirname $LIB_PATH) + # Copies dependency to the newly created directory + cp $SOURCE_DIR/$LIB_PATH $DEST_DIR/$LIB_PATH +done + +# Copy foundry config to the temporary directory +cp $SOURCE_DIR/foundry.toml $DEST_DIR/foundry.toml + +# Move into the temporary directory +pushd $TEMP_DIR + +# Build libraries +echo "Building libraries..." +forge build + +# Deploy libraries and building library flag +echo "Deploying libraries..." +export LIBRARY_FLAGS="" +for LIB_PATH in "${LIBRARIES_PATH[@]}"; do + LIB_NAME="${LIB_PATH#*:}" + # For example: + # LIB_PATH = "contracts/common/linkedlists/AddressSortedLinkedListWithMedian.sol:AddressSortedLinkedListWithMedian" + # LIB_NAME = AddressSortedLinkedListWithMedian + echo "Deploying library: $LIB_NAME" + create_library_out=`forge create $LIB_PATH --from $FROM_ACCOUNT --rpc-url http://127.0.0.1:$ANVIL_PORT --unlocked --json` + LIB_ADDRESS=`echo $create_library_out | jq -r '.deployedTo'` + # Constructing library flag so the remaining contracts can be built and linkeded to these libraries + LIBRARY_FLAGS="$LIBRARY_FLAGS --libraries $LIB_PATH:$LIB_ADDRESS" +done + +# Move out of the temporary directory +popd + +# Remove the temporary directory +rm -rf $TEMP_DIR +