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

Horizon: Integration tests #1109

Draft
wants to merge 9 commits into
base: horizon
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Logs
yarn-debug.log*
yarn-error.log*
node.log

# Dependency directories
node_modules/
Expand Down
17 changes: 16 additions & 1 deletion packages/horizon/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { hardhatBaseConfig } from 'hardhat-graph-protocol/sdk'
import { existsSync, readdirSync } from 'fs'
import { join } from 'path'

// Hardhat plugins
import '@nomicfoundation/hardhat-foundry'
import '@nomicfoundation/hardhat-toolbox'
import '@nomicfoundation/hardhat-ignition-ethers'
import 'hardhat-storage-layout'
import 'hardhat-contract-sizer'
import 'hardhat-secure-accounts'
import { HardhatUserConfig } from 'hardhat/types'

// Hardhat tasks
function loadTasks() {
const tasksPath = join(__dirname, 'tasks')
readdirSync(tasksPath)
.filter(pth => pth.includes('.ts'))
.forEach((file) => {
require(join(tasksPath, file))
})
}

if (existsSync(join(__dirname, 'build/contracts'))) {
loadTasks()
}

// Skip importing hardhat-graph-protocol when building the project, it has circular dependency
if (process.env.BUILD_RUN !== 'true') {
require('hardhat-graph-protocol')
Expand Down
5 changes: 4 additions & 1 deletion packages/horizon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"lint": "yarn lint:ts && yarn lint:sol",
"clean": "rm -rf build dist cache cache_forge typechain-types",
"build": "BUILD_RUN=true hardhat compile",
"test": "forge test && hardhat test"
"test": "forge test && hardhat test:integration --deploy-type deploy",
"test:e2e:deploy": "./scripts/e2e-deploy",
"test:e2e:migrate": "./scripts/e2e-migrate"
},
"devDependencies": {
"@graphprotocol/contracts": "workspace:^7.0.0",
Expand All @@ -40,6 +42,7 @@
"eslint": "^8.56.0",
"eslint-graph-config": "workspace:^0.0.1",
"ethers": "^6.13.4",
"glob": "^11.0.1",
"hardhat": "^2.22.18",
"hardhat-contract-sizer": "^2.10.0",
"hardhat-gas-reporter": "^1.0.8",
Expand Down
56 changes: 56 additions & 0 deletions packages/horizon/scripts/e2e-deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

set -eo pipefail

# Remove ignition deployment files if they exist
echo "Removing ignition deployment files..."
rm -rf ignition/deployments/chain-31337

# Export environment variable for all commands
export SECURE_ACCOUNTS_DISABLE_PROVIDER=true

# Function to cleanup resources
cleanup() {
# Kill hardhat node
if [ ! -z "$NODE_PID" ]; then
echo "Cleaning up node process..."
kill $NODE_PID 2>/dev/null || true
fi

# Unset environment variable
# echo "Cleaning up environment variables..."
# unset SECURE_ACCOUNTS_DISABLE_PROVIDER
}

# Set trap to call cleanup function on script exit (normal or error)
trap cleanup EXIT

# Check if port 8545 is in use
if lsof -i:8545 > /dev/null 2>&1; then
echo "Error: Port 8545 is already in use"
exit 1
fi

# Start local hardhat node
echo "Starting local hardhat node..."
npx hardhat node > node.log 2>&1 &
NODE_PID=$!

# Wait for node to start
sleep 10

# Deploy protocol
npx hardhat deploy:protocol --network localhost

# Run integration tests - After transition period
echo "Running after-transition-period tests..."
npx hardhat test:integration --phase after-transition --network localhost

# Enable delegation slashing
npx hardhat transition:enable-delegation-slashing --network localhost --governor-index 1

# Run integration tests - After delegation slashing enabled
echo "Running after-delegation-slashing tests..."
npx hardhat test:integration --phase after-delegation-slashing --network localhost

echo "E2E tests completed successfully!"
82 changes: 82 additions & 0 deletions packages/horizon/scripts/e2e-migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

set -eo pipefail

# Function to cleanup resources
cleanup() {
# Remove ignition deployment files
echo "Removing ignition deployment files..."
rm -rf ignition/deployments/horizon-localhost

# Kill hardhat node
if [ ! -z "$NODE_PID" ]; then
echo "Cleaning up node process..."
kill $NODE_PID 2>/dev/null || true
fi
}

# Set trap to call cleanup function on script exit (normal or error)
trap cleanup EXIT

# Check required env variables
if [ -z "$ARBITRUM_SEPOLIA_RPC" ]; then
echo "ARBITRUM_SEPOLIA_RPC environment variable is required"
exit 1
fi

echo "Starting e2e tests..."

# Check if port 8545 is in use
if lsof -i:8545 > /dev/null 2>&1; then
echo "Error: Port 8545 is already in use"
exit 1
fi

# Start local hardhat node forked from Arbitrum Sepolia
echo "Starting local hardhat node..."
npx hardhat node --fork $ARBITRUM_SEPOLIA_RPC > node.log 2>&1 &
NODE_PID=$!

# Wait for node to start
sleep 10

# Run migration steps
echo "Running migration steps..."

# Step 1 - Deployer
echo "Step 1 - Deployer"
npx hardhat deploy:migrate --network localhost --step 1

# Step 2 - Governor
echo "Step 2 - Governor"
npx hardhat deploy:migrate --network localhost --step 2 --patch-config

# Step 3 - Deployer
echo "Step 3 - Deployer"
npx hardhat deploy:migrate --network localhost --step 3 --patch-config

# Step 4 - Governor
echo "Step 4 - Governor"
npx hardhat deploy:migrate --network localhost --step 4 --patch-config

# Run integration tests - During transition period
echo "Running during-transition-period tests..."
npx hardhat test:integration --phase during-transition --network localhost

# Clear thawing period
echo "Clearing thawing period..."
npx hardhat transition:clear-thawing --network localhost --governor-index 0

# Run integration tests - After transition period
echo "Running after-transition-period tests..."
npx hardhat test:integration --phase after-transition --network localhost

# Enable delegation slashing
echo "Enabling delegation slashing..."
npx hardhat transition:enable-delegation-slashing --network localhost --governor-index 0

# Run integration tests - After delegation slashing enabled
echo "Running after-delegation-slashing tests..."
npx hardhat test:integration --phase after-delegation-slashing --network localhost

echo "E2E tests completed successfully!"
31 changes: 31 additions & 0 deletions packages/horizon/tasks/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TASK_TEST } from 'hardhat/builtin-tasks/task-names'
import { task } from 'hardhat/config'
import { glob } from 'glob'

task('test:integration', 'Runs all integration tests')
.addParam(
'phase',
'Test phase to run: "during-transition", "after-transition", "after-delegation-slashing"',
)
.setAction(async (taskArgs, hre) => {
// Get test files for each phase
const duringTransitionPeriodFiles = await glob('test/integration/during-transition-period/**/*.{js,ts}')
const afterTransitionPeriodFiles = await glob('test/integration/after-transition-period/**/*.{js,ts}')
const afterDelegationSlashingEnabledFiles = await glob('test/integration/after-delegation-slashing-enabled/**/*.{js,ts}')

switch (taskArgs.phase) {
case 'during-transition':
await hre.run(TASK_TEST, { testFiles: duringTransitionPeriodFiles })
break
case 'after-transition':
await hre.run(TASK_TEST, { testFiles: afterTransitionPeriodFiles })
break
case 'after-delegation-slashing':
await hre.run(TASK_TEST, { testFiles: afterDelegationSlashingEnabledFiles })
break
default:
throw new Error(
'Invalid phase. Must be "during-transition", "after-transition", "after-delegation-slashing", or "all"',
)
}
})
30 changes: 30 additions & 0 deletions packages/horizon/tasks/transitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { task, types } from 'hardhat/config'

task('transition:clear-thawing', 'Clears the thawing period in HorizonStaking')
.addOptionalParam('governorIndex', 'Index of the governor account in getSigners array', 0, types.int)
.setAction(async (taskArgs, hre) => {
const signers = await hre.ethers.getSigners()
const governor = signers[taskArgs.governorIndex]
const horizonStaking = hre.graph().horizon!.contracts.HorizonStaking

console.log('Clearing thawing period...')
const tx = await horizonStaking.connect(governor).clearThawingPeriod()
await tx.wait()
console.log('Thawing period cleared')
})

task('transition:enable-delegation-slashing', 'Enables delegation slashing in HorizonStaking')
.addOptionalParam('governorIndex', 'Index of the governor account in getSigners array', 0, types.int)
.setAction(async (taskArgs, hre) => {
const signers = await hre.ethers.getSigners()
const governor = signers[taskArgs.governorIndex]
const horizonStaking = hre.graph().horizon!.contracts.HorizonStaking

console.log('Enabling delegation slashing...')
const tx = await horizonStaking.connect(governor).setDelegationSlashingEnabled()
await tx.wait()

// Log if the delegation slashing is enabled
const delegationSlashingEnabled = await horizonStaking.isDelegationSlashingEnabled()
console.log('Delegation slashing enabled:', delegationSlashingEnabled)
})
Loading
Loading