Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Node: Validation Block #1006

Merged
merged 38 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bcb076c
added parents
Aug 10, 2023
80d5a7f
format
Aug 10, 2023
e8c60c1
wrapper
Aug 10, 2023
52c04df
bigint rename
Aug 10, 2023
9ac9cf2
wrapper imports
Aug 10, 2023
c4ac946
fmt
Aug 10, 2023
09ad3a9
Merge branch '2.0' into node/block-wrapper
Aug 10, 2023
c366479
renamed
Aug 10, 2023
23675c1
examples
Aug 10, 2023
ae478a7
removed boxedslice
Aug 10, 2023
01e9a17
ids and getters
Aug 10, 2023
fc2561f
readonly
Aug 10, 2023
9fe511e
removed unused deps
Aug 10, 2023
0789064
fmt...
Aug 10, 2023
f682ede
readonly, removed getters
Aug 11, 2023
2853242
renamed to Block
Aug 11, 2023
ba50923
Merge branch '2.0' into node/block-wrapper
Aug 14, 2023
4607911
updated Slotcommit and u64 usage, renamed some files
Aug 14, 2023
a044d82
fixed file name
Aug 14, 2023
78b54e2
Merge branch '2.0' into node/block-wrapper
Thoralf-M Aug 15, 2023
64c7f29
Update bindings/nodejs/lib/types/block/core/block.ts
kwek20 Aug 15, 2023
4ead34a
Merge branch '2.0' into node/block-wrapper
Thoralf-M Aug 16, 2023
14ae343
removed unused blokc tyle
Aug 16, 2023
7e07f2e
Merge branch 'node/block-wrapper' of https://github.com/kwek20/iota-s…
Aug 16, 2023
3723ea5
Merge branch '2.0' into node/block-wrapper
Aug 17, 2023
8759644
Merge branch '2.0' into node/block-wrapper
Aug 17, 2023
342278c
Merge branch 'node/block-wrapper' of https://github.com/kwek20/iota-s…
Aug 17, 2023
67b9528
rewrote a bit
Aug 21, 2023
4763a08
Merge branch '2.0' into node/block-wrapper
Aug 21, 2023
de3fa74
lint
Aug 21, 2023
0f6e0d2
validationblock
Aug 11, 2023
05a16a8
correct block import
Aug 14, 2023
094daac
Merge branch '2.0' into node/validation
Sep 21, 2023
3cf42ce
old and fmt
Sep 21, 2023
99f4df4
validation
Sep 21, 2023
4d1cd49
fmt
Sep 21, 2023
1e00c48
is/as validation
Sep 21, 2023
e485042
comment
Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions bindings/nodejs/lib/types/block/core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

import { BasicBlock } from './basic';
import { ValidationBlock } from './validation';
/**
* All of the block types.
*/
export enum BlockType {
/// A Basic block.
Basic = 0,
/// A Validation block.
Validation = 1,
}

export abstract class Block {
Expand Down Expand Up @@ -40,4 +43,25 @@ export abstract class Block {
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.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 as unknown as ValidationBlock;
} else {
throw new Error('invalid downcast of non-ValidationBlock');
}
}
}
7 changes: 6 additions & 1 deletion bindings/nodejs/lib/types/block/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@

import { BlockType } from './block';
import { BasicBlock } from './basic';
import { ValidationBlock } from './validation';

export * from './block';
export * from './basic';
export * from './validation';
export * from './wrapper';

// Here because in block.ts it causes a circular dependency
export const BlockDiscriminator = {
property: 'type',
subTypes: [{ value: BasicBlock, name: BlockType.Basic as any }],
subTypes: [
{ value: BasicBlock, name: BlockType.Basic as any },
{ value: ValidationBlock, name: BlockType.Validation as any },
],
};
38 changes: 38 additions & 0 deletions bindings/nodejs/lib/types/block/core/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { StrongParents, WeakParents, ShallowLikeParents } from '../parents';
import { HexEncodedString } from '../../utils';
import { Block } from './block';

/**
* A Validation Block is a special type of block 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.
*/
export class ValidationBlock extends Block {
/**
* Blocks that are strongly directly approved.
*/
readonly strongParents!: StrongParents;
/**
* Blocks that are weakly directly approved.
*/
readonly weakParents!: WeakParents;
/**
* Blocks that are directly referenced to adjust opinion.
*/
readonly shallowLikeParents!: ShallowLikeParents;

/**
* The highest supported protocol version the issuer of this block supports.
*/
readonly highestSupportedVersion!: number;

/**
* The hash of the protocol parameters for the Highest Supported Version.
*/
readonly protocolParametersHash!: HexEncodedString;
}
22 changes: 22 additions & 0 deletions bindings/nodejs/lib/types/block/core/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 './';

/**
Expand Down Expand Up @@ -92,6 +93,27 @@ class BlockWrapper {
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 parseBlockWrapper(data: any): BlockWrapper {
Expand Down
Loading