Skip to content

Commit

Permalink
Merge pull request #1927 from dusk-network/mocello/transfer_contract_…
Browse files Browse the repository at this point in the history
…fixup

transfer-contract: Add deposit to remainder-note and remove bool return for mint
  • Loading branch information
moCello authored Jul 9, 2024
2 parents 744d416 + 5135734 commit 2133332
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
26 changes: 11 additions & 15 deletions contracts/transfer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions contracts/transfer/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions execution-core/src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
) -> 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,
)
}
Expand Down
20 changes: 12 additions & 8 deletions rusk-recovery/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<Mint, bool>(TRANSFER_CONTRACT, "mint", &mint, u64::MAX)
.call::<(u64, Note), ()>(
TRANSFER_CONTRACT,
"push_note",
&(GENESIS_BLOCK_HEIGHT, note),
u64::MAX,
)
.expect("Minting should succeed");
});
});
Expand Down

0 comments on commit 2133332

Please sign in to comment.