Skip to content

Commit

Permalink
fix: hive test ContractDeploymentOutOfGas (#787)
Browse files Browse the repository at this point in the history
fix hive test `ContractDeploymentOutOfGas`
  • Loading branch information
greged93 committed Feb 23, 2024
1 parent 487fbbd commit 0f43162
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u8>>(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<u8>>(bytecode.into_iter())))
}

async fn get_logs(&self, filter: Filter) -> EthProviderResult<FilterChanges> {
Expand Down
17 changes: 16 additions & 1 deletion src/eth_provider/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D>` into a `Vec<D>`.
pub(crate) fn iter_into<D, S: Into<D>>(iter: impl IntoIterator<Item = S>) -> Vec<D> {
Expand Down Expand Up @@ -49,3 +52,15 @@ pub(crate) const fn contract_not_found<T>(err: &Result<T, Error>) -> bool {
Err(err) => matches!(err, Error::Provider(ProviderError::StarknetError(StarknetError::ContractNotFound))),
}
}

pub(crate) fn entrypoint_not_found<T>(err: &Result<T, Error>) -> 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")
),
}
}
14 changes: 14 additions & 0 deletions tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 0f43162

Please sign in to comment.