Skip to content

Commit

Permalink
Merge pull request #3739 from greymistcube/refactor/iworld-state
Browse files Browse the repository at this point in the history
♻️ Added `Version` property to `IWorldState`
  • Loading branch information
greymistcube authored Apr 12, 2024
2 parents 24fe16c + ae378c3 commit 24e8a38
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ To be released.
`BigInteger` to `int`. [[#3738]]
- (Libplanet.Store) Changed `TrieMetadata` to throw an `ArgumentException`
when trying to create an instance with an invalid version. [[#3738]]
- (Libplanet.Action) Added `IWorldState.Version` interface property.
[[#3739]]

### Backward-incompatible network protocol changes

Expand All @@ -38,6 +40,7 @@ To be released.
[#3735]: https://github.com/planetarium/libplanet/pull/3735
[#3736]: https://github.com/planetarium/libplanet/pull/3736
[#3738]: https://github.com/planetarium/libplanet/pull/3738
[#3739]: https://github.com/planetarium/libplanet/pull/3739


Version 4.3.0
Expand Down
18 changes: 17 additions & 1 deletion Libplanet.Action/State/IWorldState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics.Contracts;
using Libplanet.Crypto;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;

namespace Libplanet.Action.State
{
Expand Down Expand Up @@ -35,10 +36,25 @@ public interface IWorldState
public ITrie Trie { get; }

/// <summary>
/// Whether <see cref="IWorldState"/> is in legacy state or not.
/// Whether <see cref="IWorldState"/> is backed by an <see cref="ITrie"/> with
/// the legacy data model. This is <see langword="true"/> if and only if
/// <see cref="Version"/> is less than
/// <see cref="BlockMetadata.WorldStateProtocolVersion"/>.
/// </summary>
bool Legacy { get; }

/// <summary>
/// The version of the backend <see cref="ITrie"/> data model. This should be in sync with
/// <see cref="IBlockMetadata.ProtocolVersion"/> of the <see cref="IPreEvaluationBlock"/>
/// getting evaluated. As this was only introduced since
/// <see cref="BlockMetadata.WorldStateProtocolVersion"/>, this returns
/// zero with any <see cref="ITrie"/> representing an <see cref="IWorldState"/> before
/// <see cref="BlockMetadata.WorldStateProtocolVersion"/> or any <see cref="ITrie"/>
/// representing an <see cref="IAccountState"/>.
/// </summary>
/// <seealso cref="TrieMetadata.Version"/>
int Version { get; }

/// <summary>
/// Gets the <see cref="IAccountState"/> of the given <paramref name="address"/>.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions Libplanet.Action/State/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ private World(IWorldState baseState, IWorldDelta delta)
[Pure]
public bool Legacy => _baseState.Legacy;

/// <inheritdoc cref="IWorldState.Version/>
[Pure]
public int Version => _baseState.Version;

/// <inheritdoc cref="IWorld.GetAccount"/>
[Pure]
public IAccount GetAccount(Address address)
Expand Down
10 changes: 8 additions & 2 deletions Libplanet.Action/State/WorldBaseState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;
using static Libplanet.Action.State.KeyConverters;

namespace Libplanet.Action.State
Expand All @@ -20,14 +21,19 @@ public WorldBaseState(ITrie trie, IStateStore stateStore)
{
Trie = trie;
_stateStore = stateStore;
Legacy = trie.GetMetadata() is null;
Version = trie.GetMetadata() is { } value
? value.Version
: 0;
}

/// <inheritdoc cref="IWorldState.Trie"/>
public ITrie Trie { get; }

/// <inheritdoc cref="IWorldState.Legacy"/>
public bool Legacy { get; private set; }
public bool Legacy => Version < BlockMetadata.WorldStateProtocolVersion;

/// <inheritdoc cref="IWorldState.Version"/>
public int Version { get; }

/// <inheritdoc cref="IWorldState.GetAccountState"/>
public IAccountState GetAccountState(Address address)
Expand Down
18 changes: 14 additions & 4 deletions Libplanet.Mocks/MockWorldState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ internal MockWorldState(
{
Trie = trie;
_stateStore = stateStore;
Legacy = Trie.GetMetadata() is null;
Version = trie.GetMetadata() is { } value
? value.Version
: 0;
}

/// <inheritdoc cref="IWorldState.Trie"/>
public ITrie Trie { get; }

/// <inheritdoc cref="IWorldState.Legacy"/>
public bool Legacy { get; }
public bool Legacy => Version < BlockMetadata.WorldStateProtocolVersion;

/// <inheritdoc cref="IWorldState.Version"/>
public int Version { get; }

/// <summary>
/// Creates a new manipulable empty legacy <see cref="IWorldState"/>
Expand All @@ -78,12 +83,17 @@ public static MockWorldState CreateLegacy(IStateStore? stateStore = null)
/// <param name="stateStore">The <see cref="IStateStore"/> to use
/// as the new instance's backend storage. If <see langword="null"/>,
/// uses an ephemeral on-memory <see cref="IStateStore"/>.</param>
/// <param name="version">The version of the backing <see cref="ITrie"/>
/// to use. If not specified, defaults to
/// <see cref="BlockMetadata.CurrentProtocolVersion"/>.</param>
/// <returns>A new empty modern <see cref="IWorldState"/>.</returns>
public static MockWorldState CreateModern(IStateStore? stateStore = null)
public static MockWorldState CreateModern(
IStateStore? stateStore = null,
int version = BlockMetadata.CurrentProtocolVersion)
{
stateStore ??= new TrieStateStore(new MemoryKeyValueStore());
ITrie trie = stateStore.GetStateRoot(null);
trie = trie.SetMetadata(new TrieMetadata(Block.CurrentProtocolVersion));
trie = trie.SetMetadata(new TrieMetadata(version));
trie = stateStore.Commit(trie);
return new MockWorldState(trie, stateStore);
}
Expand Down

0 comments on commit 24e8a38

Please sign in to comment.