Skip to content

Commit

Permalink
Merge pull request #3547 from OnedgeLee/port/3.9.1-to-4.0.0
Browse files Browse the repository at this point in the history
🔀 Port 3.9.1 to 4.0.0
  • Loading branch information
OnedgeLee authored Dec 12, 2023
2 parents 7480b13 + 6e81f26 commit d6e2887
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 206 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ To be released.
[#3540]: https://github.com/planetarium/libplanet/pull/3540


Version 3.9.1
-------------

Released on December 8, 2023.

- (Libplanet.Explorer) Added `INCLUDED` to `TxStatus` enum. [[#3542]]

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


Version 3.9.0
-------------

Expand Down
201 changes: 69 additions & 132 deletions Libplanet.Explorer.Tests/GeneratedBlockChainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,50 @@ public class GeneratedBlockChainFixture

public ImmutableArray<PrivateKey> PrivateKeys { get; }

public int MaxTxCount { get; }

public ImmutableDictionary<Address, ImmutableArray<Block>>
MinedBlocks { get; private set; }

public ImmutableDictionary<
Address,
ImmutableArray<Transaction>>
public ImmutableDictionary<Address, ImmutableArray<Transaction>>
SignedTxs { get; private set; }

public ImmutableDictionary<
Address,
ImmutableArray<Transaction>>
InvolvedTxs { get; private set; }
private Random Random { get; }

public GeneratedBlockChainFixture(
int seed,
int blockCount = 20,
int maxTxCount = 20,
int privateKeyCount = 10,
ImmutableArray<ImmutableArray<ImmutableArray<SimpleAction>>>?
txActionsForPrefixBlocks = null,
ImmutableArray<ImmutableArray<ImmutableArray<SimpleAction>>>?
txActionsForSuffixBlocks = null)
{
var random = new System.Random(seed);
txActionsForSuffixBlocks ??=
ImmutableArray<ImmutableArray<ImmutableArray<SimpleAction>>>.Empty;

var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
PrivateKeys = Enumerable.Range(0, privateKeyCount)
.Aggregate(ImmutableArray<PrivateKey>.Empty, (arr, _) => arr.Add(new PrivateKey()));
MinedBlocks = PrivateKeys.Aggregate(
ImmutableDictionary<Address, ImmutableArray<Block>>
.Empty,
(dict, pk) =>
dict.SetItem(
pk.Address,
ImmutableArray<Block>.Empty));
SignedTxs = PrivateKeys.Aggregate(
ImmutableDictionary<
Address,
ImmutableArray<Transaction>>.Empty,
(dict, pk) =>
dict.SetItem(
pk.Address,
ImmutableArray<Transaction>.Empty));
InvolvedTxs = PrivateKeys.Aggregate(
ImmutableDictionary<
Address,
ImmutableArray<Transaction>>.Empty,
(dict, pk) =>
dict.SetItem(
pk.Address,
ImmutableArray<Transaction>.Empty));

Random = new Random(seed);
MaxTxCount = maxTxCount;
PrivateKeys = Enumerable
.Range(0, privateKeyCount)
.Select(_ => new PrivateKey())
.ToImmutableArray();
MinedBlocks = PrivateKeys
.ToImmutableDictionary(
key => key.Address,
key => ImmutableArray<Block>.Empty);
SignedTxs = PrivateKeys
.ToImmutableDictionary(
key => key.Address,
key => ImmutableArray<Transaction>.Empty);

var privateKey = new PrivateKey();
var policy = new BlockPolicy(
blockInterval: TimeSpan.FromMilliseconds(1),
getMaxTransactionsPerBlock: _ => int.MaxValue,
getMaxTransactionsBytes: _ => long.MaxValue);
IStore store = new MemoryStore();
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
stateStore,
Expand Down Expand Up @@ -112,135 +99,88 @@ public GeneratedBlockChainFixture(
stateStore,
genesisBlock,
actionEvaluator);

MinedBlocks = MinedBlocks.SetItem(
Chain.Genesis.Miner,
ImmutableArray<Block>.Empty.Add(Chain.Genesis));

if (txActionsForPrefixBlocks is { } txActionsForPrefixBlocksVal)
{
foreach (var actionsForTransactions in txActionsForPrefixBlocksVal)
{
var pk = PrivateKeys[random.Next(PrivateKeys.Length)];
AddBlock(
random.Next(),
actionsForTransactions.Select(actions =>
Transaction.Create(
Chain.GetNextTxNonce(pk.Address),
pk,
Chain.Genesis.Hash,
actions.ToPlainValues()))
.ToImmutableArray());
}
}

while (Chain.Count < blockCount + (txActionsForPrefixBlocks?.Length ?? 0) + 1)
while (Chain.Count < blockCount)
{
AddBlock(
random.Next(),
GetRandomTransactions(random.Next(), maxTxCount, Chain.Count == 1));
AddBlock(GetRandomTransactions());
}

if (txActionsForSuffixBlocks is { } txActionsForSuffixBlocksVal)
{
foreach (var actionsForTransactions in txActionsForSuffixBlocksVal)
{
var pk = PrivateKeys[random.Next(PrivateKeys.Length)];
AddBlock(
random.Next(),
actionsForTransactions.Select(actions =>
Transaction.Create(
Chain.GetNextTxNonce(pk.Address),
pk,
Chain.Genesis.Hash,
actions.ToPlainValues()))
.ToImmutableArray());
var pk = PrivateKeys[Random.Next(PrivateKeys.Length)];
AddBlock(actionsForTransactions
.Select(actions =>
Transaction.Create(
nonce: Chain.GetNextTxNonce(pk.Address),
privateKey: pk,
genesisHash: Chain.Genesis.Hash,
actions: actions.ToPlainValues()))
.ToImmutableArray());
}
}
}

private ImmutableArray<Transaction> GetRandomTransactions(
int seed, int maxCount, bool giveMax = false)
private ImmutableArray<Transaction> GetRandomTransactions()
{
var random = new System.Random(seed);
var nonces = ImmutableDictionary<PrivateKey, long>.Empty;
return Enumerable.Range(0, giveMax ? maxCount : random.Next(maxCount))
.Aggregate(
ImmutableArray<Transaction>.Empty,
(arr, _) =>
return Enumerable
.Range(0, Random.Next(MaxTxCount))
.Select(_ =>
{
var pk = PrivateKeys[Random.Next(PrivateKeys.Length)];
if (!nonces.TryGetValue(pk, out var nonce))
{
var pk = PrivateKeys[random.Next(PrivateKeys.Length)];
if (!nonces.TryGetValue(pk, out var nonce))
{
nonce = Chain.GetNextTxNonce(pk.Address);
}
nonce = Chain.GetNextTxNonce(pk.Address);
}

nonces = nonces.SetItem(pk, nonce + 1);
nonces = nonces.SetItem(pk, nonce + 1);

return arr.Add(GetRandomTransaction(random.Next(), pk, nonce));
})
return GetRandomTransaction(pk, nonce);
})
.OrderBy(tx => tx.Id)
.ToImmutableArray();
}

private Transaction
GetRandomTransaction(int seed, PrivateKey pk, long nonce)
private Transaction GetRandomTransaction(PrivateKey pk, long nonce)
{
var random = new System.Random(seed);
var addr = pk.Address;
var bal = (int)(Chain.GetBalance(
addr, TestCurrency, ReservedAddresses.LegacyAccount).MajorUnit & int.MaxValue);
return
new Transaction(
new UnsignedTx(
new TxInvoice(
genesisHash: Chain.Genesis.Hash,
updatedAddresses: GetRandomAddresses(random.Next()),
timestamp: DateTimeOffset.UtcNow,
actions: new TxActionList(random.Next() % 2 == 0
? GetRandomActions(random.Next()).ToPlainValues()
: ImmutableHashSet<SimpleAction>.Empty.ToPlainValues()),
maxGasPrice: null,
gasLimit: null),
new TxSigningMetadata(pk.PublicKey, nonce)),
pk);
return Transaction.Create(
nonce: nonce,
privateKey: pk,
genesisHash: Chain.Genesis.Hash,
actions: Random.Next() % 2 == 0
? GetRandomActions().ToPlainValues()
: ImmutableHashSet<SimpleAction>.Empty.ToPlainValues(),
maxGasPrice: null,
gasLimit: null);
}

private ImmutableArray<SimpleAction> GetRandomActions(int seed)
private ImmutableArray<SimpleAction> GetRandomActions()
{
var random = new System.Random(seed);
return Enumerable.Range(0, random.Next(10))
.Aggregate(
ImmutableArray<SimpleAction>.Empty,
(arr, _) => arr.Add(SimpleAction.GetAction(random.Next())));
}

private IImmutableSet<Address> GetRandomAddresses(int seed)
{
var random = new System.Random(seed);
return Enumerable.Range(0, random.Next(PrivateKeys.Length - 1) + 1)
.Aggregate(
ImmutableHashSet<Address>.Empty,
(arr, _) => arr.Add(PrivateKeys[random.Next(PrivateKeys.Length)].Address));
return Enumerable
.Range(0, Random.Next(10))
.Select(_ => SimpleAction.GetAction(Random.Next()))
.ToImmutableArray();
}

private void AddBlock(
int seed,
ImmutableArray<Transaction> transactions)
private void AddBlock(ImmutableArray<Transaction> transactions)
{
var random = new System.Random(seed);
var pk = PrivateKeys[random.Next(PrivateKeys.Length)];
var proposer = PrivateKeys[Random.Next(PrivateKeys.Length)];
var block = Chain.EvaluateAndSign(
new BlockContent(
new BlockMetadata(
Chain.Tip.Index + 1,
DateTimeOffset.UtcNow,
pk.PublicKey,
proposer.PublicKey,
Chain.Tip.Hash,
BlockContent.DeriveTxHash(transactions),
Chain.Store.GetChainBlockCommit(Chain.Store.GetCanonicalChainId()!.Value)),
transactions).Propose(),
pk);
proposer);
Chain.Append(
block,
new BlockCommit(
Expand All @@ -256,8 +196,10 @@ private void AddBlock(
DateTimeOffset.UtcNow,
pk.PublicKey,
VoteFlag.PreCommit).Sign(pk)).ToImmutableArray()));
MinedBlocks =
MinedBlocks.SetItem(pk.Address, MinedBlocks[pk.Address].Add(block));
MinedBlocks = MinedBlocks
.SetItem(
proposer.Address,
MinedBlocks[proposer.Address].Add(block));
SignedTxs = transactions.Aggregate(
SignedTxs,
(dict, tx) =>
Expand All @@ -267,10 +209,5 @@ private void AddBlock(
.Add(tx)
.OrderBy(tx => tx.Nonce)
.ToImmutableArray()));
InvolvedTxs = transactions.Aggregate(
InvolvedTxs,
(dict, tx) => tx.UpdatedAddresses.Aggregate(
dict,
(dict, addr) => dict.SetItem(addr, dict[addr].Add(tx))));
}
}
28 changes: 23 additions & 5 deletions Libplanet.Explorer.Tests/GraphTypes/TxResultTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public static IEnumerable<object[]> TestCases() {
["txStatus"] = "SUCCESS",
["blockIndex"] = 0L,
["blockHash"] = "45bcaa4c0b00f4f31eb61577e595ea58fb69c7df3ee612aa6eea945bbb0ce39d",
["inputState"] =
["inputState"] =
"7146ddfb3594089795f6992a668a3ce7fde089aacdda68075e1bc37b14ebb06f",
["outputState"] =
["outputState"] =
"72bb2e17da644cbca9045f5e689fae0323b6af56a0acab9fd828d2243b50df1c",
["exceptionNames"] = new string[] { "" },
}
Expand All @@ -71,16 +71,34 @@ public static IEnumerable<object[]> TestCases() {
"7146ddfb3594089795f6992a668a3ce7fde089aacdda68075e1bc37b14ebb06f"),
HashDigest<SHA256>.FromString(
"7146ddfb3594089795f6992a668a3ce7fde089aacdda68075e1bc37b14ebb06f"),
new List<string>() { "" }
new List<string>() { "SomeException" }
),
new Dictionary<string, object> {
["txStatus"] = "FAILURE",
["blockIndex"] = 0L,
["inputState"] =
["inputState"] =
"7146ddfb3594089795f6992a668a3ce7fde089aacdda68075e1bc37b14ebb06f",
["outputState"] =
["outputState"] =
"7146ddfb3594089795f6992a668a3ce7fde089aacdda68075e1bc37b14ebb06f",
["blockHash"] = "45bcaa4c0b00f4f31eb61577e595ea58fb69c7df3ee612aa6eea945bbb0ce39d",
["exceptionNames"] = new string[] { "SomeException" },
}
},
new object[] {
new TxResult(
TxStatus.INCLUDED,
0,
"45bcaa4c0b00f4f31eb61577e595ea58fb69c7df3ee612aa6eea945bbb0ce39d",
null,
null,
new List<string>() { "" }
),
new Dictionary<string, object> {
["txStatus"] = "INCLUDED",
["blockIndex"] = 0L,
["blockHash"] = "45bcaa4c0b00f4f31eb61577e595ea58fb69c7df3ee612aa6eea945bbb0ce39d",
["inputState"] = null,
["outputState"] = null,
["exceptionNames"] = new string[] { "" },
}
},
Expand Down
Loading

0 comments on commit d6e2887

Please sign in to comment.