Skip to content

Commit

Permalink
Refactor/integrations (#3)
Browse files Browse the repository at this point in the history
* refactor: refactored getDataItem and prevalidateDataItem for every integration

* fix: fixed labeling of nodejs version

* feat: implemented parsed txs in cosmos runtime

* fix: fixed bundle summary of cosmos integration

* Update common/protocol/src/methods/timeouts/waitForAuthorization.ts

Co-authored-by: John Letey <[email protected]>

Co-authored-by: John Letey <[email protected]>
  • Loading branch information
troykessler and johnletey authored Dec 8, 2022
1 parent d275567 commit fdd1ae0
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 115 deletions.
2 changes: 1 addition & 1 deletion common/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class Validator {
.action(() => {
console.log(`${this.runtime.name} version: ${this.runtime.version}`);
console.log(`@kyvejs/protocol version: ${this.protocolVersion}`);
console.log(`Validator version: ${process.version}`);
console.log(`Node version: ${process.version}`);
console.log();
console.log(`Platform: ${os.platform()}`);
console.log(`Arch: ${os.arch()}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function waitForAuthorization(this: Validator): Promise<void> {
`Valaccount ${this.client.account.address} has not joined the pool with id ${this.poolId} yet`
);
this.logger.info(
`Visit https://app.kyve.network/#/pools/${this.poolId} and add join the pool with the following information:\n`
`Visit https://app.kyve.network and join the pool from your validator account:\n`
);

this.logger.info(`Valaddress: ${this.client.account.address}`);
Expand Down
30 changes: 7 additions & 23 deletions integrations/bitcoin/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ export default class Bitcoin implements IRuntime {
return { key, value: block };
}

async prevalidateDataItem(_: Validator, __: DataItem): Promise<boolean> {
// TODO: validate if PoW is valid, return valid for now
return true;
async prevalidateDataItem(_: Validator, item: DataItem): Promise<boolean> {
// TODO: maybe validate if PoW is valid
// check if item value is not null
return !!item.value;
}

async transformDataItem(_: Validator, item: DataItem): Promise<DataItem> {
// don't transform data item
// Remove confirmations to maintain determinism.
delete item.value.confirmations;

return item;
}

Expand Down Expand Up @@ -56,23 +59,4 @@ export default class Bitcoin implements IRuntime {
public async nextKey(_: Validator, key: string): Promise<string> {
return (parseInt(key) + 1).toString();
}

private async generateCoinbaseCloudHeaders(v: Validator): Promise<any> {
// requestSignature for coinbase cloud
const address = v.client.account.address;
const timestamp = new Date().valueOf().toString();
const poolId = v.pool.id;

const { signature, pub_key } = await v.client.signString(
`${address}//${poolId}//${timestamp}`
);

return {
"Content-Type": "application/json",
Signature: signature,
"Public-Key": pub_key.value,
"Pool-ID": poolId,
Timestamp: timestamp,
};
}
}
7 changes: 0 additions & 7 deletions integrations/bitcoin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,3 @@ export interface Response<T> {
error: any;
id: string;
}

export interface Signature {
signature: string;
pubKey: string;
poolId: string;
timestamp: string;
}
3 changes: 0 additions & 3 deletions integrations/bitcoin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export async function fetchBlock(
): Promise<any> {
const block = await call<any>(endpoint, "getblock", [hash, 2], headers);

// Remove confirmations to maintain determinism.
delete block.confirmations;

return block;
}

Expand Down
25 changes: 16 additions & 9 deletions integrations/celo/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { StaticCeloProvider } from '@celo-tools/celo-ethers-wrapper';
import { DataItem, IRuntime, Validator, sha256 } from '@kyvejs/protocol';
import { providers } from 'ethers';

import { name, version } from '../package.json';
import { fetchBlock } from './utils';

export default class Celo implements IRuntime {
public name = name;
Expand All @@ -15,20 +16,26 @@ export default class Celo implements IRuntime {
// get auth headers for proxy endpoints
const headers = await v.getProxyAuth();

const block = await fetchBlock(source, +key, headers);
const provider = new StaticCeloProvider({ url: source, headers });
const value = await provider.getBlockWithTransactions(+key);

if (!block) throw new Error();

return { key, value: block };
return { key, value };
}

async prevalidateDataItem(_: Validator, __: DataItem): Promise<boolean> {
// TODO: return valid for now
return true;
async prevalidateDataItem(_: Validator, item: DataItem): Promise<boolean> {
// check if item value is not null
return !!item.value;
}

async transformDataItem(_: Validator, item: DataItem): Promise<DataItem> {
// don't transform data item
// Delete the number of confirmations from a transaction to maintain determinism.
item.value.transactions.forEach(
(tx: Partial<providers.TransactionResponse>) => delete tx.confirmations
);

// TODO: Figure out why `extraData` varies for some blocks.
delete item.value.extraData;

return item;
}

Expand Down
26 changes: 0 additions & 26 deletions integrations/celo/src/utils.ts

This file was deleted.

20 changes: 7 additions & 13 deletions integrations/cosmos/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataItem, IRuntime, Validator, sha256 } from '@kyvejs/protocol';
import axios from 'axios';
import { fetchBlock } from './utils';
import { name, version } from '../package.json';

export default class Cosmos implements IRuntime {
Expand All @@ -13,20 +13,14 @@ export default class Cosmos implements IRuntime {
): Promise<DataItem> {
// get auth headers for proxy endpoints
const headers = await v.getProxyAuth();
const value = await fetchBlock(source, +key, headers);

const { data } = await axios.get(
`${source}/cosmos/base/tendermint/v1beta1/blocks/${key}`,
{
headers,
}
);

return { key, value: data };
return { key, value };
}

async prevalidateDataItem(_: Validator, __: DataItem): Promise<boolean> {
// TODO: return valid for now
return true;
async prevalidateDataItem(_: Validator, item: DataItem): Promise<boolean> {
// check if item value is not null
return !!item.value;
}

async transformDataItem(_: Validator, item: DataItem): Promise<DataItem> {
Expand All @@ -50,7 +44,7 @@ export default class Cosmos implements IRuntime {
}

async summarizeDataBundle(_: Validator, bundle: DataItem[]): Promise<string> {
return bundle.at(-1)?.value?.header?.app_hash ?? '';
return bundle.at(-1)?.value?.block?.header?.app_hash ?? '';
}

async nextKey(_: Validator, key: string): Promise<string> {
Expand Down
6 changes: 0 additions & 6 deletions integrations/cosmos/src/types.ts

This file was deleted.

72 changes: 72 additions & 0 deletions integrations/cosmos/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import axios from 'axios';
import { createHash } from 'crypto';
import { sleep } from '@kyvejs/protocol';

export async function fetchBlock(
endpoint: string,
height: number,
headers: any
): Promise<any> {
const value = await call<any>(
`${endpoint}/cosmos/base/tendermint/v1beta1/blocks/${height}`,
headers
);

const parsed_txs = await fetchTransactions(
endpoint,
value.block.data.txs.map(parseEncodedTx),
headers
);

return {
...value,
__kyve: {
block: {
data: {
parsed_txs,
},
},
},
};
}

async function fetchTransactions(
endpoint: string,
hashes: string[],
headers: any
): Promise<any[]> {
const txs: any[] = [];

for (const hash of hashes) {
try {
const tx = await call<any>(
`${endpoint}/cosmos/tx/v1beta1/txs/${hash}`,
headers
);

txs.push(tx.tx_response);
} catch {
txs.push(null);
} finally {
await sleep(500);
}
}

return txs;
}

async function call<T>(endpoint: string, headers: any): Promise<T> {
const { data } = await axios.get<T>(endpoint, {
headers,
});

return data;
}

function parseEncodedTx(tx: string): string {
const txBytes = new Uint8Array(
createHash('sha256').update(Buffer.from(tx, 'base64')).digest()
);

return Buffer.from(txBytes.slice(0, 32)).toString('hex').toUpperCase();
}
33 changes: 7 additions & 26 deletions integrations/evm/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,24 @@ export default class Evm implements IRuntime {
source: string,
key: string
): Promise<DataItem> {
// set network settings if available
let network;

if (v.poolConfig.chainId && v.poolConfig.chainName) {
network = {
chainId: v.poolConfig.chainId,
name: v.poolConfig.chainName,
};
}

// get auth headers for proxy endpoints
const headers = await v.getProxyAuth();

// setup web3 provider
const provider = new providers.StaticJsonRpcProvider(
{
url: source,
headers,
},
network
);

// fetch data item
const provider = new providers.StaticJsonRpcProvider({
url: source,
headers,
});
const value = await provider.getBlockWithTransactions(+key);

// throw if data item is not available
if (!value) throw new Error();

return {
key,
value,
};
}

async prevalidateDataItem(_: Validator, __: DataItem): Promise<boolean> {
// TODO: return valid for now
return true;
async prevalidateDataItem(_: Validator, item: DataItem): Promise<boolean> {
// check if item value is not null
return !!item.value;
}

async transformDataItem(_: Validator, item: DataItem): Promise<DataItem> {
Expand Down

0 comments on commit fdd1ae0

Please sign in to comment.