Skip to content

Commit

Permalink
fixup! make max gas balance change gated
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmark committed Jan 16, 2025
1 parent 7830b6c commit 8167eeb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 51 deletions.
4 changes: 2 additions & 2 deletions aptos-move/aptos-abstract-gas-usage/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<A: GasAlgebra> GasAlgebra for CalibrationAlgebra<A> {
self.base.storage_fee_used()
}

fn adjust_initial_gas(&mut self, new_initial_gas: impl Into<Gas>) -> PartialVMResult<()> {
self.base.adjust_initial_gas(new_initial_gas)
fn inject_balance(&mut self, new_initial_gas: impl Into<Gas>) -> PartialVMResult<()> {
self.base.inject_balance(new_initial_gas)
}
}
24 changes: 8 additions & 16 deletions aptos-move/aptos-gas-meter/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use move_core_types::{
vm_status::StatusCode,
};
use std::fmt::Debug;
use std::ops::AddAssign;

/// Base gas algebra implementation that tracks the gas usage using its internal counters.
///
Expand Down Expand Up @@ -104,20 +105,6 @@ impl StandardGasAlgebra {
},
}
}

fn adjust_initial_balance(
&mut self,
reset_initial_balance_to: impl Into<Gas>,
) -> PartialVMResult<()> {
self.initial_balance = reset_initial_balance_to
.into()
.to_unit_with_params(&self.vm_gas_params.txn);
self.balance = self.initial_balance;
let total_calculated =
self.execution_gas_used + self.io_gas_used + self.storage_fee_in_internal_units;
let (_actual, res) = self.charge(total_calculated);
res
}
}

impl GasAlgebra for StandardGasAlgebra {
Expand Down Expand Up @@ -306,7 +293,12 @@ impl GasAlgebra for StandardGasAlgebra {
}

// Reset the initial gas balance to reflect the new balance with the change carried over.
fn adjust_initial_gas(&mut self, new_initial_gas: impl Into<Gas>) -> PartialVMResult<()> {
self.adjust_initial_balance(new_initial_gas)
fn inject_balance(&mut self, extra_balance: impl Into<Gas>) -> PartialVMResult<()> {
let extra_unit = extra_balance
.into()
.to_unit_with_params(&self.vm_gas_params.txn);
self.initial_balance.add_assign(extra_unit);
self.balance.add_assign(extra_unit);
Ok(())
}
}
10 changes: 5 additions & 5 deletions aptos-move/aptos-gas-meter/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ pub trait GasAlgebra {
/// Returns the amount of storage fee used.
fn storage_fee_used(&self) -> Fee;

/// Resets the initial balance and recalculates the balance based on existing gas usage.
fn adjust_initial_gas(&mut self, new_initial_gas: impl Into<Gas>) -> PartialVMResult<()>;
/// Bump the `extra_balance`.
fn inject_balance(&mut self, extra_balance: impl Into<Gas>) -> PartialVMResult<()>;
}

/// Trait representing a gas meter used inside the Aptos VM.
Expand Down Expand Up @@ -252,10 +252,10 @@ pub trait AptosGasMeter: MoveGasMeter {
self.algebra().storage_fee_used()
}

/// Resets the initial balance and recalculates the balance based on existing gas usage.
fn adjust_initial_gas(&mut self, new_initial_gas: impl Into<Gas>) -> VMResult<()> {
/// Bump the `extra_balance`.
fn inject_balance(&mut self, extra_balance: impl Into<Gas>) -> VMResult<()> {
self.algebra_mut()
.adjust_initial_gas(new_initial_gas)
.inject_balance(extra_balance)
.map_err(|e| e.finish(Location::Undefined))
}
}
13 changes: 3 additions & 10 deletions aptos-move/aptos-vm/src/aptos_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2062,16 +2062,10 @@ impl AptosVM {
}));

if self.features().is_account_abstraction_enabled() {
let max_aa_gas = match self.gas_params(&log_context) {
Ok(vm_params) => vm_params.vm.txn.max_aa_gas,
Err(err) => {
panic!("TODO: what to do here");
// return VMValidatorResult::new(Some(err.status_code()), 0);
},
};
let max_aa_gas = unwrap_or_discard!(self.gas_params(log_context)).vm.txn.max_aa_gas;
if max_aa_gas < txn_data.max_gas_amount() {
// Reset initial gas after validation with max_aa_gas.
unwrap_or_discard!(gas_meter.adjust_initial_gas(txn_data.max_gas_amount()));
unwrap_or_discard!(gas_meter.inject_balance(txn_data.max_gas_amount().checked_sub(max_aa_gas).unwrap()));
}
} else {
assert_eq!(initial_gas, gas_meter.balance());
Expand Down Expand Up @@ -3055,9 +3049,8 @@ impl VMValidator for AptosVM {
&self,
transaction: SignedTransaction,
state_view: &impl StateView,
_module_storage: &impl ModuleStorage,
module_storage: &impl ModuleStorage,
) -> VMValidatorResult {
let module_storage = &state_view.as_aptos_code_storage(self.environment());
let _timer = TXN_VALIDATION_SECONDS.start_timer();
let log_context = AdapterLogSchema::new(state_view.id(), 0);

Expand Down
38 changes: 20 additions & 18 deletions crates/transaction-workloads-lib/src/raw_module_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,24 +1744,26 @@ pub static MODULES_COMPLEX: Lazy<Vec<Vec<u8>>> = Lazy::new(|| { vec![
pub static PACKAGE_AMBASSADOR_TOKEN_METADATA: Lazy<Vec<u8>> = Lazy::new(|| {
vec![
10, 97, 109, 98, 97, 115, 115, 97, 100, 111, 114, 1, 0, 0, 0, 0, 0, 0,
0, 0, 64, 68, 70, 50, 57, 50, 70, 53, 53, 56, 50, 66, 48, 57, 50, 65,
51, 54, 55, 65, 53, 69, 55, 69, 57, 65, 48, 48, 69, 66, 51, 68, 57, 52,
48, 55, 56, 67, 49, 54, 69, 65, 66, 65, 48, 50, 52, 69, 65, 65, 50, 52,
57, 52, 49, 55, 70, 54, 50, 51, 52, 53, 67, 50, 48, 253, 1, 31, 139, 8,
0, 0, 0, 0, 0, 2, 255, 173, 142, 63, 79, 195, 64, 12, 197, 247, 251, 20,
86, 24, 186, 144, 63, 172, 72, 12, 21, 162, 43, 11, 91, 84, 161, 203, 157, 155,
28, 73, 236, 232, 236, 20, 36, 196, 119, 231, 142, 22, 144, 16, 99, 55, 219, 239,
249, 253, 94, 187, 88, 55, 218, 30, 247, 134, 236, 140, 112, 7, 27, 59, 119, 86,
196, 122, 142, 27, 115, 196, 40, 129, 41, 159, 111, 170, 166, 106, 54, 198, 92, 129,
103, 32, 86, 112, 131, 165, 30, 65, 25, 138, 231, 226, 26, 28, 147, 139, 168, 8,
214, 251, 136, 34, 64, 136, 30, 61, 28, 56, 194, 204, 126, 157, 176, 92, 214, 110,
10, 50, 128, 162, 104, 160, 30, 14, 49, 33, 95, 57, 142, 166, 61, 63, 161, 236,
205, 47, 63, 97, 139, 230, 237, 126, 187, 123, 40, 140, 105, 61, 46, 72, 30, 201,
133, 236, 218, 46, 202, 178, 251, 14, 72, 206, 119, 232, 131, 230, 143, 65, 117, 145,
219, 186, 78, 235, 176, 118, 149, 227, 185, 182, 217, 92, 78, 182, 147, 243, 248, 67,
174, 146, 43, 149, 151, 181, 243, 225, 139, 247, 71, 79, 90, 196, 99, 22, 102, 27,
136, 80, 11, 248, 56, 177, 159, 120, 68, 122, 236, 94, 208, 169, 92, 26, 175, 57,
187, 228, 83, 248, 191, 21, 62, 1, 7, 30, 19, 174, 183, 1, 0, 0, 1, 10,
0, 0, 64, 70, 52, 52, 65, 66, 48, 70, 68, 68, 52, 52, 65, 48, 49, 48,
68, 68, 57, 67, 48, 54, 53, 52, 49, 66, 68, 53, 69, 70, 51, 49, 56, 70,
70, 68, 70, 70, 69, 69, 56, 70, 53, 57, 52, 68, 68, 52, 51, 53, 48, 68,
57, 69, 56, 67, 57, 67, 57, 48, 66, 56, 70, 66, 70, 161, 2, 31, 139, 8,
0, 0, 0, 0, 0, 2, 255, 173, 144, 63, 79, 195, 48, 16, 197, 119, 127, 138,
83, 58, 116, 33, 127, 88, 145, 24, 42, 68, 87, 22, 182, 168, 66, 142, 125, 77,
76, 98, 95, 228, 115, 2, 18, 226, 187, 99, 211, 54, 136, 210, 5, 169, 146, 7,
219, 239, 221, 251, 61, 187, 30, 165, 234, 101, 139, 59, 225, 164, 69, 184, 135, 181,
180, 141, 100, 150, 154, 252, 90, 204, 232, 217, 144, 75, 215, 183, 69, 85, 84, 107,
33, 86, 160, 9, 28, 5, 80, 157, 116, 45, 66, 32, 200, 94, 178, 27, 80, 228,
148, 199, 128, 32, 181, 246, 200, 12, 14, 81, 163, 134, 61, 121, 176, 164, 167, 1,
243, 113, 106, 6, 195, 29, 4, 228, 96, 92, 11, 123, 31, 145, 111, 228, 123, 81,
31, 135, 144, 119, 226, 135, 31, 177, 89, 245, 254, 176, 217, 62, 102, 66, 212, 26,
71, 116, 26, 157, 50, 201, 181, 130, 205, 24, 136, 183, 167, 136, 232, 253, 128, 214,
132, 52, 211, 133, 48, 242, 93, 89, 198, 99, 55, 53, 133, 34, 91, 202, 100, 206,
7, 217, 240, 113, 187, 176, 139, 232, 138, 245, 121, 106, 180, 249, 38, 158, 233, 81,
243, 56, 39, 193, 74, 227, 28, 134, 12, 62, 79, 244, 103, 234, 209, 61, 53, 175,
168, 2, 95, 187, 64, 72, 217, 57, 29, 194, 47, 150, 184, 240, 1, 3, 41, 57,
36, 91, 81, 148, 203, 58, 228, 89, 154, 177, 92, 160, 231, 37, 150, 188, 63, 79,
250, 111, 228, 239, 222, 49, 246, 11, 123, 181, 222, 205, 98, 2, 0, 0, 1, 10,
97, 109, 98, 97, 115, 115, 97, 100, 111, 114, 0, 0, 0, 4, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 14, 65, 112, 116, 111, 115, 70, 114,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

module 0xABCD::permissioned_transfer {
use aptos_framework::aptos_account;
use aptos_framework::permissioned_signer;
use aptos_framework::primary_fungible_store;

public entry fun transfer_permissioned(
source: &signer, to: address, amount: u64
) {
let handle = permissioned_signer::create_permissioned_handle(source);
let permissioned_signer = permissioned_signer::signer_from_permissioned_handle(&handle);

primary_fungible_store::grant_apt_permission(source, &permissioned_signer, amount);
aptos_account::transfer(&permissioned_signer, to, amount);

permissioned_signer::destroy_permissioned_handle(handle);
}

public entry fun transfer(
source: &signer, to: address, amount: u64
) {
aptos_account::transfer(source, to, amount);
}
}

0 comments on commit 8167eeb

Please sign in to comment.