Skip to content

Commit

Permalink
native: error handling, bump neon to 1.0.0
Browse files Browse the repository at this point in the history
native: remove panics, bump edition

bump indexer-native version

declare napi 6

workflow to test binaries

back out specified napi versions

fix json

remove node 17 support

restore bin publish workflow

ensure lib name is indexer_native
  • Loading branch information
dwerner committed Aug 2, 2024
1 parent a19fd96 commit 65cdec5
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 102 deletions.
9 changes: 0 additions & 9 deletions .github/workflows/publish-native-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ jobs:
target: x86_64-apple-darwin
- os: ubuntu-22.04
target: x86_64-unknown-linux-gnu
include:
- node_version: 17
system:
os: ubuntu-20.04
target: x86_64-unknown-linux-gnu
- node_version: 17
system:
os: macos-11
target: x86_64-apple-darwin
runs-on: ${{ matrix.system.os }}
steps:
- name: Checkout the repo
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"graphql": "16.8.0"
},
"engines": {
"node": ">=12.22.0"
"node": ">=18.0.0"
},
"jest": {
"transformIgnorePatterns": [
Expand Down
109 changes: 34 additions & 75 deletions packages/indexer-native/native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/indexer-native/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ authors = [
"Theo Butler <[email protected]>",
]
license = "MIT"
edition = "2018"
edition = "2021"

[lib]
name = "indexer_native"
crate-type = ["cdylib"]

[dependencies]
Expand All @@ -17,5 +18,5 @@ arc-swap = "1.2"
eip-712-derive = { git = "https://github.com/graphprotocol/eip-712-derive" }
keccak-hash = "0.10.0"
lazy_static = "1.4"
neon = { version = "0.10", default-features = false, features = ["napi-6"] }
neon = { version = "1.0.0", default-features = false, features = ["napi-6"] }
secp256k1 = { version = "0.27", features = ["recovery"] }
56 changes: 46 additions & 10 deletions packages/indexer-native/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,66 @@ use signature_verification::SignatureVerifier;

pub struct SignatureVerifierProxy;

// Lifting neon's `or_throw` extension trait pattern here.
trait ResultDbgExt<T> {
fn or_else_throw<'a, C: Context<'a>>(self, cx: &mut C) -> NeonResult<T>;
}
impl<T, E: std::fmt::Debug> ResultDbgExt<T> for Result<T, E> {
fn or_else_throw<'a, C: Context<'a>>(self, cx: &mut C) -> NeonResult<T> {
self.or_else(|err| cx.throw_error(format!("{err:?}")))
}
}

fn signature_verifier_new(mut cx: FunctionContext) -> JsResult<JsBox<SignatureVerifier>> {
let address: Address = cx.argument::<JsString>(0)?.value(&mut cx).parse().unwrap();
let address: Address = cx
.argument::<JsString>(0)?
.value(&mut cx)
.parse()
.or_else_throw(&mut cx)?;
Ok(cx.boxed(SignatureVerifier::new(address)))
}

fn signature_verifier_verify(mut cx: FunctionContext) -> JsResult<JsBoolean> {
let this = cx.argument::<JsBox<SignatureVerifier>>(0)?;
let message: Bytes = cx.argument::<JsString>(1)?.value(&mut cx).parse().unwrap();
let signature: FixedBytes<65> = cx.argument::<JsString>(2)?.value(&mut cx).parse().unwrap();
let message: Bytes = cx
.argument::<JsString>(1)?
.value(&mut cx)
.parse()
.or_else_throw(&mut cx)?;
let signature: FixedBytes<65> = cx
.argument::<JsString>(2)?
.value(&mut cx)
.parse()
.or_else_throw(&mut cx)?;
let recovery_id = signature[64] as i32;
let recovery_id = match recovery_id {
0 | 1 => RecoveryId::from_i32(recovery_id).unwrap(),
27 | 28 => RecoveryId::from_i32(recovery_id - 27).unwrap(),
0 | 1 => RecoveryId::from_i32(recovery_id).or_else_throw(&mut cx)?,
27 | 28 => RecoveryId::from_i32(recovery_id - 27).or_else_throw(&mut cx)?,
_ => panic!("Invalid recovery id"),
};
let signature = RecoverableSignature::from_compact(&signature[..64], recovery_id).unwrap();
Ok(cx.boolean(this.verify(&message, &signature).unwrap()))
let signature =
RecoverableSignature::from_compact(&signature[..64], recovery_id).or_else_throw(&mut cx)?;
let verified = this.verify(&message, &signature).or_else_throw(&mut cx)?;
Ok(cx.boolean(verified))
}

fn attestation_signer_new(mut cx: FunctionContext) -> JsResult<JsBox<AttestationSigner>> {
let chain_id = cx.argument::<JsNumber>(0)?.value(&mut cx) as u64;
let dispute_manager: Address = cx.argument::<JsString>(1)?.value(&mut cx).parse().unwrap();
let signer: B256 = cx.argument::<JsString>(2)?.value(&mut cx).parse().unwrap();
let subgraph_deployment_id: B256 = cx.argument::<JsString>(3)?.value(&mut cx).parse().unwrap();
let dispute_manager: Address = cx
.argument::<JsString>(1)?
.value(&mut cx)
.parse()
.or_else_throw(&mut cx)?;
let signer: B256 = cx
.argument::<JsString>(2)?
.value(&mut cx)
.parse()
.or_else_throw(&mut cx)?;
let subgraph_deployment_id: B256 = cx
.argument::<JsString>(3)?
.value(&mut cx)
.parse()
.or_else_throw(&mut cx)?;
Ok(cx.boxed(AttestationSigner::new(
U256::from(chain_id),
dispute_manager,
Expand Down
33 changes: 29 additions & 4 deletions packages/indexer-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphprotocol/indexer-native",
"version": "0.20.11",
"version": "0.21.4",
"description": "Performance sensitive indexer code",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand All @@ -16,13 +16,31 @@
},
"author": "Zac Burns <[email protected]>",
"license": "MIT",
"os": [
"darwin",
"linux"
],
"cpu": [
"x64",
"arm",
"arm64"
],
"engines": {
"node": ">=12.22.0"
},
"scripts": {
"build": "cd native && cargo-cp-artifact -nc ../binary/index.node -- cargo build --release --message-format=json-render-diagnostics",
"build": "cd native && cargo-cp-artifact -a cdylib indexer_native ../binary/index.node -- cargo build --message-format=json-render-diagnostics",
"build-debug": "yarn build --",
"build-release": "yarn build --release",
"pull-or-build": "node-pre-gyp install --fallback-to-build=false --update-binary || yarn build-release",
"package": "node-pre-gyp package",
"publish-github-draft": "node-pre-gyp-github publish",
"publish-github": "node-pre-gyp-github publish --release",
"build-test-pack-publish": "yarn build-release && yarn test && yarn package && yarn publish-github-draft",
"format": "prettier --write 'lib/**/*.js'",
"lint": "eslint .",
"prepare": "yarn format && yarn lint",
"test:ci": "jest --verbose --ci",
"install": "yarn build",
"install": "yarn pull-or-build",
"test": "yarn build && jest --colors --verbose --forceExit",
"test:ci": "yarn build && jest --verbose --ci",
"clean": "rm -rf ./node_modules ./binary ./build ./coverage ./native/target ./native/artifacts.json ./native/index.node"
Expand All @@ -41,5 +59,12 @@
"jest": "<30.0.0-0",
"prettier": "3.0.3"
},
"binary": {
"module_name": "index",
"module_path": "./binary",
"host": "https://github.com/graphprotocol/indexer/releases/download/",
"remote_path": "v{version}",
"package_name": "graphprotocol-indexer-native-v{version}-{node_abi}-{platform}-{arch}.tar.gz"
},
"gitHead": "972ab96774007b2aee15b1da169d2ff4be9f9d27"
}
2 changes: 1 addition & 1 deletion packages/indexer-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@google-cloud/profiler": "6.0.1",
"@graphprotocol/common-ts": "2.0.9",
"@graphprotocol/indexer-common": "^0.21.4",
"@graphprotocol/indexer-native": "0.20.11",
"@graphprotocol/indexer-native": "0.21.4",
"@graphql-tools/load": "8.0.0",
"@graphql-tools/url-loader": "8.0.0",
"@graphql-tools/wrap": "10.0.1",
Expand Down

0 comments on commit 65cdec5

Please sign in to comment.