Deep EVM #20: CI/CD para Contratos Inteligentes — Testing, Regresión de Gas y Seguridad
Engineering Team
La necesidad de CI/CD en smart contracts
Los contratos inteligentes son inmutables una vez desplegados. Un bug en producción no se puede parchear — solo se puede desplegar un nuevo contrato y migrar el estado. Esto hace que el CI/CD sea más crítico que en el software tradicional.
Pipeline completo
# .github/workflows/smart-contracts.yml
name: Smart Contracts CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: foundry-rs/foundry-toolchain@v1
- run: forge fmt --check
- run: forge build --sizes # Verificar tamaño de contratos
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: foundry-rs/foundry-toolchain@v1
- run: forge test -vvv
- run: forge test --gas-report > gas-report.txt
- uses: actions/upload-artifact@v4
with:
name: gas-report
path: gas-report.txt
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pip install slither-analyzer
- run: slither . --print human-summary
- run: slither . --detect reentrancy-eth,reentrancy-no-eth
gas-regression:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: foundry-rs/foundry-toolchain@v1
- run: forge snapshot
- run: forge snapshot --diff .gas-snapshot
continue-on-error: true
Seguimiento de regresión de gas
Foundry genera snapshots de gas que permiten detectar regresiones:
# Generar snapshot base
forge snapshot
# Comparar contra snapshot anterior
forge snapshot --diff .gas-snapshot
Salida:
testTransfer() (gas: 25000 -> 27000) REGRESSION (+8%)
testApprove() (gas: 22000 -> 22000) OK
testMint() (gas: 45000 -> 43000) IMPROVEMENT (-4.4%)
Configurar un umbral de alerta (e.g., fallar CI si algún test aumenta > 5% en gas) previene regresiones accidentales.
Análisis estático con Slither
Slither analiza contratos Solidity buscando vulnerabilidades conocidas:
slither . --detect reentrancy-eth,reentrancy-no-eth,\
uninitialized-state,arbitrary-send-eth,\
controlled-delegatecall,unchecked-transfer
Para Huff, Slither no es directamente aplicable, pero puedes analizar la interfaz Solidity que define los tests.
Verificación automatizada en Etherscan
verify:
needs: [test, security]
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: foundry-rs/foundry-toolchain@v1
- run: |
forge verify-contract $CONTRACT_ADDRESS \
src/MiContrato.sol:MiContrato \
--chain mainnet \
--etherscan-api-key $ETHERSCAN_KEY
Controles de seguridad pre-despliegue
Antes de desplegar, verificar:
- Todos los tests pasan incluyendo fork tests contra mainnet
- Sin regresiones de gas significativas
- Slither no reporta vulnerabilidades de severidad alta
- Código verificable en Etherscan
- Timelock configurado para funciones de admin
- Multi-firma para ownership
Conclusión
Un pipeline CI/CD robusto para contratos inteligentes combina testing exhaustivo, análisis estático, tracking de gas, y verificación automatizada. Dado que los contratos son inmutables, invertir en infraestructura de CI/CD no es un lujo — es una necesidad absoluta.