Skip to content
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
50 changes: 34 additions & 16 deletions crates/evm-helpers/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ use tokio::sync::Mutex;

static NONCE_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

pub async fn next_pending_nonce<P>(provider: &P) -> eyre::Result<u64>
/// Get the next pending nonce for a given address from the provider
async fn get_next_nonce<P>(provider: &P, address: Address) -> eyre::Result<u64>
where
P: Provider<Ethereum> + Send + Sync,
{
let from = provider.get_accounts().await?[0];
provider
.get_transaction_count(from)
.get_transaction_count(address)
.pending()
.await
.map_err(Into::into)
Expand Down Expand Up @@ -174,6 +174,7 @@ impl ProviderType for ReadWrite {
pub struct EnclaveContract<T: ProviderType> {
pub provider: Arc<T::Provider>,
pub contract_address: Address,
pub wallet_address: Option<Address>,
_marker: PhantomData<T>,
}

Expand Down Expand Up @@ -225,13 +226,10 @@ pub type EnclaveReadOnlyProvider = FillProvider<
pub type EnclaveWriteProvider = FillProvider<
JoinFill<
JoinFill<
JoinFill<
Identity,
JoinFill<GasFiller, JoinFill<BlobGasFiller, JoinFill<NonceFiller, ChainIdFiller>>>,
>,
WalletFiller<EthereumWallet>,
Identity,
JoinFill<GasFiller, JoinFill<BlobGasFiller, JoinFill<NonceFiller, ChainIdFiller>>>,
>,
NonceFiller,
WalletFiller<EthereumWallet>,
>,
RootProvider<Ethereum>,
Ethereum,
Expand All @@ -254,16 +252,17 @@ impl EnclaveContractFactory {
let contract_address = contract_address.parse()?;

let signer: PrivateKeySigner = private_key.parse()?;
let wallet_address = signer.address();
let wallet = EthereumWallet::from(signer);
let provider = ProviderBuilder::new()
.wallet(wallet)
.with_cached_nonce_management()
.connect(http_rpc_url)
.await?;

Ok(EnclaveContract::<ReadWrite> {
provider: Arc::new(provider),
contract_address,
wallet_address: Some(wallet_address),
_marker: PhantomData,
})
}
Expand All @@ -280,6 +279,7 @@ impl EnclaveContractFactory {
Ok(EnclaveContract::<ReadOnly> {
provider: Arc::new(provider),
contract_address,
wallet_address: None,
_marker: PhantomData,
})
}
Expand Down Expand Up @@ -347,7 +347,10 @@ impl EnclaveWrite for EnclaveContract<ReadWrite> {
compute_provider_params: Bytes,
) -> Result<TransactionReceipt> {
let _guard = NONCE_LOCK.lock().await;
let nonce = next_pending_nonce(&*self.provider).await?;
let wallet_addr = self
.wallet_address
.ok_or_else(|| eyre::eyre!("No wallet address configured"))?;
let nonce = get_next_nonce(&*self.provider, wallet_addr).await?;

let e3_request = E3RequestParams {
filter,
Expand All @@ -371,7 +374,10 @@ impl EnclaveWrite for EnclaveContract<ReadWrite> {

async fn activate(&self, e3_id: U256, pub_key: Bytes) -> Result<TransactionReceipt> {
let _guard = NONCE_LOCK.lock().await;
let nonce = next_pending_nonce(&*self.provider).await?;
let wallet_addr = self
.wallet_address
.ok_or_else(|| eyre::eyre!("No wallet address configured"))?;
let nonce = get_next_nonce(&*self.provider, wallet_addr).await?;

let contract = Enclave::new(self.contract_address, &self.provider);
let builder = contract.activate(e3_id, pub_key).nonce(nonce);
Expand All @@ -382,7 +388,10 @@ impl EnclaveWrite for EnclaveContract<ReadWrite> {

async fn enable_e3_program(&self, e3_program: Address) -> Result<TransactionReceipt> {
let _guard = NONCE_LOCK.lock().await;
let nonce = next_pending_nonce(&*self.provider).await?;
let wallet_addr = self
.wallet_address
.ok_or_else(|| eyre::eyre!("No wallet address configured"))?;
let nonce = get_next_nonce(&*self.provider, wallet_addr).await?;

let contract = Enclave::new(self.contract_address, &self.provider);
let builder = contract.enableE3Program(e3_program).nonce(nonce);
Expand All @@ -393,7 +402,10 @@ impl EnclaveWrite for EnclaveContract<ReadWrite> {

async fn publish_input(&self, e3_id: U256, data: Bytes) -> Result<TransactionReceipt> {
let _guard = NONCE_LOCK.lock().await;
let nonce = next_pending_nonce(&*self.provider).await?;
let wallet_addr = self
.wallet_address
.ok_or_else(|| eyre::eyre!("No wallet address configured"))?;
let nonce = get_next_nonce(&*self.provider, wallet_addr).await?;

let contract = Enclave::new(self.contract_address, &self.provider);
let builder = contract.publishInput(e3_id, data).nonce(nonce);
Expand All @@ -409,7 +421,10 @@ impl EnclaveWrite for EnclaveContract<ReadWrite> {
proof: Bytes,
) -> Result<TransactionReceipt> {
let _guard = NONCE_LOCK.lock().await;
let nonce = next_pending_nonce(&*self.provider).await?;
let wallet_addr = self
.wallet_address
.ok_or_else(|| eyre::eyre!("No wallet address configured"))?;
let nonce = get_next_nonce(&*self.provider, wallet_addr).await?;

let contract = Enclave::new(self.contract_address, &self.provider);
let builder = contract
Expand All @@ -427,7 +442,10 @@ impl EnclaveWrite for EnclaveContract<ReadWrite> {
proof: Bytes,
) -> Result<TransactionReceipt> {
let _guard = NONCE_LOCK.lock().await;
let nonce = next_pending_nonce(&*self.provider).await?;
let wallet_addr = self
.wallet_address
.ok_or_else(|| eyre::eyre!("No wallet address configured"))?;
let nonce = get_next_nonce(&*self.provider, wallet_addr).await?;

let contract = Enclave::new(self.contract_address, &self.provider);
let builder = contract
Expand Down
5 changes: 4 additions & 1 deletion crates/support/contracts/ImageID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
pragma solidity ^0.8.20;

library ImageID {
bytes32 public constant PROGRAM_ID = bytes32(0x281708e32ec4018495d0efb9e6c5e36c238927c23afc9743fd4f20c67f47b489);
bytes32 public constant PROGRAM_ID =
bytes32(
0x23734b77b0f76e85623a88d7a82f24c34c94834f2501964ea123b7a2027013a2
);
}
5 changes: 1 addition & 4 deletions examples/CRISP/contracts/ImageID.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@
pragma solidity ^0.8.20;

library ImageID {
bytes32 public constant PROGRAM_ID =
bytes32(
0x281708e32ec4018495d0efb9e6c5e36c238927c23afc9743fd4f20c67f47b489
);
bytes32 public constant PROGRAM_ID = bytes32(0x23734b77b0f76e85623a88d7a82f24c34c94834f2501964ea123b7a2027013a2);
}
41 changes: 30 additions & 11 deletions packages/enclave-contracts/deployed_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,47 @@
"Enclave": {
"constructorArgs": {
"params": "0x000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000fc00100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000003fffffff000001",
"owner": "0x4f1f3a157073A35515C4fC4A8af2F1Af088f0676",
"owner": "0x8837e47c4Bb520ADE83AAB761C3B60679443af1B",
"maxDuration": "2592000",
"registry": "0x0000000000000000000000000000000000000001"
},
"blockNumber": 9181738,
"address": "0xD43c70A343E631475d8470d0403e0c4b43c76927"
"blockNumber": 9375734,
"address": "0x7aF55D944285cc6f417Ddfda05393FA18679046d"
},
"CiphernodeRegistry": {
"constructorArgs": {
"enclaveAddress": "0xD43c70A343E631475d8470d0403e0c4b43c76927",
"owner": "0x4f1f3a157073A35515C4fC4A8af2F1Af088f0676"
"enclaveAddress": "0x7aF55D944285cc6f417Ddfda05393FA18679046d",
"owner": "0x8837e47c4Bb520ADE83AAB761C3B60679443af1B"
},
"blockNumber": 9181748,
"address": "0x5ABDfCbA0366ABF2893D3f2465F4C97908488A6d"
"blockNumber": 9375745,
"address": "0x772CED56a1aA2dFBF53e9ad65D2b2De227F7CEBE"
},
"NaiveRegistryFilter": {
"constructorArgs": {
"ciphernodeRegistryAddress": "0x5ABDfCbA0366ABF2893D3f2465F4C97908488A6d",
"owner": "0x4f1f3a157073A35515C4fC4A8af2F1Af088f0676"
"ciphernodeRegistryAddress": "0x772CED56a1aA2dFBF53e9ad65D2b2De227F7CEBE",
"owner": "0x8837e47c4Bb520ADE83AAB761C3B60679443af1B"
},
"blockNumber": 9181753,
"address": "0x58708A1bf1AEdf8e75755FDa1882F8dc46985009"
"blockNumber": 9375774,
"address": "0x6FD987C9CC35bfc5a9Dfc3B89EFd787985C262A5"
},
"MockComputeProvider": {
"blockNumber": 9377000,
"address": "0x473c9ac12C0F8265657741f7300b7C89D927A673"
},
"MockDecryptionVerifier": {
"blockNumber": 9377008,
"address": "0x64962f93cF88B391572D81c4A1eaAA59c6A0D12c"
},
"MockInputValidator": {
"blockNumber": 9377014,
"address": "0x910680dB8F077dd07977B9A9725A485170e5bb8a"
},
"MockE3Program": {
"constructorArgs": {
"mockInputValidator": "0x910680dB8F077dd07977B9A9725A485170e5bb8a"
},
"blockNumber": 9377020,
"address": "0x7e6F82A4367Abd4A8f1C54E38C850b59094DcD9a"
}
}
}
5 changes: 5 additions & 0 deletions packages/enclave-contracts/scripts/deployEnclave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const deployEnclave = async (withMocks?: boolean) => {
const THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30;
const addressOne = "0x0000000000000000000000000000000000000001";

console.log("Deploying Enclave");
const { enclave } = await deployAndSaveEnclave({
params: encoded,
owner: ownerAddress,
Expand All @@ -42,6 +43,7 @@ export const deployEnclave = async (withMocks?: boolean) => {

const enclaveAddress = await enclave.getAddress();

console.log("Deploying CiphernodeRegistry");
const { ciphernodeRegistry } = await deployAndSaveCiphernodeRegistryOwnable({
enclaveAddress: enclaveAddress,
owner: ownerAddress,
Expand All @@ -50,6 +52,7 @@ export const deployEnclave = async (withMocks?: boolean) => {

const ciphernodeRegistryAddress = await ciphernodeRegistry.getAddress();

console.log("Deploying NaiveRegistryFilter");
const { naiveRegistryFilter } = await deployAndSaveNaiveRegistryFilter({
ciphernodeRegistryAddress: ciphernodeRegistryAddress,
owner: ownerAddress,
Expand All @@ -60,6 +63,7 @@ export const deployEnclave = async (withMocks?: boolean) => {

const registryAddress = await enclave.ciphernodeRegistry();

console.log("Setting CiphernodeRegistry in Enclave");
if (registryAddress === ciphernodeRegistryAddress) {
console.log(`Enclave contract already has registry`);
} else {
Expand All @@ -81,6 +85,7 @@ export const deployEnclave = async (withMocks?: boolean) => {
const shouldDeployMocks = process.env.DEPLOY_MOCKS === "true" || withMocks;

if (shouldDeployMocks) {
console.log("Deploying Mocks");
const { decryptionVerifierAddress, e3ProgramAddress } = await deployMocks();

const encryptionSchemeId = ethers.keccak256(
Expand Down
4 changes: 4 additions & 0 deletions packages/enclave-contracts/scripts/deployMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ export interface MockDeployments {
* @returns The addresses of the mock contracts.
*/
export const deployMocks = async (): Promise<MockDeployments> => {
console.log("Deploying Compute Provider");
const { computeProvider } = await deployAndSaveMockComputeProvider(hre);

const computeProviderAddress = await computeProvider.getAddress();

console.log("Deploying Decryption Verifier");
const { decryptionVerifier } = await deployAndSaveMockDecryptionVerifier(hre);

const decryptionVerifierAddress = await decryptionVerifier.getAddress();

console.log("Deploying Input Validator");
const { inputValidator } = await deployAndSaveMockInputValidator(hre);

const inputValidatorAddress = await inputValidator.getAddress();

console.log("Deploying E3 Program");
const { e3Program } = await deployAndSaveMockProgram({
mockInputValidator: inputValidatorAddress,
hre,
Expand Down
Loading