Skip to content

Commit

Permalink
Memoize bytecode (#2918)
Browse files Browse the repository at this point in the history
# Description
The newly added metrics show that filtering invalid signatures can be
quite slow. Since I don't see any low hanging fruit to speed up the
simulation code let's just at least memoize the bytecode we pass to the
simulation function since that never changes anyway.
This skips hex-decoding the contract bytecode on every simulation.

## How to test
existing e2e tests should continue to work
  • Loading branch information
MartinquaXD authored Aug 22, 2024
1 parent 0fc0fb3 commit d866a88
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
19 changes: 6 additions & 13 deletions crates/contracts/src/storage_accessible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use {
},
H160,
},
std::sync::LazyLock,
};

/// Encode a call to a `StorageAccessible` `target` to execute `call` with the
Expand All @@ -27,20 +28,12 @@ pub fn call(target: H160, code: Bytes, call: Bytes) -> CallRequest {
ethcontract::Bytes(call.0).into_token(),
]);

// memoize value to skip hex-decodeing on every call
static BYTECODE: LazyLock<Vec<u8>> =
LazyLock::new(|| SimulateCode::raw_contract().bytecode.to_bytes().unwrap().0);

CallRequest {
data: Some(
[
SimulateCode::raw_contract()
.bytecode
.to_bytes()
.unwrap()
.0
.as_slice(),
&args,
]
.concat()
.into(),
),
data: Some([BYTECODE.as_slice(), &args].concat().into()),
..Default::default()
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/shared/src/signature_validator/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
ethrpc::Web3,
futures::future,
primitive_types::{H160, U256},
std::sync::LazyLock,
};

pub struct Validator {
Expand All @@ -33,13 +34,17 @@ impl Validator {
&self,
check: &SignatureCheck,
) -> Result<Simulation, SignatureValidationError> {
// memoize byte code to not hex-decode it on every call
static BYTECODE: LazyLock<web3::types::Bytes> =
LazyLock::new(|| contracts::bytecode!(contracts::support::Signatures));

// We simulate the signature verification from the Settlement contract's
// context. This allows us to check:
// 1. How the pre-interactions would behave as part of the settlement
// 2. Simulate the actual `isValidSignature` calls that would happen as part of
// a settlement
let gas_used = contracts::storage_accessible::simulate(
contracts::bytecode!(contracts::support::Signatures),
BYTECODE.clone(),
self.signatures.methods().validate(
(self.settlement, self.vault_relayer),
check.signer,
Expand Down

0 comments on commit d866a88

Please sign in to comment.