From 6742ac5750f43853563469ab3f7f55d7517e4575 Mon Sep 17 00:00:00 2001 From: kanthub <564080210@qq.com> Date: Fri, 10 Oct 2025 11:03:04 +0800 Subject: [PATCH 1/3] test and deploy scripts complements --- foundry.lock | 11 + script/Event/deployEvent.s.sol | 109 ++++ script/Event/deployEventPod.s.sol | 85 +++ script/Event/upgradeEventPod.s.sol | 57 ++ script/{ => Oracle}/deployOracle.s.sol | 58 +- script/{ => Oracle}/deployOraclePod.s.sol | 49 +- script/{ => Oracle}/upgradeOraclePod.s.sol | 21 +- script/Vrf/deployVrf.s.sol | 110 ++++ script/Vrf/deployVrfPod.s.sol | 81 +++ script/Vrf/upgradeVrfPod.s.sol | 57 ++ src/bls/BLSApkRegistry.sol | 167 ++++-- src/core/EventManager.sol | 33 +- src/core/OracleManager.sol | 32 +- src/core/PodManager.sol | 20 +- src/core/VrfManager.sol | 35 +- src/interfaces/IBLSApkRegistry.sol | 23 +- src/interfaces/IOracleManager.sol | 2 +- src/interfaces/IVrfManager.sol | 4 +- src/pod/EventPod.sol | 41 +- test/TestEvent.t.sol | 394 ++++++++++++ test/TestOracle.t.sol | 664 +++++++++++---------- test/TestVrf.t.sol | 362 +++++++++++ test/Test_BLSApkRegistry.t.sol | 45 +- 23 files changed, 2000 insertions(+), 460 deletions(-) create mode 100644 foundry.lock create mode 100644 script/Event/deployEvent.s.sol create mode 100644 script/Event/deployEventPod.s.sol create mode 100644 script/Event/upgradeEventPod.s.sol rename script/{ => Oracle}/deployOracle.s.sol (60%) rename script/{ => Oracle}/deployOraclePod.s.sol (57%) rename script/{ => Oracle}/upgradeOraclePod.s.sol (71%) create mode 100644 script/Vrf/deployVrf.s.sol create mode 100644 script/Vrf/deployVrfPod.s.sol create mode 100644 script/Vrf/upgradeVrfPod.s.sol create mode 100644 test/TestEvent.t.sol create mode 100644 test/TestVrf.t.sol diff --git a/foundry.lock b/foundry.lock new file mode 100644 index 0000000..6ba2638 --- /dev/null +++ b/foundry.lock @@ -0,0 +1,11 @@ +{ + "lib/forge-std": { + "rev": "77041d2ce690e692d6e03cc812b57d1ddaa4d505" + }, + "lib/openzeppelin-contracts": { + "rev": "e4f70216d759d8e6a64144a9e1f7bbeed78e7079" + }, + "lib/openzeppelin-contracts-upgradeable": { + "rev": "60b305a8f3ff0c7688f02ac470417b6bbf1c4d27" + } +} \ No newline at end of file diff --git a/script/Event/deployEvent.s.sol b/script/Event/deployEvent.s.sol new file mode 100644 index 0000000..056b936 --- /dev/null +++ b/script/Event/deployEvent.s.sol @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import "forge-std/Vm.sol"; +import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; +import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; + +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {BLSApkRegistry} from "../../src/bls/BLSApkRegistry.sol"; +import {EventManager} from "../../src/core/EventManager.sol"; +import {console, Script} from "forge-std/Script.sol"; + +contract deployEventScript is Script { + EmptyContract public emptyContract; + + ProxyAdmin public blsApkRegistryProxyAdmin; + ProxyAdmin public eventManagerAdmin; + + BLSApkRegistry public blsApkRegistry; + BLSApkRegistry public blsApkRegistryImplementation; + + EventManager public eventManager; + EventManager public eventManagerImplementation; + + function run() public { + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + address relayerManagerAddr = vm.envAddress("RELAYER_MANAGER"); + + address deployerAddress = vm.addr(deployerPrivateKey); + vm.startBroadcast(deployerPrivateKey); + + // Deploy BLSApkRegistry proxy and delegate to a empty contract first + emptyContract = new EmptyContract(); + TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); + blsApkRegistry = BLSApkRegistry(address(proxyBlsApkRegistry)); + blsApkRegistryImplementation = new BLSApkRegistry(); + blsApkRegistryProxyAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyBlsApkRegistry)) + ); + + // Deploy EventManager proxy and delegate to a empty contract first + TransparentUpgradeableProxy proxyEventManager = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); + eventManager = EventManager(address(proxyEventManager)); + eventManagerImplementation = new EventManager(); + eventManagerAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyEventManager)) + ); + + // Upgrade and initialize the implementations + blsApkRegistryProxyAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(address(blsApkRegistry)), + address(blsApkRegistryImplementation), + abi.encodeWithSelector( + BLSApkRegistry.initialize.selector, + deployerAddress, + relayerManagerAddr, + address(proxyEventManager) + ) + ); + + eventManagerAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(address(eventManager)), + address(eventManagerImplementation), + abi.encodeWithSelector( + EventManager.initialize.selector, + deployerAddress, + proxyBlsApkRegistry, + deployerAddress + ) + ); + + console.log( + "deploy proxyBlsApkRegistry:", + address(proxyBlsApkRegistry) + ); + console.log("deploy proxyEventManager:", address(proxyEventManager)); + string memory path = "deployed_addresses.json"; + string memory data = string( + abi.encodePacked( + '{"proxyBlsApkRegistry": "', + vm.toString(address(proxyBlsApkRegistry)), + '", ', + '"proxyEventManager": "', + vm.toString(address(proxyEventManager)), + '"}' + ) + ); + vm.writeJson(data, path); + vm.stopBroadcast(); + } + + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { + address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; + Vm vm = Vm(CHEATCODE_ADDRESS); + + bytes32 adminSlot = vm.load(proxy, ERC1967Utils.ADMIN_SLOT); + return address(uint160(uint256(adminSlot))); + } +} diff --git a/script/Event/deployEventPod.s.sol b/script/Event/deployEventPod.s.sol new file mode 100644 index 0000000..f890131 --- /dev/null +++ b/script/Event/deployEventPod.s.sol @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import "forge-std/Vm.sol"; +import {console, Script} from "forge-std/Script.sol"; +import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; +import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; + +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {EventPod} from "../../src/pod/EventPod.sol"; +import {EventManager} from "../../src/core/EventManager.sol"; + +import {IEventManager} from "../../src/interfaces/IEventManager.sol"; +import {IEventPod} from "../../src/interfaces/IEventPod.sol"; + +contract deployEventPodScript is Script { + EmptyContract public emptyContract; + + ProxyAdmin public eventPodAdmin; + EventPod public eventPod; + EventPod public eventPodImplementation; + + function run() public { + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + address eventManagerAddr = vm.envAddress("ORACLE_MANAGER"); + + address deployerAddress = vm.addr(deployerPrivateKey); + + vm.startBroadcast(deployerPrivateKey); + + emptyContract = new EmptyContract(); + TransparentUpgradeableProxy proxyEventPod = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); + eventPod = EventPod(address(proxyEventPod)); + eventPodImplementation = new EventPod(); + eventPodAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyEventPod)) + ); + + console.log( + "eventPodImplementation===", + address(eventPodImplementation) + ); + + eventPodAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(address(eventPod)), + address(eventPodImplementation), + abi.encodeWithSelector( + EventPod.initialize.selector, + deployerAddress, + eventManagerAddr + ) + ); + + // EventManager(eventManagerAddr).addEventPodToFillWhitelist(proxyEventPod); + + console.log("deploy proxyEventPod:", address(proxyEventPod)); + string memory path = "deployed_addresses.json"; + string memory data = string( + abi.encodePacked( + '{"proxyEventPod": "', + vm.toString(address(proxyEventPod)), + '", ', + '"eventPodImplementation": "', + vm.toString(address(eventPodImplementation)), + '"}' + ) + ); + vm.writeJson(data, path); + vm.stopBroadcast(); + } + + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { + address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; + Vm vm = Vm(CHEATCODE_ADDRESS); + + bytes32 adminSlot = vm.load(proxy, ERC1967Utils.ADMIN_SLOT); + return address(uint160(uint256(adminSlot))); + } +} diff --git a/script/Event/upgradeEventPod.s.sol b/script/Event/upgradeEventPod.s.sol new file mode 100644 index 0000000..b8ed535 --- /dev/null +++ b/script/Event/upgradeEventPod.s.sol @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import "forge-std/Vm.sol"; +import {console, Script} from "forge-std/Script.sol"; +import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; +import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; + +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {EventPod} from "../../src/pod/EventPod.sol"; +import {IEventManager} from "../../src/interfaces/IEventManager.sol"; +import {IEventPod} from "../../src/interfaces/IEventPod.sol"; + +contract upgradeEventPodScript is Script { + address public ORACLE_POD = vm.envAddress("ORACLE_POD"); + + function run() public { + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + address deployerAddress = vm.addr(deployerPrivateKey); + + console.log("Deployer address:", deployerAddress); + console.log("Event Pod Proxy:", ORACLE_POD); + + address proxyAdminAddress = getProxyAdminAddress(ORACLE_POD); + console.log("Calculated Event Pod Proxy Admin:", proxyAdminAddress); + + ProxyAdmin messageManagerProxyAdmin = ProxyAdmin(proxyAdminAddress); + + vm.startBroadcast(deployerPrivateKey); + + EventPod newEventPodImplementation = new EventPod(); + + console.log( + "New EventPod implementation:", + address(newEventPodImplementation) + ); + + messageManagerProxyAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(ORACLE_POD), + address(newEventPodImplementation), + "" + ); + + console.log("Upgrade completed successfully!"); + vm.stopBroadcast(); + } + + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { + address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; + Vm vm = Vm(CHEATCODE_ADDRESS); + + bytes32 adminSlot = vm.load(proxy, ERC1967Utils.ADMIN_SLOT); + return address(uint160(uint256(adminSlot))); + } +} diff --git a/script/deployOracle.s.sol b/script/Oracle/deployOracle.s.sol similarity index 60% rename from script/deployOracle.s.sol rename to script/Oracle/deployOracle.s.sol index e4b9a8a..01a7e43 100644 --- a/script/deployOracle.s.sol +++ b/script/Oracle/deployOracle.s.sol @@ -5,10 +5,10 @@ import "forge-std/Vm.sol"; import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; -import { EmptyContract } from "../src/utils/EmptyContract.sol"; -import { BLSApkRegistry } from "../src/bls/BLSApkRegistry.sol"; -import { OracleManager } from "../src/core/OracleManager.sol"; -import { console, Script } from "forge-std/Script.sol"; +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {BLSApkRegistry} from "../../src/bls/BLSApkRegistry.sol"; +import {OracleManager} from "../../src/core/OracleManager.sol"; +import {console, Script} from "forge-std/Script.sol"; contract deployOracleScript is Script { EmptyContract public emptyContract; @@ -24,24 +24,37 @@ contract deployOracleScript is Script { function run() public { uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); - address relayerManagerAddr = vm.envAddress("RELAYER_MANAGER"); + address relayerManagerAddr = vm.envAddress("RELAYER_MANAGER"); address deployerAddress = vm.addr(deployerPrivateKey); vm.startBroadcast(deployerPrivateKey); + // Deploy BLSApkRegistry proxy and delegate to a empty contract first emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); + TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); blsApkRegistry = BLSApkRegistry(address(proxyBlsApkRegistry)); blsApkRegistryImplementation = new BLSApkRegistry(); - blsApkRegistryProxyAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyBlsApkRegistry))); - + blsApkRegistryProxyAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyBlsApkRegistry)) + ); - TransparentUpgradeableProxy proxyOracleManager = new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); + // Deploy OracleManager proxy and delegate to a empty contract first + TransparentUpgradeableProxy proxyOracleManager = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); oracleManager = OracleManager(address(proxyOracleManager)); oracleManagerImplementation = new OracleManager(); - oracleManagerAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyOracleManager))); - + oracleManagerAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyOracleManager)) + ); + // Upgrade and initialize the implementations blsApkRegistryProxyAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(blsApkRegistry)), address(blsApkRegistryImplementation), @@ -64,18 +77,29 @@ contract deployOracleScript is Script { ) ); - console.log("deploy proxyBlsApkRegistry:", address(proxyBlsApkRegistry)); + console.log( + "deploy proxyBlsApkRegistry:", + address(proxyBlsApkRegistry) + ); console.log("deploy proxyOracleManager:", address(proxyOracleManager)); string memory path = "deployed_addresses.json"; - string memory data = string(abi.encodePacked( - '{"proxyBlsApkRegistry": "', vm.toString(address(proxyBlsApkRegistry)), '", ', - '"proxyOracleManager": "', vm.toString(address(proxyOracleManager)), '"}' - )); + string memory data = string( + abi.encodePacked( + '{"proxyBlsApkRegistry": "', + vm.toString(address(proxyBlsApkRegistry)), + '", ', + '"proxyOracleManager": "', + vm.toString(address(proxyOracleManager)), + '"}' + ) + ); vm.writeJson(data, path); vm.stopBroadcast(); } - function getProxyAdminAddress(address proxy) internal view returns (address) { + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/deployOraclePod.s.sol b/script/Oracle/deployOraclePod.s.sol similarity index 57% rename from script/deployOraclePod.s.sol rename to script/Oracle/deployOraclePod.s.sol index 7cabfe2..e7d40cb 100644 --- a/script/deployOraclePod.s.sol +++ b/script/Oracle/deployOraclePod.s.sol @@ -2,17 +2,16 @@ pragma solidity ^0.8.20; import "forge-std/Vm.sol"; -import { console, Script } from "forge-std/Script.sol"; +import {console, Script} from "forge-std/Script.sol"; import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; -import { EmptyContract } from "../src/utils/EmptyContract.sol"; -import { OraclePod } from "../src/pod/OraclePod.sol"; -import { OracleManager } from "../src/core/OracleManager.sol"; - -import { IOracleManager } from "../src/interfaces/IOracleManager.sol"; -import { IOraclePod } from "../src/interfaces/IOraclePod.sol"; +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {OraclePod} from "../../src/pod/OraclePod.sol"; +import {OracleManager} from "../../src/core/OracleManager.sol"; +import {IOracleManager} from "../../src/interfaces/IOracleManager.sol"; +import {IOraclePod} from "../../src/interfaces/IOraclePod.sol"; contract deployOraclePodScript is Script { EmptyContract public emptyContract; @@ -30,12 +29,21 @@ contract deployOraclePodScript is Script { vm.startBroadcast(deployerPrivateKey); emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyOraclePod = new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); + TransparentUpgradeableProxy proxyOraclePod = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); oraclePod = OraclePod(address(proxyOraclePod)); oraclePodImplementation = new OraclePod(); - oraclePodAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyOraclePod))); + oraclePodAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyOraclePod)) + ); - console.log("oraclePodImplementation===", address(oraclePodImplementation)); + console.log( + "oraclePodImplementation===", + address(oraclePodImplementation) + ); oraclePodAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(oraclePod)), @@ -47,20 +55,27 @@ contract deployOraclePodScript is Script { ) ); -// OracleManager(oracleManagerAddr).addOraclePodToFillWhitelist(proxyOraclePod); - + // OracleManager(oracleManagerAddr).addOraclePodToFillWhitelist(proxyOraclePod); console.log("deploy proxyOraclePod:", address(proxyOraclePod)); string memory path = "deployed_addresses.json"; - string memory data = string(abi.encodePacked( - '{"proxyOraclePod": "', vm.toString(address(proxyOraclePod)), '", ', - '"oraclePodImplementation": "', vm.toString(address(oraclePodImplementation)), '"}' - )); + string memory data = string( + abi.encodePacked( + '{"proxyOraclePod": "', + vm.toString(address(proxyOraclePod)), + '", ', + '"oraclePodImplementation": "', + vm.toString(address(oraclePodImplementation)), + '"}' + ) + ); vm.writeJson(data, path); vm.stopBroadcast(); } - function getProxyAdminAddress(address proxy) internal view returns (address) { + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/upgradeOraclePod.s.sol b/script/Oracle/upgradeOraclePod.s.sol similarity index 71% rename from script/upgradeOraclePod.s.sol rename to script/Oracle/upgradeOraclePod.s.sol index b2a0da1..60fde6d 100644 --- a/script/upgradeOraclePod.s.sol +++ b/script/Oracle/upgradeOraclePod.s.sol @@ -2,16 +2,16 @@ pragma solidity ^0.8.20; import "forge-std/Vm.sol"; -import { console, Script } from "forge-std/Script.sol"; +import {console, Script} from "forge-std/Script.sol"; import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; -import { EmptyContract } from "../src/utils/EmptyContract.sol"; -import { OraclePod } from "../src/pod/OraclePod.sol"; -import { IOracleManager } from "../src/interfaces/IOracleManager.sol"; -import { IOraclePod } from "../src/interfaces/IOraclePod.sol"; +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {OraclePod} from "../../src/pod/OraclePod.sol"; +import {IOracleManager} from "../../src/interfaces/IOracleManager.sol"; +import {IOraclePod} from "../../src/interfaces/IOraclePod.sol"; -contract upgradeOraclePodScript is Script { +contract upgradeOraclePodScript is Script { address public ORACLE_POD = vm.envAddress("ORACLE_POD"); function run() public { @@ -30,7 +30,10 @@ contract upgradeOraclePodScript is Script { OraclePod newOraclePodImplementation = new OraclePod(); - console.log("New OraclePod implementation:", address(newOraclePodImplementation)); + console.log( + "New OraclePod implementation:", + address(newOraclePodImplementation) + ); messageManagerProxyAdmin.upgradeAndCall( ITransparentUpgradeableProxy(ORACLE_POD), @@ -42,7 +45,9 @@ contract upgradeOraclePodScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress(address proxy) internal view returns (address) { + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Vrf/deployVrf.s.sol b/script/Vrf/deployVrf.s.sol new file mode 100644 index 0000000..26279a5 --- /dev/null +++ b/script/Vrf/deployVrf.s.sol @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import "forge-std/Vm.sol"; +import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; +import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; + +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {BLSApkRegistry} from "../../src/bls/BLSApkRegistry.sol"; +import {VrfManager} from "../../src/core/VrfManager.sol"; +import {console, Script} from "forge-std/Script.sol"; + +contract deployVrfScript is Script { + EmptyContract public emptyContract; + + ProxyAdmin public blsApkRegistryProxyAdmin; + ProxyAdmin public vrfManagerAdmin; + + BLSApkRegistry public blsApkRegistry; + BLSApkRegistry public blsApkRegistryImplementation; + + VrfManager public vrfManager; + VrfManager public vrfManagerImplementation; + + function run() public { + // owner and relayerManager + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + address relayerManagerAddr = vm.envAddress("RELAYER_MANAGER"); + + address deployerAddress = vm.addr(deployerPrivateKey); + vm.startBroadcast(deployerPrivateKey); + + // Deploy BLSApkRegistry proxy and delegate to a empty contract first + emptyContract = new EmptyContract(); + TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); + blsApkRegistry = BLSApkRegistry(address(proxyBlsApkRegistry)); + blsApkRegistryImplementation = new BLSApkRegistry(); + blsApkRegistryProxyAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyBlsApkRegistry)) + ); + + // Deploy VrfManager proxy and delegate to a empty contract first + TransparentUpgradeableProxy proxyVrfManager = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); + vrfManager = VrfManager(address(proxyVrfManager)); + vrfManagerImplementation = new VrfManager(); + vrfManagerAdmin = ProxyAdmin( + getProxyAdminAddress(address(proxyVrfManager)) + ); + + // Upgrade and initialize the implementations + blsApkRegistryProxyAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(address(blsApkRegistry)), + address(blsApkRegistryImplementation), + abi.encodeWithSelector( + BLSApkRegistry.initialize.selector, + deployerAddress, + relayerManagerAddr, + address(proxyVrfManager) + ) + ); + + vrfManagerAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(address(vrfManager)), + address(vrfManagerImplementation), + abi.encodeWithSelector( + VrfManager.initialize.selector, + deployerAddress, + proxyBlsApkRegistry, + deployerAddress + ) + ); + + console.log( + "deploy proxyBlsApkRegistry:", + address(proxyBlsApkRegistry) + ); + console.log("deploy proxyVrfManager:", address(proxyVrfManager)); + string memory path = "deployed_addresses.json"; + string memory data = string( + abi.encodePacked( + '{"proxyBlsApkRegistry": "', + vm.toString(address(proxyBlsApkRegistry)), + '", ', + '"proxyVrfManager": "', + vm.toString(address(proxyVrfManager)), + '"}' + ) + ); + vm.writeJson(data, path); + vm.stopBroadcast(); + } + + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { + address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; + Vm vm = Vm(CHEATCODE_ADDRESS); + + bytes32 adminSlot = vm.load(proxy, ERC1967Utils.ADMIN_SLOT); + return address(uint160(uint256(adminSlot))); + } +} diff --git a/script/Vrf/deployVrfPod.s.sol b/script/Vrf/deployVrfPod.s.sol new file mode 100644 index 0000000..e405821 --- /dev/null +++ b/script/Vrf/deployVrfPod.s.sol @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import "forge-std/Vm.sol"; +import {console, Script} from "forge-std/Script.sol"; +import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; +import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; + +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {VrfPod} from "../../src/pod/VrfPod.sol"; +import {VrfManager} from "../../src/core/VrfManager.sol"; + +import {IVrfManager} from "../../src/interfaces/IVrfManager.sol"; +import {IVrfPod} from "../../src/interfaces/IVrfPod.sol"; + +contract deployVrfPodScript is Script { + EmptyContract public emptyContract; + + ProxyAdmin public vrfPodAdmin; + VrfPod public vrfPod; + VrfPod public vrfPodImplementation; + + function run() public { + // owner and vrfManager + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + address vrfManagerAddr = vm.envAddress("ORACLE_MANAGER"); + + address deployerAddress = vm.addr(deployerPrivateKey); + + vm.startBroadcast(deployerPrivateKey); + + emptyContract = new EmptyContract(); + TransparentUpgradeableProxy proxyVrfPod = new TransparentUpgradeableProxy( + address(emptyContract), + deployerAddress, + "" + ); + vrfPod = VrfPod(address(proxyVrfPod)); + vrfPodImplementation = new VrfPod(); + vrfPodAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyVrfPod))); + + console.log("vrfPodImplementation===", address(vrfPodImplementation)); + + vrfPodAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(address(vrfPod)), + address(vrfPodImplementation), + abi.encodeWithSelector( + VrfPod.initialize.selector, + deployerAddress, + vrfManagerAddr + ) + ); + + // VrfManager(vrfManagerAddr).addVrfPodToFillWhitelist(proxyVrfPod); + + console.log("deploy proxyVrfPod:", address(proxyVrfPod)); + string memory path = "deployed_addresses.json"; + string memory data = string( + abi.encodePacked( + '{"proxyVrfPod": "', + vm.toString(address(proxyVrfPod)), + '", ', + '"vrfPodImplementation": "', + vm.toString(address(vrfPodImplementation)), + '"}' + ) + ); + vm.writeJson(data, path); + vm.stopBroadcast(); + } + + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { + address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; + Vm vm = Vm(CHEATCODE_ADDRESS); + + bytes32 adminSlot = vm.load(proxy, ERC1967Utils.ADMIN_SLOT); + return address(uint160(uint256(adminSlot))); + } +} diff --git a/script/Vrf/upgradeVrfPod.s.sol b/script/Vrf/upgradeVrfPod.s.sol new file mode 100644 index 0000000..26fd129 --- /dev/null +++ b/script/Vrf/upgradeVrfPod.s.sol @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import "forge-std/Vm.sol"; +import {console, Script} from "forge-std/Script.sol"; +import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol"; +import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; + +import {EmptyContract} from "../../src/utils/EmptyContract.sol"; +import {VrfPod} from "../../src/pod/VrfPod.sol"; +import {IVrfManager} from "../../src/interfaces/IVrfManager.sol"; +import {IVrfPod} from "../../src/interfaces/IVrfPod.sol"; + +contract upgradeVrfPodScript is Script { + address public ORACLE_POD = vm.envAddress("ORACLE_POD"); + + function run() public { + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); + address deployerAddress = vm.addr(deployerPrivateKey); + + console.log("Deployer address:", deployerAddress); + console.log("Vrf Pod Proxy:", ORACLE_POD); + + address proxyAdminAddress = getProxyAdminAddress(ORACLE_POD); + console.log("Calculated Vrf Pod Proxy Admin:", proxyAdminAddress); + + ProxyAdmin messageManagerProxyAdmin = ProxyAdmin(proxyAdminAddress); + + vm.startBroadcast(deployerPrivateKey); + + VrfPod newVrfPodImplementation = new VrfPod(); + + console.log( + "New VrfPod implementation:", + address(newVrfPodImplementation) + ); + + messageManagerProxyAdmin.upgradeAndCall( + ITransparentUpgradeableProxy(ORACLE_POD), + address(newVrfPodImplementation), + "" + ); + + console.log("Upgrade completed successfully!"); + vm.stopBroadcast(); + } + + function getProxyAdminAddress( + address proxy + ) internal view returns (address) { + address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; + Vm vm = Vm(CHEATCODE_ADDRESS); + + bytes32 adminSlot = vm.load(proxy, ERC1967Utils.ADMIN_SLOT); + return address(uint160(uint256(adminSlot))); + } +} diff --git a/src/bls/BLSApkRegistry.sol b/src/bls/BLSApkRegistry.sol index 8a02876..be1032d 100644 --- a/src/bls/BLSApkRegistry.sol +++ b/src/bls/BLSApkRegistry.sol @@ -11,7 +11,13 @@ import "../interfaces/IBLSApkRegistry.sol"; import "./BLSApkRegistryStorage.sol"; -contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, BLSApkRegistryStorage, EIP712 { +contract BLSApkRegistry is + Initializable, + OwnableUpgradeable, + IBLSApkRegistry, + BLSApkRegistryStorage, + EIP712 +{ using BN254 for BN254.G1Point; uint256 internal constant PAIRING_EQUALITY_CHECK_GAS = 120000; @@ -26,7 +32,8 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B modifier onlyOracleManager() { require( - msg.sender == oracleManager, "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" + msg.sender == oracleManager, + "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" ); _; } @@ -35,23 +42,29 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B _disableInitializers(); } - function initialize(address _initialOwner, address _whiteListAddress, address _oracleManager) - external - initializer - { + function initialize( + address _initialOwner, + address _whiteListAddress, + address _oracleManager + ) external initializer { __Ownable_init(_initialOwner); whiteListAddress = _whiteListAddress; oracleManager = _oracleManager; _initializeApk(); } - function registerOperator(address operator) public onlyOracleManager { - require(operator != address(0), "BLSApkRegistry.registerBLSPublicKey: Operator is zero address"); + require( + operator != address(0), + "BLSApkRegistry.registerBLSPublicKey: Operator is zero address" + ); - require(!operatorIsRegister[operator], "BLSApkRegistry.registerBLSPublicKey: Operator have already register"); + require( + !operatorIsRegister[operator], + "BLSApkRegistry.registerBLSPublicKey: Operator have already register" + ); - (BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator); + (BN254.G1Point memory pubkey, ) = getRegisteredPubkey(operator); _processApkUpdate(pubkey); @@ -63,9 +76,12 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B } function deregisterOperator(address operator) public onlyOracleManager { - require(operatorIsRegister[operator], "BLSApkRegistry.registerBLSPublicKey: Operator have already deregister"); + require( + operatorIsRegister[operator], + "BLSApkRegistry.registerBLSPublicKey: Operator have already deregister" + ); - (BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator); + (BN254.G1Point memory pubkey, ) = getRegisteredPubkey(operator); _processApkUpdate(pubkey.negate()); @@ -93,7 +109,10 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B bytes32 pubkeyHash = BN254.hashG1Point(params.pubkeyG1); - require(pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey"); + require( + pubkeyHash != ZERO_PK_HASH, + "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey" + ); require( operatorToPubkeyHash[operator] == bytes32(0), "BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey" @@ -121,9 +140,13 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B require( BN254.pairing( - params.pubkeyRegistrationSignature.plus(params.pubkeyG1.scalar_mul(gamma)), + params.pubkeyRegistrationSignature.plus( + params.pubkeyG1.scalar_mul(gamma) + ), BN254.negGeneratorG2(), - pubkeyRegistrationMessageHash.plus(BN254.generatorG1().scalar_mul(gamma)), + pubkeyRegistrationMessageHash.plus( + BN254.generatorG1().scalar_mul(gamma) + ), params.pubkeyG2 ), "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match" @@ -138,13 +161,14 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B return pubkeyHash; } - function checkSignatures(bytes32 msgHash, uint256 referenceBlockNumber, OracleNonSignerAndSignature memory params) - public - view - returns (uint256, bytes32) - { + function checkSignatures( + bytes32 msgHash, + uint256 referenceBlockNumber, + NonSignerAndSignature memory params + ) public view returns (uint256, bytes32) { require( - referenceBlockNumber < uint32(block.number), "BLSSignatureChecker.checkSignatures: invalid reference block" + referenceBlockNumber < uint32(block.number), + "BLSSignatureChecker.checkSignatures: invalid reference block" ); uint256 nonSingerNode = params.nonSignerPubkeys.length; @@ -158,26 +182,53 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B BN254.G1Point memory signerApk = BN254.G1Point(0, 0); bytes32[] memory nonSignersPubkeyHashes; if (params.nonSignerPubkeys.length > 0) { - nonSignersPubkeyHashes = new bytes32[](params.nonSignerPubkeys.length); + nonSignersPubkeyHashes = new bytes32[]( + params.nonSignerPubkeys.length + ); for (uint256 j = 0; j < params.nonSignerPubkeys.length; j++) { - nonSignersPubkeyHashes[j] = params.nonSignerPubkeys[j].hashG1Point(); - signerApk = currentApk.plus(params.nonSignerPubkeys[j].negate()); + nonSignersPubkeyHashes[j] = params + .nonSignerPubkeys[j] + .hashG1Point(); + signerApk = currentApk.plus( + params.nonSignerPubkeys[j].negate() + ); } } else { signerApk = currentApk; } - (bool pairingSuccessful, bool signatureIsValid) = - trySignatureAndApkVerification(msgHash, signerApk, params.apkG2, params.sigma); - require(pairingSuccessful, "BLSSignatureChecker.checkSignatures: pairing precompile call failed"); - require(signatureIsValid, "BLSSignatureChecker.checkSignatures: signature is invalid"); + ( + bool pairingSuccessful, + bool signatureIsValid + ) = trySignatureAndApkVerification( + msgHash, + signerApk, + params.apkG2, + params.sigma + ); + require( + pairingSuccessful, + "BLSSignatureChecker.checkSignatures: pairing precompile call failed" + ); + require( + signatureIsValid, + "BLSSignatureChecker.checkSignatures: signature is invalid" + ); - bytes32 signatoryRecordHash = keccak256(abi.encodePacked(referenceBlockNumber, nonSignersPubkeyHashes)); + bytes32 signatoryRecordHash = keccak256( + abi.encodePacked(referenceBlockNumber, nonSignersPubkeyHashes) + ); return (params.totalStake, signatoryRecordHash); } - function addOrRemoveBlsRegisterWhitelist(address register, bool isAdd) external onlyWhiteListManager { - require(register != address(0), "BLSApkRegistry.addOrRemoverBlsRegisterWhitelist: operator address is zero"); + function addOrRemoveBlsRegisterWhitelist( + address register, + bool isAdd + ) external onlyWhiteListManager { + require( + register != address(0), + "BLSApkRegistry.addOrRemoverBlsRegisterWhitelist: operator address is zero" + ); blsRegisterWhitelist[register] = isAdd; } @@ -190,7 +241,15 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B uint256 gamma = uint256( keccak256( abi.encodePacked( - msgHash, apk.X, apk.Y, apkG2.X[0], apkG2.X[1], apkG2.Y[0], apkG2.Y[1], sigma.X, sigma.Y + msgHash, + apk.X, + apk.Y, + apkG2.X[0], + apkG2.X[1], + apkG2.Y[0], + apkG2.Y[1], + sigma.X, + sigma.Y ) ) ) % BN254.FR_MODULUS; @@ -207,7 +266,10 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B BN254.G1Point memory newApk; uint256 historyLength = apkHistory.length; - require(historyLength != 0, "BLSApkRegistry._processApkUpdate: quorum does not exist"); + require( + historyLength != 0, + "BLSApkRegistry._processApkUpdate: quorum does not exist" + ); newApk = currentApk.plus(point); currentApk = newApk; @@ -220,29 +282,54 @@ contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, B } else { lastUpdate.nextUpdateBlockNumber = uint32(block.number); apkHistory.push( - ApkUpdate({apkHash: newApkHash, updateBlockNumber: uint32(block.number), nextUpdateBlockNumber: 0}) + ApkUpdate({ + apkHash: newApkHash, + updateBlockNumber: uint32(block.number), + nextUpdateBlockNumber: 0 + }) ); } } - function getRegisteredPubkey(address operator) public view returns (BN254.G1Point memory, bytes32) { + function getRegisteredPubkey( + address operator + ) public view returns (BN254.G1Point memory, bytes32) { BN254.G1Point memory pubkey = operatorToPubkey[operator]; bytes32 pubkeyHash = operatorToPubkeyHash[operator]; - require(pubkeyHash != bytes32(0), "BLSApkRegistry.getRegisteredPubkey: operator is not registered"); + require( + pubkeyHash != bytes32(0), + "BLSApkRegistry.getRegisteredPubkey: operator is not registered" + ); return (pubkey, pubkeyHash); } - function getPubkeyRegMessageHash(address operator) public view returns (BN254.G1Point memory) { - return BN254.hashToG1(_hashTypedDataV4(keccak256(abi.encode(PUBKEY_REGISTRATION_TYPEHASH, operator)))); + function getPubkeyRegMessageHash( + address operator + ) public view returns (BN254.G1Point memory) { + return + BN254.hashToG1( + _hashTypedDataV4( + keccak256( + abi.encode(PUBKEY_REGISTRATION_TYPEHASH, operator) + ) + ) + ); } function _initializeApk() internal { - require(apkHistory.length == 0, "BLSApkRegistry.initializeApk: apk already exists"); + require( + apkHistory.length == 0, + "BLSApkRegistry.initializeApk: apk already exists" + ); apkHistory.push( - ApkUpdate({apkHash: bytes24(0), updateBlockNumber: uint32(block.number), nextUpdateBlockNumber: 0}) + ApkUpdate({ + apkHash: bytes24(0), + updateBlockNumber: uint32(block.number), + nextUpdateBlockNumber: 0 + }) ); } diff --git a/src/core/EventManager.sol b/src/core/EventManager.sol index 6138fe3..fc6a1ff 100644 --- a/src/core/EventManager.sol +++ b/src/core/EventManager.sol @@ -11,7 +11,6 @@ import "../interfaces/IEventPod.sol"; import "./EventManagerStorage.sol"; import "./PodManager.sol"; - contract EventManager is OwnableUpgradeable, PodManager, EventManagerStorage { constructor() { _disableInitializers(); @@ -29,17 +28,31 @@ contract EventManager is OwnableUpgradeable, PodManager, EventManagerStorage { function fillEventResultWithSignature( IEventPod eventPod, PredictEvents calldata predictEvents, - IBLSApkRegistry.OracleNonSignerAndSignature memory oracleNonSignerAndSignature - ) external onlyAggregatorManager onlyPodWhitelistedForFill(address(eventPod)) { - ( - uint256 totalStaking, - bytes32 signatoryRecordHash - ) = blsApkRegistry.checkSignatures(predictEvents.msgHash, predictEvents.blockNumber, oracleNonSignerAndSignature); + IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature + ) + external + onlyAggregatorManager + onlyPodWhitelistedForFill(address(eventPod)) + { + (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry + .checkSignatures( + predictEvents.msgHash, + predictEvents.blockNumber, + oracleNonSignerAndSignature + ); string memory winner = predictEvents.winner; - eventPod.submitEventResult(predictEvents.requestId, predictEvents.winner); - - emit VerifyPredictEventSig(predictEvents.requestId, totalStaking, signatoryRecordHash, winner); + eventPod.submitEventResult( + predictEvents.requestId, + predictEvents.winner + ); + + emit VerifyPredictEventSig( + predictEvents.requestId, + totalStaking, + signatoryRecordHash, + winner + ); } } diff --git a/src/core/OracleManager.sol b/src/core/OracleManager.sol index e4f2d0e..afae3b6 100644 --- a/src/core/OracleManager.sol +++ b/src/core/OracleManager.sol @@ -11,7 +11,12 @@ import "../interfaces/IOraclePod.sol"; import "./OracleManagerStorage.sol"; import "./PodManager.sol"; -contract OracleManager is OwnableUpgradeable, PodManager, OracleManagerStorage, IOracleManager { +contract OracleManager is + OwnableUpgradeable, + PodManager, + OracleManagerStorage, + IOracleManager +{ constructor() { _disableInitializers(); } @@ -29,17 +34,28 @@ contract OracleManager is OwnableUpgradeable, PodManager, OracleManagerStorage, function fillSymbolPriceWithSignature( IOraclePod oraclePod, OracleBatch calldata oracleBatch, - IBLSApkRegistry.OracleNonSignerAndSignature memory oracleNonSignerAndSignature - ) external onlyAggregatorManager onlyPodWhitelistedForFill(address(oraclePod)) { - ( - uint256 totalStaking, - bytes32 signatoryRecordHash - ) = blsApkRegistry.checkSignatures(oracleBatch.msgHash, oracleBatch.blockNumber, oracleNonSignerAndSignature); + IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature + ) + external + onlyAggregatorManager + onlyPodWhitelistedForFill(address(oraclePod)) + { + (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry + .checkSignatures( + oracleBatch.msgHash, + oracleBatch.blockNumber, + oracleNonSignerAndSignature + ); string memory symbolPrice = oracleBatch.symbolPrice; oraclePod.fillSymbolPrice(symbolPrice); - emit VerifyOracleSig(confirmBatchId++, totalStaking, signatoryRecordHash, symbolPrice); + emit VerifyOracleSig( + confirmBatchId++, + totalStaking, + signatoryRecordHash, + symbolPrice + ); } } diff --git a/src/core/PodManager.sol b/src/core/PodManager.sol index 6194b3e..5d71d0c 100644 --- a/src/core/PodManager.sol +++ b/src/core/PodManager.sol @@ -6,7 +6,6 @@ import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; import "../interfaces/IBLSApkRegistry.sol"; - abstract contract PodManager is Initializable, OwnableUpgradeable { IBLSApkRegistry public blsApkRegistry; @@ -62,28 +61,35 @@ abstract contract PodManager is Initializable, OwnableUpgradeable { emit OperatorDeRegistered(msg.sender); } - function addOrRemoveOperatorWhitelist(address operator, bool isAdd) external onlyAggregatorManager { + function addOrRemoveOperatorWhitelist( + address operator, + bool isAdd + ) external onlyAggregatorManager { require( - operator != address (0), + operator != address(0), "PodManager.addOperatorWhitelist: operator address is zero" ); operatorWhitelist[operator] = isAdd; } - function setAggregatorAddress(address _aggregatorAddress) external onlyOwner { + function setAggregatorAddress( + address _aggregatorAddress + ) external onlyOwner { require( - _aggregatorAddress != address (0), + _aggregatorAddress != address(0), "PodManager.addAggregator: aggregatorAddress address is zero" ); aggregatorAddress = _aggregatorAddress; } - function addOraclePodToFillWhitelist(address pod) external onlyAggregatorManager { + function addPodToFillWhitelist(address pod) external onlyAggregatorManager { podIsWhitelistedForFill[pod] = true; emit PodAddedToFillWhitelist(pod); } - function removeOraclePodToFillWhitelist(address pod) external onlyAggregatorManager { + function removePodToFillWhitelist( + address pod + ) external onlyAggregatorManager { podIsWhitelistedForFill[pod] = false; emit PodRemoveToFillWhitelist(pod); } diff --git a/src/core/VrfManager.sol b/src/core/VrfManager.sol index 17f127b..bd937a9 100644 --- a/src/core/VrfManager.sol +++ b/src/core/VrfManager.sol @@ -11,7 +11,6 @@ import "../interfaces/IVrfPod.sol"; import "./PodManager.sol"; import "./VrfManagerStorage.sol"; - contract VrfManager is OwnableUpgradeable, PodManager, VrfManagerStorage { constructor() { _disableInitializers(); @@ -29,15 +28,29 @@ contract VrfManager is OwnableUpgradeable, PodManager, VrfManagerStorage { function fillRandWordsWithSignature( IVrfPod vrfPod, VrfRandomWords calldata vrfRandomWords, - IBLSApkRegistry.OracleNonSignerAndSignature memory oracleNonSignerAndSignature - ) external onlyAggregatorManager onlyPodWhitelistedForFill(address(vrfPod)) { - ( - uint256 totalStaking, - bytes32 signatoryRecordHash - ) = blsApkRegistry.checkSignatures(vrfRandomWords.msgHash, vrfRandomWords.blockNumber, oracleNonSignerAndSignature); - - vrfPod.fulfillRandomWords(vrfRandomWords.requestId, vrfRandomWords._randomWords); - - emit VerifyVrfSig(vrfRandomWords.requestId, totalStaking, signatoryRecordHash, vrfRandomWords._randomWords); + IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature + ) + external + onlyAggregatorManager + onlyPodWhitelistedForFill(address(vrfPod)) + { + (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry + .checkSignatures( + vrfRandomWords.msgHash, + vrfRandomWords.blockNumber, + oracleNonSignerAndSignature + ); + + vrfPod.fulfillRandomWords( + vrfRandomWords.requestId, + vrfRandomWords._randomWords + ); + + emit VerifyVrfSig( + vrfRandomWords.requestId, + totalStaking, + signatoryRecordHash, + vrfRandomWords._randomWords + ); } } diff --git a/src/interfaces/IBLSApkRegistry.sol b/src/interfaces/IBLSApkRegistry.sol index 437e84c..e536e8d 100644 --- a/src/interfaces/IBLSApkRegistry.sol +++ b/src/interfaces/IBLSApkRegistry.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.20; import "../libraries/BN254.sol"; interface IBLSApkRegistry { - struct OracleNonSignerAndSignature { + struct NonSignerAndSignature { BN254.G1Point[] nonSignerPubkeys; BN254.G2Point apkG2; BN254.G1Point sigma; @@ -23,7 +23,11 @@ interface IBLSApkRegistry { BN254.G2Point pubkeyG2; } - event NewPubkeyRegistration(address indexed operator, BN254.G1Point pubkeyG1, BN254.G2Point pubkeyG2); + event NewPubkeyRegistration( + address indexed operator, + BN254.G1Point pubkeyG1, + BN254.G2Point pubkeyG2 + ); event OperatorAdded(address operator, bytes32 operatorId); @@ -42,12 +46,19 @@ interface IBLSApkRegistry { function checkSignatures( bytes32 msgHash, uint256 referenceBlockNumber, - OracleNonSignerAndSignature memory params + NonSignerAndSignature memory params ) external view returns (uint256, bytes32); - function getRegisteredPubkey(address operator) external view returns (BN254.G1Point memory, bytes32); + function getRegisteredPubkey( + address operator + ) external view returns (BN254.G1Point memory, bytes32); - function addOrRemoveBlsRegisterWhitelist(address operator, bool isAdd) external; + function addOrRemoveBlsRegisterWhitelist( + address operator, + bool isAdd + ) external; - function getPubkeyRegMessageHash(address operator) external view returns (BN254.G1Point memory); + function getPubkeyRegMessageHash( + address operator + ) external view returns (BN254.G1Point memory); } diff --git a/src/interfaces/IOracleManager.sol b/src/interfaces/IOracleManager.sol index f330976..db548e0 100644 --- a/src/interfaces/IOracleManager.sol +++ b/src/interfaces/IOracleManager.sol @@ -23,6 +23,6 @@ interface IOracleManager { function fillSymbolPriceWithSignature( IOraclePod oraclePod, OracleBatch calldata oracleBatch, - IBLSApkRegistry.OracleNonSignerAndSignature memory oracleNonSignerAndSignature + IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature ) external; } diff --git a/src/interfaces/IVrfManager.sol b/src/interfaces/IVrfManager.sol index d1324a4..78bc0a8 100644 --- a/src/interfaces/IVrfManager.sol +++ b/src/interfaces/IVrfManager.sol @@ -13,7 +13,7 @@ interface IVrfManager { uint256[] _randomWords ); - struct VrfRandomWords{ + struct VrfRandomWords { uint256 requestId; uint256[] _randomWords; bytes32 blockHash; @@ -24,6 +24,6 @@ interface IVrfManager { function fillRandWordsWithSignature( IVrfPod vrfPod, VrfRandomWords calldata vrfRandomWords, - IBLSApkRegistry.OracleNonSignerAndSignature memory oracleNonSignerAndSignature + IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature ) external; } diff --git a/src/pod/EventPod.sol b/src/pod/EventPod.sol index 4a3f52a..35ec77c 100644 --- a/src/pod/EventPod.sol +++ b/src/pod/EventPod.sol @@ -1,11 +1,10 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.20; - import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; -import { EventPodStorage } from "./EventPodStorage.sol"; +import {EventPodStorage} from "./EventPodStorage.sol"; contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { constructor() { @@ -13,20 +12,29 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { } modifier onlyEventManager() { - require ( - msg.sender == eventManager, "EventPod.onlyEventManager: caller is not the oracle manager address" + require( + msg.sender == eventManager, + "EventPod.onlyEventManager: caller is not the event manager address" ); _; } - function initialize(address _initialOwner, address _eventManager) external initializer { + function initialize( + address _initialOwner, + address _eventManager + ) external initializer { __Ownable_init(_initialOwner); eventManager = _eventManager; } - function createEvent(uint256 _requestId, string memory _eventDescribe, string memory _predictPosSide, string memory _predictNegSide) external { + function createEvent( + uint256 _requestId, + string memory _eventDescribe, + string memory _predictPosSide, + string memory _predictNegSide + ) external { predictEventMapping[_requestId] = PredictEventInfo({ - requestId: _requestId, + requestId: _requestId, eventDescribe: _eventDescribe, predictPosSide: _predictPosSide, predictNegSide: _predictNegSide, @@ -41,7 +49,10 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { ); } - function submitEventResult(uint256 _requestId, string memory _winner) external onlyEventManager { + function submitEventResult( + uint256 _requestId, + string memory _winner + ) external onlyEventManager { predictEventMapping[_requestId].winner = _winner; emit PredictEventResult( _requestId, @@ -51,7 +62,17 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { ); } - function fetchEventResult(uint256 requestId) external view returns (string memory predictPosSide, string memory predictNegSid, string memory winner) { + function fetchEventResult( + uint256 requestId + ) + external + view + returns ( + string memory predictPosSide, + string memory predictNegSid, + string memory winner + ) + { return ( predictEventMapping[requestId].predictPosSide, predictEventMapping[requestId].predictNegSide, @@ -59,7 +80,7 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { ); } - function setEventManager(address _eventManager) external { + function setEventManager(address _eventManager) external onlyOwner { eventManager = _eventManager; } } diff --git a/test/TestEvent.t.sol b/test/TestEvent.t.sol new file mode 100644 index 0000000..15ae483 --- /dev/null +++ b/test/TestEvent.t.sol @@ -0,0 +1,394 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import "forge-std/Test.sol"; +import "../src/pod/EventPod.sol"; +import {EventPodStorage} from "../src/pod/EventPodStorage.sol"; +import "../src/bls/BLSApkRegistry.sol"; +import "../src/core/EventManager.sol"; +import "../src/interfaces/IBLSApkRegistry.sol"; +import "../src/interfaces/IEventManager.sol"; +import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; + +contract EventPodTest is Test { + EventPod logic; + EventPod pod; + + address deployer = address(0xA1); + address eventManager = address(0xB1); + address other = address(0xC1); + uint256 requestId = 888; + string winner = "pos"; + + EventPodStorage.PredictEventInfo predictEventInfo = + EventPodStorage.PredictEventInfo({ + requestId: 888, + eventDescribe: "Team A vs Team B", + predictPosSide: "Team A wins", + predictNegSide: "Team B wins", + winner: "unknown" + }); + + function setUp() public { + vm.prank(deployer); + logic = new EventPod(); + + bytes memory initData = abi.encodeWithSelector( + EventPod.initialize.selector, + deployer, + eventManager + ); + + vm.prank(deployer); + ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); + pod = EventPod(address(proxy)); + } + + function testInitializeSetsOwnerAndEventManager() public view { + assertEq(pod.owner(), deployer); + assertEq(pod.eventManager(), eventManager); + } + + function testOnlyEventManagerCanSubmitEventResult() public { + // 非 EventManager 调用失败 + vm.prank(other); + vm.expectRevert( + "EventPod.onlyEventManager: caller is not the event manager address" + ); + pod.submitEventResult(requestId, winner); + + // EventManager 调用成功 + vm.prank(eventManager); + pod.submitEventResult(requestId, winner); + + ( + string memory posSide, + string memory negSide, + string memory winner1 + ) = pod.fetchEventResult(requestId); + + // assertEq(posSide, true); + assertEq(winner1, winner); + } + + function testOnlyOwnerCanSetEventManager() public { + vm.prank(eventManager); + vm.expectRevert( + abi.encodeWithSelector( + Ownable.OwnableUnauthorizedAccount.selector, + eventManager + ) + ); + pod.setEventManager(address(0xD1)); + + vm.prank(deployer); + pod.setEventManager(address(0xD1)); + assertEq(pod.eventManager(), address(0xD1)); + } + + function testCreateEvent() public { + pod.createEvent( + requestId, + "Team A vs Team B", + "Team A wins", + "Team B wins" + ); + + ( + uint256 reqId, + string memory eventDescribe, + string memory posSide, + string memory negSide, + string memory winner1 + ) = pod.predictEventMapping(requestId); + + ( + string memory posSide1, + string memory negSide1, + string memory winner11 + ) = pod.fetchEventResult(requestId); + + assertEq(posSide1, posSide); + assertEq(negSide1, negSide); + assertEq(winner11, winner1); + assertEq(reqId, predictEventInfo.requestId); + assertEq(eventDescribe, predictEventInfo.eventDescribe); + assertEq(posSide, predictEventInfo.predictPosSide); + assertEq(negSide, predictEventInfo.predictNegSide); + assertEq(winner1, predictEventInfo.winner); + } +} + +contract EventManagerTest is Test { + EventManager eventManager; + BLSApkRegistry blsRegistry; + EventPod eventPod; + + address owner = address(0xA1); + address aggregator = address(0xA2); + address operator = address(0xA3); + // address blsRegister = address(0xA4); + address whiteListManager = address(0xA5); + + function setUp() public { + // 部署逻辑合约 + EventManager os_logic = new EventManager(); + BLSApkRegistry bls_logic = new BLSApkRegistry(); + EventPod op_logic = new EventPod(); + + // 部署代理合约 + ERC1967Proxy os_proxy = new ERC1967Proxy(address(os_logic), ""); + ERC1967Proxy op_proxy = new ERC1967Proxy(address(op_logic), ""); + ERC1967Proxy bls_proxy = new ERC1967Proxy(address(bls_logic), ""); + + // 转换代理合约的接口 + eventManager = EventManager(address(os_proxy)); + blsRegistry = BLSApkRegistry(address(bls_proxy)); + eventPod = EventPod(address(op_proxy)); + + // 初始化合约 + vm.prank(owner); + eventManager.initialize(owner, address(blsRegistry), aggregator); + vm.prank(owner); + blsRegistry.initialize(owner, whiteListManager, address(eventManager)); + vm.prank(owner); + eventPod.initialize(owner, address(eventManager)); + + // 添加 operator 到白名单 + vm.prank(whiteListManager); + blsRegistry.addOrRemoveBlsRegisterWhitelist(operator, true); + + // 构造一组bls公钥和签名 + BN254.G1Point memory msgHash = BN254.G1Point({ + X: 18521112453352730579645358173921106118252889045846003563531873900220182176793, + Y: 12220611982697050695278792018747974293998452760543899595396661668417277566823 + }); + + BN254.G1Point memory signature = BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }); + + BN254.G2Point memory pubKeyG2 = BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }); + + BN254.G1Point memory pubKeyG1 = BN254.G1Point({ + X: 21552948824382449035487501529869156133453687741764572533699451941285719913479, + Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 + }); + + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry + .PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); + + // operator注册新的 pubkey + vm.prank(operator); + bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( + operator, + params, + msgHash + ); + + vm.prank(address(eventManager)); + blsRegistry.registerOperator(address(operator)); + } + + function test_addOrRemoveOperatorWhitelist() public { + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + eventManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(aggregator); + eventManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(aggregator); + vm.expectRevert( + "PodManager.addOperatorWhitelist: operator address is zero" + ); + eventManager.addOrRemoveOperatorWhitelist(address(0), true); + } + + function test_setAggregatorAddress() public { + vm.prank(address(0xE5)); + vm.expectRevert(); + eventManager.setAggregatorAddress(aggregator); + + vm.prank(owner); + eventManager.setAggregatorAddress(aggregator); + + vm.prank(owner); + vm.expectRevert( + "PodManager.addAggregator: aggregatorAddress address is zero" + ); + eventManager.setAggregatorAddress(address(0)); + } + + function test_addOrRemoveEventPodToFillWhitelist() public { + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + eventManager.addPodToFillWhitelist(address(eventPod)); + + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + eventManager.removePodToFillWhitelist(address(eventPod)); + + vm.prank(aggregator); + eventManager.addPodToFillWhitelist(address(eventPod)); + vm.prank(aggregator); + eventManager.removePodToFillWhitelist(address(eventPod)); + } + + function test_RegisterandDegisterOperator() public { + vm.prank(aggregator); + eventManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + eventManager.registerOperator("http://node.url"); + + vm.prank(operator); + vm.expectRevert( + "BLSApkRegistry.registerBLSPublicKey: Operator have already register" + ); + eventManager.registerOperator("http://node.url"); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + eventManager.deRegisterOperator(); + + vm.prank(operator); + eventManager.deRegisterOperator(); + + vm.prank(aggregator); + eventManager.addOrRemoveOperatorWhitelist(operator, false); + + vm.prank(operator); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + eventManager.registerOperator("http://node.url"); + } + + function testFillEventResultWithSignature() public { + vm.prank(aggregator); + eventManager.addPodToFillWhitelist(address(eventPod)); + + IBLSApkRegistry.NonSignerAndSignature + memory noSignerAndSignature = IBLSApkRegistry + .NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }), + sigma: BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }), + totalStake: 888 + }); + + IEventManager.PredictEvents memory predictEvents = IEventManager + .PredictEvents({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + winner: "pos" + }); + + vm.prank(aggregator); + eventManager.fillEventResultWithSignature( + eventPod, + predictEvents, + noSignerAndSignature + ); + ( + string memory posSide1, + string memory negSide1, + string memory winner11 + ) = eventPod.fetchEventResult(888); + + assertEq(winner11, "pos"); + } + + function testFillSymbolPriceWithoutWhitelistOrAuthority() public { + IBLSApkRegistry.NonSignerAndSignature + memory noSignerAndSignature = IBLSApkRegistry + .NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 19552866287184064427995511006223057169680536518603642638640105365054342788017, + 19912786774583403697047133238687463296134677575618298225286334615015816916116 + ], + Y: [ + 2970994197396269892653525920024039859830728356246595152296683945713431676344, + 18119535013136907197909765078809655896321461883746857179927989514870514777799 + ] + }), + sigma: BN254.G1Point({ + X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, + Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 + }), + totalStake: 888 + }); + + IEventManager.PredictEvents memory predictEvents = IEventManager + .PredictEvents({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + winner: "pos" + }); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + eventManager.fillEventResultWithSignature( + eventPod, + predictEvents, + noSignerAndSignature + ); + + vm.prank(aggregator); + vm.expectRevert( + "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" + ); + eventManager.fillEventResultWithSignature( + eventPod, + predictEvents, + noSignerAndSignature + ); + } +} diff --git a/test/TestOracle.t.sol b/test/TestOracle.t.sol index de80108..59d30f1 100644 --- a/test/TestOracle.t.sol +++ b/test/TestOracle.t.sol @@ -10,316 +10,356 @@ import "../src/interfaces/IOracleManager.sol"; import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; contract OraclePodTest is Test { -// OraclePod logic; -// OraclePod pod; -// -// address deployer = address(0xA1); -// address oracleManager = address(0xB1); -// address other = address(0xC1); -// -// function setUp() public { -// vm.prank(deployer); -// logic = new OraclePod(); -// -// bytes memory initData = abi.encodeWithSelector( -// OraclePod.initialize.selector, -// deployer, -// oracleManager -// ); -// -// vm.prank(deployer); -// ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); -// pod = OraclePod(address(proxy)); -// } -// -// function testInitializeSetsOwnerAndOracleManager() public view { -// assertEq(pod.owner(), deployer); -// assertEq(pod.oracleManager(), oracleManager); -// } -// -// function testOnlyOracleManagerCanFillSymbolPrice() public { -// string memory price = "1000"; -// -// // 非 oracleManager 调用失败 -// vm.prank(other); -// vm.expectRevert( -// "OraclePod.onlyOracleManager: caller is not the oracle manager address" -// ); -// pod.fillSymbolPrice(price); -// -// // oracleManager 调用成功 -// vm.prank(oracleManager); -// pod.fillSymbolPrice(price); -// -// assertEq(pod.getSymbolPrice(), price); -// } -// -// function testFillAndGetPrice() public { -// string memory price = "1234"; -// -// vm.prank(oracleManager); -// pod.fillSymbolPrice(price); -// -// string memory got = pod.getSymbolPrice(); -// assertEq(got, price); -// } -//} -// -//contract OracleManagerTest is Test { -// OracleManager oracleManager; -// BLSApkRegistry blsRegistry; -// OraclePod oraclePod; -// -// address owner = address(0xA1); -// address aggregator = address(0xA2); -// address operator = address(0xA3); -// // address blsRegister = address(0xA4); -// address whiteListManager = address(0xA5); -// -// function setUp() public { -// // 部署逻辑合约 -// OracleManager os_logic = new OracleManager(); -// BLSApkRegistry bls_logic = new BLSApkRegistry(); -// OraclePod op_logic = new OraclePod(); -// -// // 部署代理合约 -// ERC1967Proxy os_proxy = new ERC1967Proxy(address(os_logic), ""); -// ERC1967Proxy op_proxy = new ERC1967Proxy(address(op_logic), ""); -// ERC1967Proxy bls_proxy = new ERC1967Proxy(address(bls_logic), ""); -// -// // 转换代理合约的接口 -// oracleManager = OracleManager(address(os_proxy)); -// blsRegistry = BLSApkRegistry(address(bls_proxy)); -// oraclePod = OraclePod(address(op_proxy)); -// -// // 初始化合约 -// vm.prank(owner); -// oracleManager.initialize(owner, address(blsRegistry), aggregator); -// vm.prank(owner); -// blsRegistry.initialize(owner, whiteListManager, address(oracleManager)); -// vm.prank(owner); -// oraclePod.initialize(owner, address(oracleManager)); -// -// // 添加 operator 到白名单 -// vm.prank(whiteListManager); -// blsRegistry.addOrRemoveBlsRegisterWhitelist(operator, true); -// -// // 构造一组bls公钥和签名 -// BN254.G1Point memory msgHash = BN254.G1Point({ -// X: 18521112453352730579645358173921106118252889045846003563531873900220182176793, -// Y: 12220611982697050695278792018747974293998452760543899595396661668417277566823 -// }); -// -// BN254.G1Point memory signature = BN254.G1Point({ -// X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, -// Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 -// }); -// -// BN254.G2Point memory pubKeyG2 = BN254.G2Point({ -// X: [ -// 6814450613988925037276906495559354220267038225890288520888556922179861427221, -// 11097154366204527428819849175191533397314611771099148982308553889852330000313 -// ], -// Y: [ -// 20799884507081215979545766399242808376431798816319714422985505673585902041706, -// 13670248609089265475970799020243713070902269374832615406626549692922451548915 -// ] -// }); -// -// BN254.G1Point memory pubKeyG1 = BN254.G1Point({ -// X: 21552948824382449035487501529869156133453687741764572533699451941285719913479, -// Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 -// }); -// -// IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry -// .PubkeyRegistrationParams({ -// pubkeyG1: pubKeyG1, -// pubkeyG2: pubKeyG2, -// pubkeyRegistrationSignature: signature -// }); -// -// // operator注册新的 pubkey -// vm.prank(operator); -// bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( -// operator, -// params, -// msgHash -// ); -// -// vm.prank(address(oracleManager)); -// blsRegistry.registerOperator(address(operator)); -// } -// -// function test_addOrRemoveOperatorWhitelist() public { -// vm.prank(address(0xE5)); -// vm.expectRevert( -// "OracleManager.onlyOracleWhiteListManager: not the aggregator address" -// ); -// oracleManager.addOrRemoveOperatorWhitelist(operator, true); -// -// vm.prank(aggregator); -// oracleManager.addOrRemoveOperatorWhitelist(operator, true); -// -// vm.prank(aggregator); -// vm.expectRevert( -// "OracleManager.addOperatorWhitelist: operator address is zero" -// ); -// oracleManager.addOrRemoveOperatorWhitelist(address(0), true); -// } -// -// function test_setAggregatorAddress() public { -// vm.prank(address(0xE5)); -// vm.expectRevert(); -// oracleManager.setAggregatorAddress(aggregator); -// -// vm.prank(owner); -// oracleManager.setAggregatorAddress(aggregator); -// -// vm.prank(owner); -// vm.expectRevert( -// "OracleManager.addAggregator: aggregatorAddress address is zero" -// ); -// oracleManager.setAggregatorAddress(address(0)); -// } -// -// function test_addOrRemoveOraclePodToFillWhitelist() public { -// vm.prank(address(0xE5)); -// vm.expectRevert( -// "OracleManager.onlyOracleWhiteListManager: not the aggregator address" -// ); -// oracleManager.addOraclePodToFillWhitelist(oraclePod); -// -// vm.prank(address(0xE5)); -// vm.expectRevert( -// "OracleManager.onlyOracleWhiteListManager: not the aggregator address" -// ); -// oracleManager.removeOraclePodToFillWhitelist(oraclePod); -// -// vm.prank(aggregator); -// oracleManager.addOraclePodToFillWhitelist(oraclePod); -// vm.prank(aggregator); -// oracleManager.removeOraclePodToFillWhitelist(oraclePod); -// } -// -// function test_RegisterandDegisterOperator() public { -// vm.prank(aggregator); -// oracleManager.addOrRemoveOperatorWhitelist(operator, true); -// -// vm.prank(address(0xE1)); -// vm.expectRevert( -// "OracleManager.registerOperator: this address have not permission to register " -// ); -// oracleManager.registerOperator("http://node.url"); -// -// vm.prank(operator); -// oracleManager.registerOperator("http://node.url"); -// -// vm.prank(address(0xE1)); -// vm.expectRevert( -// "OracleManager.registerOperator: this address have not permission to register " -// ); -// oracleManager.deRegisterOperator(); -// -// vm.prank(operator); -// oracleManager.deRegisterOperator(); -// -// vm.prank(aggregator); -// oracleManager.addOrRemoveOperatorWhitelist(operator, false); -// -// vm.prank(operator); -// vm.expectRevert( -// "OracleManager.registerOperator: this address have not permission to register " -// ); -// oracleManager.registerOperator("http://node.url"); -// } -// -// function testFillSymbolPriceWithSignature() public { -// vm.prank(aggregator); -// oracleManager.addOraclePodToFillWhitelist(oraclePod); -// -// IBLSApkRegistry.OracleNonSignerAndSignature -// memory noSignerAndSignature = IBLSApkRegistry -// .OracleNonSignerAndSignature({ -// nonSignerPubkeys: new BN254.G1Point[](0), -// apkG2: BN254.G2Point({ -// X: [ -// 6814450613988925037276906495559354220267038225890288520888556922179861427221, -// 11097154366204527428819849175191533397314611771099148982308553889852330000313 -// ], -// Y: [ -// 20799884507081215979545766399242808376431798816319714422985505673585902041706, -// 13670248609089265475970799020243713070902269374832615406626549692922451548915 -// ] -// }), -// sigma: BN254.G1Point({ -// X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, -// Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 -// }), -// totalStake: 888 -// }); -// IOracleManager.OracleBatch memory batch = IOracleManager.OracleBatch({ -// msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, -// blockNumber: block.number - 1, -// symbolPrice: "888", -// blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935 -// }); -// -// vm.prank(aggregator); -// oracleManager.fillSymbolPriceWithSignature( -// oraclePod, -// batch, -// noSignerAndSignature -// ); -// -// assertEq(oraclePod.getSymbolPrice(), "888"); -// } -// -// function testFillSymbolPriceWithoutWhitelistOrAuthority() public { -// IBLSApkRegistry.OracleNonSignerAndSignature -// memory noSignerAndSignature = IBLSApkRegistry -// .OracleNonSignerAndSignature({ -// nonSignerPubkeys: new BN254.G1Point[](0), -// apkG2: BN254.G2Point({ -// X: [ -// 19552866287184064427995511006223057169680536518603642638640105365054342788017, -// 19912786774583403697047133238687463296134677575618298225286334615015816916116 -// ], -// Y: [ -// 2970994197396269892653525920024039859830728356246595152296683945713431676344, -// 18119535013136907197909765078809655896321461883746857179927989514870514777799 -// ] -// }), -// sigma: BN254.G1Point({ -// X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, -// Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 -// }), -// totalStake: 888 -// }); -// IOracleManager.OracleBatch memory batch = IOracleManager.OracleBatch({ -// msgHash: 0x3f0a377ba0a4a460ecb616f6507ce0d8cfa3e704025d4fda3ed0c5ca05468728, -// blockNumber: block.number - 1, -// symbolPrice: "888", -// blockHash: 0x3f0a377ba0a4a460ecb616f6507ce0d8cfa3e704025d4fda3ed0c5ca05468728 -// }); -// -// vm.prank(address(0xE1)); -// vm.expectRevert( -// "OracleManager.onlyOracleWhiteListManager: not the aggregator address" -// ); -// oracleManager.fillSymbolPriceWithSignature( -// oraclePod, -// batch, -// noSignerAndSignature -// ); -// -// vm.prank(aggregator); -// vm.expectRevert( -// "OracleManager.onlyPodWhitelistedForFill: oraclePod not whitelisted" -// ); -// oracleManager.fillSymbolPriceWithSignature( -// oraclePod, -// batch, -// noSignerAndSignature -// ); -// } + OraclePod logic; + OraclePod pod; + + address deployer = address(0xA1); + address oracleManager = address(0xB1); + address other = address(0xC1); + + function setUp() public { + vm.prank(deployer); + logic = new OraclePod(); + + bytes memory initData = abi.encodeWithSelector( + OraclePod.initialize.selector, + deployer, + oracleManager + ); + + vm.prank(deployer); + ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); + pod = OraclePod(address(proxy)); + } + + function testInitializeSetsOwnerAndOracleManager() public view { + assertEq(pod.owner(), deployer); + assertEq(pod.oracleManager(), oracleManager); + } + + function testOnlyOracleManagerCanFillSymbolPrice() public { + string memory price = "1000"; + + // 非 oracleManager 调用失败 + vm.prank(other); + vm.expectRevert( + "OraclePod.onlyOracleManager: caller is not the oracle manager address" + ); + pod.fillSymbolPrice(price); + + // oracleManager 调用成功 + vm.prank(oracleManager); + pod.fillSymbolPrice(price); + + assertEq(pod.getSymbolPrice(), price); + } + + function testFillAndGetPrice() public { + string memory price = "1234"; + + vm.prank(address(0xE5)); + vm.expectRevert( + "OraclePod.onlyOracleManager: caller is not the oracle manager address" + ); + pod.fillSymbolPrice(price); + + vm.prank(oracleManager); + pod.fillSymbolPrice(price); + + string memory got = pod.getSymbolPrice(); + assertEq(got, price); + } + + function testIsDataFresh() public { + string memory price = "5678"; + + vm.prank(oracleManager); + pod.fillSymbolPrice(price); + + // 立即检查,数据应为新鲜 + bool fresh = pod.isDataFresh(10); + assertTrue(fresh); + + // 增加时间超过阈值,数据应不新鲜 + vm.warp(block.timestamp + 20); + fresh = pod.isDataFresh(10); + assertTrue(!fresh); + } + + function testOnlyOwnerCanSetOracleManager() public { + address newManager = address(0xD1); + + // 非 owner 调用失败 + vm.prank(other); + vm.expectRevert(); + pod.setOracleManager(newManager); + + // owner 调用成功 + vm.prank(deployer); + pod.setOracleManager(newManager); + + assertEq(pod.oracleManager(), newManager); + } +} + +contract OracleManagerTest is Test { + OracleManager oracleManager; + BLSApkRegistry blsRegistry; + OraclePod oraclePod; + + address owner = address(0xA1); + address aggregator = address(0xA2); + address operator = address(0xA3); + // address blsRegister = address(0xA4); + address whiteListManager = address(0xA5); + + function setUp() public { + // 部署逻辑合约 + OracleManager os_logic = new OracleManager(); + BLSApkRegistry bls_logic = new BLSApkRegistry(); + OraclePod op_logic = new OraclePod(); + + // 部署代理合约 + ERC1967Proxy os_proxy = new ERC1967Proxy(address(os_logic), ""); + ERC1967Proxy op_proxy = new ERC1967Proxy(address(op_logic), ""); + ERC1967Proxy bls_proxy = new ERC1967Proxy(address(bls_logic), ""); + + // 转换代理合约的接口 + oracleManager = OracleManager(address(os_proxy)); + blsRegistry = BLSApkRegistry(address(bls_proxy)); + oraclePod = OraclePod(address(op_proxy)); + + // 初始化合约 + vm.prank(owner); + oracleManager.initialize(owner, address(blsRegistry), aggregator); + vm.prank(owner); + blsRegistry.initialize(owner, whiteListManager, address(oracleManager)); + vm.prank(owner); + oraclePod.initialize(owner, address(oracleManager)); + + // 添加 operator 到白名单 + vm.prank(whiteListManager); + blsRegistry.addOrRemoveBlsRegisterWhitelist(operator, true); + + // 构造一组bls公钥和签名 + BN254.G1Point memory msgHash = BN254.G1Point({ + X: 18521112453352730579645358173921106118252889045846003563531873900220182176793, + Y: 12220611982697050695278792018747974293998452760543899595396661668417277566823 + }); + + BN254.G1Point memory signature = BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }); + + BN254.G2Point memory pubKeyG2 = BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }); + + BN254.G1Point memory pubKeyG1 = BN254.G1Point({ + X: 21552948824382449035487501529869156133453687741764572533699451941285719913479, + Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 + }); + + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry + .PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); + + // operator注册新的 pubkey + vm.prank(operator); + bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( + operator, + params, + msgHash + ); + + vm.prank(address(oracleManager)); + blsRegistry.registerOperator(address(operator)); + } + + function test_addOrRemoveOperatorWhitelist() public { + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + oracleManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(aggregator); + oracleManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(aggregator); + vm.expectRevert( + "PodManager.addOperatorWhitelist: operator address is zero" + ); + oracleManager.addOrRemoveOperatorWhitelist(address(0), true); + } + + function test_setAggregatorAddress() public { + vm.prank(address(0xE5)); + vm.expectRevert(); + oracleManager.setAggregatorAddress(aggregator); + + vm.prank(owner); + oracleManager.setAggregatorAddress(aggregator); + + vm.prank(owner); + vm.expectRevert( + "PodManager.addAggregator: aggregatorAddress address is zero" + ); + oracleManager.setAggregatorAddress(address(0)); + } + + function test_addOrRemoveOraclePodToFillWhitelist() public { + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + oracleManager.addPodToFillWhitelist(address(oraclePod)); + + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + oracleManager.removePodToFillWhitelist(address(oraclePod)); + + vm.prank(aggregator); + oracleManager.addPodToFillWhitelist(address(oraclePod)); + vm.prank(aggregator); + oracleManager.removePodToFillWhitelist(address(oraclePod)); + } + + function test_RegisterandDegisterOperator() public { + vm.prank(aggregator); + oracleManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + oracleManager.registerOperator("http://node.url"); + + vm.prank(operator); + vm.expectRevert( + "BLSApkRegistry.registerBLSPublicKey: Operator have already register" + ); + oracleManager.registerOperator("http://node.url"); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + oracleManager.deRegisterOperator(); + + vm.prank(operator); + oracleManager.deRegisterOperator(); + + vm.prank(aggregator); + oracleManager.addOrRemoveOperatorWhitelist(operator, false); + + vm.prank(operator); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + oracleManager.registerOperator("http://node.url"); + } + + function testFillSymbolPriceWithSignature() public { + vm.prank(aggregator); + oracleManager.addPodToFillWhitelist(address(oraclePod)); + + IBLSApkRegistry.NonSignerAndSignature + memory noSignerAndSignature = IBLSApkRegistry + .NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }), + sigma: BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }), + totalStake: 888 + }); + IOracleManager.OracleBatch memory batch = IOracleManager.OracleBatch({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + symbolPrice: "888", + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935 + }); + + vm.prank(aggregator); + oracleManager.fillSymbolPriceWithSignature( + oraclePod, + batch, + noSignerAndSignature + ); + + assertEq(oraclePod.getSymbolPrice(), "888"); + } + + function testFillSymbolPriceWithoutWhitelistOrAuthority() public { + IBLSApkRegistry.NonSignerAndSignature + memory noSignerAndSignature = IBLSApkRegistry + .NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 19552866287184064427995511006223057169680536518603642638640105365054342788017, + 19912786774583403697047133238687463296134677575618298225286334615015816916116 + ], + Y: [ + 2970994197396269892653525920024039859830728356246595152296683945713431676344, + 18119535013136907197909765078809655896321461883746857179927989514870514777799 + ] + }), + sigma: BN254.G1Point({ + X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, + Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 + }), + totalStake: 888 + }); + IOracleManager.OracleBatch memory batch = IOracleManager.OracleBatch({ + msgHash: 0x3f0a377ba0a4a460ecb616f6507ce0d8cfa3e704025d4fda3ed0c5ca05468728, + blockNumber: block.number - 1, + symbolPrice: "888", + blockHash: 0x3f0a377ba0a4a460ecb616f6507ce0d8cfa3e704025d4fda3ed0c5ca05468728 + }); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + oracleManager.fillSymbolPriceWithSignature( + oraclePod, + batch, + noSignerAndSignature + ); + + vm.prank(aggregator); + vm.expectRevert( + "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" + ); + oracleManager.fillSymbolPriceWithSignature( + oraclePod, + batch, + noSignerAndSignature + ); + } } diff --git a/test/TestVrf.t.sol b/test/TestVrf.t.sol new file mode 100644 index 0000000..c029561 --- /dev/null +++ b/test/TestVrf.t.sol @@ -0,0 +1,362 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import "forge-std/Test.sol"; +import "../src/pod/VrfPod.sol"; +import "../src/bls/BLSApkRegistry.sol"; +import "../src/core/VrfManager.sol"; +import "../src/interfaces/IBLSApkRegistry.sol"; +import "../src/interfaces/IVrfManager.sol"; +import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; + +contract VrfPodTest is Test { + VrfPod logic; + VrfPod pod; + + address deployer = address(0xA1); + address vrfManager = address(0xB1); + address other = address(0xC1); + + uint256 requestId = 1; + uint256[] randomWords = [uint256(123), uint256(456), uint256(789)]; + + function setUp() public { + vm.prank(deployer); + logic = new VrfPod(); + + bytes memory initData = abi.encodeWithSelector( + VrfPod.initialize.selector, + deployer, + vrfManager + ); + + vm.prank(deployer); + ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); + pod = VrfPod(address(proxy)); + } + + function testInitializeSetsOwnerAndVrfManager() public view { + assertEq(pod.owner(), deployer); + assertEq(pod.vrfManager(), vrfManager); + } + + function testOnlyVrfManagerCanFulfillRandomWords() public { + // 非 VrfManager 调用失败 + vm.prank(other); + vm.expectRevert("DappLinkVRF.onlyVrfManager can call this function"); + pod.fulfillRandomWords(requestId, randomWords); + + // VrfManager 调用成功 + vm.prank(vrfManager); + pod.fulfillRandomWords(requestId, randomWords); + + (bool fulfilled, uint256[] memory words) = pod.getRandomWordsWithStatus( + requestId + ); + + assertEq(fulfilled, true); + assertEq(words, randomWords); + } + + function testOnlyOwnerCanSetVrfManager() public { + vm.prank(vrfManager); + vm.expectRevert( + abi.encodeWithSelector( + Ownable.OwnableUnauthorizedAccount.selector, + vrfManager + ) + ); + pod.setVrfManager(address(0xD1)); + + vm.prank(deployer); + pod.setVrfManager(address(0xD1)); + assertEq(pod.vrfManager(), address(0xD1)); + } + + function testRequestRandomWords() public { + pod.requestRandomWords(requestId, 3); + + (bool fulfilled, uint256[] memory words) = pod.getRandomWordsWithStatus( + requestId + ); + + assertEq(fulfilled, false); + assertEq(words.length, 0); + } +} + +contract VrfManagerTest is Test { + VrfManager vrfManager; + BLSApkRegistry blsRegistry; + VrfPod vrfPod; + + address owner = address(0xA1); + address aggregator = address(0xA2); + address operator = address(0xA3); + // address blsRegister = address(0xA4); + address whiteListManager = address(0xA5); + + function setUp() public { + // 部署逻辑合约 + VrfManager os_logic = new VrfManager(); + BLSApkRegistry bls_logic = new BLSApkRegistry(); + VrfPod op_logic = new VrfPod(); + + // 部署代理合约 + ERC1967Proxy os_proxy = new ERC1967Proxy(address(os_logic), ""); + ERC1967Proxy op_proxy = new ERC1967Proxy(address(op_logic), ""); + ERC1967Proxy bls_proxy = new ERC1967Proxy(address(bls_logic), ""); + + // 转换代理合约的接口 + vrfManager = VrfManager(address(os_proxy)); + blsRegistry = BLSApkRegistry(address(bls_proxy)); + vrfPod = VrfPod(address(op_proxy)); + + // 初始化合约 + vm.prank(owner); + vrfManager.initialize(owner, address(blsRegistry), aggregator); + vm.prank(owner); + blsRegistry.initialize(owner, whiteListManager, address(vrfManager)); + vm.prank(owner); + vrfPod.initialize(owner, address(vrfManager)); + + // 添加 operator 到白名单 + vm.prank(whiteListManager); + blsRegistry.addOrRemoveBlsRegisterWhitelist(operator, true); + + // 构造一组bls公钥和签名 + BN254.G1Point memory msgHash = BN254.G1Point({ + X: 18521112453352730579645358173921106118252889045846003563531873900220182176793, + Y: 12220611982697050695278792018747974293998452760543899595396661668417277566823 + }); + + BN254.G1Point memory signature = BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }); + + BN254.G2Point memory pubKeyG2 = BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }); + + BN254.G1Point memory pubKeyG1 = BN254.G1Point({ + X: 21552948824382449035487501529869156133453687741764572533699451941285719913479, + Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 + }); + + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry + .PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); + + // operator注册新的 pubkey + vm.prank(operator); + bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( + operator, + params, + msgHash + ); + + vm.prank(address(vrfManager)); + blsRegistry.registerOperator(address(operator)); + } + + function test_addOrRemoveOperatorWhitelist() public { + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + vrfManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(aggregator); + vrfManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(aggregator); + vm.expectRevert( + "PodManager.addOperatorWhitelist: operator address is zero" + ); + vrfManager.addOrRemoveOperatorWhitelist(address(0), true); + } + + function test_setAggregatorAddress() public { + vm.prank(address(0xE5)); + vm.expectRevert(); + vrfManager.setAggregatorAddress(aggregator); + + vm.prank(owner); + vrfManager.setAggregatorAddress(aggregator); + + vm.prank(owner); + vm.expectRevert( + "PodManager.addAggregator: aggregatorAddress address is zero" + ); + vrfManager.setAggregatorAddress(address(0)); + } + + function test_addOrRemoveVrfPodToFillWhitelist() public { + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + vrfManager.addPodToFillWhitelist(address(vrfPod)); + + vm.prank(address(0xE5)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + vrfManager.removePodToFillWhitelist(address(vrfPod)); + + vm.prank(aggregator); + vrfManager.addPodToFillWhitelist(address(vrfPod)); + vm.prank(aggregator); + vrfManager.removePodToFillWhitelist(address(vrfPod)); + } + + function test_RegisterandDegisterOperator() public { + vm.prank(aggregator); + vrfManager.addOrRemoveOperatorWhitelist(operator, true); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + vrfManager.registerOperator("http://node.url"); + + vm.prank(operator); + vm.expectRevert( + "BLSApkRegistry.registerBLSPublicKey: Operator have already register" + ); + vrfManager.registerOperator("http://node.url"); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + vrfManager.deRegisterOperator(); + + vm.prank(operator); + vrfManager.deRegisterOperator(); + + vm.prank(aggregator); + vrfManager.addOrRemoveOperatorWhitelist(operator, false); + + vm.prank(operator); + vm.expectRevert( + "PodManager.registerOperator: this address have not permission to register " + ); + vrfManager.registerOperator("http://node.url"); + } + + function testFillRandWordsWithSignature() public { + vm.prank(aggregator); + vrfManager.addPodToFillWhitelist(address(vrfPod)); + + IBLSApkRegistry.NonSignerAndSignature + memory noSignerAndSignature = IBLSApkRegistry + .NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }), + sigma: BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }), + totalStake: 888 + }); + + uint256[] memory arr = new uint256[](1); + arr[0] = 43; + IVrfManager.VrfRandomWords memory randomWords = IVrfManager + .VrfRandomWords({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + _randomWords: arr + }); + + vm.prank(aggregator); + vrfManager.fillRandWordsWithSignature( + vrfPod, + randomWords, + noSignerAndSignature + ); + (bool fulfilled, uint256[] memory words) = vrfPod + .getRandomWordsWithStatus(888); + + assertEq(fulfilled, true); + assertEq(words, arr); + } + + function testFillSymbolPriceWithoutWhitelistOrAuthority() public { + IBLSApkRegistry.NonSignerAndSignature + memory noSignerAndSignature = IBLSApkRegistry + .NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 19552866287184064427995511006223057169680536518603642638640105365054342788017, + 19912786774583403697047133238687463296134677575618298225286334615015816916116 + ], + Y: [ + 2970994197396269892653525920024039859830728356246595152296683945713431676344, + 18119535013136907197909765078809655896321461883746857179927989514870514777799 + ] + }), + sigma: BN254.G1Point({ + X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, + Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 + }), + totalStake: 888 + }); + + uint256[] memory arr = new uint256[](1); + arr[0] = 43; + IVrfManager.VrfRandomWords memory randomWords = IVrfManager + .VrfRandomWords({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + _randomWords: arr + }); + + vm.prank(address(0xE1)); + vm.expectRevert( + "PodManager.onlyAggregatorManager: not the aggregator address" + ); + vrfManager.fillRandWordsWithSignature( + vrfPod, + randomWords, + noSignerAndSignature + ); + + vm.prank(aggregator); + vm.expectRevert( + "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" + ); + vrfManager.fillRandWordsWithSignature( + vrfPod, + randomWords, + noSignerAndSignature + ); + } +} diff --git a/test/Test_BLSApkRegistry.t.sol b/test/Test_BLSApkRegistry.t.sol index f6910fc..1ec86a9 100644 --- a/test/Test_BLSApkRegistry.t.sol +++ b/test/Test_BLSApkRegistry.t.sol @@ -81,12 +81,12 @@ contract BLSApkRegistryTest is Test { // 测试注册新的 pubkey 是否成功 vm.prank(blsRegister); bytes32 pubkeyHash = registry.registerBLSPublicKey( - address(0xBEE), + blsRegister, params, msgHash ); - bytes32 readHash = registry.getPubkeyHash(address(0xBEE)); + bytes32 readHash = registry.getPubkeyHash(blsRegister); assertEq(pubkeyHash, readHash); // 测试同一个 operator 不能重复注册 pubkey @@ -94,7 +94,7 @@ contract BLSApkRegistryTest is Test { vm.expectRevert( "BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey" ); - registry.registerBLSPublicKey(address(0xBEE), params, msgHash); + registry.registerBLSPublicKey(blsRegister, params, msgHash); } function testCannotRegisterWithZeroPubkey() public { @@ -117,7 +117,13 @@ contract BLSApkRegistryTest is Test { vm.expectRevert( "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey" ); - registry.registerBLSPublicKey(address(0xC0DE), params, dummyMsgHash); + registry.registerBLSPublicKey(blsRegister, params, dummyMsgHash); + + vm.prank(blsRegister); + vm.expectRevert( + "BLSApkRegistry.registerBLSPublicKey: this caller is not operator" + ); + registry.registerBLSPublicKey(address(0xEdc), params, dummyMsgHash); } function testOnlyOracleManagerCanRegisterAndDeregister() public { @@ -129,11 +135,11 @@ contract BLSApkRegistryTest is Test { vm.expectRevert( "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" ); - registry.registerOperator(address(0xBEE)); + registry.registerOperator(blsRegister); // 正常调用 vm.prank(oracleManager); - registry.registerOperator(address(0xBEE)); + registry.registerOperator(blsRegister); // 非 oracleManager 调用 deregisterOperator vm.prank(address(0x111)); @@ -144,7 +150,7 @@ contract BLSApkRegistryTest is Test { // 正常调用 vm.prank(oracleManager); - registry.deregisterOperator(address(0xBEE)); + registry.deregisterOperator(blsRegister); } function testGetPubkeyRegMessageHash() public view { @@ -178,9 +184,15 @@ contract BLSApkRegistryTest is Test { vm.prank(tester); vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key" + "BLSApkRegistry.registerBLSPublicKey: this caller is not operator" ); registry.registerBLSPublicKey(address(0xDDD), params, dummy); + + vm.prank(tester); + vm.expectRevert( + "BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key" + ); + registry.registerBLSPublicKey(tester, params, dummy); } function testTrySignatureAndApkVerification() public view { @@ -256,15 +268,26 @@ contract BLSApkRegistryTest is Test { pubkeyRegistrationSignature: signature }); + // 测试白名单限制 + vm.prank(address(0xA5)); + vm.expectRevert( + "BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key" + ); + bytes32 pubkeyHash1 = registry.registerBLSPublicKey( + address(0xA5), + params, + msgHash + ); + // 测试注册新的 pubkey 是否成功 vm.prank(blsRegister); bytes32 pubkeyHash = registry.registerBLSPublicKey( - address(0xBEE), + blsRegister, params, msgHash ); - bytes32 readHash = registry.getPubkeyHash(address(0xBEE)); + bytes32 readHash = registry.getPubkeyHash(blsRegister); assertEq(pubkeyHash, readHash); // 测试同一个 operator 不能重复注册 pubkey @@ -272,6 +295,6 @@ contract BLSApkRegistryTest is Test { vm.expectRevert( "BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey" ); - registry.registerBLSPublicKey(address(0xBEE), params, msgHash); + registry.registerBLSPublicKey(blsRegister, params, msgHash); } } From 30003c1484fbc8f977b5707a1b69526ef95463a8 Mon Sep 17 00:00:00 2001 From: kanthub <564080210@qq.com> Date: Fri, 10 Oct 2025 11:14:27 +0800 Subject: [PATCH 2/3] tests and deploys cpmplement --- script/Event/deployEvent.s.sol | 41 ++--- script/Event/deployEventPod.s.sol | 26 +-- script/Event/upgradeEventPod.s.sol | 13 +- script/Oracle/deployOracle.s.sol | 41 ++--- script/Oracle/deployOraclePod.s.sol | 26 +-- script/Oracle/upgradeOraclePod.s.sol | 13 +- script/Vrf/deployVrf.s.sol | 41 ++--- script/Vrf/deployVrfPod.s.sol | 17 +- script/Vrf/upgradeVrfPod.s.sol | 13 +- src/bls/BLSApkRegistry.sol | 176 +++++-------------- src/core/EventManager.sol | 36 ++-- src/core/EventManagerStorage.sol | 5 +- src/core/OracleManager.sol | 37 +--- src/core/OracleManagerStorage.sol | 1 - src/core/PodManager.sol | 44 ++--- src/core/VrfManager.sol | 36 ++-- src/core/VrfManagerStorage.sol | 5 +- src/interfaces/IBLSApkRegistry.sol | 28 +-- src/interfaces/IEventManager.sol | 9 +- src/interfaces/IEventPod.sol | 25 ++- src/interfaces/IOracleManager.sol | 7 +- src/interfaces/IOraclePod.sol | 11 +- src/interfaces/IVrfManager.sol | 7 +- src/interfaces/IVrfPod.sol | 16 +- src/libraries/BN254.sol | 76 ++++----- src/libraries/SafeCall.sol | 124 +++++++------- src/pod/EventPod.sol | 33 +--- src/pod/EventPodStorage.sol | 13 +- src/pod/OraclePod.sol | 7 +- src/pod/OraclePodStorage.sol | 4 +- src/pod/VrfPod.sol | 22 ++- src/pod/VrfPodStorage.sol | 3 +- test/TestEvent.t.sol | 246 ++++++++++----------------- test/TestOracle.t.sol | 169 +++++++----------- test/TestVrf.t.sol | 209 ++++++++--------------- test/Test_BLSApkRegistry.t.sol | 137 +++++---------- 36 files changed, 544 insertions(+), 1173 deletions(-) diff --git a/script/Event/deployEvent.s.sol b/script/Event/deployEvent.s.sol index 056b936..13cf887 100644 --- a/script/Event/deployEvent.s.sol +++ b/script/Event/deployEvent.s.sol @@ -31,38 +31,25 @@ contract deployEventScript is Script { // Deploy BLSApkRegistry proxy and delegate to a empty contract first emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyBlsApkRegistry = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); blsApkRegistry = BLSApkRegistry(address(proxyBlsApkRegistry)); blsApkRegistryImplementation = new BLSApkRegistry(); - blsApkRegistryProxyAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyBlsApkRegistry)) - ); + blsApkRegistryProxyAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyBlsApkRegistry))); // Deploy EventManager proxy and delegate to a empty contract first - TransparentUpgradeableProxy proxyEventManager = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyEventManager = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); eventManager = EventManager(address(proxyEventManager)); eventManagerImplementation = new EventManager(); - eventManagerAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyEventManager)) - ); + eventManagerAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyEventManager))); // Upgrade and initialize the implementations blsApkRegistryProxyAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(blsApkRegistry)), address(blsApkRegistryImplementation), abi.encodeWithSelector( - BLSApkRegistry.initialize.selector, - deployerAddress, - relayerManagerAddr, - address(proxyEventManager) + BLSApkRegistry.initialize.selector, deployerAddress, relayerManagerAddr, address(proxyEventManager) ) ); @@ -70,17 +57,11 @@ contract deployEventScript is Script { ITransparentUpgradeableProxy(address(eventManager)), address(eventManagerImplementation), abi.encodeWithSelector( - EventManager.initialize.selector, - deployerAddress, - proxyBlsApkRegistry, - deployerAddress + EventManager.initialize.selector, deployerAddress, proxyBlsApkRegistry, deployerAddress ) ); - console.log( - "deploy proxyBlsApkRegistry:", - address(proxyBlsApkRegistry) - ); + console.log("deploy proxyBlsApkRegistry:", address(proxyBlsApkRegistry)); console.log("deploy proxyEventManager:", address(proxyEventManager)); string memory path = "deployed_addresses.json"; string memory data = string( @@ -97,9 +78,7 @@ contract deployEventScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Event/deployEventPod.s.sol b/script/Event/deployEventPod.s.sol index f890131..561d759 100644 --- a/script/Event/deployEventPod.s.sol +++ b/script/Event/deployEventPod.s.sol @@ -29,30 +29,18 @@ contract deployEventPodScript is Script { vm.startBroadcast(deployerPrivateKey); emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyEventPod = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyEventPod = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); eventPod = EventPod(address(proxyEventPod)); eventPodImplementation = new EventPod(); - eventPodAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyEventPod)) - ); + eventPodAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyEventPod))); - console.log( - "eventPodImplementation===", - address(eventPodImplementation) - ); + console.log("eventPodImplementation===", address(eventPodImplementation)); eventPodAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(eventPod)), address(eventPodImplementation), - abi.encodeWithSelector( - EventPod.initialize.selector, - deployerAddress, - eventManagerAddr - ) + abi.encodeWithSelector(EventPod.initialize.selector, deployerAddress, eventManagerAddr) ); // EventManager(eventManagerAddr).addEventPodToFillWhitelist(proxyEventPod); @@ -73,9 +61,7 @@ contract deployEventPodScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Event/upgradeEventPod.s.sol b/script/Event/upgradeEventPod.s.sol index b8ed535..fb4f36a 100644 --- a/script/Event/upgradeEventPod.s.sol +++ b/script/Event/upgradeEventPod.s.sol @@ -30,24 +30,17 @@ contract upgradeEventPodScript is Script { EventPod newEventPodImplementation = new EventPod(); - console.log( - "New EventPod implementation:", - address(newEventPodImplementation) - ); + console.log("New EventPod implementation:", address(newEventPodImplementation)); messageManagerProxyAdmin.upgradeAndCall( - ITransparentUpgradeableProxy(ORACLE_POD), - address(newEventPodImplementation), - "" + ITransparentUpgradeableProxy(ORACLE_POD), address(newEventPodImplementation), "" ); console.log("Upgrade completed successfully!"); vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Oracle/deployOracle.s.sol b/script/Oracle/deployOracle.s.sol index 01a7e43..52db890 100644 --- a/script/Oracle/deployOracle.s.sol +++ b/script/Oracle/deployOracle.s.sol @@ -31,38 +31,25 @@ contract deployOracleScript is Script { // Deploy BLSApkRegistry proxy and delegate to a empty contract first emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyBlsApkRegistry = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); blsApkRegistry = BLSApkRegistry(address(proxyBlsApkRegistry)); blsApkRegistryImplementation = new BLSApkRegistry(); - blsApkRegistryProxyAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyBlsApkRegistry)) - ); + blsApkRegistryProxyAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyBlsApkRegistry))); // Deploy OracleManager proxy and delegate to a empty contract first - TransparentUpgradeableProxy proxyOracleManager = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyOracleManager = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); oracleManager = OracleManager(address(proxyOracleManager)); oracleManagerImplementation = new OracleManager(); - oracleManagerAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyOracleManager)) - ); + oracleManagerAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyOracleManager))); // Upgrade and initialize the implementations blsApkRegistryProxyAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(blsApkRegistry)), address(blsApkRegistryImplementation), abi.encodeWithSelector( - BLSApkRegistry.initialize.selector, - deployerAddress, - relayerManagerAddr, - address(proxyOracleManager) + BLSApkRegistry.initialize.selector, deployerAddress, relayerManagerAddr, address(proxyOracleManager) ) ); @@ -70,17 +57,11 @@ contract deployOracleScript is Script { ITransparentUpgradeableProxy(address(oracleManager)), address(oracleManagerImplementation), abi.encodeWithSelector( - OracleManager.initialize.selector, - deployerAddress, - proxyBlsApkRegistry, - deployerAddress + OracleManager.initialize.selector, deployerAddress, proxyBlsApkRegistry, deployerAddress ) ); - console.log( - "deploy proxyBlsApkRegistry:", - address(proxyBlsApkRegistry) - ); + console.log("deploy proxyBlsApkRegistry:", address(proxyBlsApkRegistry)); console.log("deploy proxyOracleManager:", address(proxyOracleManager)); string memory path = "deployed_addresses.json"; string memory data = string( @@ -97,9 +78,7 @@ contract deployOracleScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Oracle/deployOraclePod.s.sol b/script/Oracle/deployOraclePod.s.sol index e7d40cb..ed88b5c 100644 --- a/script/Oracle/deployOraclePod.s.sol +++ b/script/Oracle/deployOraclePod.s.sol @@ -29,30 +29,18 @@ contract deployOraclePodScript is Script { vm.startBroadcast(deployerPrivateKey); emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyOraclePod = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyOraclePod = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); oraclePod = OraclePod(address(proxyOraclePod)); oraclePodImplementation = new OraclePod(); - oraclePodAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyOraclePod)) - ); + oraclePodAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyOraclePod))); - console.log( - "oraclePodImplementation===", - address(oraclePodImplementation) - ); + console.log("oraclePodImplementation===", address(oraclePodImplementation)); oraclePodAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(oraclePod)), address(oraclePodImplementation), - abi.encodeWithSelector( - OraclePod.initialize.selector, - deployerAddress, - oracleManagerAddr - ) + abi.encodeWithSelector(OraclePod.initialize.selector, deployerAddress, oracleManagerAddr) ); // OracleManager(oracleManagerAddr).addOraclePodToFillWhitelist(proxyOraclePod); @@ -73,9 +61,7 @@ contract deployOraclePodScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Oracle/upgradeOraclePod.s.sol b/script/Oracle/upgradeOraclePod.s.sol index 60fde6d..e71ff0a 100644 --- a/script/Oracle/upgradeOraclePod.s.sol +++ b/script/Oracle/upgradeOraclePod.s.sol @@ -30,24 +30,17 @@ contract upgradeOraclePodScript is Script { OraclePod newOraclePodImplementation = new OraclePod(); - console.log( - "New OraclePod implementation:", - address(newOraclePodImplementation) - ); + console.log("New OraclePod implementation:", address(newOraclePodImplementation)); messageManagerProxyAdmin.upgradeAndCall( - ITransparentUpgradeableProxy(ORACLE_POD), - address(newOraclePodImplementation), - "" + ITransparentUpgradeableProxy(ORACLE_POD), address(newOraclePodImplementation), "" ); console.log("Upgrade completed successfully!"); vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Vrf/deployVrf.s.sol b/script/Vrf/deployVrf.s.sol index 26279a5..e42afc9 100644 --- a/script/Vrf/deployVrf.s.sol +++ b/script/Vrf/deployVrf.s.sol @@ -32,38 +32,25 @@ contract deployVrfScript is Script { // Deploy BLSApkRegistry proxy and delegate to a empty contract first emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyBlsApkRegistry = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyBlsApkRegistry = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); blsApkRegistry = BLSApkRegistry(address(proxyBlsApkRegistry)); blsApkRegistryImplementation = new BLSApkRegistry(); - blsApkRegistryProxyAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyBlsApkRegistry)) - ); + blsApkRegistryProxyAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyBlsApkRegistry))); // Deploy VrfManager proxy and delegate to a empty contract first - TransparentUpgradeableProxy proxyVrfManager = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyVrfManager = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); vrfManager = VrfManager(address(proxyVrfManager)); vrfManagerImplementation = new VrfManager(); - vrfManagerAdmin = ProxyAdmin( - getProxyAdminAddress(address(proxyVrfManager)) - ); + vrfManagerAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyVrfManager))); // Upgrade and initialize the implementations blsApkRegistryProxyAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(blsApkRegistry)), address(blsApkRegistryImplementation), abi.encodeWithSelector( - BLSApkRegistry.initialize.selector, - deployerAddress, - relayerManagerAddr, - address(proxyVrfManager) + BLSApkRegistry.initialize.selector, deployerAddress, relayerManagerAddr, address(proxyVrfManager) ) ); @@ -71,17 +58,11 @@ contract deployVrfScript is Script { ITransparentUpgradeableProxy(address(vrfManager)), address(vrfManagerImplementation), abi.encodeWithSelector( - VrfManager.initialize.selector, - deployerAddress, - proxyBlsApkRegistry, - deployerAddress + VrfManager.initialize.selector, deployerAddress, proxyBlsApkRegistry, deployerAddress ) ); - console.log( - "deploy proxyBlsApkRegistry:", - address(proxyBlsApkRegistry) - ); + console.log("deploy proxyBlsApkRegistry:", address(proxyBlsApkRegistry)); console.log("deploy proxyVrfManager:", address(proxyVrfManager)); string memory path = "deployed_addresses.json"; string memory data = string( @@ -98,9 +79,7 @@ contract deployVrfScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Vrf/deployVrfPod.s.sol b/script/Vrf/deployVrfPod.s.sol index e405821..e9d44bc 100644 --- a/script/Vrf/deployVrfPod.s.sol +++ b/script/Vrf/deployVrfPod.s.sol @@ -30,11 +30,8 @@ contract deployVrfPodScript is Script { vm.startBroadcast(deployerPrivateKey); emptyContract = new EmptyContract(); - TransparentUpgradeableProxy proxyVrfPod = new TransparentUpgradeableProxy( - address(emptyContract), - deployerAddress, - "" - ); + TransparentUpgradeableProxy proxyVrfPod = + new TransparentUpgradeableProxy(address(emptyContract), deployerAddress, ""); vrfPod = VrfPod(address(proxyVrfPod)); vrfPodImplementation = new VrfPod(); vrfPodAdmin = ProxyAdmin(getProxyAdminAddress(address(proxyVrfPod))); @@ -44,11 +41,7 @@ contract deployVrfPodScript is Script { vrfPodAdmin.upgradeAndCall( ITransparentUpgradeableProxy(address(vrfPod)), address(vrfPodImplementation), - abi.encodeWithSelector( - VrfPod.initialize.selector, - deployerAddress, - vrfManagerAddr - ) + abi.encodeWithSelector(VrfPod.initialize.selector, deployerAddress, vrfManagerAddr) ); // VrfManager(vrfManagerAddr).addVrfPodToFillWhitelist(proxyVrfPod); @@ -69,9 +62,7 @@ contract deployVrfPodScript is Script { vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/script/Vrf/upgradeVrfPod.s.sol b/script/Vrf/upgradeVrfPod.s.sol index 26fd129..72a15d3 100644 --- a/script/Vrf/upgradeVrfPod.s.sol +++ b/script/Vrf/upgradeVrfPod.s.sol @@ -30,24 +30,17 @@ contract upgradeVrfPodScript is Script { VrfPod newVrfPodImplementation = new VrfPod(); - console.log( - "New VrfPod implementation:", - address(newVrfPodImplementation) - ); + console.log("New VrfPod implementation:", address(newVrfPodImplementation)); messageManagerProxyAdmin.upgradeAndCall( - ITransparentUpgradeableProxy(ORACLE_POD), - address(newVrfPodImplementation), - "" + ITransparentUpgradeableProxy(ORACLE_POD), address(newVrfPodImplementation), "" ); console.log("Upgrade completed successfully!"); vm.stopBroadcast(); } - function getProxyAdminAddress( - address proxy - ) internal view returns (address) { + function getProxyAdminAddress(address proxy) internal view returns (address) { address CHEATCODE_ADDRESS = 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D; Vm vm = Vm(CHEATCODE_ADDRESS); diff --git a/src/bls/BLSApkRegistry.sol b/src/bls/BLSApkRegistry.sol index be1032d..fb525d7 100644 --- a/src/bls/BLSApkRegistry.sol +++ b/src/bls/BLSApkRegistry.sol @@ -11,29 +11,19 @@ import "../interfaces/IBLSApkRegistry.sol"; import "./BLSApkRegistryStorage.sol"; -contract BLSApkRegistry is - Initializable, - OwnableUpgradeable, - IBLSApkRegistry, - BLSApkRegistryStorage, - EIP712 -{ +contract BLSApkRegistry is Initializable, OwnableUpgradeable, IBLSApkRegistry, BLSApkRegistryStorage, EIP712 { using BN254 for BN254.G1Point; uint256 internal constant PAIRING_EQUALITY_CHECK_GAS = 120000; modifier onlyWhiteListManager() { - require( - msg.sender == whiteListAddress, - "BLSApkRegistry.onlyWhiteListManager: caller is not white list address" - ); + require(msg.sender == whiteListAddress, "BLSApkRegistry.onlyWhiteListManager: caller is not white list address"); _; } modifier onlyOracleManager() { require( - msg.sender == oracleManager, - "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" + msg.sender == oracleManager, "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" ); _; } @@ -42,11 +32,10 @@ contract BLSApkRegistry is _disableInitializers(); } - function initialize( - address _initialOwner, - address _whiteListAddress, - address _oracleManager - ) external initializer { + function initialize(address _initialOwner, address _whiteListAddress, address _oracleManager) + external + initializer + { __Ownable_init(_initialOwner); whiteListAddress = _whiteListAddress; oracleManager = _oracleManager; @@ -54,17 +43,11 @@ contract BLSApkRegistry is } function registerOperator(address operator) public onlyOracleManager { - require( - operator != address(0), - "BLSApkRegistry.registerBLSPublicKey: Operator is zero address" - ); + require(operator != address(0), "BLSApkRegistry.registerBLSPublicKey: Operator is zero address"); - require( - !operatorIsRegister[operator], - "BLSApkRegistry.registerBLSPublicKey: Operator have already register" - ); + require(!operatorIsRegister[operator], "BLSApkRegistry.registerBLSPublicKey: Operator have already register"); - (BN254.G1Point memory pubkey, ) = getRegisteredPubkey(operator); + (BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator); _processApkUpdate(pubkey); @@ -76,12 +59,9 @@ contract BLSApkRegistry is } function deregisterOperator(address operator) public onlyOracleManager { - require( - operatorIsRegister[operator], - "BLSApkRegistry.registerBLSPublicKey: Operator have already deregister" - ); + require(operatorIsRegister[operator], "BLSApkRegistry.registerBLSPublicKey: Operator have already deregister"); - (BN254.G1Point memory pubkey, ) = getRegisteredPubkey(operator); + (BN254.G1Point memory pubkey,) = getRegisteredPubkey(operator); _processApkUpdate(pubkey.negate()); @@ -97,10 +77,7 @@ contract BLSApkRegistry is PubkeyRegistrationParams calldata params, BN254.G1Point calldata pubkeyRegistrationMessageHash ) external returns (bytes32) { - require( - msg.sender == operator, - "BLSApkRegistry.registerBLSPublicKey: this caller is not operator" - ); + require(msg.sender == operator, "BLSApkRegistry.registerBLSPublicKey: this caller is not operator"); require( blsRegisterWhitelist[msg.sender], @@ -109,10 +86,7 @@ contract BLSApkRegistry is bytes32 pubkeyHash = BN254.hashG1Point(params.pubkeyG1); - require( - pubkeyHash != ZERO_PK_HASH, - "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey" - ); + require(pubkeyHash != ZERO_PK_HASH, "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey"); require( operatorToPubkeyHash[operator] == bytes32(0), "BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey" @@ -140,13 +114,9 @@ contract BLSApkRegistry is require( BN254.pairing( - params.pubkeyRegistrationSignature.plus( - params.pubkeyG1.scalar_mul(gamma) - ), + params.pubkeyRegistrationSignature.plus(params.pubkeyG1.scalar_mul(gamma)), BN254.negGeneratorG2(), - pubkeyRegistrationMessageHash.plus( - BN254.generatorG1().scalar_mul(gamma) - ), + pubkeyRegistrationMessageHash.plus(BN254.generatorG1().scalar_mul(gamma)), params.pubkeyG2 ), "BLSApkRegistry.registerBLSPublicKey: either the G1 signature is wrong, or G1 and G2 private key do not match" @@ -161,14 +131,13 @@ contract BLSApkRegistry is return pubkeyHash; } - function checkSignatures( - bytes32 msgHash, - uint256 referenceBlockNumber, - NonSignerAndSignature memory params - ) public view returns (uint256, bytes32) { + function checkSignatures(bytes32 msgHash, uint256 referenceBlockNumber, NonSignerAndSignature memory params) + public + view + returns (uint256, bytes32) + { require( - referenceBlockNumber < uint32(block.number), - "BLSSignatureChecker.checkSignatures: invalid reference block" + referenceBlockNumber < uint32(block.number), "BLSSignatureChecker.checkSignatures: invalid reference block" ); uint256 nonSingerNode = params.nonSignerPubkeys.length; @@ -182,53 +151,26 @@ contract BLSApkRegistry is BN254.G1Point memory signerApk = BN254.G1Point(0, 0); bytes32[] memory nonSignersPubkeyHashes; if (params.nonSignerPubkeys.length > 0) { - nonSignersPubkeyHashes = new bytes32[]( - params.nonSignerPubkeys.length - ); + nonSignersPubkeyHashes = new bytes32[](params.nonSignerPubkeys.length); for (uint256 j = 0; j < params.nonSignerPubkeys.length; j++) { - nonSignersPubkeyHashes[j] = params - .nonSignerPubkeys[j] - .hashG1Point(); - signerApk = currentApk.plus( - params.nonSignerPubkeys[j].negate() - ); + nonSignersPubkeyHashes[j] = params.nonSignerPubkeys[j].hashG1Point(); + signerApk = currentApk.plus(params.nonSignerPubkeys[j].negate()); } } else { signerApk = currentApk; } - ( - bool pairingSuccessful, - bool signatureIsValid - ) = trySignatureAndApkVerification( - msgHash, - signerApk, - params.apkG2, - params.sigma - ); - require( - pairingSuccessful, - "BLSSignatureChecker.checkSignatures: pairing precompile call failed" - ); - require( - signatureIsValid, - "BLSSignatureChecker.checkSignatures: signature is invalid" - ); + (bool pairingSuccessful, bool signatureIsValid) = + trySignatureAndApkVerification(msgHash, signerApk, params.apkG2, params.sigma); + require(pairingSuccessful, "BLSSignatureChecker.checkSignatures: pairing precompile call failed"); + require(signatureIsValid, "BLSSignatureChecker.checkSignatures: signature is invalid"); - bytes32 signatoryRecordHash = keccak256( - abi.encodePacked(referenceBlockNumber, nonSignersPubkeyHashes) - ); + bytes32 signatoryRecordHash = keccak256(abi.encodePacked(referenceBlockNumber, nonSignersPubkeyHashes)); return (params.totalStake, signatoryRecordHash); } - function addOrRemoveBlsRegisterWhitelist( - address register, - bool isAdd - ) external onlyWhiteListManager { - require( - register != address(0), - "BLSApkRegistry.addOrRemoverBlsRegisterWhitelist: operator address is zero" - ); + function addOrRemoveBlsRegisterWhitelist(address register, bool isAdd) external onlyWhiteListManager { + require(register != address(0), "BLSApkRegistry.addOrRemoverBlsRegisterWhitelist: operator address is zero"); blsRegisterWhitelist[register] = isAdd; } @@ -241,15 +183,7 @@ contract BLSApkRegistry is uint256 gamma = uint256( keccak256( abi.encodePacked( - msgHash, - apk.X, - apk.Y, - apkG2.X[0], - apkG2.X[1], - apkG2.Y[0], - apkG2.Y[1], - sigma.X, - sigma.Y + msgHash, apk.X, apk.Y, apkG2.X[0], apkG2.X[1], apkG2.Y[0], apkG2.Y[1], sigma.X, sigma.Y ) ) ) % BN254.FR_MODULUS; @@ -266,10 +200,7 @@ contract BLSApkRegistry is BN254.G1Point memory newApk; uint256 historyLength = apkHistory.length; - require( - historyLength != 0, - "BLSApkRegistry._processApkUpdate: quorum does not exist" - ); + require(historyLength != 0, "BLSApkRegistry._processApkUpdate: quorum does not exist"); newApk = currentApk.plus(point); currentApk = newApk; @@ -282,54 +213,29 @@ contract BLSApkRegistry is } else { lastUpdate.nextUpdateBlockNumber = uint32(block.number); apkHistory.push( - ApkUpdate({ - apkHash: newApkHash, - updateBlockNumber: uint32(block.number), - nextUpdateBlockNumber: 0 - }) + ApkUpdate({apkHash: newApkHash, updateBlockNumber: uint32(block.number), nextUpdateBlockNumber: 0}) ); } } - function getRegisteredPubkey( - address operator - ) public view returns (BN254.G1Point memory, bytes32) { + function getRegisteredPubkey(address operator) public view returns (BN254.G1Point memory, bytes32) { BN254.G1Point memory pubkey = operatorToPubkey[operator]; bytes32 pubkeyHash = operatorToPubkeyHash[operator]; - require( - pubkeyHash != bytes32(0), - "BLSApkRegistry.getRegisteredPubkey: operator is not registered" - ); + require(pubkeyHash != bytes32(0), "BLSApkRegistry.getRegisteredPubkey: operator is not registered"); return (pubkey, pubkeyHash); } - function getPubkeyRegMessageHash( - address operator - ) public view returns (BN254.G1Point memory) { - return - BN254.hashToG1( - _hashTypedDataV4( - keccak256( - abi.encode(PUBKEY_REGISTRATION_TYPEHASH, operator) - ) - ) - ); + function getPubkeyRegMessageHash(address operator) public view returns (BN254.G1Point memory) { + return BN254.hashToG1(_hashTypedDataV4(keccak256(abi.encode(PUBKEY_REGISTRATION_TYPEHASH, operator)))); } function _initializeApk() internal { - require( - apkHistory.length == 0, - "BLSApkRegistry.initializeApk: apk already exists" - ); + require(apkHistory.length == 0, "BLSApkRegistry.initializeApk: apk already exists"); apkHistory.push( - ApkUpdate({ - apkHash: bytes24(0), - updateBlockNumber: uint32(block.number), - nextUpdateBlockNumber: 0 - }) + ApkUpdate({apkHash: bytes24(0), updateBlockNumber: uint32(block.number), nextUpdateBlockNumber: 0}) ); } diff --git a/src/core/EventManager.sol b/src/core/EventManager.sol index fc6a1ff..9be9363 100644 --- a/src/core/EventManager.sol +++ b/src/core/EventManager.sol @@ -16,11 +16,10 @@ contract EventManager is OwnableUpgradeable, PodManager, EventManagerStorage { _disableInitializers(); } - function initialize( - address _initialOwner, - address _blsApkRegistry, - address _aggregatorAddress - ) external initializer { + function initialize(address _initialOwner, address _blsApkRegistry, address _aggregatorAddress) + external + initializer + { __Ownable_init(_initialOwner); __PodManager_init(_blsApkRegistry, _aggregatorAddress); } @@ -29,30 +28,15 @@ contract EventManager is OwnableUpgradeable, PodManager, EventManagerStorage { IEventPod eventPod, PredictEvents calldata predictEvents, IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature - ) - external - onlyAggregatorManager - onlyPodWhitelistedForFill(address(eventPod)) - { - (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry - .checkSignatures( - predictEvents.msgHash, - predictEvents.blockNumber, - oracleNonSignerAndSignature - ); + ) external onlyAggregatorManager onlyPodWhitelistedForFill(address(eventPod)) { + (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry.checkSignatures( + predictEvents.msgHash, predictEvents.blockNumber, oracleNonSignerAndSignature + ); string memory winner = predictEvents.winner; - eventPod.submitEventResult( - predictEvents.requestId, - predictEvents.winner - ); + eventPod.submitEventResult(predictEvents.requestId, predictEvents.winner); - emit VerifyPredictEventSig( - predictEvents.requestId, - totalStaking, - signatoryRecordHash, - winner - ); + emit VerifyPredictEventSig(predictEvents.requestId, totalStaking, signatoryRecordHash, winner); } } diff --git a/src/core/EventManagerStorage.sol b/src/core/EventManagerStorage.sol index 9a718c6..1d516a2 100644 --- a/src/core/EventManagerStorage.sol +++ b/src/core/EventManagerStorage.sol @@ -5,7 +5,4 @@ import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; import "../interfaces/IEventManager.sol"; - -abstract contract EventManagerStorage is Initializable, IEventManager{ - -} +abstract contract EventManagerStorage is Initializable, IEventManager {} diff --git a/src/core/OracleManager.sol b/src/core/OracleManager.sol index afae3b6..75f5511 100644 --- a/src/core/OracleManager.sol +++ b/src/core/OracleManager.sol @@ -11,21 +11,15 @@ import "../interfaces/IOraclePod.sol"; import "./OracleManagerStorage.sol"; import "./PodManager.sol"; -contract OracleManager is - OwnableUpgradeable, - PodManager, - OracleManagerStorage, - IOracleManager -{ +contract OracleManager is OwnableUpgradeable, PodManager, OracleManagerStorage, IOracleManager { constructor() { _disableInitializers(); } - function initialize( - address _initialOwner, - address _blsApkRegistry, - address _aggregatorAddress - ) external initializer { + function initialize(address _initialOwner, address _blsApkRegistry, address _aggregatorAddress) + external + initializer + { __Ownable_init(_initialOwner); __PodManager_init(_blsApkRegistry, _aggregatorAddress); confirmBatchId = 0; @@ -35,27 +29,14 @@ contract OracleManager is IOraclePod oraclePod, OracleBatch calldata oracleBatch, IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature - ) - external - onlyAggregatorManager - onlyPodWhitelistedForFill(address(oraclePod)) - { - (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry - .checkSignatures( - oracleBatch.msgHash, - oracleBatch.blockNumber, - oracleNonSignerAndSignature - ); + ) external onlyAggregatorManager onlyPodWhitelistedForFill(address(oraclePod)) { + (uint256 totalStaking, bytes32 signatoryRecordHash) = + blsApkRegistry.checkSignatures(oracleBatch.msgHash, oracleBatch.blockNumber, oracleNonSignerAndSignature); string memory symbolPrice = oracleBatch.symbolPrice; oraclePod.fillSymbolPrice(symbolPrice); - emit VerifyOracleSig( - confirmBatchId++, - totalStaking, - signatoryRecordHash, - symbolPrice - ); + emit VerifyOracleSig(confirmBatchId++, totalStaking, signatoryRecordHash, symbolPrice); } } diff --git a/src/core/OracleManagerStorage.sol b/src/core/OracleManagerStorage.sol index aafdf5d..f953280 100644 --- a/src/core/OracleManagerStorage.sol +++ b/src/core/OracleManagerStorage.sol @@ -6,7 +6,6 @@ import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; import "../interfaces/IBLSApkRegistry.sol"; import "../interfaces/IOraclePod.sol"; - abstract contract OracleManagerStorage is Initializable { uint256 public confirmBatchId; } diff --git a/src/core/PodManager.sol b/src/core/PodManager.sol index 5d71d0c..62be86f 100644 --- a/src/core/PodManager.sol +++ b/src/core/PodManager.sol @@ -20,33 +20,23 @@ abstract contract PodManager is Initializable, OwnableUpgradeable { event PodRemoveToFillWhitelist(address pod); modifier onlyAggregatorManager() { - require( - msg.sender == aggregatorAddress, - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + require(msg.sender == aggregatorAddress, "PodManager.onlyAggregatorManager: not the aggregator address"); _; } modifier onlyPodWhitelistedForFill(address pod) { - require( - podIsWhitelistedForFill[pod], - "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" - ); + require(podIsWhitelistedForFill[pod], "PodManager.onlyPodWhitelistedForFill: pod not whitelisted"); _; } - function __PodManager_init( - address _blsApkRegistry, - address _aggregatorAddress - ) internal { + function __PodManager_init(address _blsApkRegistry, address _aggregatorAddress) internal { blsApkRegistry = IBLSApkRegistry(_blsApkRegistry); aggregatorAddress = _aggregatorAddress; } function registerOperator(string calldata nodeUrl) external { require( - operatorWhitelist[msg.sender], - "PodManager.registerOperator: this address have not permission to register " + operatorWhitelist[msg.sender], "PodManager.registerOperator: this address have not permission to register " ); blsApkRegistry.registerOperator(msg.sender); emit OperatorRegistered(msg.sender, nodeUrl); @@ -54,31 +44,19 @@ abstract contract PodManager is Initializable, OwnableUpgradeable { function deRegisterOperator() external { require( - operatorWhitelist[msg.sender], - "PodManager.registerOperator: this address have not permission to register " + operatorWhitelist[msg.sender], "PodManager.registerOperator: this address have not permission to register " ); blsApkRegistry.deregisterOperator(msg.sender); emit OperatorDeRegistered(msg.sender); } - function addOrRemoveOperatorWhitelist( - address operator, - bool isAdd - ) external onlyAggregatorManager { - require( - operator != address(0), - "PodManager.addOperatorWhitelist: operator address is zero" - ); + function addOrRemoveOperatorWhitelist(address operator, bool isAdd) external onlyAggregatorManager { + require(operator != address(0), "PodManager.addOperatorWhitelist: operator address is zero"); operatorWhitelist[operator] = isAdd; } - function setAggregatorAddress( - address _aggregatorAddress - ) external onlyOwner { - require( - _aggregatorAddress != address(0), - "PodManager.addAggregator: aggregatorAddress address is zero" - ); + function setAggregatorAddress(address _aggregatorAddress) external onlyOwner { + require(_aggregatorAddress != address(0), "PodManager.addAggregator: aggregatorAddress address is zero"); aggregatorAddress = _aggregatorAddress; } @@ -87,9 +65,7 @@ abstract contract PodManager is Initializable, OwnableUpgradeable { emit PodAddedToFillWhitelist(pod); } - function removePodToFillWhitelist( - address pod - ) external onlyAggregatorManager { + function removePodToFillWhitelist(address pod) external onlyAggregatorManager { podIsWhitelistedForFill[pod] = false; emit PodRemoveToFillWhitelist(pod); } diff --git a/src/core/VrfManager.sol b/src/core/VrfManager.sol index bd937a9..f178a91 100644 --- a/src/core/VrfManager.sol +++ b/src/core/VrfManager.sol @@ -16,11 +16,10 @@ contract VrfManager is OwnableUpgradeable, PodManager, VrfManagerStorage { _disableInitializers(); } - function initialize( - address _initialOwner, - address _blsApkRegistry, - address _aggregatorAddress - ) external initializer { + function initialize(address _initialOwner, address _blsApkRegistry, address _aggregatorAddress) + external + initializer + { __Ownable_init(_initialOwner); __PodManager_init(_blsApkRegistry, _aggregatorAddress); } @@ -29,28 +28,13 @@ contract VrfManager is OwnableUpgradeable, PodManager, VrfManagerStorage { IVrfPod vrfPod, VrfRandomWords calldata vrfRandomWords, IBLSApkRegistry.NonSignerAndSignature memory oracleNonSignerAndSignature - ) - external - onlyAggregatorManager - onlyPodWhitelistedForFill(address(vrfPod)) - { - (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry - .checkSignatures( - vrfRandomWords.msgHash, - vrfRandomWords.blockNumber, - oracleNonSignerAndSignature - ); - - vrfPod.fulfillRandomWords( - vrfRandomWords.requestId, - vrfRandomWords._randomWords + ) external onlyAggregatorManager onlyPodWhitelistedForFill(address(vrfPod)) { + (uint256 totalStaking, bytes32 signatoryRecordHash) = blsApkRegistry.checkSignatures( + vrfRandomWords.msgHash, vrfRandomWords.blockNumber, oracleNonSignerAndSignature ); - emit VerifyVrfSig( - vrfRandomWords.requestId, - totalStaking, - signatoryRecordHash, - vrfRandomWords._randomWords - ); + vrfPod.fulfillRandomWords(vrfRandomWords.requestId, vrfRandomWords._randomWords); + + emit VerifyVrfSig(vrfRandomWords.requestId, totalStaking, signatoryRecordHash, vrfRandomWords._randomWords); } } diff --git a/src/core/VrfManagerStorage.sol b/src/core/VrfManagerStorage.sol index 11fde43..79292bd 100644 --- a/src/core/VrfManagerStorage.sol +++ b/src/core/VrfManagerStorage.sol @@ -7,7 +7,4 @@ import "../interfaces/IBLSApkRegistry.sol"; import "../interfaces/IVrfPod.sol"; import "../interfaces/IVrfManager.sol"; - -abstract contract VrfManagerStorage is Initializable, IVrfManager{ - -} +abstract contract VrfManagerStorage is Initializable, IVrfManager {} diff --git a/src/interfaces/IBLSApkRegistry.sol b/src/interfaces/IBLSApkRegistry.sol index e536e8d..8b46a1f 100644 --- a/src/interfaces/IBLSApkRegistry.sol +++ b/src/interfaces/IBLSApkRegistry.sol @@ -23,11 +23,7 @@ interface IBLSApkRegistry { BN254.G2Point pubkeyG2; } - event NewPubkeyRegistration( - address indexed operator, - BN254.G1Point pubkeyG1, - BN254.G2Point pubkeyG2 - ); + event NewPubkeyRegistration(address indexed operator, BN254.G1Point pubkeyG1, BN254.G2Point pubkeyG2); event OperatorAdded(address operator, bytes32 operatorId); @@ -43,22 +39,14 @@ interface IBLSApkRegistry { BN254.G1Point memory msgHash ) external returns (bytes32); - function checkSignatures( - bytes32 msgHash, - uint256 referenceBlockNumber, - NonSignerAndSignature memory params - ) external view returns (uint256, bytes32); + function checkSignatures(bytes32 msgHash, uint256 referenceBlockNumber, NonSignerAndSignature memory params) + external + view + returns (uint256, bytes32); - function getRegisteredPubkey( - address operator - ) external view returns (BN254.G1Point memory, bytes32); + function getRegisteredPubkey(address operator) external view returns (BN254.G1Point memory, bytes32); - function addOrRemoveBlsRegisterWhitelist( - address operator, - bool isAdd - ) external; + function addOrRemoveBlsRegisterWhitelist(address operator, bool isAdd) external; - function getPubkeyRegMessageHash( - address operator - ) external view returns (BN254.G1Point memory); + function getPubkeyRegMessageHash(address operator) external view returns (BN254.G1Point memory); } diff --git a/src/interfaces/IEventManager.sol b/src/interfaces/IEventManager.sol index e8d0a80..0639f7a 100644 --- a/src/interfaces/IEventManager.sol +++ b/src/interfaces/IEventManager.sol @@ -2,14 +2,9 @@ pragma solidity ^0.8.20; interface IEventManager { - event VerifyPredictEventSig( - uint256 requestId, - uint256 totalStaking, - bytes32 signatoryRecordHash, - string winner - ); + event VerifyPredictEventSig(uint256 requestId, uint256 totalStaking, bytes32 signatoryRecordHash, string winner); - struct PredictEvents{ + struct PredictEvents { uint256 requestId; string winner; bytes32 blockHash; diff --git a/src/interfaces/IEventPod.sol b/src/interfaces/IEventPod.sol index bdf6eaa..1fa4c69 100644 --- a/src/interfaces/IEventPod.sol +++ b/src/interfaces/IEventPod.sol @@ -3,22 +3,21 @@ pragma solidity ^0.8.20; interface IEventPod { event CreatePredictEvent( - uint256 requestId, - string eventDescribe, - string predictPosSide, - string predictNegSid, - address podAddress + uint256 requestId, string eventDescribe, string predictPosSide, string predictNegSid, address podAddress ); - event PredictEventResult( - uint256 requestId, - string winner, - string predictPosSide, - string predictNegSid - ); + event PredictEventResult(uint256 requestId, string winner, string predictPosSide, string predictNegSid); - function createEvent(uint256 _requestId, string memory _eventDescribe, string memory _predictPosSide, string memory _predictNegSide) external; + function createEvent( + uint256 _requestId, + string memory _eventDescribe, + string memory _predictPosSide, + string memory _predictNegSide + ) external; function submitEventResult(uint256 _requestId, string memory _winner) external; - function fetchEventResult(uint256 _requestId) external view returns (string memory predictPosSide, string memory predictNegSid, string memory winner); + function fetchEventResult(uint256 _requestId) + external + view + returns (string memory predictPosSide, string memory predictNegSid, string memory winner); function setEventManager(address _eventManager) external; } diff --git a/src/interfaces/IOracleManager.sol b/src/interfaces/IOracleManager.sol index db548e0..75e1a0a 100644 --- a/src/interfaces/IOracleManager.sol +++ b/src/interfaces/IOracleManager.sol @@ -6,12 +6,7 @@ import "./IBLSApkRegistry.sol"; import {IOraclePod} from "./IOraclePod.sol"; interface IOracleManager { - event VerifyOracleSig( - uint256 batchId, - uint256 totalStaking, - bytes32 signatoryRecordHash, - string marketPrice - ); + event VerifyOracleSig(uint256 batchId, uint256 totalStaking, bytes32 signatoryRecordHash, string marketPrice); struct OracleBatch { string symbolPrice; diff --git a/src/interfaces/IOraclePod.sol b/src/interfaces/IOraclePod.sol index c98082b..bd7d8a8 100644 --- a/src/interfaces/IOraclePod.sol +++ b/src/interfaces/IOraclePod.sol @@ -2,16 +2,9 @@ pragma solidity ^0.8.20; interface IOraclePod { - event MarketPriceUpdated( - string oldPrice , - string price, - uint256 timestamp - ); + event MarketPriceUpdated(string oldPrice, string price, uint256 timestamp); - event OracleManagerUpdate( - address oldManagerAddress , - address newManagerAddress - ); + event OracleManagerUpdate(address oldManagerAddress, address newManagerAddress); function fillSymbolPrice(string memory price) external; function isDataFresh(uint256 maxAge) external view returns (bool); diff --git a/src/interfaces/IVrfManager.sol b/src/interfaces/IVrfManager.sol index 78bc0a8..ce25f38 100644 --- a/src/interfaces/IVrfManager.sol +++ b/src/interfaces/IVrfManager.sol @@ -6,12 +6,7 @@ import "./IBLSApkRegistry.sol"; import {IVrfPod} from "./IVrfPod.sol"; interface IVrfManager { - event VerifyVrfSig( - uint256 requestId, - uint256 totalStaking, - bytes32 signatoryRecordHash, - uint256[] _randomWords - ); + event VerifyVrfSig(uint256 requestId, uint256 totalStaking, bytes32 signatoryRecordHash, uint256[] _randomWords); struct VrfRandomWords { uint256 requestId; diff --git a/src/interfaces/IVrfPod.sol b/src/interfaces/IVrfPod.sol index 8ba6719..43f785d 100644 --- a/src/interfaces/IVrfPod.sol +++ b/src/interfaces/IVrfPod.sol @@ -2,19 +2,15 @@ pragma solidity ^0.8.20; interface IVrfPod { - event RequestSent( - uint256 requestId, - uint256 _numWords, - address current - ); + event RequestSent(uint256 requestId, uint256 _numWords, address current); - event FillRandomWords( - uint256 requestId, - uint256[] randomWords - ); + event FillRandomWords(uint256 requestId, uint256[] randomWords); function requestRandomWords(uint256 _requestId, uint256 _numWords) external; function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) external; - function getRandomWordsWithStatus(uint256 _requestId) external view returns (bool fulfilled, uint256[] memory randomWords); + function getRandomWordsWithStatus(uint256 _requestId) + external + view + returns (bool fulfilled, uint256[] memory randomWords); function setVrfManager(address _vrfManager) external; } diff --git a/src/libraries/BN254.sol b/src/libraries/BN254.sol index 0ef9c41..9ac994b 100644 --- a/src/libraries/BN254.sol +++ b/src/libraries/BN254.sol @@ -3,11 +3,9 @@ pragma solidity ^0.8.20; library BN254 { // modulus for the underlying field F_p of the elliptic curve - uint256 internal constant FP_MODULUS = - 21888242871839275222246405745257275088696311157297823662689037894645226208583; + uint256 internal constant FP_MODULUS = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // modulus for the underlying field F_r of the elliptic curve - uint256 internal constant FR_MODULUS = - 21888242871839275222246405745257275088548364400416034343698204186575808495617; + uint256 internal constant FR_MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617; struct G1Point { uint256 X; @@ -51,8 +49,7 @@ library BN254 { return G2Point([nG2x1, nG2x0], [nG2y1, nG2y0]); } - bytes32 internal constant powersOfTauMerkleRoot = - 0x22c998e49752bbb1918ba87d6d59dd0e83620a311ba91dd4b2cc84990b31b56f; + bytes32 internal constant powersOfTauMerkleRoot = 0x22c998e49752bbb1918ba87d6d59dd0e83620a311ba91dd4b2cc84990b31b56f; /** * @param p Some point in G1. @@ -81,11 +78,9 @@ library BN254 { // solium-disable-next-line security/no-inline-assembly assembly { success := staticcall(sub(gas(), 2000), 6, input, 0x80, r, 0x40) - // Use "invalid" to make gas estimation work + // Use "invalid" to make gas estimation work switch success - case 0 { - invalid() - } + case 0 { invalid() } } require(success, "ec-add-failed"); @@ -98,10 +93,10 @@ library BN254 { * @dev this function is only safe to use if the scalar is 9 bits or less */ function scalar_mul_tiny(BN254.G1Point memory p, uint16 s) internal view returns (BN254.G1Point memory) { - require(s < 2**9, "scalar-too-large"); + require(s < 2 ** 9, "scalar-too-large"); // if s is 1 return p - if(s == 1) { + if (s == 1) { return p; } @@ -115,16 +110,16 @@ library BN254 { uint8 i = 0; //loop until we reach the most significant bit - while(s >= m){ + while (s >= m) { unchecked { - // if the current bit is 1, add the 2^n*p to the accumulated product + // if the current bit is 1, add the 2^n*p to the accumulated product if ((s >> i) & 1 == 1) { acc = plus(acc, p2n); } - // double the 2^n*p for the next iteration + // double the 2^n*p for the next iteration p2n = plus(p2n, p2n); - // increment the index and double the value of the most significant bit + // increment the index and double the value of the most significant bit m <<= 1; ++i; } @@ -148,11 +143,9 @@ library BN254 { // solium-disable-next-line security/no-inline-assembly assembly { success := staticcall(sub(gas(), 2000), 7, input, 0x60, r, 0x40) - // Use "invalid" to make gas estimation work + // Use "invalid" to make gas estimation work switch success - case 0 { - invalid() - } + case 0 { invalid() } } require(success, "ec-mul-failed"); } @@ -163,12 +156,11 @@ library BN254 { * For example, * pairing([P1(), P1().negate()], [P2(), P2()]) should return true. */ - function pairing( - G1Point memory a1, - G2Point memory a2, - G1Point memory b1, - G2Point memory b2 - ) internal view returns (bool) { + function pairing(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) + internal + view + returns (bool) + { G1Point[2] memory p1 = [a1, b1]; G2Point[2] memory p2 = [a2, b2]; @@ -190,11 +182,9 @@ library BN254 { // solium-disable-next-line security/no-inline-assembly assembly { success := staticcall(sub(gas(), 2000), 8, input, mul(12, 0x20), out, 0x20) - // Use "invalid" to make gas estimation work + // Use "invalid" to make gas estimation work switch success - case 0 { - invalid() - } + case 0 { invalid() } } require(success, "pairing-opcode-failed"); @@ -206,13 +196,11 @@ library BN254 { * @notice This function is functionally the same as pairing(), however it specifies a gas limit * the user can set, as a precompile may use the entire gas budget if it reverts. */ - function safePairing( - G1Point memory a1, - G2Point memory a2, - G1Point memory b1, - G2Point memory b2, - uint256 pairingGas - ) internal view returns (bool, bool) { + function safePairing(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2, uint256 pairingGas) + internal + view + returns (bool, bool) + { G1Point[2] memory p1 = [a1, b1]; G2Point[2] memory p2 = [a2, b2]; @@ -254,9 +242,7 @@ library BN254 { /// @return the keccak256 hash of the G2 Point /// @dev used for BLS signatures - function hashG2Point( - BN254.G2Point memory pk - ) internal pure returns (bytes32) { + function hashG2Point(BN254.G2Point memory pk) internal pure returns (bytes32) { return keccak256(abi.encodePacked(pk.X[0], pk.X[1], pk.Y[0], pk.Y[1])); } @@ -273,7 +259,7 @@ library BN254 { (beta, y) = findYFromX(x); // y^2 == beta - if( beta == mulmod(y, y, FP_MODULUS) ) { + if (beta == mulmod(y, y, FP_MODULUS)) { return G1Point(x, y); } @@ -303,7 +289,7 @@ library BN254 { function expMod(uint256 _base, uint256 _exponent, uint256 _modulus) internal view returns (uint256 retval) { bool success; uint256[1] memory output; - uint[6] memory input; + uint256[6] memory input; input[0] = 0x20; // baseLen = new(big.Int).SetBytes(getData(input, 0, 32)) input[1] = 0x20; // expLen = new(big.Int).SetBytes(getData(input, 32, 32)) input[2] = 0x20; // modLen = new(big.Int).SetBytes(getData(input, 64, 32)) @@ -312,11 +298,9 @@ library BN254 { input[5] = _modulus; assembly { success := staticcall(sub(gas(), 2000), 5, input, 0xc0, output, 0x20) - // Use "invalid" to make gas estimation work + // Use "invalid" to make gas estimation work switch success - case 0 { - invalid() - } + case 0 { invalid() } } require(success, "BN254.expMod: call failure"); return output[0]; diff --git a/src/libraries/SafeCall.sol b/src/libraries/SafeCall.sol index 94476f0..eb716d7 100644 --- a/src/libraries/SafeCall.sol +++ b/src/libraries/SafeCall.sol @@ -2,43 +2,36 @@ pragma solidity ^0.8.20; library SafeCall { - function send( - address _target, - uint256 _gas, - uint256 _value - ) internal returns (bool) { + function send(address _target, uint256 _gas, uint256 _value) internal returns (bool) { bool _success; assembly { - _success := call( - _gas, // gas - _target, // recipient - _value, // ether value - 0, // inloc - 0, // inlen - 0, // outloc - 0 // outlen - ) + _success := + call( + _gas, // gas + _target, // recipient + _value, // ether value + 0, // inloc + 0, // inlen + 0, // outloc + 0 // outlen + ) } return _success; } - function call( - address _target, - uint256 _gas, - uint256 _value, - bytes memory _calldata - ) internal returns (bool) { + function call(address _target, uint256 _gas, uint256 _value, bytes memory _calldata) internal returns (bool) { bool _success; assembly { - _success := call( - _gas, // gas - _target, // recipient - _value, // ether value - add(_calldata, 32), // inloc - mload(_calldata), // inlen - 0, // outloc - 0 // outlen - ) + _success := + call( + _gas, // gas + _target, // recipient + _value, // ether value + add(_calldata, 32), // inloc + mload(_calldata), // inlen + 0, // outloc + 0 // outlen + ) } return _success; } @@ -46,59 +39,56 @@ library SafeCall { function hasMinGas(uint256 _minGas, uint256 _reservedGas) internal view returns (bool) { bool _hasMinGas; assembly { - // Equation: gas × 63 ≥ minGas × 64 + 63(40_000 + reservedGas) - _hasMinGas := iszero( - lt(mul(gas(), 63), add(mul(_minGas, 64), mul(add(40000, _reservedGas), 63))) - ) + // Equation: gas × 63 ≥ minGas × 64 + 63(40_000 + reservedGas) + _hasMinGas := iszero(lt(mul(gas(), 63), add(mul(_minGas, 64), mul(add(40000, _reservedGas), 63)))) } return _hasMinGas; } - function callWithMinGas( - address _target, - uint256 _minGas, - uint256 _value, - bytes memory _calldata - ) internal returns (bool) { + function callWithMinGas(address _target, uint256 _minGas, uint256 _value, bytes memory _calldata) + internal + returns (bool) + { bool _success; bool _hasMinGas = hasMinGas(_minGas, 0); assembly { - // Assertion: gasleft() >= (_minGas * 64) / 63 + 40_000 + // Assertion: gasleft() >= (_minGas * 64) / 63 + 40_000 if iszero(_hasMinGas) { - // Store the "Error(string)" selector in scratch space. + // Store the "Error(string)" selector in scratch space. mstore(0, 0x08c379a0) - // Store the pointer to the string length in scratch space. + // Store the pointer to the string length in scratch space. mstore(32, 32) - // Store the string. - // - // SAFETY: - // - We pad the beginning of the string with two zero bytes as well as the - // length (24) to ensure that we override the free memory pointer at offset - // 0x40. This is necessary because the free memory pointer is likely to - // be greater than 1 byte when this function is called, but it is incredibly - // unlikely that it will be greater than 3 bytes. As for the data within - // 0x60, it is ensured that it is 0 due to 0x60 being the zero offset. - // - It's fine to clobber the free memory pointer, we're reverting. + // Store the string. + // + // SAFETY: + // - We pad the beginning of the string with two zero bytes as well as the + // length (24) to ensure that we override the free memory pointer at offset + // 0x40. This is necessary because the free memory pointer is likely to + // be greater than 1 byte when this function is called, but it is incredibly + // unlikely that it will be greater than 3 bytes. As for the data within + // 0x60, it is ensured that it is 0 due to 0x60 being the zero offset. + // - It's fine to clobber the free memory pointer, we're reverting. mstore(88, 0x0000185361666543616c6c3a204e6f7420656e6f75676820676173) - // Revert with 'Error("SafeCall: Not enough gas")' + // Revert with 'Error("SafeCall: Not enough gas")' revert(28, 100) } - // The call will be supplied at least ((_minGas * 64) / 63) gas due to the - // above assertion. This ensures that, in all circumstances (except for when the - // `_minGas` does not account for the `memory_expansion_cost` and `code_execution_cost` - // factors of the dynamic cost of the `CALL` opcode), the call will receive at least - // the minimum amount of gas specified. - _success := call( - gas(), // gas - _target, // recipient - _value, // ether value - add(_calldata, 32), // inloc - mload(_calldata), // inlen - 0x00, // outloc - 0x00 // outlen - ) + // The call will be supplied at least ((_minGas * 64) / 63) gas due to the + // above assertion. This ensures that, in all circumstances (except for when the + // `_minGas` does not account for the `memory_expansion_cost` and `code_execution_cost` + // factors of the dynamic cost of the `CALL` opcode), the call will receive at least + // the minimum amount of gas specified. + _success := + call( + gas(), // gas + _target, // recipient + _value, // ether value + add(_calldata, 32), // inloc + mload(_calldata), // inlen + 0x00, // outloc + 0x00 // outlen + ) } return _success; } diff --git a/src/pod/EventPod.sol b/src/pod/EventPod.sol index 35ec77c..7dfc42e 100644 --- a/src/pod/EventPod.sol +++ b/src/pod/EventPod.sol @@ -12,17 +12,11 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { } modifier onlyEventManager() { - require( - msg.sender == eventManager, - "EventPod.onlyEventManager: caller is not the event manager address" - ); + require(msg.sender == eventManager, "EventPod.onlyEventManager: caller is not the event manager address"); _; } - function initialize( - address _initialOwner, - address _eventManager - ) external initializer { + function initialize(address _initialOwner, address _eventManager) external initializer { __Ownable_init(_initialOwner); eventManager = _eventManager; } @@ -40,19 +34,10 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { predictNegSide: _predictNegSide, winner: "unknown" }); - emit CreatePredictEvent( - _requestId, - _eventDescribe, - _predictPosSide, - _predictNegSide, - address(this) - ); + emit CreatePredictEvent(_requestId, _eventDescribe, _predictPosSide, _predictNegSide, address(this)); } - function submitEventResult( - uint256 _requestId, - string memory _winner - ) external onlyEventManager { + function submitEventResult(uint256 _requestId, string memory _winner) external onlyEventManager { predictEventMapping[_requestId].winner = _winner; emit PredictEventResult( _requestId, @@ -62,16 +47,10 @@ contract EventPod is Initializable, OwnableUpgradeable, EventPodStorage { ); } - function fetchEventResult( - uint256 requestId - ) + function fetchEventResult(uint256 requestId) external view - returns ( - string memory predictPosSide, - string memory predictNegSid, - string memory winner - ) + returns (string memory predictPosSide, string memory predictNegSid, string memory winner) { return ( predictEventMapping[requestId].predictPosSide, diff --git a/src/pod/EventPodStorage.sol b/src/pod/EventPodStorage.sol index f82ffae..d118a81 100644 --- a/src/pod/EventPodStorage.sol +++ b/src/pod/EventPodStorage.sol @@ -1,16 +1,15 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.20; -import { IEventPod } from "../interfaces/IEventPod.sol"; +import {IEventPod} from "../interfaces/IEventPod.sol"; - -abstract contract EventPodStorage is IEventPod{ +abstract contract EventPodStorage is IEventPod { struct PredictEventInfo { uint256 requestId; - string eventDescribe; - string predictPosSide; - string predictNegSide; - string winner; + string eventDescribe; + string predictPosSide; + string predictNegSide; + string winner; } address public eventManager; diff --git a/src/pod/OraclePod.sol b/src/pod/OraclePod.sol index b2a83ff..48b6f50 100644 --- a/src/pod/OraclePod.sol +++ b/src/pod/OraclePod.sol @@ -4,8 +4,7 @@ pragma solidity ^0.8.20; import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; -import { OraclePodStorage } from "./OraclePodStorage.sol"; - +import {OraclePodStorage} from "./OraclePodStorage.sol"; contract OraclePod is Initializable, OwnableUpgradeable, OraclePodStorage { constructor() { @@ -13,9 +12,7 @@ contract OraclePod is Initializable, OwnableUpgradeable, OraclePodStorage { } modifier onlyOracleManager() { - require ( - msg.sender == oracleManager, "OraclePod.onlyOracleManager: caller is not the oracle manager address" - ); + require(msg.sender == oracleManager, "OraclePod.onlyOracleManager: caller is not the oracle manager address"); _; } diff --git a/src/pod/OraclePodStorage.sol b/src/pod/OraclePodStorage.sol index e12d1db..43cb57e 100644 --- a/src/pod/OraclePodStorage.sol +++ b/src/pod/OraclePodStorage.sol @@ -1,9 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.20; - -import { IOraclePod } from "../interfaces/IOraclePod.sol"; - +import {IOraclePod} from "../interfaces/IOraclePod.sol"; abstract contract OraclePodStorage is IOraclePod { address public oracleManager; diff --git a/src/pod/VrfPod.sol b/src/pod/VrfPod.sol index 738c6b5..befc59d 100644 --- a/src/pod/VrfPod.sol +++ b/src/pod/VrfPod.sol @@ -4,9 +4,9 @@ pragma solidity ^0.8.20; import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; -import { VrfPodStorage } from "./VrfPodStorage.sol"; +import {VrfPodStorage} from "./VrfPodStorage.sol"; -contract VrfPod is Initializable, OwnableUpgradeable, VrfPodStorage{ +contract VrfPod is Initializable, OwnableUpgradeable, VrfPodStorage { modifier onlyVrfManager() { require(msg.sender == vrfManager, "DappLinkVRF.onlyVrfManager can call this function"); _; @@ -22,22 +22,20 @@ contract VrfPod is Initializable, OwnableUpgradeable, VrfPodStorage{ } function requestRandomWords(uint256 _requestId, uint256 _numWords) external { - randomWordsMapping[_requestId] = RandomWordsInfo({ - randomWords: new uint256[](0), - fulfilled: false - }); + randomWordsMapping[_requestId] = RandomWordsInfo({randomWords: new uint256[](0), fulfilled: false}); emit RequestSent(_requestId, _numWords, address(this)); } - function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) external onlyVrfManager { - randomWordsMapping[_requestId] = RandomWordsInfo({ - fulfilled: true, - randomWords: _randomWords - }); + function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) external onlyVrfManager { + randomWordsMapping[_requestId] = RandomWordsInfo({fulfilled: true, randomWords: _randomWords}); emit FillRandomWords(_requestId, _randomWords); } - function getRandomWordsWithStatus(uint256 _requestId) external view returns (bool fulfilled, uint256[] memory randomWords){ + function getRandomWordsWithStatus(uint256 _requestId) + external + view + returns (bool fulfilled, uint256[] memory randomWords) + { return (randomWordsMapping[_requestId].fulfilled, randomWordsMapping[_requestId].randomWords); } diff --git a/src/pod/VrfPodStorage.sol b/src/pod/VrfPodStorage.sol index 69ca573..a2eb9dd 100644 --- a/src/pod/VrfPodStorage.sol +++ b/src/pod/VrfPodStorage.sol @@ -1,8 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.20; -import { IVrfPod } from "../interfaces/IVrfPod.sol"; - +import {IVrfPod} from "../interfaces/IVrfPod.sol"; abstract contract VrfPodStorage is IVrfPod { struct RandomWordsInfo { diff --git a/test/TestEvent.t.sol b/test/TestEvent.t.sol index 15ae483..506b26f 100644 --- a/test/TestEvent.t.sol +++ b/test/TestEvent.t.sol @@ -21,24 +21,19 @@ contract EventPodTest is Test { uint256 requestId = 888; string winner = "pos"; - EventPodStorage.PredictEventInfo predictEventInfo = - EventPodStorage.PredictEventInfo({ - requestId: 888, - eventDescribe: "Team A vs Team B", - predictPosSide: "Team A wins", - predictNegSide: "Team B wins", - winner: "unknown" - }); + EventPodStorage.PredictEventInfo predictEventInfo = EventPodStorage.PredictEventInfo({ + requestId: 888, + eventDescribe: "Team A vs Team B", + predictPosSide: "Team A wins", + predictNegSide: "Team B wins", + winner: "unknown" + }); function setUp() public { vm.prank(deployer); logic = new EventPod(); - bytes memory initData = abi.encodeWithSelector( - EventPod.initialize.selector, - deployer, - eventManager - ); + bytes memory initData = abi.encodeWithSelector(EventPod.initialize.selector, deployer, eventManager); vm.prank(deployer); ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); @@ -53,20 +48,14 @@ contract EventPodTest is Test { function testOnlyEventManagerCanSubmitEventResult() public { // 非 EventManager 调用失败 vm.prank(other); - vm.expectRevert( - "EventPod.onlyEventManager: caller is not the event manager address" - ); + vm.expectRevert("EventPod.onlyEventManager: caller is not the event manager address"); pod.submitEventResult(requestId, winner); // EventManager 调用成功 vm.prank(eventManager); pod.submitEventResult(requestId, winner); - ( - string memory posSide, - string memory negSide, - string memory winner1 - ) = pod.fetchEventResult(requestId); + (string memory posSide, string memory negSide, string memory winner1) = pod.fetchEventResult(requestId); // assertEq(posSide, true); assertEq(winner1, winner); @@ -74,12 +63,7 @@ contract EventPodTest is Test { function testOnlyOwnerCanSetEventManager() public { vm.prank(eventManager); - vm.expectRevert( - abi.encodeWithSelector( - Ownable.OwnableUnauthorizedAccount.selector, - eventManager - ) - ); + vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, eventManager)); pod.setEventManager(address(0xD1)); vm.prank(deployer); @@ -88,12 +72,7 @@ contract EventPodTest is Test { } function testCreateEvent() public { - pod.createEvent( - requestId, - "Team A vs Team B", - "Team A wins", - "Team B wins" - ); + pod.createEvent(requestId, "Team A vs Team B", "Team A wins", "Team B wins"); ( uint256 reqId, @@ -103,11 +82,7 @@ contract EventPodTest is Test { string memory winner1 ) = pod.predictEventMapping(requestId); - ( - string memory posSide1, - string memory negSide1, - string memory winner11 - ) = pod.fetchEventResult(requestId); + (string memory posSide1, string memory negSide1, string memory winner11) = pod.fetchEventResult(requestId); assertEq(posSide1, posSide); assertEq(negSide1, negSide); @@ -186,20 +161,15 @@ contract EventManagerTest is Test { Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 }); - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: pubKeyG1, - pubkeyG2: pubKeyG2, - pubkeyRegistrationSignature: signature - }); + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); // operator注册新的 pubkey vm.prank(operator); - bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( - operator, - params, - msgHash - ); + bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey(operator, params, msgHash); vm.prank(address(eventManager)); blsRegistry.registerOperator(address(operator)); @@ -207,18 +177,14 @@ contract EventManagerTest is Test { function test_addOrRemoveOperatorWhitelist() public { vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); eventManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(aggregator); eventManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(aggregator); - vm.expectRevert( - "PodManager.addOperatorWhitelist: operator address is zero" - ); + vm.expectRevert("PodManager.addOperatorWhitelist: operator address is zero"); eventManager.addOrRemoveOperatorWhitelist(address(0), true); } @@ -231,23 +197,17 @@ contract EventManagerTest is Test { eventManager.setAggregatorAddress(aggregator); vm.prank(owner); - vm.expectRevert( - "PodManager.addAggregator: aggregatorAddress address is zero" - ); + vm.expectRevert("PodManager.addAggregator: aggregatorAddress address is zero"); eventManager.setAggregatorAddress(address(0)); } function test_addOrRemoveEventPodToFillWhitelist() public { vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); eventManager.addPodToFillWhitelist(address(eventPod)); vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); eventManager.removePodToFillWhitelist(address(eventPod)); vm.prank(aggregator); @@ -261,21 +221,15 @@ contract EventManagerTest is Test { eventManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); eventManager.registerOperator("http://node.url"); vm.prank(operator); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: Operator have already register" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: Operator have already register"); eventManager.registerOperator("http://node.url"); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); eventManager.deRegisterOperator(); vm.prank(operator); @@ -285,9 +239,7 @@ contract EventManagerTest is Test { eventManager.addOrRemoveOperatorWhitelist(operator, false); vm.prank(operator); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); eventManager.registerOperator("http://node.url"); } @@ -295,100 +247,74 @@ contract EventManagerTest is Test { vm.prank(aggregator); eventManager.addPodToFillWhitelist(address(eventPod)); - IBLSApkRegistry.NonSignerAndSignature - memory noSignerAndSignature = IBLSApkRegistry - .NonSignerAndSignature({ - nonSignerPubkeys: new BN254.G1Point[](0), - apkG2: BN254.G2Point({ - X: [ - 6814450613988925037276906495559354220267038225890288520888556922179861427221, - 11097154366204527428819849175191533397314611771099148982308553889852330000313 - ], - Y: [ - 20799884507081215979545766399242808376431798816319714422985505673585902041706, - 13670248609089265475970799020243713070902269374832615406626549692922451548915 - ] - }), - sigma: BN254.G1Point({ - X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, - Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 - }), - totalStake: 888 - }); - - IEventManager.PredictEvents memory predictEvents = IEventManager - .PredictEvents({ - msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - blockNumber: block.number - 1, - requestId: 888, - blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - winner: "pos" - }); + IBLSApkRegistry.NonSignerAndSignature memory noSignerAndSignature = IBLSApkRegistry.NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }), + sigma: BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }), + totalStake: 888 + }); + + IEventManager.PredictEvents memory predictEvents = IEventManager.PredictEvents({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + winner: "pos" + }); vm.prank(aggregator); - eventManager.fillEventResultWithSignature( - eventPod, - predictEvents, - noSignerAndSignature - ); - ( - string memory posSide1, - string memory negSide1, - string memory winner11 - ) = eventPod.fetchEventResult(888); + eventManager.fillEventResultWithSignature(eventPod, predictEvents, noSignerAndSignature); + (string memory posSide1, string memory negSide1, string memory winner11) = eventPod.fetchEventResult(888); assertEq(winner11, "pos"); } function testFillSymbolPriceWithoutWhitelistOrAuthority() public { - IBLSApkRegistry.NonSignerAndSignature - memory noSignerAndSignature = IBLSApkRegistry - .NonSignerAndSignature({ - nonSignerPubkeys: new BN254.G1Point[](0), - apkG2: BN254.G2Point({ - X: [ - 19552866287184064427995511006223057169680536518603642638640105365054342788017, - 19912786774583403697047133238687463296134677575618298225286334615015816916116 - ], - Y: [ - 2970994197396269892653525920024039859830728356246595152296683945713431676344, - 18119535013136907197909765078809655896321461883746857179927989514870514777799 - ] - }), - sigma: BN254.G1Point({ - X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, - Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 - }), - totalStake: 888 - }); - - IEventManager.PredictEvents memory predictEvents = IEventManager - .PredictEvents({ - msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - blockNumber: block.number - 1, - requestId: 888, - blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - winner: "pos" - }); + IBLSApkRegistry.NonSignerAndSignature memory noSignerAndSignature = IBLSApkRegistry.NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 19552866287184064427995511006223057169680536518603642638640105365054342788017, + 19912786774583403697047133238687463296134677575618298225286334615015816916116 + ], + Y: [ + 2970994197396269892653525920024039859830728356246595152296683945713431676344, + 18119535013136907197909765078809655896321461883746857179927989514870514777799 + ] + }), + sigma: BN254.G1Point({ + X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, + Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 + }), + totalStake: 888 + }); + + IEventManager.PredictEvents memory predictEvents = IEventManager.PredictEvents({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + winner: "pos" + }); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); - eventManager.fillEventResultWithSignature( - eventPod, - predictEvents, - noSignerAndSignature - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); + eventManager.fillEventResultWithSignature(eventPod, predictEvents, noSignerAndSignature); vm.prank(aggregator); - vm.expectRevert( - "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" - ); - eventManager.fillEventResultWithSignature( - eventPod, - predictEvents, - noSignerAndSignature - ); + vm.expectRevert("PodManager.onlyPodWhitelistedForFill: pod not whitelisted"); + eventManager.fillEventResultWithSignature(eventPod, predictEvents, noSignerAndSignature); } } diff --git a/test/TestOracle.t.sol b/test/TestOracle.t.sol index 59d30f1..34edf67 100644 --- a/test/TestOracle.t.sol +++ b/test/TestOracle.t.sol @@ -21,11 +21,7 @@ contract OraclePodTest is Test { vm.prank(deployer); logic = new OraclePod(); - bytes memory initData = abi.encodeWithSelector( - OraclePod.initialize.selector, - deployer, - oracleManager - ); + bytes memory initData = abi.encodeWithSelector(OraclePod.initialize.selector, deployer, oracleManager); vm.prank(deployer); ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); @@ -42,9 +38,7 @@ contract OraclePodTest is Test { // 非 oracleManager 调用失败 vm.prank(other); - vm.expectRevert( - "OraclePod.onlyOracleManager: caller is not the oracle manager address" - ); + vm.expectRevert("OraclePod.onlyOracleManager: caller is not the oracle manager address"); pod.fillSymbolPrice(price); // oracleManager 调用成功 @@ -58,9 +52,7 @@ contract OraclePodTest is Test { string memory price = "1234"; vm.prank(address(0xE5)); - vm.expectRevert( - "OraclePod.onlyOracleManager: caller is not the oracle manager address" - ); + vm.expectRevert("OraclePod.onlyOracleManager: caller is not the oracle manager address"); pod.fillSymbolPrice(price); vm.prank(oracleManager); @@ -168,20 +160,15 @@ contract OracleManagerTest is Test { Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 }); - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: pubKeyG1, - pubkeyG2: pubKeyG2, - pubkeyRegistrationSignature: signature - }); + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); // operator注册新的 pubkey vm.prank(operator); - bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( - operator, - params, - msgHash - ); + bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey(operator, params, msgHash); vm.prank(address(oracleManager)); blsRegistry.registerOperator(address(operator)); @@ -189,18 +176,14 @@ contract OracleManagerTest is Test { function test_addOrRemoveOperatorWhitelist() public { vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); oracleManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(aggregator); oracleManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(aggregator); - vm.expectRevert( - "PodManager.addOperatorWhitelist: operator address is zero" - ); + vm.expectRevert("PodManager.addOperatorWhitelist: operator address is zero"); oracleManager.addOrRemoveOperatorWhitelist(address(0), true); } @@ -213,23 +196,17 @@ contract OracleManagerTest is Test { oracleManager.setAggregatorAddress(aggregator); vm.prank(owner); - vm.expectRevert( - "PodManager.addAggregator: aggregatorAddress address is zero" - ); + vm.expectRevert("PodManager.addAggregator: aggregatorAddress address is zero"); oracleManager.setAggregatorAddress(address(0)); } function test_addOrRemoveOraclePodToFillWhitelist() public { vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); oracleManager.addPodToFillWhitelist(address(oraclePod)); vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); oracleManager.removePodToFillWhitelist(address(oraclePod)); vm.prank(aggregator); @@ -243,21 +220,15 @@ contract OracleManagerTest is Test { oracleManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); oracleManager.registerOperator("http://node.url"); vm.prank(operator); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: Operator have already register" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: Operator have already register"); oracleManager.registerOperator("http://node.url"); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); oracleManager.deRegisterOperator(); vm.prank(operator); @@ -267,9 +238,7 @@ contract OracleManagerTest is Test { oracleManager.addOrRemoveOperatorWhitelist(operator, false); vm.prank(operator); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); oracleManager.registerOperator("http://node.url"); } @@ -277,26 +246,24 @@ contract OracleManagerTest is Test { vm.prank(aggregator); oracleManager.addPodToFillWhitelist(address(oraclePod)); - IBLSApkRegistry.NonSignerAndSignature - memory noSignerAndSignature = IBLSApkRegistry - .NonSignerAndSignature({ - nonSignerPubkeys: new BN254.G1Point[](0), - apkG2: BN254.G2Point({ - X: [ - 6814450613988925037276906495559354220267038225890288520888556922179861427221, - 11097154366204527428819849175191533397314611771099148982308553889852330000313 - ], - Y: [ - 20799884507081215979545766399242808376431798816319714422985505673585902041706, - 13670248609089265475970799020243713070902269374832615406626549692922451548915 - ] - }), - sigma: BN254.G1Point({ - X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, - Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 - }), - totalStake: 888 - }); + IBLSApkRegistry.NonSignerAndSignature memory noSignerAndSignature = IBLSApkRegistry.NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }), + sigma: BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }), + totalStake: 888 + }); IOracleManager.OracleBatch memory batch = IOracleManager.OracleBatch({ msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, blockNumber: block.number - 1, @@ -305,36 +272,30 @@ contract OracleManagerTest is Test { }); vm.prank(aggregator); - oracleManager.fillSymbolPriceWithSignature( - oraclePod, - batch, - noSignerAndSignature - ); + oracleManager.fillSymbolPriceWithSignature(oraclePod, batch, noSignerAndSignature); assertEq(oraclePod.getSymbolPrice(), "888"); } function testFillSymbolPriceWithoutWhitelistOrAuthority() public { - IBLSApkRegistry.NonSignerAndSignature - memory noSignerAndSignature = IBLSApkRegistry - .NonSignerAndSignature({ - nonSignerPubkeys: new BN254.G1Point[](0), - apkG2: BN254.G2Point({ - X: [ - 19552866287184064427995511006223057169680536518603642638640105365054342788017, - 19912786774583403697047133238687463296134677575618298225286334615015816916116 - ], - Y: [ - 2970994197396269892653525920024039859830728356246595152296683945713431676344, - 18119535013136907197909765078809655896321461883746857179927989514870514777799 - ] - }), - sigma: BN254.G1Point({ - X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, - Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 - }), - totalStake: 888 - }); + IBLSApkRegistry.NonSignerAndSignature memory noSignerAndSignature = IBLSApkRegistry.NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 19552866287184064427995511006223057169680536518603642638640105365054342788017, + 19912786774583403697047133238687463296134677575618298225286334615015816916116 + ], + Y: [ + 2970994197396269892653525920024039859830728356246595152296683945713431676344, + 18119535013136907197909765078809655896321461883746857179927989514870514777799 + ] + }), + sigma: BN254.G1Point({ + X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, + Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 + }), + totalStake: 888 + }); IOracleManager.OracleBatch memory batch = IOracleManager.OracleBatch({ msgHash: 0x3f0a377ba0a4a460ecb616f6507ce0d8cfa3e704025d4fda3ed0c5ca05468728, blockNumber: block.number - 1, @@ -343,23 +304,11 @@ contract OracleManagerTest is Test { }); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); - oracleManager.fillSymbolPriceWithSignature( - oraclePod, - batch, - noSignerAndSignature - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); + oracleManager.fillSymbolPriceWithSignature(oraclePod, batch, noSignerAndSignature); vm.prank(aggregator); - vm.expectRevert( - "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" - ); - oracleManager.fillSymbolPriceWithSignature( - oraclePod, - batch, - noSignerAndSignature - ); + vm.expectRevert("PodManager.onlyPodWhitelistedForFill: pod not whitelisted"); + oracleManager.fillSymbolPriceWithSignature(oraclePod, batch, noSignerAndSignature); } } diff --git a/test/TestVrf.t.sol b/test/TestVrf.t.sol index c029561..f374930 100644 --- a/test/TestVrf.t.sol +++ b/test/TestVrf.t.sol @@ -25,11 +25,7 @@ contract VrfPodTest is Test { vm.prank(deployer); logic = new VrfPod(); - bytes memory initData = abi.encodeWithSelector( - VrfPod.initialize.selector, - deployer, - vrfManager - ); + bytes memory initData = abi.encodeWithSelector(VrfPod.initialize.selector, deployer, vrfManager); vm.prank(deployer); ERC1967Proxy proxy = new ERC1967Proxy(address(logic), initData); @@ -51,9 +47,7 @@ contract VrfPodTest is Test { vm.prank(vrfManager); pod.fulfillRandomWords(requestId, randomWords); - (bool fulfilled, uint256[] memory words) = pod.getRandomWordsWithStatus( - requestId - ); + (bool fulfilled, uint256[] memory words) = pod.getRandomWordsWithStatus(requestId); assertEq(fulfilled, true); assertEq(words, randomWords); @@ -61,12 +55,7 @@ contract VrfPodTest is Test { function testOnlyOwnerCanSetVrfManager() public { vm.prank(vrfManager); - vm.expectRevert( - abi.encodeWithSelector( - Ownable.OwnableUnauthorizedAccount.selector, - vrfManager - ) - ); + vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, vrfManager)); pod.setVrfManager(address(0xD1)); vm.prank(deployer); @@ -77,9 +66,7 @@ contract VrfPodTest is Test { function testRequestRandomWords() public { pod.requestRandomWords(requestId, 3); - (bool fulfilled, uint256[] memory words) = pod.getRandomWordsWithStatus( - requestId - ); + (bool fulfilled, uint256[] memory words) = pod.getRandomWordsWithStatus(requestId); assertEq(fulfilled, false); assertEq(words.length, 0); @@ -152,20 +139,15 @@ contract VrfManagerTest is Test { Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 }); - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: pubKeyG1, - pubkeyG2: pubKeyG2, - pubkeyRegistrationSignature: signature - }); + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); // operator注册新的 pubkey vm.prank(operator); - bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey( - operator, - params, - msgHash - ); + bytes32 pubkeyHash = blsRegistry.registerBLSPublicKey(operator, params, msgHash); vm.prank(address(vrfManager)); blsRegistry.registerOperator(address(operator)); @@ -173,18 +155,14 @@ contract VrfManagerTest is Test { function test_addOrRemoveOperatorWhitelist() public { vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); vrfManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(aggregator); vrfManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(aggregator); - vm.expectRevert( - "PodManager.addOperatorWhitelist: operator address is zero" - ); + vm.expectRevert("PodManager.addOperatorWhitelist: operator address is zero"); vrfManager.addOrRemoveOperatorWhitelist(address(0), true); } @@ -197,23 +175,17 @@ contract VrfManagerTest is Test { vrfManager.setAggregatorAddress(aggregator); vm.prank(owner); - vm.expectRevert( - "PodManager.addAggregator: aggregatorAddress address is zero" - ); + vm.expectRevert("PodManager.addAggregator: aggregatorAddress address is zero"); vrfManager.setAggregatorAddress(address(0)); } function test_addOrRemoveVrfPodToFillWhitelist() public { vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); vrfManager.addPodToFillWhitelist(address(vrfPod)); vm.prank(address(0xE5)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); vrfManager.removePodToFillWhitelist(address(vrfPod)); vm.prank(aggregator); @@ -227,21 +199,15 @@ contract VrfManagerTest is Test { vrfManager.addOrRemoveOperatorWhitelist(operator, true); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); vrfManager.registerOperator("http://node.url"); vm.prank(operator); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: Operator have already register" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: Operator have already register"); vrfManager.registerOperator("http://node.url"); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); vrfManager.deRegisterOperator(); vm.prank(operator); @@ -251,9 +217,7 @@ contract VrfManagerTest is Test { vrfManager.addOrRemoveOperatorWhitelist(operator, false); vm.prank(operator); - vm.expectRevert( - "PodManager.registerOperator: this address have not permission to register " - ); + vm.expectRevert("PodManager.registerOperator: this address have not permission to register "); vrfManager.registerOperator("http://node.url"); } @@ -261,102 +225,79 @@ contract VrfManagerTest is Test { vm.prank(aggregator); vrfManager.addPodToFillWhitelist(address(vrfPod)); - IBLSApkRegistry.NonSignerAndSignature - memory noSignerAndSignature = IBLSApkRegistry - .NonSignerAndSignature({ - nonSignerPubkeys: new BN254.G1Point[](0), - apkG2: BN254.G2Point({ - X: [ - 6814450613988925037276906495559354220267038225890288520888556922179861427221, - 11097154366204527428819849175191533397314611771099148982308553889852330000313 - ], - Y: [ - 20799884507081215979545766399242808376431798816319714422985505673585902041706, - 13670248609089265475970799020243713070902269374832615406626549692922451548915 - ] - }), - sigma: BN254.G1Point({ - X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, - Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 - }), - totalStake: 888 - }); + IBLSApkRegistry.NonSignerAndSignature memory noSignerAndSignature = IBLSApkRegistry.NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 6814450613988925037276906495559354220267038225890288520888556922179861427221, + 11097154366204527428819849175191533397314611771099148982308553889852330000313 + ], + Y: [ + 20799884507081215979545766399242808376431798816319714422985505673585902041706, + 13670248609089265475970799020243713070902269374832615406626549692922451548915 + ] + }), + sigma: BN254.G1Point({ + X: 15194033674394012071916983731564882240605499108993224505298052923469296043512, + Y: 839159203127434969034550706910060963494405052210926279105817372573420151443 + }), + totalStake: 888 + }); uint256[] memory arr = new uint256[](1); arr[0] = 43; - IVrfManager.VrfRandomWords memory randomWords = IVrfManager - .VrfRandomWords({ - msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - blockNumber: block.number - 1, - requestId: 888, - blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - _randomWords: arr - }); + IVrfManager.VrfRandomWords memory randomWords = IVrfManager.VrfRandomWords({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + _randomWords: arr + }); vm.prank(aggregator); - vrfManager.fillRandWordsWithSignature( - vrfPod, - randomWords, - noSignerAndSignature - ); - (bool fulfilled, uint256[] memory words) = vrfPod - .getRandomWordsWithStatus(888); + vrfManager.fillRandWordsWithSignature(vrfPod, randomWords, noSignerAndSignature); + (bool fulfilled, uint256[] memory words) = vrfPod.getRandomWordsWithStatus(888); assertEq(fulfilled, true); assertEq(words, arr); } function testFillSymbolPriceWithoutWhitelistOrAuthority() public { - IBLSApkRegistry.NonSignerAndSignature - memory noSignerAndSignature = IBLSApkRegistry - .NonSignerAndSignature({ - nonSignerPubkeys: new BN254.G1Point[](0), - apkG2: BN254.G2Point({ - X: [ - 19552866287184064427995511006223057169680536518603642638640105365054342788017, - 19912786774583403697047133238687463296134677575618298225286334615015816916116 - ], - Y: [ - 2970994197396269892653525920024039859830728356246595152296683945713431676344, - 18119535013136907197909765078809655896321461883746857179927989514870514777799 - ] - }), - sigma: BN254.G1Point({ - X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, - Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 - }), - totalStake: 888 - }); + IBLSApkRegistry.NonSignerAndSignature memory noSignerAndSignature = IBLSApkRegistry.NonSignerAndSignature({ + nonSignerPubkeys: new BN254.G1Point[](0), + apkG2: BN254.G2Point({ + X: [ + 19552866287184064427995511006223057169680536518603642638640105365054342788017, + 19912786774583403697047133238687463296134677575618298225286334615015816916116 + ], + Y: [ + 2970994197396269892653525920024039859830728356246595152296683945713431676344, + 18119535013136907197909765078809655896321461883746857179927989514870514777799 + ] + }), + sigma: BN254.G1Point({ + X: 15723530600246276940894768360396890326319571568844052976858037242805072605559, + Y: 11650315804718231422577338154702931145725917843701074925949828011449296498014 + }), + totalStake: 888 + }); uint256[] memory arr = new uint256[](1); arr[0] = 43; - IVrfManager.VrfRandomWords memory randomWords = IVrfManager - .VrfRandomWords({ - msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - blockNumber: block.number - 1, - requestId: 888, - blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, - _randomWords: arr - }); + IVrfManager.VrfRandomWords memory randomWords = IVrfManager.VrfRandomWords({ + msgHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + blockNumber: block.number - 1, + requestId: 888, + blockHash: 0xea83cdcdd06bf61e414054115a551e23133711d0507dcbc07a4bab7dc4581935, + _randomWords: arr + }); vm.prank(address(0xE1)); - vm.expectRevert( - "PodManager.onlyAggregatorManager: not the aggregator address" - ); - vrfManager.fillRandWordsWithSignature( - vrfPod, - randomWords, - noSignerAndSignature - ); + vm.expectRevert("PodManager.onlyAggregatorManager: not the aggregator address"); + vrfManager.fillRandWordsWithSignature(vrfPod, randomWords, noSignerAndSignature); vm.prank(aggregator); - vm.expectRevert( - "PodManager.onlyPodWhitelistedForFill: pod not whitelisted" - ); - vrfManager.fillRandWordsWithSignature( - vrfPod, - randomWords, - noSignerAndSignature - ); + vm.expectRevert("PodManager.onlyPodWhitelistedForFill: pod not whitelisted"); + vrfManager.fillRandWordsWithSignature(vrfPod, randomWords, noSignerAndSignature); } } diff --git a/test/Test_BLSApkRegistry.t.sol b/test/Test_BLSApkRegistry.t.sol index 1ec86a9..0bb1f1a 100644 --- a/test/Test_BLSApkRegistry.t.sol +++ b/test/Test_BLSApkRegistry.t.sol @@ -17,12 +17,8 @@ contract BLSApkRegistryTest is Test { function setUp() public { BLSApkRegistry logic = new BLSApkRegistry(); - bytes memory data = abi.encodeWithSelector( - BLSApkRegistry.initialize.selector, - owner, - whiteListManager, - oracleManager - ); + bytes memory data = + abi.encodeWithSelector(BLSApkRegistry.initialize.selector, owner, whiteListManager, oracleManager); proxy = new ERC1967Proxy(address(logic), data); registry = BLSApkRegistry(address(proxy)); @@ -37,9 +33,7 @@ contract BLSApkRegistryTest is Test { // 测试 onlyWhiteListManager vm.prank(someAddr); - vm.expectRevert( - "BLSApkRegistry.onlyWhiteListManager: caller is not white list address" - ); + vm.expectRevert("BLSApkRegistry.onlyWhiteListManager: caller is not white list address"); registry.addOrRemoveBlsRegisterWhitelist(address(0xDEAD), true); } @@ -71,58 +65,43 @@ contract BLSApkRegistryTest is Test { Y: 13194418538414467055050033907265106142712607841859396823411150580302706299760 }); - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: pubKeyG1, - pubkeyG2: pubKeyG2, - pubkeyRegistrationSignature: signature - }); + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); // 测试注册新的 pubkey 是否成功 vm.prank(blsRegister); - bytes32 pubkeyHash = registry.registerBLSPublicKey( - blsRegister, - params, - msgHash - ); + bytes32 pubkeyHash = registry.registerBLSPublicKey(blsRegister, params, msgHash); bytes32 readHash = registry.getPubkeyHash(blsRegister); assertEq(pubkeyHash, readHash); // 测试同一个 operator 不能重复注册 pubkey vm.prank(blsRegister); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey"); registry.registerBLSPublicKey(blsRegister, params, msgHash); } function testCannotRegisterWithZeroPubkey() public { BN254.G1Point memory zeroG1 = BN254.G1Point(0, 0); - BN254.G2Point memory pubKeyG2 = BN254.G2Point( - [uint256(1), 1], - [uint256(1), 1] - ); - - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: zeroG1, - pubkeyG2: pubKeyG2, - pubkeyRegistrationSignature: zeroG1 // 乱填,为了过编译 - }); + BN254.G2Point memory pubKeyG2 = BN254.G2Point([uint256(1), 1], [uint256(1), 1]); + + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: zeroG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: zeroG1 // 乱填,为了过编译 + }); BN254.G1Point memory dummyMsgHash = BN254.G1Point(1, 2); // dummy vm.prank(blsRegister); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: cannot register zero pubkey"); registry.registerBLSPublicKey(blsRegister, params, dummyMsgHash); vm.prank(blsRegister); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: this caller is not operator" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: this caller is not operator"); registry.registerBLSPublicKey(address(0xEdc), params, dummyMsgHash); } @@ -132,9 +111,7 @@ contract BLSApkRegistryTest is Test { // 非 oracleManager 调用 registerOperator vm.prank(address(0x111)); - vm.expectRevert( - "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" - ); + vm.expectRevert("BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address"); registry.registerOperator(blsRegister); // 正常调用 @@ -143,9 +120,7 @@ contract BLSApkRegistryTest is Test { // 非 oracleManager 调用 deregisterOperator vm.prank(address(0x111)); - vm.expectRevert( - "BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address" - ); + vm.expectRevert("BLSApkRegistry.onlyOracleManager: caller is not the oracle manager address"); registry.deregisterOperator(address(0xBEE)); // 正常调用 @@ -154,9 +129,7 @@ contract BLSApkRegistryTest is Test { } function testGetPubkeyRegMessageHash() public view { - BN254.G1Point memory h = registry.getPubkeyRegMessageHash( - address(0xBEE) - ); + BN254.G1Point memory h = registry.getPubkeyRegMessageHash(address(0xBEE)); assertTrue(h.X != 0 && h.Y != 0, "Message hash should not be zero"); } @@ -170,28 +143,20 @@ contract BLSApkRegistryTest is Test { registry.addOrRemoveBlsRegisterWhitelist(tester, false); BN254.G1Point memory dummy = BN254.G1Point(1, 2); - BN254.G2Point memory dummyG2 = BN254.G2Point( - [uint256(11), 21], - [uint256(11), 41] - ); - - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: dummy, - pubkeyG2: dummyG2, - pubkeyRegistrationSignature: dummy - }); + BN254.G2Point memory dummyG2 = BN254.G2Point([uint256(11), 21], [uint256(11), 41]); + + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: dummy, + pubkeyG2: dummyG2, + pubkeyRegistrationSignature: dummy + }); vm.prank(tester); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: this caller is not operator" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: this caller is not operator"); registry.registerBLSPublicKey(address(0xDDD), params, dummy); vm.prank(tester); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key"); registry.registerBLSPublicKey(tester, params, dummy); } @@ -220,13 +185,8 @@ contract BLSApkRegistryTest is Test { }); // 4. 调用验证函数 - (bool pairingSuccessful, bool sigValid) = registry - .trySignatureAndApkVerification( - msgHash, - pubKeyG1, - pubKeyG2, - signature - ); + (bool pairingSuccessful, bool sigValid) = + registry.trySignatureAndApkVerification(msgHash, pubKeyG1, pubKeyG2, signature); // 5. 断言 assertTrue(pairingSuccessful, "Pairing failed"); @@ -261,40 +221,27 @@ contract BLSApkRegistryTest is Test { Y: 18512095983377956955654133313299197583137445769983185530805027107069225976299 }); - IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry - .PubkeyRegistrationParams({ - pubkeyG1: pubKeyG1, - pubkeyG2: pubKeyG2, - pubkeyRegistrationSignature: signature - }); + IBLSApkRegistry.PubkeyRegistrationParams memory params = IBLSApkRegistry.PubkeyRegistrationParams({ + pubkeyG1: pubKeyG1, + pubkeyG2: pubKeyG2, + pubkeyRegistrationSignature: signature + }); // 测试白名单限制 vm.prank(address(0xA5)); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key" - ); - bytes32 pubkeyHash1 = registry.registerBLSPublicKey( - address(0xA5), - params, - msgHash - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: this address have not permission to register bls key"); + bytes32 pubkeyHash1 = registry.registerBLSPublicKey(address(0xA5), params, msgHash); // 测试注册新的 pubkey 是否成功 vm.prank(blsRegister); - bytes32 pubkeyHash = registry.registerBLSPublicKey( - blsRegister, - params, - msgHash - ); + bytes32 pubkeyHash = registry.registerBLSPublicKey(blsRegister, params, msgHash); bytes32 readHash = registry.getPubkeyHash(blsRegister); assertEq(pubkeyHash, readHash); // 测试同一个 operator 不能重复注册 pubkey vm.prank(blsRegister); - vm.expectRevert( - "BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey" - ); + vm.expectRevert("BLSApkRegistry.registerBLSPublicKey: operator already registered pubkey"); registry.registerBLSPublicKey(blsRegister, params, msgHash); } } From 49142f0b965e72c735745ea14d782e065719e2f0 Mon Sep 17 00:00:00 2001 From: kanthub <564080210@qq.com> Date: Fri, 10 Oct 2025 11:23:22 +0800 Subject: [PATCH 3/3] ci: use stable foundry toolchain --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 762a296..7fbe76f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,7 +23,7 @@ jobs: - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 with: - version: nightly + version: stable - name: Show Forge version run: |