Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reinit confidential control #21

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/blockad/BlockAdV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { DynamicUintArray } from "./lib/Utils.sol";
import { Builder } from "./lib/Builder.sol";


Check warning on line 11 in contracts/blockad/BlockAdV2.sol

View workflow job for this annotation

GitHub Actions / lint

Delete ⏎
contract BlockAdAuctionV2 is AnyBundleContract, ConfidentialControl {
using DynamicUintArray for bytes;

Expand Down Expand Up @@ -71,13 +71,13 @@
* 🔒 CONFIDENTIAL METHODS *
***********************************************************************/

function confidentialConstructor() public override onlyConfidential returns (bytes memory) {
return ConfidentialControl.confidentialConstructor();
function confidentialConstructor() public onlyConfidential returns (bytes memory) {
return ConfidentialControl.init();
}

function buyAd(uint64 blockLimit, string memory extra) external onlyConfidential returns (bytes memory) {
bytes memory paymentBundle = this.fetchConfidentialBundleData();
(,uint64 egp) = simulateBundleSafe(paymentBundle, true);

Check warning on line 80 in contracts/blockad/BlockAdV2.sol

View workflow job for this annotation

GitHub Actions / lint

Insert ·
crequire(egp > 0, "egp too low");
Suave.DataId paymentBidId = storePaymentBundle(paymentBundle);
AdRequest memory request = AdRequest(nextId, extra, blockLimit, paymentBidId);
Expand Down Expand Up @@ -167,9 +167,9 @@
allowedPeekers[1] = Suave.BUILD_ETH_BLOCK;
allowedPeekers[2] = address(this);
Suave.DataRecord memory paymentBundleBid = Suave.newDataRecord(
blockHeight,

Check warning on line 170 in contracts/blockad/BlockAdV2.sol

View workflow job for this annotation

GitHub Actions / lint

Delete ·
allowedPeekers,

Check warning on line 171 in contracts/blockad/BlockAdV2.sol

View workflow job for this annotation

GitHub Actions / lint

Delete ·
allowedPeekers,

Check warning on line 172 in contracts/blockad/BlockAdV2.sol

View workflow job for this annotation

GitHub Actions / lint

Delete ·
EB_NAMESPACE
);
Suave.confidentialStore(paymentBundleBid.id, EB_NAMESPACE, bestOffer.paymentBundle);
Expand Down
30 changes: 11 additions & 19 deletions contracts/blockad/lib/ConfidentialControl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ abstract contract ConfidentialControl is SuaveContract {
nonce++;
}

modifier onlyOwner() {
crequire(msg.sender == ccontrolOwner, "Unauthorized");
_;
}

address public immutable ccontrolOwner = msg.sender;
string internal constant S_NAMESPACE = "blockad:v0:secret";
Suave.DataId internal secretBidId;
bytes32 internal presentHash;
uint internal nonce;

/**********************************************************************
* ⛓️ ON-CHAIN METHODS *
***********************************************************************/

function ccCallback(bytes32 nextHash, Suave.DataId sBidId) external {
crequire(!isInitialized(), "Already initialized");
function ccCallback(bytes32 nextHash, Suave.DataId sBidId) external onlyOwner {
presentHash = nextHash;
secretBidId = sBidId;
}
Expand All @@ -38,21 +39,16 @@ abstract contract ConfidentialControl is SuaveContract {
return presentHash != 0;
}

/**********************************************************************
* 🔒 CONFIDENTIAL METHODS *
***********************************************************************/

function confidentialConstructor() public virtual onlyConfidential returns (bytes memory) {
crequire(!isInitialized(), "Already initialized");
function init() internal onlyOwner returns (bytes memory) {
bytes memory secret = Suave.confidentialInputs();
Suave.DataId sBidId = storeSecret(secret);
bytes32 nextHash = makeHash(abi.decode(secret, (bytes32)), nonce);
return abi.encodeWithSelector(this.ccCallback.selector, nextHash, sBidId);
}

/**********************************************************************
* 🛠️ INTERNAL METHODS *
***********************************************************************/
function getUnlockPair() internal returns (UnlockArgs memory) {
return UnlockArgs(getKey(nonce), getHash(nonce + 1));
}

function storeSecret(bytes memory secret) internal returns (Suave.DataId) {
address[] memory peekers = new address[](3);
Expand All @@ -68,10 +64,6 @@ abstract contract ConfidentialControl is SuaveContract {
return keccak256(abi.encode(key)) == presentHash;
}

function getUnlockPair() internal returns (UnlockArgs memory) {
return UnlockArgs(getKey(nonce), getHash(nonce + 1));
}

function getHash(uint _nonce) internal returns (bytes32) {
return keccak256(abi.encode(getKey(_nonce)));
}
Expand Down
81 changes: 47 additions & 34 deletions deployments/toliman/BlockAdAuctionV2.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'hardhat-deploy'
import { getEnvValSafe } from './tasks/utils'
import './tasks/oracle-updates'
import './tasks/build-blocks'
import './tasks/adblock-init'
import './tasks/mevshare'
import './tasks/adblock'

Expand Down
6 changes: 6 additions & 0 deletions scripts/blockad_init_toliman.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

npx hardhat block-ad-init --network toliman

mkdir -p ~/.log
echo "BlockAd init executed on $(date)" >> ~/.log/suave-reinit.log
39 changes: 39 additions & 0 deletions tasks/adblock-init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { HardhatRuntimeEnvironment as HRE } from 'hardhat/types'
import { task } from 'hardhat/config'
import { ethers } from 'ethers'

import { supportedSuaveChains } from './utils/const'
import { SuaveContract } from 'ethers-suave'
import * as utils from './utils'


// todo: handle duplicate code in tasks/adblock.ts

task('block-ad-init', 'Initialize the BlockAdAuction contract')
.addOptionalParam('blockad', 'Address of a BlockAd contract. By default fetch most recently deployed one.')
.setAction(async function (taskArgs: any, hre: HRE) {
utils.checkChain(hre, supportedSuaveChains)
const contract = await getBlockAdContract(hre, taskArgs.blockad)
await confidentialInit(contract)
})

async function confidentialInit(contract: SuaveContract): Promise<boolean> {
const confidentialInputs = ethers.utils.id(utils.getRandomStr())
const ccrPromise = contract.confidentialConstructor.sendCCR({ confidentialInputs })
console.log('Sending init tx')
return utils.prettyPromise(ccrPromise, contract, 'Initializing BlockAdAuction')
.then(utils.handleResult)
}

async function getBlockAdContract(hre: HRE, blockad?: string): Promise<SuaveContract> {
const chainId = utils.getNetworkChainId(hre)
const suaveSigner = utils.makeSuaveSigner(chainId)
const blockadContract = blockad
? await hre.ethers.getContractAt('BlockAdAuctionV2', blockad)
: await utils.fetchDeployedContract(hre, 'BlockAdAuctionV2')
return new SuaveContract(
blockadContract.address,
blockadContract.interface,
suaveSigner
)
}
10 changes: 3 additions & 7 deletions tasks/adblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ task('block-ad', 'Submit bids, build blocks and send them to relay')
.addOptionalParam('adbid', 'Bid amount in ETH for including the ad', 0.2, types.float)
.addOptionalParam('nslots', 'For how many blocks the ad-request is valid', 2, types.int)
.addOptionalParam('builder', 'Address of a Builder contract. By default fetch most recently deployed one.')
.addOptionalParam('mevshare', 'Address of a MevShare contract. By default fetch most recently deployed one.')
.addOptionalParam('blockad', 'Address of a BlockAd contract. By default fetch most recently deployed one.')
.addFlag('build', 'Whether to build blocks after sending the ad-request')
.setAction(async function (taskArgs: any, hre: HRE) {
utils.checkChain(hre, supportedSuaveChains)
Expand Down Expand Up @@ -92,7 +92,7 @@ async function confidentialInit(c: ITaskConfig): Promise<boolean> {
const confidentialInputs = ethers.utils.id(utils.getRandomStr())
const ccrPromise = c.blockad.confidentialConstructor.sendCCR({ confidentialInputs })
console.log('Sending init tx')
return utils.prettyPromise(ccrPromise, c.blockad, 'Building block')
return utils.prettyPromise(ccrPromise, c.blockad, 'Initializing BlockAdAuction')
.then(utils.handleResult)
}

Expand Down Expand Up @@ -133,11 +133,7 @@ async function getConfig(hre: HRE, taskArgs: any): Promise<ITaskConfig> {
export async function getEnvConfig(hhChainId: number) {
const holeskySigner = utils.makeHoleskySigner()
const suaveSigner = utils.makeSuaveSigner(hhChainId)
const executionNodeAdd = await (suaveSigner.provider as any)
.send('eth_kettleAddress', [])
.then((res: string[]) => res[0])
return {
executionNodeAdd,
holeskySigner,
suaveSigner,
}
Expand All @@ -148,7 +144,7 @@ async function parseTaskArgs(hre: HRE, taskArgs: any) {
const extra = taskArgs.extra
const adBid = taskArgs.adbid
const blockadContract = taskArgs.blockad
? await hre.ethers.getContractAt('BlockAdAuctionV2', taskArgs.mevshare)
? await hre.ethers.getContractAt('BlockAdAuctionV2', taskArgs.blockad)
: await utils.fetchDeployedContract(hre, 'BlockAdAuctionV2')

return { blockadContract, extra, adBid, nslots }
Expand Down
Loading