-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added BlockId type * Updating type definitions for BlockId All instances of Checksum256 that reference a BlockId have been updated to use the new BlockId type. * Test coverage for BlockId * Removed placeholder math * Moving `BlockId` to its own serializable class * Added missing fromABI call * Removed BlockId from builtins
- Loading branch information
Showing
8 changed files
with
139 additions
and
37 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
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,78 @@ | ||
import {Bytes, BytesType, Checksum256, Checksum256Type, isInstanceOf, UInt32, UInt32Type} from '..' | ||
import {ABIDecoder, ABIEncoder, ABISerializableObject} from '../serializer' | ||
import {arrayEquals, arrayToHex} from '../utils' | ||
|
||
export type BlockIdType = BlockId | BytesType | {blockNum: UInt32Type; checksum: Checksum256Type} | ||
|
||
export class BlockId implements ABISerializableObject { | ||
static abiName = 'block_id_type' // eosio contract context defines this with a _type suffix for some reason | ||
|
||
static from(value: BlockIdType) { | ||
if (isInstanceOf(value, this)) { | ||
return value | ||
} | ||
if (Bytes.isBytes(value)) { | ||
return new this(Bytes.from(value).array) | ||
} else { | ||
return this.fromBlockChecksum(value.checksum, value.blockNum) | ||
} | ||
} | ||
|
||
static fromABI(decoder: ABIDecoder) { | ||
return new this(decoder.readArray(32)) | ||
} | ||
|
||
static fromBlockChecksum(checksum: Checksum256Type, blockNum: UInt32Type): BlockId { | ||
const id = new BlockId(Checksum256.from(checksum).array) | ||
const numBuffer = new Uint8Array(4) | ||
numBuffer[0] = (Number(blockNum) >> 24) & 0xff | ||
numBuffer[1] = (Number(blockNum) >> 16) & 0xff | ||
numBuffer[2] = (Number(blockNum) >> 8) & 0xff | ||
numBuffer[3] = Number(blockNum) & 0xff | ||
id.array.set(numBuffer, 0) | ||
return id | ||
} | ||
|
||
readonly array: Uint8Array | ||
|
||
constructor(array: Uint8Array) { | ||
if (array.byteLength !== 32) { | ||
throw new Error(`BlockId size mismatch, expected 32 bytes got ${array.byteLength}`) | ||
} | ||
this.array = array | ||
} | ||
|
||
equals(other: BlockIdType): boolean { | ||
const self = this.constructor as typeof BlockId | ||
try { | ||
return arrayEquals(this.array, self.from(other).array) | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
toABI(encoder: ABIEncoder) { | ||
encoder.writeArray(this.array) | ||
} | ||
|
||
toString() { | ||
return this.hexString | ||
} | ||
|
||
toJSON() { | ||
return this.toString() | ||
} | ||
|
||
get hexString(): string { | ||
return arrayToHex(this.array) | ||
} | ||
|
||
get blockNum(): UInt32 { | ||
const bytes = this.array.slice(0, 4) | ||
let num = 0 | ||
for (let i = 0; i < 4; i++) { | ||
num = (num << 8) + bytes[i] | ||
} | ||
return UInt32.from(num) | ||
} | ||
} |
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
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
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