Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix modexp record gas #1290

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions frame/evm/precompile/modexp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ fn read_input(source: &[u8], target: &mut [u8], source_offset: &mut usize) {

impl Precompile for Modexp {
fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult {
handle.record_cost(MIN_GAS_COST)?;

let input = handle.input();
let mut input_offset = 0;

Expand Down Expand Up @@ -182,7 +184,6 @@ impl Precompile for Modexp {

// Gas formula allows arbitrary large exp_len when base and modulus are empty, so we need to handle empty base first.
let r = if base_len == 0 && mod_len == 0 {
handle.record_cost(MIN_GAS_COST)?;
BigUint::zero()
} else {
// read the numbers themselves.
Expand All @@ -207,7 +208,7 @@ impl Precompile for Modexp {
modulus.is_even(),
);

handle.record_cost(gas_cost)?;
handle.record_cost(gas_cost.saturating_sub(MIN_GAS_COST))?;

if modulus.is_zero() || modulus.is_one() {
BigUint::zero()
Expand Down Expand Up @@ -255,11 +256,35 @@ mod tests {
Ok(())
}

#[test]
fn test_min_gas() {
let context: Context = Context {
address: Default::default(),
caller: Default::default(),
apparent_value: From::from(0),
};

assert_eq!(
Modexp::execute(&mut MockHandle::new(vec![], Some(199), context.clone())),
Err(PrecompileFailure::Error {
exit_status: ExitError::OutOfGas,
})
);

assert_eq!(
Modexp::execute(&mut MockHandle::new(vec![], Some(200), context)),
Ok(PrecompileOutput {
exit_status: ExitSucceed::Returned,
output: vec![],
})
);
}

#[test]
fn test_empty_input() {
let input = Vec::new();

let cost: u64 = 1;
let cost: u64 = 200;

let context: Context = Context {
address: Default::default(),
Expand Down Expand Up @@ -288,7 +313,7 @@ mod tests {
)
.expect("Decode failed");

let cost: u64 = 1;
let cost: u64 = 10000;

let context: Context = Context {
address: Default::default(),
Expand All @@ -310,15 +335,15 @@ mod tests {
}

#[test]
fn test_excessive_input() -> Result<(), PrecompileFailure> {
fn test_excessive_input() {
let input = hex::decode(
"1000000000000000000000000000000000000000000000000000000000000001\
0000000000000000000000000000000000000000000000000000000000000001\
0000000000000000000000000000000000000000000000000000000000000001",
)
.expect("Decode failed");

let cost: u64 = 1;
let cost: u64 = 200;

let context: Context = Context {
address: Default::default(),
Expand All @@ -328,20 +353,12 @@ mod tests {

let mut handle = MockHandle::new(input, Some(cost), context);

match Modexp::execute(&mut handle) {
Ok(_) => {
panic!("Test not expected to pass");
}
Err(e) => {
assert_eq!(
e,
PrecompileFailure::Error {
exit_status: ExitError::Other("unreasonably large base length".into())
}
);
Ok(())
}
}
assert_eq!(
Modexp::execute(&mut handle),
Err(PrecompileFailure::Error {
exit_status: ExitError::Other("unreasonably large base length".into())
})
);
}

#[test]
Expand Down Expand Up @@ -515,7 +532,7 @@ mod tests {
apparent_value: From::from(0),
};

let mut handle = MockHandle::new(input, Some(100_000), context);
let mut handle = MockHandle::new(input, Some(1000_000), context);

let _ = Modexp::execute(&mut handle).expect("Modexp::execute() returned error");

Expand Down
10 changes: 8 additions & 2 deletions frame/evm/test-vector-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ impl PrecompileHandle for MockHandle {

fn record_cost(&mut self, cost: u64) -> Result<(), ExitError> {
self.gas_used += cost;

if let Some(gas_limit) = self.gas_limit {
if self.gas_used > gas_limit {
return Err(ExitError::OutOfGas);
}
}
Ok(())
}

Expand Down Expand Up @@ -132,7 +138,7 @@ pub fn test_precompile_test_vectors<P: Precompile>(filepath: &str) -> Result<(),
for test in tests {
let input: Vec<u8> = hex::decode(test.input).expect("Could not hex-decode test input data");

let cost: u64 = 10000000;
let cost: u64 = 100000000;

let context: Context = Context {
address: Default::default(),
Expand Down Expand Up @@ -184,7 +190,7 @@ pub fn test_precompile_failure_test_vectors<P: Precompile>(filepath: &str) -> Re
for test in tests {
let input: Vec<u8> = hex::decode(test.input).expect("Could not hex-decode test input data");

let cost: u64 = 10000000;
let cost: u64 = 100000000;

let context: Context = Context {
address: Default::default(),
Expand Down
Loading