Skip to content

Commit

Permalink
contracts: host enforcing update FuncId, as per task bVPVbQ
Browse files Browse the repository at this point in the history
  • Loading branch information
skoupidi committed Feb 19, 2025
1 parent e44bc17 commit 01cd3b1
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 84 deletions.
6 changes: 2 additions & 4 deletions src/contract/dao/src/entrypoint/auth_xfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use darkfi_sdk::{
pasta::pallas,
ContractCall,
};
use darkfi_serial::{deserialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, Encodable};

use crate::{
error::DaoError,
Expand Down Expand Up @@ -222,7 +222,5 @@ pub(crate) fn dao_authxfer_process_instruction(
// We do not need to check the amounts, since sum(input values) == sum(output values)
// otherwise the money::transfer() call is invalid.

let mut update_data = vec![];
update_data.write_u8(DaoFunction::AuthMoneyTransfer as u8)?;
Ok(update_data)
Ok(vec![])
}
5 changes: 2 additions & 3 deletions src/contract/dao/src/entrypoint/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
blockwindow,
error::DaoError,
model::{DaoExecParams, DaoExecUpdate, DaoProposalMetadata, VecAuthCallCommit},
DaoFunction, DAO_CONTRACT_DB_PROPOSAL_BULLAS, DAO_CONTRACT_ZKAS_DAO_EARLY_EXEC_NS,
DAO_CONTRACT_DB_PROPOSAL_BULLAS, DAO_CONTRACT_ZKAS_DAO_EARLY_EXEC_NS,
DAO_CONTRACT_ZKAS_DAO_EXEC_NS,
};

Expand Down Expand Up @@ -143,7 +143,6 @@ pub(crate) fn dao_exec_process_instruction(
// Create state update
let update = DaoExecUpdate { proposal_bulla: params.proposal_bulla };
let mut update_data = vec![];
update_data.write_u8(DaoFunction::Exec as u8)?;
update.encode(&mut update_data)?;
Ok(update_data)
}
Expand Down
7 changes: 3 additions & 4 deletions src/contract/dao/src/entrypoint/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::DaoError,
model::{DaoMintParams, DaoMintUpdate},
DaoFunction, DAO_CONTRACT_DB_DAO_BULLAS, DAO_CONTRACT_DB_DAO_MERKLE_ROOTS,
DAO_CONTRACT_DB_INFO_TREE, DAO_CONTRACT_KEY_DAO_MERKLE_TREE, DAO_CONTRACT_KEY_LATEST_DAO_ROOT,
DAO_CONTRACT_DB_DAO_BULLAS, DAO_CONTRACT_DB_DAO_MERKLE_ROOTS, DAO_CONTRACT_DB_INFO_TREE,
DAO_CONTRACT_KEY_DAO_MERKLE_TREE, DAO_CONTRACT_KEY_LATEST_DAO_ROOT,
DAO_CONTRACT_ZKAS_DAO_MINT_NS,
};

Expand Down Expand Up @@ -83,7 +83,6 @@ pub(crate) fn dao_mint_process_instruction(
// Create state update
let update = DaoMintUpdate { dao_bulla: params.dao_bulla };
let mut update_data = vec![];
update_data.write_u8(DaoFunction::Mint as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
3 changes: 2 additions & 1 deletion src/contract/dao/src/entrypoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {

/// This function attempts to write a given state update provided the previous
/// steps of the contract call execution were successful. The payload given to
/// the function is the update data retrieved from `process_instruction()`.
/// the function is the update data retrieved from `process_instruction()`,
/// prefixed with the contract function.
fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
match DaoFunction::try_from(update_data[0])? {
DaoFunction::Mint => {
Expand Down
5 changes: 2 additions & 3 deletions src/contract/dao/src/entrypoint/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ use darkfi_sdk::{
tx::TransactionHash,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
blockwindow,
error::DaoError,
model::{DaoBlindAggregateVote, DaoProposalMetadata, DaoProposeParams, DaoProposeUpdate},
DaoFunction, DAO_CONTRACT_DB_DAO_MERKLE_ROOTS, DAO_CONTRACT_DB_PROPOSAL_BULLAS,
DAO_CONTRACT_DB_DAO_MERKLE_ROOTS, DAO_CONTRACT_DB_PROPOSAL_BULLAS,
DAO_CONTRACT_ZKAS_DAO_PROPOSE_INPUT_NS, DAO_CONTRACT_ZKAS_DAO_PROPOSE_MAIN_NS,
PROPOSAL_SNAPSHOT_CUTOFF_LIMIT,
};
Expand Down Expand Up @@ -219,7 +219,6 @@ pub(crate) fn dao_propose_process_instruction(
let update =
DaoProposeUpdate { proposal_bulla: params.proposal_bulla, snapshot_coins, snapshot_nulls };
let mut update_data = vec![];
update_data.write_u8(DaoFunction::Propose as u8)?;
update.encode(&mut update_data)?;
Ok(update_data)
}
Expand Down
5 changes: 2 additions & 3 deletions src/contract/dao/src/entrypoint/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
blockwindow,
error::DaoError,
model::{DaoProposalMetadata, DaoVoteParams, DaoVoteUpdate},
DaoFunction, DAO_CONTRACT_DB_PROPOSAL_BULLAS, DAO_CONTRACT_DB_VOTE_NULLIFIERS,
DAO_CONTRACT_DB_PROPOSAL_BULLAS, DAO_CONTRACT_DB_VOTE_NULLIFIERS,
DAO_CONTRACT_ZKAS_DAO_VOTE_INPUT_NS, DAO_CONTRACT_ZKAS_DAO_VOTE_MAIN_NS,
};

Expand Down Expand Up @@ -170,7 +170,6 @@ pub(crate) fn dao_vote_process_instruction(
DaoVoteUpdate { proposal_bulla: params.proposal_bulla, proposal_metadata, vote_nullifiers };

let mut update_data = vec![];
update_data.write_u8(DaoFunction::Vote as u8)?;
update.encode(&mut update_data)?;
Ok(update_data)
}
Expand Down
3 changes: 2 additions & 1 deletion src/contract/deployooor/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
/// This function attempts to write a given state update provided the previous
/// steps of the contract call execution were all successful. It's the last in
/// line, and assumes that the transaction/call was successful. The payload
/// given to the function is the update data retrieved from `process_instruction()`.
/// given to the function is the update data retrieved from `process_instruction()`,
/// prefixed with the contract function.
fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
match DeployFunction::try_from(update_data[0])? {
DeployFunction::DeployV1 => {
Expand Down
5 changes: 2 additions & 3 deletions src/contract/deployooor/src/entrypoint/deploy_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};
use wasmparser::{
ExternalKind::{Func, Memory},
Payload::ExportSection,
};

use crate::{error::DeployError, model::DeployUpdateV1, DeployFunction, DEPLOY_CONTRACT_LOCK_TREE};
use crate::{error::DeployError, model::DeployUpdateV1, DEPLOY_CONTRACT_LOCK_TREE};

/// `get_metadata` function for `Deploy::DeployV1`
pub(crate) fn deploy_get_metadata_v1(
Expand Down Expand Up @@ -143,7 +143,6 @@ pub(crate) fn deploy_process_instruction_v1(

let update = DeployUpdateV1 { contract_id };
let mut update_data = vec![];
update_data.write_u8(DeployFunction::DeployV1 as u8)?;
update.encode(&mut update_data)?;
Ok(update_data)
}
Expand Down
5 changes: 2 additions & 3 deletions src/contract/deployooor/src/entrypoint/lock_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::DeployError,
model::{LockParamsV1, LockUpdateV1},
DeployFunction, DEPLOY_CONTRACT_LOCK_TREE,
DEPLOY_CONTRACT_LOCK_TREE,
};

/// `get_metadata` function for `Deploy::LockV1`
Expand Down Expand Up @@ -82,7 +82,6 @@ pub(crate) fn lock_process_instruction_v1(

let update = LockUpdateV1 { contract_id };
let mut update_data = vec![];
update_data.write_u8(DeployFunction::LockV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
3 changes: 2 additions & 1 deletion src/contract/money/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
/// This function attempts to write a given state update provided the previous steps
/// of the contract call execution all were successful. It's the last in line, and
/// assumes that the transaction/call was successful. The payload given to the function
/// is the update data retrieved from `process_instruction()`.
/// is the update data retrieved from `process_instruction()`, prefixed with the
/// contract function.
fn process_update(cid: ContractId, update_data: &[u8]) -> ContractResult {
match MoneyFunction::try_from(update_data[0])? {
MoneyFunction::FeeV1 => {
Expand Down
5 changes: 2 additions & 3 deletions src/contract/money/src/entrypoint/auth_token_freeze_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::MoneyError,
model::{MoneyAuthTokenFreezeParamsV1, MoneyAuthTokenFreezeUpdateV1},
MoneyFunction, MONEY_CONTRACT_TOKEN_FREEZE_TREE, MONEY_CONTRACT_ZKAS_AUTH_TOKEN_MINT_NS_V1,
MONEY_CONTRACT_TOKEN_FREEZE_TREE, MONEY_CONTRACT_ZKAS_AUTH_TOKEN_MINT_NS_V1,
};

/// `get_metadata` function for `Money::AuthTokenFreezeV1`
Expand Down Expand Up @@ -84,7 +84,6 @@ pub(crate) fn money_auth_token_freeze_process_instruction_v1(
// Create a state update. We only need the new coin.
let update = MoneyAuthTokenFreezeUpdateV1 { token_id: params.token_id };
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::AuthTokenFreezeV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
5 changes: 2 additions & 3 deletions src/contract/money/src/entrypoint/auth_token_mint_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::MoneyError,
model::{MoneyAuthTokenMintParamsV1, MoneyAuthTokenMintUpdateV1},
MoneyFunction, MONEY_CONTRACT_TOKEN_FREEZE_TREE, MONEY_CONTRACT_ZKAS_AUTH_TOKEN_MINT_NS_V1,
MONEY_CONTRACT_TOKEN_FREEZE_TREE, MONEY_CONTRACT_ZKAS_AUTH_TOKEN_MINT_NS_V1,
};

/// `get_metadata` function for `Money::AuthTokenMintV1`
Expand Down Expand Up @@ -79,7 +79,6 @@ pub(crate) fn money_auth_token_mint_process_instruction_v1(
// Create a state update.
let update = MoneyAuthTokenMintUpdateV1 {};
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::AuthTokenMintV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
12 changes: 5 additions & 7 deletions src/contract/money/src/entrypoint/fee_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::MoneyError,
model::{MoneyFeeParamsV1, MoneyFeeUpdateV1, DARK_TOKEN_ID},
MoneyFunction, MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE,
MONEY_CONTRACT_COIN_ROOTS_TREE, MONEY_CONTRACT_FEES_TREE, MONEY_CONTRACT_INFO_TREE,
MONEY_CONTRACT_LATEST_COIN_ROOT, MONEY_CONTRACT_LATEST_NULLIFIER_ROOT,
MONEY_CONTRACT_NULLIFIERS_TREE, MONEY_CONTRACT_NULLIFIER_ROOTS_TREE,
MONEY_CONTRACT_ZKAS_FEE_NS_V1,
MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE, MONEY_CONTRACT_COIN_ROOTS_TREE,
MONEY_CONTRACT_FEES_TREE, MONEY_CONTRACT_INFO_TREE, MONEY_CONTRACT_LATEST_COIN_ROOT,
MONEY_CONTRACT_LATEST_NULLIFIER_ROOT, MONEY_CONTRACT_NULLIFIERS_TREE,
MONEY_CONTRACT_NULLIFIER_ROOTS_TREE, MONEY_CONTRACT_ZKAS_FEE_NS_V1,
};

/// `get_metadata` function for `Money::FeeV1`
Expand Down Expand Up @@ -191,7 +190,6 @@ pub(crate) fn money_fee_process_instruction_v1(
fee: paid_fee,
};
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::FeeV1 as u8)?;
update.encode(&mut update_data)?;
// and return it
Ok(update_data)
Expand Down
7 changes: 3 additions & 4 deletions src/contract/money/src/entrypoint/genesis_mint_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::MoneyError,
model::{MoneyGenesisMintParamsV1, MoneyGenesisMintUpdateV1, DARK_TOKEN_ID},
MoneyFunction, MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE,
MONEY_CONTRACT_COIN_ROOTS_TREE, MONEY_CONTRACT_INFO_TREE, MONEY_CONTRACT_LATEST_COIN_ROOT,
MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE, MONEY_CONTRACT_COIN_ROOTS_TREE,
MONEY_CONTRACT_INFO_TREE, MONEY_CONTRACT_LATEST_COIN_ROOT,
MONEY_CONTRACT_LATEST_NULLIFIER_ROOT, MONEY_CONTRACT_NULLIFIERS_TREE,
MONEY_CONTRACT_NULLIFIER_ROOTS_TREE, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
};
Expand Down Expand Up @@ -143,7 +143,6 @@ pub(crate) fn money_genesis_mint_process_instruction_v1(
// Create a state update. We only need the new coins.
let update = MoneyGenesisMintUpdateV1 { coins: new_coins };
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::GenesisMintV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
12 changes: 5 additions & 7 deletions src/contract/money/src/entrypoint/pow_reward_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::MoneyError,
model::{MoneyPoWRewardParamsV1, MoneyPoWRewardUpdateV1, DARK_TOKEN_ID},
MoneyFunction, MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE,
MONEY_CONTRACT_COIN_ROOTS_TREE, MONEY_CONTRACT_FEES_TREE, MONEY_CONTRACT_INFO_TREE,
MONEY_CONTRACT_LATEST_COIN_ROOT, MONEY_CONTRACT_LATEST_NULLIFIER_ROOT,
MONEY_CONTRACT_NULLIFIERS_TREE, MONEY_CONTRACT_NULLIFIER_ROOTS_TREE,
MONEY_CONTRACT_ZKAS_MINT_NS_V1,
MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE, MONEY_CONTRACT_COIN_ROOTS_TREE,
MONEY_CONTRACT_FEES_TREE, MONEY_CONTRACT_INFO_TREE, MONEY_CONTRACT_LATEST_COIN_ROOT,
MONEY_CONTRACT_LATEST_NULLIFIER_ROOT, MONEY_CONTRACT_NULLIFIERS_TREE,
MONEY_CONTRACT_NULLIFIER_ROOTS_TREE, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
};

/// `get_metadata` function for `Money::PoWRewardV1`
Expand Down Expand Up @@ -157,7 +156,6 @@ pub(crate) fn money_pow_reward_process_instruction_v1(
let update =
MoneyPoWRewardUpdateV1 { coin: params.output.coin, height: verifying_block_height };
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::PoWRewardV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
9 changes: 2 additions & 7 deletions src/contract/money/src/entrypoint/swap_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use super::transfer_v1::{money_transfer_get_metadata_v1, money_transfer_process_update_v1};
use crate::{
error::MoneyError,
model::{MoneyTransferParamsV1, MoneyTransferUpdateV1},
MoneyFunction, MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_ROOTS_TREE,
MONEY_CONTRACT_NULLIFIERS_TREE,
MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_ROOTS_TREE, MONEY_CONTRACT_NULLIFIERS_TREE,
};

/// `get_metadata` function for `Money::OtcSwapV1`
Expand Down Expand Up @@ -148,12 +147,8 @@ pub(crate) fn money_otcswap_process_instruction_v1(
// Create a state update. We also use `MoneyTransferUpdateV1` because
// they're essentially the same thing, just with a different transition
// ruleset.
// FIXME: The function should not actually be written here. It should
// be prepended by the host to enforce correctness. The host can
// simply copy it from the payload.
let update = MoneyTransferUpdateV1 { nullifiers: new_nullifiers, coins: new_coins };
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::OtcSwapV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
7 changes: 3 additions & 4 deletions src/contract/money/src/entrypoint/token_mint_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use darkfi_sdk::{
pasta::pallas,
wasm, ContractCall,
};
use darkfi_serial::{deserialize, serialize, Encodable, WriteExt};
use darkfi_serial::{deserialize, serialize, Encodable};

use crate::{
error::MoneyError,
model::{MoneyTokenMintParamsV1, MoneyTokenMintUpdateV1},
MoneyFunction, MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE,
MONEY_CONTRACT_COIN_ROOTS_TREE, MONEY_CONTRACT_INFO_TREE, MONEY_CONTRACT_LATEST_COIN_ROOT,
MONEY_CONTRACT_COINS_TREE, MONEY_CONTRACT_COIN_MERKLE_TREE, MONEY_CONTRACT_COIN_ROOTS_TREE,
MONEY_CONTRACT_INFO_TREE, MONEY_CONTRACT_LATEST_COIN_ROOT,
MONEY_CONTRACT_LATEST_NULLIFIER_ROOT, MONEY_CONTRACT_NULLIFIERS_TREE,
MONEY_CONTRACT_NULLIFIER_ROOTS_TREE, MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1,
};
Expand Down Expand Up @@ -101,7 +101,6 @@ pub(crate) fn money_token_mint_process_instruction_v1(
// Create a state update. We only need the new coin.
let update = MoneyTokenMintUpdateV1 { coin: params.coin };
let mut update_data = vec![];
update_data.write_u8(MoneyFunction::TokenMintV1 as u8)?;
update.encode(&mut update_data)?;

Ok(update_data)
Expand Down
Loading

0 comments on commit 01cd3b1

Please # to comment.