|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {IOracle, Response, ConstantOracle} from "./ConstantOracle.sol"; |
| 5 | +import {HashLib} from "../../../libraries/HashLib.sol"; |
| 6 | +import {console} from "forge-std/Test.sol"; |
| 7 | + |
| 8 | +type ResponseArr is uint256; |
| 9 | + |
| 10 | +library UnsafeResponseArrLib { |
| 11 | + error CacheCallFailed(); |
| 12 | + bytes4 constant cacheSelector = bytes4(keccak256("cache((bytes32,uint256)[])")); |
| 13 | + |
| 14 | + function newResponseArr(Response[] calldata responses) internal pure returns (ResponseArr arr) { |
| 15 | + assembly ("memory-safe") { |
| 16 | + // allocate 0x44 = 68 additional bytes before the array |
| 17 | + arr := add(mload(0x40), 0x44) |
| 18 | + let len := shl(6, responses.length) |
| 19 | + mstore(0x40, add(arr, len)) |
| 20 | + // copy `data` from responses, `feedId`s will be rewritten anyway |
| 21 | + calldatacopy(arr, responses.offset, len) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // the caller has to make sure idx is in bounds |
| 26 | + function at(ResponseArr arr, uint256 idx) internal pure returns (Response memory res) { |
| 27 | + assembly ("memory-safe") { |
| 28 | + res := add(arr, shl(6, idx)) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + function hash(ResponseArr arr, uint256 length) internal pure returns (bytes32 res) { |
| 33 | + assembly ("memory-safe") { |
| 34 | + res := keccak256(arr, shl(6, length)) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + function cache(ResponseArr arr, ConstantOracle co, uint256 length) internal { |
| 39 | + bool success; |
| 40 | + bytes4 selector = cacheSelector; |
| 41 | + assembly ("memory-safe") { |
| 42 | + arr := sub(arr, 0x44) |
| 43 | + mstore(arr, selector) |
| 44 | + mstore(add(arr, 0x04), 0x20) |
| 45 | + mstore(add(arr, 0x24), length) |
| 46 | + success := call(gas(), co, 0, arr, add(0x44, shl(6, length)), 0, 0) |
| 47 | + } |
| 48 | + if (!success) |
| 49 | + revert CacheCallFailed(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +using UnsafeResponseArrLib for ResponseArr; |
| 54 | + |
| 55 | +// if the list of signers contains the same address multiple times, it will be |
| 56 | +// able to vote multiple times as well |
| 57 | +contract MultisigOracle is IOracle { |
| 58 | + ConstantOracle immutable constantOracle; |
| 59 | + |
| 60 | + error SignatureValidationFailed(); |
| 61 | + |
| 62 | + constructor(ConstantOracle _constantOracle) { |
| 63 | + constantOracle = _constantOracle; |
| 64 | + } |
| 65 | + |
| 66 | + function cache( |
| 67 | + uint256 threshold, |
| 68 | + address[] calldata signers, |
| 69 | + Response[] calldata responses, |
| 70 | + bytes calldata signatures |
| 71 | + ) public { |
| 72 | + bytes32 multisig = getMultisig(threshold, signers); |
| 73 | + |
| 74 | + ResponseArr arr = UnsafeResponseArrLib.newResponseArr(responses); |
| 75 | + for (uint256 i = 0; i < responses.length; i++) { |
| 76 | + Response memory res = arr.at(i); |
| 77 | + res.feedId = HashLib.hash(multisig, res.feedId); |
| 78 | + } |
| 79 | + |
| 80 | + bytes32 payload = arr.hash(responses.length); |
| 81 | + |
| 82 | + // verify signatures |
| 83 | + uint256 idx = 0; |
| 84 | + uint256 ptr = 0; |
| 85 | + uint256 signed = 0; |
| 86 | + unchecked { |
| 87 | + while (idx < signers.length) { |
| 88 | + uint8 v = uint8(signatures[ptr++]); |
| 89 | + if (v < 27) { |
| 90 | + idx += v + 1; |
| 91 | + continue; |
| 92 | + } |
| 93 | + bytes32 r = bytes32(signatures[ptr:]); |
| 94 | + bytes32 s = bytes32(signatures[ptr + 32:]); |
| 95 | + ptr += 64; |
| 96 | + if (signers[idx] != ecrecover(payload, v, r, s)) |
| 97 | + revert SignatureValidationFailed(); |
| 98 | + |
| 99 | + signed++; |
| 100 | + idx++; |
| 101 | + } |
| 102 | + } |
| 103 | + if (signed < threshold) |
| 104 | + revert SignatureValidationFailed(); |
| 105 | + |
| 106 | + arr.cache(constantOracle, responses.length); |
| 107 | + } |
| 108 | + |
| 109 | + function getReading(bytes32 /*feedId*/, bytes calldata /*proof*/) external pure returns (uint256) { |
| 110 | + // (uint8 v, bytes32 r, bytes32 s, uint256 response) = |
| 111 | + // abi.decode(proof, (uint8, bytes32, bytes32, uint256)); |
| 112 | + // bytes32 payload = bytes32(HashLib.hash(uint256(feedId), response)); |
| 113 | + |
| 114 | + // require(ecrecover(payload, v, r, s) == signer); |
| 115 | + // return response; |
| 116 | + revert(); |
| 117 | + } |
| 118 | + |
| 119 | + function getReading(bytes32) external pure returns (uint256) { |
| 120 | + revert(); |
| 121 | + } |
| 122 | + |
| 123 | + function getMultisig(uint256 threshold, address[] calldata signers) internal pure returns (bytes32 res) { |
| 124 | + assembly ("memory-safe") { |
| 125 | + let ptr := mload(0x40) |
| 126 | + mstore(ptr, threshold) |
| 127 | + let len := shl(5, signers.length) |
| 128 | + calldatacopy(add(ptr, 0x20), signers.offset, len) |
| 129 | + res := keccak256(ptr, add(len, 0x20)) |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments