Skip to content

Commit

Permalink
Create public common module in test crate (graphprotocol#2678)
Browse files Browse the repository at this point in the history
* Extracted common module out of test crate.

* Make instance and api_version fields public.

* Make common module public.

* Export mock_context()

* Make instance_ctx public.

* Remove debug assertions annotation.
  • Loading branch information
pete-eiger authored Aug 4, 2021
1 parent 619e259 commit c87281e
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 165 deletions.
2 changes: 1 addition & 1 deletion chain/ethereum/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use runtime_adapter::RuntimeAdapter;

pub mod abi;
mod runtime_adapter;
pub mod runtime_adapter;
2 changes: 1 addition & 1 deletion chain/ethereum/src/runtime/runtime_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn eth_call(
}

#[derive(Clone, Debug)]
pub(crate) struct UnresolvedContractCall {
pub struct UnresolvedContractCall {
pub contract_name: String,
pub contract_address: Address,
pub function_name: String,
Expand Down
2 changes: 1 addition & 1 deletion runtime/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "graph-runtime-test"
version = "0.23.1"
edition = "2018"

[dev-dependencies]
[dependencies]
semver = "1.0"
wasmtime = "0.27.0"
graph = { path = "../../graph" }
Expand Down
127 changes: 127 additions & 0 deletions runtime/test/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use ethabi::Contract;
use graph::components::store::DeploymentLocator;
use graph::data::subgraph::*;
use graph::ipfs_client::IpfsClient;
use graph::prelude::*;
use graph_chain_ethereum::{Chain, DataSource, DataSourceTemplate};
use graph_runtime_wasm::{HostExports, MappingContext};
use semver::Version;
use std::str::FromStr;
use web3::types::Address;

fn mock_host_exports(
subgraph_id: DeploymentHash,
data_source: DataSource,
store: Arc<impl SubgraphStore>,
api_version: Version,
) -> HostExports<Chain> {
let templates = vec![DataSourceTemplate {
kind: String::from("ethereum/contract"),
name: String::from("example template"),
network: Some(String::from("mainnet")),
source: TemplateSource {
abi: String::from("foo"),
},
mapping: Mapping {
kind: String::from("ethereum/events"),
api_version,
language: String::from("wasm/assemblyscript"),
entities: vec![],
abis: vec![],
event_handlers: vec![],
call_handlers: vec![],
block_handlers: vec![],
link: Link {
link: "link".to_owned(),
},
runtime: Arc::new(vec![]),
},
}];

let network = data_source.network.clone().unwrap();
HostExports::new(
subgraph_id,
&data_source,
network,
Arc::new(templates),
Arc::new(graph_core::LinkResolver::from(IpfsClient::localhost())),
store,
)
}

fn mock_abi() -> MappingABI {
MappingABI {
name: "mock_abi".to_string(),
contract: Contract::load(
r#"[
{
"inputs": [
{
"name": "a",
"type": "address"
}
],
"type": "constructor"
}
]"#
.as_bytes(),
)
.unwrap(),
}
}

pub fn mock_context(
deployment: DeploymentLocator,
data_source: DataSource,
store: Arc<impl SubgraphStore>,
api_version: Version,
) -> MappingContext<Chain> {
MappingContext {
logger: Logger::root(slog::Discard, o!()),
block_ptr: BlockPtr {
hash: Default::default(),
number: 0,
},
host_exports: Arc::new(mock_host_exports(
deployment.hash.clone(),
data_source,
store.clone(),
api_version,
)),
state: BlockState::new(store.writable(&deployment).unwrap(), Default::default()),
proof_of_indexing: None,
host_fns: Arc::new(Vec::new()),
}
}

pub fn mock_data_source(path: &str, api_version: Version) -> DataSource {
let runtime = std::fs::read(path).unwrap();

DataSource {
kind: String::from("ethereum/contract"),
name: String::from("example data source"),
network: Some(String::from("mainnet")),
source: Source {
address: Some(Address::from_str("0123123123012312312301231231230123123123").unwrap()),
abi: String::from("123123"),
start_block: 0,
},
mapping: Mapping {
kind: String::from("ethereum/events"),
api_version,
language: String::from("wasm/assemblyscript"),
entities: vec![],
abis: vec![],
event_handlers: vec![],
call_handlers: vec![],
block_handlers: vec![],
link: Link {
link: "link".to_owned(),
},
runtime: Arc::new(runtime.clone()),
},
context: Default::default(),
creation_block: None,
contract_abi: Arc::new(mock_abi()),
}
}
3 changes: 1 addition & 2 deletions runtime/test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! This crate is for tests only.
pub mod common;
#[cfg(test)]
mod test;
Loading

0 comments on commit c87281e

Please sign in to comment.