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

Add output storage score tests #1827

Merged
merged 11 commits into from
Jan 17, 2024
606 changes: 143 additions & 463 deletions sdk/tests/types/fixtures/protocol_parameters.json

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions sdk/tests/types/fixtures/tip40_delegation_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"output": {
"type": 5,
"amount": "200000000",
"delegatedAmount": "500000000",
"delegationId": "0x08b987baffaacb9da156734275ee01a42a35fe06653823be654821a7ddf92380",
"validatorAddress": {
"type": 8,
"accountId": "0x17432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a"
},
"startEpoch": 30,
"endEpoch": 50,
"unlockConditions": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0xed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a"
}
}
]
},
"bytes": "0x0500c2eb0b000000000065cd1d0000000008b987baffaacb9da156734275ee01a42a35fe06653823be654821a7ddf923800817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a1e00000032000000010000ed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a",
"storage_score": 313
}
39 changes: 39 additions & 0 deletions sdk/tests/types/fixtures/tip41_basic_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"output": {
"type": 0,
"amount": "200000000",
"mana": "555000000",
"unlockConditions": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0xed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a"
}
},
{
"type": 2,
"slot": 999
}
],
"features": [
{
"type": 2,
"entries": {
"iota": "0x322e30"
}
},
{
"type": 4,
"tag": "0x73746f726167655f73636f7265"
},
{
"type": 5,
"id": "0x083ef5555d65ba314fb1680147da0d54b08cecfd9a728bdcf5f1ebda1fedb9f852435d8d1000",
"amount": "0x2a"
}
]
},
"bytes": "0x0000c2eb0b00000000c0a0142100000000020000ed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a02e703000003020104696f74610300322e30040d73746f726167655f73636f726505083ef5555d65ba314fb1680147da0d54b08cecfd9a728bdcf5f1ebda1fedb9f852435d8d10002a00000000000000000000000000000000000000000000000000000000000000",
"storage_score": 244
}
58 changes: 58 additions & 0 deletions sdk/tests/types/fixtures/tip42_account_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"output": {
"type": 1,
"amount": "200000000",
"mana": "333000000",
"accountId": "0xe8494fe353f99783d3771c78798e1e839e649310513770fc6dc974fe53cf1a86",
"foundryCounter": 0,
"unlockConditions": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0xed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a"
}
}
],
"features": [
{
"type": 6,
"expirySlot": 888,
"blockIssuerKeys": [
{
"type": 0,
"publicKey": "0x9e05a32eafedefd40298e24ad4f8c334580187f7e9afbd9da13b5ba4007dd1b5"
},
{
"type": 0,
"publicKey": "0xa504844f7a0df2c5101d31696593b309040f8660d41035aba508f24c00668b21"
}
]
},
{
"type": 7,
"stakedAmount": "150000000",
"fixedCost": "400",
"startEpoch": 25,
"endEpoch": 4294967295
}
],
"immutableFeatures": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0xed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a"
}
},
{
"type": 2,
"entries": {
"iota": "0x322e30"
}
}
]
},
"bytes": "0x0100c2eb0b00000000402dd91300000000e8494fe353f99783d3771c78798e1e839e649310513770fc6dc974fe53cf1a8600000000010000ed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a02067803000002009e05a32eafedefd40298e24ad4f8c334580187f7e9afbd9da13b5ba4007dd1b500a504844f7a0df2c5101d31696593b309040f8660d41035aba508f24c00668b210780d1f00800000000900100000000000019000000ffffffff020000ed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a020104696f74610300322e30",
"storage_score": 621
}
43 changes: 43 additions & 0 deletions sdk/tests/types/fixtures/tip43_nft_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"output": {
"type": 4,
"amount": "421000000",
"mana": "0",
"nftId": "0x3163482ff326658dd1bfead468a609bc6b3e74cb2e5094e0da9b63eae7e0166c",
"unlockConditions": [
{
"type": 0,
"address": {
"type": 8,
"accountId": "0x17432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a"
}
},
{
"type": 1,
"returnAddress": {
"type": 0,
"pubKeyHash": "0xed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a"
},
"amount": "4000000"
}
],
"features": [
{
"type": 2,
"entries": {
"iota": "0x322e30"
}
}
],
"immutableFeatures": [
{
"type": 2,
"entries": {
"nft": "0x696f7461"
}
}
]
},
"bytes": "0x0440f317190000000000000000000000003163482ff326658dd1bfead468a609bc6b3e74cb2e5094e0da9b63eae7e0166c02000817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a0100ed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a00093d000000000001020104696f74610300322e30010201036e66740400696f7461",
"storage_score": 240
}
37 changes: 37 additions & 0 deletions sdk/tests/types/fixtures/tip44_foundry_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"output": {
"type": 3,
"amount": "420000000",
"serialNumber": 0,
"tokenScheme": {
"type": 0,
"mintedTokens": "0x9c40",
"meltedTokens": "0x2710",
"maximumSupply": "0xc350"
},
"unlockConditions": [
{
"type": 6,
"address": {
"type": 8,
"accountId": "0x17432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a"
}
}
],
"features": [
{
"type": 2,
"entries": {
"iota": "0x322e3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030"
}
},
{
"type": 5,
"id": "0x0817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a0000000000",
"amount": "0xa410"
}
]
},
"bytes": "0x0300b10819000000000000000000409c000000000000000000000000000000000000000000000000000000000000102700000000000000000000000000000000000000000000000000000000000050c300000000000000000000000000000000000000000000000000000000000001060817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a02020104696f74612e00322e3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030050817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a000000000010a400000000000000000000000000000000000000000000000000000000000000",
"storage_score": 361
}
50 changes: 50 additions & 0 deletions sdk/tests/types/fixtures/tip54_anchor_output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"output": {
"type": 2,
"amount": "420000000",
"mana": "42",
"anchorId": "0x0000000000000000000000000000000000000000000000000000000000000000",
"stateIndex": 0,
"unlockConditions": [
{
"type": 4,
"address": {
"type": 8,
"accountId": "0x17432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a"
}
},
{
"type": 5,
"address": {
"type": 8,
"accountId": "0x17432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a"
}
}
],
"features": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0xed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a"
}
},
{
"type": 2,
"entries": {
"iota": "0x322e30"
}
}
],
"immutableFeatures": [
{
"type": 2,
"entries": {
"iota": "0x322e30"
}
}
]
},
"bytes": "0x0200b10819000000002a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000002040817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a050817432c5a7a672503480241125e3952414a7a320441080c624c264b004e09614a020000ed1484f4d1f7d8c037087fed661dd92faccae1eed3c01182d6fdd6828cea144a020104696f74610300322e3001020104696f74610300322e30",
"storage_score": 270
}
61 changes: 43 additions & 18 deletions sdk/tests/types/storage_score.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

// TODO: Re-enable when storage score is figured out
use iota_sdk::{
types::block::output::{Output, StorageScore, StorageScoreParameters},
utils::serde::prefix_hex_bytes,
};
use pretty_assertions::assert_eq;
use serde::Deserialize;

// use iota_sdk::types::block::{
// output::{Output, Rent},
// protocol::protocol_parameters,
// rand::output::{rand_account_output, rand_basic_output, rand_foundry_output, rand_nft_output},
// };
// From https://github.com/iotaledger/tips/blob/tip49/tips/TIP-0049/tip-0049.md#test-vectors
fn storage_score_parameters() -> Result<StorageScoreParameters, Box<dyn std::error::Error>> {
let json: serde_json::Value =
serde_json::from_reader(std::fs::File::open("./tests/types/fixtures/protocol_parameters.json")?)?;
Ok(StorageScoreParameters::deserialize(
&json["params"]["storageScoreParameters"],
)?)
}

// fn output_in_range(output: Output, range: std::ops::RangeInclusive<u64>) {
// let cost = output.rent_cost(Default::default());
// assert!(range.contains(&cost), "{output:#?} has a required byte cost of {cost}");
// }
#[derive(Deserialize)]
struct OutputFixture {
output: Output,
#[serde(with = "prefix_hex_bytes")]
bytes: Vec<u8>,
storage_score: u64,
}

// #[test]
// fn valid_rent_cost_range() {
// let token_supply = protocol_parameters().token_supply();
fn output_fixture(filename: &str) -> Result<OutputFixture, Box<dyn std::error::Error>> {
Ok(serde_json::from_value(serde_json::from_reader(std::fs::File::open(
format!("./tests/types/fixtures/{filename}"),
)?)?)?)
}

// output_in_range(Output::Account(rand_account_output(token_supply)), 445..=29_620);
// output_in_range(Output::Basic(rand_basic_output(token_supply)), 414..=13_485);
// output_in_range(Output::Foundry(rand_foundry_output(token_supply)), 496..=21_365);
// output_in_range(Output::Nft(rand_nft_output(token_supply)), 435..=21_734);
// }
// From https://github.com/iotaledger/tips/blob/tip41/tips/TIP-0041/tip-0041.md#storage-score
#[test]
fn output_storage_score() {
for fixture in [
output_fixture("tip41_basic_output.json").unwrap(),
output_fixture("tip42_account_output.json").unwrap(),
output_fixture("tip43_nft_output.json").unwrap(),
output_fixture("tip44_foundry_output.json").unwrap(),
output_fixture("tip54_anchor_output.json").unwrap(),
output_fixture("tip40_delegation_output.json").unwrap(),
] {
assert_eq!(
fixture.output.storage_score(storage_score_parameters().unwrap()),
fixture.storage_score
);
}
}
Loading