// SPDX-License-Identifier: BUSL-1.1 // Copyright (c) 2026 Liquidbox Corp. // Licensed under the Business Source License 1.1. See LICENSE.md. // Terms of use: https://www.inco.org/terms-of-services // Security contact team@inco.network pragma solidity ^0.8.29; import {Test} from "forge-std/Test.sol"; import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; import {IncoLightning} from "../IncoLightning.sol"; import {IncoVerifier} from "../IncoVerifier.sol"; import {IIncoVerifier} from "../interfaces/IIncoVerifier.sol"; import {IQuoteVerifier} from "../interfaces/automata-interfaces/IQuoteVerifier.sol"; contract VerificationSnapshotVerifier is IncoVerifier { constructor(address lightningAddress) IncoVerifier(lightningAddress) {} function exposedAddSigner(address signer) external onlyOwner { addSigner(signer); } } contract TestVerificationSnapshot is Test { address internal constant ALICE = address(0x11); address internal constant BOB = address(0x22); address internal constant CAROL = address(0x33); address internal constant DAVE = address(0x44); IQuoteVerifier internal constant QUOTE_VERIFIER = IQuoteVerifier(address(0x100)); VerificationSnapshotVerifier internal verifier; IncoLightning internal lightning; function setUp() public { vm.mockCall(address(QUOTE_VERIFIER), abi.encodeCall(IQuoteVerifier.quoteVersion, ()), abi.encode(uint16(4))); // The verifier implementation, verifier proxy and Lightning implementation deploy before this proxy. address expectedLightning = vm.computeCreateAddress(address(this), vm.getNonce(address(this)) + 3); verifier = _deployVerifier(expectedLightning, "IncoVerifier", "1"); IncoLightning implementation = new IncoLightning(bytes32(0), IIncoVerifier(address(verifier))); lightning = IncoLightning( payable(address( new ERC1967Proxy(address(implementation), abi.encodeCall(IncoLightning.initialize, (address(this)))) )) ); assertEq(address(lightning), expectedLightning); } function testEmptySnapshotIsReadableFromBothProxies() public view { IIncoVerifier.VerificationSnapshot memory snapshot = lightning.getVerificationSnapshot(); assertEq(snapshot.threshold, 0); assertEq(snapshot.signers.length, 0); assertFalse(snapshot.paused); _assertMatchesVerifier(snapshot, verifier); } function testConfiguredSnapshotMatchesExistingVerifierReads() public { _configureSigners(); IIncoVerifier.VerificationSnapshot memory snapshot = lightning.getVerificationSnapshot(); assertEq(snapshot.domain.name, "IncoVerifier"); assertEq(snapshot.domain.version, "1"); assertEq(snapshot.threshold, 2); assertEq(snapshot.signers.length, 3); assertEq(snapshot.signers[0], ALICE); assertEq(snapshot.signers[1], BOB); assertEq(snapshot.signers[2], CAROL); _assertMatchesVerifier(snapshot, verifier); } function testSnapshotReflectsSignerAndThresholdChanges() public { _configureSigners(); verifier.exposedAddSigner(DAVE); verifier.setThreshold(3); verifier.removeSigner(BOB); IIncoVerifier.VerificationSnapshot memory snapshot = lightning.getVerificationSnapshot(); assertEq(snapshot.threshold, 3); assertEq(snapshot.signers.length, 3); assertEq(snapshot.signers[0], ALICE); assertEq(snapshot.signers[1], DAVE); assertEq(snapshot.signers[2], CAROL); assertFalse(verifier.isSigner(BOB)); _assertMatchesVerifier(snapshot, verifier); verifier.setThreshold(1); verifier.removeSigner(ALICE); snapshot = lightning.getVerificationSnapshot(); assertEq(snapshot.threshold, 1); assertEq(snapshot.signers.length, 2); assertEq(snapshot.signers[0], CAROL); assertEq(snapshot.signers[1], DAVE); _assertMatchesVerifier(snapshot, verifier); } function testSnapshotRemainsReadableWhileBothContractsArePaused() public { _configureSigners(); verifier.pause(); lightning.pause(); IIncoVerifier.VerificationSnapshot memory snapshot = lightning.getVerificationSnapshot(); assertTrue(snapshot.paused); _assertMatchesVerifier(snapshot, verifier); verifier.unpause(); snapshot = lightning.getVerificationSnapshot(); assertFalse(snapshot.paused); assertTrue(lightning.paused()); _assertMatchesVerifier(snapshot, verifier); } function testSnapshotUsesCurrentBlockAndChainId() public { vm.roll(100); vm.chainId(8453); IIncoVerifier.VerificationSnapshot memory snapshot = lightning.getVerificationSnapshot(); assertEq(snapshot.blockNumber, 100); assertEq(snapshot.domain.chainId, 8453); _assertMatchesVerifier(snapshot, verifier); vm.roll(101); vm.chainId(31337); snapshot = lightning.getVerificationSnapshot(); assertEq(snapshot.blockNumber, 101); assertEq(snapshot.domain.chainId, 31337); _assertMatchesVerifier(snapshot, verifier); } function testLightningUpgradeForwardsToTheNewVerifier() public { _configureSigners(); VerificationSnapshotVerifier replacement = _deployVerifier(address(lightning), "ReplacementVerifier", "2"); replacement.exposedAddSigner(DAVE); replacement.setThreshold(1); replacement.pause(); IncoLightning implementation = new IncoLightning(bytes32(uint256(1)), IIncoVerifier(address(replacement))); lightning.upgradeToAndCall(address(implementation), ""); IIncoVerifier.VerificationSnapshot memory snapshot = lightning.getVerificationSnapshot(); assertEq(address(lightning.incoVerifier()), address(replacement)); assertEq(snapshot.domain.name, "ReplacementVerifier"); assertEq(snapshot.domain.version, "2"); assertEq(snapshot.threshold, 1); assertEq(snapshot.signers.length, 1); assertEq(snapshot.signers[0], DAVE); assertTrue(snapshot.paused); _assertMatchesVerifier(snapshot, replacement); assertEq(verifier.getVerificationSnapshot().threshold, 2); } function _configureSigners() internal { verifier.exposedAddSigner(ALICE); verifier.exposedAddSigner(BOB); verifier.exposedAddSigner(CAROL); verifier.setThreshold(2); } function _deployVerifier(address lightningAddress, string memory name, string memory version) internal returns (VerificationSnapshotVerifier) { VerificationSnapshotVerifier implementation = new VerificationSnapshotVerifier(lightningAddress); return VerificationSnapshotVerifier( address( new ERC1967Proxy( address(implementation), abi.encodeCall(IncoVerifier.initialize, (address(this), name, version, QUOTE_VERIFIER)) ) ) ); } function _assertMatchesVerifier( IIncoVerifier.VerificationSnapshot memory snapshot, VerificationSnapshotVerifier expectedVerifier ) internal view { (, string memory name, string memory version, uint256 chainId, address verifyingContract,,) = expectedVerifier.eip712Domain(); assertEq(snapshot.blockNumber, block.number); assertEq(snapshot.verifierAddress, address(expectedVerifier)); assertEq(snapshot.domain.name, name); assertEq(snapshot.domain.version, version); assertEq(snapshot.domain.chainId, chainId); assertEq(snapshot.domain.verifyingContract, verifyingContract); assertEq(snapshot.domain.verifyingContract, address(expectedVerifier)); assertEq(snapshot.threshold, expectedVerifier.getThreshold()); assertEq(snapshot.paused, expectedVerifier.paused()); assertEq(snapshot.signers.length, expectedVerifier.getSignersCount()); for (uint256 i; i < snapshot.signers.length; ++i) { assertEq(snapshot.signers[i], expectedVerifier.getSignerAtIndex(i)); assertTrue(expectedVerifier.isSigner(snapshot.signers[i])); } assertEq(abi.encode(snapshot), abi.encode(expectedVerifier.getVerificationSnapshot())); } }