Skip to content

Commit c50b72c

Browse files
committed
add MultisigOracle
1 parent 1ed6bc0 commit c50b72c

3 files changed

Lines changed: 238 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

‎src/libraries/HashLib.sol‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ library HashLib {
1919
res := keccak256(0, 0x40)
2020
}
2121
}
22+
23+
function hash(bytes32 val0, bytes32 val1) internal pure returns (bytes32) {
24+
return bytes32(hash(uint256(val0), uint256(val1)));
25+
}
2226
}

‎test/MultisigOracle.t.sol‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// // SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import {Test, console} from "forge-std/Test.sol";
5+
import {HashLib} from "../src/BlueprintManager.sol";
6+
import {
7+
ConstantOracle,
8+
MultisigOracle,
9+
Response
10+
} from "../src/blueprints/oracleBased/oracle/MultisigOracle.sol";
11+
12+
library BytesLib {
13+
// gotta make sure the memory after the array is allocated not to overwrite
14+
// other memory variables
15+
function append(bytes memory b, bytes memory a) internal pure {
16+
assembly ("memory-safe") {
17+
let blen := mload(b)
18+
let alen := mload(a)
19+
mcopy(add(add(0x20, b), blen), add(a, 0x20), alen)
20+
mstore(b, add(blen, alen))
21+
}
22+
}
23+
24+
function create(uint256 length) internal pure returns (bytes memory b) {
25+
assembly ("memory-safe") {
26+
b := mload(0x40)
27+
mstore(0x40, add(b, add(0x20, length)))
28+
mstore(b, 0)
29+
}
30+
}
31+
}
32+
33+
using BytesLib for bytes;
34+
35+
contract MultisigOracleTest is Test {
36+
mapping (bytes32 internalFeed => bool responded) responded;
37+
ConstantOracle oracle = new ConstantOracle();
38+
MultisigOracle moracle = new MultisigOracle(oracle);
39+
40+
function setUp() external {}
41+
42+
function testMultisig(uint256 signers, Response[] calldata responses) public {
43+
vm.assume(signers < 1000);
44+
45+
uint256 threshold = vm.randomUint(0, signers);
46+
Response[] memory res = responses;
47+
48+
address[] memory signersList = new address[](signers);
49+
for (uint256 i = 0; i < signers; i++) {
50+
signersList[i] = vm.createWallet(i + 1).addr;
51+
}
52+
53+
bytes32 multisig = keccak256(abi.encodePacked(threshold, signersList));
54+
bytes memory packedResponses = BytesLib.create(64 * res.length);
55+
for (uint256 i = 0; i < res.length; i++) {
56+
vm.assume(!responded[res[i].feedId]);
57+
responded[res[i].feedId] = true;
58+
res[i].feedId = keccak256(abi.encodePacked(multisig, res[i].feedId));
59+
packedResponses.append(abi.encodePacked(res[i].feedId, res[i].data));
60+
}
61+
62+
bytes32 digest = keccak256(packedResponses);
63+
64+
bytes memory signatures = BytesLib.create(signers * 65);
65+
uint256 signed = 0;
66+
uint256 skipped = 0;
67+
for (uint256 i = 0; i < signers; i++) {
68+
if (vm.randomBool()) {
69+
while (skipped != 0) {
70+
uint256 skipping = skipped > 27 ? 27 : skipped;
71+
signatures.append(abi.encodePacked(uint8(skipping - 1)));
72+
skipped -= skipping;
73+
}
74+
(uint8 v, bytes32 r, bytes32 s) = vm.sign(i + 1, digest);
75+
signatures.append(abi.encodePacked(v, r, s));
76+
signed++;
77+
} else {
78+
skipped++;
79+
}
80+
}
81+
while (skipped != 0) {
82+
uint skipping = skipped > 26 ? 26 : skipped;
83+
signatures.append(abi.encodePacked(skipping));
84+
skipped -= skipping;
85+
}
86+
87+
bool failure = signed < threshold;
88+
if (failure)
89+
vm.expectRevert(MultisigOracle.SignatureValidationFailed.selector);
90+
91+
moracle.cache(threshold, signersList, responses, signatures);
92+
93+
if (!failure) {
94+
for (uint256 i = 0; i < res.length; i++) {
95+
bytes32 feed = keccak256(abi.encodePacked(moracle, res[i].feedId));
96+
vm.assertEq(oracle.getReading(feed), res[i].data);
97+
}
98+
}
99+
100+
// moracle.cache(3, new address[](0), new Response[](0));
101+
}
102+
}

0 commit comments

Comments
 (0)