forked from graphprotocol/graph-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create public common module in test crate (graphprotocol#2678)
* 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
1 parent
619e259
commit c87281e
Showing
9 changed files
with
259 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.