|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2024 Aztec Labs. |
| 3 | +pragma solidity >=0.8.27; |
| 4 | + |
| 5 | +import {Test} from "forge-std/Test.sol"; |
| 6 | + |
| 7 | +import {IInstance} from "@aztec/core/interfaces/IInstance.sol"; |
| 8 | +import {MultiAdder, CheatDepositArgs} from "@aztec/mock/MultiAdder.sol"; |
| 9 | +import {G1Point, G2Point} from "@aztec/shared/libraries/BN254Lib.sol"; |
| 10 | +import {RollupBuilder} from "@test/builder/RollupBuilder.sol"; |
| 11 | +import {TestERC20} from "@aztec/mock/TestERC20.sol"; |
| 12 | + |
| 13 | +struct RegistrationData { |
| 14 | + address attester; |
| 15 | + G1Point publicKeyInG1; |
| 16 | + G2Point publicKeyInG2; |
| 17 | + G1Point proofOfPossession; |
| 18 | +} |
| 19 | + |
| 20 | +contract MigrationScript is Test { |
| 21 | + address internal constant ME = address(0xf8d7d601759CBcfB78044bA7cA9B0c0D6301A54f); |
| 22 | + |
| 23 | + address public constant WITHDRAWER = address(0xdead); |
| 24 | + |
| 25 | + // Update ME! |
| 26 | + IInstance public INSTANCE; |
| 27 | + TestERC20 public STAKING_ASSET; |
| 28 | + // No more! |
| 29 | + |
| 30 | + MultiAdder public ADDER; |
| 31 | + |
| 32 | + RegistrationData[] public $registrations; |
| 33 | + |
| 34 | + function setUp() public { |
| 35 | + string memory root = vm.projectRoot(); |
| 36 | + string memory path = string.concat(root, "/script/registration_data.json"); |
| 37 | + string memory json = vm.readFile(path); |
| 38 | + bytes memory jsonBytes = vm.parseJson(json); |
| 39 | + RegistrationData[] memory registrations = abi.decode(jsonBytes, (RegistrationData[])); |
| 40 | + |
| 41 | + for (uint256 i = 0; i < registrations.length; i++) { |
| 42 | + $registrations.push(registrations[i]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + function emulate() public { |
| 47 | + // If emulating we will deploy a rollup and other components ahead of time, otherwise |
| 48 | + // you should provide them by updating the values above. |
| 49 | + RollupBuilder builder = new RollupBuilder(ME).setUpdateOwnerships(false).deploy(); |
| 50 | + |
| 51 | + INSTANCE = IInstance(address(builder.getConfig().rollup)); |
| 52 | + STAKING_ASSET = builder.getConfig().testERC20; |
| 53 | + |
| 54 | + vm.prank(STAKING_ASSET.owner()); |
| 55 | + STAKING_ASSET.addMinter(ME); |
| 56 | + |
| 57 | + migrate(); |
| 58 | + } |
| 59 | + |
| 60 | + function migrate() public { |
| 61 | + uint256 activationThreshold = INSTANCE.getActivationThreshold(); |
| 62 | + |
| 63 | + vm.startBroadcast(ME); |
| 64 | + ADDER = new MultiAdder(address(INSTANCE), ME); |
| 65 | + STAKING_ASSET.mint(address(ADDER), activationThreshold * $registrations.length); |
| 66 | + vm.stopBroadcast(); |
| 67 | + |
| 68 | + uint256 start = 0; |
| 69 | + uint256 end = $registrations.length; |
| 70 | + uint256 batchSize = 10; |
| 71 | + |
| 72 | + while (start < end) { |
| 73 | + uint256 batchEnd = start + batchSize; |
| 74 | + if (batchEnd > end) { |
| 75 | + batchEnd = end; |
| 76 | + } |
| 77 | + |
| 78 | + CheatDepositArgs[] memory args = new CheatDepositArgs[](batchEnd - start); |
| 79 | + |
| 80 | + for (uint256 i = start; i < batchEnd; i++) { |
| 81 | + emit log_named_address("Adding validator", $registrations[i].attester); |
| 82 | + args[i - start] = CheatDepositArgs( |
| 83 | + $registrations[i].attester, |
| 84 | + WITHDRAWER, |
| 85 | + $registrations[i].publicKeyInG1, |
| 86 | + $registrations[i].publicKeyInG2, |
| 87 | + $registrations[i].proofOfPossession |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + vm.startBroadcast(ME); |
| 92 | + ADDER.addValidators(args); |
| 93 | + vm.stopBroadcast(); |
| 94 | + |
| 95 | + start = batchEnd; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments