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

🧹 Removed Nonce struct #3794

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ To be released.

### Deprecated APIs

- (Libplanet.Common) Removed `Nonce` struct. [[#3793], [#3794]]

### Backward-incompatible API changes

- (Libplanet.Types) Removed `nonce` parameter from
`BlockMetadata.DerivePreEvaluationHash()` and
`BlockMetadata.MakeCandidateData()` methods. [[#3793], [#3794]]

### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes
Expand All @@ -24,6 +30,9 @@ To be released.

### CLI tools

[#3793]: https://github.com/planetarium/libplanet/issues/3793
[#3794]: https://github.com/planetarium/libplanet/pull/3794


Version 4.5.0
-------------
Expand Down
130 changes: 0 additions & 130 deletions Libplanet.Common/Nonce.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Libplanet.Net.Tests/Consensus/ContextNonProposerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,7 @@ message is ConsensusPreCommitMsg commit &&
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: null);
var preEval = new PreEvaluationBlock(
new PreEvaluationBlockHeader(
metadata, metadata.DerivePreEvaluationHash(default)),
new PreEvaluationBlockHeader(metadata, metadata.DerivePreEvaluationHash()),
txs);
var invalidBlock = preEval.Sign(
TestUtils.PrivateKeys[1],
Expand Down
3 changes: 1 addition & 2 deletions Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,7 @@ public void CannotAppendBlockWithInvalidActions()
txHash: BlockContent.DeriveTxHash(txs),
lastCommit: null);
var preEval = new PreEvaluationBlock(
new PreEvaluationBlockHeader(
metadata, metadata.DerivePreEvaluationHash(default)),
new PreEvaluationBlockHeader(metadata, metadata.DerivePreEvaluationHash()),
txs);
var block = preEval.Sign(
_fx.Proposer, HashDigest<SHA256>.DeriveFrom(TestUtils.GetRandomBytes(1024)));
Expand Down
82 changes: 11 additions & 71 deletions Libplanet.Tests/Blocks/BlockMetadataTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Tests.Fixtures;
Expand Down Expand Up @@ -124,16 +123,12 @@ public void MakeCandidateData()
.Add(
"transaction_fingerprint",
ParseHex("3d8e87977b1142863435b9385657e69557df8951a0698e9719f7d06c5fb8db1f"));
AssertBencodexEqual(expectedGenesis, GenesisMetadata.MakeCandidateData(default));
AssertBencodexEqual(
expectedGenesis.SetItem("nonce", new byte[] { 0x00, 0x01, 0x02 }),
GenesisMetadata.MakeCandidateData(new Nonce(new byte[] { 0x00, 0x01, 0x02 }))
);
AssertBencodexEqual(expectedGenesis, GenesisMetadata.MakeCandidateData());

Bencodex.Types.Dictionary expectedBlock1 = Bencodex.Types.Dictionary.Empty
.Add("index", 1L)
.Add("timestamp", "2021-09-06T08:01:09.045000Z")
.Add("nonce", new byte[] { 0xff, 0xef, 0x01, 0xcc })
.Add("nonce", ImmutableArray<byte>.Empty)
.Add(
"public_key",
ParseHex("0215ba27a461a986f4ce7bcda1fd73dc708da767d0405729edaacaad7b7ff60eed")
Expand All @@ -149,39 +144,7 @@ public void MakeCandidateData()
.Add("protocol_version", BlockMetadata.CurrentProtocolVersion);
AssertBencodexEqual(
expectedBlock1,
Block1Metadata.MakeCandidateData(new Nonce(new byte[] { 0xff, 0xef, 0x01, 0xcc }))
);

Bencodex.Types.Dictionary expectedPv0 = Bencodex.Types.Dictionary.Empty
.Add("index", 0L)
.Add("timestamp", "2021-09-06T04:46:39.123000Z")
.Add("nonce", ImmutableArray<byte>.Empty)
.Add("reward_beneficiary", ParseHex("268344BA46e6CA2A8a5096565548b9018bc687Ce"));
AssertBencodexEqual(expectedPv0, GenesisMetadataPv0.MakeCandidateData(default));
AssertBencodexEqual(
expectedPv0.SetItem("nonce", new byte[] { 0x00, 0x01, 0x02 }),
GenesisMetadataPv0.MakeCandidateData(new Nonce(new byte[] { 0x00, 0x01, 0x02 }))
);

Bencodex.Types.Dictionary expectedPv1 = Bencodex.Types.Dictionary.Empty
.Add("index", 1L)
.Add("timestamp", "2021-09-06T08:01:09.045000Z")
.Add("nonce", ImmutableArray<byte>.Empty)
.Add("reward_beneficiary", ParseHex("8a29de186B85560D708451101C4Bf02D63b25c50"))
.Add(
"previous_hash",
ParseHex("341e8f360597d5bc45ab96aabc5f1b0608063f30af7bd4153556c9536a07693a")
)
.Add(
"transaction_fingerprint",
ParseHex("654698d34b6d9a55b0c93e4ffb2639278324868c91965bc5f96cb3071d6903a0")
)
.Add("protocol_version", 1);
AssertBencodexEqual(expectedPv1, Block1MetadataPv1.MakeCandidateData(default));
AssertBencodexEqual(
expectedPv1.SetItem("nonce", new byte[] { 0x00, 0x01, 0x02 }),
Block1MetadataPv1.MakeCandidateData(new Nonce(new byte[] { 0x00, 0x01, 0x02 }))
);
Block1Metadata.MakeCandidateData());
}

[Fact]
Expand All @@ -190,29 +153,18 @@ public void MakeCandidateDataPv1()
Bencodex.Types.Dictionary expected = Bencodex.Types.Dictionary.Empty
.Add("index", 1L)
.Add("timestamp", "2021-09-06T08:01:09.045000Z")
.Add("nonce", new byte[] { 0xff, 0xef, 0x01, 0xcc })
.Add(
"reward_beneficiary",
ParseHex("8a29de186B85560D708451101C4Bf02D63b25c50")
)
.Add("nonce", ImmutableArray<byte>.Empty)
.Add("reward_beneficiary", ParseHex("8a29de186B85560D708451101C4Bf02D63b25c50"))
.Add(
"previous_hash",
ParseHex(
"341e8f360597d5bc45ab96aabc5f1b0608063f30af7bd4153556c9536a07693a"
)
)
"341e8f360597d5bc45ab96aabc5f1b0608063f30af7bd4153556c9536a07693a"))
.Add(
"transaction_fingerprint",
ParseHex(
"654698d34b6d9a55b0c93e4ffb2639278324868c91965bc5f96cb3071d6903a0"
)
)
"654698d34b6d9a55b0c93e4ffb2639278324868c91965bc5f96cb3071d6903a0"))
.Add("protocol_version", 1);
AssertBencodexEqual(
expected,
Block1MetadataPv1.MakeCandidateData(
new Nonce(new byte[] { 0xff, 0xef, 0x01, 0xcc }))
);
AssertBencodexEqual(expected, Block1MetadataPv1.MakeCandidateData());
}

[Fact]
Expand All @@ -222,31 +174,19 @@ public void MakeCandidateDataPv0()
.Add("index", 0L)
.Add("timestamp", "2021-09-06T04:46:39.123000Z")
.Add("nonce", ImmutableArray<byte>.Empty)
.Add(
"reward_beneficiary",
ParseHex("268344BA46e6CA2A8a5096565548b9018bc687Ce")
);
AssertBencodexEqual(
(IValue)expected.Remove((Text)"protocol_version"),
GenesisMetadataPv0.MakeCandidateData(default)
);
.Add("reward_beneficiary", ParseHex("268344BA46e6CA2A8a5096565548b9018bc687Ce"));
AssertBencodexEqual(expected, GenesisMetadataPv0.MakeCandidateData());
}

[Fact]
public void DerivePreEvaluationHash()
{
ImmutableArray<byte> FromHex(string hex) => ParseHexToImmutable(hex);

HashDigest<SHA256> hash = GenesisMetadata.DerivePreEvaluationHash(default);
HashDigest<SHA256> hash = GenesisMetadata.DerivePreEvaluationHash();
AssertBytesEqual(
FromHex("db037e64000f3f672288e62a7f9a646cc9f636256a91d40559ebbffb53340948"),
hash.ByteArray);

hash = Block1Metadata.DerivePreEvaluationHash(
new Nonce(FromHex("e7c1adf92c65d35aaae5")));
AssertBytesEqual(
FromHex("e1fb185006f05b18d8c264622c2e86d231fc7fbb6a777ae86629c41b37dac0d2"),
hash.ByteArray);
}

[Fact]
Expand Down
Loading
Loading