Skip to content

Commit

Permalink
test: run binary-codec tests in the browser (#2566)
Browse files Browse the repository at this point in the history
- Convert tests to typescript
- Update type definitions causing errors in tests
  - `makeParser` to accept a `Buffer` in addition to `string`
  - `SerializedType` constructor allows not passing in a byte array
  - `Comparable` is now a generic type so that it allows `compareTo`
methods to take more that the type itself. Example: `Uint64.compareTo`
can accept `number`
- Update tests to use jasmine compatible functions
  - Switching from `test` to `it`.
  - Updated test checking if coretypes all implement SerializedType
  - Import fixtures directly instead of using `loadFixture` utility
- Remove importing of `buffer/` explicitly.  It was throwing off type
checking in tests.  Buffer is going away in a future PR anyway.
- Fixed `npm run clean` not clearing `.tsbuildinfo` files for keypairs
- Remove unused account-tx-transactions.db. It was likely used in the
past to test historical ledgers.
  • Loading branch information
ckniffen authored Nov 15, 2023
1 parent 3cbeeae commit cb0cb43
Show file tree
Hide file tree
Showing 58 changed files with 382 additions and 367 deletions.
6 changes: 6 additions & 0 deletions packages/ripple-binary-codec/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
* Remove `assert` dependency. If you were catching `AssertionError` you need to change to `Error`.
* Remove `create-hash` in favor of `@noble/hashes`

### Changes
* Update type definitions which causing errors in tests that the code already supported
* `makeParser` to accept a `Buffer` in addition to `string`
* `SerializedType` constructor allows not passing in a byte array
* `Comparable` is now a generic type so that it allows `compareTo` methods to take more that the type itself.

## 1.10.0 (2023-09-27)
### Added
- Support for the XChainBridge amendment.
Expand Down
15 changes: 15 additions & 0 deletions packages/ripple-binary-codec/karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const baseKarmaConfig = require('../../karma.config')
const webpackConfig = require('./test/webpack.config')
delete webpackConfig.entry

module.exports = function (config) {
baseKarmaConfig(config)

config.set({
base: '',
webpack: webpackConfig,

// list of files / patterns to load in the browser
files: ['test/**/*.test.ts'],
})
}
7 changes: 4 additions & 3 deletions packages/ripple-binary-codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"ripple-address-codec": "^5.0.0-beta.0"
},
"scripts": {
"build": "tsc -b && copyfiles ./src/enums/definitions.json ./dist/enums/",
"clean": "rm -rf ./dist ./coverage tsconfig.tsbuildinfo",
"build": "tsc --build tsconfig.build.json && copyfiles ./src/enums/definitions.json ./dist/enums/",
"clean": "rm -rf ./dist ./coverage ./test/testCompiledForWeb tsconfig.build.tsbuildinfo",
"prepublishOnly": "npm test",
"test": "npm run build && jest --verbose false --silent=false ./test/*.test.js",
"test": "npm run build && jest --verbose false --silent=false ./test/*.test.ts",
"test:browser": "npm run build && karma start ./karma.config.js",
"lint": "eslint . --ext .ts --ext .test.js"
},
"keywords": [
Expand Down
13 changes: 8 additions & 5 deletions packages/ripple-binary-codec/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ import {
} from './enums'
import { STObject } from './types/st-object'
import { JsonObject } from './types/serialized-type'
import { Buffer } from 'buffer/'

/**
* Construct a BinaryParser
*
* @param bytes hex-string to construct BinaryParser from
* @param bytes hex-string or Buffer to construct BinaryParser from
* @param definitions rippled definitions used to parse the values of transaction types and such.
* Can be customized for sidechains and amendments.
* @returns A BinaryParser
* @returns BinaryParser
*/
const makeParser = (
bytes: string,
bytes: string | Buffer,
definitions?: XrplDefinitionsBase,
): BinaryParser => new BinaryParser(bytes, definitions)
): BinaryParser =>
new BinaryParser(
bytes instanceof Buffer ? bytes.toString('hex') : bytes,
definitions,
)

/**
* Parse BinaryParser into JSON
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/enums/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BytesList, BinaryParser } from '../binary'
import { Buffer } from 'buffer/'

/*
* @brief: Bytes, name, and ordinal representing one type, ledger_type, transaction type, or result
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/enums/field.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Bytes } from './bytes'
import { SerializedType } from '../types/serialized-type'
import { TYPE_WIDTH } from './constants'
import { Buffer } from 'buffer/'

/**
* Encoding information for a rippled field, often used in transactions.
Expand Down
2 changes: 0 additions & 2 deletions packages/ripple-binary-codec/src/hash-prefixes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Buffer } from 'buffer/'

/**
* Write a 32 bit integer to a Buffer
*
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/hashes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HashPrefix } from './hash-prefixes'
import { Hash256 } from './types'
import { BytesList } from './serdes/binary-serializer'
import { Buffer } from 'buffer/'

import { sha512 } from '@xrplf/isomorphic/sha512'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/quality.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { coreTypes } from './types'
import { Buffer } from 'buffer/'

import BigNumber from 'bignumber.js'

/**
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/serdes/binary-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
FieldInstance,
} from '../enums'
import { type SerializedType } from '../types/serialized-type'
import { Buffer } from 'buffer/'

/**
* BinaryParser is used to compute fields and values from a HexString
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FieldInstance } from '../enums'
import { type SerializedType } from '../types/serialized-type'
import { Buffer } from 'buffer/'

/**
* Bytes list is a collection of buffer objects
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/shamap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HashPrefix } from './hash-prefixes'
import { Sha512Half } from './hashes'
import { Hash256 } from './types/hash-256'
import { BytesList } from './serdes/binary-serializer'
import { Buffer } from 'buffer/'

/**
* Abstract class describing a SHAMapNode
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/account-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
xAddressToClassicAddress,
} from 'ripple-address-codec'
import { Hash160 } from './hash-160'
import { Buffer } from 'buffer/'

const HEX_REGEX = /^[A-F0-9]{40}$/

Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { Currency } from './currency'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'

import BigNumber from 'bignumber.js'

/**
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/blob.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SerializedType } from './serialized-type'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Variable length encoded type
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/currency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash160 } from './hash-160'
import { Buffer } from 'buffer/'

const XRP_HEX_REGEX = /^0{40}$/
const ISO_REGEX = /^[A-Z0-9a-z?!@#$%^&*(){}[\]|]{3}$/
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/hash-128.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash } from './hash'
import { Buffer } from 'buffer/'

/**
* Hash with a width of 128 bits
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/hash-160.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash } from './hash'
import { Buffer } from 'buffer/'

/**
* Hash with a width of 160 bits
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/hash-256.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hash } from './hash'
import { Buffer } from 'buffer/'

/**
* Hash with a width of 256 bits
Expand Down
3 changes: 1 addition & 2 deletions packages/ripple-binary-codec/src/types/hash.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Comparable } from './serialized-type'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Base class defining how to encode and decode hashes
*/
class Hash extends Comparable {
class Hash extends Comparable<Hash | string> {
static readonly width: number

constructor(bytes: Buffer) {
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { Currency } from './currency'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'

/**
* Interface for JSON objects that represent amounts
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/path-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AccountID } from './account-id'
import { Currency } from './currency'
import { BinaryParser } from '../serdes/binary-parser'
import { SerializedType, JsonObject } from './serialized-type'
import { Buffer } from 'buffer/'

/**
* Constants for separating Paths in a PathSet
Expand Down
25 changes: 15 additions & 10 deletions packages/ripple-binary-codec/src/types/serialized-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BytesList } from '../serdes/binary-serializer'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

import { XrplDefinitionsBase } from '../enums'

type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject
Expand All @@ -13,7 +13,7 @@ type JsonObject = { [key: string]: JSON }
class SerializedType {
protected readonly bytes: Buffer = Buffer.alloc(0)

constructor(bytes: Buffer) {
constructor(bytes?: Buffer) {
this.bytes = bytes ?? Buffer.alloc(0)
}

Expand Down Expand Up @@ -80,26 +80,31 @@ class SerializedType {
}

/**
* Base class for SerializedTypes that are comparable
* Base class for SerializedTypes that are comparable.
*
* @template T - What types you want to allow comparisons between. You must specify all types. Primarily used to allow
* comparisons between built-in types (like `string`) and SerializedType subclasses (like `Hash`).
*
* Ex. `class Hash extends Comparable<Hash | string>`
*/
class Comparable extends SerializedType {
lt(other: Comparable): boolean {
class Comparable<T extends Object> extends SerializedType {
lt(other: T): boolean {
return this.compareTo(other) < 0
}

eq(other: Comparable): boolean {
eq(other: T): boolean {
return this.compareTo(other) === 0
}

gt(other: Comparable): boolean {
gt(other: T): boolean {
return this.compareTo(other) > 0
}

gte(other: Comparable): boolean {
gte(other: T): boolean {
return this.compareTo(other) > -1
}

lte(other: Comparable): boolean {
lte(other: T): boolean {
return this.compareTo(other) < 1
}

Expand All @@ -109,7 +114,7 @@ class Comparable extends SerializedType {
* @param other The comparable object to compare this to
* @returns A number denoting the relationship of this and other
*/
compareTo(other: Comparable): number {
compareTo(other: T): number {
throw new Error(`cannot compare ${this.toString()} and ${other.toString()}`)
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/st-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DEFAULT_DEFINITIONS, XrplDefinitionsBase } from '../enums'
import { SerializedType, JsonObject } from './serialized-type'
import { STObject } from './st-object'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

const ARRAY_END_MARKER = Buffer.from([0xf1])
const ARRAY_END_MARKER_NAME = 'ArrayEndMarker'
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/st-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SerializedType, JsonObject } from './serialized-type'
import { xAddressToClassicAddress, isValidXAddress } from 'ripple-address-codec'
import { BinaryParser } from '../serdes/binary-parser'
import { BinarySerializer, BytesList } from '../serdes/binary-serializer'
import { Buffer } from 'buffer/'

import { STArray } from './st-array'

const OBJECT_END_MARKER_BYTE = Buffer.from([0xe1])
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-16.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Derived UInt class for serializing/deserializing 16 bit UInt
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-32.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Derived UInt class for serializing/deserializing 32 bit UInt
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-64.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
const mask = BigInt(0x00000000ffffffff)
Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/uint-8.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UInt } from './uint'
import { BinaryParser } from '../serdes/binary-parser'
import { Buffer } from 'buffer/'

/**
* Derived UInt class for serializing/deserializing 8 bit UInt
Expand Down
5 changes: 2 additions & 3 deletions packages/ripple-binary-codec/src/types/uint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Comparable } from './serialized-type'
import { Buffer } from 'buffer/'

/**
* Compare numbers and bigInts n1 and n2
Expand All @@ -15,7 +14,7 @@ function compare(n1: number | bigint, n2: number | bigint): number {
/**
* Base class for serializing and deserializing unsigned integers.
*/
abstract class UInt extends Comparable {
abstract class UInt extends Comparable<UInt | number> {
protected static width: number

constructor(bytes: Buffer) {
Expand All @@ -28,7 +27,7 @@ abstract class UInt extends Comparable {
* @param other other UInt to compare this to
* @returns -1, 0, or 1 depending on how the objects relate to each other
*/
compareTo(other: UInt): number {
compareTo(other: UInt | number): number {
return compare(this.valueOf(), other.valueOf())
}

Expand Down
1 change: 0 additions & 1 deletion packages/ripple-binary-codec/src/types/vector-256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { SerializedType } from './serialized-type'
import { BinaryParser } from '../serdes/binary-parser'
import { Hash256 } from './hash-256'
import { BytesList } from '../serdes/binary-serializer'
import { Buffer } from 'buffer/'

/**
* TypeGuard for Array<string>
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple-binary-codec/src/types/xchain-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BinaryParser } from '../serdes/binary-parser'

import { AccountID } from './account-id'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'

import { Issue, IssueObject } from './issue'

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { loadFixture } = require('./utils')
const { coreTypes } = require('../src/types')
import { coreTypes } from '../src/types'
import fixtures from './fixtures/data-driven-tests.json'
const { Amount } = coreTypes
const fixtures = loadFixture('data-driven-tests.json')

function amountErrorTests() {
fixtures.values_tests
Expand Down
Loading

0 comments on commit cb0cb43

Please sign in to comment.