Skip to content

Commit

Permalink
fix: encoding Decimal values
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 16, 2024
1 parent a32320a commit 6808b11
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 1,092 deletions.
2 changes: 1 addition & 1 deletion packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"devDependencies": {
"@agoric/cosmos": "^0.34.1",
"@ava/typescript": "^4.1.0",
"@cosmology/telescope": "^1.7.1",
"@cosmology/telescope": "https://gitpkg.vercel.app/agoric-labs/telescope/packages/telescope?8d2c2f6ba637a5578eead09a7368dc41c262a9d0",
"@endo/bundle-source": "^3.4.0",
"@endo/import-bundle": "^1.2.2",
"ava": "^5.3.1",
Expand Down
14 changes: 7 additions & 7 deletions packages/cosmic-proto/src/codegen/binary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* This file and any referenced files were automatically generated by @cosmology/telescope@1.8.3
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down Expand Up @@ -100,7 +100,7 @@ export class BinaryReader implements IBinaryReader {
len: number;

assertBounds(): void {
if (this.pos > this.len) throw RangeError('premature EOF');
if (this.pos > this.len) throw new RangeError('premature EOF');
}

constructor(buf?: ArrayLike<number>) {
Expand All @@ -115,7 +115,7 @@ export class BinaryReader implements IBinaryReader {
fieldNo = tag >>> 3,
wireType = tag & 7;
if (fieldNo <= 0 || wireType < 0 || wireType > 5)
throw Error(
throw new Error(
'illegal tag: field no ' + fieldNo + ' wire type ' + wireType,
);
return [fieldNo, wireType, tag];
Expand Down Expand Up @@ -214,11 +214,11 @@ export class BinaryReader implements IBinaryReader {
}

float(): number {
throw Error('float not supported');
throw new Error('float not supported');
}

double(): number {
throw Error('double not supported');
throw new Error('double not supported');
}

bool(): boolean {
Expand Down Expand Up @@ -466,11 +466,11 @@ export class BinaryWriter implements IBinaryWriter {
sfixed32 = BinaryWriter.prototype.fixed32;

float(value: number): BinaryWriter {
throw Error('float not supported' + value);
throw new Error('float not supported' + value);
}

double(value: number): BinaryWriter {
throw Error('double not supported' + value);
throw new Error('double not supported' + value);
}

bytes(value: Uint8Array): BinaryWriter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export enum FieldDescriptorProto_Type {
* treat group fields as unknown fields.
*/
TYPE_GROUP = 10,
/** TYPE_MESSAGE - Length-delimited aggregate. */
TYPE_MESSAGE = 11,
/** TYPE_BYTES - New in version 2. */
TYPE_BYTES = 12,
Expand Down Expand Up @@ -198,12 +199,9 @@ export function fieldDescriptorProto_LabelToJSON(
}
/** Generated classes can be optimized for speed or code size. */
export enum FileOptions_OptimizeMode {
/**
* SPEED - Generate complete code for parsing, serialization,
* etc.
*/
/** SPEED - Generate complete code for parsing, serialization, */
SPEED = 1,
/** CODE_SIZE - Use ReflectionOps to implement these methods. */
/** CODE_SIZE - etc. */
CODE_SIZE = 2,
/** LITE_RUNTIME - Generate code using MessageLite and the lite runtime. */
LITE_RUNTIME = 3,
Expand Down Expand Up @@ -393,6 +391,7 @@ export interface FileDescriptorSetSDKType {
export interface FileDescriptorProto {
/** file name, relative to root of source tree */
name: string;
/** e.g. "foo", "foo.bar", etc. */
package: string;
/** Names of files imported by this file. */
dependency: string[];
Expand Down
72 changes: 60 additions & 12 deletions packages/cosmic-proto/src/codegen/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* This file and any referenced files were automatically generated by @cosmology/telescope@1.8.3
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down Expand Up @@ -35,7 +35,7 @@ export function omitDefault<T extends string | number | bigint | boolean>(
return input === BigInt(0) ? undefined : input;
}

throw Error(`Got unsupported type ${typeof input}`);
throw new Error(`Got unsupported type ${typeof input}`);
}

interface Duration {
Expand Down Expand Up @@ -223,9 +223,10 @@ function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

// START agoric-sdk patch
// The largest value we need is 18 (Ether).
const maxFractionalDigits = 30;
// Subset of Decimal in @cosmjs/math

/**
* A type for arbitrary precision, non-negative decimals.
*
Expand All @@ -241,7 +242,9 @@ export class Decimal {
const badCharacter = input.match(/[^0-9.]/);
if (badCharacter) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
throw Error(`Invalid character at position ${badCharacter.index! + 1}`);
throw new Error(
`Invalid character at position ${badCharacter.index! + 1}`,
);
}

let whole: string;
Expand All @@ -250,7 +253,7 @@ export class Decimal {
if (input === '') {
whole = '0';
fractional = '';
} else if (input.search(/./) === -1) {
} else if (input.search(/\./) === -1) {
// integer format, no separator
whole = input;
fractional = '';
Expand All @@ -259,21 +262,21 @@ export class Decimal {
switch (parts.length) {
case 0:
case 1:
throw Error(
throw new Error(
'Fewer than two elements in split result. This must not happen here.',
);
case 2:
if (!parts[1]) throw Error('Fractional part missing');
if (!parts[1]) throw new Error('Fractional part missing');
whole = parts[0];
fractional = parts[1].replace(/0+$/, '');
break;
default:
throw Error('More than one separator found');
throw new Error('More than one separator found');
}
}

if (fractional.length > fractionalDigits) {
throw Error('Got more fractional digits than supported');
throw new Error('Got more fractional digits than supported');
}

const quantity = `${whole}${fractional.padEnd(fractionalDigits, '0')}`;
Expand All @@ -291,11 +294,56 @@ export class Decimal {

private static verifyFractionalDigits(fractionalDigits: number): void {
if (!Number.isInteger(fractionalDigits))
throw Error('Fractional digits is not an integer');
throw new Error('Fractional digits is not an integer');
if (fractionalDigits < 0)
throw Error('Fractional digits must not be negative');
throw new Error('Fractional digits must not be negative');
if (fractionalDigits > maxFractionalDigits) {
throw Error(`Fractional digits must not exceed ${maxFractionalDigits}`);
throw new Error(
`Fractional digits must not exceed ${maxFractionalDigits}`,
);
}
}

public get atomics(): string {
return this.data.atomics.toString();
}

public get fractionalDigits(): number {
return this.data.fractionalDigits;
}

private readonly data: {
readonly atomics: bigint;
readonly fractionalDigits: number;
};

private constructor(atomics: string, fractionalDigits: number) {
if (!atomics.match(/^[0-9]+$/)) {
throw new Error(
'Invalid string format. Only non-negative integers in decimal representation supported.',
);
}

this.data = {
atomics: BigInt(atomics),
fractionalDigits: fractionalDigits,
};
}

public toString(): string {
const factor = BigInt(10) ** BigInt(this.data.fractionalDigits);
const whole = this.data.atomics / factor;
const fractional = this.data.atomics % factor;

if (fractional === 0n) {
return whole.toString();
} else {
const fullFractionalPart = fractional
.toString()
.padStart(this.data.fractionalDigits, '0');
const trimmedFractionalPart = fullFractionalPart.replace(/0+$/, '');
return `${whole.toString()}.${trimmedFractionalPart}`;
}
}
}
// END agoric-sdk patch
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* This file and any referenced files were automatically generated by @cosmology/telescope@1.8.3
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
14 changes: 8 additions & 6 deletions packages/cosmic-proto/src/codegen/json-safe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* This file and any referenced files were automatically generated by @cosmology/telescope@1.8.3
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/

export type JsonSafe<T> = {
[Prop in keyof T]: T[Prop] extends Uint8Array | bigint | Date
? string
: T[Prop];
};
export type JsonSafe<T> = T extends Uint8Array | bigint | Date
? string
: T extends Array<infer U>
? Array<JsonSafe<U>>
: T extends object
? { [K in keyof T]: JsonSafe<T[K]> }
: T;
7 changes: 2 additions & 5 deletions packages/cosmic-proto/src/codegen/tendermint/abci/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,9 @@ export interface TxResultSDKType {
}
/** Validator */
export interface Validator {
/**
* The first 20 bytes of SHA256(public key)
* PubKey pub_key = 2 [(gogoproto.nullable)=false];
*/
/** The first 20 bytes of SHA256(public key) */
address: Uint8Array;
/** The voting power */
/** PubKey pub_key = 2 [(gogoproto.nullable)=false]; */
power: bigint;
}
export interface ValidatorProtoMsg {
Expand Down
2 changes: 2 additions & 0 deletions packages/cosmic-proto/src/codegen/tendermint/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export interface Header {
lastBlockId: BlockID;
/** hashes of block data */
lastCommitHash: Uint8Array;
/** transactions */
dataHash: Uint8Array;
/** hashes from the app output from the prev block */
validatorsHash: Uint8Array;
Expand All @@ -167,6 +168,7 @@ export interface Header {
consensusHash: Uint8Array;
/** state after txs from the previous block */
appHash: Uint8Array;
/** root hash of all results from the txs from the previous block */
lastResultsHash: Uint8Array;
/** consensus info */
evidenceHash: Uint8Array;
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-proto/src/codegen/utf8.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* This file and any referenced files were automatically generated by @cosmology/telescope@1.8.3
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/cosmic-proto/src/codegen/varint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ts-nocheck
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@1.7.1
* This file and any referenced files were automatically generated by @cosmology/telescope@1.8.3
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/
Expand Down Expand Up @@ -86,7 +86,7 @@ export function varint64read(this: ReaderLike): [number, number] {
}
}

throw Error('invalid varint');
throw new Error('invalid varint');
}

/**
Expand Down Expand Up @@ -353,7 +353,7 @@ export function varint32read(this: ReaderLike): number {
for (let readBytes = 5; (b & 0x80) !== 0 && readBytes < 10; readBytes++)
b = this.buf[this.pos++];

if ((b & 0x80) != 0) throw Error('invalid varint');
if ((b & 0x80) != 0) throw new Error('invalid varint');

this.assertBounds();

Expand Down
Loading

0 comments on commit 6808b11

Please sign in to comment.