@typeberry/trie
Advanced tools
Comparing version
@@ -39,3 +39,3 @@ /** | ||
private constructor(data: Uint8Array) { | ||
protected constructor(data: Uint8Array) { | ||
this.buffer = data; | ||
@@ -52,13 +52,41 @@ this.length = data.byteLength; | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other: BytesBlob): boolean { | ||
if (this.length !== other.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.buffer[i] !== other.buffer[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v: string): BytesBlob { | ||
const encoder = new TextEncoder(); | ||
return BytesBlob.fromBlob(encoder.encode(v)); | ||
return BytesBlob.from(encoder.encode(v)); | ||
} | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
static fromBlob(v: Uint8Array): BytesBlob { | ||
static from(v: Uint8Array): BytesBlob { | ||
return new BytesBlob(v); | ||
} | ||
/** Create a new [`BytesBlob`] by concatenating data from multiple `Uint8Array`s. */ | ||
static copyFromBlobs(v: Uint8Array, ...rest: Uint8Array[]) { | ||
const totalLength = v.length + rest.reduce((a, v) => a + v.length, 0); | ||
const buffer = new Uint8Array(totalLength); | ||
buffer.set(v, 0); | ||
let offset = v.length; | ||
for (const r of rest) { | ||
buffer.set(r, offset); | ||
offset += r.length; | ||
} | ||
return new BytesBlob(buffer); | ||
} | ||
/** Create a new [`BytesBlob`] from an array of bytes. */ | ||
@@ -99,5 +127,3 @@ static fromNumbers(v: number[]): BytesBlob { | ||
*/ | ||
declare class Bytes<T extends number> { | ||
/** Raw bytes array. */ | ||
readonly raw: Uint8Array; | ||
declare class Bytes<T extends number> extends BytesBlob { | ||
/** Length of the bytes array. */ | ||
@@ -107,27 +133,12 @@ readonly length: T; | ||
private constructor(raw: Uint8Array, len: T) { | ||
super(raw); | ||
check(raw.byteLength === len, `Given buffer has incorrect size ${raw.byteLength} vs expected ${len}`); | ||
this.raw = raw; | ||
this.length = len; | ||
} | ||
/** Return hex encoding of the sequence. */ | ||
toString() { | ||
return bytesToHexString(this.raw); | ||
/** Raw bytes array. */ | ||
get raw(): Uint8Array { | ||
return this.buffer; | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other: Bytes<T>): boolean { | ||
if (this.length !== other.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.raw[i] !== other.raw[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** Create new [`Bytes<X>`] given a backing buffer and it's length. */ | ||
@@ -242,4 +253,18 @@ static fromBlob<X extends number>(v: Uint8Array, len: X): Bytes<X> { | ||
type Hash = Bytes<32>; | ||
type StateKey = Opaque<Bytes<32>, "stateKey">; | ||
/** | ||
* Size of the output of the hash functions. | ||
* | ||
* https://graypaper.fluffylabs.dev/#/387103d/071401071f01 | ||
* | ||
*/ | ||
declare const HASH_SIZE = 32; | ||
/** A type for the above value. */ | ||
type HASH_SIZE = typeof HASH_SIZE; | ||
/** | ||
* Opaque, unknown hash. | ||
*/ | ||
type OpaqueHash = Bytes<HASH_SIZE>; | ||
type StateKey = Opaque<OpaqueHash, "stateKey">; | ||
type TruncatedStateKey = Opaque<Bytes<31>, "stateKey">; | ||
@@ -251,4 +276,4 @@ /** | ||
*/ | ||
type TrieHash = Opaque<Hash, "trie">; | ||
type ValueHash = Opaque<Hash, "trieValue">; | ||
type TrieHash = Opaque<OpaqueHash, "trie">; | ||
type ValueHash = Opaque<OpaqueHash, "trieValue">; | ||
@@ -433,3 +458,3 @@ /** | ||
const len = this.getValueLength(); | ||
return BytesBlob.fromBlob(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.from(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
} | ||
@@ -448,12 +473,4 @@ | ||
/** | ||
* Size of the output of the hash functions. | ||
* | ||
* https://graypaper.fluffylabs.dev/#/387103d/071401071f01 | ||
* | ||
*/ | ||
declare const HASH_SIZE = 32; | ||
/** A map which uses hashes as keys. */ | ||
declare class HashDictionary<K extends Bytes<typeof HASH_SIZE>, V> { | ||
declare class HashDictionary<K extends OpaqueHash, V> { | ||
// TODO [ToDr] [crit] We can't use `TrieHash` directly in the map, | ||
@@ -460,0 +477,0 @@ // because of the way it's being compared. Hence having `string` here. |
52
index.js
@@ -35,11 +35,35 @@ 'use strict'; | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other) { | ||
if (this.length !== other.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.buffer[i] !== other.buffer[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** Create a new [`BytesBlob'] by converting given UTF-u encoded string into bytes. */ | ||
static fromString(v) { | ||
const encoder = new TextEncoder(); | ||
return BytesBlob.fromBlob(encoder.encode(v)); | ||
return BytesBlob.from(encoder.encode(v)); | ||
} | ||
/** Create a new [`BytesBlob`] from existing [`Uint8Array`]. */ | ||
static fromBlob(v) { | ||
static from(v) { | ||
return new BytesBlob(v); | ||
} | ||
/** Create a new [`BytesBlob`] by concatenating data from multiple `Uint8Array`s. */ | ||
static copyFromBlobs(v, ...rest) { | ||
const totalLength = v.length + rest.reduce((a, v) => a + v.length, 0); | ||
const buffer = new Uint8Array(totalLength); | ||
buffer.set(v, 0); | ||
let offset = v.length; | ||
for (const r of rest) { | ||
buffer.set(r, offset); | ||
offset += r.length; | ||
} | ||
return new BytesBlob(buffer); | ||
} | ||
/** Create a new [`BytesBlob`] from an array of bytes. */ | ||
@@ -76,24 +100,12 @@ static fromNumbers(v) { | ||
*/ | ||
class Bytes { | ||
class Bytes extends BytesBlob { | ||
constructor(raw, len) { | ||
super(raw); | ||
check(raw.byteLength === len, `Given buffer has incorrect size ${raw.byteLength} vs expected ${len}`); | ||
this.raw = raw; | ||
this.length = len; | ||
} | ||
/** Return hex encoding of the sequence. */ | ||
toString() { | ||
return bytesToHexString(this.raw); | ||
/** Raw bytes array. */ | ||
get raw() { | ||
return this.buffer; | ||
} | ||
/** Compare the sequence to another one. */ | ||
isEqualTo(other) { | ||
if (this.length !== other.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.length; i++) { | ||
if (this.raw[i] !== other.raw[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
/** Create new [`Bytes<X>`] given a backing buffer and it's length. */ | ||
@@ -392,3 +404,3 @@ static fromBlob(v, len) { | ||
const len = this.getValueLength(); | ||
return BytesBlob.fromBlob(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
return BytesBlob.from(this.node.data.subarray(HASH_BYTES, HASH_BYTES + len)); | ||
} | ||
@@ -395,0 +407,0 @@ /** |
{ | ||
"name": "@typeberry/trie", | ||
"version": "0.0.1-b8158c0", | ||
"version": "0.0.1-ba2ea72", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": "Fluffy Labs", |
43472
-26.24%3
-84.21%1228
-24.29%