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

Consider token as good when node doesn't support trace calls #3002

Merged
merged 5 commits into from
Sep 20, 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
6 changes: 5 additions & 1 deletion crates/autopilot/src/solvable_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,11 @@ async fn find_unsupported_tokens(
match bad_token.detect(token).await {
Ok(quality) => (!quality.is_good()).then_some(token),
Err(err) => {
tracing::warn!(?token, ?err, "unable to determine token quality");
tracing::warn!(
?token,
?err,
"unable to determine token quality, assume good"
);
Some(token)
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/shared/src/bad_token/instrumented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
anyhow::Result,
prometheus::IntCounterVec,
prometheus_metric_storage::MetricStorage,
tracing::Instrument,
};

pub trait InstrumentedBadTokenDetectorExt {
Expand Down Expand Up @@ -33,7 +34,14 @@ pub struct InstrumentedBadTokenDetector {
#[async_trait::async_trait]
impl BadTokenDetecting for InstrumentedBadTokenDetector {
async fn detect(&self, token: ethcontract::H160) -> Result<TokenQuality> {
let result = self.inner.detect(token).await;
let result = self
.inner
.detect(token)
.instrument(tracing::info_span!(
"token_quality",
token = format!("{token:#x}")
))
.await;

let label = match &result {
Ok(TokenQuality::Good) => "good",
Expand Down
38 changes: 33 additions & 5 deletions crates/shared/src/bad_token/trace_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ use {
crate::{ethrpc::Web3, trace_many},
anyhow::{bail, ensure, Context, Result},
contracts::ERC20,
ethcontract::{dyns::DynTransport, transaction::TransactionBuilder, PrivateKey},
ethcontract::{
dyns::DynTransport,
jsonrpc::ErrorCode,
transaction::TransactionBuilder,
PrivateKey,
},
primitive_types::{H160, U256},
std::{cmp, sync::Arc},
web3::{
error::TransportError,
signing::keccak256,
types::{BlockTrace, CallRequest, Res},
},
Expand Down Expand Up @@ -73,9 +79,31 @@ impl TraceCallDetector {
// implementation sending to an address that does not have any balance
// yet (implicitly 0) causes an allocation.
let request = self.create_trace_request(token, amount, take_from);
let traces = trace_many::trace_many(request, &self.web3)
.await
.context("trace_many")?;
let traces = match trace_many::trace_many(request, &self.web3).await {
Ok(result) => result,
Err(e) => {
// If the node doesn't support trace calls, consider the token as good to not
// block the system
if matches!(
e,
web3::Error::Rpc(ethcontract::jsonrpc::Error {
code: ErrorCode::MethodNotFound,
..
})
) || matches!(
e,
// Alchemy specific error
web3::Error::Transport(TransportError::Message(ref msg)) if msg == "HTTP error 400 Bad Request"
) {
tracing::warn!(
error=?e,
"unable to perform trace call with configured node, assume good quality"
);
return Ok(TokenQuality::Good);
}
return Err(e).context("trace_many");
}
};
Self::handle_response(&traces, amount, take_from)
}

Expand Down Expand Up @@ -663,7 +691,7 @@ mod tests {
UniswapV3Finder::new(
IUniswapV3Factory::deployed(&web3).await.unwrap(),
base_tokens.to_vec(),
FeeValues::Dynamic,
FeeValues::Static,
)
.await
.unwrap(),
Expand Down
13 changes: 6 additions & 7 deletions crates/shared/src/trace_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use {
anyhow::{Context, Result},
web3::{
types::{BlockNumber, BlockTrace, CallRequest, TraceType},
Error,
Transport,
},
};

// Use the trace_callMany api https://openethereum.github.io/JSONRPC-trace-module#trace_callmany
// api to simulate these call requests applied together one after another.
// Err if communication with the node failed.
pub async fn trace_many(requests: Vec<CallRequest>, web3: &Web3) -> Result<Vec<BlockTrace>> {
pub async fn trace_many(requests: Vec<CallRequest>, web3: &Web3) -> Result<Vec<BlockTrace>, Error> {
let transport = web3.transport();
let requests = requests
.into_iter()
Expand All @@ -20,17 +21,15 @@ pub async fn trace_many(requests: Vec<CallRequest>, web3: &Web3) -> Result<Vec<B
serde_json::to_value(vec![TraceType::Trace])?,
])
})
.collect::<Result<Vec<_>>>()?;
.collect::<Result<Vec<_>>>()
.map_err(|e| Error::Decoder(e.to_string()))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can only hit this .map_err() if our JSON serialization fails so using the Error::Decoder variant seems odd. OTOH web3 doesn't offer a more suitable error variant anyway.

let block = BlockNumber::Latest;
let params = vec![
serde_json::to_value(requests)?,
serde_json::to_value(block)?,
];
let response = transport
.execute("trace_callMany", params)
.await
.context("trace_callMany failed")?;
serde_json::from_value(response).context("failed to decode trace_callMany response")
let response = transport.execute("trace_callMany", params).await?;
serde_json::from_value(response).map_err(|e| Error::Decoder(e.to_string()))
}

// Check the return value of trace_many for whether all top level transactions
Expand Down
Loading