Skip to content

Commit

Permalink
Bindings: Add ProtocolParameters tests (#1508)
Browse files Browse the repository at this point in the history
* update and re-endable protocol parameters test

* python: protocol parameters from/to dict

* protocol_parameters.json

* Use hex encoded bytes

* Fix params_serde_hash

* Cleanup rust test

* Add MultiAddress packable test

* add python test

* add nodejs test

* add protocol_parameters.spec.ts

* partially ready

* add protocol parameters hash method

* comments

* rm some imports

* some fixes

* add python hash check

* doc fixes

* fmt

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Alex6323 and thibault-martinez authored Dec 4, 2023
1 parent 6d91fc2 commit 5b36631
Show file tree
Hide file tree
Showing 17 changed files with 7,684 additions and 5,498 deletions.
72 changes: 14 additions & 58 deletions bindings/core/src/method/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,95 +29,51 @@ use crate::OmittedDebug;
#[non_exhaustive]
pub enum UtilsMethod {
/// Transforms bech32 to hex
Bech32ToHex {
/// Bech32 encoded address
bech32: Bech32Address,
},
Bech32ToHex { bech32: Bech32Address },
/// Transforms a hex encoded address to a bech32 encoded address
#[serde(rename_all = "camelCase")]
HexToBech32 {
/// Hex encoded bech32 address
hex: String,
/// Human readable part
bech32_hrp: Hrp,
},
HexToBech32 { hex: String, bech32_hrp: Hrp },
/// Transforms an account id to a bech32 encoded address
#[serde(rename_all = "camelCase")]
AccountIdToBech32 {
/// Account ID
account_id: AccountId,
/// Human readable part
bech32_hrp: Hrp,
},
AccountIdToBech32 { account_id: AccountId, bech32_hrp: Hrp },
/// Transforms an nft id to a bech32 encoded address
#[serde(rename_all = "camelCase")]
NftIdToBech32 {
/// Nft ID
nft_id: NftId,
/// Human readable part
bech32_hrp: Hrp,
},
NftIdToBech32 { nft_id: NftId, bech32_hrp: Hrp },
/// Transforms a hex encoded public key to a bech32 encoded address
#[serde(rename_all = "camelCase")]
HexPublicKeyToBech32Address {
/// Hex encoded public key
hex: String,
/// Human readable part
bech32_hrp: Hrp,
},
HexPublicKeyToBech32Address { hex: String, bech32_hrp: Hrp },
/// Returns a valid Address parsed from a String.
ParseBech32Address {
/// Address
address: Bech32Address,
},
ParseBech32Address { address: Bech32Address },
/// Checks if a String is a valid bech32 encoded address.
IsAddressValid {
/// Address
address: String,
},
IsAddressValid { address: String },
/// Generates a new mnemonic.
GenerateMnemonic,
/// Returns a hex encoded seed for a mnemonic.
MnemonicToHexSeed {
/// Mnemonic
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
mnemonic: String,
},
/// Returns a block ID from a block and slot protocol parameters
#[serde(rename_all = "camelCase")]
BlockId {
/// Block
block: BlockDto,
/// Network Protocol Parameters
protocol_parameters: ProtocolParameters,
},
/// Returns the transaction ID (Blake2b256 hash of the provided transaction payload)
TransactionId {
/// Transaction Payload
payload: SignedTransactionPayloadDto,
},
TransactionId { payload: SignedTransactionPayloadDto },
/// Computes the account ID
#[serde(rename_all = "camelCase")]
ComputeAccountId {
/// Output ID
output_id: OutputId,
},
ComputeAccountId { output_id: OutputId },
/// Computes the Foundry ID
#[serde(rename_all = "camelCase")]
ComputeFoundryId {
/// Alias id
account_id: AccountId,
/// Serial number
serial_number: u32,
/// Token scheme kind
token_scheme_type: u8,
},
/// Computes the NFT ID
#[serde(rename_all = "camelCase")]
ComputeNftId {
/// Output ID
output_id: OutputId,
},
ComputeNftId { output_id: OutputId },
/// Computes the output ID from transaction id and output index
ComputeOutputId { id: TransactionId, index: u16 },
/// Computes a tokenId from the aliasId, serial number and token scheme type.
Expand All @@ -127,11 +83,11 @@ pub enum UtilsMethod {
serial_number: u32,
token_scheme_type: u8,
},
/// Computes the hash of the given protocol parameters.
#[serde(rename_all = "camelCase")]
ProtocolParametersHash { protocol_parameters: ProtocolParameters },
/// Computes the signing hash of a transaction.
TransactionSigningHash {
/// The transaction.
transaction: TransactionDto,
},
TransactionSigningHash { transaction: TransactionDto },
/// Computes the minimum required amount of an output.
#[serde(rename_all = "camelCase")]
ComputeMinimumOutputAmount {
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
let foundry_id = FoundryId::build(&AccountAddress::new(account_id), serial_number, token_scheme_type);
Response::TokenId(TokenId::from(foundry_id))
}
UtilsMethod::ProtocolParametersHash { protocol_parameters } => {
Response::Hash(protocol_parameters.hash().to_string())
}
UtilsMethod::TransactionSigningHash { transaction } => {
Response::Hash(Transaction::try_from_dto(transaction)?.signing_hash().to_string())
}
Expand Down
24 changes: 18 additions & 6 deletions bindings/nodejs/lib/types/models/info/node-info-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ export interface ProtocolParameters {
* Work Score Parameters lists the work score of each type, it is used to denote the computation costs of processing an object.
*/
workScoreParameters: WorkScoreParameters;
/**
* The parameters used by mana calculation.
*/
manaParameters: ManaParameters;
/**
* Current supply of base token.
*/
tokenSupply: u64;
/**
* Genesis Slot defines the slot of the genesis.
*/
genesisSlot: number;
/**
* The genesis timestamp at which the slots start to count.
*/
Expand All @@ -63,10 +71,6 @@ export interface ProtocolParameters {
* The number of slots in an epoch expressed as an exponent of 2.
*/
slotsPerEpochExponent: number;
/**
* The parameters used by mana calculation.
*/
manaParameters: ManaParameters;
/**
* The unbonding period in epochs before an account can stop staking.
*/
Expand All @@ -80,9 +84,13 @@ export interface ProtocolParameters {
*/
punishmentEpochs: number;
/**
* Determine if a block is eligible by evaluating issuingTime and commitments in its pastcone to ATT and lastCommittedSlot respectively.
* Liveness Threshold Lower Bound is used by tip-selection to determine if a block is eligible by evaluating issuingTimes.
*/
livenessThreshold: number;
livenessThresholdLowerBound: number;
/**
* Liveness Threshold Upper Bound is used by tip-selection to determine if a block is eligible by evaluating issuingTimes.
*/
livenessThresholdUpperBound: number;
/**
* MinCommittableAge is the minimum age relative to the accepted tangle time slot index that a slot can be committed.
*/
Expand All @@ -107,6 +115,10 @@ export interface ProtocolParameters {
* Rewards Parameters defines the parameters that are used to calculate Mana rewards.
*/
rewardsParameters: RewardsParameters;
/**
* Target Committee Size defines the target size of the committee. If there's fewer candidates the actual committee size could be smaller in a given epoch.
*/
targetCommitteeSize: number;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
__NftIdToBech32Method__,
__HexPublicKeyToBech32AddressMethod__,
__IsAddressValidMethod__,
__ProtocolParametersHashMethod__,
__TransactionSigningHashMethod__,
__VerifyEd25519SignatureMethod__,
__VerifySecp256k1EcdsaSignatureMethod__,
Expand Down Expand Up @@ -45,6 +46,7 @@ export type __UtilsMethods__ =
| __NftIdToBech32Method__
| __HexPublicKeyToBech32AddressMethod__
| __IsAddressValidMethod__
| __ProtocolParametersHashMethod__
| __TransactionSigningHashMethod__
| __VerifyEd25519SignatureMethod__
| __VerifySecp256k1EcdsaSignatureMethod__
Expand Down
7 changes: 7 additions & 0 deletions bindings/nodejs/lib/types/utils/bridge/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ export interface __IsAddressValidMethod__ {
};
}

export interface __ProtocolParametersHashMethod__ {
name: 'protocolParametersHash';
data: {
protocolParameters: ProtocolParameters;
};
}

export interface __TransactionSigningHashMethod__ {
name: 'transactionSigningHash';
data: {
Expand Down
17 changes: 17 additions & 0 deletions bindings/nodejs/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,23 @@ export class Utils {
});
}

/**
* Compute the hash of an instance of ProtocolParameters.
*
* @param protocolParameters A ProtocolParameters instance.
* @returns The hash of the protocol parameters as a hex-encoded string.
*/
static protocolParametersHash(
protocolParameters: ProtocolParameters,
): HexEncodedString {
return callUtilsMethod({
name: 'protocolParametersHash',
data: {
protocolParameters,
},
});
}

/**
* Compute the signing hash of a transaction.
*
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"typescript": "^4.9.4"
},
"devDependencies": {
"@napi-rs/cli": "^1.0.0",
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
Expand All @@ -48,8 +49,7 @@
"typedoc": "^0.24.6",
"typedoc-plugin-markdown": "^3.14.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"@napi-rs/cli": "^1.0.0"
"webpack-cli": "^5.1.4"
},
"overrides": {
"tar@<=4.4.17": "^4.4.19",
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/tests/client/examples.spec.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 { describe, it } from '@jest/globals';
import { expect, describe, it } from '@jest/globals';
import {
Client,
utf8ToHex,
Expand Down
21 changes: 21 additions & 0 deletions bindings/nodejs/tests/types/protocol_parameters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import 'reflect-metadata';

import { expect, describe, it } from '@jest/globals';
import * as protocol_parameters from '../../../../sdk/tests/types/fixtures/protocol_parameters.json';
import { ProtocolParameters } from '../../lib/types/models/info/node-info-protocol';
import { Utils } from '../../';

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

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

const params = protocol_parameters.params as unknown as ProtocolParameters;
const hash = Utils.protocolParametersHash(params);
const expected_hash = protocol_parameters.hash;

expect(hash).toEqual(expected_hash);
});
});
Loading

0 comments on commit 5b36631

Please sign in to comment.