From 0f4316232652e1c7f2a5ed114761c8ef43c5ea3b Mon Sep 17 00:00:00 2001 From: greged93 <82421016+greged93@users.noreply.github.com> Date: Fri, 23 Feb 2024 10:37:45 +0100 Subject: [PATCH] fix: hive test `ContractDeploymentOutOfGas` (#787) fix hive test `ContractDeploymentOutOfGas` --- src/eth_provider/provider.rs | 10 ++++++++-- src/eth_provider/utils.rs | 17 ++++++++++++++++- tests/eth_provider.rs | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/eth_provider/provider.rs b/src/eth_provider/provider.rs index fb00b6ce6..ff39fd323 100644 --- a/src/eth_provider/provider.rs +++ b/src/eth_provider/provider.rs @@ -44,6 +44,7 @@ use super::starknet::kakarot_core::{ use super::starknet::ERC20Reader; use super::starknet::STARKNET_NATIVE_TOKEN; use super::utils::contract_not_found; +use super::utils::entrypoint_not_found; use super::utils::iter_into; use super::utils::split_u256; use super::utils::try_from_u8_iterator; @@ -318,9 +319,14 @@ where let address = starknet_address(address); let contract = ContractAccountReader::new(address, &self.starknet_provider); - let bytecode = contract.bytecode().block_id(starknet_block_id).call().await?.bytecode; + let bytecode = contract.bytecode().block_id(starknet_block_id).call().await; - Ok(Bytes::from(try_from_u8_iterator::<_, Vec>(bytecode.0.into_iter()))) + if contract_not_found(&bytecode) || entrypoint_not_found(&bytecode) { + return Ok(Bytes::default()); + } + + let bytecode = bytecode?.bytecode.0; + Ok(Bytes::from(try_from_u8_iterator::<_, Vec>(bytecode.into_iter()))) } async fn get_logs(&self, filter: Filter) -> EthProviderResult { diff --git a/src/eth_provider/utils.rs b/src/eth_provider/utils.rs index 1aada1531..80b816c67 100644 --- a/src/eth_provider/utils.rs +++ b/src/eth_provider/utils.rs @@ -3,7 +3,10 @@ use std::fmt::LowerHex; use cainome::cairo_serde::Error; use mongodb::bson::{doc, Document}; use reth_primitives::{U128, U256}; -use starknet::{core::types::StarknetError, providers::ProviderError}; +use starknet::{ + core::types::{ContractErrorData, StarknetError}, + providers::ProviderError, +}; /// Converts an iterator of `Into` into a `Vec`. pub(crate) fn iter_into>(iter: impl IntoIterator) -> Vec { @@ -49,3 +52,15 @@ pub(crate) const fn contract_not_found(err: &Result) -> bool { Err(err) => matches!(err, Error::Provider(ProviderError::StarknetError(StarknetError::ContractNotFound))), } } + +pub(crate) fn entrypoint_not_found(err: &Result) -> bool { + match err { + Ok(_) => false, + Err(err) => matches!( + err, + Error::Provider(ProviderError::StarknetError(StarknetError::ContractError(ContractErrorData { + revert_error: reason + }))) if reason.contains("Entry point") && reason.contains("not found in contract") + ), + } +} diff --git a/tests/eth_provider.rs b/tests/eth_provider.rs index 978a42793..e1931485a 100644 --- a/tests/eth_provider.rs +++ b/tests/eth_provider.rs @@ -212,6 +212,20 @@ async fn test_get_code(#[future] counter: (Katana, KakarotEvmContract), _setup: assert_eq!(bytecode, Bytes::from(expected)); } +#[rstest] +#[awt] +#[tokio::test(flavor = "multi_thread")] +async fn test_get_code_no_contract(#[future] katana: Katana, _setup: ()) { + // Given + let eth_provider = katana.eth_provider(); + + // When + let bytecode = eth_provider.get_code(Address::random(), None).await.unwrap(); + + // Then + assert_eq!(bytecode, Bytes::default()); +} + #[rstest] #[awt] #[tokio::test(flavor = "multi_thread")]