From c32fe8c596af061314883cab6e05e533bbc0c748 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:11:33 +0100 Subject: [PATCH] Nodejs: rename block types (#1706) * nodejs rename block types * Call inner methods * Update bindings/nodejs/lib/types/block/core/block-body.ts * Add BlockHeader --------- Co-authored-by: Thibault Martinez Co-authored-by: Thibault Martinez --- .../nodejs/examples/client/06-simple-block.ts | 6 +- .../nodejs/examples/client/08-data-block.ts | 4 +- bindings/nodejs/examples/client/10-mqtt.ts | 4 +- bindings/nodejs/lib/client/client.ts | 32 +-- .../lib/secret_manager/secret-manager.ts | 10 +- bindings/nodejs/lib/types/block/core/basic.ts | 6 +- .../nodejs/lib/types/block/core/block-body.ts | 68 ++++++ bindings/nodejs/lib/types/block/core/block.ts | 217 ++++++++++++++--- bindings/nodejs/lib/types/block/core/index.ts | 16 +- .../lib/types/block/core/signed-block.ts | 222 ------------------ .../nodejs/lib/types/block/core/validation.ts | 8 +- .../nodejs/lib/types/client/bridge/client.ts | 6 +- .../nodejs/lib/types/utils/bridge/utils.ts | 4 +- bindings/nodejs/lib/utils/utils.ts | 4 +- bindings/nodejs/tests/client/examples.spec.ts | 10 +- .../tests/client/messageMethods.spec.ts | 4 +- 16 files changed, 309 insertions(+), 312 deletions(-) create mode 100644 bindings/nodejs/lib/types/block/core/block-body.ts delete mode 100644 bindings/nodejs/lib/types/block/core/signed-block.ts diff --git a/bindings/nodejs/examples/client/06-simple-block.ts b/bindings/nodejs/examples/client/06-simple-block.ts index 8201d22862..a4f0d7ef03 100644 --- a/bindings/nodejs/examples/client/06-simple-block.ts +++ b/bindings/nodejs/examples/client/06-simple-block.ts @@ -55,9 +55,9 @@ async function run() { issuerId, new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')), ); - const signedBlock = await secretManager.signBlock(unsignedBlock, chain); - const blockId = await client.postBlock(signedBlock); - console.log('Block:', signedBlock, '\n'); + const block = await secretManager.signBlock(unsignedBlock, chain); + const blockId = await client.postBlock(block); + console.log('Block:', block, '\n'); console.log( `Empty block sent: ${process.env.EXPLORER_URL}/block/${blockId}`, diff --git a/bindings/nodejs/examples/client/08-data-block.ts b/bindings/nodejs/examples/client/08-data-block.ts index be84efc5a9..1bf9bbae4b 100644 --- a/bindings/nodejs/examples/client/08-data-block.ts +++ b/bindings/nodejs/examples/client/08-data-block.ts @@ -55,8 +55,8 @@ async function run() { issuerId, new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')), ); - const signedBlock = await secretManager.signBlock(unsignedBlock, chain); - const blockId = await client.postBlock(signedBlock); + const block = await secretManager.signBlock(unsignedBlock, chain); + const blockId = await client.postBlock(block); console.log(`Block sent: ${process.env.EXPLORER_URL}/block/${blockId}`); diff --git a/bindings/nodejs/examples/client/10-mqtt.ts b/bindings/nodejs/examples/client/10-mqtt.ts index 54c31df9a6..1503d9101e 100644 --- a/bindings/nodejs/examples/client/10-mqtt.ts +++ b/bindings/nodejs/examples/client/10-mqtt.ts @@ -1,7 +1,7 @@ // Copyright 2021-2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { Client, initLogger, parseSignedBlock } from '@iota/sdk'; +import { Client, initLogger, parseBlock } from '@iota/sdk'; require('dotenv').config({ path: '.env' }); @@ -35,7 +35,7 @@ async function run() { const parsed = JSON.parse(data); if (parsed.topic == 'blocks') { - const block = parseSignedBlock(JSON.parse(parsed.payload)); + const block = parseBlock(JSON.parse(parsed.payload)); if (block.isBasic()) { const basic = block.asBasic(); diff --git a/bindings/nodejs/lib/client/client.ts b/bindings/nodejs/lib/client/client.ts index fdd64f7f1d..fd3e60b511 100644 --- a/bindings/nodejs/lib/client/client.ts +++ b/bindings/nodejs/lib/client/client.ts @@ -35,8 +35,8 @@ import { UnlockCondition, Payload, SignedTransactionPayload, - parseSignedBlock, - SignedBlock, + parseBlock, + Block, AccountId, AnchorId, NftId, @@ -149,7 +149,7 @@ export class Client { * @param block The block to post. * @returns The block ID once the block has been posted. */ - async postBlock(block: SignedBlock): Promise { + async postBlock(block: Block): Promise { const response = await this.methodHandler.callMethod({ name: 'postBlock', data: { @@ -166,7 +166,7 @@ export class Client { * @param blockId The corresponding block ID of the requested block. * @returns The requested block. */ - async getBlock(blockId: BlockId): Promise { + async getBlock(blockId: BlockId): Promise { const response = await this.methodHandler.callMethod({ name: 'getBlock', data: { @@ -174,8 +174,8 @@ export class Client { }, }); - const parsed = JSON.parse(response) as Response; - return parseSignedBlock(parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** @@ -404,7 +404,7 @@ export class Client { * @param block The block. * @returns The ID of the posted block. */ - async postBlockRaw(block: SignedBlock): Promise { + async postBlockRaw(block: Block): Promise { const response = await this.methodHandler.callMethod({ name: 'postBlockRaw', data: { @@ -438,15 +438,15 @@ export class Client { * @param transactionId The ID of the transaction. * @returns The included block that contained the transaction. */ - async getIncludedBlock(transactionId: TransactionId): Promise { + async getIncludedBlock(transactionId: TransactionId): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlock', data: { transactionId, }, }); - const parsed = JSON.parse(response) as Response; - return parseSignedBlock(parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** @@ -457,15 +457,15 @@ export class Client { */ async getIncludedBlockMetadata( transactionId: TransactionId, - ): Promise { + ): Promise { const response = await this.methodHandler.callMethod({ name: 'getIncludedBlockMetadata', data: { transactionId, }, }); - const parsed = JSON.parse(response) as Response; - return parseSignedBlock(parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** @@ -582,15 +582,15 @@ export class Client { * @param blockIds An array of `BlockId`s. * @returns An array of corresponding blocks. */ - async findBlocks(blockIds: BlockId[]): Promise { + async findBlocks(blockIds: BlockId[]): Promise { const response = await this.methodHandler.callMethod({ name: 'findBlocks', data: { blockIds, }, }); - const parsed = JSON.parse(response) as Response; - return parsed.payload.map((p) => parseSignedBlock(p)); + const parsed = JSON.parse(response) as Response; + return parsed.payload.map((p) => parseBlock(p)); } /** diff --git a/bindings/nodejs/lib/secret_manager/secret-manager.ts b/bindings/nodejs/lib/secret_manager/secret-manager.ts index 7ccaeecd91..5afccb4d6f 100644 --- a/bindings/nodejs/lib/secret_manager/secret-manager.ts +++ b/bindings/nodejs/lib/secret_manager/secret-manager.ts @@ -19,8 +19,8 @@ import { Unlock, Response, UnsignedBlock, - SignedBlock, - parseSignedBlock, + Block, + parseBlock, } from '../types'; import { plainToInstance } from 'class-transformer'; @@ -122,7 +122,7 @@ export class SecretManager { async signBlock( unsignedBlock: UnsignedBlock, chain: Bip44, - ): Promise { + ): Promise { const response = await this.methodHandler.callMethod({ name: 'signBlock', data: { @@ -131,8 +131,8 @@ export class SecretManager { }, }); - const parsed = JSON.parse(response) as Response; - return parseSignedBlock(parsed.payload); + const parsed = JSON.parse(response) as Response; + return parseBlock(parsed.payload); } /** diff --git a/bindings/nodejs/lib/types/block/core/basic.ts b/bindings/nodejs/lib/types/block/core/basic.ts index 3019e4dfd7..8d63466e15 100644 --- a/bindings/nodejs/lib/types/block/core/basic.ts +++ b/bindings/nodejs/lib/types/block/core/basic.ts @@ -4,13 +4,13 @@ import { Payload, PayloadDiscriminator } from '../payload'; import { Type } from 'class-transformer'; import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; -import { Block } from './block'; +import { BlockBody } from './block-body'; import { u64 } from '../../utils'; /** - * Basic Block layout. + * Basic Block Body layout. */ -export class BasicBlock extends Block { +export class BasicBlockBody extends BlockBody { /** * Blocks that are strongly directly approved. */ diff --git a/bindings/nodejs/lib/types/block/core/block-body.ts b/bindings/nodejs/lib/types/block/core/block-body.ts new file mode 100644 index 0000000000..f45b00559f --- /dev/null +++ b/bindings/nodejs/lib/types/block/core/block-body.ts @@ -0,0 +1,68 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import { BasicBlockBody } from './basic'; +import { ValidationBlockBody } from './validation'; + +/** + * All of the block body types. + */ +export enum BlockBodyType { + /// A Basic block body. + Basic = 0, + /// A Validation block body. + Validation = 1, +} + +export abstract class BlockBody { + readonly type: BlockBodyType; + + /** + * @param type The type of BlockBody. + */ + constructor(type: BlockBodyType) { + this.type = type; + } + + /** + * Checks whether the block body is a `BasicBlockBody`. + * @returns true if it is, otherwise false + */ + isBasic(): boolean { + return this.type === BlockBodyType.Basic; + } + + /** + * Gets the block body as an actual `BasicBlockBody`. + * NOTE: Will throw an error if the block is not a `BasicBlockBody`. + * @returns The BasicBlockBody + */ + asBasic(): BasicBlockBody { + if (this.isBasic()) { + return this as unknown as BasicBlockBody; + } else { + throw new Error('invalid downcast of non-BasicBlockBody'); + } + } + + /** + * Checks whether the block body is a `ValidationBlockBody`. + * @returns true if it is, otherwise false + */ + isValidation(): boolean { + return this.type === BlockBodyType.Validation; + } + + /** + * Gets the block body as an actual `ValidationBlockBody`. + * NOTE: Will throw an error if the block is not a `ValidationBlockBody`. + * @returns The ValidationBlockBody + */ + asValidation(): ValidationBlockBody { + if (this.isValidation()) { + return this as unknown as ValidationBlockBody; + } else { + throw new Error('invalid downcast of non-ValidationBlockBody'); + } + } +} diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index 42ad18b781..529afa73ca 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -1,67 +1,218 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { BasicBlock } from './basic'; -import { ValidationBlock } from './validation'; +import { AccountId } from '../id'; +import { Signature, SignatureDiscriminator } from '../signature'; +import { SlotCommitmentId, SlotIndex } from '../slot'; +import { u64 } from '../../utils/type-aliases'; +import { plainToInstance, Type } from 'class-transformer'; +import { BlockBody } from './block-body'; +import { BasicBlockBody } from './basic'; +import { ValidationBlockBody } from './validation'; +import { BlockBodyDiscriminator } from '.'; + /** - * All of the block types. + * The block header. */ -export enum BlockType { - /// A Basic block. - Basic = 0, - /// A Validation block. - Validation = 1, +class BlockHeader { + /** + * Protocol version of the block. + */ + readonly protocolVersion!: number; + /** + * Network identifier. + */ + readonly networkId!: u64; + /** + * The time at which the block was issued. It is a Unix timestamp in nanoseconds. + */ + readonly issuingTime!: u64; + /** + * The identifier of the slot to which this block commits. + */ + readonly slotCommitmentId!: SlotCommitmentId; + /** + * The slot index of the latest finalized slot. + */ + readonly latestFinalizedSlot!: SlotIndex; + /** + * The identifier of the account that issued this block. + */ + readonly issuerId!: AccountId; + + constructor( + protocolVersion: number, + networkId: u64, + issuingTime: u64, + slotCommitmentId: SlotCommitmentId, + latestFinalizedSlot: SlotIndex, + issuerId: AccountId, + ) { + this.protocolVersion = protocolVersion; + this.networkId = networkId; + this.issuingTime = issuingTime; + this.slotCommitmentId = slotCommitmentId; + this.latestFinalizedSlot = latestFinalizedSlot; + this.issuerId = issuerId; + } } -export abstract class Block { - readonly type: BlockType; +/** + * Represent the object that nodes gossip around the network. + */ +class Block { + /** + * The block header. + */ + readonly header!: BlockHeader; + + @Type(() => BlockBody, { + discriminator: BlockBodyDiscriminator, + }) + readonly body!: BlockBody; /** - * @param type The type of Block. + * The block signature; used to validate issuance capabilities. */ - constructor(type: BlockType) { - this.type = type; + @Type(() => Signature, { + discriminator: SignatureDiscriminator, + }) + readonly signature!: Signature; + + constructor(header: BlockHeader, body: BlockBody, signature: Signature) { + this.header = header; + this.body = body; + this.signature = signature; } /** - * Checks whether the block is a `BasicBlock`. + * Checks whether the block body is a `BasicBlockBody`. * @returns true if it is, otherwise false */ isBasic(): boolean { - return this.type === BlockType.Basic; + return this.body.isBasic(); } /** - * Gets the block as an actual `BasicBlock`. - * NOTE: Will throw an error if the block is not a `BasicBlock`. + * Gets the block body as an actual `BasicBlockBody`. + * NOTE: Will throw an error if the block body is not a `BasicBlockBody`. * @returns The block */ - asBasic(): BasicBlock { - if (this.isBasic()) { - return this as unknown as BasicBlock; - } else { - throw new Error('invalid downcast of non-BasicBlock'); - } + asBasic(): BasicBlockBody { + return this.body.asBasic(); } /** - * Checks whether the block is a `ValidationBlock`. + * Checks whether the block body is a `ValidationBlockBody`. * @returns true if it is, otherwise false */ isValidation(): boolean { - return this.type === BlockType.Validation; + return this.body.isValidation(); } /** - * Gets the block as an actual `ValidationBlock`. - * NOTE: Will throw an error if the block is not a `ValidationBlock`. + * Gets the block body as an actual `ValidationBlockBody`. + * NOTE: Will throw an error if the block body is not a `ValidationBlockBody`. * @returns The block */ - asValidation(): ValidationBlock { - if (this.isValidation()) { - return this as unknown as ValidationBlock; - } else { - throw new Error('invalid downcast of non-ValidationBlock'); - } + asValidation(): ValidationBlockBody { + return this.body.asValidation(); } } + +function parseBlock(data: any): Block { + return plainToInstance(Block, data) as any as Block; +} + +/** + * Represent the object that nodes gossip around the network. + */ +class UnsignedBlock { + /** + * Protocol version of the block. + */ + readonly protocolVersion!: number; + /** + * Network identifier. + */ + readonly networkId!: u64; + /** + * The time at which the block was issued. It is a Unix timestamp in nanoseconds. + */ + readonly issuingTime!: u64; + /** + * The identifier of the slot to which this block commits. + */ + readonly slotCommitmentId!: SlotCommitmentId; + /** + * The slot index of the latest finalized slot. + */ + readonly latestFinalizedSlot!: SlotIndex; + /** + * The identifier of the account that issued this block. + */ + readonly issuerId!: AccountId; + + @Type(() => BlockBody, { + discriminator: BlockBodyDiscriminator, + }) + readonly body!: BlockBody; + + constructor( + protocolVersion: number, + networkId: u64, + issuingTime: u64, + slotCommitmentId: SlotCommitmentId, + latestFinalizedSlot: SlotIndex, + issuerId: AccountId, + body: BlockBody, + ) { + this.protocolVersion = protocolVersion; + this.networkId = networkId; + this.issuingTime = issuingTime; + this.slotCommitmentId = slotCommitmentId; + this.latestFinalizedSlot = latestFinalizedSlot; + this.issuerId = issuerId; + this.body = body; + } + + /** + * Checks whether the block body is a `BasicBlockBody`. + * @returns true if it is, otherwise false + */ + isBasic(): boolean { + return this.body.isBasic(); + } + + /** + * Gets the block body as an actual `BasicBlock`. + * NOTE: Will throw an error if the block body is not a `BasicBlockBody`. + * @returns The BasicBlockBody + */ + asBasic(): BasicBlockBody { + return this.body.asBasic(); + } + + /** + * Checks whether the block body is a `ValidationBlockBody`. + * @returns true if it is, otherwise false + */ + isValidation(): boolean { + return this.body.isValidation(); + } + + /** + * Gets the block body as an actual `ValidationBlockBody`. + * NOTE: Will throw an error if the block body is not a `ValidationBlockBody`. + * @returns The ValidationBlockBody + */ + asValidation(): ValidationBlockBody { + return this.body.asValidation(); + } +} + +function parseUnsignedBlock(data: any): UnsignedBlock { + return plainToInstance(UnsignedBlock, data) as any as UnsignedBlock; +} + +export { Block, BlockHeader, parseBlock, UnsignedBlock, parseUnsignedBlock }; diff --git a/bindings/nodejs/lib/types/block/core/index.ts b/bindings/nodejs/lib/types/block/core/index.ts index 14d027ecfe..bcfe8c456f 100644 --- a/bindings/nodejs/lib/types/block/core/index.ts +++ b/bindings/nodejs/lib/types/block/core/index.ts @@ -1,20 +1,20 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { BlockType } from './block'; -import { BasicBlock } from './basic'; -import { ValidationBlock } from './validation'; +import { BlockBodyType } from './block-body'; +import { BasicBlockBody } from './basic'; +import { ValidationBlockBody } from './validation'; export * from './block'; +export * from './block-body'; export * from './basic'; export * from './validation'; -export * from './signed-block'; -// Here because in block.ts it causes a circular dependency -export const BlockDiscriminator = { +// Here because in block-body.ts it causes a circular dependency +export const BlockBodyDiscriminator = { property: 'type', subTypes: [ - { value: BasicBlock, name: BlockType.Basic as any }, - { value: ValidationBlock, name: BlockType.Validation as any }, + { value: BasicBlockBody, name: BlockBodyType.Basic as any }, + { value: ValidationBlockBody, name: BlockBodyType.Validation as any }, ], }; diff --git a/bindings/nodejs/lib/types/block/core/signed-block.ts b/bindings/nodejs/lib/types/block/core/signed-block.ts deleted file mode 100644 index 0649a03c0e..0000000000 --- a/bindings/nodejs/lib/types/block/core/signed-block.ts +++ /dev/null @@ -1,222 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -import { AccountId } from '../id'; -import { Signature, SignatureDiscriminator } from '../signature'; -import { SlotCommitmentId, SlotIndex } from '../slot'; -import { u64 } from '../../utils/type-aliases'; -import { plainToInstance, Type } from 'class-transformer'; -import { Block, BlockType } from './block'; -import { BasicBlock } from './basic'; -import { ValidationBlock } from './validation'; -import { BlockDiscriminator } from '.'; - -/** - * Represent the object that nodes gossip around the network. - */ -class SignedBlock { - /** - * Protocol version of the block. - */ - readonly protocolVersion!: number; - /** - * Network identifier. - */ - readonly networkId!: u64; - /** - * The time at which the block was issued. It is a Unix timestamp in nanoseconds. - */ - readonly issuingTime!: u64; - /** - * The identifier of the slot to which this block commits. - */ - readonly slotCommitmentId!: SlotCommitmentId; - /** - * The slot index of the latest finalized slot. - */ - readonly latestFinalizedSlot!: SlotIndex; - /** - * The identifier of the account that issued this block. - */ - readonly issuerId!: AccountId; - - @Type(() => Block, { - discriminator: BlockDiscriminator, - }) - readonly block!: Block; - - /** - * The block signature; used to validate issuance capabilities. - */ - @Type(() => Signature, { - discriminator: SignatureDiscriminator, - }) - readonly signature!: Signature; - - constructor( - protocolVersion: number, - networkId: u64, - issuingTime: u64, - slotCommitmentId: SlotCommitmentId, - latestFinalizedSlot: SlotIndex, - issuerId: AccountId, - block: Block, - signature: Signature, - ) { - this.protocolVersion = protocolVersion; - this.networkId = networkId; - this.issuingTime = issuingTime; - this.slotCommitmentId = slotCommitmentId; - this.latestFinalizedSlot = latestFinalizedSlot; - this.issuerId = issuerId; - this.block = block; - this.signature = signature; - } - - /** - * Checks whether the block is a `BasicBlock`. - * @returns true if it is, otherwise false - */ - isBasic(): boolean { - return this.block.type === BlockType.Basic; - } - - /** - * Gets the block as an actual `BasicBlock`. - * NOTE: Will throw an error if the block is not a `BasicBlock`. - * @returns The block - */ - asBasic(): BasicBlock { - if (this.isBasic()) { - return this.block as unknown as BasicBlock; - } else { - throw new Error('invalid downcast of non-BasicBlock'); - } - } - - /** - * Checks whether the block is a `ValidationBlock`. - * @returns true if it is, otherwise false - */ - isValidation(): boolean { - return this.block.type === BlockType.Validation; - } - - /** - * Gets the block as an actual `ValidationBlock`. - * NOTE: Will throw an error if the block is not a `ValidationBlock`. - * @returns The block - */ - asValidation(): ValidationBlock { - if (this.isValidation()) { - return this.block as unknown as ValidationBlock; - } else { - throw new Error('invalid downcast of non-ValidationBlock'); - } - } -} - -function parseSignedBlock(data: any): SignedBlock { - return plainToInstance(SignedBlock, data) as any as SignedBlock; -} - -/** - * Represent the object that nodes gossip around the network. - */ -class UnsignedBlock { - /** - * Protocol version of the block. - */ - readonly protocolVersion!: number; - /** - * Network identifier. - */ - readonly networkId!: u64; - /** - * The time at which the block was issued. It is a Unix timestamp in nanoseconds. - */ - readonly issuingTime!: u64; - /** - * The identifier of the slot to which this block commits. - */ - readonly slotCommitmentId!: SlotCommitmentId; - /** - * The slot index of the latest finalized slot. - */ - readonly latestFinalizedSlot!: SlotIndex; - /** - * The identifier of the account that issued this block. - */ - readonly issuerId!: AccountId; - - @Type(() => Block, { - discriminator: BlockDiscriminator, - }) - readonly block!: Block; - - constructor( - protocolVersion: number, - networkId: u64, - issuingTime: u64, - slotCommitmentId: SlotCommitmentId, - latestFinalizedSlot: SlotIndex, - issuerId: AccountId, - block: Block, - ) { - this.protocolVersion = protocolVersion; - this.networkId = networkId; - this.issuingTime = issuingTime; - this.slotCommitmentId = slotCommitmentId; - this.latestFinalizedSlot = latestFinalizedSlot; - this.issuerId = issuerId; - this.block = block; - } - - /** - * Checks whether the block is a `BasicBlock`. - * @returns true if it is, otherwise false - */ - isBasic(): boolean { - return this.block.type === BlockType.Basic; - } - - /** - * Gets the block as an actual `BasicBlock`. - * NOTE: Will throw an error if the block is not a `BasicBlock`. - * @returns The block - */ - asBasic(): BasicBlock { - if (this.isBasic()) { - return this.block as unknown as BasicBlock; - } else { - throw new Error('invalid downcast of non-BasicBlock'); - } - } - - /** - * Checks whether the block is a `ValidationBlock`. - * @returns true if it is, otherwise false - */ - isValidation(): boolean { - return this.block.type === BlockType.Validation; - } - - /** - * Gets the block as an actual `ValidationBlock`. - * NOTE: Will throw an error if the block is not a `ValidationBlock`. - * @returns The block - */ - asValidation(): ValidationBlock { - if (this.isValidation()) { - return this.block as unknown as ValidationBlock; - } else { - throw new Error('invalid downcast of non-ValidationBlock'); - } - } -} - -function parseUnsignedBlock(data: any): UnsignedBlock { - return plainToInstance(UnsignedBlock, data) as any as UnsignedBlock; -} - -export { SignedBlock, parseSignedBlock, UnsignedBlock, parseUnsignedBlock }; diff --git a/bindings/nodejs/lib/types/block/core/validation.ts b/bindings/nodejs/lib/types/block/core/validation.ts index 58fc769f0c..4dc45d5ab8 100644 --- a/bindings/nodejs/lib/types/block/core/validation.ts +++ b/bindings/nodejs/lib/types/block/core/validation.ts @@ -3,16 +3,16 @@ import { StrongParents, WeakParents, ShallowLikeParents } from '../parents'; import { HexEncodedString } from '../../utils'; -import { Block } from './block'; +import { BlockBody } from './block-body'; /** - * A Validation Block is a special type of block used by validators to secure the network. + * A Validation Block Body is a special type of block body used by validators to secure the network. * It is recognised by the Congestion Control of the IOTA 2.0 protocol and can be issued without * burning Mana within the constraints of the allowed validator throughput. * - * It is allowed to reference more parent blocks than a normal Basic Block. + * It is allowed to reference more parent blocks than a normal Basic Block Body. */ -export class ValidationBlock extends Block { +export class ValidationBlockBody extends BlockBody { /** * Blocks that are strongly directly approved. */ diff --git a/bindings/nodejs/lib/types/client/bridge/client.ts b/bindings/nodejs/lib/types/client/bridge/client.ts index 3c36f250b9..27e1f188e1 100644 --- a/bindings/nodejs/lib/types/client/bridge/client.ts +++ b/bindings/nodejs/lib/types/client/bridge/client.ts @@ -7,7 +7,7 @@ import type { } from '../../secret_manager/secret-manager'; import type { AccountId, - SignedBlock, + Block, BlockId, FoundryId, AnchorId, @@ -56,7 +56,7 @@ export interface __GetOutputsMethod__ { export interface __PostBlockMethod__ { name: 'postBlock'; data: { - block: SignedBlock; + block: Block; }; } @@ -160,7 +160,7 @@ export interface __GetPeersMethod__ { export interface __PostBlockRawMethod__ { name: 'postBlockRaw'; data: { - block: SignedBlock; + block: Block; }; } diff --git a/bindings/nodejs/lib/types/utils/bridge/utils.ts b/bindings/nodejs/lib/types/utils/bridge/utils.ts index e354021982..1309fe14c5 100644 --- a/bindings/nodejs/lib/types/utils/bridge/utils.ts +++ b/bindings/nodejs/lib/types/utils/bridge/utils.ts @@ -7,7 +7,7 @@ import { TokenSchemeType, Output, StorageScoreParameters, - SignedBlock, + Block, ProtocolParameters, OutputId, NftId, @@ -86,7 +86,7 @@ export interface __ParseBech32AddressMethod__ { export interface __BlockIdMethod__ { name: 'blockId'; data: { - block: SignedBlock; + block: Block; params: ProtocolParameters; }; } diff --git a/bindings/nodejs/lib/utils/utils.ts b/bindings/nodejs/lib/utils/utils.ts index edeb5e8a05..4b74781881 100644 --- a/bindings/nodejs/lib/utils/utils.ts +++ b/bindings/nodejs/lib/utils/utils.ts @@ -14,7 +14,7 @@ import { StorageScoreParameters, OutputId, u64, - SignedBlock, + Block, ProtocolParameters, Bech32Address, InputSigningData, @@ -190,7 +190,7 @@ export class Utils { * @param params The network protocol parameters. * @returns The corresponding block ID. */ - static blockId(block: SignedBlock, params: ProtocolParameters): BlockId { + static blockId(block: Block, params: ProtocolParameters): BlockId { return callUtilsMethod({ name: 'blockId', data: { diff --git a/bindings/nodejs/tests/client/examples.spec.ts b/bindings/nodejs/tests/client/examples.spec.ts index a94aff8c28..7cbbf5bdf6 100644 --- a/bindings/nodejs/tests/client/examples.spec.ts +++ b/bindings/nodejs/tests/client/examples.spec.ts @@ -143,9 +143,9 @@ describe.skip('Main examples', () => { .getNativeTokens() ?.forEach( (token) => - (totalNativeTokens[token.id] = - (totalNativeTokens[token.id] || 0) + - Number(token.amount)), + (totalNativeTokens[token.id] = + (totalNativeTokens[token.id] || 0) + + Number(token.amount)), ); } @@ -184,8 +184,8 @@ describe.skip('Main examples', () => { issuerId, new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')), ); - const signedBlock = await secretManager.signBlock(unsignedBlock, chain); - const blockId = await client.postBlock(signedBlock); + const block = await secretManager.signBlock(unsignedBlock, chain); + const blockId = await client.postBlock(block); const fetchedBlock = await client.getBlock(blockId); diff --git a/bindings/nodejs/tests/client/messageMethods.spec.ts b/bindings/nodejs/tests/client/messageMethods.spec.ts index e1c1cf379a..e938689042 100644 --- a/bindings/nodejs/tests/client/messageMethods.spec.ts +++ b/bindings/nodejs/tests/client/messageMethods.spec.ts @@ -44,9 +44,9 @@ describe.skip('Block methods', () => { issuerId, new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')), ); - const signedBlock = await secretManager.signBlock(unsignedBlock, chain); + const block = await secretManager.signBlock(unsignedBlock, chain); - const blockId = await client.postBlockRaw(signedBlock); + const blockId = await client.postBlockRaw(block); expect(blockId).toBeValidBlockId(); });