Skip to content

Commit

Permalink
CLI for TxIdBlockHash index (#1340)
Browse files Browse the repository at this point in the history
add commands for indexing TxId->BlockHash
* planet store build-index-tx-block [STORE] [OFFSET] [LIMIT]
* planet store blocks-by-tx-id [STORE] [TX-ID]
* planet store block-hashes-by-tx-id [STORE] [TX-ID]
  • Loading branch information
kfangw authored Jul 1, 2021
1 parent 301f616 commit 77d8785
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 28 deletions.
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,12 @@ To be released.
key without asking passphrase. [[#1213], [#1265]]
- Added `planet key derive` subcommand to derive the address or
public key from a private. [[#1268]]
- Added `planet query [block-by-hash|block-by-index|tx-by-id]` commands
- Added `planet store [block-by-hash|block-by-index|tx-by-id]` commands
to retrieve the data from the store. [[#1284], [#1285], [#1298]]
- Added `planet store block-by-tx-id` commands
to retrieve the data from the store. [[#1316], [#1340]]
- Added `planet store build-index-tx-block` commands
to build index from TxId to BlockHash. [[#1316], [#1340]]

[#1156]: https://github.com/planetarium/libplanet/issues/1156
[#1192]: https://github.com/planetarium/libplanet/issues/1192
Expand Down Expand Up @@ -304,12 +308,14 @@ To be released.
[#1301]: https://github.com/planetarium/libplanet/issues/1301
[#1305]: https://github.com/planetarium/libplanet/pull/1305
[#1315]: https://github.com/planetarium/libplanet/issues/1315
[#1316]: https://github.com/planetarium/libplanet/issues/1316
[#1320]: https://github.com/planetarium/libplanet/issues/1320
[#1325]: https://github.com/planetarium/libplanet/pull/1325
[#1328]: https://github.com/planetarium/libplanet/pull/1328
[#1329]: https://github.com/planetarium/libplanet/pull/1329
[#1334]: https://github.com/planetarium/libplanet/pull/1334
[#1339]: https://github.com/planetarium/libplanet/issues/1339
[#1340]: https://github.com/planetarium/libplanet/pull/1340
[#1342]: https://github.com/planetarium/libplanet/pull/1342
[#1343]: https://github.com/planetarium/libplanet/pull/1343
[#1348]: https://github.com/planetarium/libplanet/pull/1348
Expand Down
209 changes: 188 additions & 21 deletions Libplanet.Extensions.Cocona.Tests/Commands/StoreCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@
using System.Collections.Immutable;
using System.IO;
using Cocona;
using Libplanet.Blocks;
using Libplanet.Crypto;
using Libplanet.Extensions.Cocona.Commands;
using Libplanet.RocksDBStore.Tests;
using Libplanet.Tests;
using Libplanet.Tests.Store;
using Libplanet.Tx;
using Xunit;

namespace Libplanet.Extensions.Cocona.Tests.Commands
{
public class StoreCommandTest : IDisposable
{
private readonly ImmutableArray<StoreFixture> _storeFixtures;
private readonly StoreCommand _command;
private readonly TextWriter _originalWriter;
private readonly Block<Utils.DummyAction> _genesisBlock;
private readonly Block<Utils.DummyAction> _block1;
private readonly Block<Utils.DummyAction> _block2;
private readonly Block<Utils.DummyAction> _block3;
private readonly Block<Utils.DummyAction> _block4;
private readonly Block<Utils.DummyAction> _block5;
private readonly Transaction<Utils.DummyAction> _transaction1;
private readonly Transaction<Utils.DummyAction> _transaction2;
private readonly Transaction<Utils.DummyAction> _transaction3;
private readonly Transaction<Utils.DummyAction> _transaction4;

public StoreCommandTest()
{
_command = new StoreCommand();
_originalWriter = Console.Out;

try
{
_storeFixtures = ImmutableArray.Create<StoreFixture>(
Expand All @@ -32,13 +45,40 @@ public StoreCommandTest()
throw new SkipException("RocksDB is not available.");
}

_genesisBlock = TestUtils.MineGenesis<Utils.DummyAction>();
_transaction1 = DummyTransaction();
_transaction2 = DummyTransaction();
_transaction3 = DummyTransaction();
_transaction4 = DummyTransaction();

_block1 = TestUtils.MineNext(_genesisBlock, new[] { _transaction1 });
_block2 = TestUtils.MineNext(_block1, new[] { _transaction2 });
_block3 = TestUtils.MineNext(_block2, new[] { _transaction3 });
_block4 = TestUtils.MineNext(_block3, new[] { _transaction3 });
_block5 = TestUtils.MineNext(_block4);

var guid = Guid.NewGuid();
foreach (var v in _storeFixtures)
{
var guid = Guid.NewGuid();
v.Store.SetCanonicalChainId(guid);
v.Store.PutBlock(v.Block1);
v.Store.AppendIndex(guid, v.Block1.Hash);
v.Store.PutTransaction(v.Transaction1);
v.Store.PutBlock(_genesisBlock);
v.Store.AppendIndex(guid, _genesisBlock.Hash);

v.Store.PutBlock(_block1);
v.Store.AppendIndex(guid, _block1.Hash);
v.Store.PutTransaction(_transaction1);

v.Store.PutBlock(_block2);
v.Store.AppendIndex(guid, _block2.Hash);
v.Store.PutTransaction(_transaction2);

v.Store.PutBlock(_block3);
v.Store.AppendIndex(guid, _block3.Hash);
v.Store.PutTransaction(_transaction3);

v.Store.PutBlock(_block4);
v.Store.AppendIndex(guid, _block4.Hash);

(v.Store as IDisposable)?.Dispose();
(v.StateStore as IDisposable)?.Dispose();
}
Expand All @@ -48,31 +88,146 @@ public StoreCommandTest()
public void TestInvalidArguments()
{
Assert.Throws<ArgumentException>(() =>
_command.BlockByHash(
new StoreCommand().BlockByHash(
"rocksdb+file+file://" + "/blah",
"dummy"
));
Assert.Throws<NotSupportedException>(() =>
_command.BlockByHash(
new StoreCommand().BlockByHash(
"rocksdb+memory://" + "/blah",
"dummy"
));
Assert.Throws<NotSupportedException>(() =>
_command.BlockByHash(
new StoreCommand().BlockByHash(
"leveldb://" + "/blah",
"dummy"
));
}

[SkippableFact]
public void TestBlockByTxIdNotExist()
{
foreach (var fx in _storeFixtures)
{
new StoreCommand().BuildIndexTxBlock(
fx.Scheme + fx.Path,
0,
10);
}

foreach (var fx in _storeFixtures)
{
Assert.Throws<CommandExitedException>(() =>
new StoreCommand().BlocksByTxId(
fx.Scheme + fx.Path,
_transaction4.Id.ToString()
));
}
}

[SkippableFact]
public void TestBlockByTxIdTwo()
{
foreach (var fx in _storeFixtures)
{
new StoreCommand().BuildIndexTxBlock(
fx.Scheme + fx.Path,
0,
10);
}

foreach (var fx in _storeFixtures)
{
using var sw = new StringWriter();
Console.SetOut(sw);
new StoreCommand().BlocksByTxId(
fx.Scheme + fx.Path,
_transaction3.Id.ToString()
);
var actual = sw.ToString();
var expected = Utils.SerializeHumanReadable(new[] { _block3, _block4 });
if (expected.TrimEnd() != actual.TrimEnd())
{
expected = Utils.SerializeHumanReadable(new[] { _block4, _block3 });
}

Assert.Equal(expected.TrimEnd(), actual.TrimEnd());
}
}

[SkippableFact]
public void TestBlockHashesByTxId()
{
foreach (var fx in _storeFixtures)
{
new StoreCommand().BuildIndexTxBlock(
fx.Scheme + fx.Path,
0,
10);
}

foreach (var fx in _storeFixtures)
{
using var sw = new StringWriter();
Console.SetOut(sw);
new StoreCommand().BlockHashesByTxId(
fx.Scheme + fx.Path,
_transaction3.Id.ToString()
);
var actual = sw.ToString();
var expected = Utils.SerializeHumanReadable(new[] { _block3.Hash, _block4.Hash });
if (expected.TrimEnd() != actual.TrimEnd())
{
expected = Utils.SerializeHumanReadable(new[] { _block4.Hash, _block3.Hash });
}

Assert.Equal(expected.TrimEnd(), actual.TrimEnd());
}
}

[SkippableFact]
public void TestBuildIndexTxBlockBlockByTxId()
{
foreach (var fx in _storeFixtures)
{
new StoreCommand().BuildIndexTxBlock(
fx.Scheme + fx.Path,
0,
10);
}

foreach (var fx in _storeFixtures)
{
void AssertTxBlockIndex(
Transaction<Utils.DummyAction> tx,
Block<Utils.DummyAction> block
)
{
using var sw = new StringWriter();
Console.SetOut(sw);
new StoreCommand().BlocksByTxId(
fx.Scheme + fx.Path,
tx.Id.ToString()
);
var actual = sw.ToString();
var expected = Utils.SerializeHumanReadable(new[] { block });
Assert.Equal(expected.TrimEnd(), actual.TrimEnd());
}

AssertTxBlockIndex(_transaction1, _block1);
AssertTxBlockIndex(_transaction2, _block2);
}
}

[SkippableFact]
public void TestBlockByHashNotExists()
{
foreach (var fx in _storeFixtures)
{
Assert.Throws<CommandExitedException>(() =>
_command.BlockByHash(
new StoreCommand().BlockByHash(
fx.Scheme + fx.Path,
fx.Block2.Hash.ToString())
_block5.Hash.ToString())
);
}
}
Expand All @@ -84,11 +239,11 @@ public void TestBlockByHash()
{
using var sw = new StringWriter();
Console.SetOut(sw);
_command.BlockByHash(
new StoreCommand().BlockByHash(
fx.Scheme + fx.Path,
fx.Block1.Hash.ToString());
_block1.Hash.ToString());
var actual = sw.ToString();
var expected = Utils.SerializeHumanReadable(fx.Block1);
var expected = Utils.SerializeHumanReadable(_block1);
Assert.Equal(expected.TrimEnd(), actual.TrimEnd());
}
}
Expand All @@ -99,7 +254,7 @@ public void TestBlockByIndexNotExists()
foreach (var fx in _storeFixtures)
{
Assert.Throws<CommandExitedException>(() =>
_command.BlockByIndex(
new StoreCommand().BlockByIndex(
fx.Scheme + fx.Path,
9999999 * 100
)
Expand All @@ -114,9 +269,9 @@ public void TestBlockByIndex()
{
using var sw = new StringWriter();
Console.SetOut(sw);
_command.BlockByIndex(fx.Scheme + fx.Path, 0);
new StoreCommand().BlockByIndex(fx.Scheme + fx.Path, 0);
var actual = sw.ToString();
var expected = Utils.SerializeHumanReadable(fx.Block1);
var expected = Utils.SerializeHumanReadable(_genesisBlock);
Assert.Equal(expected.TrimEnd(), actual.TrimEnd());
}
}
Expand All @@ -127,7 +282,7 @@ public void TestTxByIdNotExists()
foreach (var fx in _storeFixtures)
{
Assert.Throws<CommandExitedException>(() =>
_command.TxById(
new StoreCommand().TxById(
fx.Scheme + fx.Path,
fx.Transaction2.Id.ToString())
);
Expand All @@ -141,11 +296,11 @@ public void TestTxById()
{
using var sw = new StringWriter();
Console.SetOut(sw);
_command.TxById(
new StoreCommand().TxById(
fx.Scheme + fx.Path,
fx.Transaction1.Id.ToString());
_transaction1.Id.ToString());
var actual = sw.ToString();
var expected = Utils.SerializeHumanReadable(fx.Transaction1);
var expected = Utils.SerializeHumanReadable(_transaction1);
Assert.Equal(expected.TrimEnd(), actual.TrimEnd());
}
}
Expand All @@ -154,5 +309,17 @@ public void Dispose()
{
Console.SetOut(_originalWriter);
}

private Transaction<Utils.DummyAction> DummyTransaction()
{
return Transaction<Utils.DummyAction>.Create(
0,
new PrivateKey(),
_genesisBlock.Hash,
new[] { new Utils.DummyAction() },
null,
DateTimeOffset.UtcNow
);
}
}
}
Loading

0 comments on commit 77d8785

Please sign in to comment.