Skip to content

Commit

Permalink
wip on rebuild logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bz888 committed Mar 26, 2024
1 parent 6124174 commit d707c8f
Show file tree
Hide file tree
Showing 4 changed files with 4,427 additions and 199 deletions.
18 changes: 18 additions & 0 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from 'cosmjs-types/cosmwasm/wasm/v1/tx';
import { SubqueryProject } from '../configure/SubqueryProject';
import * as CosmosUtil from '../utils/cosmos';
import { KyveApi } from '../utils/kyve';
import { CosmosClientConnection } from './cosmosClient.connection';
import { BlockContent } from './types';

Expand All @@ -46,6 +47,7 @@ export class ApiService
implements OnApplicationShutdown
{
private fetchBlocksBatches = CosmosUtil.fetchBlocksBatches;
private kyveClient: KyveApi;
registry: Registry;

constructor(
Expand Down Expand Up @@ -137,8 +139,12 @@ export class ApiService
return res;
}
}
// instead of using log,
// use events
// step 1. get rid of logs and use just events.

export class CosmosClient extends CosmWasmClient {
private kyveClient: KyveApi;
constructor(
private readonly tendermintClient: Tendermint37Client,
public registry: Registry,
Expand All @@ -156,6 +162,18 @@ export class CosmosClient extends CosmWasmClient {
}
*/

// todo, a trigger for when kyve should used
async initKyveClient(chainId: string): Promise<void> {
this.kyveClient = new KyveApi(chainId);
await this.kyveClient.init();
}

async kyveBlockByHeight(
height: number,
): Promise<[BlockResponse, BlockResultsResponse]> {
return this.kyveClient.getBlockByHeight(height);
}

// eslint-disable-next-line @typescript-eslint/require-await
async blockInfo(height?: number): Promise<BlockResponse> {
return this.tendermintClient.block(height);
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/utils/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export async function fetchCosmosBlocksArray(
api: CosmosClient,
blockArray: number[],
): Promise<[BlockResponse, BlockResultsResponse][]> {
// todo this is where kyve introduction should be.
return Promise.all(
blockArray.map(async (height) => getBlockByHeight(api, height)),
);
Expand Down Expand Up @@ -324,7 +325,6 @@ export async function fetchBlocksBatches(
blockArray: number[],
): Promise<BlockContent[]> {
const blocks = await fetchCosmosBlocksArray(api, blockArray);
// todo add kyve support here
return blocks.map(([blockInfo, blockResults]) => {
try {
assert(
Expand Down
117 changes: 52 additions & 65 deletions packages/node/src/utils/kyve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ import { KyveApi } from './kyve';
jest.setTimeout(100000);
describe('KyveClient', () => {
let kyveApi: KyveApi;
let tendermint: Tendermint37Client;

it('getBundle by height', async () => {
beforeAll(async () => {
kyveApi = new KyveApi('archway-1');
await kyveApi.init();
const client = new HttpClient('https://rpc.mainnet.archway.io:443');
tendermint = await Tendermint37Client.create(client);
});

it('getBundle by height', async () => {
const v = await kyveApi.getBlockByHeight(3856726);
// todo expect // timing out
// expect(v).toEqual()
});
it('ensure correct bundle ID on binary search', async () => {
kyveApi = new KyveApi('archway-1');
await kyveApi.init();

const a = Date.now();
const laterBundle = await (kyveApi as any).getBundleId(3489747); // https://app.kyve.network/#/pools/2/bundles/5149474
const b = Date.now();
Expand All @@ -43,12 +46,6 @@ describe('KyveClient', () => {
expect(laterBundle).toBe(113773);
});
it('cosmos client with kyve', async () => {
kyveApi = new KyveApi('archway-1');
await kyveApi.init();

const client = new HttpClient('https://rpc.mainnet.archway.io:443');
const tendermint = await Tendermint37Client.create(client);

const wasmTypes: ReadonlyArray<[string, GeneratedType]> = [
['/cosmwasm.wasm.v1.MsgClearAdmin', MsgClearAdmin],
['/cosmwasm.wasm.v1.MsgExecuteContract', MsgExecuteContract],
Expand Down Expand Up @@ -79,71 +76,61 @@ describe('KyveClient', () => {
// TODO
});
it('compare block info', async () => {
const height = 13885474;

const client = new HttpClient('https://rpc.osmosis.zone:443');
const tendermint = await Tendermint37Client.create(client);

kyveApi = new KyveApi('osmosis-1');
await kyveApi.init();
// todo, use a archway block

const height = 13885474;
const tendermintBlockInfo = await tendermint.block(height);
const [kyveBlockInfo] = await kyveApi.getBlockByHeight(height);
expect(isEqual(tendermintBlockInfo, kyveBlockInfo)).toBe(true);
});
it('compare block results', async () => {
kyveApi = new KyveApi('archway-1');
await kyveApi.init();

const client = new HttpClient('https://rpc.mainnet.archway.io:443');
const tendermint = await Tendermint37Client.create(client);
// bundleId: 113773
const height = 3489747;

const [tendermintBlockInfo, tendermintBlockResult] = await Promise.all([
tendermint.block(height),
tendermint.blockResults(height),
]);
// const [tendermintBlockInfo, tendermintBlockResult] = await Promise.all([
// tendermint.block(height),
// tendermint.blockResults(height),
// ]);
const [kyveBlockInfo, kyveBlockResult] = await kyveApi.getBlockByHeight(
height,
);

expect(isEqual(tendermintBlockInfo, kyveBlockInfo)).toBe(true);

// Note: block result will fail on expect due to deep nested array order being wrong
// RPC uses base64 decoded, due to recent upgrade.
// However, kyve stores base64 encoded
expect(
tendermintBlockResult.beginBlockEvents[4].attributes.find(
(a) => a.key === 'sender',
),
).toEqual(
kyveBlockResult.beginBlockEvents[4].attributes.find(
(a) => a.key === 'c2VuZGVy',
),
);
expect(
isEqual(
tendermintBlockResult.consensusUpdates,
kyveBlockResult.consensusUpdates,
),
).toBe(true);
expect(tendermintBlockResult.results.length).toBe(
kyveBlockResult.results.length,
);
expect(tendermintBlockResult.validatorUpdates.length).toBe(
kyveBlockResult.validatorUpdates.length,
);
expect(tendermintBlockResult.height).toBe(kyveBlockResult.height);

expect(
tendermintBlockResult.endBlockEvents[2].attributes.find(
(a) => a.key === 'recipient',
),
).toEqual(
kyveBlockResult.endBlockEvents[2].attributes.find(
(a) => a.key === 'cmVjaXBpZW50',
),
);
//
// expect(isEqual(tendermintBlockInfo, kyveBlockInfo)).toBe(true);
//
// // Note: block result will fail on expect due to deep nested array order being wrong
// // RPC uses base64 decoded, due to recent upgrade.
// // However, kyve stores base64 encoded
// expect(
// tendermintBlockResult.beginBlockEvents[4].attributes.find(
// (a) => a.key === 'sender',
// ),
// ).toEqual(
// kyveBlockResult.beginBlockEvents[4].attributes.find(
// (a) => a.key === 'c2VuZGVy',
// ),
// );
// expect(
// isEqual(
// tendermintBlockResult.consensusUpdates,
// kyveBlockResult.consensusUpdates,
// ),
// ).toBe(true);
// expect(tendermintBlockResult.results.length).toBe(
// kyveBlockResult.results.length,
// );
// expect(tendermintBlockResult.validatorUpdates.length).toBe(
// kyveBlockResult.validatorUpdates.length,
// );
// expect(tendermintBlockResult.height).toBe(kyveBlockResult.height);
//
// expect(
// tendermintBlockResult.endBlockEvents[2].attributes.find(
// (a) => a.key === 'recipient',
// ),
// ).toEqual(
// kyveBlockResult.endBlockEvents[2].attributes.find(
// (a) => a.key === 'cmVjaXBpZW50',
// ),
// );
});
it('determine correct pool', () => {
expect((kyveApi as any).poolId).toBe('2');
Expand Down
Loading

0 comments on commit d707c8f

Please sign in to comment.