Skip to content

Commit

Permalink
Merge pull request #3432 from planetarium/migrate/iaccount-to-itrie
Browse files Browse the repository at this point in the history
Migrate to `ITrie`
  • Loading branch information
greymistcube authored Sep 25, 2023
2 parents 7235c0f + 6e8bf54 commit f6a36ea
Show file tree
Hide file tree
Showing 53 changed files with 1,618 additions and 1,121 deletions.
28 changes: 28 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,37 @@ To be released.

- Added `IBlockChainStates.GetAccountState(HashDigest<SHA256>?)`
interface method. [[#3425]]
- Removed `TxFailure.ExceptionMetadata` property. [[#3428]]
- Removed `ISerializable` interface from `TxExecution`, `TxSuccess`,
and `TxFailure`. [[#3428]]
- Removed `TxSuccess` and `TxFailure` class. [[#3429]]
- Changed `TxExecution` class as `sealed` from `abstract.` [[#3429]]
- All properties of `TxExecution` except `BlockHash` and `TxId` were
overhauled. [[#3429]]
- (Libplanet.Store) Removed `IStore.PutTxExecution(TxSuccess)` and
`IStore.PutTxExecution(TxFailure)`;
added `IStore.PutTxExecution(TxExecution)`. [[#3429]]
- (Libplanet.Explorer) Removed `TxResult.ExceptionName` of type `string?`
and added `TxResult.ExceptionNames` of type `List<string?>?`. [[#3429]]
- (Libplanet.Explorer) Removed `TxResult.UpdatedStates` and
`TxResult.UpdatedFungibleAssets`. [[#3429]]
- Changed `IActionRenderer.RenderAction(IValue, IActionContext, IAccount)`
to `IActionRenderer.RenderAction(IValue, ICommittedActionContext,
HashDigest<SHA256>)`. [[#3431]]
- Changed `IActionRenderer.RenderActionError(IValue, IActionContext,
Exception)` to `IActionRenderer.RenderActionError(IValue,
ICommittedActionContext, Exception)`. [[#3431]]

### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes

### Added APIs

- Added `AccountDiff` class. [[#3424]]
- Added `ICommittedActionContext` interface. [[#3431]]
- Added `ICommittedActionEvaluation` interface. [[#3431]]

### Behavioral changes

### Bug fixes
Expand All @@ -27,7 +51,11 @@ To be released.

### CLI tools

[#3424]: https://github.com/planetarium/libplanet/pull/3424
[#3425]: https://github.com/planetarium/libplanet/pull/3425
[#3428]: https://github.com/planetarium/libplanet/pull/3428
[#3429]: https://github.com/planetarium/libplanet/pull/3429
[#3431]: https://github.com/planetarium/libplanet/pull/3431


Version 3.3.1
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Action.Tests/Sys/InitializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public void Execute()
validatorSet: _validatorSet
);

var nextStates = initialize.Execute(context);
var nextState = initialize.Execute(context);

Assert.Equal(_validatorSet, nextStates.GetValidatorSet());
Assert.Equal(_states[default], nextStates.GetState(default));
Assert.Equal(_validatorSet, nextState.GetValidatorSet());
Assert.Equal(_states[default], nextState.GetState(default));
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions Libplanet.Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public ActionContext(
/// <inheritdoc cref="IActionContext.UseGas(long)"/>
public void UseGas(long gas) => GetGasMeter.Value?.UseGas(gas);

/// <inheritdoc cref="IActionContext.GetUnconsumedContext"/>
[Pure]
public IActionContext GetUnconsumedContext() =>
new ActionContext(
Expand Down
96 changes: 96 additions & 0 deletions Libplanet.Action/CommittedActionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Diagnostics.Contracts;
using System.Security.Cryptography;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Tx;

namespace Libplanet.Action
{
public class CommittedActionContext : ICommittedActionContext
{
public CommittedActionContext(IActionContext context)
: this(
signer: context.Signer,
txId: context.TxId,
miner: context.Miner,
blockIndex: context.BlockIndex,
blockProtocolVersion: context.BlockProtocolVersion,
rehearsal: context.Rehearsal,
previousState: context.PreviousState.Trie.Hash,
random: context.Random,
blockAction: context.BlockAction)
{
}

public CommittedActionContext(
Address signer,
TxId? txId,
Address miner,
long blockIndex,
int blockProtocolVersion,
bool rehearsal,
HashDigest<SHA256> previousState,
IRandom random,
bool blockAction)
{
Signer = signer;
TxId = txId;
Miner = miner;
BlockIndex = blockIndex;
BlockProtocolVersion = blockProtocolVersion;
Rehearsal = rehearsal;
PreviousState = previousState;
Random = random;
BlockAction = blockAction;
}

/// <inheritdoc cref="ICommittedActionContext.Signer"/>
[Pure]
public Address Signer { get; }

/// <inheritdoc cref="ICommittedActionContext.TxId"/>
[Pure]
public TxId? TxId { get; }

/// <inheritdoc cref="ICommittedActionContext.Miner"/>
[Pure]
public Address Miner { get; }

/// <inheritdoc cref="ICommittedActionContext.BlockIndex"/>
[Pure]
public long BlockIndex { get; }

/// <inheritdoc cref="ICommittedActionContext.BlockProtocolVersion"/>
[Pure]
public int BlockProtocolVersion { get; }

/// <inheritdoc cref="ICommittedActionContext.Rehearsal"/>
[Pure]
public bool Rehearsal { get; }

/// <inheritdoc cref="ICommittedActionContext.PreviousState"/>
[Pure]
public HashDigest<SHA256> PreviousState { get; }

/// <inheritdoc cref="ICommittedActionContext.Random"/>
public IRandom Random { get; }

/// <inheritdoc cref="ICommittedActionContext.BlockAction"/>
[Pure]
public bool BlockAction { get; }

/// <inheritdoc cref="ICommittedActionContext.Copy"/>
[Pure]
public ICommittedActionContext Copy() =>
new CommittedActionContext(
Signer,
TxId,
Miner,
BlockIndex,
BlockProtocolVersion,
Rehearsal,
PreviousState,
new Random(Random.Seed),
BlockAction);
}
}
60 changes: 60 additions & 0 deletions Libplanet.Action/CommittedActionEvaluation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Common;

namespace Libplanet.Action
{
/// <summary>
/// A record type to represent an evaluation plan and result of
/// a single action.
/// </summary>
public class CommittedActionEvaluation : ICommittedActionEvaluation
{
/// <summary>
/// Creates an <see cref="CommittedActionEvaluation"/> instance
/// with filling properties.
/// </summary>
/// <param name="action">An action to evaluate.</param>
/// <param name="inputContext">An input <see cref="IActionContext"/> to
/// evaluate <paramref name="action"/>.</param>
/// <param name="outputState">The result states that
/// <paramref name="action"/> makes.</param>
/// <param name="exception">An exception that has risen during evaluating a given
/// <paramref name="action"/>.</param>
public CommittedActionEvaluation(
IValue action,
ICommittedActionContext inputContext,
HashDigest<SHA256> outputState,
Exception? exception = null)
{
Action = action;
InputContext = inputContext;
OutputState = outputState;
Exception = exception;
}

/// <summary>
/// An action to evaluate.
/// </summary>
public IValue Action { get; }

/// <summary>
/// An input <see cref="ICommittedActionContext"/> to evaluate
/// <see cref="Action"/>.
/// </summary>
/// <remarks>Its <see cref="ICommittedActionContext.Random"/> property
/// is not consumed yet.</remarks>
public ICommittedActionContext InputContext { get; }

/// <summary>
/// The result states that <see cref="Action"/> makes.
/// </summary>
public HashDigest<SHA256> OutputState { get; }

/// <summary>
/// An exception that had risen during evaluation.
/// </summary>
public Exception? Exception { get; }
}
}
88 changes: 88 additions & 0 deletions Libplanet.Action/ICommittedActionContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Diagnostics.Contracts;
using System.Security.Cryptography;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;

namespace Libplanet.Action
{
/// <summary>
/// Contextual data determined by a transaction and a block for rendering.
/// </summary>
public interface ICommittedActionContext
{
/// <summary>
/// The <see cref="Transaction.Signer"/> of the <see cref="Transaction"/> that contains
/// the <see cref="IAction"/> to be executed. If the <see cref="IAction"/> is
/// not part of a <see cref="Transaction"/>, e.g. <see cref="IBlockPolicy.BlockAction"/>,
/// this is set to <see cref="Block.Miner"/> instead.
/// </summary>
[Pure]
Address Signer { get; }

/// <summary>
/// The <see cref="Transaction.Id"/> of the <see cref="Transaction"/> that contains
/// the <see cref="IAction"/>. If the <see cref="IAction"/> is not part of
/// a <see cref="Transaction"/>, e.g. <see cref="IBlockPolicy.BlockAction"/>,
/// this is set to <see langword="null"/>.
/// </summary>
[Pure]
TxId? TxId { get; }

/// <summary>
/// The <see cref="Block.Miner"/> of the <see cref="Block"/> that contains
/// the <see cref="IAction"/>.
/// </summary>
[Pure]
Address Miner { get; }

/// <summary>
/// The <see cref="Block.Index"/> of the <see cref="Block"/> that contains
/// the <see cref="IAction"/>.
/// </summary>
[Pure]
long BlockIndex { get; }

/// <summary>
/// The <see cref="Block.ProtocolVersion"/> of the <see cref="Block"/> that contains
/// the <see cref="IAction"/>.
/// </summary>
[Pure]
int BlockProtocolVersion { get; }

/// <summary>
/// Whether an <see cref="IAction"/> is being executed during
/// &#x201c;rehearsal mode&#x201d;, that there is nothing
/// in <see cref="PreviousState"/>.
/// </summary>
[Pure]
bool Rehearsal { get; }

/// <summary>
/// The state root hash of the previous state.
/// </summary>
[Pure]
HashDigest<SHA256> PreviousState { get; }

/// <summary>
/// An initialized pseudorandom number generator. Its seed (state)
/// is determined by a block and a transaction, which is
/// deterministic so that every node can replay the same action and
/// then reproduce the same result, while neither a single block miner
/// nor a single transaction signer can predict the result and cheat.
/// </summary>
/// <returns>A random object that shares interface mostly equivalent
/// to <see cref="System.Random"/>.</returns>
IRandom Random { get; }

/// <summary>
/// Whether this action is executed as a block action.
/// <see langword="false"/> if it belongs to a transaction.
/// </summary>
[Pure]
bool BlockAction { get; }

ICommittedActionContext Copy();
}
}
35 changes: 35 additions & 0 deletions Libplanet.Action/ICommittedActionEvaluation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Common;

namespace Libplanet.Action
{
public interface ICommittedActionEvaluation
{
/// <summary>
/// An action data to evaluate. When the
/// <see cref="InputContext"/>.<see cref="IActionContext.BlockAction"/> is true,
/// use <see cref="IBlockPolicy.BlockAction"/> instead of trying deserialization.
/// </summary>
public IValue Action { get; }

/// <summary>
/// An input <see cref="IActionContext"/> to evaluate
/// <see cref="Action"/>.
/// </summary>
/// <remarks>Its <see cref="IActionContext.Random"/> property
/// is not consumed yet.</remarks>
public ICommittedActionContext InputContext { get; }

/// <summary>
/// The result states that <see cref="Action"/> makes.
/// </summary>
public HashDigest<SHA256> OutputState { get; }

/// <summary>
/// An exception that had risen during evaluation.
/// </summary>
public Exception? Exception { get; }
}
}
3 changes: 0 additions & 3 deletions Libplanet.Action/Random.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#nullable disable
using System;

namespace Libplanet.Action
{
internal class Random : System.Random, IRandom
Expand Down
Loading

0 comments on commit f6a36ea

Please sign in to comment.