Pass provider while instantiating the loader and use block hash while making the eth-calls (#74)

* Pass provider while instantiating the loader

* Use custom provider for graph-node tests

* Use block hash while making an eth-call
This commit is contained in:
prathamesh0 2021-12-08 18:24:46 +05:30 committed by nabarun
parent 3d3ebb0e43
commit 3b835e81f8
9 changed files with 82 additions and 24 deletions

View File

@ -7,10 +7,12 @@ import chai, { assert, expect } from 'chai';
import spies from 'chai-spies'; import spies from 'chai-spies';
import { utils } from 'ethers'; import { utils } from 'ethers';
import { getDummyEventData, getDummyGraphData, getTestDatabase, getTestIndexer } from '../test/utils'; import { BaseProvider } from '@ethersproject/providers';
import { getDummyEventData, getDummyGraphData, getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
import abi from '../test/subgraph/example1/build/Example1/abis/Example1.json'; import abi from '../test/subgraph/example1/build/Example1/abis/Example1.json';
import { instantiate } from './loader'; import { instantiate } from './loader';
import { createEvent, createBlock, Block } from './utils'; import { createEvent, createBlock, Block, EventData } from './utils';
import { Database } from './database'; import { Database } from './database';
import { Indexer } from '../test/utils/indexer'; import { Indexer } from '../test/utils/indexer';
@ -22,14 +24,19 @@ describe('call handler in mapping code', () => {
let exports: any; let exports: any;
let db: Database; let db: Database;
let indexer: Indexer; let indexer: Indexer;
let provider: BaseProvider;
// Create dummy test data. let dummyEventData: EventData;
const dummyEventData = getDummyEventData(); let dummyGraphData: any;
const dummyGraphData = getDummyGraphData();
before(async () => { before(async () => {
db = getTestDatabase(); db = getTestDatabase();
indexer = getTestIndexer(); indexer = getTestIndexer();
provider = getTestProvider();
// Create dummy test data.
dummyEventData = await getDummyEventData();
dummyGraphData = getDummyGraphData();
sandbox.on(indexer, 'createDiffStaged', (contractAddress: string, blockHash: string, data: any) => { sandbox.on(indexer, 'createDiffStaged', (contractAddress: string, blockHash: string, data: any) => {
assert(contractAddress); assert(contractAddress);
@ -63,6 +70,7 @@ describe('call handler in mapping code', () => {
const instance = await instantiate( const instance = await instantiate(
db, db,
indexer, indexer,
provider,
{ event: { block: dummyEventData.block } }, { event: { block: dummyEventData.block } },
filePath, filePath,
dummyGraphData dummyGraphData

View File

@ -8,12 +8,14 @@ import path from 'path';
import chai from 'chai'; import chai from 'chai';
import spies from 'chai-spies'; import spies from 'chai-spies';
import { BaseProvider } from '@ethersproject/providers';
import { instantiate } from './loader'; import { instantiate } from './loader';
import { createEvent, Block, createBlock } from './utils'; import { createEvent, Block, createBlock, EventData } from './utils';
import edenNetworkAbi from '../test/subgraph/eden/EdenNetwork/abis/EdenNetwork.json'; import edenNetworkAbi from '../test/subgraph/eden/EdenNetwork/abis/EdenNetwork.json';
import merkleDistributorAbi from '../test/subgraph/eden/EdenNetworkDistribution/abis/MerkleDistributor.json'; import merkleDistributorAbi from '../test/subgraph/eden/EdenNetworkDistribution/abis/MerkleDistributor.json';
import distributorGovernanceAbi from '../test/subgraph/eden/EdenNetworkGovernance/abis/DistributorGovernance.json'; import distributorGovernanceAbi from '../test/subgraph/eden/EdenNetworkGovernance/abis/DistributorGovernance.json';
import { getDummyEventData, getTestDatabase, getTestIndexer } from '../test/utils'; import { getDummyEventData, getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
import { Database } from './database'; import { Database } from './database';
import { Indexer } from '../test/utils/indexer'; import { Indexer } from '../test/utils/indexer';
@ -26,13 +28,17 @@ const sandbox = chai.spy.sandbox();
describe('eden wasm loader tests', async () => { describe('eden wasm loader tests', async () => {
let db: Database; let db: Database;
let indexer: Indexer; let indexer: Indexer;
let provider: BaseProvider;
// Create dummy event data. let dummyEventData: EventData;
const dummyEventData = getDummyEventData();
before(async () => { before(async () => {
db = getTestDatabase(); db = getTestDatabase();
indexer = getTestIndexer(); indexer = getTestIndexer();
provider = getTestProvider();
// Create dummy test data.
dummyEventData = await getDummyEventData();
sandbox.on(indexer, 'createDiffStaged', (contractAddress: string, blockHash: string, data: any) => { sandbox.on(indexer, 'createDiffStaged', (contractAddress: string, blockHash: string, data: any) => {
assert(contractAddress); assert(contractAddress);
@ -81,6 +87,7 @@ describe('eden wasm loader tests', async () => {
({ exports } = await instantiate( ({ exports } = await instantiate(
db, db,
indexer, indexer,
provider,
{ event: { block: dummyEventData.block } }, { event: { block: dummyEventData.block } },
filePath, filePath,
data data
@ -195,6 +202,7 @@ describe('eden wasm loader tests', async () => {
const filePath = path.resolve(__dirname, '../test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm'); const filePath = path.resolve(__dirname, '../test/subgraph/eden/EdenNetworkDistribution/EdenNetworkDistribution.wasm');
({ exports } = await instantiate(db, ({ exports } = await instantiate(db,
indexer, indexer,
provider,
{ event: { block: dummyEventData.block } }, { event: { block: dummyEventData.block } },
filePath, filePath,
data data
@ -307,6 +315,7 @@ describe('eden wasm loader tests', async () => {
({ exports } = await instantiate( ({ exports } = await instantiate(
db, db,
indexer, indexer,
provider,
{ event: { block: dummyEventData.block } }, { event: { block: dummyEventData.block } },
filePath, filePath,
data data

View File

@ -5,16 +5,20 @@
import assert from 'assert'; import assert from 'assert';
import path from 'path'; import path from 'path';
import { BaseProvider } from '@ethersproject/providers';
import { instantiate } from './loader'; import { instantiate } from './loader';
import exampleAbi from '../test/subgraph/example1/build/Example1/abis/Example1.json'; import exampleAbi from '../test/subgraph/example1/build/Example1/abis/Example1.json';
import { getTestDatabase, getTestIndexer } from '../test/utils'; import { getTestDatabase, getTestIndexer, getTestProvider, getDummyEventData } from '../test/utils';
import { Database } from './database'; import { Database } from './database';
import { Indexer } from '../test/utils/indexer'; import { Indexer } from '../test/utils/indexer';
import { EventData } from './utils';
describe('eth-call wasm tests', () => { describe('eth-call wasm tests', () => {
let exports: any; let exports: any;
let db: Database; let db: Database;
let indexer: Indexer; let indexer: Indexer;
let provider: BaseProvider;
const contractAddress = process.env.EXAMPLE_CONTRACT_ADDRESS; const contractAddress = process.env.EXAMPLE_CONTRACT_ADDRESS;
assert(contractAddress); assert(contractAddress);
@ -28,9 +32,15 @@ describe('eth-call wasm tests', () => {
} }
}; };
let dummyEventData: EventData;
before(async () => { before(async () => {
db = getTestDatabase(); db = getTestDatabase();
indexer = getTestIndexer(); indexer = getTestIndexer();
provider = getTestProvider();
// Create dummy test data.
dummyEventData = await getDummyEventData();
}); });
it('should load the subgraph example wasm', async () => { it('should load the subgraph example wasm', async () => {
@ -38,7 +48,8 @@ describe('eth-call wasm tests', () => {
const instance = await instantiate( const instance = await instantiate(
db, db,
indexer, indexer,
{ event: {} }, provider,
{ event: { block: dummyEventData.block } },
filePath, filePath,
data data
); );

View File

@ -5,8 +5,10 @@
import path from 'path'; import path from 'path';
import { expect } from 'chai'; import { expect } from 'chai';
import { BaseProvider } from '@ethersproject/providers';
import { instantiate } from './loader'; import { instantiate } from './loader';
import { getTestDatabase, getTestIndexer } from '../test/utils'; import { getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
import { Database } from './database'; import { Database } from './database';
import { Indexer } from '../test/utils/indexer'; import { Indexer } from '../test/utils/indexer';
@ -16,15 +18,18 @@ describe('wasm loader tests', () => {
let exports: any; let exports: any;
let db: Database; let db: Database;
let indexer: Indexer; let indexer: Indexer;
let provider: BaseProvider;
before(async () => { before(async () => {
db = getTestDatabase(); db = getTestDatabase();
indexer = getTestIndexer(); indexer = getTestIndexer();
provider = getTestProvider();
const filePath = path.resolve(__dirname, WASM_FILE_PATH); const filePath = path.resolve(__dirname, WASM_FILE_PATH);
const instance = await instantiate( const instance = await instantiate(
db, db,
indexer, indexer,
provider,
{ event: {} }, { event: {} },
filePath filePath
); );

View File

@ -7,7 +7,6 @@ import fs from 'fs/promises';
import { import {
utils, utils,
BigNumber, BigNumber,
getDefaultProvider,
Contract, Contract,
ContractInterface ContractInterface
} from 'ethers'; } from 'ethers';
@ -15,6 +14,7 @@ import JSONbig from 'json-bigint';
import BN from 'bn.js'; import BN from 'bn.js';
import debug from 'debug'; import debug from 'debug';
import { BaseProvider } from '@ethersproject/providers';
import loader from '@vulcanize/assemblyscript/lib/loader'; import loader from '@vulcanize/assemblyscript/lib/loader';
import { IndexerInterface, GraphDecimal, getGraphDigitsAndExp } from '@vulcanize/util'; import { IndexerInterface, GraphDecimal, getGraphDigitsAndExp } from '@vulcanize/util';
@ -27,8 +27,6 @@ import {
} from './utils'; } from './utils';
import { Database } from './database'; import { Database } from './database';
const NETWORK_URL = 'http://127.0.0.1:8081';
// Endianness of BN used in bigInt store host API. // Endianness of BN used in bigInt store host API.
// Negative bigInt is being stored in wasm in 2's compliment, 'le' representation. // Negative bigInt is being stored in wasm in 2's compliment, 'le' representation.
// (for eg. bigInt.fromString(negativeI32Value)) // (for eg. bigInt.fromString(negativeI32Value))
@ -56,13 +54,13 @@ const log = debug('vulcanize:graph-node');
export const instantiate = async ( export const instantiate = async (
database: Database, database: Database,
indexer: IndexerInterface, indexer: IndexerInterface,
provider: BaseProvider,
context: Context, context: Context,
filePath: string, filePath: string,
data: GraphData = {} data: GraphData = {}
): Promise<loader.ResultObject & { exports: any }> => { ): Promise<loader.ResultObject & { exports: any }> => {
const { abis = {}, dataSource } = data; const { abis = {}, dataSource } = data;
const buffer = await fs.readFile(filePath); const buffer = await fs.readFile(filePath);
const provider = getDefaultProvider(NETWORK_URL);
const imports: WebAssembly.Imports = { const imports: WebAssembly.Imports = {
index: { index: {
@ -160,8 +158,10 @@ export const instantiate = async (
functionParams = await Promise.all(functionParamsPromise); functionParams = await Promise.all(functionParamsPromise);
assert(context.event.block);
// TODO: Check for function overloading. // TODO: Check for function overloading.
let result = await contract[functionName](...functionParams); let result = await contract[functionName](...functionParams, { blockTag: context.event.block.blockHash });
// Using function signature does not work. // Using function signature does not work.
const { outputs } = contract.interface.getFunction(functionName); const { outputs } = contract.interface.getFunction(functionName);

View File

@ -7,9 +7,10 @@ import { expect } from 'chai';
import BN from 'bn.js'; import BN from 'bn.js';
import { GraphDecimal } from '@vulcanize/util'; import { GraphDecimal } from '@vulcanize/util';
import { BaseProvider } from '@ethersproject/providers';
import { instantiate } from './loader'; import { instantiate } from './loader';
import { getTestDatabase, getTestIndexer } from '../test/utils'; import { getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
import { Database } from './database'; import { Database } from './database';
import { Indexer } from '../test/utils/indexer'; import { Indexer } from '../test/utils/indexer';
import { import {
@ -29,15 +30,18 @@ describe('numbers wasm tests', () => {
let exports: any; let exports: any;
let db: Database; let db: Database;
let indexer: Indexer; let indexer: Indexer;
let provider: BaseProvider;
before(async () => { before(async () => {
db = getTestDatabase(); db = getTestDatabase();
indexer = getTestIndexer(); indexer = getTestIndexer();
provider = getTestProvider();
const filePath = path.resolve(__dirname, EXAMPLE_WASM_FILE_PATH); const filePath = path.resolve(__dirname, EXAMPLE_WASM_FILE_PATH);
const instance = await instantiate( const instance = await instantiate(
db, db,
indexer, indexer,
provider,
{ event: {} }, { event: {} },
filePath filePath
); );

View File

@ -6,8 +6,10 @@ import path from 'path';
import { expect } from 'chai'; import { expect } from 'chai';
import { utils, BigNumber } from 'ethers'; import { utils, BigNumber } from 'ethers';
import { BaseProvider } from '@ethersproject/providers';
import { instantiate } from './loader'; import { instantiate } from './loader';
import { getTestDatabase, getTestIndexer } from '../test/utils'; import { getTestDatabase, getTestIndexer, getTestProvider } from '../test/utils';
import { Database } from './database'; import { Database } from './database';
import { Indexer } from '../test/utils/indexer'; import { Indexer } from '../test/utils/indexer';
@ -17,16 +19,19 @@ describe('typeConversion wasm tests', () => {
let exports: any; let exports: any;
let db: Database; let db: Database;
let indexer: Indexer; let indexer: Indexer;
let provider: BaseProvider;
before(async () => { before(async () => {
db = getTestDatabase(); db = getTestDatabase();
indexer = getTestIndexer(); indexer = getTestIndexer();
provider = getTestProvider();
const filePath = path.resolve(__dirname, EXAMPLE_WASM_FILE_PATH); const filePath = path.resolve(__dirname, EXAMPLE_WASM_FILE_PATH);
const instance = await instantiate( const instance = await instantiate(
db, db,
indexer, indexer,
{ event: { } }, provider,
{ event: {} },
filePath filePath
); );
exports = instance.exports; exports = instance.exports;

View File

@ -75,7 +75,7 @@ export class GraphWatcher {
assert(this._indexer); assert(this._indexer);
return { return {
instance: await instantiate(this._database, this._indexer, this._context, filePath, data), instance: await instantiate(this._database, this._indexer, this._ethProvider, this._context, filePath, data),
contractInterface contractInterface
}; };
}, {}); }, {});

View File

@ -2,17 +2,27 @@
// Copyright 2021 Vulcanize, Inc. // Copyright 2021 Vulcanize, Inc.
// //
import { BaseProvider } from '@ethersproject/providers';
import { getCustomProvider } from '@vulcanize/util';
import { EventData } from '../../src/utils'; import { EventData } from '../../src/utils';
import { Database } from '../../src/database'; import { Database } from '../../src/database';
import { Indexer } from './indexer'; import { Indexer } from './indexer';
const NETWORK_URL = 'http://127.0.0.1:8081';
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
export const ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000'; export const ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000';
export const getDummyEventData = (): EventData => { export const getDummyEventData = async (): Promise<EventData> => {
// Get the latest mined block from the chain.
const provider = getCustomProvider(NETWORK_URL);
const blockNumber = await provider.getBlockNumber();
const ethersBlock = await provider.getBlock(blockNumber);
const block = { const block = {
blockHash: ZERO_HASH, blockHash: ethersBlock.hash,
blockNumber: '0', blockNumber: ethersBlock.number.toString(),
timestamp: '0', timestamp: '0',
parentHash: ZERO_HASH, parentHash: ZERO_HASH,
stateRoot: ZERO_HASH, stateRoot: ZERO_HASH,
@ -58,3 +68,9 @@ export const getTestDatabase = (): Database => {
export const getTestIndexer = (): Indexer => { export const getTestIndexer = (): Indexer => {
return new Indexer(); return new Indexer();
}; };
export const getTestProvider = (): BaseProvider => {
const provider = getCustomProvider(NETWORK_URL);
return provider;
};