Skip to content

Commit

Permalink
Merge pull request #3520 from greymistcube/refactor/trie-based-account
Browse files Browse the repository at this point in the history
♻️ `ITrie` based `IAccount`
  • Loading branch information
greymistcube authored Dec 5, 2023
2 parents 5ffe61d + 595f02d commit e9cc22e
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 428 deletions.
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ be compatible with this version, specifically, those that ran with

### Backward-incompatible API changes

- (Libplanet.Action) Removed unnecessary extension methods: [[#3520]]
- `IReadOnlyList<IActionEvaluation>.GetRawTotalDelta()`
- `IReadOnlyList<IAccountDelta>.OrderedSum()`
- `IAccountDelta.ToRawDelta()`
- `IAccount.GetUpdatedStates()`
- `IAccount.GetUpdatedBalances()`
- `IAccount.GetUpdatedTotalSupplies()`
- (Libplanet.Action) Changed `IAccount` to no longer track `IAccountDelta`.
[[#3520]]
- (Libplanet.Action) Removed `IAccountDelta` as parameter for constructors
of `Account` class. [[#3520]]
- (Libplanet.Action) Removed `hashedSignature` of type `byte[]` parameter
from `ActionEvaluator.GenerateRandomSeed()`. [[#3523]]
- Changed `TxInvoice` to no longer allow having the null-ness of
Expand All @@ -35,6 +46,7 @@ be compatible with this version, specifically, those that ran with

### CLI tools

[#3520]: https://github.com/planetarium/libplanet/pull/3520
[#3523]: https://github.com/planetarium/libplanet/pull/3523
[#3529]: https://github.com/planetarium/libplanet/pull/3529
[Libplanet 2.0.0]: https://www.nuget.org/packages/Libplanet/2.0.0
Expand Down
35 changes: 0 additions & 35 deletions Libplanet.Action.Tests/ActionEvaluationExtensions.cs

This file was deleted.

22 changes: 0 additions & 22 deletions Libplanet.Action/ActionEvaluationsExtensions.cs

This file was deleted.

73 changes: 37 additions & 36 deletions Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,39 +148,18 @@ public IReadOnlyList<ICommittedActionEvaluation> Evaluate(
/// <param name="previousState">The states immediately before <paramref name="actions"/>
/// being executed.</param>
/// <param name="actions">Actions to evaluate.</param>
/// <param name="stateStore">An <see cref="IStateStore"/> to use.</param>
/// <param name="logger">An optional logger.</param>
/// <returns>An enumeration of <see cref="ActionEvaluation"/>s for each
/// <see cref="IAction"/> in <paramref name="actions"/>.
/// </returns>
/// <remarks>
/// <para>Each <see cref="IActionContext.Random"/> object has an unconsumed state.</para>
/// <para>
/// The returned enumeration has the following properties:
/// <list type="bullet">
/// <item><description>
/// The first <see cref="ActionEvaluation"/> in the enumerated result,
/// if any, has <see cref="ActionEvaluation.OutputState"/> with
/// <see cref="IAccount.Delta"/> that is a
/// "superset" of <paramref name="previousState"/>'s
/// <see cref="IAccount.Delta"/> (possibly except for
/// <see cref="IAccountDelta.ValidatorSet"/>).
/// </description></item>
/// <item><description>
/// Each <see cref="ActionEvaluation"/> in the enumerated result
/// has <see cref="ActionEvaluation.OutputState"/> with
/// <see cref="IAccount.Delta"/> that is a "superset"
/// of the previous one, if any (possibly except for
/// <see cref="IAccountDelta.ValidatorSet"/>).
/// </description></item>
/// </list>
/// </para>
/// </remarks>
[Pure]
internal static IEnumerable<ActionEvaluation> EvaluateActions(
IPreEvaluationBlockHeader blockHeader,
ITransaction? tx,
IAccount previousState,
IImmutableList<IAction> actions,
IStateStore stateStore,
ILogger? logger = null)
{
IActionContext CreateActionContext(
Expand Down Expand Up @@ -214,6 +193,7 @@ IActionContext CreateActionContext(
tx,
context,
action,
stateStore,
logger);

yield return result.Evaluation;
Expand All @@ -233,9 +213,18 @@ internal static (ActionEvaluation Evaluation, long NextGasLimit) EvaluateAction(
ITransaction? tx,
IActionContext context,
IAction action,
IStateStore stateStore,
ILogger? logger = null)
{
if (!context.PreviousState.Trie.Recorded)
{
throw new InvalidOperationException(
$"Given {nameof(context)} must have its previous state's " +
$"{nameof(ITrie)} recorded.");
}

IActionContext inputContext = context;

IAccount state = inputContext.PreviousState;
Exception? exc = null;
IFeeCollector feeCollector = new FeeCollector(context, tx?.MaxGasPrice);
Expand Down Expand Up @@ -324,6 +313,18 @@ IActionContext CreateActionContext(IAccount newPrevState)

state = feeCollector.Refund(state);
state = feeCollector.Reward(state);
state = state is Account a
? new Account(
new AccountState(stateStore.Commit(a.Trie)),
a.TotalUpdatedFungibles)
: throw new InvalidOperationException(
$"Internal {nameof(IAccount)} is not of valid type: {state.GetType()}");

if (!state.Trie.Recorded)
{
throw new InvalidOperationException(
$"Failed to record {nameof(IAccount)}'s {nameof(ITrie)}.");
}

return (
new ActionEvaluation(
Expand Down Expand Up @@ -394,7 +395,6 @@ internal IEnumerable<ActionEvaluation> EvaluateBlock(
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
delta = Account.Flush(delta);

IEnumerable<ActionEvaluation> evaluations = EvaluateTx(
blockHeader: block,
Expand Down Expand Up @@ -441,6 +441,7 @@ internal IEnumerable<ActionEvaluation> EvaluateTx(
tx: tx,
previousState: previousState,
actions: actions,
stateStore: _stateStore,
logger: _logger);
}

Expand Down Expand Up @@ -477,7 +478,9 @@ internal ActionEvaluation EvaluatePolicyBlockAction(
blockHeader: blockHeader,
tx: null,
previousState: previousState,
actions: new[] { policyBlockAction }.ToImmutableList()).Single();
actions: new[] { policyBlockAction }.ToImmutableList(),
stateStore: _stateStore,
logger: _logger).Single();
}

internal IAccount PrepareInitialDelta(HashDigest<SHA256>? stateRootHash)
Expand All @@ -500,14 +503,7 @@ internal IReadOnlyList<ICommittedActionEvaluation>
int setCount = 0;
foreach (var evaluation in evaluations)
{
ITrie nextTrie = trie;
foreach (var kv in evaluation.OutputState.Delta.ToRawDelta())
{
nextTrie = nextTrie.Set(kv.Key, kv.Value);
setCount++;
}

nextTrie = _stateStore.Commit(nextTrie);
#pragma warning disable SA1118
var committedEvaluation = new CommittedActionEvaluation(
action: evaluation.Action,
inputContext: new CommittedActionContext(
Expand All @@ -516,14 +512,19 @@ internal IReadOnlyList<ICommittedActionEvaluation>
miner: evaluation.InputContext.Miner,
blockIndex: evaluation.InputContext.BlockIndex,
blockProtocolVersion: evaluation.InputContext.BlockProtocolVersion,
previousState: trie.Hash,
previousState: evaluation.InputContext.PreviousState.Trie.Recorded
? evaluation.InputContext.PreviousState.Trie.Hash
: throw new ArgumentException("Trie is not recorded"),
randomSeed: evaluation.InputContext.RandomSeed,
blockAction: evaluation.InputContext.BlockAction),
outputState: nextTrie.Hash,
outputState: evaluation.OutputState.Trie.Recorded
? evaluation.OutputState.Trie.Hash
: throw new ArgumentException("Trie is not recorded"),
exception: evaluation.Exception);
committedEvaluations.Add(committedEvaluation);
#pragma warning restore SA1118

trie = nextTrie;
trie = evaluation.OutputState.Trie;
}

_logger
Expand Down
Loading

0 comments on commit e9cc22e

Please sign in to comment.