diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6a7144a..eaa7932 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -10,12 +10,12 @@ jobs: tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 with: submodules: recursive - name: Install Foundry - uses: foundry-rs/foundry-toolchain@c7450ba673e133f5ee30098b3b54f444d3a2ca2d + uses: foundry-rs/foundry-toolchain@dba66c27052e63476d886f22216e119ba7e615bf # v1.9.2 with: version: nightly diff --git a/.github/workflows/non-via-ir-compilation.yml b/.github/workflows/non-via-ir-compilation.yml index 943912d..a7139cd 100644 --- a/.github/workflows/non-via-ir-compilation.yml +++ b/.github/workflows/non-via-ir-compilation.yml @@ -10,13 +10,13 @@ jobs: tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 with: submodules: recursive - run: yarn install - name: Install Foundry - uses: foundry-rs/foundry-toolchain@c7450ba673e133f5ee30098b3b54f444d3a2ca2d + uses: foundry-rs/foundry-toolchain@dba66c27052e63476d886f22216e119ba7e615bf # v1.9.2 with: version: nightly diff --git a/.github/workflows/solc-version-tests.yml b/.github/workflows/solc-version-tests.yml index 1fd07d8..7056ab6 100644 --- a/.github/workflows/solc-version-tests.yml +++ b/.github/workflows/solc-version-tests.yml @@ -10,13 +10,13 @@ jobs: tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 with: submodules: recursive - run: yarn install - name: Install Foundry - uses: foundry-rs/foundry-toolchain@c7450ba673e133f5ee30098b3b54f444d3a2ca2d + uses: foundry-rs/foundry-toolchain@dba66c27052e63476d886f22216e119ba7e615bf # v1.9.2 with: version: nightly diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 47ce15b..26caba7 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -10,13 +10,13 @@ jobs: tests: runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 with: submodules: recursive - run: yarn install - name: Install Foundry - uses: foundry-rs/foundry-toolchain@c7450ba673e133f5ee30098b3b54f444d3a2ca2d + uses: foundry-rs/foundry-toolchain@dba66c27052e63476d886f22216e119ba7e615bf # v1.9.2 with: version: nightly diff --git a/docs/Management.md b/docs/Management.md index 24cafe0..a445e2b 100644 --- a/docs/Management.md +++ b/docs/Management.md @@ -21,6 +21,7 @@ This document describes how to manage deployed `Scribe` and `ScribeOptimistic` i - [`IToll::diss`](#itolldiss) - [Offboarding](#offboarding) - [Offboard via `ScribeOffboarder`](#offboard-via-scribeoffboarder) + - [Offboard via `ScribeOptimisticOffboarder`](#offboard-via-scribeoptimisticoffboarder) - [Deactivation](#deactivation) - [Fund Rescue](#fund-resuce) - [Killing](#killing) @@ -287,15 +288,20 @@ Offboarding a Scribe(Optimistic) instance simply means _Chronicle Protocol_ is n However, to ensure an offboarded Scribe(Optimistic) instance may not behave unexpectedly it needs to be deactivated. Furthermore, if the contract is a ScribeOptimistic instance ETH held by the contract may need to be rescued. If its certain the contract will never be used again it is recommended to kill it. -A Scribe(Optimistic) instance can be deactivated in two ways: +A Scribe(Optimistic) instance can be deactivated in three ways: -- In a single transaction via a deployed [`ScribeOffboarder`](#offboard-via-scribeoffboarder) contract that holds `auth` on the instance. +- In a single transaction via a deployed [`ScribeOffboarder`](#offboard-via-scribeoffboarder) contract that holds `auth` on the instance. Note that this is only supported for Scribe instances. +- In two transactions via a deployed [`ScribeOptimisticOffboarder`](#offboard-via-scribeoptimisticoffboarder) contract that holds `auth` on the instance. Note that this is only supported for ScribeOptimistic instances. - Via two distinct `forge script` [deactivation](#deactivation) commands signed by an `auth`'ed EOA. ### Offboard via `ScribeOffboarder` Offboarding through a `ScribeOffboarder` instance drops every currently-lifted feed, sets `bar` to `1`, and pokes value `0` in a single transaction. After the call, `read()` reverts and no feed is lifted, so no future poke can verify. +> [!IMPORTANT] +> +> `ScribeOffboarder` only supports Scribe instances. ScribeOptimistic instances set their value's age to the current timestamp on every `drop` and `setBar`, leading the value `0` poke in the same transaction to revert. Use [`ScribeOptimisticOffboarder`](#offboard-via-scribeoptimisticoffboarder) instead. + Expects: - `OFFBOARDER` is a deployed `ScribeOffboarder` instance that holds `auth` on `SCRIBE`. @@ -320,6 +326,57 @@ $ forge script \ Note that `auth`'ed addresses on `SCRIBE` are untouched by `offboard` — to make the deactivation permanent, follow up with [`kill`](#killing). +### Offboard via `ScribeOptimisticOffboarder` + +Offboarding through a `ScribeOptimisticOffboarder` instance drops every currently-lifted feed and sets `bar` to `1` in a first transaction, and pokes value `0` in a second transaction. After the second call, `read()` reverts and no feed is lifted, so no future poke can verify. + +Note that no feed is lifted between both transactions, so no poke or opPoke can verify. However, between both transactions the instance still provides its last value with the first transaction's timestamp as age. Therefore, the second transaction should be executed in the next block. + +> [!IMPORTANT] +> +> Offboarding requires running two distinct `forge script` commands, with step 2 being executed in a later block than step 1. +> +> It is of utmost importance to run both commands and NOT leave the ScribeOptimistic instance in an undefined state. + +Expects: + +- `OFFBOARDER` is a deployed `ScribeOptimisticOffboarder` instance that holds `auth` on `SCRIBE`. +- The caller holds `auth` on `OFFBOARDER`. + +Set the additional environment variable: + +- `OFFBOARDER`: The `ScribeOptimisticOffboarder` instance to offboard `SCRIBE` with. + +Step 1: + +```bash +$ forge script \ + --keystore "$KEYSTORE" \ + --password "$KEYSTORE_PASSWORD" \ + --broadcast \ + --rpc-url "$RPC_URL" \ + --sig $(cast calldata "offboard_Step1(address,address)" "$OFFBOARDER" "$SCRIBE") \ + -vvv \ + script/ScribeOptimistic.s.sol:ScribeOptimisticScript +``` + +Step 2: + +```bash +$ forge script \ + --keystore "$KEYSTORE" \ + --password "$KEYSTORE_PASSWORD" \ + --broadcast \ + --rpc-url "$RPC_URL" \ + --sig $(cast calldata "offboard_Step2(address,address)" "$OFFBOARDER" "$SCRIBE") \ + -vvv \ + script/ScribeOptimistic.s.sol:ScribeOptimisticScript +``` + +Note that ETH held by `SCRIBE` is not rescued by offboarding — follow up with [fund rescue](#fund-rescue). + +Note that `auth`'ed addresses on `SCRIBE` are untouched by `offboard_Step2` — to make the deactivation permanent, follow up with [`kill`](#killing). + ### Deactivation Deactivating a Scribe(Optimistic) instance means its value is set to zero, leading all `read()` calls to revert/fail, no feeds are lifted, and `bar` is set to `255`. diff --git a/foundry.toml b/foundry.toml index fb7136b..0f96259 100644 --- a/foundry.toml +++ b/foundry.toml @@ -50,7 +50,3 @@ runs = 100 runs = 100 # See more config options https://github.com/foundry-rs/foundry/tree/master/config - - -[profile.default.zksync] -zksolc = "1.5.1" diff --git a/script/Scribe.s.sol b/script/Scribe.s.sol index e328cfb..5c24a6c 100644 --- a/script/Scribe.s.sol +++ b/script/Scribe.s.sol @@ -284,7 +284,7 @@ contract ScribeScript is Script { /// /// @dev Note that `auth`'ed addresses on `scribe` are untouched. To make /// the deactivation permanent, follow up with `kill(scribe)`. - function offboard(address offboarder, address scribe) public { + function offboard(address offboarder, address scribe) public virtual { vm.startBroadcast(); ScribeOffboarder(offboarder).offboard(scribe); vm.stopBroadcast(); diff --git a/script/ScribeOptimistic.s.sol b/script/ScribeOptimistic.s.sol index 8a62fcd..2eecea4 100644 --- a/script/ScribeOptimistic.s.sol +++ b/script/ScribeOptimistic.s.sol @@ -21,6 +21,10 @@ import {LibFeed} from "./libs/LibFeed.sol"; import {Rescuer} from "./rescue/Rescuer.sol"; +import { + ScribeOptimisticOffboarder +} from "./offboard/ScribeOptimisticOffboarder.sol"; + /** * @title ScribeOptimistic Management Script */ @@ -118,6 +122,51 @@ contract ScribeOptimisticScript is ScribeScript { vm.stopBroadcast(); } + // -- Offboarding Functions -- + + /// @dev Note that `ScribeOffboarder` cannot offboard a ScribeOptimistic + /// instance. Use `offboard_Step1` and `offboard_Step2` instead. + function offboard(address, address) public pure override(ScribeScript) { + revert("Use offboard_Step1 and offboard_Step2"); + } + + /// @dev Step 1 of offboarding instance `scribe` via + /// `ScribeOptimisticOffboarder` instance `offboarder`. + /// + /// @dev Step 1 drops every currently-lifted feed and sets bar to 1. + /// + /// @dev Expects: + /// - `offboarder` to hold `auth` on `scribe` + /// - `msg.sender` to hold `auth` on `offboarder` + /// + /// @dev Note that offboarding MUST be executed via two distinct + /// `forge script` commands with step 2 being executed in a later + /// block than step 1. + function offboard_Step1(address offboarder, address scribe) public { + vm.startBroadcast(); + ScribeOptimisticOffboarder(offboarder).offboard_Step1(scribe); + vm.stopBroadcast(); + + console2.log("Offboarded Step 1", scribe); + } + + /// @dev Step 2 of offboarding instance `scribe` via + /// `ScribeOptimisticOffboarder` instance `offboarder`. + /// + /// @dev Step 2 pokes value 0, sets bar to type(uint8).max, and denies + /// `offboarder` on `scribe`. After the call `scribe.read()` reverts + /// and no feed is lifted, so no future poke can verify. + /// + /// @dev Note that `auth`'ed addresses on `scribe` are untouched. To make + /// the deactivation permanent, follow up with `kill(scribe)`. + function offboard_Step2(address offboarder, address scribe) public { + vm.startBroadcast(); + ScribeOptimisticOffboarder(offboarder).offboard_Step2(scribe); + vm.stopBroadcast(); + + console2.log("Offboarded Step 2", scribe); + } + /// @dev Rescues ETH held in deactivated `self`. /// /// @dev Call via: diff --git a/script/offboard/ScribeOffboarder.sol b/script/offboard/ScribeOffboarder.sol index 48eced5..b4c2814 100644 --- a/script/offboard/ScribeOffboarder.sol +++ b/script/offboard/ScribeOffboarder.sol @@ -139,6 +139,7 @@ contract ScribeOffboarder is Auth { }) ); + // Drop offboarder again. target.drop(FEED_ID); // Set bar to max. diff --git a/script/offboard/ScribeOptimisticOffboarder.sol b/script/offboard/ScribeOptimisticOffboarder.sol new file mode 100644 index 0000000..4704c91 --- /dev/null +++ b/script/offboard/ScribeOptimisticOffboarder.sol @@ -0,0 +1,248 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.16; + +import {Auth} from "chronicle-std/auth/Auth.sol"; +import {IAuth} from "chronicle-std/auth/IAuth.sol"; + +import {IScribe} from "../../src/IScribe.sol"; + +import {LibSecp256k1} from "../../src/libs/LibSecp256k1.sol"; + +/** + * @title ScribeOptimisticOffboarder + * + * @notice Permanently disables a `ScribeOptimistic` oracle in two + * transactions. The contract must be granted `auth` on the target + * scribe. + * + * `offboard_Step1(scribe)` enumerates every currently-lifted feed, + * drops them, and sets bar = 1. + * + * `offboard_Step2(scribe)` lifts the hardcoded offboarder feed, pokes + * val = 0, drops the offboarder feed, and raises bar = 255. + * + * @dev Offboarding MUST be executed via two distinct transactions with + * `offboard_Step2` being executed at a later timestamp than + * `offboard_Step1`. + * + * Every `drop` and `setBar` on a `ScribeOptimistic` sets the age of the + * contract's current value to block.timestamp. As a poke's age must be + * greater than the current value's age, a poke at the same timestamp as + * the `setBar(1)` call reverts with `StaleMessage`. Note that `lift` + * does not update the current value's age. + * + * @dev Note that between `offboard_Step1` and `offboard_Step2` the scribe + * provides its last value with an age of `offboard_Step1`'s timestamp. + * Therefore, `offboard_Step2` should be executed in the next block. + * + * @dev Security note: the offboarder feed's private key is hardcoded as a + * `constant` in the bytecode and is therefore publicly readable. This + * is intentional; the feed is only lifted inside an `offboard_Step2` + * call and dropped again before the transaction returns, so any + * signatures craftable from the leaked key require the attacker to + * already hold `auth` on the scribe and re-lift the feed (at which point + * they could poke arbitrary values directly anyway). + * + * Note that the offboarder feed is intentionally not lifted during + * `offboard_Step1`. Otherwise, anyone could poke arbitrary values via + * the leaked key between the two transactions as bar = 1. + * + * @dev Note that ETH held by the scribe is not rescued. Use the `Rescuer` + * after offboarding. + */ +contract ScribeOptimisticOffboarder is Auth { + // ----------------------------------------------------------------------------- + // Hardcoded Offboarder Feed + // + // Note that the offboarder feed is the same as `ScribeOffboarder`'s. + // + // All four values below are derived from a single string seed: + // + // SEED = "Chronicle.ScribeOffboarder.v1" + // FEED_PRIV_KEY = uint(keccak256(SEED)) mod Q + // pubKey = vm.createWallet(FEED_PRIV_KEY).{publicKeyX, publicKeyY} + // ECDSA(v,r,s) = vm.sign(FEED_PRIV_KEY, FEED_REGISTRATION_MESSAGE) + // + // where Q is the order of secp256k1 and + // + // FEED_REGISTRATION_MESSAGE = + // keccak256( + // "\x19Ethereum Signed Message:\n32" ‖ + // keccak256("Chronicle Feed Registration") + // ) + // + // FEED_REGISTRATION_MESSAGE is a Scribe protocol constant (see + // `Scribe.feedRegistrationMessage`), so the ECDSA signature below is + // valid against every Scribe deployment. + + /// @dev `keccak256("Chronicle.ScribeOffboarder.v1") mod Q`. + uint private constant FEED_PRIV_KEY = + 0xf067ac0350b6ab5e39cf9138ae16508404c05af8b58676e07ae14e9a510a600e; + + uint private constant FEED_PUBKEY_X = + 0xc2f3bfebb984817bbcaaf2b8e185259b09493f6db4b2c30d7b4feca89f7a64b2; + + uint private constant FEED_PUBKEY_Y = + 0x53a0dc54c1d22a62fbb1b3724737486c7e61c26080a44817eef7f5992a521a40; + + /// @notice Feed id is the highest-order byte of the ethereum address + /// derived from the offboarder feed's public key + /// (= 0x03da1Db2b7875106395548682B068a7C7b296902 => id = 0x03). + uint8 private constant FEED_ID = 0x03; + + // ECDSA signature over FEED_REGISTRATION_MESSAGE by FEED_PRIV_KEY. + uint8 private constant LIFT_V = 28; + bytes32 private constant LIFT_R = + 0xc9988947c96dd6899cc097e3979b1e4d9228e09b4bc7cf53a9e0fe2ff395a77c; + bytes32 private constant LIFT_S = + 0x76ec989c8765878f18daceb7893ea2c24b1a87401c847e240a1301c3565f8dd7; + + // ----------------------------------------------------------------------------- + // secp256k1 Constants + + /// @dev x-coordinate of the secp256k1 generator G. + uint private constant G_X = + 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; + + /// @dev Order of the secp256k1 group. + uint private constant Q = + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141; + + /// @dev `v` value corresponding to G (G_y is even, so parity = 0). + uint8 private constant G_V = 27; + + // ----------------------------------------------------------------------------- + // Events + + event OffboardedStep1(address indexed caller, address indexed scribe); + event OffboardedStep2(address indexed caller, address indexed scribe); + + // ----------------------------------------------------------------------------- + // Constructor + + constructor(address initialAuthed) Auth(initialAuthed) {} + + // ----------------------------------------------------------------------------- + // Offboard + + /// @notice Step 1 of offboarding `scribe`: enumerates the + /// currently-lifted feeds, drops them, and sets bar = 1. + /// + /// @dev Note that the offboarder feed is intentionally not lifted. + function offboard_Step1(address scribe) external auth { + require(IAuth(scribe).authed(address(this))); + + IScribe target = IScribe(scribe); + + // Drop every currently-lifted feed. + address[] memory currentFeeds = target.feeds(); + if (currentFeeds.length != 0) { + uint8[] memory ids = new uint8[](currentFeeds.length); + for (uint i; i < currentFeeds.length; i++) { + ids[i] = uint8(uint(uint160(currentFeeds[i])) >> 152); + } + target.drop(ids); + } + + // Set bar to one. + target.setBar(1); + + emit OffboardedStep1(msg.sender, scribe); + } + + /// @notice Step 2 of offboarding `scribe`: lifts the hardcoded offboarder + /// feed, pokes val = 0, drops the offboarder feed, and raises + /// bar = 255. + /// + /// @dev Reverts if `offboard_Step1` not executed for `scribe`. + /// @dev Reverts if executed at the same timestamp as `offboard_Step1`. + function offboard_Step2(address scribe) external auth { + require(IAuth(scribe).authed(address(this))); + + IScribe target = IScribe(scribe); + + // Require step 1 to be executed. + require(target.feeds().length == 0); + require(target.bar() == 1); + + // Lift the offboarder feed. + // + // Note that lift does not update the current value's age. + target.lift( + LibSecp256k1.Point({x: FEED_PUBKEY_X, y: FEED_PUBKEY_Y}), + IScribe.ECDSAData({v: LIFT_V, r: LIFT_R, s: LIFT_S}) + ); + + // Poke val = 0 via the offboarder feed's 1-of-1 Schnorr signature. + uint32 pokeAge = uint32(block.timestamp); + bytes32 message = target.constructPokeMessage( + IScribe.PokeData({val: 0, age: pokeAge}) + ); + (bytes32 signature, address commitment) = _signSchnorr(message); + target.poke( + IScribe.PokeData({val: 0, age: pokeAge}), + IScribe.SchnorrData({ + signature: signature, + commitment: commitment, + feedIds: abi.encodePacked(FEED_ID) + }) + ); + + // Drop offboarder again. + target.drop(FEED_ID); + + // Set bar to max. + target.setBar(type(uint8).max); + + // Deny offboarder. + IAuth(scribe).deny(address(this)); + + emit OffboardedStep2(msg.sender, scribe); + } + + // ----------------------------------------------------------------------------- + // Internal — Schnorr signing + + /// @dev Produces a 1-of-1 Schnorr signature over `message` using the + /// hardcoded offboarder feed. + /// + /// The ethereum address `R_e = address(k·G)` is computed via the + /// ecrecover precompile trick rather than a full secp256k1 scalar + /// multiplication: with `h = 0`, `r = G_x`, `v = G_v`, + /// `s = k·G_x mod Q`, ecrecover returns exactly `address(k·G)` — + /// which is all the signing scheme needs as the commitment. + /// + /// See the scribe repo's `docs/Schnorr.md` for the scheme this + /// implements. + function _signSchnorr(bytes32 message) + internal + pure + returns (bytes32 signature, address commitment) + { + // k = H(privKey ‖ m) mod Q + uint nonce = + uint(keccak256(abi.encodePacked(FEED_PRIV_KEY, message))) % Q; + + // R_e = address(k·G) via the ecrecover trick. + commitment = ecrecover( + bytes32(0), G_V, bytes32(G_X), bytes32(mulmod(nonce, G_X, Q)) + ); + require( + commitment != address(0), + "ScribeOptimisticOffboarder: bad commitment" + ); + + // e = H(Pₓ ‖ Pₚ ‖ m ‖ R_e) mod Q + uint challenge = uint( + keccak256( + abi.encodePacked( + FEED_PUBKEY_X, uint8(FEED_PUBKEY_Y & 1), message, commitment + ) + ) + ) % Q; + + // s = k + e·x mod Q + signature = + bytes32(addmod(nonce, mulmod(challenge, FEED_PRIV_KEY, Q), Q)); + } +} diff --git a/test/offboard/ScribeOptimisticOffboarder.t.sol b/test/offboard/ScribeOptimisticOffboarder.t.sol new file mode 100644 index 0000000..4d28294 --- /dev/null +++ b/test/offboard/ScribeOptimisticOffboarder.t.sol @@ -0,0 +1,265 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.16; + +import {Test, Vm} from "forge-std/Test.sol"; + +import {IAuth} from "chronicle-std/auth/IAuth.sol"; +import {IToll} from "chronicle-std/toll/IToll.sol"; + +import {IScribe} from "../../src/IScribe.sol"; +import {IScribeOptimistic} from "../../src/IScribeOptimistic.sol"; +import {ScribeOptimistic} from "../../src/ScribeOptimistic.sol"; +import {LibSecp256k1} from "../../src/libs/LibSecp256k1.sol"; + +import { + ScribeOptimisticOffboarder +} from "../../script/offboard/ScribeOptimisticOffboarder.sol"; + +contract ScribeOptimisticOffboarderTest is Test { + bytes32 internal constant WAT = bytes32("ETH/USD"); + + /// @dev Must match `feedId` in `ScribeOptimisticOffboarder.sol`. + uint8 internal constant OFFBOARDER_FEED_ID = 0x03; + + ScribeOptimistic internal scribe; + ScribeOptimisticOffboarder internal offboarder; + + function setUp() public { + scribe = new ScribeOptimistic({initialAuthed: address(this), wat_: WAT}); + IToll(address(scribe)).kiss(address(this)); + + offboarder = + new ScribeOptimisticOffboarder({initialAuthed: address(this)}); + IAuth(address(scribe)).rely(address(offboarder)); + + vm.warp(1_700_000_000); + } + + // ----------------------------------------------------------------------------- + // Offboard + + function test_offboard() public { + _liftRandomFeeds(3, 0xBEEF); + + offboarder.offboard_Step1(address(scribe)); + vm.warp(block.timestamp + 1); + offboarder.offboard_Step2(address(scribe)); + + assertEq(scribe.feeds().length, 0); + assertEq(scribe.bar(), type(uint8).max); + (bool ok,) = scribe.tryRead(); + assertFalse(ok); + } + + function testFuzz_offboard( + uint8 nFeeds, + uint64 timeAdvance, + uint64 seedSalt + ) public { + nFeeds = uint8(bound(nFeeds, 0, 32)); + timeAdvance = uint64(bound(timeAdvance, 1, 365 days)); + + _liftRandomFeeds(nFeeds, uint(seedSalt) | 1); + + offboarder.offboard_Step1(address(scribe)); + vm.warp(block.timestamp + timeAdvance); + offboarder.offboard_Step2(address(scribe)); + + assertEq(scribe.feeds().length, 0); + assertEq(scribe.bar(), type(uint8).max); + (bool ok,) = scribe.tryRead(); + assertFalse(ok); + + assertFalse(IAuth(address(scribe)).authed(address(offboarder))); + } + + // ----------------------------------------------------------------------------- + // Step 1 + + function testFuzz_offboard_Step1_DropsFeedsAndSetsBarToOne( + uint8 nFeeds, + uint64 seedSalt + ) public { + nFeeds = uint8(bound(nFeeds, 0, 32)); + _liftRandomFeeds(nFeeds, uint(seedSalt) | 1); + + offboarder.offboard_Step1(address(scribe)); + + // Note that no feed is lifted, ie no poke can verify. + assertEq(scribe.feeds().length, 0); + assertEq(scribe.bar(), 1); + assertTrue(IAuth(address(scribe)).authed(address(offboarder))); + } + + // ----------------------------------------------------------------------------- + // Step 2 + + function test_offboard_Step2_FailsIf_Step1NotExecuted() public { + _liftRandomFeeds(3, 0xBEEF); + + vm.expectRevert(); + offboarder.offboard_Step2(address(scribe)); + } + + function test_offboard_Step2_FailsIf_BarNotOne() public { + offboarder.offboard_Step1(address(scribe)); + scribe.setBar(2); + vm.warp(block.timestamp + 1); + + vm.expectRevert(); + offboarder.offboard_Step2(address(scribe)); + } + + function test_offboard_Step2_FailsIf_SameTimestampAsStep1() public { + _liftRandomFeeds(3, 0xBEEF); + + offboarder.offboard_Step1(address(scribe)); + + uint32 age = uint32(block.timestamp); + vm.expectRevert( + abi.encodeWithSelector(IScribe.StaleMessage.selector, age, age) + ); + offboarder.offboard_Step2(address(scribe)); + } + + // ----------------------------------------------------------------------------- + // Auth Protection + + function testFuzz_offboard_Step1_isAuthProtected(address caller) public { + vm.assume(!IAuth(address(offboarder)).authed(caller)); + + vm.prank(caller); + vm.expectRevert( + abi.encodeWithSelector(IAuth.NotAuthorized.selector, caller) + ); + offboarder.offboard_Step1(address(scribe)); + } + + function testFuzz_offboard_Step2_isAuthProtected(address caller) public { + vm.assume(!IAuth(address(offboarder)).authed(caller)); + + vm.prank(caller); + vm.expectRevert( + abi.encodeWithSelector(IAuth.NotAuthorized.selector, caller) + ); + offboarder.offboard_Step2(address(scribe)); + } + + // ----------------------------------------------------------------------------- + // Value zeroing + + function testFuzz_offboard_zeroesValue(uint8 nFeeds, uint64 seedSalt) + public + { + nFeeds = uint8(bound(nFeeds, 0, 32)); + _liftRandomFeeds(nFeeds, uint(seedSalt) | 1); + + offboarder.offboard_Step1(address(scribe)); + vm.warp(block.timestamp + 1); + offboarder.offboard_Step2(address(scribe)); + + (bool ok, uint val) = scribe.tryRead(); + assertFalse(ok, "tryRead should report invalid"); + assertEq(val, 0, "scribe stored value must be zero"); + + vm.expectRevert(); + scribe.read(); + } + + function test_offboard_zeroesValue_If_OpPokeFinalized() public { + _opPoke(1e18); + vm.warp(block.timestamp + scribe.opChallengePeriod()); + + uint val = scribe.read(); + assertEq(val, 1e18); + + offboarder.offboard_Step1(address(scribe)); + vm.warp(block.timestamp + 1); + offboarder.offboard_Step2(address(scribe)); + + (bool ok, uint val_) = scribe.tryRead(); + assertFalse(ok); + assertEq(val_, 0); + } + + function test_offboard_zeroesValue_If_OpPokeNotFinalized() public { + _opPoke(1e18); + + offboarder.offboard_Step1(address(scribe)); + vm.warp(block.timestamp + scribe.opChallengePeriod()); + offboarder.offboard_Step2(address(scribe)); + + (bool ok, uint val) = scribe.tryRead(); + assertFalse(ok); + assertEq(val, 0); + } + + // ----------------------------------------------------------------------------- + // Helpers + + function _feedId(address who) internal pure returns (uint8) { + return uint8(uint(uint160(who)) >> 152); + } + + /// @dev Lifts a feed and opPokes `val` with an invalid Schnorr signature. + function _opPoke(uint128 val) internal { + Vm.Wallet memory w = vm.createWallet(0xC0FFEE); + require(_feedId(w.addr) != OFFBOARDER_FEED_ID); + + (uint8 v, bytes32 r, bytes32 s) = + vm.sign(w.privateKey, scribe.feedRegistrationMessage()); + scribe.lift( + LibSecp256k1.Point({x: w.publicKeyX, y: w.publicKeyY}), + IScribe.ECDSAData({v: v, r: r, s: s}) + ); + + IScribe.PokeData memory pokeData = + IScribe.PokeData({val: val, age: uint32(block.timestamp)}); + IScribe.SchnorrData memory schnorrData = IScribe.SchnorrData({ + signature: bytes32(0), commitment: address(0), feedIds: hex"" + }); + + (v, r, s) = vm.sign( + w.privateKey, scribe.constructOpPokeMessage(pokeData, schnorrData) + ); + IScribeOptimistic(address(scribe)) + .opPoke( + pokeData, schnorrData, IScribe.ECDSAData({v: v, r: r, s: s}) + ); + } + + /// @dev Lifts up to `n` feeds derived from sequential private keys + /// starting at `seed`. Skips collisions with already-lifted feed + /// ids and with the offboarder's reserved id. + function _liftRandomFeeds(uint8 n, uint seed) + internal + returns (uint lifted) + { + if (n == 0) { + return 0; + } + + bytes32 reg = scribe.feedRegistrationMessage(); + + uint bloom; + uint pk = seed; + uint tried; + while (lifted < n && tried < 1024) { + tried++; + Vm.Wallet memory w = vm.createWallet(pk++); + uint8 id = _feedId(w.addr); + + if (bloom & (1 << id) != 0) { + continue; + } + bloom |= 1 << id; + + (uint8 v, bytes32 r, bytes32 s) = vm.sign(w.privateKey, reg); + scribe.lift( + LibSecp256k1.Point({x: w.publicKeyX, y: w.publicKeyY}), + IScribe.ECDSAData({v: v, r: r, s: s}) + ); + lifted++; + } + } +} diff --git a/test/rescue/RescuerTest.t.sol b/test/rescue/RescuerTest.t.sol index 0c4592c..a5d4add 100644 --- a/test/rescue/RescuerTest.t.sol +++ b/test/rescue/RescuerTest.t.sol @@ -165,6 +165,7 @@ contract RescuerTest is Test { ) public { vm.assume(receiver.code.length == 0); vm.assume(uint160(address(receiver)) > 100); // Ensure no precompile + vm.assume(receiver.balance == 0); // Ensure no pre-funded account vm.assume(balance >= withdrawal); // Let rescuer have ETH balance.