contracts: simplyfied call index usize usage

This commit is contained in:
skoupidi
2024-04-08 12:10:22 +03:00
parent 509d9bf0d4
commit 1a0f997f28
18 changed files with 76 additions and 76 deletions

View File

@@ -40,17 +40,17 @@ use crate::{
/// `get_metdata` function for `Dao::Exec`
pub(crate) fn dao_authxfer_get_metadata(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let self_params: DaoAuthMoneyTransferParams = deserialize(&self_.data.data[1..])?;
let sibling_idx = call_idx + 1;
let xfer_call = &calls[sibling_idx as usize].data;
let xfer_call = &calls[sibling_idx].data;
let xfer_params: MoneyTransferParamsV1 = deserialize(&xfer_call.data[1..])?;
let parent_idx = calls[call_idx as usize].parent_index.unwrap();
let parent_idx = calls[call_idx].parent_index.unwrap();
let exec_callnode = &calls[parent_idx];
let exec_params: DaoExecParams = deserialize(&exec_callnode.data.data[1..])?;
@@ -114,12 +114,12 @@ pub(crate) fn dao_authxfer_get_metadata(
fn find_auth_in_parent(
exec_callnode: &DarkLeaf<ContractCall>,
proposal_auth_calls: Vec<DaoAuthCall>,
self_call_idx: u8,
self_call_idx: usize,
) -> Option<DaoAuthCall> {
for (auth_call, child_idx) in
proposal_auth_calls.into_iter().zip(exec_callnode.children_indexes.iter())
{
if *child_idx == self_call_idx as usize {
if *child_idx == self_call_idx {
return Some(auth_call)
}
}
@@ -129,11 +129,11 @@ fn find_auth_in_parent(
/// `process_instruction` function for `Dao::Exec`
pub(crate) fn dao_authxfer_process_instruction(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let sibling_idx = call_idx + 1;
let xfer_call = &calls[sibling_idx as usize].data;
let xfer_call = &calls[sibling_idx].data;
///////////////////////////////////////////////////
// 1. Next call should be money transfer
@@ -173,7 +173,7 @@ pub(crate) fn dao_authxfer_process_instruction(
///////////////////////////////////////////////////
// Find this auth_call in the parent DAO::exec()
let parent_idx = calls[call_idx as usize].parent_index.unwrap();
let parent_idx = calls[call_idx].parent_index.unwrap();
let exec_callnode = &calls[parent_idx];
let exec_params: DaoExecParams = deserialize(&exec_callnode.data.data[1..])?;

View File

@@ -35,10 +35,10 @@ use crate::{
/// `get_metdata` function for `Dao::Exec`
pub(crate) fn dao_exec_get_metadata(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: DaoExecParams = deserialize(&self_.data.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -75,10 +75,10 @@ pub(crate) fn dao_exec_get_metadata(
/// `process_instruction` function for `Dao::Exec`
pub(crate) fn dao_exec_process_instruction(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: DaoExecParams = deserialize(&self_.data.data[1..])?;
///////////////////////////////////////////////////

View File

@@ -37,10 +37,10 @@ use crate::{
/// `get_metadata` function for `Dao::Mint`
pub(crate) fn dao_mint_get_metadata(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: DaoMintParams = deserialize(&self_.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -67,10 +67,10 @@ pub(crate) fn dao_mint_get_metadata(
/// `process_instruction` function for `Dao::Mint`
pub(crate) fn dao_mint_process_instruction(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: DaoMintParams = deserialize(&self_.data[1..])?;
// Check the DAO bulla doesn't already exist

View File

@@ -143,9 +143,9 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
/// for verifying signatures and ZK proofs. The payload given here are all the
/// contract calls in the transaction.
fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()?;
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let func = DaoFunction::try_from(self_.data[0])?;
let metadata = match func {
@@ -162,9 +162,9 @@ fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
/// This function verifies a state transition and produces a state update
/// if everything is successful.
fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()?;
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let func = DaoFunction::try_from(self_.data[0])?;
let update_data = match func {

View File

@@ -43,10 +43,10 @@ use crate::{
/// `get_metdata` function for `Dao::Propose`
pub(crate) fn dao_propose_get_metadata(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: DaoProposeParams = deserialize(&self_.data[1..])?;
if params.inputs.is_empty() {
@@ -112,10 +112,10 @@ pub(crate) fn dao_propose_get_metadata(
/// `process_instruction` function for `Dao::Propose`
pub(crate) fn dao_propose_process_instruction(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: DaoProposeParams = deserialize(&self_.data[1..])?;
let coin_roots_db = wasm::db::db_lookup(*MONEY_CONTRACT_ID, MONEY_CONTRACT_COIN_ROOTS_TREE)?;

View File

@@ -37,10 +37,10 @@ use crate::{
/// `get_metdata` function for `Dao::Vote`
pub(crate) fn dao_vote_get_metadata(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: DaoVoteParams = deserialize(&self_.data[1..])?;
if params.inputs.is_empty() {
@@ -125,10 +125,10 @@ pub(crate) fn dao_vote_get_metadata(
/// `process_instruction` function for `Dao::Vote`
pub(crate) fn dao_vote_process_instruction(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: DaoVoteParams = deserialize(&self_.data[1..])?;
// Check proposal bulla exists

View File

@@ -68,9 +68,9 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
/// for verifying signatures and zk proofs. The payload given here are all the
/// contract calls in the transaction.
fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()?;
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let func = DeployFunction::try_from(self_.data[0])?;
let metadata = match func {
@@ -84,9 +84,9 @@ fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
/// This function verifies a state transition and produces a state update
/// if everything is successful.
fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()?;
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let func = DeployFunction::try_from(self_.data[0])?;
let update_data = match func {

View File

@@ -36,10 +36,10 @@ use crate::{error::DeployError, model::DeployUpdateV1, DeployFunction, DEPLOY_CO
/// `get_metadata` function for `Deploy::DeployV1`
pub(crate) fn deploy_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: DeployParamsV1 = deserialize(&self_.data.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -58,10 +58,10 @@ pub(crate) fn deploy_get_metadata_v1(
/// `process_instruction` function for `Deploy::DeployV1`
pub(crate) fn deploy_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: DeployParamsV1 = deserialize(&self_.data.data[1..])?;
// In this function, we have to check that the contract isn't locked.

View File

@@ -35,10 +35,10 @@ use crate::{
/// `get_metadata` function for `Deploy::LockV1`
pub(crate) fn lock_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: LockParamsV1 = deserialize(&self_.data.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -57,10 +57,10 @@ pub(crate) fn lock_get_metadata_v1(
/// `process_instruction` function for `Deploy::LockV1`
pub(crate) fn lock_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: LockParamsV1 = deserialize(&self_.data.data[1..])?;
// In this function, we check that the contract exists, and that it isn't

View File

@@ -209,9 +209,9 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
/// for verifying signatures and zk proofs. The payload given here are all the
/// contract calls in the transaction.
fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()?;
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let func = MoneyFunction::try_from(self_.data[0])?;
let metadata = match func {
@@ -240,9 +240,9 @@ fn get_metadata(cid: ContractId, ix: &[u8]) -> ContractResult {
/// if everything is successful. This step should happen **after** the host
/// has successfully verified the metadata from `get_metadata()`.
fn process_instruction(cid: ContractId, ix: &[u8]) -> ContractResult {
let call_idx = wasm::util::get_call_index()?;
let call_idx = wasm::util::get_call_index()? as usize;
let calls: Vec<DarkLeaf<ContractCall>> = deserialize(ix)?;
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let func = MoneyFunction::try_from(self_.data[0])?;
let update_data = match func {

View File

@@ -35,10 +35,10 @@ use crate::{
/// `get_metadata` function for `Money::AuthTokenMintV1`
pub(crate) fn money_auth_token_mint_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_node = &calls[call_idx as usize];
let self_node = &calls[call_idx];
let self_data = &self_node.data;
let self_params: MoneyAuthTokenMintParamsV1 = deserialize(&self_data.data[1..])?;
@@ -77,10 +77,10 @@ pub(crate) fn money_auth_token_mint_get_metadata_v1(
/// `process_instruction` function for `Money::AuthTokenMintV1`
pub(crate) fn money_auth_token_mint_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyAuthTokenMintParamsV1 = deserialize(&self_.data[1..])?;
// We have to check if the token mint is frozen.

View File

@@ -47,10 +47,10 @@ use crate::{
/// `get_metadata` function for `Money::FeeV1`
pub(crate) fn money_fee_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
// The first 8 bytes here is the u64 fee, so we get the params from that offset.
// (Plus 1, which is the function identifier byte)
let params: MoneyFeeParamsV1 = deserialize(&self_.data[9..])?;
@@ -93,10 +93,10 @@ pub(crate) fn money_fee_get_metadata_v1(
/// `process_instruction` function for `Money::FeeV1`
pub(crate) fn money_fee_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let fee: u64 = deserialize(&self_.data.data[1..9])?;
let params: MoneyFeeParamsV1 = deserialize(&self_.data.data[9..])?;

View File

@@ -38,10 +38,10 @@ use crate::{
/// `get_metadata` function for `Money::GenesisMintV1`
pub(crate) fn money_genesis_mint_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyGenesisMintParamsV1 = deserialize(&self_.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -73,10 +73,10 @@ pub(crate) fn money_genesis_mint_get_metadata_v1(
/// `process_instruction` function for `Money::GenesisMintV1`
pub(crate) fn money_genesis_mint_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyGenesisMintParamsV1 = deserialize(&self_.data[1..])?;
// Verify this contract call is verified against genesis block(0).

View File

@@ -39,10 +39,10 @@ use crate::{
/// `get_metadata` function for `Money::PoWRewardV1`
pub(crate) fn money_pow_reward_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyPoWRewardParamsV1 = deserialize(&self_.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -74,10 +74,10 @@ pub(crate) fn money_pow_reward_get_metadata_v1(
/// `process_instruction` function for `Money::PoWRewardV1`
pub(crate) fn money_pow_reward_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyPoWRewardParamsV1 = deserialize(&self_.data[1..])?;
// Verify this contract call is not verified against genesis block

View File

@@ -44,7 +44,7 @@ use crate::{
/// `get_metadata` function for `Money::OtcSwapV1`
pub(crate) fn money_otcswap_get_metadata_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
// In here we can use the same function as we use in `TransferV1`.
@@ -54,10 +54,10 @@ pub(crate) fn money_otcswap_get_metadata_v1(
/// `process_instruction` function for `Money::OtcSwapV1`
pub(crate) fn money_otcswap_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyTransferParamsV1 = deserialize(&self_.data[1..])?;
// The atomic swap is able to use the same parameters as `TransferV1`.

View File

@@ -35,10 +35,10 @@ use crate::{
/// `get_metadata` function for `Money::TokenFreezeV1`
pub(crate) fn money_token_freeze_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyTokenFreezeParamsV1 = deserialize(&self_.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -66,10 +66,10 @@ pub(crate) fn money_token_freeze_get_metadata_v1(
/// `process_instruction` function for `Money::TokenFreezeV1`
pub(crate) fn money_token_freeze_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyTokenFreezeParamsV1 = deserialize(&self_.data[1..])?;
// We just check if the mint was already frozen beforehand

View File

@@ -38,10 +38,10 @@ use crate::{
/// `get_metadata` function for `Money::TokenMintV1`
pub(crate) fn money_token_mint_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyTokenMintParamsV1 = deserialize(&self_.data[1..])?;
let parent_idx = calls[call_idx as usize].parent_index.unwrap();
@@ -73,10 +73,10 @@ pub(crate) fn money_token_mint_get_metadata_v1(
/// `process_instruction` function for `Money::TokenMintV1`
pub(crate) fn money_token_mint_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyTokenMintParamsV1 = deserialize(&self_.data[1..])?;
// We have to check if the token mint is frozen, and if by some chance

View File

@@ -46,10 +46,10 @@ use crate::{
/// `get_metadata` function for `Money::TransferV1`
pub(crate) fn money_transfer_get_metadata_v1(
_cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize].data;
let self_ = &calls[call_idx].data;
let params: MoneyTransferParamsV1 = deserialize(&self_.data[1..])?;
// Public inputs for the ZK proofs we have to verify
@@ -117,10 +117,10 @@ pub(crate) fn money_transfer_get_metadata_v1(
/// `process_instruction` function for `Money::TransferV1`
pub(crate) fn money_transfer_process_instruction_v1(
cid: ContractId,
call_idx: u8,
call_idx: usize,
calls: Vec<DarkLeaf<ContractCall>>,
) -> Result<Vec<u8>, ContractError> {
let self_ = &calls[call_idx as usize];
let self_ = &calls[call_idx];
let params: MoneyTransferParamsV1 = deserialize(&self_.data.data[1..])?;
if params.inputs.is_empty() {