Skip to content

Commit

Permalink
TIP BlockId tests (#1607)
Browse files Browse the repository at this point in the history
* uncomment block id tests

* move json as fixtures into own .json files

* Remove bindings fixtures

* Cleanup fixtures

* Cleanup tests

* Uncomment tests

* Add TODO

* python: basic block tagged data payload

* python: it's a signed block

* python: basic block transaction payload

* python: validation block

* nodejs: basic block tagged data payload

* nodejs: basic block transaction payload

* nodejs: validation block

* simplify

* nodejs: fix some block tests

* remove temporary stuff

* nits

* back to interfaces

* format

* nit

* cleanup

* rename instead of serde rename

* reverted u64

* update test vectors

* attempt to fix tests

* Update JSONs

* Remove FIXMEs

* Fix test

* copyright

Co-authored-by: Thoralf-M <[email protected]>

* nodejs: re-enable validation block test

* fix 1/3 python block id tests

* allow passing args to pytest

* Python: fix serialization/deserialization

* linter, review

* format

* format (are you serious?)

* lint tests

* format annoying lints

* assign id method for block

* yarn lint

* type field first

* get rid of to_dict

* update fixture

* format

* python: fix lint

* python: fix metadata feature output tests

* apply review suggestion

Co-authored-by: Thoralf-M <[email protected]>

* Nits

---------

Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Brord van Wierst <[email protected]>
Co-authored-by: Thoralf-M <[email protected]>
Co-authored-by: DaughterOfMars <[email protected]>
  • Loading branch information
5 people committed Jan 30, 2024
1 parent 1edc4f2 commit eb737eb
Show file tree
Hide file tree
Showing 35 changed files with 732 additions and 844 deletions.
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class Client {
* Get the token supply.
*/
async getTokenSupply(): Promise<u64> {
return (await this.getProtocolParameters()).tokenSupply;
return BigInt((await this.getProtocolParameters()).tokenSupply);
}

/**
Expand Down
20 changes: 17 additions & 3 deletions bindings/nodejs/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import 'reflect-metadata';
import { callUtilsMethod } from './bindings';
import {
Block,
BlockId,
BlockError,
ClientError,
ClientErrorName,
Expand All @@ -15,8 +17,9 @@ import {
UTXOInput,
WalletError,
WalletErrorName,
ProtocolParameters,
} from './types';
import { bigIntToHex } from './utils';
import { bigIntToHex, Utils } from './utils';

// Allow bigint to be serialized as hex string.
//
Expand All @@ -27,8 +30,8 @@ import { bigIntToHex } from './utils';
return bigIntToHex(this);
};

// Assign the util method on UTXOInput here,
// to prevent loading bindings (callUtilsMethod) when importing UTXOInput just for typing.
// Assign the util method on UTXOInput here to prevent loading bindings (callUtilsMethod)
// when importing UTXOInput just for typing.
Object.assign(UTXOInput, {
/**
* Creates a `UTXOInput` from an output id.
Expand All @@ -44,6 +47,17 @@ Object.assign(UTXOInput, {
},
});

// Assign the util method on Block here to prevent loading bindings (callUtilsMethod)
// when importing Block just for typing.
Object.assign(Block, {
/**
* Returns the block id for the given block.
*/
id(block: Block, params: ProtocolParameters): BlockId {
return Utils.blockId(block, params);
},
});

export * from './client';
export * from './secret_manager';
export * from './types';
Expand Down
10 changes: 10 additions & 0 deletions bindings/nodejs/lib/types/block/core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BlockBody } from './block-body';
import { BasicBlockBody } from './basic';
import { ValidationBlockBody } from './validation';
import { BlockBodyDiscriminator } from '.';
import { ProtocolParameters, BlockId } from '../../..';

/**
* The block header.
Expand Down Expand Up @@ -118,6 +119,15 @@ class Block {
asValidation(): ValidationBlockBody {
return this.body.asValidation();
}

/**
* Returns the block id.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static id(block: Block, params: ProtocolParameters): BlockId {
// Implementation injected in lib/index.ts, as it uses bindings.
return null as unknown as BlockId;
}
}

function parseBlock(data: any): Block {
Expand Down
27 changes: 19 additions & 8 deletions bindings/nodejs/lib/types/models/info/node-info-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import type { StorageScoreParameters } from '../storage-score';
import { EpochIndex } from '../../block/slot';
import { u64 } from '../../utils';
import { u64 } from '../../utils/type-aliases';

/**
* The Protocol Info.
*/
export interface ProtocolInfo {
interface ProtocolInfo {
/**
* The start epoch of the set of protocol parameters.
*/
Expand All @@ -22,7 +22,7 @@ export interface ProtocolInfo {
/**
* The Protocol Parameters.
*/
export interface ProtocolParameters {
interface ProtocolParameters {
/**
* Set to value 0 to denote a IOTA 2.0 protocol parameter.
*/
Expand Down Expand Up @@ -129,7 +129,7 @@ export interface ProtocolParameters {
/**
* Rewards Parameters defines the parameters that are used to calculate Mana rewards.
*/
export interface RewardsParameters {
interface RewardsParameters {
/**
* Profit Margin Exponent is used for shift operation for calculation of profit margin.
*/
Expand Down Expand Up @@ -164,7 +164,7 @@ export interface RewardsParameters {
/**
* Work Score Parameters lists the work score of each type, it is used to denote the computation costs of processing an object.
*/
export interface WorkScoreParameters {
interface WorkScoreParameters {
/**
* DataByte accounts for the network traffic per kibibyte.
*/
Expand Down Expand Up @@ -210,7 +210,7 @@ export interface WorkScoreParameters {
/**
* ManaParameters defines the parameters used by mana calculation.
*/
export interface ManaParameters {
interface ManaParameters {
/**
* The number of bits used to represent Mana.
*/
Expand Down Expand Up @@ -244,7 +244,7 @@ export interface ManaParameters {
/**
* Congestion Control Parameters defines the parameters used to calculate the Reference Mana Cost (RMC).
*/
export interface CongestionControlParameters {
interface CongestionControlParameters {
/**
* The minimum value of the reference Mana cost.
*/
Expand Down Expand Up @@ -282,7 +282,7 @@ export interface CongestionControlParameters {
/**
* The version signaling parameters.
*/
export interface VersionSignalingParameters {
interface VersionSignalingParameters {
/**
* The size of the window in epochs to find which version of protocol parameters was most signaled, from currentEpoch - windowSize to currentEpoch.
*/
Expand All @@ -296,3 +296,14 @@ export interface VersionSignalingParameters {
*/
activationOffset: number;
}

export {
ProtocolInfo,
ProtocolParameters,
RewardsParameters,
WorkScoreParameters,
StorageScoreParameters,
ManaParameters,
VersionSignalingParameters,
CongestionControlParameters,
};
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/types/models/storage-score.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { u64 } from '../../utils';
import { u64 } from '../utils/type-aliases';

/**
* Defines the parameters of storage score calculations on objects which take node resources.
Expand Down
3 changes: 1 addition & 2 deletions bindings/nodejs/tests/client/examples.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { expect, describe, it } from '@jest/globals';
import {
Client,
utf8ToHex,
Expand Down Expand Up @@ -215,7 +214,7 @@ describe.skip('Main examples', () => {
),
],
});

//let payload = await secretManager.signTransaction(prepared);
const unsignedBlock = await client.buildBasicBlock("", undefined);
const signedBlock = await secretManager.signBlock(unsignedBlock, chain);
Expand Down
38 changes: 38 additions & 0 deletions bindings/nodejs/tests/types/block.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import 'reflect-metadata';

import { expect, describe, it } from '@jest/globals';
import * as basic_block_tagged_data_payload_json from '../../../../sdk/tests/types/fixtures/basic_block_tagged_data_payload.json';
import * as basic_block_transaction_payload_json from '../../../../sdk/tests/types/fixtures/basic_block_transaction_payload.json';
import * as validation_block_json from '../../../../sdk/tests/types/fixtures/validation_block.json';
import * as protocol_parameters_json from '../../../../sdk/tests/types/fixtures/protocol_parameters.json';
import { Block, BlockId, parseBlock, ProtocolParameters } from '../../';

describe('Block tests', () => {

it('compares basic block tagged data payload from a fixture', async () => {
const block = parseBlock(basic_block_tagged_data_payload_json.block);
expect(block).toBeInstanceOf(Block);
const params: ProtocolParameters = JSON.parse(JSON.stringify(protocol_parameters_json.params));
const expected_id = basic_block_tagged_data_payload_json.id as BlockId;
expect(Block.id(block, params)).toEqual(expected_id);
});

it('compares basic block transaction payload from a fixture', async () => {
const block = parseBlock(basic_block_transaction_payload_json.block);
expect(block).toBeInstanceOf(Block);
const params: ProtocolParameters = JSON.parse(JSON.stringify(protocol_parameters_json.params));
const expected_id = basic_block_transaction_payload_json.id as BlockId;
expect(Block.id(block, params)).toEqual(expected_id);
});

it('compares validation block from a fixture', async () => {
const block = parseBlock(validation_block_json.block);
expect(block).toBeInstanceOf(Block);
const params: ProtocolParameters = JSON.parse(JSON.stringify(protocol_parameters_json.params));
const expected_id = validation_block_json.id as BlockId;
expect(Block.id(block, params)).toEqual(expected_id);
});
});
2 changes: 1 addition & 1 deletion bindings/nodejs/tests/types/protocol_parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('ProtocolParameters tests', () => {

it('compares ProtocolParameters hash from a fixture', async () => {

const params = protocol_parameters.params as unknown as ProtocolParameters;
const params: ProtocolParameters = JSON.parse(JSON.stringify(protocol_parameters.params));
const hash = Utils.protocolParametersHash(params);
const expected_hash = protocol_parameters.hash;

Expand Down
Loading

0 comments on commit eb737eb

Please sign in to comment.