Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Fix json-rpc tests but break integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsmale90 committed Mar 28, 2022
1 parent 0ec3f30 commit f39c97d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class BaseJsonRpcType<
case "string": {
// handle hex-encoded string
if ((value as string).indexOf("0x") === 0) {
toStrings.set(this, () => (+value).toString(16));
strCache.set(this, (value as string).toLowerCase());
toBuffers.set(this, () => {
let fixedValue = (value as string).slice(2);
Expand Down
31 changes: 15 additions & 16 deletions src/packages/utils/src/things/json-rpc/json-rpc-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@ import { BaseJsonRpcType } from "./json-rpc-base-types";
import { strCache, toStrings } from "./json-rpc-base-types";

function validateByteLength(byteLength?: number) {
if (typeof byteLength !== "number" || byteLength < 0) {
throw new Error(`byteLength must be a number greater than 0`);
if (typeof byteLength !== "number" || !(byteLength > 0)) {
throw new Error(`byteLength must be a number greater than 0, provided: ${byteLength}`);
}
}
const byteLengths = new WeakMap();

export class Data extends BaseJsonRpcType {
constructor(value: string | Buffer, byteLength?: number) {

constructor(value: string | Buffer, private _byteLength?: number) {
super(value);
if (typeof value === "bigint") {
throw new Error(`Cannot create a ${typeof value} as a Data`);
}
super(value);
if (byteLength !== void 0) {
validateByteLength(byteLength);
byteLengths.set(this, byteLength | 0);
if (_byteLength !== undefined) {
validateByteLength(_byteLength);
}
}
public toString(byteLength?: number): string {
const str = strCache.get(this) as string;
if (str !== void 0) {
return str;
if (byteLength === undefined) {
byteLength = this._byteLength;
}
if (byteLength === undefined && strCache.has(this)) {
return strCache.get(this) as string;
} else {
let str = toStrings.get(this)() as string;
let length = str.length;

if (length % 2 === 1) {
length++;
str = `0${str}`;
}

if (byteLength !== void 0) {
if (byteLength !== undefined) {
validateByteLength(byteLength);
} else {
byteLength = byteLengths.get(this);
}
if (byteLength !== void 0) {
const strLength = byteLength * 2;
const padBy = strLength - length;
if (padBy < 0) {
Expand Down
9 changes: 6 additions & 3 deletions src/packages/utils/tests/json-rpc-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("json-rpc-data", () => {
const d = new Data(c.input, c.constructorLength);
const stringified = d.toString();

assert.equal(stringified, c.expected);
assert.equal(stringified, c.expected, `Expected "${stringified}" to be "${c.expected}", for input "${c.input}" and constructorLength ${c.constructorLength}`);
});
});

Expand All @@ -87,10 +87,13 @@ describe("json-rpc-data", () => {
it("should fail with invalid byte lengths", () => {
const invalidByteLengths = [ -1, 0, "1", {}, [], null, NaN ]; // undefined is a valid value

const d = new Data("0x01");
invalidByteLengths.forEach(byteLength => {
const d = new Data("0x01");
assert.throws(() => d.toString(<any>byteLength), { message: `byteLength must be a number greater than 0, provided: ${byteLength}` }, `Invalid bytelength provided to toString: <${typeof byteLength}>: ${byteLength}`);
});

invalidByteLengths.forEach(byteLength => {
assert.throws(() => d.toString(<any>byteLength), { message: "byteLength must be a number greater than 0" }, `Invalid bytelength provided: <${typeof byteLength}>: ${byteLength}`);
assert.throws(() => new Data("0x01", <any>byteLength), { message: `byteLength must be a number greater than 0, provided: ${byteLength}` }, `Invalid bytelength provided to ctor: <${typeof byteLength}>: ${byteLength}`);
});
});
});
Expand Down

0 comments on commit f39c97d

Please sign in to comment.