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

add edgeware dictionary #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 17 additions & 23 deletions astar/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
EvmLog as EvmLogModel
} from "../types";
import FrontierEvmDatasourcePlugin, { FrontierEvmCall } from "@subql/frontier-evm-processor/";
import {inputToFunctionSighash, isZero, getSelector, wrapExtrinsics, wrapEvents} from "../utils";
import {inputToFunctionSighash, isZero, wrapExtrinsics, wrapEvents} from "../utils";
import {ApiPromise} from "@polkadot/api";

import {u8, Vec} from "@polkadot/types";

let specVersion: SpecVersion;

Expand Down Expand Up @@ -67,23 +67,17 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
evmTransactions.push(handleEvmTransaction(call.idx.toString(),frontierEvmCall))
}
})
// seems there is a concurrent limitation for promise.all and bulkCreate work together,
// the last entity upsertion are missed
// We will put them into two promise for now.
await Promise.all([
store.bulkCreate('Event', events),
store.bulkCreate('ContractEmitted', contractEmittedEvents),
store.bulkCreate('EvmLog', evmLogs),
]);
await Promise.all([
store.bulkCreate('Extrinsic', calls),
store.bulkCreate('ContractsCall', contractCalls),
store.bulkCreate('EvmTransaction', evmTransactions)
]);
// seems there is a concurrent limitation for promise.all and bulkCreate work together
await store.bulkCreate('Event', events)
await store.bulkCreate('ContractEmitted', contractEmittedEvents)
await store.bulkCreate('EvmLog', evmLogs)
await store.bulkCreate('Extrinsic', calls)
await store.bulkCreate('ContractsCall', contractCalls)
await store.bulkCreate('EvmTransaction', evmTransactions)
}

export function handleEvent(event: SubstrateEvent): Event {
const newEvent = new Event(`${event.block.block.header.number.toString()}-${event.idx}`);
const newEvent = new Event(`${event.block.block.header.number.toString()}-${event.idx.toString()}`);
newEvent.blockHeight = event.block.block.header.number.toBigInt();
newEvent.module = event.event.section;
newEvent.event = event.event.method;
Expand All @@ -102,8 +96,7 @@ export function handleCall(extrinsic: SubstrateExtrinsic): Extrinsic {

function handleEvmEvent(event: SubstrateEvent): EvmLogModel {
const [{address, data, topics}] = event.event.data as unknown as [EvmLog];

const evmLog = new EvmLogModel(`${event.block.block.header.number.toString()}-${event.idx}`)
const evmLog = new EvmLogModel(`${event.block.block.header.number.toString()}-${event.idx.toString()}`)
evmLog.address = address.toString()
evmLog.blockHeight= event.block.block.header.number.toBigInt();
evmLog.topics0= topics[0].toHex();
Expand All @@ -118,7 +111,7 @@ export function handleEvmTransaction(idx: string, tx: FrontierEvmCall): EvmTrans
return;
}
const func = isZero(tx.data) ? undefined : inputToFunctionSighash(tx.data);
const evmTransaction = new EvmTransaction(`${tx.blockNumber.toString()}-${idx}`)
const evmTransaction = new EvmTransaction(`${tx.blockNumber.toString()}-${idx.toString()}`)
evmTransaction.txHash = tx.hash;
evmTransaction.from = tx.from;
evmTransaction.to= tx.to;
Expand All @@ -130,22 +123,23 @@ export function handleEvmTransaction(idx: string, tx: FrontierEvmCall): EvmTrans


export function handleContractCalls(call: SubstrateExtrinsic): ContractsCall {
const [dest,,,, data] = call.extrinsic.method.args;
const contractCall = new ContractsCall(`${call.block.block.header.number.toString()}-${call.idx}`)
const dest = call.extrinsic.args[0];
const data = call.extrinsic.args[4];
const contractCall = new ContractsCall(`${call.block.block.header.number.toString()}-${call.idx.toString()}`)
contractCall.from = call.extrinsic.isSigned? call.extrinsic.signer.toString(): undefined;
contractCall.success = !call.events.find(
(evt) => evt.event.section === 'system' && evt.event.method === 'ExtrinsicFailed'
);
contractCall.dest = (dest as Address).toString();
contractCall.blockHeight = call.block.block.header.number.toBigInt();
contractCall.selector = getSelector(data.toU8a())
contractCall.selector =(data as Vec<u8>).toHex().slice(0, 10)
return contractCall;

}

export function handleContractsEmitted(event: SubstrateEvent):ContractEmitted{
const [contract, data] = event.event.data as unknown as ContractEmittedResult;
const contractEmitted = new ContractEmitted(`${event.block.block.header.number.toString()}-${event.event.index.toString()}`);
const contractEmitted = new ContractEmitted(`${event.block.block.header.number.toString()}-${event.idx.toString()}`);
contractEmitted.blockHeight = event.block.block.header.number.toBigInt();
contractEmitted.contract= contract.toString();
contractEmitted.from= event.extrinsic.extrinsic.isSigned? event.extrinsic.extrinsic.signer.toString(): EMPTY_ADDRESS;
Expand Down
8 changes: 0 additions & 8 deletions astar/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ function getExtrinsicSuccess(events: EventRecord[]): boolean {
);
}

export function getSelector(data: Uint8Array): string {
//This should align with https://github.com/polkadot-js/api/blob/0b6f7861080c920407a346e2a3dbe64adcb07a1e/packages/api-contract/src/Abi/index.ts#L249
const [, trimmed] = compactStripLength(data);
return u8aToHex(trimmed.subarray(0, 4));
}



export function wrapEvents(
extrinsics: SubstrateExtrinsic[],
events: EventRecord[],
Expand Down
53 changes: 53 additions & 0 deletions edgeware/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# These are some examples of commonly ignored file patterns.
# You should customize this list as applicable to your project.
# Learn more about .gitignore:
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore

# Node artifact files
node_modules/
dist/
yarn.lock

# Compiled Java class files
*.class

# Compiled Python bytecode
*.py[cod]

# Log files
*.log

# Package files
*.jar

# Maven
target/
dist/
src/types

# JetBrains IDE
.idea/

# Unit test reports
TEST*.xml

# Generated by MacOS
.DS_Store

# Generated by Windows
Thumbs.db

# Applications
*.app
*.exe
*.war

# Large media files
*.mp4
*.tiff
*.avi
*.flv
*.mov
*.wmv

.data
28 changes: 28 additions & 0 deletions edgeware/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SubQuery - Dictionary

This special SubQuery Project provides a dictionary of data that pre-indexes events on chain to dramatically improve indexing the performance of your own SubQuery Project, sometimes up to 10x faster.

It scans over the network, and simply records the module and method for every event/extrinsic on each block - please see the standard entities in `schema.graphql`.

**If you want to create your SubQuery Dictionary to speed up indexing of your own Substrate chain, fork this project and let us know**

# Geting Started
### 1. Install dependencies
```shell
yarn
```

### 2. Generate types
```shell
yarn codegen
```

### 3. Build
```shell
yarn build
```

### 4. Run locally
```shell
yarn start:docker
```
44 changes: 44 additions & 0 deletions edgeware/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3'

services:
postgres:
image: postgres:12-alpine
ports:
- 5432:5432
volumes:
- .data/postgres:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres

subquery-node:
image: onfinality/subql-node:latest
depends_on:
- "postgres"
restart: always
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_PORT: 5432
volumes:
- ./:/app
command:
- -f=/app

graphql-engine:
image: onfinality/subql-query:latest
ports:
- 3000:3000
depends_on:
- "postgres"
restart: always
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_PORT: 5432
command:
- --name=app
- --playground
34 changes: 34 additions & 0 deletions edgeware/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "subql-dictionary",
"version": "1.0.0",
"description": "A SubQuery Dictionary Project that provides increased indexing speed to all projects",
"main": "dist/index.js",
"scripts": {
"build": "subql build",
"prepack": "rm -rf dist && npm build",
"test": "jest",
"codegen": "./node_modules/.bin/subql codegen",
"start:docker": "docker-compose pull && docker-compose up --remove-orphans"
},
"homepage": "https://github.com/subquery/subql-dictionary",
"repository": "github:subquery/subql-dictionary",
"files": [
"dist",
"schema.graphql",
"project.yaml"
],
"author": "SubQuery Network",
"license": "Apache-2.0",
"resolutions": {
"@polkadot/api": "9.2.4",
"ipfs-unixfs": "6.0.6"
},
"devDependencies": {
"@polkadot/api": "9.2.4",
"@subql/cli": "latest",
"@subql/frontier-evm-processor": "0.1.2",
"@subql/types": "latest",
"typescript": "^4.4.4",
"abab": "latest"
}
}
27 changes: 27 additions & 0 deletions edgeware/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
specVersion: 1.0.0
name: SubQuery Dictionary - Edgeware
version: 1.0.0
runner:
node:
name: '@subql/node'
version: '>=0.32.0'
query:
name: '@subql/query'
version: '*'
description: >-
A SubQuery Dictionary Project that provides increased indexing speed to all
projects
repository: 'https://github.com/subquery/subql-dictionary'
schema:
file: ./schema.graphql
network:
chainId: '0x742a2ca70c2fda6cee4f8df98d64c4c670a052d9568058982dad9d5a7a135c5b'
endpoint: 'wss://edgeware.api.onfinality.io/public-ws'
dataSources:
- kind: substrate/Runtime
startBlock: 1
mapping:
file: ./dist/index.js
handlers:
- handler: handleBlock
kind: substrate/BlockHandler
100 changes: 100 additions & 0 deletions edgeware/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
type SpecVersion @entity {

id: ID! #specVersion

blockHeight: BigInt!

}

type Event @entity {

id: ID!

module: String! @index

event: String! @index

blockHeight: BigInt! @index
}

type Extrinsic @entity {

id: ID!

module: String! @index

call: String! @index

blockHeight: BigInt! @index

success: Boolean!

isSigned: Boolean!

}

type EvmTransaction @entity {

id: ID!

txHash: String! @index

from: String! @index

to: String! @index

func: String @index

blockHeight: BigInt! @index

success: Boolean!

}

type EvmLog @entity {

id: ID!

address: String! @index

blockHeight: BigInt! @index

topics0: String!

topics1: String

topics2: String

topics3: String

}

type ContractsCall @entity {

id: ID!

from: String! @index

dest: String! @index #contract address

selector: String! @index

blockHeight: BigInt! @index

success: Boolean!

}

type ContractEmitted @entity {

id: ID!

contract: String! @index

from: String !@index

eventIndex: Int! @index

blockHeight: BigInt! @index

}
Loading