Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Dec 20, 2024
1 parent 8f58e92 commit 5c48492
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions precompiles/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
keys: Vec<U256>,
values: Vec<U256>,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_write_gas_cost())?;
handle.record_cost(RuntimeHelper::<Runtime>::db_write_gas_cost() * keys.len())?;

let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

Expand Down Expand Up @@ -83,9 +83,9 @@ where

let key = Self::u256_to_key(key)?;

// Get the value from the oracle
// Get the value from the oracle, return (0, 0) if not found
let TimestampedValue { value, timestamp } = pallet_oracle::Pallet::<Runtime>::get(key)
.ok_or_else(|| revert("No value found for key"))?;
.unwrap_or(TimestampedValue { value: 0, timestamp: 0 });

Ok((value.into(), timestamp.into()))
}
Expand Down
10 changes: 7 additions & 3 deletions precompiles/oracle/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ fn get_value_works() {
}

#[test]
fn get_value_fails_for_non_existent_key() {
fn get_value_returns_zero_for_non_existent_key() {
ExtBuilder::default().build().execute_with(|| {
let mut tester = PrecompileTester::new(precompiles(), H160::repeat_byte(0x01), PRECOMPILE_ADDRESS);

// Try to read a non-existent key
tester
let (value, timestamp): (U256, U256) = tester
.call("getValue(uint256)", U256::from(999u32))
.expect_no_logs()
.execute_reverts(|output| output == b"No value found for key");
.execute_returns();

// Should return zero values for both value and timestamp
assert_eq!(value, U256::zero());
assert_eq!(timestamp, U256::zero());
});
}

0 comments on commit 5c48492

Please sign in to comment.