Skip to content

Commit

Permalink
wip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evaporei committed Apr 8, 2021
1 parent b63b3a5 commit 9e2e604
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 23 deletions.
21 changes: 18 additions & 3 deletions runtime/wasm/src/host_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,20 +813,35 @@ pub(crate) fn bytes_to_string(logger: &Logger, bytes: Vec<u8>) -> String {
s.trim_end_matches('\u{0000}').to_string()
}

pub(crate) fn abi_encode(params: Vec<Token>) -> Result<Vec<u8>, anyhow::Error> {
pub(crate) fn ethereum_encode(params: Vec<Token>) -> Result<Vec<u8>, anyhow::Error> {
println!("---------------INSIDE ENCODE {:?}", params);
Ok(encode(&params))
}

pub(crate) fn abi_decode(types: String, data: Vec<u8>) -> Result<Token, anyhow::Error> {
pub(crate) fn ethereum_decode(types: String, data: Vec<u8>) -> Result<Token, anyhow::Error> {
println!("---------------INSIDE DECODE {} {:?}", types, data);
let param_types =
Reader::read(&types).or_else(|e| Err(anyhow::anyhow!("Failed to read types: {}", e)))?;
Reader::read(&types).or_else(|e| {
println!("-------------------INSIDE DECODE Reader::read err {:?}", e);
Err(anyhow::anyhow!("Failed to read types: {}", e))
})?;

println!("---------------INSIDE DECODE param_types {:?}", param_types);

decode(&[param_types], &data)
.map(|a| {
println!("-------------------INSIDE DECODE map {:?}", a);
a
})
// The `.pop().unwrap()` here is ok because we're always only passing one
// `param_types` to `decode`, so the returned `Vec` has always size of one.
// We can't do `tokens[0]` because the value can't be moved out of the `Vec`.
.map(|mut tokens| tokens.pop().unwrap())
.context("Failed to decode")
.map_err(|e| {
println!("-------------------INSIDE DECODE decode err {:?}", e);
e
})
}

#[test]
Expand Down
19 changes: 12 additions & 7 deletions runtime/wasm/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ impl WasmInstance {
})?;
}

link!("ethereum.encode", ethereum_encode, params_ptr);
link!("ethereum.decode", ethereum_decode, params_ptr, data_ptr);

link!("abort", abort, message_ptr, file_name_ptr, line, column);

link!("store.get", store_get, "host_export_store_get", entity, id);
Expand Down Expand Up @@ -554,9 +557,6 @@ impl WasmInstance {

link!("log.log", log_log, level, msg_ptr);

link!("abi.encode", abi_encode, params_ptr);
link!("abi.decode", abi_decode, params_ptr, data_ptr);

link!("arweave.transactionData", arweave_transaction_data, ptr);

link!("box.profile", box_profile, ptr);
Expand Down Expand Up @@ -1373,23 +1373,28 @@ impl WasmInstanceContext {
}

/// function encode(params: Array<ethereum.Value>): Bytes | null
fn abi_encode(
fn ethereum_encode(
&mut self,
params_ptr: AscPtr<Array<AscPtr<AscEnum<EthereumValueKind>>>>,
) -> Result<AscPtr<Uint8Array>, DeterministicHostError> {
let data = host_exports::abi_encode(self.asc_get(params_ptr)?);
println!("--------INSIDE ENCODE CONVERSIONS");
println!("--------INSIDE ENCODE CONVERSIONS");
println!("--------INSIDE ENCODE CONVERSIONS");
println!("--------INSIDE ENCODE CONVERSIONS");
let data = host_exports::ethereum_encode(self.asc_get(params_ptr)?);
// return `null` if it fails
data.map(|bytes| self.asc_new(&*bytes))
.unwrap_or(Ok(AscPtr::null()))
}

/// function decode(types: String, data: Bytes): Array<ethereum.Value> | null
fn abi_decode(
fn ethereum_decode(
&mut self,
types_ptr: AscPtr<AscString>,
data_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<AscEnum<EthereumValueKind>>, DeterministicHostError> {
let result = host_exports::abi_decode(self.asc_get(types_ptr)?, self.asc_get(data_ptr)?);
println!("--------INSIDE DECODE CONVERSIONS");
let result = host_exports::ethereum_decode(self.asc_get(types_ptr)?, self.asc_get(data_ptr)?);
// return `null` if it fails
result
.map(|param| self.asc_new(&param))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/big-decimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"devDependencies": {
"@graphprotocol/graph-cli": "https://github.com/graphprotocol/graph-cli#master",
"@graphprotocol/graph-ts": "https://github.com/graphprotocol/graph-ts#master",
"@graphprotocol/graph-ts": "https://github.com/graphprotocol/graph-ts#otavio\/support-encode-decode-hosted-abi",
"solc": "^0.8.2"
},
"dependencies": {
Expand Down
35 changes: 34 additions & 1 deletion tests/integration-tests/big-decimal/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import { Trigger } from "../generated/Contract/Contract";
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { Address, BigDecimal, BigInt, ethereum, log } from "@graphprotocol/graph-ts";

// Test that host exports work in globals.
let one = BigDecimal.fromString("1");

export function handleTrigger(event: Trigger): void {
log.info('-------------oi {}', ['1'])
log.error('------------oi {}', ['2'])
log.debug('------------oi {}', ['3'])
log.warning('------------oi {}', ['4'])
log.critical('------------oi {}', ['5'])
// throw new Error('errrooooooo')

let params: Array<ethereum.Value> = [
ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(64)),
ethereum.Value.fromAddress(Address.fromString("0x0000000000000000000000000000000000000420"))
];
log.critical('critical---------------params {}', [params[0].toString(), params[1].toString()])
log.error('error--------------params {}', [params[0].toString(), params[1].toString()])
log.warning('warning--------------params {}', [params[0].toString(), params[1].toString()])
log.info('info---------------params {}', [params[0].toString(), params[1].toString()])
log.debug('debug-----------------params {}', [params[0].toString(), params[1].toString()])

let encoded = ethereum.encode(params);
log.critical('critical----------------------encoded {}', [encoded.toString()])
log.error('error----------------------encoded {}', [encoded.toString()])
log.warning('warning----------------------encoded {}', [encoded.toString()])
log.info('info----------------------encoded {}', [encoded.toString()])
log.debug('debug----------------------encoded {}', [encoded.toString()])

// log.info("Encoded data {}", [encoded.toHexString()])

let decoded = ethereum.decode("[uint256,address]", encoded!);
log.critical('critical------------------decoded {}', [decoded.toString()])
log.error('error------------------decoded {}', [decoded.toString()])
log.warning('warning------------------decoded {}', [decoded.toString()])
log.info('info------------------decoded {}', [decoded.toString()])
log.debug('debug------------------decoded {}', [decoded.toString()])

// There are 35 digits after the dot.
// big_decimal exponent will be: -35 - 6109 = -6144.
// Minimum exponent is: -6143.
Expand Down
4 changes: 2 additions & 2 deletions tests/integration-tests/big-decimal/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const exec = (cmd) => {

const waitForSubgraphToBeSynced = async () =>
new Promise((resolve, reject) => {
// Wait for 60s
let deadline = Date.now() + 60 * 1000;
// Wait for 2 minutes
let deadline = Date.now() + 60 * 1000 * 2;

// Function to check if the subgraph is synced
const checkSubgraphSynced = async () => {
Expand Down
6 changes: 6 additions & 0 deletions tests/integration-tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,12 @@
dependencies:
assemblyscript "https://github.com/AssemblyScript/assemblyscript#36040d5b5312f19a025782b5e36663823494c2f3"

"@graphprotocol/graph-ts@https://github.com/graphprotocol/graph-ts#otavio/support-encode-decode-hosted-abi":
version "0.20.0"
resolved "https://github.com/graphprotocol/graph-ts#81d6540f2eb27c1fb5fdd68a866b1250ee43f669"
dependencies:
assemblyscript "https://github.com/AssemblyScript/assemblyscript#36040d5b5312f19a025782b5e36663823494c2f3"

"@graphql-tools/batch-delegate@^6.2.4", "@graphql-tools/batch-delegate@^6.2.6":
version "6.2.6"
resolved "https://registry.yarnpkg.com/@graphql-tools/batch-delegate/-/batch-delegate-6.2.6.tgz#fbea98dc825f87ef29ea5f3f371912c2a2aa2f2c"
Expand Down
19 changes: 10 additions & 9 deletions tests/tests/parallel_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ use tokio::process::{Child, Command};
const DEFAULT_N_CONCURRENT_TESTS: usize = 15;

/// All integration tests subdirectories to run
pub const INTEGRATION_TESTS_DIRECTORIES: [&str; 9] = [
// pub const INTEGRATION_TESTS_DIRECTORIES: [&str; 9] = [
pub const INTEGRATION_TESTS_DIRECTORIES: [&str; 1] = [
// "arweave-and-3box",
"big-decimal",
"data-source-context",
"data-source-revert",
"fatal-error",
"ganache-reverts",
"non-fatal-errors",
"overloaded-contract-functions",
"remove-then-update",
"value-roundtrip",
// "data-source-context",
// "data-source-revert",
// "fatal-error",
// "ganache-reverts",
// "non-fatal-errors",
// "overloaded-contract-functions",
// "remove-then-update",
// "value-roundtrip",
];

/// Contains all information a test command needs
Expand Down

0 comments on commit 9e2e604

Please sign in to comment.