Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use block number for eth_call in rpc-eth-client #435

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/codegen/src/data/entities/BlockProgress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ columns:
columnType: PrimaryGeneratedColumn
- name: cid
pgType: varchar
tsType: string
tsType: string | null
columnType: Column
columnOptions:
- option: nullable
Expand Down
3 changes: 0 additions & 3 deletions packages/codegen/src/data/entities/StateSyncStatus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ columns:
pgType: integer
tsType: number
columnType: Column
columnOptions:
- option: nullable
value: true
imports:
- toImport:
- Entity
Expand Down
2 changes: 2 additions & 0 deletions packages/codegen/src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ export class Entity {
option: 'nullable',
value: 'true'
});

columnObject.tsType = `${tsType} | null`;
}

entityObject.columns.push(columnObject);
Expand Down
3 changes: 3 additions & 0 deletions packages/codegen/src/templates/config-template.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
# Use -1 for skipping check on block range.
maxEventsBlockRange = 1000

# Flag to specify whether RPC endpoint supports block hash as block tag parameter
rpcSupportsBlockHashParam = true

# GQL cache settings
[server.gqlCache]
enabled = true
Expand Down
10 changes: 9 additions & 1 deletion packages/codegen/subgraph-demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@
yarn && yarn build
```

* In `packages/test-watcher`, run the job-runner:
* In `packages/test-watcher`, run fill for the subgraph start block:

```bash
yarn fill --start-block 10 --end-block 10
```

* Subgraph start block is the lowest `startBlock` in example [subgraph.yaml](../graph-node/test/subgraph/example1/subgraph.yaml)

* Run the job-runner:

```bash
yarn job-runner
Expand Down
1 change: 1 addition & 0 deletions packages/graph-node/src/call-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ xdescribe('call handler in mapping code', () => {
indexer,
provider,
{
rpcSupportsBlockHashParam: true,
block: dummyEventData.block,
contractAddress: dummyGraphData.dataSource.address
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-node/src/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('crypto host api', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
filePath,
dummyGraphData
);
Expand Down
3 changes: 3 additions & 0 deletions packages/graph-node/src/eden.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ xdescribe('eden wasm loader tests', async () => {
indexer,
provider,
{
rpcSupportsBlockHashParam: true,
block: dummyEventData.block,
contractAddress
},
Expand Down Expand Up @@ -210,6 +211,7 @@ xdescribe('eden wasm loader tests', async () => {
indexer,
provider,
{
rpcSupportsBlockHashParam: true,
block: dummyEventData.block,
contractAddress
},
Expand Down Expand Up @@ -328,6 +330,7 @@ xdescribe('eden wasm loader tests', async () => {
indexer,
provider,
{
rpcSupportsBlockHashParam: true,
block: dummyEventData.block,
contractAddress
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-node/src/eth-abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('ethereum ABI encode decode', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
filePath,
dummyGraphData
);
Expand Down
1 change: 1 addition & 0 deletions packages/graph-node/src/eth-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ xdescribe('eth-call wasm tests', () => {
indexer,
provider,
{
rpcSupportsBlockHashParam: true,
block: dummyEventData.block,
contractAddress
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-node/src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('json host api', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
filePath,
dummyGraphData
);
Expand Down
4 changes: 2 additions & 2 deletions packages/graph-node/src/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('wasm loader tests', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
filePath,
dummyGraphData
);
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('wasm loader tests', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
module,
dummyGraphData
);
Expand Down
18 changes: 15 additions & 3 deletions packages/graph-node/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export interface GraphData {
}

export interface Context {
block?: Block
contractAddress?: string
rpcSupportsBlockHashParam: boolean;
block?: Block;
contractAddress?: string;
}

const log = debug('vulcanize:graph-node');
Expand Down Expand Up @@ -173,10 +174,21 @@ export const instantiate = async (
functionParams = await Promise.all(functionParamsPromise);

assert(context.block);
let result: any;

// TODO: Check for function overloading.
console.time(`time:loader#ethereum.call-${functionName}`);
let result = await contract[functionName](...functionParams, { blockTag: context.block.blockHash });
if (context.rpcSupportsBlockHashParam) {
result = await contract[functionName](
...functionParams,
{ blockTag: context.block.blockHash }
);
} else {
result = await contract[functionName](
...functionParams,
{ blockTag: BigNumber.from(context.block.blockNumber).toHexString() }
);
}
console.timeEnd(`time:loader#ethereum.call-${functionName}`);

// Using function signature does not work.
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-node/src/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('numbers wasm tests', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
filePath,
dummyGraphData
);
Expand Down
1 change: 1 addition & 0 deletions packages/graph-node/src/storage-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ xdescribe('storage-call wasm tests', () => {
indexer,
provider,
{
rpcSupportsBlockHashParam: true,
block: dummyEventData.block,
contractAddress
},
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-node/src/type-conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('typeConversion wasm tests', () => {
db,
indexer,
provider,
{},
{ rpcSupportsBlockHashParam: true },
filePath,
dummyGraphData
);
Expand Down
6 changes: 5 additions & 1 deletion packages/graph-node/src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ export class GraphWatcher {
_dataSourceMap: { [key: string]: DataSource } = {};
_transactionsMap: Map<string, Transaction> = new Map();

_context: Context = {};
_context: Context;

constructor (database: GraphDatabase, ethClient: EthClient, ethProvider: providers.BaseProvider, serverConfig: ServerConfig) {
this._database = database;
this._ethClient = ethClient;
this._ethProvider = ethProvider;
this._subgraphPath = serverConfig.subgraphPath;
this._wasmRestartBlocksInterval = serverConfig.wasmRestartBlocksInterval;

this._context = {
rpcSupportsBlockHashParam: Boolean(serverConfig.rpcSupportsBlockHashParam)
};
}

async init () {
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-eth-client/src/eth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class EthClient implements EthClientInterface {
Extra: rawBlock.extraData,
MixDigest: rawBlock.mixHash,
Nonce: BigInt(rawBlock.nonce),
BaseFee: BigInt(rawBlock.baseFeePerGas)
BaseFee: rawBlock.baseFeePerGas ?? BigInt(rawBlock.baseFeePerGas)
};

const rlpData = encodeHeader(header);
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export interface ServerConfig {

p2p: P2PConfig;

// TODO: Move flag to config upstream.ethServer
// Flag to specify whether RPC endpoint supports block hash as block tag parameter
// https://ethereum.org/en/developers/docs/apis/json-rpc/#default-block
rpcSupportsBlockHashParam: boolean;
Expand Down
7 changes: 6 additions & 1 deletion packages/util/src/graph/state-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ export const getContractEntitiesMap = (dataSources: any[]): Map<string, string[]
// Populate contractEntitiesMap using data sources from subgraph
dataSources.forEach((dataSource: any) => {
const { source: { address: contractAddress }, mapping: { entities } } = dataSource;
contractEntitiesMap.set(ethers.utils.getAddress(contractAddress), entities as string[]);

// TODO: Handle template data source
// TODO: Avoid mapping subgraph entities to contract address in watcher state
if (contractAddress) {
contractEntitiesMap.set(ethers.utils.getAddress(contractAddress), entities as string[]);
}
});

return contractEntitiesMap;
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/graph/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ export const toEntityValue = async (instanceExports: any, entityInstance: any, d
// Check if the entity property is nullable.
// No need to set the property if the value is null as well.
if (isNullable && value === null) {
return;
return value;
}

const entityValue = await formatEntityValue(instanceExports, subgraphValue, type, value, isArray);
Expand Down
4 changes: 2 additions & 2 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface StateStatus {

export type ResultState = {
block: {
cid: string;
cid: string | null;
hash: string;
number: number;
timestamp: number;
Expand All @@ -66,7 +66,7 @@ export type ResultState = {

export type ResultEvent = {
block: {
cid: string;
cid: string | null;
hash: string;
number: number;
timestamp: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/state-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface StateDataMeta {
},
ethBlock: {
cid: {
'/': string
'/': string | null
},
num: number
}
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export enum StateKind {

export interface BlockProgressInterface {
id: number;
cid: string;
cid: string | null;
blockHash: string;
parentHash: string;
blockNumber: number;
Expand Down
Loading