Skip to content

Commit

Permalink
chain/ethereum: return a boolean instead of entire code for eth_get_code
Browse files Browse the repository at this point in the history
  • Loading branch information
incrypto32 committed Apr 15, 2024
1 parent 94affa3 commit b2d7096
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 38 deletions.
23 changes: 12 additions & 11 deletions chain/ethereum/src/runtime/runtime_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use graph::{
semver::Version,
slog::Logger,
};
use graph_runtime_wasm::asc_abi::class::{AscBigInt, AscEnumArray, AscWrapped, EthereumValueKind};
use itertools::Itertools;
use graph_runtime_wasm::asc_abi::class::{AscBigInt, AscEnumArray, EthereumValueKind, Uint8Array};

use super::abi::{AscUnresolvedContractCall, AscUnresolvedContractCall_0_0_4};

Expand All @@ -56,8 +56,8 @@ pub const ETHEREUM_CALL: Gas = Gas::new(5_000_000_000);
// TODO: Determine the appropriate gas cost for `ETH_GET_BALANCE`, initially aligned with `ETHEREUM_CALL`.
pub const ETH_GET_BALANCE: Gas = Gas::new(5_000_000_000);

// TODO: Determine the appropriate gas cost for `ETH_GET_CODE`, initially aligned with `ETHEREUM_CALL`.
pub const ETH_GET_CODE: Gas = Gas::new(5_000_000_000);
// TODO: Determine the appropriate gas cost for `ETH_HAS_CODE`, initially aligned with `ETHEREUM_CALL`.
pub const ETH_HAS_CODE: Gas = Gas::new(5_000_000_000);

pub struct RuntimeAdapter {
pub eth_adapters: Arc<EthereumNetworkAdapters>,
Expand Down Expand Up @@ -120,13 +120,13 @@ impl blockchain::RuntimeAdapter<Chain> for RuntimeAdapter {

let eth_adapters = self.eth_adapters.cheap_clone();
let ethereum_get_code = HostFn {
name: "ethereum.getCode",
name: "ethereum.hasCode",
func: Arc::new(move |ctx, wasm_ptr| {
let eth_adapter = eth_adapters.cheapest_with(&NodeCapabilities {
archive,
traces: false,
})?;
eth_get_code(&eth_adapter, ctx, wasm_ptr).map(|ptr| ptr.wasm_ptr())
eth_has_code(&eth_adapter, ctx, wasm_ptr).map(|ptr| ptr.wasm_ptr())
}),
};

Expand Down Expand Up @@ -209,17 +209,17 @@ fn eth_get_balance(
}
}

fn eth_get_code(
fn eth_has_code(
eth_adapter: &EthereumAdapter,
ctx: HostFnCtx<'_>,
wasm_ptr: u32,
) -> Result<AscPtr<Uint8Array>, HostExportError> {
) -> Result<AscPtr<AscWrapped<bool>>, HostExportError> {
ctx.gas
.consume_host_fn_with_metrics(ETH_GET_CODE, "eth_get_code")?;
.consume_host_fn_with_metrics(ETH_HAS_CODE, "eth_has_code")?;

if ctx.heap.api_version() < API_VERSION_0_0_9 {
return Err(HostExportError::Deterministic(anyhow!(
"ethereum.getCode call is not supported before API version 0.0.9"
"ethereum.hasCode call is not supported before API version 0.0.9"
)));
}

Expand All @@ -232,10 +232,11 @@ fn eth_get_code(
eth_adapter
.get_code(logger, address, block_ptr.clone())
.compat(),
);
)
.map(|v| !v.0.is_empty());

match result {
Ok(v) => Ok(asc_new(ctx.heap, &v, &ctx.gas)?),
Ok(v) => Ok(asc_new(ctx.heap, &AscWrapped { inner: v }, &ctx.gas)?),
// Retry on any kind of error
Err(EthereumRpcError::Web3Error(e)) => Err(HostExportError::PossibleReorg(e.into())),
Err(EthereumRpcError::Timeout) => Err(HostExportError::PossibleReorg(
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/ethereum-api-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.69.0",
"@graphprotocol/graph-ts": "0.34.0-alpha-20240311104414-5f75d93",
"@graphprotocol/graph-ts": "0.34.0-alpha-20240319121936-1ceb12b",
"solc": "^0.8.2"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/ethereum-api-tests/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Foo @entity {
id: ID!
balance: BigInt!
code: Bytes!
hasCode: Boolean!
}
4 changes: 2 additions & 2 deletions tests/integration-tests/ethereum-api-tests/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export function handleTrigger(event: Trigger): void {
let address_str = dataSource.context().getString("contract");
let address = Address.fromString(address_str);

let code = ethereum.getCode(address);
let hasCode = ethereum.hasCode(address);

if (!entity) {
entity = new Foo(event.params.x.toString());
}

entity.balance = balance;
entity.code = code;
entity.hasCode = hasCode;

entity.save();
}
8 changes: 4 additions & 4 deletions tests/integration-tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,10 @@
dependencies:
assemblyscript "0.19.10"

"@graphprotocol/[email protected]20240311104414-5f75d93":
version "0.34.0-alpha-20240311104414-5f75d93"
resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.34.0-alpha-20240311104414-5f75d93.tgz#1d8826127e02b39379565b50a6113c4d1f09f794"
integrity sha512-Z5FCd/kfqR5r/truhuDYO/K2KOKZxau7PDeEFkVIB1SZFFz/FThVYboGldTL3xF278Fxu/cYaBQGMpTJy4nqYQ==
"@graphprotocol/[email protected]20240319121936-1ceb12b":
version "0.34.0-alpha-20240319121936-1ceb12b"
resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.34.0-alpha-20240319121936-1ceb12b.tgz#5adc6aa0ad4d5f4d2f002b91b817cfa5f989aba4"
integrity sha512-N4n5B62cbdduHlbaeddg0v6GujLkrdRoBi1aHEVLMcfIQpPzw3fyK4dNDMLmO85PHnzuKCkucVel2RyRKqtPYA==
dependencies:
assemblyscript "0.19.10"

Expand Down
24 changes: 5 additions & 19 deletions tests/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::time::{Duration, Instant};

use anyhow::{anyhow, bail, Context};
use futures::StreamExt;
use graph::prelude::hex;
use graph::prelude::serde_json::{json, Value};
use graph_tests::contract::Contract;
use graph_tests::helpers::{run_checked, TestFile};
Expand All @@ -31,9 +30,7 @@ type TestFn = Box<
+ Send,
>;

struct TestContext {
contracts: Vec<Contract>,
}
struct TestContext {}

enum TestStatus {
Ok,
Expand Down Expand Up @@ -145,9 +142,7 @@ impl TestCase {
status!(&self.name, "Subgraph ({}) has failed", subgraph.deployment);
}

let ctx = TestContext {
contracts: contracts.to_vec(),
};
let ctx = TestContext {};

status!(&self.name, "Starting test");
let subgraph2 = subgraph.clone();
Expand Down Expand Up @@ -428,30 +423,21 @@ async fn test_block_handlers(subgraph: Subgraph, _ctx: TestContext) -> anyhow::R
Ok(())
}

async fn test_eth_api(subgraph: Subgraph, ctx: TestContext) -> anyhow::Result<()> {
async fn test_eth_api(subgraph: Subgraph, _ctx: TestContext) -> anyhow::Result<()> {
assert!(subgraph.healthy);

let simple_contract = ctx
.contracts
.iter()
.find(|c| c.name == "SimpleContract")
.unwrap();

let code = simple_contract.code().await;
let code_hex = hex::encode(&code.0);

let expected_response = json!({
"foo": {
"id": "1",
"balance": "10000000000000000000000",
"code": format!("0x{}", code_hex),
"hasCode": true,
}
});

query_succeeds(
"Balance should be right",
&subgraph,
"{ foo(id: \"1\") { id balance code } }",
"{ foo(id: \"1\") { id balance hasCode } }",
expected_response,
)
.await?;
Expand Down

0 comments on commit b2d7096

Please sign in to comment.