Skip to content

Commit

Permalink
Api update & revert api at (subquery#402)
Browse files Browse the repository at this point in the history
* Revert "Update polkadot/api to 5.0.1 (subquery#385)"

This reverts commit 2aeae01

* - Polkadot/api to 5.2.1
- revert change for api.at()
- waiting fix (/polkadot-js/api/issues/3812)

* - prettier-ignore
- fix .idea in gitignore
  • Loading branch information
jiqiang90 authored Jul 27, 2021
1 parent adb2667 commit ea6f053
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 219 deletions.
39 changes: 2 additions & 37 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,7 @@
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
.idea

# CMake
cmake-build-*/
Expand Down Expand Up @@ -209,4 +174,4 @@ docs/.vuepress/.caches
.data
lib

.DS_Store
.DS_Store
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"vuepress-plugin-fulltext-search": "^2.2.1"
},
"resolutions": {
"@polkadot/api": "5.0.1"
"@polkadot/api": "5.2.1"
},
"scripts": {
"docs:dev": "vuepress dev docs",
Expand Down
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@nestjs/event-emitter": "^0.0.2",
"@nestjs/platform-express": "^7.6.1",
"@nestjs/schedule": "^0.4.2",
"@polkadot/api": "^5.0.1",
"@polkadot/api": "^5.2.1",
"@subql/common": "workspace:*",
"@subql/types": "workspace:*",
"@subql/x-vm2": "latest",
Expand Down
92 changes: 69 additions & 23 deletions packages/node/src/indexer/api.service.test.ts

Large diffs are not rendered by default.

40 changes: 15 additions & 25 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,37 @@ export class ApiService implements OnApplicationShutdown {
value: 1,
});
this.patchedApi = patchedApi;
await this.patchApi();
this.patchApi();
return this.patchedApi;
}

private async patchApi(
registry?: Registry,
blockHash?: BlockHash,
): Promise<void> {
//To support api.findCall & api.findError
private patchApi(registry?: Registry): void {
if (registry) {
Object.defineProperty(this.patchedApi, 'registry', {
value: registry,
writable: false,
configurable: true,
});
}
if (blockHash) {
await this.patchApiQuery(this.patchedApi, blockHash);
}
this.patchApiAt(this.patchedApi);
this.patchApiQuery(this.patchedApi);
this.patchApiTx(this.patchedApi);
this.patchApiQueryMulti(this.patchedApi);
this.patchDerive(this.patchedApi);
this.patchApiRpc(this.patchedApi);
(this.patchedApi as any).isPatched = true;
}

async setBlockhash(blockHash: BlockHash): Promise<void> {
async setBlockhash(blockHash: BlockHash, inject = false): Promise<void> {
if (!this.patchedApi) {
await this.getPatchedApi();
}
const { registry } = await this.api.getBlockRegistry(blockHash);
this.currentBlockHash = blockHash;
await this.patchApi(registry, blockHash);
if (inject) {
const { metadata, registry } = await this.api.getBlockRegistry(blockHash);
this.patchedApi.injectMetadata(metadata, true, registry);
this.patchApi(registry);
}
}

private replaceToAtVersion(
Expand All @@ -155,7 +152,10 @@ export class ApiService implements OnApplicationShutdown {
original: QueryableStorageEntry<'promise' | 'rxjs', AnyTuple>,
apiType: 'promise' | 'rxjs',
): QueryableStorageEntry<'promise' | 'rxjs', AnyTuple> {
const newEntryFunc = original;
const newEntryFunc = this.replaceToAtVersion(
original,
'at',
) as QueryableStorageEntry<'promise' | 'rxjs', AnyTuple>;
newEntryFunc.at = NOT_SUPPORT('at');
newEntryFunc.creator = original.creator;
newEntryFunc.entries = this.replaceToAtVersion(original, 'entriesAt');
Expand All @@ -177,7 +177,6 @@ export class ApiService implements OnApplicationShutdown {
newEntryFunc as QueryableStorageEntry<'rxjs', AnyTuple>,
);
}
// wait polkadot/api fix isssue #3763 to support multi
newEntryFunc.multi = ((args: any[]) => {
const keys = args.map((arg) => {
const key = new StorageKey(
Expand All @@ -186,11 +185,6 @@ export class ApiService implements OnApplicationShutdown {
...(original.creator.meta.type.isDoubleMap ? arg : [arg]),
),
);
console.log(
JSON.stringify([
...(original.creator.meta.type.isDoubleMap ? arg : [arg]),
]),
);
key.setMeta(original.creator.meta);
return key;
});
Expand Down Expand Up @@ -253,12 +247,8 @@ export class ApiService implements OnApplicationShutdown {
combineLatest(keys.map((key) => newEntryFunc(key)))) as any;
}

private async patchApiQuery(
api: ApiPromise,
blockHash: BlockHash,
): Promise<void> {
const apiAt = await this.api.at(blockHash);
(api as any)._query = Object.entries(apiAt.query).reduce(
private patchApiQuery(api: ApiPromise): void {
(api as any)._query = Object.entries(api.query).reduce(
(acc, [module, moduleStorageItems]) => {
acc[module] = Object.entries(moduleStorageItems).reduce(
(accInner, [storageName, storageEntry]) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/indexer/indexer.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export class IndexerManager {
this.storeService.setTransaction(tx);

try {
await this.apiService.setBlockhash(block.block.hash);
const inject = block.specVersion !== this.prevSpecVersion;
await this.apiService.setBlockhash(block.block.hash, inject);

const dataSources = this.project.dataSources.filter(
(ds) =>
Expand Down
Loading

0 comments on commit ea6f053

Please sign in to comment.