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

Speed up signature check for orders with no pre-interactions #2953

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 10 additions & 9 deletions crates/e2e/tests/e2e/eth_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use {
ethcontract::U256,
model::{
order::{OrderCreation, OrderKind, BUY_ETH_ADDRESS},
signature::EcdsaSigningScheme,
signature::{hashed_eip712_message, Signature},
},
secp256k1::SecretKey,
shared::ethrpc::Web3,
web3::signing::SecretKeyRef,
};

#[tokio::test]
Expand All @@ -31,6 +29,9 @@ async fn test(web3: Web3) {
.await;

token.mint(trader.address(), to_wei(4)).await;
safe.exec_call(token.approve(onchain.contracts().allowance, to_wei(4)))
.await;
token.mint(safe.address(), to_wei(4)).await;
tx!(
trader.account(),
token.approve(onchain.contracts().allowance, to_wei(4))
Expand All @@ -49,7 +50,8 @@ async fn test(web3: Web3) {
.await
.unwrap();
assert_eq!(balance, 0.into());
let order = OrderCreation {
let mut order = OrderCreation {
from: Some(safe.address()),
sell_token: token.address(),
sell_amount: to_wei(4),
buy_token: BUY_ETH_ADDRESS,
Expand All @@ -59,12 +61,11 @@ async fn test(web3: Web3) {
kind: OrderKind::Sell,
receiver: Some(safe.address()),
..Default::default()
}
.sign(
EcdsaSigningScheme::Eip712,
};
order.signature = Signature::Eip1271(safe.sign_message(&hashed_eip712_message(
&onchain.contracts().domain_separator,
SecretKeyRef::from(&SecretKey::from_slice(trader.private_key()).unwrap()),
);
&order.data().hash_struct(),
)));
services.create_order(&order).await.unwrap();

tracing::info!("Waiting for trade.");
Expand Down
37 changes: 36 additions & 1 deletion crates/shared/src/signature_validator/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
super::{SignatureCheck, SignatureValidating, SignatureValidationError},
crate::ethcontract_error::EthcontractErrorType,
anyhow::Result,
contracts::ERC1271SignatureValidator,
ethcontract::Bytes,
ethrpc::Web3,
futures::future,
Expand All @@ -18,16 +19,46 @@ pub struct Validator {
signatures: contracts::support::Signatures,
settlement: H160,
vault_relayer: H160,
web3: Web3,
}

impl Validator {
/// The result returned from `isValidSignature` if the signature is correct
const IS_VALID_SIGNATURE_MAGIC_BYTES: &'static str = "1626ba7e";

pub fn new(web3: &Web3, settlement: H160, vault_relayer: H160) -> Self {
let web3 = ethrpc::instrumented::instrument_with_label(web3, "signatureValidation".into());
Self {
signatures: contracts::support::Signatures::at(&web3, settlement),
settlement,
vault_relayer,
web3: web3.clone(),
}
}

/// Simulate isValidSignature for the cases in which the order does not have
/// pre-interactions
async fn simulate_without_pre_interactions(
&self,
check: &SignatureCheck,
) -> Result<(), SignatureValidationError> {
// Since there are no interactions (no dynamic conditions / complex pre-state
// change), the order's validity can be directly determined by whether
// the signature matches the expected hash of the order data, checked
// with isValidSignature method called on the owner's contract
let contract = ERC1271SignatureValidator::at(&self.web3, check.signer);
let magic_bytes = contract
.methods()
.is_valid_signature(Bytes(check.hash), Bytes(check.signature.clone()))
.call()
.await
.map(|value| hex::encode(value.0))?;

if magic_bytes != Self::IS_VALID_SIGNATURE_MAGIC_BYTES {
return Err(SignatureValidationError::Invalid);
}

Ok(())
}

async fn simulate(
Expand Down Expand Up @@ -73,7 +104,11 @@ impl SignatureValidating for Validator {
checks: Vec<SignatureCheck>,
) -> Vec<Result<(), SignatureValidationError>> {
future::join_all(checks.into_iter().map(|check| async move {
self.simulate(&check).await?;
if check.interactions.is_empty() {
self.simulate_without_pre_interactions(&check).await?;
} else {
self.simulate(&check).await?;
}
Ok(())
}))
.await
Expand Down
Loading