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

✨ Allow deleting contracts #800

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion crates/client-sdk/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub mod test {
success: true,
tx_hash: contract_input.tx_hash.clone(),
tx_ctx: None,
registered_contracts: vec![],
onchain_effects: vec![],
program_outputs: vec![],
};
Ok(hyle_output)
Expand Down
2 changes: 1 addition & 1 deletion crates/contract-sdk/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn fail(input: ContractInput, initial_state_digest: StateDigest, message: &s
success: false,
tx_hash: input.tx_hash,
tx_ctx: input.tx_ctx,
registered_contracts: vec![],
onchain_effects: vec![],
program_outputs: message.to_string().into_bytes(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/contract-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! info {
}
}

pub type RunResult = Result<(String, ExecutionContext, Vec<RegisterContractEffect>), String>;
pub type RunResult = Result<(String, ExecutionContext, Vec<OnchainEffect>), String>;

pub trait HyleContract {
fn execute(&mut self, contract_input: &ContractInput) -> RunResult;
Expand Down
4 changes: 2 additions & 2 deletions crates/contract-sdk/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn as_hyle_output<State: Digestable + BorshDeserialize>(
res: &mut crate::RunResult,
) -> HyleOutput {
match res {
Ok((ref mut program_output, execution_context, ref mut registered_contracts)) => {
Ok((ref mut program_output, execution_context, ref mut onchain_effects)) => {
if !execution_context.callees_blobs.is_empty() {
return fail(
contract_input,
Expand All @@ -119,7 +119,7 @@ pub fn as_hyle_output<State: Digestable + BorshDeserialize>(
success: true,
tx_hash: contract_input.tx_hash,
tx_ctx: contract_input.tx_ctx,
registered_contracts: core::mem::take(registered_contracts),
onchain_effects: core::mem::take(onchain_effects),
program_outputs: core::mem::take(program_output).into_bytes(),
}
}
Expand Down
Binary file modified crates/contracts/amm/amm.img
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/contracts/amm/amm.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22ac110f3f834c8280eb889e6cdfc42603e1e02d365e6d3d6c6c599d0ea3c9f3
a7e2b0a643d4b9bcaf6150b6150727c99e252770a22c99489addd4c2ede9a1ac
Binary file modified crates/contracts/hydentity/hydentity.img
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/contracts/hydentity/hydentity.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0eebe0bf4ec1f41e04657ce85ebada2db7b794b58cae8ede5d4f5cbb0ee7ea52
fb7fcd711f9daa9d38f7009c3dcc8a872fd65d4255252be404a94a47aa58c3c6
Binary file modified crates/contracts/hyllar/hyllar.img
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/contracts/hyllar/hyllar.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a9a8e1c923319fd8294d588a8292cae29d64bc803a3e4154f6178ee49b1c187f
88d35bcce3c7b8bdef7093f6c24182a377b9491a82578ea336c26ea42f4b1f18
Binary file modified crates/contracts/risc0-recursion/risc0-recursion.img
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/contracts/risc0-recursion/risc0-recursion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e5e79abef98ee0f3efacd9442f1c20890cced17a25ecf210f052ffcb065dca36
55ebfdcda965221c5d576841b6fa38d8582e8fcad4c5183ee31ed4874a32ef9b
Binary file modified crates/contracts/staking/staking.img
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/contracts/staking/staking.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9151ca0f22f0513e1f2f0c5bbe4b81b72fbc7252dcdbb060c820fcff8c55c521
1ec82aa7092fc5be42561dbdc4a71f1708bca82fa414e77fef2a42e1608b1ea0
21 changes: 12 additions & 9 deletions crates/contracts/uuid-tld/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rand::Rng;
use rand_seeder::SipHasher;
use sdk::{
info, utils::parse_raw_contract_input, Blob, BlobData, BlobIndex, ContractAction,
ContractInput, ContractName, Digestable, HyleContract, ProgramId, RegisterContractEffect,
RunResult, StateDigest, Verifier,
ContractInput, ContractName, Digestable, HyleContract, OnchainEffect, ProgramId,
RegisterContractEffect, RunResult, StateDigest, Verifier,
};
use uuid::Uuid;

Expand Down Expand Up @@ -57,7 +57,7 @@ impl HyleContract for UuidTld {
Ok((
format!("registered {}", id.clone()),
exec_ctx,
vec![RegisterContractEffect {
vec![OnchainEffect::RegisterContract(RegisterContractEffect {
contract_name: format!(
"{}.{}",
id, contract_input.blobs[contract_input.index.0].contract_name.0
Expand All @@ -66,7 +66,7 @@ impl HyleContract for UuidTld {
verifier: action.verifier,
program_id: action.program_id,
state_digest: action.state_digest,
}],
})],
))
}
}
Expand Down Expand Up @@ -158,20 +158,23 @@ mod test {

let contract_input = make_contract_input(action.clone(), borsh::to_vec(&state).unwrap());

let (_, _, registered_contracts) = state.execute(&contract_input).unwrap();

let effect: &RegisterContractEffect = registered_contracts.first().unwrap();
let (_, _, onchain_effects) = state.execute(&contract_input).unwrap();

let OnchainEffect::RegisterContract(effect) = onchain_effects.first().unwrap() else {
panic!("Expected RegisterContract effect");
};
assert_eq!(
effect.contract_name.0,
"7de07efe-e91d-45f7-a5d2-0b813c1d3e10.uuid"
);

let contract_input = make_contract_input(action.clone(), borsh::to_vec(&state).unwrap());

let (_, _, registered_contracts) = state.execute(&contract_input).unwrap();
let (_, _, onchain_effects) = state.execute(&contract_input).unwrap();

let effect = registered_contracts.first().unwrap();
let OnchainEffect::RegisterContract(effect) = onchain_effects.first().unwrap() else {
panic!("Expected RegisterContract effect");
};

assert_eq!(
effect.contract_name.0,
Expand Down
Binary file modified crates/contracts/uuid-tld/uuid-tld.img
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/contracts/uuid-tld/uuid-tld.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13e4c348917b5d83138b71aee0eef86a182284245b9cf0b764fba29eba552ce6
8cfdb6ed48c6c61883ea31c5e1f22745f7575e35985a188edc8284d9f2590af7
1 change: 1 addition & 0 deletions crates/hyle-model/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Block {
pub new_bounded_validators: Vec<ValidatorPublicKey>,
pub staking_actions: Vec<(Identity, StakingAction)>,
pub registered_contracts: Vec<(TxHash, RegisterContractEffect)>,
pub deleted_contracts: Vec<(TxHash, ContractName)>,
pub updated_states: BTreeMap<ContractName, StateDigest>,
pub transactions_events: BTreeMap<TxHash, Vec<TransactionStateEvent>>,
}
Expand Down
40 changes: 39 additions & 1 deletion crates/hyle-model/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ pub struct Verifier(pub String);
#[cfg_attr(feature = "full", derive(utoipa::ToSchema))]
pub struct ProgramId(pub Vec<u8>);

#[derive(
Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize,
)]
#[cfg_attr(feature = "full", derive(utoipa::ToSchema))]
/// Enum for various side-effects blobs can have on the chain.
/// This is implemented as an enum for easier forward compatibility.
pub enum OnchainEffect {
RegisterContract(RegisterContractEffect),
DeleteContract(ContractName),
}

#[derive(
Default,
Serialize,
Expand All @@ -342,7 +353,7 @@ pub struct HyleOutput {
// Optional - if empty, these won't be checked, but also can't be used inside the program.
pub tx_ctx: Option<TxContext>,

pub registered_contracts: Vec<RegisterContractEffect>,
pub onchain_effects: Vec<OnchainEffect>,

pub program_outputs: Vec<u8>,
}
Expand Down Expand Up @@ -507,6 +518,7 @@ impl Add<BlockHeight> for BlockHeight {
#[derive(
Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize,
)]
/// Used as a blob action to register a contract in the 'hyle' TLD.
pub struct RegisterContractAction {
pub verifier: Verifier,
pub program_id: ProgramId,
Expand Down Expand Up @@ -547,6 +559,32 @@ impl ContractAction for RegisterContractAction {
}
}

#[derive(
Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize,
)]
/// Used as a blob action to delete a contract in the 'hyle' TLD.
pub struct DeleteContractAction {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

un bout de RustDoc sur cette struct serait bien, pour avoir qqpart l'explication de comment on l'utilise / comment les "droits de suppression" sont gérés (s'ils le sont).

Peut-être de même avoir un bout de doc qqpart sur le register de contract pour expliquer comment on update un contrat ? (ok pour faire ça dans une autre PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En soit ça c'est purement la spec du contrat hyle TLD, c'est réutilisable par d'autres mais pas forcément lié à comment l'utiliser.
Je vais quand même rajouter un peu de doc.

pub contract_name: ContractName,
}

impl ContractAction for DeleteContractAction {
fn as_blob(
&self,
contract_name: ContractName,
caller: Option<BlobIndex>,
callees: Option<Vec<BlobIndex>>,
) -> Blob {
Blob {
contract_name,
data: BlobData::from(StructuredBlobData {
caller,
callees,
parameters: self.clone(),
}),
}
}
}

/// Used by the Hylé node to recognize contract registration.
/// Simply output this struct in your HyleOutput registered_contracts.
/// See uuid-tld for examples.
Expand Down
9 changes: 5 additions & 4 deletions crates/hyle-model/src/node/data_availability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ impl Hashed<HyleOutputHash> for HyleOutput {
hasher.update(self.index.0.to_le_bytes());
hasher.update(&self.blobs);
hasher.update([self.success as u8]);
hasher.update(self.registered_contracts.len().to_le_bytes());
self.registered_contracts
.iter()
.for_each(|c| hasher.update(contract::Hashed::hashed(c).0));
hasher.update(self.onchain_effects.len().to_le_bytes());
self.onchain_effects.iter().for_each(|c| match c {
OnchainEffect::RegisterContract(c) => hasher.update(contract::Hashed::hashed(c).0),
OnchainEffect::DeleteContract(cn) => hasher.update(cn.0.as_bytes()),
});
hasher.update(&self.program_outputs);
HyleOutputHash(hasher.finalize().to_vec())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hyle-verifiers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
success: true,
tx_hash: TxHash::default(), // TODO
tx_ctx: None,
registered_contracts: vec![],
onchain_effects: vec![],
program_outputs: vec![]
}]
);
Expand Down
2 changes: 1 addition & 1 deletion crates/hyle-verifiers/src/noir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn parse_noir_output(vector: &mut Vec<String>) -> Result<HyleOutput, Error>
index: BlobIndex(index as usize),
blobs,
success,
registered_contracts: vec![],
onchain_effects: vec![],
program_outputs: vec![],
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ mod test {
index: blob_index,
blobs,
success: true,
registered_contracts: vec![],
onchain_effects: vec![],
program_outputs: vec![],
},
}],
Expand Down
2 changes: 1 addition & 1 deletion src/mempool/verifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn verify_native(
success,
tx_hash,
tx_ctx: None,
registered_contracts: vec![],
onchain_effects: vec![],
program_outputs: vec![],
}
}
Expand Down
Loading
Loading