diff --git a/contracts/transfer/src/state.rs b/contracts/transfer/src/state.rs index 8f9a3103f6..ad248434c5 100644 --- a/contracts/transfer/src/state.rs +++ b/contracts/transfer/src/state.rs @@ -47,17 +47,12 @@ impl TransferState { /// Mint a new phoenix note. /// - /// This can only be called by the transfer- and stake-contracts. - /// If called by the `stake-contract`, this method will increase the total - /// amount of circulating dusk. This happens when the reward for staking - /// and participating in the consensus is withdrawn. - /// If called by the transfer-contract itself, it is important to make sure - /// that the minted value is subtracted from a contracts balance before - /// creating a phoenix-note. - pub fn mint(&mut self, mint: Mint) -> bool { - // why return bool? + /// This can only be called by the stake-contracts. The method will increase + /// the total amount of circulating dusk. This happens when the reward for + /// staking and participating in the consensus is withdrawn. + pub fn mint(&mut self, mint: Mint) { let caller = rusk_abi::caller(); - if caller != STAKE_CONTRACT && !rusk_abi::caller().is_uninitialized() { + if caller != STAKE_CONTRACT { panic!("Can only be called by the stake contract!") } let sender = SenderAccount { @@ -68,8 +63,6 @@ impl TransferState { let note = Note::transparent_stealth(mint.address, mint.value, sender); self.push_note_current_height(note); - - true } /// Withdraw from a contract's balance into a phoenix-note. @@ -213,12 +206,15 @@ impl TransferState { } /// Refund the previously performed transaction, taking into account the - /// given gas spent. The notes produced will be refunded to the address - /// present in the fee structure. + /// given gas spent and a potential deposit that hasn't been picked up by + /// the contract. The note produced will be refunded to the address present + /// in the fee structure. /// /// This function guarantees that it will not panic. pub fn refund(&mut self, fee: Fee, gas_spent: u64) { - let remainder_note = fee.gen_remainder_note(gas_spent); + let deposit = self.deposit.map(|(_, d)| d); + + let remainder_note = fee.gen_remainder_note(gas_spent, deposit); let remainder_value = remainder_note .value(None) diff --git a/contracts/transfer/src/tree.rs b/contracts/transfer/src/tree.rs index 605f65832d..358bca88b2 100644 --- a/contracts/transfer/src/tree.rs +++ b/contracts/transfer/src/tree.rs @@ -57,8 +57,7 @@ impl Tree { for note in notes { // skip transparent notes with a value of 0 if !note.value(None).is_ok_and(|value| value == 0) { - let leaf = TreeLeaf { block_height, note }; - self.push(leaf); + self.push(TreeLeaf { block_height, note }); } } } diff --git a/execution-core/src/transfer.rs b/execution-core/src/transfer.rs index 78b227ac72..ea140c7b37 100644 --- a/execution-core/src/transfer.rs +++ b/execution-core/src/transfer.rs @@ -251,19 +251,26 @@ impl Fee { Hash::digest(Domain::Other, &hash_inputs)[0] } - /// Generates a remainder from the fee and the given gas consumed + /// Generates a remainder from the fee and the given gas consumed. + /// + /// If there is a deposit, it means that the deposit hasn't been picked up + /// by the contract. In this case, it is added to the remainder note. #[must_use] - pub fn gen_remainder_note(&self, gas_consumed: u64) -> Note { - // Consuming more gas than the limit provided should never - // occur, and it's not responsability of the `Fee` to check that. - // Here defensively ensure it's not panicking, capping the gas - // consumed to the gas limit. + pub fn gen_remainder_note( + &self, + gas_consumed: u64, + deposit: Option, + ) -> Note { + // Consuming more gas than the limit provided should never occur, and + // it's not the responsibility of the `Fee` to check that. + // Here defensively ensure it's not panicking, capping the gas consumed + // to the gas limit. let gas_consumed = cmp::min(gas_consumed, self.gas_limit); let gas_changes = (self.gas_limit - gas_consumed) * self.gas_price; Note::transparent_stealth( self.stealth_address, - gas_changes, + gas_changes + deposit.unwrap_or_default(), self.sender, ) } diff --git a/rusk-recovery/src/state.rs b/rusk-recovery/src/state.rs index 10bff59558..3037affbd5 100644 --- a/rusk-recovery/src/state.rs +++ b/rusk-recovery/src/state.rs @@ -17,8 +17,8 @@ use tracing::info; use url::Url; use execution_core::{ - stake::StakeData, transfer::Mint, BlsPublicKey, BlsSecretKey, JubJubScalar, - PublicKey, + stake::StakeData, transfer::SenderAccount, BlsPublicKey, BlsSecretKey, + JubJubScalar, Note, PublicKey, }; use rusk_abi::{ContractData, ContractId, Session, VM}; use rusk_abi::{LICENSE_CONTRACT, STAKE_CONTRACT, TRANSFER_CONTRACT}; @@ -67,14 +67,18 @@ fn generate_transfer_state( balance.notes.iter().for_each(|&amount| { let r = JubJubScalar::random(&mut rng); let address = balance.address().gen_stealth_address(&r); - let sender = BlsPublicKey::from(&BlsSecretKey::random(&mut rng)); - let mint = Mint { - address, - value: amount, - sender, + let sender = SenderAccount { + contract: STAKE_CONTRACT.to_bytes(), + account: BlsPublicKey::from(&BlsSecretKey::random(&mut rng)), }; + let note = Note::transparent_stealth(address, amount, sender); session - .call::(TRANSFER_CONTRACT, "mint", &mint, u64::MAX) + .call::<(u64, Note), ()>( + TRANSFER_CONTRACT, + "push_note", + &(GENESIS_BLOCK_HEIGHT, note), + u64::MAX, + ) .expect("Minting should succeed"); }); });