-
Notifications
You must be signed in to change notification settings - Fork 331
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 #1187 from cosmos/large-voting-powers
Support voting powers > MAX_SAFE_INTEGER
- Loading branch information
Showing
17 changed files
with
276 additions
and
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { apiToBigInt } from "./inthelpers"; | ||
|
||
describe("inthelpers", () => { | ||
describe("apiToBigInt", () => { | ||
it("works for positive an negative ints", () => { | ||
expect(apiToBigInt("0")).toEqual(BigInt(0)); | ||
expect(apiToBigInt("1")).toEqual(BigInt(1)); | ||
expect(apiToBigInt("100")).toEqual(BigInt(100)); | ||
expect(apiToBigInt("-1")).toEqual(BigInt(-1)); | ||
expect(apiToBigInt("-100")).toEqual(BigInt(-100)); | ||
expect(apiToBigInt("9007199254740991")).toEqual(BigInt(9007199254740991)); | ||
expect(apiToBigInt("-9007199254740991")).toEqual(BigInt(-9007199254740991)); | ||
// uint64 max | ||
expect(apiToBigInt("18446744073709551615")).toEqual(BigInt("18446744073709551615")); | ||
// int64 min/max | ||
expect(apiToBigInt("-9223372036854775808")).toEqual(BigInt("-9223372036854775808")); | ||
expect(apiToBigInt("9223372036854775807")).toEqual(BigInt("9223372036854775807")); | ||
}); | ||
|
||
it("throws for ill-formatted inputs", () => { | ||
// empty | ||
expect(() => apiToBigInt("")).toThrowError(/invalid string format/i); | ||
expect(() => apiToBigInt("-")).toThrowError(/invalid string format/i); | ||
|
||
// non decimal representation | ||
expect(() => apiToBigInt("0x0")).toThrowError(/invalid string format/i); | ||
expect(() => apiToBigInt("0x01")).toThrowError(/invalid string format/i); | ||
expect(() => apiToBigInt("0x")).toThrowError(/invalid string format/i); | ||
|
||
// decimal points | ||
expect(() => apiToBigInt("1.0")).toThrowError(/invalid string format/i); | ||
|
||
// Invalid dashes | ||
expect(() => apiToBigInt("1-")).toThrowError(/invalid string format/i); | ||
expect(() => apiToBigInt("--1")).toThrowError(/invalid string format/i); | ||
expect(() => apiToBigInt("1-1")).toThrowError(/invalid string format/i); | ||
expect(() => apiToBigInt("-1-1")).toThrowError(/invalid string format/i); | ||
}); | ||
}); | ||
}); |
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,36 @@ | ||
import { Int53 } from "@cosmjs/math"; | ||
|
||
import { assertString } from "./tendermint34/encodings"; | ||
|
||
/** | ||
* Takes an integer value from the Tendermint RPC API and | ||
* returns it as number. | ||
* | ||
* Only works within the safe integer range. | ||
*/ | ||
export function apiToSmallInt(input: string | number): number { | ||
const asInt = typeof input === "number" ? new Int53(input) : Int53.fromString(input); | ||
return asInt.toNumber(); | ||
} | ||
|
||
/** | ||
* Takes an integer value from the Tendermint RPC API and | ||
* returns it as BigInt. | ||
* | ||
* This supports the full uint64 and int64 ranges. | ||
*/ | ||
export function apiToBigInt(input: string): bigint { | ||
assertString(input); // Runtime check on top of TypeScript just to be safe for semi-trusted API types | ||
if (!input.match(/^-?[0-9]+$/)) { | ||
throw new Error("Invalid string format"); | ||
} | ||
return BigInt(input); | ||
} | ||
|
||
/** | ||
* Takes an integer in the safe integer range and returns | ||
* a string representation to be used in the Tendermint RPC API. | ||
*/ | ||
export function smallIntToApi(num: number): string { | ||
return new Int53(num).toString(); | ||
} |
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
Oops, something went wrong.