From 975117c6dfcfb30d446bbee5e19aa6a3c38b7d9d Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Wed, 1 Nov 2023 16:04:17 +0000 Subject: [PATCH 1/4] tests: Don't compare WASM backtraces While it would be nice to test them in isolation, here it is just making these high-level tests too sensitive to unrelated changes. --- tests/tests/runner_tests.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/tests/tests/runner_tests.rs b/tests/tests/runner_tests.rs index 7411e5f3176..afa250c0bc5 100644 --- a/tests/tests/runner_tests.rs +++ b/tests/tests/runner_tests.rs @@ -69,6 +69,31 @@ impl RunnerTestRecipe { } } +fn assert_eq_ignore_backtrace(err: &SubgraphError, expected: &SubgraphError) { + let equal = { + if err.subgraph_id != expected.subgraph_id + || err.block_ptr != expected.block_ptr + || err.handler != expected.handler + || err.deterministic != expected.deterministic + { + false; + } + + // Ignore any WASM backtrace in the error message + let split_err: Vec<&str> = err.message.split("\\twasm backtrace:").collect(); + let split_expected: Vec<&str> = expected.message.split("\\twasm backtrace:").collect(); + + split_err.get(0) == split_expected.get(0) + }; + + if !equal { + // Will fail + let mut err_no_trace = err.clone(); + err_no_trace.message = expected.message.split("\\twasm backtrace:").collect(); + assert_eq!(&err_no_trace, expected); + } +} + #[tokio::test] async fn data_source_revert() -> anyhow::Result<()> { let RunnerTestRecipe { @@ -230,7 +255,7 @@ async fn api_version_0_0_8() { let ctx = fixture::setup(subgraph_name.clone(), &hash, &stores, &chain, None, None).await; let stop_block = blocks.last().unwrap().block.ptr(); let err = ctx.start_and_sync_to_error(stop_block.clone()).await; - let message = "transaction 0000000000000000000000000000000000000000000000000000000000000000: Attempted to set undefined fields [invalid_field] for the entity type `TestResult`. Make sure those fields are defined in the schema.\twasm backtrace:\t 0: 0x2ebc - !src/mapping/handleTestEvent\t in handler `handleTestEvent` at block #1 (0000000000000000000000000000000000000000000000000000000000000001)".to_string(); + let message = "transaction 0000000000000000000000000000000000000000000000000000000000000000: Attempted to set undefined fields [invalid_field] for the entity type `TestResult`. Make sure those fields are defined in the schema.".to_string(); let expected_err = SubgraphError { subgraph_id: ctx.deployment.hash.clone(), message, @@ -238,7 +263,7 @@ async fn api_version_0_0_8() { handler: None, deterministic: true, }; - assert_eq!(err, expected_err); + assert_eq_ignore_backtrace(&err, &expected_err); } #[tokio::test] @@ -519,7 +544,8 @@ async fn file_data_sources() { let stop_block = test_ptr(7); let err = ctx.start_and_sync_to_error(stop_block.clone()).await; let message = "entity type `IpfsFile1` is not on the 'entities' list for data source `File2`. \ - Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`.\twasm backtrace:\t 0: 0x3737 - !src/mapping/handleFile1\t in handler `handleFile1` at block #7 ()".to_string(); + Hint: Add `IpfsFile1` to the 'entities' list, which currently is: `IpfsFile`." + .to_string(); let expected_err = SubgraphError { subgraph_id: ctx.deployment.hash.clone(), message, @@ -527,7 +553,7 @@ async fn file_data_sources() { handler: None, deterministic: false, }; - assert_eq!(err, expected_err); + assert_eq_ignore_backtrace(&err, &expected_err); // Unfail the subgraph to test a conflict between an onchain and offchain entity { From 852b06a3fc547e47a6193d6342e4b30bfd62d6d5 Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Wed, 1 Nov 2023 16:09:45 +0000 Subject: [PATCH 2/4] tests: Test PoI as hex and update to current value This changed probably because graph-cli and -ts were updated. --- tests/tests/runner_tests.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/tests/runner_tests.rs b/tests/tests/runner_tests.rs index afa250c0bc5..936ab129f66 100644 --- a/tests/tests/runner_tests.rs +++ b/tests/tests/runner_tests.rs @@ -16,7 +16,7 @@ use graph::ipfs_client::IpfsClient; use graph::object; use graph::prelude::ethabi::ethereum_types::H256; use graph::prelude::{ - CheapClone, DeploymentHash, SubgraphAssignmentProvider, SubgraphName, SubgraphStore, + hex, CheapClone, DeploymentHash, SubgraphAssignmentProvider, SubgraphName, SubgraphStore, }; use graph_tests::fixture::ethereum::{ chain, empty_block, generate_empty_blocks_for_range, genesis, push_test_log, @@ -809,11 +809,8 @@ async fn template_static_filters_false_positives() { // POI table. If this fails it's likely that either the bug was re-introduced or there is // a change in the POI infrastructure. Or the subgraph id changed. assert_eq!( - poi.unwrap(), - [ - 253, 249, 50, 171, 127, 117, 77, 13, 79, 132, 88, 246, 223, 214, 225, 39, 112, 19, 73, - 97, 193, 132, 103, 19, 191, 5, 28, 14, 232, 137, 76, 9 - ], + hex::encode(poi.unwrap()), + "c72af01a19a4e35a35778821a354b7a781062a9320ac8796ea65b115cb9844bf" ); } From fbbb13113b441e974701f40a86a8a63bd65d75be Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Wed, 1 Nov 2023 18:56:33 +0000 Subject: [PATCH 3/4] tests: Make `data_source_revert` more robust to code updates Previously it would break whenever the graft base subgraph id would change, now that id is updated automatically. --- tests/runner-tests/api-version/subgraph.yaml | 2 +- .../data-source-revert/grafted.yaml | 5 ++-- .../data-source-revert/package.json | 4 +-- .../data-source-revert/updateAndDeploy.js | 17 +++++++++++ tests/tests/runner_tests.rs | 29 ++++++++++++++----- 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 tests/runner-tests/data-source-revert/updateAndDeploy.js diff --git a/tests/runner-tests/api-version/subgraph.yaml b/tests/runner-tests/api-version/subgraph.yaml index 464a10d3f0c..39d6a73190c 100644 --- a/tests/runner-tests/api-version/subgraph.yaml +++ b/tests/runner-tests/api-version/subgraph.yaml @@ -10,7 +10,7 @@ dataSources: abi: Contract mapping: kind: ethereum/events - apiVersion: 0.0.8 + apiVersion: 0.0.7 language: wasm/assemblyscript abis: - name: Contract diff --git a/tests/runner-tests/data-source-revert/grafted.yaml b/tests/runner-tests/data-source-revert/grafted.yaml index ac5fe04d971..9992583098b 100644 --- a/tests/runner-tests/data-source-revert/grafted.yaml +++ b/tests/runner-tests/data-source-revert/grafted.yaml @@ -4,8 +4,9 @@ features: schema: file: ./schema.graphql graft: - # Must match the id from building `subgraph.yaml` - base: QmR3TAJuimii5huAswW7uqSzJfsR4GmAxwfawK2DwF3YFh + # This can be overwritten by `updateAndDeploy.js`. + # Please commit this file when this happens. + base: QmcAL39QSKZvRssr2ToCJrav7XK9ggajxvBR7M1NNUCqdh block: 3 dataSources: - kind: ethereum/contract diff --git a/tests/runner-tests/data-source-revert/package.json b/tests/runner-tests/data-source-revert/package.json index 47d28fc84ce..05b74b33945 100644 --- a/tests/runner-tests/data-source-revert/package.json +++ b/tests/runner-tests/data-source-revert/package.json @@ -4,10 +4,10 @@ "scripts": { "codegen": "graph codegen --skip-migrations", "deploy:test": "graph deploy test/data-source-revert --version-label v0.0.1 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI", - "deploy:test-grafted": "graph deploy test/data-source-revert-grafted grafted.yaml --version-label v0.0.1 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI" + "deploy:test-grafted": "node updateAndDeploy.js" }, "devDependencies": { "@graphprotocol/graph-cli": "0.60.0", "@graphprotocol/graph-ts": "0.31.0" } -} +} \ No newline at end of file diff --git a/tests/runner-tests/data-source-revert/updateAndDeploy.js b/tests/runner-tests/data-source-revert/updateAndDeploy.js new file mode 100644 index 00000000000..d7bc0971346 --- /dev/null +++ b/tests/runner-tests/data-source-revert/updateAndDeploy.js @@ -0,0 +1,17 @@ +// This takes a Qm.. hash as a CLI input, which is the graft base. + +const fs = require('fs'); +const { execSync } = require('child_process'); + +const graftBase = process.argv[2]; + +const yamlPath = './grafted.yaml'; +let yamlContent = fs.readFileSync(yamlPath, 'utf-8'); +yamlContent = yamlContent.replace(/base: .+/, `base: ${graftBase}`); +fs.writeFileSync(yamlPath, yamlContent); +console.log("bafuzz") + +// Assuming you have your IPFS_URI and GRAPH_NODE_ADMIN_URI exported as environment variables. +execSync('graph deploy test/data-source-revert-grafted grafted.yaml --version-label v0.0.1 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI', { + stdio: 'inherit' +}); diff --git a/tests/tests/runner_tests.rs b/tests/tests/runner_tests.rs index 936ab129f66..e8eafef5e8e 100644 --- a/tests/tests/runner_tests.rs +++ b/tests/tests/runner_tests.rs @@ -129,9 +129,12 @@ async fn data_source_revert() -> anyhow::Result<()> { // Test grafted version let subgraph_name = SubgraphName::new("data-source-revert-grafted").unwrap(); - let hash = - build_subgraph_with_yarn_cmd("./runner-tests/data-source-revert", "deploy:test-grafted") - .await; + let hash = build_subgraph_with_yarn_cmd_and_arg( + "./runner-tests/data-source-revert", + "deploy:test-grafted", + Some(&hash), + ) + .await; let graft_block = Some(test_ptr(3)); let ctx = fixture::setup( subgraph_name.clone(), @@ -1076,6 +1079,14 @@ async fn build_subgraph(dir: &str, deploy_cmd: Option<&str>) -> DeploymentHash { } async fn build_subgraph_with_yarn_cmd(dir: &str, yarn_cmd: &str) -> DeploymentHash { + build_subgraph_with_yarn_cmd_and_arg(dir, yarn_cmd, None).await +} + +async fn build_subgraph_with_yarn_cmd_and_arg( + dir: &str, + yarn_cmd: &str, + arg: Option<&str>, +) -> DeploymentHash { // Test that IPFS is up. IpfsClient::localhost() .test() @@ -1095,11 +1106,14 @@ async fn build_subgraph_with_yarn_cmd(dir: &str, yarn_cmd: &str) -> DeploymentHa // Run codegen. run_cmd(Command::new("yarn").arg("codegen").current_dir(dir)); + let mut args = vec![yarn_cmd]; + args.extend(arg); + // Run `deploy` for the side effect of uploading to IPFS, the graph node url // is fake and the actual deploy call is meant to fail. let deploy_output = run_cmd( Command::new("yarn") - .arg(yarn_cmd) + .args(&args) .env("IPFS_URI", "http://127.0.0.1:5001") .env("GRAPH_NODE_ADMIN_URI", "http://localhost:0") .current_dir(dir), @@ -1107,10 +1121,9 @@ async fn build_subgraph_with_yarn_cmd(dir: &str, yarn_cmd: &str) -> DeploymentHa // Hack to extract deployment id from `graph deploy` output. const ID_PREFIX: &str = "Build completed: "; - let mut line = deploy_output - .lines() - .find(|line| line.contains(ID_PREFIX)) - .expect("found no matching line"); + let Some(mut line) = deploy_output.lines().find(|line| line.contains(ID_PREFIX)) else { + panic!("No deployment id found, graph deploy probably had an error") + }; if !line.starts_with(ID_PREFIX) { line = &line[5..line.len() - 5]; // workaround for colored output } From be355ca884165774aa123f829af3b3c088ee2db4 Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Wed, 1 Nov 2023 20:20:30 +0000 Subject: [PATCH 4/4] Update tests/runner-tests/data-source-revert/package.json Co-authored-by: Saihajpreet Singh --- tests/runner-tests/data-source-revert/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/runner-tests/data-source-revert/package.json b/tests/runner-tests/data-source-revert/package.json index 05b74b33945..a8ed0e1b473 100644 --- a/tests/runner-tests/data-source-revert/package.json +++ b/tests/runner-tests/data-source-revert/package.json @@ -10,4 +10,4 @@ "@graphprotocol/graph-cli": "0.60.0", "@graphprotocol/graph-ts": "0.31.0" } -} \ No newline at end of file +}