Skip to content

Commit

Permalink
Cosmos SDK 0.50 support (#270)
Browse files Browse the repository at this point in the history
* Update cosmos and subql deps and utilise latest changes

* Update changelogs

* Updated tests to use archway instead of juno, fix filter bug

* Fix Kyve tests
  • Loading branch information
stwiname authored Jul 17, 2024
1 parent 3a2222a commit ffb59b5
Show file tree
Hide file tree
Showing 17 changed files with 526 additions and 840 deletions.
2 changes: 2 additions & 0 deletions packages/common-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Update cosmos and subql dependencies (#270)

## [5.0.0] - 2024-07-01
### Added
Expand Down
4 changes: 2 additions & 2 deletions packages/common-cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"main": "dist/index.js",
"license": "GPL-3.0",
"dependencies": {
"@cosmwasm/ts-codegen": "^0.35.7",
"@cosmwasm/ts-codegen": "^1.11.1",
"@protobufs/cosmos": "^0.1.0",
"@protobufs/cosmos_proto": "^0.0.10",
"@protobufs/cosmwasm": "^0.1.1",
"@protobufs/gogoproto": "^0.0.10",
"@protobufs/google": "^0.0.10",
"@protobufs/ibc": "^0.1.0",
"@protobufs/tendermint": "^0.0.10",
"@subql/common": "^4.0.0",
"@subql/common": "^4.1.0",
"@subql/types-cosmos": "workspace:*",
"@subql/x-cosmology-telescope": "^1.4.14",
"fs-extra": "^11.1.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Update cosmos and subql dependencies (#270)

### Added
- Detection of Cosmos SDK version to use correct client type (#270)

## [3.12.1] - 2024-07-01
### Fixed
Expand Down
12 changes: 6 additions & 6 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
"subql-node-cosmos": "./bin/run"
},
"dependencies": {
"@cosmjs/cosmwasm-stargate": "^0.32.3",
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@cosmjs/cosmwasm-stargate": "^0.32.4",
"@cosmjs/proto-signing": "^0.32.4",
"@cosmjs/stargate": "^0.32.4",
"@kyvejs/protocol": "^1.0.12",
"@kyvejs/sdk": "^1.1.1",
"@kyvejs/sdk": "^1.2.0",
"@nestjs/common": "^9.4.0",
"@nestjs/core": "^9.4.0",
"@nestjs/event-emitter": "^2.0.0",
"@nestjs/platform-express": "^9.4.0",
"@nestjs/schedule": "^3.0.1",
"@subql/common": "^4.0.0",
"@subql/common": "^4.1.0",
"@subql/common-cosmos": "workspace:*",
"@subql/node-core": "^10.10.0",
"@subql/node-core": "^11.0.0",
"@subql/types-cosmos": "workspace:*",
"lodash": "^4.17.21",
"protobufjs": "^6.11.4",
Expand Down
17 changes: 7 additions & 10 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { toHex } from '@cosmjs/encoding';
import { Uint53 } from '@cosmjs/math';
import { GeneratedType, Registry } from '@cosmjs/proto-signing';
import { Block, defaultRegistryTypes, SearchTxQuery } from '@cosmjs/stargate';
import {
Tendermint37Client,
toRfc3339WithNanoseconds,
} from '@cosmjs/tendermint-rpc';
import { CometClient, toRfc3339WithNanoseconds } from '@cosmjs/tendermint-rpc';
import {
BlockResponse,
BlockResultsResponse,
Expand Down Expand Up @@ -188,15 +185,15 @@ export class ApiService

export class CosmosClient extends CosmWasmClient {
constructor(
private readonly tendermintClient: Tendermint37Client,
private readonly _cometClient: CometClient,
public registry: Registry,
) {
super(tendermintClient);
super(_cometClient);
}

// eslint-disable-next-line @typescript-eslint/require-await
async blockInfo(height?: number): Promise<BlockResponse> {
return this.tendermintClient.block(height);
return this._cometClient.block(height);
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand All @@ -206,7 +203,7 @@ export class CosmosClient extends CosmWasmClient {

// eslint-disable-next-line @typescript-eslint/require-await
async blockResults(height: number): Promise<BlockResultsResponse> {
return this.tendermintClient.blockResults(height);
return this._cometClient.blockResults(height);
}

static handleError(e: Error): Error {
Expand Down Expand Up @@ -237,8 +234,8 @@ export class CosmosSafeClient
{
height: number;

constructor(tmClient: Tendermint37Client, height: number) {
super(tmClient);
constructor(cometClient: CometClient, height: number) {
super(cometClient);
this.height = height;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ export class BlockDispatcherService
);
}

async init(
onDynamicDsCreated: (height: number) => Promise<void>,
): Promise<void> {
async init(onDynamicDsCreated: (height: number) => void): Promise<void> {
await super.init(onDynamicDsCreated);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ export class WorkerBlockDispatcherService
);
}

async init(
onDynamicDsCreated: (height: number) => Promise<void>,
): Promise<void> {
async init(onDynamicDsCreated: (height: number) => void): Promise<void> {
await super.init(onDynamicDsCreated);
}

Expand Down
53 changes: 39 additions & 14 deletions packages/node/src/indexer/cosmosClient.connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

import { Registry } from '@cosmjs/proto-signing';
import { HttpEndpoint } from '@cosmjs/stargate';
import { Tendermint37Client } from '@cosmjs/tendermint-rpc';
import {
CometClient,
Tendermint37Client,
Comet38Client,
Tendermint34Client,
} from '@cosmjs/tendermint-rpc';
import {
IBlock,
ApiConnectionError,
Expand All @@ -20,9 +25,33 @@ import { BlockContent } from './types';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: packageVersion } = require('../../package.json');

const RETRY_DELAY = 2_500;

const logger = getLogger('cosmos-client-connection');
const logger = getLogger('CosmosClientConnection');

/**
* Auto-detects the version of the backend and uses a suitable client.
*/
export async function connectComet(
client: WebsocketClient | HttpClient,
): Promise<CometClient> {
// Tendermint/CometBFT 0.34/0.37/0.38 auto-detection. Starting with 0.37 we seem to get reliable versions again 🎉
// Using 0.34 as the fallback.
let out: CometClient;
const tm37Client = await Tendermint37Client.create(client);
const version = (await tm37Client.status()).nodeInfo.version;
if (version.startsWith('0.37.')) {
logger.debug(`Using Tendermint 37 Client`);
out = tm37Client;
} else if (version.startsWith('0.38.')) {
tm37Client.disconnect();
logger.debug(`Using Comet 38 Client`);
out = await Comet38Client.create(client);
} else {
tm37Client.disconnect();
logger.debug(`Using Tendermint 34 Client`);
out = await Tendermint34Client.create(client);
}
return out;
}

type FetchFunc = (
api: CosmosClient,
Expand All @@ -37,7 +66,7 @@ export class CosmosClientConnection
IBlock<BlockContent>[]
>
{
private tmClient: Tendermint37Client;
private cometClient: CometClient;
private registry: Registry;
readonly networkMeta: NetworkMetadataPayload;

Expand Down Expand Up @@ -75,16 +104,16 @@ export class CosmosClientConnection
})
: new HttpClient(httpEndpoint);

const tendermint = await Tendermint37Client.create(rpcClient);
const cometClient = await connectComet(rpcClient);

const api = new CosmosClient(tendermint, registry);
const api = new CosmosClient(cometClient, registry);

const connection = new CosmosClientConnection(
api,
fetchBlocksBatches,
await api.getChainId(),
);
connection.setTmClient(tendermint);
connection.cometClient = cometClient;
connection.setRegistry(registry);

logger.info(`connected to ${endpoint}`);
Expand All @@ -93,11 +122,7 @@ export class CosmosClientConnection
}

safeApi(height: number): CosmosSafeClient {
return new CosmosSafeClient(this.tmClient, height);
}

private setTmClient(tmClient: Tendermint37Client): void {
this.tmClient = tmClient;
return new CosmosSafeClient(this.cometClient, height);
}

private setRegistry(registry: Registry): void {
Expand All @@ -106,7 +131,7 @@ export class CosmosClientConnection

// eslint-disable-next-line @typescript-eslint/require-await
async apiConnect(): Promise<void> {
this.unsafeApi = new CosmosClient(this.tmClient, this.registry);
this.unsafeApi = new CosmosClient(this.cometClient, this.registry);
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/dictionary/dictionary.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DsProcessorService } from '../ds-processor.service';
import { BlockContent } from '../types';
import { DictionaryV1 } from './v1';

const logger = getLogger('eth-dictionary');
const logger = getLogger('DictionaryService');

@Injectable()
export class DictionaryService extends BaseDictionaryService<
Expand Down
17 changes: 10 additions & 7 deletions packages/node/src/indexer/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
CosmosHandlerKind,
CosmosDataSource,
} from '@subql/common-cosmos';
import { NodeConfig, BaseFetchService, getModulos } from '@subql/node-core';
import {
NodeConfig,
BaseFetchService,
getModulos,
Header,
} from '@subql/node-core';
import { SubqueryProject } from '../configure/SubqueryProject';
import * as CosmosUtil from '../utils/cosmos';
import { cosmosBlockToHeader } from '../utils/cosmos';
Expand Down Expand Up @@ -37,7 +42,7 @@ export class FetchService extends BaseFetchService<
@Inject('IBlockDispatcher')
blockDispatcher: ICosmosBlockDispatcher,
dictionaryService: DictionaryService,
private unfinalizedBlocksService: UnfinalizedBlocksService,
unfinalizedBlocksService: UnfinalizedBlocksService,
eventEmitter: EventEmitter2,
schedulerRegistry: SchedulerRegistry,
) {
Expand All @@ -49,20 +54,18 @@ export class FetchService extends BaseFetchService<
dictionaryService,
eventEmitter,
schedulerRegistry,
unfinalizedBlocksService,
);
}

get api(): CosmosClient {
return this.apiService.unsafeApi;
}

protected async getFinalizedHeight(): Promise<number> {
protected async getFinalizedHeader(): Promise<Header> {
// Cosmos has instant finalization
const height = await this.api.getHeight();
const header = cosmosBlockToHeader(height);
this.unfinalizedBlocksService.registerFinalizedBlock(header);

return height;
return cosmosBlockToHeader(height);
}

protected async getBestHeight(): Promise<number> {
Expand Down
13 changes: 7 additions & 6 deletions packages/node/src/utils/cosmos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ import { CosmosClient } from '../indexer/api.service';
import { HttpClient } from '../indexer/rpc-clients';
import { decodeMsg, filterMessageData, wrapEvent } from './cosmos';

const ENDPOINT = 'https://rpc-archive.junonetwork.io/';
const ENDPOINT = 'https://rpc.mainnet.archway.io';

const TEST_BLOCKNUMBER = 4136538;
const TEST_BLOCKNUMBER = 4_136_542; //https://www.mintscan.io/archway/block/4136542?chainId=archway-1

const TEST_FAILTX_BLOCKNUMBER = 4136536;

const TEST_MESSAGE_FILTER_TRUE: CosmosMessageFilter = {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
contractCall: 'swap',
values: {
sender: 'juno1p5afwncel44vfrvylghncu2su7we57gmf7gjcu',
contract: 'juno1e8n6ch7msks487ecznyeagmzd5ml2pq9tgedqt2u63vra0q0r9mqrjy6ys',
sender: 'archway1nh8r3fka9amu4dvzf5r3lsyyx8xqm74c4vwz4s',
contract:
'archway1ymgz3t32j2h7j5rehwhac83tc0lkh8udc8yfh2y2hnqt9kn76xjq4zwfgw',
},
};

Expand All @@ -56,8 +57,8 @@ const TEST_NESTED_MESSAGE_FILTER_TRUE: CosmosMessageFilter = {
type: '/cosmwasm.wasm.v1.MsgExecuteContract',
contractCall: 'swap',
values: {
'msg.swap.input_token': 'Token1',
},
'msg.swap.swap_to_asset_index': 1,
} as any, // TODO update types
};

const TEST_NESTED_MESSAGE_FILTER_FALSE: CosmosMessageFilter = {
Expand Down
13 changes: 10 additions & 3 deletions packages/node/src/utils/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,16 @@ export function filterMessageData(
}
if (filter.values) {
for (const key in filter.values) {
let decodedMsgData = key
.split('.')
.reduce((acc, curr) => acc[curr], data.msg.decodedMsg);
let decodedMsgData: unknown;
try {
decodedMsgData = key
.split('.')
.reduce((acc, curr) => acc[curr], data.msg.decodedMsg);
} catch (e) {
// This message is assuming an error where acc[curr] is undefined and tries to access a further nested property
logger.warn(`Message doesn't contain data at value with path ${key}`);
return false;
}

//stringify Long for equality check
if (isLong(decodedMsgData)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/utils/kyve/kyve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ describe('KyveApi', () => {
});

it('ensure correct bundle ID on binary search', async () => {
(kyveApi as any).currentBundleId = -1; // reset cached bundle Id
const a = Date.now();
const firstBundle = await (kyveApi as any).getBundleIdSearch(120); // https://app.kyve.network/#/pools/2/bundles/0
const b = Date.now();
Expand All @@ -161,6 +160,7 @@ describe('KyveApi', () => {
expect(firstBundle).toBe(0);
expect(laterBundle).toBe(113773);
});

it('Ensure bundleId is defined', async () => {
getBundleDataSpy.mockResolvedValue(JSON.stringify({ mock: 'value' }));

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/utils/kyve/kyve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class KyveApi {
let low =
lowestCacheHeight.length > 0
? Math.min(...lowestCacheHeight.map(parseDecimal))
: -1;
: 0;
let high = await this.getLatestBundleId();

while (low <= high) {
Expand Down
2 changes: 2 additions & 0 deletions packages/types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Update cosmos and subql dependencies (#270)

## [3.5.0] - 2024-07-01
### Added
Expand Down
8 changes: 4 additions & 4 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"/dist"
],
"dependencies": {
"@cosmjs/cosmwasm-stargate": "^0.32.3",
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@subql/types-core": "^0.9.0"
"@cosmjs/cosmwasm-stargate": "^0.32.4",
"@cosmjs/proto-signing": "^0.32.4",
"@cosmjs/stargate": "^0.32.4",
"@subql/types-core": "^0.10.0"
}
}
Loading

0 comments on commit ffb59b5

Please sign in to comment.