feat: implement emergency migration system and migrate project to ESM #29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD Pipeline | |
| on: | |
| push: | |
| branches: [ main, develop ] | |
| pull_request: | |
| branches: [ main, develop ] | |
| release: | |
| types: [ published ] | |
| jobs: | |
| test: | |
| name: Run Tests | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| node-version: [16.x, 18.x, 20.x] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js ${{ matrix.node-version }} | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ matrix.node-version }} | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run linting | |
| run: npm run lint || echo "Linting not configured" | |
| - name: Build project | |
| run: npm run build | |
| - name: Run tests | |
| run: npm run test:coverage | |
| - name: Run security system tests | |
| run: npm run test:security || echo "Security tests not configured yet" | |
| - name: Upload coverage to Codecov | |
| uses: codecov/codecov-action@v3 | |
| with: | |
| file: ./coverage/lcov.info | |
| flags: unittests | |
| name: codecov-umbrella | |
| fail_ci_if_error: false | |
| test-security: | |
| name: Test Security System | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run security system tests | |
| run: npm run test:security | |
| - name: Validate security components | |
| run: | | |
| node -e " | |
| const { SecurityMonitor } = require('./contracts/security/SecurityMonitor'); | |
| const { AnomalyDetection } = require('./contracts/security/algorithms/AnomalyDetection'); | |
| const { ComplianceEngine } = require('./contracts/security/engines/ComplianceEngine'); | |
| console.log('✅ SecurityMonitor imported successfully'); | |
| console.log('✅ AnomalyDetection imported successfully'); | |
| console.log('✅ ComplianceEngine imported successfully'); | |
| // Test basic functionality | |
| const monitor = new SecurityMonitor(); | |
| console.log('✅ SecurityMonitor instantiated'); | |
| const stats = monitor.getSecurityStatistics(); | |
| console.log('✅ Security statistics accessible:', stats); | |
| " | |
| test-fees: | |
| name: Test Fee Management System | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run fee system tests | |
| run: npm run test:fees | |
| - name: Validate fee calculations | |
| run: | | |
| node -e " | |
| const { FeeManager } = require('./contracts/fees/FeeManager'); | |
| const feeManager = new FeeManager('0xowner'); | |
| // Test basic fee calculation | |
| const result = feeManager.calculateFee(1000, '0xuser', 'TRADE'); | |
| console.log('Basic fee calculation:', result); | |
| // Test batch calculations | |
| const batchResults = feeManager.batchCalculateFees([ | |
| { amount: 1000, userAddress: '0xuser1', transactionType: 'TRADE' }, | |
| { amount: 2000, userAddress: '0xuser2', transactionType: 'TRADE' } | |
| ]); | |
| console.log('Batch calculations:', batchResults); | |
| // Verify gas optimization | |
| const metrics = feeManager.getOptimizationMetrics(); | |
| console.log('Optimization metrics:', metrics); | |
| " | |
| test-upgrade: | |
| name: Test Upgrade Management System | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run upgrade system tests | |
| run: npm run test:upgrade | |
| - name: Validate upgrade functionality | |
| run: | | |
| node -e " | |
| const { UpgradeManager } = require('./contracts/upgrade/UpgradeManager'); | |
| const { UpgradeProxy } = require('./contracts/upgrade/proxies/UpgradeProxy'); | |
| const { ProxyFactory } = require('./contracts/upgrade/proxies/UpgradeProxy'); | |
| // Test upgrade manager initialization | |
| const upgradeManager = new UpgradeManager('0x1234567890123456789012345678901234567890'); | |
| console.log('✅ Upgrade Manager initialized'); | |
| // Test proxy creation | |
| const proxy = ProxyFactory.createProxy('0xadmin', '0ximplementation'); | |
| console.log('✅ Proxy created:', proxy.getImplementation()); | |
| // Test proxy info | |
| const proxyInfo = proxy.getProxyInfo(); | |
| console.log('✅ Proxy info:', proxyInfo); | |
| // Test gas estimation | |
| const gasEstimate = proxy.getGasEstimate('upgrade'); | |
| console.log('✅ Gas estimate for upgrade:', gasEstimate); | |
| " | |
| security: | |
| name: Security Audit | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run security audit | |
| run: npm audit --audit-level moderate | |
| - name: Run CodeQL Analysis | |
| uses: github/codeql-action/analyze@v2 | |
| with: | |
| languages: javascript | |
| deploy-dev: | |
| name: Deploy to Development | |
| runs-on: ubuntu-latest | |
| needs: [test, test-security, test-fees, security] | |
| if: github.ref == 'refs/heads/develop' | |
| environment: development | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Deploy fee system to development | |
| run: npm run deploy:fees development | |
| env: | |
| NODE_ENV: development | |
| - name: Deploy security system to development | |
| run: npm run deploy:security:dev | |
| env: | |
| NODE_ENV: development | |
| deploy-testnet: | |
| name: Deploy to Testnet | |
| runs-on: ubuntu-latest | |
| needs: [test, test-security, test-fees, security] | |
| if: github.ref == 'refs/heads/main' | |
| environment: testnet | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Deploy fee system to testnet | |
| run: npm run deploy:fees testnet | |
| env: | |
| NODE_ENV: testnet | |
| TESTNET_RPC_URL: ${{ secrets.TESTNET_RPC_URL }} | |
| TESTNET_PRIVATE_KEY: ${{ secrets.TESTNET_PRIVATE_KEY }} | |
| - name: Deploy security system to testnet | |
| run: npm run deploy:security:testnet | |
| env: | |
| NODE_ENV: testnet | |
| TESTNET_RPC_URL: ${{ secrets.TESTNET_RPC_URL }} | |
| TESTNET_PRIVATE_KEY: ${{ secrets.TESTNET_PRIVATE_KEY }} | |
| deploy-mainnet: | |
| name: Deploy to Mainnet | |
| runs-on: ubuntu-latest | |
| needs: [test, test-security, test-fees, security] | |
| if: github.event_name == 'release' | |
| environment: mainnet | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Deploy fee system to mainnet | |
| run: npm run deploy:fees mainnet | |
| env: | |
| NODE_ENV: production | |
| MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }} | |
| MAINNET_PRIVATE_KEY: ${{ secrets.MAINNET_PRIVATE_KEY }} | |
| MAINNET_OWNER: ${{ secrets.MAINNET_OWNER }} | |
| MAINNET_TREASURY: ${{ secrets.MAINNET_TREASURY }} | |
| MAINNET_VALIDATORS: ${{ secrets.MAINNET_VALIDATORS }} | |
| MAINNET_DEVELOPERS: ${{ secrets.MAINNET_DEVELOPERS }} | |
| - name: Deploy security system to mainnet | |
| run: npm run deploy:security:mainnet | |
| env: | |
| NODE_ENV: production | |
| MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }} | |
| MAINNET_PRIVATE_KEY: ${{ secrets.MAINNET_PRIVATE_KEY }} | |
| MAINNET_OWNER: ${{ secrets.MAINNET_OWNER }} | |
| MAINNET_TREASURY: ${{ secrets.MAINNET_TREASURY }} | |
| MAINNET_VALIDATORS: ${{ secrets.MAINNET_VALIDATORS }} | |
| MAINNET_DEVELOPERS: ${{ secrets.MAINNET_DEVELOPERS }} | |
| validate-deployment: | |
| name: Validate Deployment | |
| runs-on: ubuntu-latest | |
| needs: [deploy-dev, deploy-testnet, deploy-mainnet] | |
| if: always() && (needs.deploy-dev.result == 'success' || needs.deploy-testnet.result == 'success' || needs.deploy-mainnet.result == 'success') | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Validate deployment | |
| run: | | |
| node -e " | |
| const { FeeSystemDeployer } = require('./scripts/deploy_fees'); | |
| const { SecuritySystemDeployer } = require('./scripts/deploy_security_monitor'); | |
| // Load latest deployment | |
| const fs = require('fs'); | |
| const deployments = fs.readdirSync('./deployments') | |
| .filter(f => f.endsWith('.json')) | |
| .sort() | |
| .reverse(); | |
| if (deployments.length > 0) { | |
| const latestDeployment = JSON.parse(fs.readFileSync('./deployments/' + deployments[0], 'utf8')); | |
| console.log('Validating deployment:', latestDeployment.deploymentHash); | |
| // Perform validation checks | |
| console.log('✅ Deployment file found and valid'); | |
| console.log('✅ Network:', latestDeployment.network); | |
| console.log('✅ Timestamp:', new Date(latestDeployment.timestamp)); | |
| console.log('✅ Gas used:', latestDeployment.gasUsed); | |
| // Validate upgrade system if present | |
| if (latestDeployment.upgradeManager) { | |
| console.log('✅ Upgrade Manager deployed:', latestDeployment.upgradeManager); | |
| } | |
| if (latestDeployment.proxy) { | |
| console.log('✅ Proxy deployed:', latestDeployment.proxy); | |
| } | |
| } else { | |
| console.log('⚠️ No deployment files found'); | |
| } | |
| // Validate security system deployment | |
| console.log('🔒 Validating security system deployment...'); | |
| try { | |
| const securityConfig = require('./scripts/deploy_security_monitor').SECURITY_MONITOR_CONFIGS.development; | |
| console.log('✅ Security configuration loaded'); | |
| console.log('✅ Jurisdictions:', securityConfig.jurisdictions); | |
| console.log('✅ Emergency controls:', securityConfig.enableEmergencyControls); | |
| } catch (error) { | |
| console.log('⚠️ Security system validation failed:', error.message); | |
| } | |
| " | |
| performance: | |
| name: Performance Tests | |
| runs-on: ubuntu-latest | |
| needs: [test, test-security, test-fees] | |
| if: github.event_name == 'pull_request' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run performance benchmarks | |
| run: | | |
| node -e " | |
| const { FeeManager } = require('./contracts/fees/FeeManager'); | |
| const { FeeCalculation } = require('./contracts/fees/libraries/FeeCalculation'); | |
| const { SecurityMonitor } = require('./contracts/security/SecurityMonitor'); | |
| const { AnomalyDetection } = require('./contracts/security/algorithms/AnomalyDetection'); | |
| const feeManager = new FeeManager('0xowner'); | |
| const securityMonitor = new SecurityMonitor(); | |
| // Performance test: Fee calculations | |
| console.time('Fee calculations'); | |
| for (let i = 0; i < 1000; i++) { | |
| feeManager.calculateFee(1000 + i, '0xuser' + i, 'TRADE'); | |
| } | |
| console.timeEnd('Fee calculations'); | |
| // Performance test: Batch fee calculations | |
| console.time('Batch fee calculations'); | |
| const requests = Array.from({ length: 1000 }, (_, i) => ({ | |
| amount: 1000 + i, | |
| userAddress: '0xuser' + i, | |
| transactionType: 'TRADE' | |
| })); | |
| feeManager.batchCalculateFees(requests); | |
| console.timeEnd('Batch fee calculations'); | |
| // Performance test: Security monitoring | |
| console.time('Security monitoring'); | |
| for (let i = 0; i < 1000; i++) { | |
| securityMonitor.monitorTransaction( | |
| '0xuser' + i, | |
| '0xcontract', | |
| 1000 + i, | |
| '0xdata', | |
| 21000 | |
| ); | |
| } | |
| console.timeEnd('Security monitoring'); | |
| // Performance test: Anomaly detection | |
| console.time('Anomaly detection'); | |
| for (let i = 0; i < 100; i++) { | |
| securityMonitor.detectAnomalies('0xuser' + i, 3600000); | |
| } | |
| console.timeEnd('Anomaly detection'); | |
| // Gas optimization metrics | |
| const feeMetrics = feeManager.getOptimizationMetrics(); | |
| console.log('Fee optimization metrics:', feeMetrics); | |
| const securityStats = securityMonitor.getSecurityStatistics(); | |
| console.log('Security statistics:', securityStats); | |
| " | |
| documentation: | |
| name: Build Documentation | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Validate documentation | |
| run: | | |
| # Check if all required documentation files exist | |
| if [ ! -f 'docs/fees/FeeManager.md' ]; then | |
| echo '❌ FeeManager documentation missing' | |
| exit 1 | |
| fi | |
| if [ ! -f 'docs/security/SecurityMonitor.md' ]; then | |
| echo '❌ SecurityMonitor documentation missing' | |
| exit 1 | |
| fi | |
| if [ ! -f 'README.md' ]; then | |
| echo '❌ README.md missing' | |
| exit 1 | |
| fi | |
| echo '✅ All documentation files present' | |
| # Check documentation quality | |
| echo '📊 Documentation statistics:' | |
| echo 'FeeManager.md lines:' $(wc -l < docs/fees/FeeManager.md) | |
| echo 'SecurityMonitor.md lines:' $(wc -l < docs/security/SecurityMonitor.md) | |
| echo 'README.md lines:' $(wc -l < README.md) | |
| notify: | |
| name: Notify Results | |
| runs-on: ubuntu-latest | |
| needs: [test, test-security, test-fees, security, deploy-dev, deploy-testnet, deploy-mainnet, validate-deployment, performance, documentation] | |
| if: always() | |
| steps: | |
| - name: Notify success | |
| if: needs.test.result == 'success' && needs.test-security.result == 'success' && needs.test-fees.result == 'success' && needs.security.result == 'success' | |
| run: | | |
| echo '🎉 All tests passed successfully!' | |
| echo '✅ Unit tests: PASSED' | |
| echo '✅ Security system tests: PASSED' | |
| echo '✅ Fee system tests: PASSED' | |
| echo '✅ Upgrade system tests: PASSED' | |
| echo '✅ Security audit: PASSED' | |
| - name: Notify deployment | |
| if: needs.deploy-dev.result == 'success' || needs.deploy-testnet.result == 'success' || needs.deploy-mainnet.result == 'success' | |
| run: | | |
| echo '🚀 Deployment completed!' | |
| if [ '${{ needs.deploy-dev.result }}' == 'success' ]; then | |
| echo '✅ Development environment: DEPLOYED' | |
| fi | |
| if [ '${{ needs.deploy-testnet.result }}' == 'success' ]; then | |
| echo '✅ Testnet environment: DEPLOYED' | |
| fi | |
| if [ '${{ needs.deploy-mainnet.result }}' == 'success' ]; then | |
| echo '✅ Mainnet environment: DEPLOYED' | |
| fi | |
| - name: Notify failures | |
| if: needs.test.result == 'failure' || needs.test-security.result == 'failure' || needs.test-fees.result == 'failure' || needs.security.result == 'failure' | |
| run: | | |
| echo '❌ Pipeline failed!' | |
| if [ '${{ needs.test.result }}' == 'failure' ]; then | |
| echo '❌ Unit tests: FAILED' | |
| fi | |
| if [ '${{ needs.test-security.result }}' == 'failure' ]; then | |
| echo '❌ Security system tests: FAILED' | |
| fi | |
| if [ '${{ needs.test-fees.result }}' == 'failure' ]; then | |
| echo '❌ Fee system tests: FAILED' | |
| fi | |
| if [ '${{ needs.test-upgrade.result }}' == 'failure' ]; then | |
| echo '❌ Upgrade system tests: FAILED' | |
| fi | |
| if [ '${{ needs.security.result }}' == 'failure' ]; then | |
| echo '❌ Security audit: FAILED' | |
| fi | |
| exit 1 |