From 6f1bf8b55fe6cf661b40feffb00662878ad5ad10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Tue, 28 Nov 2023 12:02:07 +0100 Subject: [PATCH 1/4] nodejs rename block types --- .../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 | 67 ++++++ bindings/nodejs/lib/types/block/core/block.ts | 211 ++++++++++++++--- 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, 307 insertions(+), 307 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 4721ae10a3..dbf6bf44fd 100644 --- a/bindings/nodejs/examples/client/06-simple-block.ts +++ b/bindings/nodejs/examples/client/06-simple-block.ts @@ -53,9 +53,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 d2ee34a409..a7d46d093a 100644 --- a/bindings/nodejs/examples/client/08-data-block.ts +++ b/bindings/nodejs/examples/client/08-data-block.ts @@ -53,8 +53,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 72a0da348b..8478cb163f 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' }); @@ -33,7 +33,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 dcbc06e1a5..bbac14b6fa 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, @@ -148,7 +148,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: { @@ -165,7 +165,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: { @@ -173,8 +173,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); } /** @@ -386,7 +386,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: { @@ -420,15 +420,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); } /** @@ -439,15 +439,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); } /** @@ -564,15 +564,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..36d8429178 --- /dev/null +++ b/bindings/nodejs/lib/types/block/core/block-body.ts @@ -0,0 +1,67 @@ +// 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..6ee7bd4a0c 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -1,67 +1,222 @@ // 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, BlockBodyType } from './block-body'; +import { BasicBlockBody } from './basic'; +import { ValidationBlockBody } from './validation'; +import { BlockBodyDiscriminator } from '.'; + /** - * All of the block types. + * Represent the object that nodes gossip around the network. */ -export enum BlockType { - /// A Basic block. - Basic = 0, - /// A Validation block. - Validation = 1, -} +class Block { + /** + * 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; -export abstract class Block { - readonly type: BlockType; + @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( + protocolVersion: number, + networkId: u64, + issuingTime: u64, + slotCommitmentId: SlotCommitmentId, + latestFinalizedSlot: SlotIndex, + issuerId: AccountId, + body: BlockBody, + signature: Signature, + ) { + this.protocolVersion = protocolVersion; + this.networkId = networkId; + this.issuingTime = issuingTime; + this.slotCommitmentId = slotCommitmentId; + this.latestFinalizedSlot = latestFinalizedSlot; + this.issuerId = issuerId; + 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.type === BlockBodyType.Basic; } /** - * 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 { + asBasic(): BasicBlockBody { if (this.isBasic()) { - return this as unknown as BasicBlock; + return this.body as unknown as BasicBlockBody; } else { - throw new Error('invalid downcast of non-BasicBlock'); + throw new Error('invalid downcast of non-BasicBlockBody'); } } /** - * 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.type === BlockBodyType.Validation; } /** - * 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 { + asValidation(): ValidationBlockBody { if (this.isValidation()) { - return this as unknown as ValidationBlock; + return this.body as unknown as ValidationBlockBody; } else { - throw new Error('invalid downcast of non-ValidationBlock'); + throw new Error('invalid downcast of non-ValidationBlockBody'); } } } + +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.type === BlockBodyType.Basic; + } + + /** + * 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 { + if (this.isBasic()) { + return this.body 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.body.type === BlockBodyType.Validation; + } + + /** + * 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 { + if (this.isValidation()) { + return this.body as unknown as ValidationBlockBody; + } else { + throw new Error('invalid downcast of non-ValidationBlockBody'); + } + } +} + +function parseUnsignedBlock(data: any): UnsignedBlock { + return plainToInstance(UnsignedBlock, data) as any as UnsignedBlock; +} + +export { Block, 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 6db9d5ac29..1cc44a5514 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; }; } @@ -153,7 +153,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 ea1ccd0a43..375dab3f22 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, @@ -85,7 +85,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 bcd94c41b3..417d7aa45b 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, } from '../types'; @@ -189,7 +189,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(); }); From 4428c3cf883200bbf5e5001eb43fc554dd9dbf75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Tue, 28 Nov 2023 14:48:53 +0100 Subject: [PATCH 2/4] Call inner methods --- bindings/nodejs/lib/types/block/core/block.ts | 34 +++++-------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index 6ee7bd4a0c..89b21801c3 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -6,7 +6,7 @@ import { Signature, SignatureDiscriminator } from '../signature'; import { SlotCommitmentId, SlotIndex } from '../slot'; import { u64 } from '../../utils/type-aliases'; import { plainToInstance, Type } from 'class-transformer'; -import { BlockBody, BlockBodyType } from './block-body'; +import { BlockBody } from './block-body'; import { BasicBlockBody } from './basic'; import { ValidationBlockBody } from './validation'; import { BlockBodyDiscriminator } from '.'; @@ -78,7 +78,7 @@ class Block { * @returns true if it is, otherwise false */ isBasic(): boolean { - return this.body.type === BlockBodyType.Basic; + return this.body.isBasic(); } /** @@ -87,11 +87,7 @@ class Block { * @returns The block */ asBasic(): BasicBlockBody { - if (this.isBasic()) { - return this.body as unknown as BasicBlockBody; - } else { - throw new Error('invalid downcast of non-BasicBlockBody'); - } + return this.body.asBasic(); } /** @@ -99,7 +95,7 @@ class Block { * @returns true if it is, otherwise false */ isValidation(): boolean { - return this.body.type === BlockBodyType.Validation; + return this.body.isValidation(); } /** @@ -108,11 +104,7 @@ class Block { * @returns The block */ asValidation(): ValidationBlockBody { - if (this.isValidation()) { - return this.body as unknown as ValidationBlockBody; - } else { - throw new Error('invalid downcast of non-ValidationBlockBody'); - } + return this.body.asValidation(); } } @@ -177,7 +169,7 @@ class UnsignedBlock { * @returns true if it is, otherwise false */ isBasic(): boolean { - return this.body.type === BlockBodyType.Basic; + return this.body.isBasic(); } /** @@ -186,11 +178,7 @@ class UnsignedBlock { * @returns The BasicBlockBody */ asBasic(): BasicBlockBody { - if (this.isBasic()) { - return this.body as unknown as BasicBlockBody; - } else { - throw new Error('invalid downcast of non-BasicBlockBody'); - } + return this.body.asBasic(); } /** @@ -198,7 +186,7 @@ class UnsignedBlock { * @returns true if it is, otherwise false */ isValidation(): boolean { - return this.body.type === BlockBodyType.Validation; + return this.body.isValidation(); } /** @@ -207,11 +195,7 @@ class UnsignedBlock { * @returns The ValidationBlockBody */ asValidation(): ValidationBlockBody { - if (this.isValidation()) { - return this.body as unknown as ValidationBlockBody; - } else { - throw new Error('invalid downcast of non-ValidationBlockBody'); - } + return this.body.asValidation(); } } From e658fe5d542f6643523c04c3f2874ae9ea592885 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 29 Nov 2023 10:14:26 +0100 Subject: [PATCH 3/4] Update bindings/nodejs/lib/types/block/core/block-body.ts --- bindings/nodejs/lib/types/block/core/block-body.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/bindings/nodejs/lib/types/block/core/block-body.ts b/bindings/nodejs/lib/types/block/core/block-body.ts index 36d8429178..f45b00559f 100644 --- a/bindings/nodejs/lib/types/block/core/block-body.ts +++ b/bindings/nodejs/lib/types/block/core/block-body.ts @@ -3,6 +3,7 @@ import { BasicBlockBody } from './basic'; import { ValidationBlockBody } from './validation'; + /** * All of the block body types. */ From abd4caa69e508ecf1655f96eb3f8ac5e8aefb0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Wed, 29 Nov 2023 10:48:49 +0100 Subject: [PATCH 4/4] Add BlockHeader --- bindings/nodejs/lib/types/block/core/block.ts | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/bindings/nodejs/lib/types/block/core/block.ts b/bindings/nodejs/lib/types/block/core/block.ts index 89b21801c3..529afa73ca 100644 --- a/bindings/nodejs/lib/types/block/core/block.ts +++ b/bindings/nodejs/lib/types/block/core/block.ts @@ -12,9 +12,9 @@ import { ValidationBlockBody } from './validation'; import { BlockBodyDiscriminator } from '.'; /** - * Represent the object that nodes gossip around the network. + * The block header. */ -class Block { +class BlockHeader { /** * Protocol version of the block. */ @@ -40,19 +40,6 @@ class Block { */ readonly issuerId!: AccountId; - @Type(() => BlockBody, { - discriminator: BlockBodyDiscriminator, - }) - readonly body!: BlockBody; - - /** - * The block signature; used to validate issuance capabilities. - */ - @Type(() => Signature, { - discriminator: SignatureDiscriminator, - }) - readonly signature!: Signature; - constructor( protocolVersion: number, networkId: u64, @@ -60,8 +47,6 @@ class Block { slotCommitmentId: SlotCommitmentId, latestFinalizedSlot: SlotIndex, issuerId: AccountId, - body: BlockBody, - signature: Signature, ) { this.protocolVersion = protocolVersion; this.networkId = networkId; @@ -69,6 +54,33 @@ class Block { this.slotCommitmentId = slotCommitmentId; this.latestFinalizedSlot = latestFinalizedSlot; this.issuerId = issuerId; + } +} + +/** + * Represent the object that nodes gossip around the network. + */ +class Block { + /** + * The block header. + */ + readonly header!: BlockHeader; + + @Type(() => BlockBody, { + discriminator: BlockBodyDiscriminator, + }) + readonly body!: BlockBody; + + /** + * The block signature; used to validate issuance capabilities. + */ + @Type(() => Signature, { + discriminator: SignatureDiscriminator, + }) + readonly signature!: Signature; + + constructor(header: BlockHeader, body: BlockBody, signature: Signature) { + this.header = header; this.body = body; this.signature = signature; } @@ -203,4 +215,4 @@ function parseUnsignedBlock(data: any): UnsignedBlock { return plainToInstance(UnsignedBlock, data) as any as UnsignedBlock; } -export { Block, parseBlock, UnsignedBlock, parseUnsignedBlock }; +export { Block, BlockHeader, parseBlock, UnsignedBlock, parseUnsignedBlock };