-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1173 from cosmos/tendermint-0.35
Add Tendermint 0.35 client
- Loading branch information
Showing
22 changed files
with
3,678 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { hashBlock, hashTx } from "../hasher"; | ||
import { Params } from "./requests"; | ||
import { Responses } from "./responses"; | ||
import { Adaptor } from "./types"; | ||
|
||
export { Decoder, Encoder, Params, Responses } from "./types"; | ||
|
||
export const adaptor35: Adaptor = { | ||
params: Params, | ||
responses: Responses, | ||
hashTx: hashTx, | ||
hashBlock: hashBlock, | ||
}; |
184 changes: 184 additions & 0 deletions
184
packages/tendermint-rpc/src/tendermint35/adaptor/requests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { toBase64, toHex } from "@cosmjs/encoding"; | ||
import { JsonRpcRequest } from "@cosmjs/json-rpc"; | ||
|
||
import { createJsonRpcRequest } from "../../jsonrpc"; | ||
import { assertNotEmpty, Integer, may } from "../encodings"; | ||
import * as requests from "../requests"; | ||
|
||
interface HeightParam { | ||
readonly height?: number; | ||
} | ||
interface RpcHeightParam { | ||
readonly height?: string; | ||
} | ||
function encodeHeightParam(param: HeightParam): RpcHeightParam { | ||
return { | ||
height: may(Integer.encode, param.height), | ||
}; | ||
} | ||
|
||
interface RpcBlockchainRequestParams { | ||
readonly minHeight?: string; | ||
readonly maxHeight?: string; | ||
} | ||
|
||
function encodeBlockchainRequestParams(param: requests.BlockchainRequestParams): RpcBlockchainRequestParams { | ||
return { | ||
minHeight: may(Integer.encode, param.minHeight), | ||
maxHeight: may(Integer.encode, param.maxHeight), | ||
}; | ||
} | ||
|
||
interface RpcBlockSearchParams { | ||
readonly query: string; | ||
readonly page?: string; | ||
readonly per_page?: string; | ||
readonly order_by?: string; | ||
} | ||
function encodeBlockSearchParams(params: requests.BlockSearchParams): RpcBlockSearchParams { | ||
return { | ||
query: params.query, | ||
page: may(Integer.encode, params.page), | ||
per_page: may(Integer.encode, params.per_page), | ||
order_by: params.order_by, | ||
}; | ||
} | ||
|
||
interface RpcAbciQueryParams { | ||
readonly path: string; | ||
/** hex encoded */ | ||
readonly data: string; | ||
readonly height?: string; | ||
readonly prove?: boolean; | ||
} | ||
|
||
function encodeAbciQueryParams(params: requests.AbciQueryParams): RpcAbciQueryParams { | ||
return { | ||
path: assertNotEmpty(params.path), | ||
data: toHex(params.data), | ||
height: may(Integer.encode, params.height), | ||
prove: params.prove, | ||
}; | ||
} | ||
|
||
interface RpcBroadcastTxParams { | ||
/** base64 encoded */ | ||
readonly tx: string; | ||
} | ||
function encodeBroadcastTxParams(params: requests.BroadcastTxParams): RpcBroadcastTxParams { | ||
return { | ||
tx: toBase64(assertNotEmpty(params.tx)), | ||
}; | ||
} | ||
|
||
interface RpcTxParams { | ||
/** hex encoded */ | ||
readonly hash: string; | ||
readonly prove?: boolean; | ||
} | ||
function encodeTxParams(params: requests.TxParams): RpcTxParams { | ||
return { | ||
hash: toHex(assertNotEmpty(params.hash)), | ||
prove: params.prove, | ||
}; | ||
} | ||
|
||
interface RpcTxSearchParams { | ||
readonly query: string; | ||
readonly prove?: boolean; | ||
readonly page?: string; | ||
readonly per_page?: string; | ||
readonly order_by?: string; | ||
} | ||
function encodeTxSearchParams(params: requests.TxSearchParams): RpcTxSearchParams { | ||
return { | ||
query: params.query, | ||
prove: params.prove, | ||
page: may(Integer.encode, params.page), | ||
per_page: may(Integer.encode, params.per_page), | ||
order_by: params.order_by, | ||
}; | ||
} | ||
|
||
interface RpcValidatorsParams { | ||
readonly height?: string; | ||
readonly page?: string; | ||
readonly per_page?: string; | ||
} | ||
function encodeValidatorsParams(params: requests.ValidatorsParams): RpcValidatorsParams { | ||
return { | ||
height: may(Integer.encode, params.height), | ||
page: may(Integer.encode, params.page), | ||
per_page: may(Integer.encode, params.per_page), | ||
}; | ||
} | ||
|
||
export class Params { | ||
public static encodeAbciInfo(req: requests.AbciInfoRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method); | ||
} | ||
|
||
public static encodeAbciQuery(req: requests.AbciQueryRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeAbciQueryParams(req.params)); | ||
} | ||
|
||
public static encodeBlock(req: requests.BlockRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeHeightParam(req.params)); | ||
} | ||
|
||
public static encodeBlockchain(req: requests.BlockchainRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeBlockchainRequestParams(req.params)); | ||
} | ||
|
||
public static encodeBlockResults(req: requests.BlockResultsRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeHeightParam(req.params)); | ||
} | ||
|
||
public static encodeBlockSearch(req: requests.BlockSearchRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeBlockSearchParams(req.params)); | ||
} | ||
|
||
public static encodeBroadcastTx(req: requests.BroadcastTxRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeBroadcastTxParams(req.params)); | ||
} | ||
|
||
public static encodeCommit(req: requests.CommitRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeHeightParam(req.params)); | ||
} | ||
|
||
public static encodeGenesis(req: requests.GenesisRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method); | ||
} | ||
|
||
public static encodeHealth(req: requests.HealthRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method); | ||
} | ||
|
||
public static encodeNumUnconfirmedTxs(req: requests.NumUnconfirmedTxsRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method); | ||
} | ||
|
||
public static encodeStatus(req: requests.StatusRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method); | ||
} | ||
|
||
public static encodeSubscribe(req: requests.SubscribeRequest): JsonRpcRequest { | ||
const eventTag = { key: "tm.event", value: req.query.type }; | ||
const query = requests.buildQuery({ tags: [eventTag], raw: req.query.raw }); | ||
return createJsonRpcRequest("subscribe", { query: query }); | ||
} | ||
|
||
public static encodeTx(req: requests.TxRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeTxParams(req.params)); | ||
} | ||
|
||
// TODO: encode params for query string??? | ||
public static encodeTxSearch(req: requests.TxSearchRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeTxSearchParams(req.params)); | ||
} | ||
|
||
public static encodeValidators(req: requests.ValidatorsRequest): JsonRpcRequest { | ||
return createJsonRpcRequest(req.method, encodeValidatorsParams(req.params)); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
packages/tendermint-rpc/src/tendermint35/adaptor/responses.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { fromBase64, fromHex } from "@cosmjs/encoding"; | ||
|
||
import { decodeValidatorGenesis, decodeValidatorInfo, decodeValidatorUpdate } from "./responses"; | ||
|
||
describe("Adaptor Responses", () => { | ||
describe("decodeValidatorGenesis", () => { | ||
it("works for genesis format", () => { | ||
// from https://raw.githubusercontent.com/cosmos/mainnet/master/genesis.json | ||
const validator = decodeValidatorGenesis({ | ||
address: "A03DC128D38DB0BC5F18AE1872F1CB2E1FD41157", | ||
name: "真本聪&IOSG", | ||
power: "169980", | ||
pub_key: { | ||
type: "tendermint/PubKeyEd25519", | ||
value: "2BX6Zuj8RmdJAkD1BAg6KB0v04liyM7jBdwOGIb9F9Q=", | ||
}, | ||
}); | ||
expect(validator).toEqual({ | ||
address: fromHex("A03DC128D38DB0BC5F18AE1872F1CB2E1FD41157"), | ||
votingPower: 169980, | ||
pubkey: { | ||
algorithm: "ed25519", | ||
data: fromBase64("2BX6Zuj8RmdJAkD1BAg6KB0v04liyM7jBdwOGIb9F9Q="), | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("decodeValidatorUpdate", () => { | ||
it("works for block results format", () => { | ||
// from https://rpc.cosmos.network/block_results?height=10539773 | ||
const update = decodeValidatorUpdate({ | ||
pub_key: { | ||
Sum: { | ||
type: "tendermint.crypto.PublicKey_Ed25519", | ||
value: { | ||
ed25519: "0kNlxBMpm+5WtfHIG1xsWatOXTKPLtmSqn3EiEIDZeI=", | ||
}, | ||
}, | ||
}, | ||
power: "11418237", | ||
}); | ||
expect(update).toEqual({ | ||
pubkey: { | ||
algorithm: "ed25519", | ||
data: fromBase64("0kNlxBMpm+5WtfHIG1xsWatOXTKPLtmSqn3EiEIDZeI="), | ||
}, | ||
votingPower: 11418237, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("decodeValidatorInfo", () => { | ||
it("works for validators format", () => { | ||
// from https://rpc.cosmos.network/validators?height=10601034 | ||
const info = decodeValidatorInfo({ | ||
address: "AC2D56057CD84765E6FBE318979093E8E44AA18F", | ||
pub_key: { | ||
type: "tendermint/PubKeyEd25519", | ||
value: "0kNlxBMpm+5WtfHIG1xsWatOXTKPLtmSqn3EiEIDZeI=", | ||
}, | ||
voting_power: "11228980", | ||
proposer_priority: "62870960", | ||
}); | ||
expect(info).toEqual({ | ||
address: fromHex("AC2D56057CD84765E6FBE318979093E8E44AA18F"), | ||
pubkey: { | ||
algorithm: "ed25519", | ||
data: fromBase64("0kNlxBMpm+5WtfHIG1xsWatOXTKPLtmSqn3EiEIDZeI="), | ||
}, | ||
votingPower: 11228980, | ||
proposerPriority: 62870960, | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.