From 1def1f1098ed38e17eeeae915fa6d39cbdf0eed4 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 31 Jul 2023 09:04:23 +0200 Subject: [PATCH] Check types and fix tendermint --- .github/workflows/deno.yml | 29 +++++++++++++++++++++++++++++ deps.ts | 2 ++ loop.ts | 5 ++++- main.ts | 5 ++++- tendermint.ts | 16 ++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/deno.yml create mode 100644 tendermint.ts diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml new file mode 100644 index 0000000..1a23ebd --- /dev/null +++ b/.github/workflows/deno.yml @@ -0,0 +1,29 @@ +name: Deno Checks + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + deno-version: [1.35.3] + + steps: + - name: Git Checkout Deno Module + uses: actions/checkout@v2 + - name: Use Deno Version ${{ matrix.deno-version }} + uses: denolib/setup-deno@v2 + with: + deno-version: ${{ matrix.deno-version }} + - name: Check types + run: | + deno check main.ts + deno check *_test.ts + - name: Check formatting + run: deno fmt --check diff --git a/deps.ts b/deps.ts index 67c7ba1..5613e84 100644 --- a/deps.ts +++ b/deps.ts @@ -14,10 +14,12 @@ export { sha256 } from "npm:@cosmjs/crypto@^0.31.0"; export { toUtf8 } from "npm:@cosmjs/encoding@^0.31.0"; export { Decimal } from "npm:@cosmjs/math@^0.31.0"; export { DirectSecp256k1HdWallet } from "npm:@cosmjs/proto-signing@^0.31.0"; +export { Tendermint34Client, Tendermint37Client } from "npm:@cosmjs/tendermint-rpc@^0.31.0"; export { assert, isDefined, sleep } from "npm:@cosmjs/utils@^0.31.0"; export type { Coin } from "npm:@cosmjs/amino@^0.31.0"; export type { MsgExecuteContractEncodeObject } from "npm:@cosmjs/cosmwasm-stargate@^0.31.0"; export type { SignerData } from "npm:@cosmjs/stargate@^0.31.0"; +export type { TendermintClient } from "npm:@cosmjs/tendermint-rpc@^0.31.0"; // drand export type { ChainClient, ChainOptions, RandomnessBeacon } from "npm:drand-client@^1.2.0"; diff --git a/loop.ts b/loop.ts index 45e129c..46dd510 100644 --- a/loop.ts +++ b/loop.ts @@ -9,12 +9,14 @@ import { RandomnessBeacon, SignerData, SigningCosmWasmClient, + TendermintClient, } from "./deps.ts"; import { makeAddBeaconMessage } from "./drand_contract.ts"; import { ibcPacketsSent } from "./ibc.ts"; interface Capture { client: SigningCosmWasmClient; + tmClient: TendermintClient; broadcaster2: CosmWasmClient | null; broadcaster3: CosmWasmClient | null; botAddress: string; @@ -29,6 +31,7 @@ interface Capture { export async function loop( { client, + tmClient, broadcaster2, broadcaster3, botAddress, @@ -110,7 +113,7 @@ export async function loop( `✔ #${beacon.round} committed (Points: ${points}; Payout: ${payout}; Gas: ${result.gasUsed}/${result.gasWanted}; Jobs processed: ${jobs}; Transaction: ${result.transactionHash})`, ); const publishTime = timeOfRound(beacon.round) / 1000; - const { block } = await client.forceGetTmClient().block(result.height); + const { block } = await tmClient.block(result.height); const commitTime = block.header.time.getTime() / 1000; // seconds with fractional part const diff = commitTime - publishTime; console.info( diff --git a/main.ts b/main.ts index 3e39a3d..2e847cb 100644 --- a/main.ts +++ b/main.ts @@ -17,6 +17,7 @@ import { import { BeaconCache } from "./cache.ts"; import { loop } from "./loop.ts"; import { queryIsAllowListed, queryIsIncentivized } from "./drand_contract.ts"; +import { connectTendermint } from "./tendermint.ts"; // Constants const gasLimitRegister = 200_000; @@ -70,7 +71,8 @@ if (import.meta.main) { const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: config.prefix }); const [firstAccount] = await wallet.getAccounts(); - const client = await SigningCosmWasmClient.connectWithSigner(config.rpcEndpoint, wallet, { + const tmClient = await connectTendermint(config.rpcEndpoint); + const client = await SigningCosmWasmClient.createWithSigner(tmClient, wallet, { gasPrice: GasPrice.fromString(config.gasPrice), }); const botAddress = firstAccount.address; @@ -150,6 +152,7 @@ if (import.meta.main) { const didSubmit = await loop({ client, + tmClient, broadcaster2, broadcaster3, getNextSignData, diff --git a/tendermint.ts b/tendermint.ts new file mode 100644 index 0000000..980483a --- /dev/null +++ b/tendermint.ts @@ -0,0 +1,16 @@ +import { Tendermint34Client, Tendermint37Client, TendermintClient } from "./deps.ts"; + +export async function connectTendermint(endpoint: string): Promise { + // Tendermint/CometBFT 0.34/0.37 auto-detection. Starting with 0.37 we seem to get reliable versions again 🎉 + // Using 0.34 as the fallback. + let tmClient: TendermintClient; + const tm37Client = await Tendermint37Client.connect(endpoint); + const version = (await tm37Client.status()).nodeInfo.version; + if (version.startsWith("0.37.")) { + tmClient = tm37Client; + } else { + tm37Client.disconnect(); + tmClient = await Tendermint34Client.connect(endpoint); + } + return tmClient; +}