Skip to content

Commit

Permalink
rusk: changed economic gas spent to economic mode
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed May 21, 2024
1 parent d6c32c2 commit f32ab8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
21 changes: 4 additions & 17 deletions rusk/src/lib/chain/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ use phoenix_core::transaction::StakeData;
use phoenix_core::Transaction as PhoenixTransaction;
use rusk_abi::dusk::Dusk;
use rusk_abi::{
CallReceipt, ContractError, ContractId, EconomicMode,
Error as PiecrustError, Event, Session, STAKE_CONTRACT, TRANSFER_CONTRACT,
VM,
CallReceipt, ContractError, ContractId, Error as PiecrustError, Event,
Session, STAKE_CONTRACT, TRANSFER_CONTRACT, VM,
};
use rusk_profile::to_rusk_state_id_path;
use tokio::sync::broadcast;
use EconomicMode::{Allowance, Charge};

use super::{coinbase_value, emission_amount, Rusk, RuskTip};
use crate::http::ContractEvent;
Expand Down Expand Up @@ -149,15 +147,10 @@ impl Rusk {
block_gas_left -= gas_spent;
let gas_price = unspent_tx.inner.fee.gas_price;
dusk_spent += gas_spent * gas_price;
let economic_gas_spent = match receipt.economic_mode {
Allowance(allowance) if allowance > 0 => 0,
Charge(charge) if charge > 0 => charge,
_ => gas_spent,
};
spent_txs.push(SpentTransaction {
inner: unspent_tx,
gas_spent,
economic_gas_spent,
economic_mode: receipt.economic_mode,
block_height,
err,
});
Expand Down Expand Up @@ -443,16 +436,10 @@ fn accept(
.checked_sub(gas_spent)
.ok_or(Error::OutOfGas)?;

let economic_gas_spent = match receipt.economic_mode {
Allowance(allowance) if allowance > 0 => 0,
Charge(charge) if charge > 0 => charge,
_ => gas_spent,
};

spent_txs.push(SpentTransaction {
inner: unspent_tx.clone(),
gas_spent,
economic_gas_spent,
economic_mode: receipt.economic_mode,
block_height,
// We're currently ignoring the result of successful calls
err: receipt.data.err().map(|e| format!("{e}")),
Expand Down
25 changes: 15 additions & 10 deletions rusk/tests/services/contract_pays_earns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use dusk_wallet_core::{self as wallet};
use rand::prelude::*;
use rand::rngs::StdRng;
use rusk::{Result, Rusk};
use rusk_abi::{ContractData, ContractId, TRANSFER_CONTRACT};
use rusk_abi::{ContractData, ContractId, EconomicMode, TRANSFER_CONTRACT};
use rusk_recovery_tools::state;
use tempfile::tempdir;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -85,7 +85,7 @@ fn make_and_execute_transaction(
rusk: &Rusk,
wallet: &wallet::Wallet<TestStore, TestStateClient, TestProverClient>,
method: impl AsRef<str>,
) -> u64 {
) -> EconomicMode {
// We will refund the transaction to ourselves.
let refund = wallet
.public_key(SENDER_INDEX)
Expand Down Expand Up @@ -137,7 +137,7 @@ fn make_and_execute_transaction(
.expect("There should be one spent transactions");

assert!(tx.err.is_none(), "Transaction should succeed");
tx.economic_gas_spent
tx.economic_mode
}

/// We call method 'pay' of a Charlie contract
Expand Down Expand Up @@ -165,9 +165,14 @@ pub async fn contract_pays() -> Result<()> {

info!("Original Root: {:?}", hex::encode(original_root));

let economic_gas_spent =
make_and_execute_transaction(&rusk, &wallet, "pay");
assert_eq!(economic_gas_spent, 0, "Transaction should be free");
let economic_mode = make_and_execute_transaction(&rusk, &wallet, "pay");
assert!(
match economic_mode {
EconomicMode::Allowance(allowance) if allowance > 0 => true,
_ => false,
},
"Transaction should be free"
);

// Check the state's root is changed from the original one
let new_root = rusk.state_root();
Expand Down Expand Up @@ -207,11 +212,11 @@ pub async fn contract_earns() -> Result<()> {

info!("Original Root: {:?}", hex::encode(original_root));

let economic_gas_spent =
make_and_execute_transaction(&rusk, &wallet, "earn");
let economic_mode = make_and_execute_transaction(&rusk, &wallet, "earn");
assert_eq!(
economic_gas_spent, CHARLIE_CHARGE,
"Transaction should cost as much as contract is charging minus discount"
economic_mode,
EconomicMode::Charge(CHARLIE_CHARGE),
"Transaction should cost as much as contract is charging"
);

// Check the state's root is changed from the original one
Expand Down

0 comments on commit f32ab8e

Please sign in to comment.