Skip to content

Introduce tx nonce command #4032

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

Merged
merged 2 commits into from
Feb 13, 2025
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 @@ -6,6 +6,15 @@ Version 5.5.1

To be released.

### Added APIs

- (Libplanet.Extensions.Cocona) Added new commands to store command:
[[#4032]]
- `GetTxNonce`
- `SetTxNonce`

[#4032]: https://github.com/planetarium/libplanet/pull/4032


Version 5.5.0
-------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,38 @@ public void TestTxById()
}
}

[SkippableFact]
public void TestGetTxNonce()
{
foreach (var fx in _storeFixtures)
{
using var sw = new StringWriter();
Console.SetOut(sw);
new StoreCommand().GetTxNonce(
fx.Scheme + fx.Path,
new PrivateKey().Address.ToString());
var actual = sw.ToString();
Assert.Equal("0", actual.TrimEnd());
}
}

[SkippableFact]
public void TestSetTxNonce()
{
foreach (var fx in _storeFixtures)
{
using var sw = new StringWriter();
Console.SetOut(sw);
const int target = 5;
new StoreCommand().SetTxNonce(
fx.Scheme + fx.Path,
new PrivateKey().Address.ToString(),
target);
var actual = sw.ToString();
Assert.Equal($"{target}", actual.TrimEnd());
}
}

public void Dispose()
{
foreach (var storeFixture in _storeFixtures)
Expand Down
44 changes: 44 additions & 0 deletions tools/Libplanet.Extensions.Cocona/Commands/StoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using global::Cocona;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
Expand Down Expand Up @@ -160,6 +161,49 @@ public void MigrateIndex(string storePath)
}
}

[Command(Description = "Query a tx nonce for the address for the canonical chain.")]
public void GetTxNonce(
[Argument("STORE", Description = StoreArgumentDescription)]
string home,
[Argument("ADDRESS", Description = "signer address")]
string address)
{
IStore store = Utils.LoadStoreFromUri(home);
var canon = store.GetCanonicalChainId();
if (canon is not { } cid)
{
Console.WriteLine("No canonical chain.");
return;
}

Console.WriteLine(store.GetTxNonce(cid, new Address(address)));
store?.Dispose();
}

[Command(Description = "Mutate a tx nonce for the address for the canonical chain.")]
public void SetTxNonce(
[Argument("STORE", Description = StoreArgumentDescription)]
string home,
[Argument("ADDRESS", Description = "signer address")]
string address,
[Argument("NONCE", Description = "nonce")]
long nonce)
{
IStore store = Utils.LoadStoreFromUri(home);
var canon = store.GetCanonicalChainId();
if (canon is not { } cid)
{
Console.WriteLine("No canonical chain.");
return;
}

var signer = new Address(address);
var currentNonce = store.GetTxNonce(cid, signer);
store.IncreaseTxNonce(cid, signer, nonce - currentNonce);
Console.WriteLine(store.GetTxNonce(cid, signer));
store?.Dispose();
}

private static Block GetBlock(IStore store, BlockHash blockHash)
{
if (!(store.GetBlock(blockHash) is { } block))
Expand Down
Loading