Browse Source

runtime: changed call_index from u32 to u8

skoupidi 2 năm trước cách đây
mục cha
commit
d9304c15cd

+ 12 - 12
bin/drk/src/dao.rs

@@ -200,7 +200,7 @@ pub struct Dao {
     /// The transaction hash where the DAO was deployed
     pub tx_hash: Option<blake3::Hash>,
     /// The call index in the transaction where the DAO was deployed
-    pub call_index: Option<u32>,
+    pub call_index: Option<u8>,
 }
 
 impl Dao {
@@ -285,7 +285,7 @@ pub struct DaoProposal {
     /// Transaction hash where this proposal was proposed
     pub tx_hash: Option<blake3::Hash>,
     /// call index in the transaction where this proposal was proposed
-    pub call_index: Option<u32>,
+    pub call_index: Option<u8>,
     /// The vote ID we've voted on this proposal
     pub vote_id: Option<pallas::Base>,
 }
@@ -355,7 +355,7 @@ pub struct DaoVote {
     /// Transaction hash where this vote was casted
     pub tx_hash: Option<blake3::Hash>,
     /// call index in the transaction where this vote was casted
-    pub call_index: Option<u32>,
+    pub call_index: Option<u8>,
 }
 
 impl Drk {
@@ -520,7 +520,7 @@ impl Drk {
             let Value::Integer(call_index) = row[11] else {
                 return Err(Error::ParseFailed("[get_daos] Call index parsing failed"))
             };
-            let Ok(call_index) = u32::try_from(call_index) else {
+            let Ok(call_index) = u8::try_from(call_index) else {
                 return Err(Error::ParseFailed("[get_daos] Call index parsing failed"))
             };
             let call_index = Some(call_index);
@@ -620,7 +620,7 @@ impl Drk {
         let Value::Integer(call_index) = row[9] else {
             return Err(Error::ParseFailed("[get_dao_proposals] Call index parsing failed"))
         };
-        let Ok(call_index) = u32::try_from(call_index) else {
+        let Ok(call_index) = u8::try_from(call_index) else {
             return Err(Error::ParseFailed("[get_dao_proposals] Call index parsing failed"))
         };
         let call_index = Some(call_index);
@@ -693,17 +693,17 @@ impl Drk {
         let (mut daos_tree, mut proposals_tree) = self.get_dao_trees().await?;
 
         // DAOs that have been minted
-        let mut new_dao_bullas: Vec<(DaoBulla, Option<blake3::Hash>, u32)> = vec![];
+        let mut new_dao_bullas: Vec<(DaoBulla, Option<blake3::Hash>, u8)> = vec![];
         // DAO proposals that have been minted
         let mut new_dao_proposals: Vec<(
             DaoProposeParams,
             Option<MerkleTree>,
             Option<blake3::Hash>,
-            u32,
+            u8,
         )> = vec![];
         let mut our_proposals: Vec<DaoProposal> = vec![];
         // DAO votes that have been seen
-        let mut new_dao_votes: Vec<(DaoVoteParams, Option<blake3::Hash>, u32)> = vec![];
+        let mut new_dao_votes: Vec<(DaoVoteParams, Option<blake3::Hash>, u8)> = vec![];
         let mut dao_votes: Vec<DaoVote> = vec![];
 
         // Run through the transaction and see what we got:
@@ -712,7 +712,7 @@ impl Drk {
                 println!("Found Dao::Mint in call {i}");
                 let params: DaoMintParams = deserialize(&call.data.data[1..])?;
                 let tx_hash = if confirm { Some(blake3::hash(&serialize(tx))) } else { None };
-                new_dao_bullas.push((params.dao_bulla, tx_hash, i as u32));
+                new_dao_bullas.push((params.dao_bulla, tx_hash, i as u8));
                 continue
             }
 
@@ -722,7 +722,7 @@ impl Drk {
                 let tx_hash = if confirm { Some(blake3::hash(&serialize(tx))) } else { None };
                 // We need to clone the tree here for reproducing the snapshot Merkle root
                 let money_tree = if confirm { Some(self.get_money_tree().await?) } else { None };
-                new_dao_proposals.push((params, money_tree, tx_hash, i as u32));
+                new_dao_proposals.push((params, money_tree, tx_hash, i as u8));
                 continue
             }
 
@@ -730,7 +730,7 @@ impl Drk {
                 println!("Found Dao::Vote in call {i}");
                 let params: DaoVoteParams = deserialize(&call.data.data[1..])?;
                 let tx_hash = if confirm { Some(blake3::hash(&serialize(tx))) } else { None };
-                new_dao_votes.push((params, tx_hash, i as u32));
+                new_dao_votes.push((params, tx_hash, i as u8));
                 continue
             }
 
@@ -1313,7 +1313,7 @@ impl Drk {
             let Value::Integer(call_index) = row[7] else {
                 return Err(Error::ParseFailed("[get_dao_proposal_votes] Call index parsing failed"))
             };
-            let Ok(call_index) = u32::try_from(call_index) else {
+            let Ok(call_index) = u8::try_from(call_index) else {
                 return Err(Error::ParseFailed("[get_dao_proposal_votes] Call index parsing failed"))
             };
             let call_index = Some(call_index);

+ 23 - 23
doc/src/arch/consensus.md

@@ -225,11 +225,11 @@ used by the protocol.
 Note that for hashes, we define custom types like `TransactionHash`, but here
 we will just use the raw byte representation `[u8; 32]`.
 
-| Index         | Type           | Description                                         |
-|---------------|----------------|-----------------------------------------------------|
-| `block_index` | `u32`          | Block height                                        |
-| `tx_index`    | `u16`          | Index of a tx within a block                        |
-| `call_index`  | `u16`          | Index of contract call within a single tx           |
+| Index         | Type  | Description                               |
+|---------------|-------|-------------------------------------------|
+| `block_index` | `u32` | Block height                              |
+| `tx_index`    | `u16` | Index of a tx within a block              |
+| `call_index`  | `u8`  | Index of contract call within a single tx |
 
 `u32` can store 4.29 billion blocks, which with a 90 second blocktime
 corresponds to 12.2k years.
@@ -239,22 +239,22 @@ the tx in Bitcoin with the most outputs has 2501.
 
 ## Header
 
-| Field       | Type           | Description                                         |
-|-------------|----------------|-----------------------------------------------------|
-| `version`   | `u8`           | Block version                                       |
-| `previous`  | `[u8; 32]`     | Previous block hash                                 |
-| `height`    | `u32`          | Block height                                        |
-| `timestamp` | `u64`          | Block creation timestamp                            |
-| `nonce`     | `u64`          | The block's nonce value                             |
-| `tree_root` | `[u8; 32]`     | Merkle tree root of the block's transactions hashes |
+| Field       | Type       | Description                                         |
+|-------------|------------|-----------------------------------------------------|
+| `version`   | `u8`       | Block version                                       |
+| `previous`  | `[u8; 32]` | Previous block hash                                 |
+| `height`    | `u32`      | Block height                                        |
+| `timestamp` | `u64`      | Block creation timestamp                            |
+| `nonce`     | `u64`      | The block's nonce value                             |
+| `tree_root` | `[u8; 32]` | Merkle tree root of the block's transactions hashes |
 
 ## Block
 
-| Field       | Type                | Description              |
-|-------------|---------------------|--------------------------|
-| `header`    | `[u8; 32]`          | Block header hash        |
-| `txs`       | `Vec<[u8; 32]`      | Transaction hashes       |
-| `signature` | `Signature`         | Block producer signature |
+| Field       | Type           | Description              |
+|-------------|----------------|--------------------------|
+| `header`    | `[u8; 32]`     | Block header hash        |
+| `txs`       | `Vec<[u8; 32]` | Transaction hashes       |
+| `signature` | `Signature`    | Block producer signature |
 
 ## Blockchain
 
@@ -265,11 +265,11 @@ the tx in Bitcoin with the most outputs has 2501.
 
 ## Fork
 
-| Field       | Type                | Description                      |
-|-------------|---------------------|----------------------------------|
-| `chain`     | `Blockchain`        | Forks current blockchain state   |
-| `proposals` | `Vec<[u8; 32]`      | Fork proposal hashes sequence    |
-| `mempool`   | `Vec<[u8; 32]`      | Valid pending transaction hashes |
+| Field       | Type           | Description                      |
+|-------------|----------------|----------------------------------|
+| `chain`     | `Blockchain`   | Forks current blockchain state   |
+| `proposals` | `Vec<[u8; 32]` | Fork proposal hashes sequence    |
+| `mempool`   | `Vec<[u8; 32]` | Valid pending transaction hashes |
 
 ## Validator
 

+ 3 - 3
src/contract/dao/src/entrypoint/auth_xfer.rs

@@ -40,7 +40,7 @@ use crate::{
 /// `get_metdata` function for `Dao::Exec`
 pub(crate) fn dao_authxfer_get_metadata(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
@@ -114,7 +114,7 @@ pub(crate) fn dao_authxfer_get_metadata(
 fn find_auth_in_parent(
     exec_callnode: &DarkLeaf<ContractCall>,
     proposal_auth_calls: Vec<DaoAuthCall>,
-    self_call_idx: u32,
+    self_call_idx: u8,
 ) -> Option<DaoAuthCall> {
     for (auth_call, child_idx) in
         proposal_auth_calls.into_iter().zip(exec_callnode.children_indexes.iter())
@@ -129,7 +129,7 @@ fn find_auth_in_parent(
 /// `process_instruction` function for `Dao::Exec`
 pub(crate) fn dao_authxfer_process_instruction(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let sibling_idx = call_idx + 1;

+ 2 - 2
src/contract/dao/src/entrypoint/exec.rs

@@ -35,7 +35,7 @@ use crate::{
 /// `get_metdata` function for `Dao::Exec`
 pub(crate) fn dao_exec_get_metadata(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
@@ -75,7 +75,7 @@ pub(crate) fn dao_exec_get_metadata(
 /// `process_instruction` function for `Dao::Exec`
 pub(crate) fn dao_exec_process_instruction(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];

+ 2 - 2
src/contract/dao/src/entrypoint/mint.rs

@@ -37,7 +37,7 @@ use crate::{
 /// `get_metadata` function for `Dao::Mint`
 pub(crate) fn dao_mint_get_metadata(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -67,7 +67,7 @@ pub(crate) fn dao_mint_get_metadata(
 /// `process_instruction` function for `Dao::Mint`
 pub(crate) fn dao_mint_process_instruction(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 3 - 3
src/contract/dao/src/entrypoint/propose.rs

@@ -43,7 +43,7 @@ use crate::{
 /// `get_metdata` function for `Dao::Propose`
 pub(crate) fn dao_propose_get_metadata(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -112,7 +112,7 @@ pub(crate) fn dao_propose_get_metadata(
 /// `process_instruction` function for `Dao::Propose`
 pub(crate) fn dao_propose_process_instruction(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -149,7 +149,7 @@ pub(crate) fn dao_propose_process_instruction(
             return Err(DaoError::NonMatchingSnapshotRoots.into())
         }
 
-        assert_eq!(coin_root_data.len(), 32 + 2);
+        assert_eq!(coin_root_data.len(), 32 + 1);
         let tx_hash_data: [u8; 32] = coin_root_data[0..32].try_into().unwrap();
         let tx_hash = TransactionHash(tx_hash_data);
         // Get block_height where tx_hash was confirmed

+ 2 - 2
src/contract/dao/src/entrypoint/vote.rs

@@ -37,7 +37,7 @@ use crate::{
 /// `get_metdata` function for `Dao::Vote`
 pub(crate) fn dao_vote_get_metadata(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -125,7 +125,7 @@ pub(crate) fn dao_vote_get_metadata(
 /// `process_instruction` function for `Dao::Vote`
 pub(crate) fn dao_vote_process_instruction(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/deployooor/src/entrypoint/deploy_v1.rs

@@ -36,7 +36,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
@@ -58,7 +58,7 @@ pub(crate) fn deploy_get_metadata_v1(
 /// `process_instruction` function for `Deploy::DeployV1`
 pub(crate) fn deploy_process_instruction_v1(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];

+ 2 - 2
src/contract/deployooor/src/entrypoint/lock_v1.rs

@@ -35,7 +35,7 @@ use crate::{
 /// `get_metadata` function for `Deploy::LockV1`
 pub(crate) fn lock_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];
@@ -57,7 +57,7 @@ pub(crate) fn lock_get_metadata_v1(
 /// `process_instruction` function for `Deploy::LockV1`
 pub(crate) fn lock_process_instruction_v1(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];

+ 5 - 5
src/contract/money/src/entrypoint.rs

@@ -125,21 +125,21 @@ fn init_contract(cid: ContractId, _ix: &[u8]) -> ContractResult {
 
     let tx_hash = wasm::util::get_tx_hash()?;
     // The max outputs for a tx in BTC is 2501
-    let call_idx = wasm::util::get_call_index()? as u16;
-    let mut roots_value_data = Vec::with_capacity(32 + 2);
+    let call_idx = wasm::util::get_call_index()?;
+    let mut roots_value_data = Vec::with_capacity(32 + 1);
     tx_hash.encode(&mut roots_value_data)?;
     call_idx.encode(&mut roots_value_data)?;
-    assert_eq!(roots_value_data.len(), 32 + 2);
+    assert_eq!(roots_value_data.len(), 32 + 1);
 
     // Set up a database tree to hold Merkle roots of all coin trees
-    // k=root_hash:32, v=(tx_hash:32, call_idx: 2)
+    // k=root_hash:32, v=(tx_hash:32, call_idx: 1)
     if wasm::db::db_lookup(cid, MONEY_CONTRACT_COIN_ROOTS_TREE).is_err() {
         let db_coin_roots = wasm::db::db_init(cid, MONEY_CONTRACT_COIN_ROOTS_TREE)?;
         wasm::db::db_set(db_coin_roots, &serialize(&EMPTY_COINS_TREE_ROOT), &roots_value_data)?;
     }
 
     // Set up a database tree to hold Merkle roots of all nullifier trees
-    // k=root_hash:32, v=(tx_hash:32, call_idx: 2)
+    // k=root_hash:32, v=(tx_hash:32, call_idx: 1)
     if wasm::db::db_lookup(cid, MONEY_CONTRACT_NULLIFIER_ROOTS_TREE).is_err() {
         let db_null_roots = wasm::db::db_init(cid, MONEY_CONTRACT_NULLIFIER_ROOTS_TREE)?;
         wasm::db::db_set(db_null_roots, &serialize(&EMPTY_NODES_FP[0]), &roots_value_data)?;

+ 2 - 2
src/contract/money/src/entrypoint/auth_token_mint_v1.rs

@@ -35,7 +35,7 @@ use crate::{
 /// `get_metadata` function for `Money::AuthTokenMintV1`
 pub(crate) fn money_auth_token_mint_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_node = &calls[call_idx as usize];
@@ -77,7 +77,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/money/src/entrypoint/fee_v1.rs

@@ -47,7 +47,7 @@ use crate::{
 /// `get_metadata` function for `Money::FeeV1`
 pub(crate) fn money_fee_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -93,7 +93,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];

+ 2 - 2
src/contract/money/src/entrypoint/genesis_mint_v1.rs

@@ -38,7 +38,7 @@ use crate::{
 /// `get_metadata` function for `Money::GenesisMintV1`
 pub(crate) fn money_genesis_mint_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -73,7 +73,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/money/src/entrypoint/pow_reward_v1.rs

@@ -39,7 +39,7 @@ use crate::{
 /// `get_metadata` function for `Money::PoWRewardV1`
 pub(crate) fn money_pow_reward_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -74,7 +74,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/money/src/entrypoint/swap_v1.rs

@@ -44,7 +44,7 @@ use crate::{
 /// `get_metadata` function for `Money::OtcSwapV1`
 pub(crate) fn money_otcswap_get_metadata_v1(
     cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     // In here we can use the same function as we use in `TransferV1`.
@@ -54,7 +54,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/money/src/entrypoint/token_freeze_v1.rs

@@ -35,7 +35,7 @@ use crate::{
 /// `get_metadata` function for `Money::TokenFreezeV1`
 pub(crate) fn money_token_freeze_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -66,7 +66,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/money/src/entrypoint/token_mint_v1.rs

@@ -38,7 +38,7 @@ use crate::{
 /// `get_metadata` function for `Money::TokenMintV1`
 pub(crate) fn money_token_mint_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -73,7 +73,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;

+ 2 - 2
src/contract/money/src/entrypoint/transfer_v1.rs

@@ -46,7 +46,7 @@ use crate::{
 /// `get_metadata` function for `Money::TransferV1`
 pub(crate) fn money_transfer_get_metadata_v1(
     _cid: ContractId,
-    call_idx: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize].data;
@@ -117,7 +117,7 @@ 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: u32,
+    call_idx: u8,
     calls: Vec<DarkLeaf<ContractCall>>,
 ) -> Result<Vec<u8>, ContractError> {
     let self_ = &calls[call_idx as usize];

+ 1 - 1
src/contract/test-harness/src/lib.rs

@@ -317,7 +317,7 @@ fn benchmark_wasm_calls(
             call.data.contract_id,
             block_height,
             tx.hash(),
-            idx as u32,
+            idx as u8,
         )
         .expect("runtime");
 

+ 3 - 3
src/runtime/import/merkle.rs

@@ -272,10 +272,10 @@ pub(crate) fn merkle_add(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u3
     let latest_root_data = serialize(&latest_root);
     assert_eq!(latest_root_data.len(), 32);
 
-    let mut value_data = Vec::with_capacity(32 + 2);
+    let mut value_data = Vec::with_capacity(32 + 1);
     env.tx_hash.inner().encode(&mut value_data).expect("Unable to serialize tx_hash");
-    (env.call_idx as u16).encode(&mut value_data).expect("Unable to serialize call_idx");
-    assert_eq!(value_data.len(), 32 + 2);
+    env.call_idx.encode(&mut value_data).expect("Unable to serialize call_idx");
+    assert_eq!(value_data.len(), 32 + 1);
 
     if overlay.insert(&db_roots.tree, &latest_root_data, &value_data).is_err() {
         error!(

+ 3 - 3
src/runtime/import/smt.rs

@@ -272,10 +272,10 @@ pub(crate) fn sparse_merkle_insert_batch(
     let latest_root_data = serialize(&latest_root);
     assert_eq!(latest_root_data.len(), 32);
 
-    let mut value_data = Vec::with_capacity(32 + 2);
+    let mut value_data = Vec::with_capacity(32 + 1);
     env.tx_hash.inner().encode(&mut value_data).expect("Unable to serialize tx_hash");
-    (env.call_idx as u16).encode(&mut value_data).expect("Unable to serialize call_idx");
-    assert_eq!(value_data.len(), 32 + 2);
+    env.call_idx.encode(&mut value_data).expect("Unable to serialize call_idx");
+    assert_eq!(value_data.len(), 32 + 1);
 
     if overlay.insert(&db_roots.tree, &latest_root_data, &value_data).is_err() {
         error!(

+ 2 - 2
src/runtime/import/util.rs

@@ -264,8 +264,8 @@ pub(crate) fn get_call_index(mut ctx: FunctionEnvMut<Env>) -> i64 {
     }
 
     // Subtract used gas. Here we count the size of the object.
-    // u32 is 4 bytes.
-    env.subtract_gas(&mut store, 4);
+    // u8 is 1 byte.
+    env.subtract_gas(&mut store, 1);
 
     env.call_idx as i64
 }

+ 2 - 2
src/runtime/vm_runtime.rs

@@ -100,7 +100,7 @@ pub struct Env {
     /// The hash for this transaction the runtime is being run against.
     pub tx_hash: TransactionHash,
     /// The index for this call in the transaction
-    pub call_idx: u32,
+    pub call_idx: u8,
     /// Parent `Instance`
     pub instance: Option<Arc<Instance>>,
 }
@@ -157,7 +157,7 @@ impl Runtime {
         contract_id: ContractId,
         verifying_block_height: u64,
         tx_hash: TransactionHash,
-        call_idx: u32,
+        call_idx: u8,
     ) -> Result<Self> {
         info!(target: "runtime::vm_runtime", "[WASM] Instantiating a new runtime");
         // This function will be called for each `Operator` encountered during

+ 2 - 2
src/sdk/src/wasm/merkle.rs

@@ -45,7 +45,7 @@ use crate::{
 ///
 /// Inside `db_roots` we store:
 ///
-/// * All [merkle root:32]s as keys. The value is the current [tx_hash:32][call_idx:2].
+/// * All [merkle root:32]s as keys. The value is the current [tx_hash:32][call_idx:1].
 ///   If no new values are added, then the root key is updated to the current (tx_hash, call_idx).
 pub fn merkle_add(
     db_info: DbHandle,
@@ -89,7 +89,7 @@ pub fn merkle_add(
 ///
 /// Inside `db_roots` we store:
 ///
-/// * All [merkle root:32]s as keys. The value is the current [tx_hash:32][call_idx:2].
+/// * All [merkle root:32]s as keys. The value is the current [tx_hash:32][call_idx:1].
 ///   If no new values are added, then the root key is updated to the current (tx_hash, call_idx).
 pub fn sparse_merkle_insert_batch(
     db_info: DbHandle,

+ 8 - 2
src/sdk/src/wasm/util.rs

@@ -119,9 +119,15 @@ pub fn get_tx_hash() -> GenericResult<TransactionHash> {
 /// ```
 /// call_idx = get_call_index();
 /// ```
-pub fn get_call_index() -> GenericResult<u32> {
+pub fn get_call_index() -> GenericResult<u8> {
     let ret = unsafe { get_call_index_() };
-    parse_retval_u32(ret)
+    if ret < 0 {
+        return Err(ContractError::from(ret))
+    }
+    assert!(ret >= 0);
+    // This should always be possible
+    let obj = ret as u8;
+    Ok(obj)
 }
 
 /// Everyone can call this. Will return current blockchain timestamp.

+ 1 - 1
src/validator/utils.rs

@@ -90,7 +90,7 @@ pub async fn deploy_native_contracts(overlay: &BlockchainOverlayPtr) -> Result<(
             nc.1,
             verifying_block_height,
             TransactionHash::none(),
-            call_idx as u32,
+            call_idx as u8,
         )?;
 
         runtime.deploy(&nc.3)?;

+ 2 - 2
src/validator/verification.rs

@@ -452,7 +452,7 @@ pub async fn verify_transaction(
             call.data.contract_id,
             verifying_block_height,
             tx_hash,
-            idx as u32,
+            idx as u8,
         )?;
 
         debug!(target: "validator::verification::verify_transaction", "Executing \"metadata\" call");
@@ -527,7 +527,7 @@ pub async fn verify_transaction(
                 deploy_cid,
                 verifying_block_height,
                 tx_hash,
-                idx as u32,
+                idx as u8,
             )?;
 
             deploy_runtime.deploy(&deploy_params.ix)?;