Skip to content

Commit

Permalink
Merge branch 'main' into feature/ci-without-net
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake authored Jan 30, 2024
2 parents 1f333e1 + 7398a22 commit 41f1b99
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 46 deletions.
24 changes: 23 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ To be released.
### CLI tools


Version 4.0.1
-------------

Released on January 26, 2024.

- (Libplanet.Action) Changed `IWorld.SetAccount()` to throw an
`ArgumentException` under certain undesirable circumstances. [[#3633]]

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


Version 4.0.0
-------------

Expand Down Expand Up @@ -97,6 +108,17 @@ Released on January 22, 2024.
[#3626]: https://github.com/planetarium/libplanet/pull/3626


Version 3.9.6
-------------

Released on January 26, 2024.

- (Libplanet.Store) Optimized `TrieStateStore.CopyStates()` to greatly
reduce the amount of memory used. [[#3634]]

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


Version 3.9.5
-------------

Expand Down Expand Up @@ -132,7 +154,7 @@ Released on January 11, 2024.
Version 3.9.3
-------------

To be released.
Released on January 4, 2024.

Due to changes in [#3567], a network ran with a prior version may not
be compatible with this version, specifically, those that ran with
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action/State/AccountState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Libplanet.Action.State
/// </summary>
public class AccountState : IAccountState
{
private ITrie _trie;
private readonly ITrie _trie;

public AccountState(ITrie trie)
{
Expand Down
36 changes: 25 additions & 11 deletions Libplanet.Action/State/IWorld.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Libplanet.Crypto;
Expand Down Expand Up @@ -53,20 +54,33 @@ public interface IWorld : IWorldState
IAccount GetAccount(Address address);

/// <summary>
/// Gets a new instance that the world state of the given
/// <paramref name="address"/> is set to the given
/// <paramref name="account"/>.
/// Creates a new instance of <see cref="IWorld"/> with given <paramref name="address"/>
/// set to given <paramref name="account"/>.
/// </summary>
/// <param name="address">The <see cref="Address"/> of the account to set.</param>
/// <param name="account">The new account to fill the account with.</param>
/// <returns>A new <see cref="IWorld"/> instance that
/// the account state of the given <paramref name="address"/>
/// is set to the given <paramref name="account"/>.</returns>
/// <param name="address">The <see cref="Address"/> for which to set
/// given <see cref="account"/> to.</param>
/// <param name="account">The new <see cref="IAccount"/> to set to
/// given <paramref name="address"/>.</param>
/// <returns>A new <see cref="IWorld"/> instance where the account state of given
/// <paramref name="address"/> is set to given <paramref name="account"/>.</returns>
/// <remarks>
/// This method method does not manipulate the instance,
/// but returns a new <see cref="IWorld"/> instance
/// with updated world instead.
/// This method method does not manipulate the instance, but returns
/// a new <see cref="IWorld"/> instance with an updated world state instead.
/// </remarks>
/// <exception cref="ArgumentException">
/// Thrown for one of the following reasons:
/// <list type="bullet">
/// <item><description>
/// If <see cref="Legacy"/> is <see langword="true"/> and <paramref name="address"/>
/// is not <see cref="ReservedAddresses.LegacyAccount"/>.
/// </description></item>
/// <item><description>
/// If <paramref name="address"/> is
/// not <see cref="ReservedAddresses.LegacyAccount"/> and
/// <see cref="IAccount.TotalUpdatedFungibleAssets"/> is non-empty.
/// </description></item>
/// </list>
/// </exception>
[Pure]
IWorld SetAccount(Address address, IAccount account);
}
Expand Down
20 changes: 14 additions & 6 deletions Libplanet.Action/State/World.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics.Contracts;
using Libplanet.Crypto;
using Libplanet.Store.Trie;
Expand Down Expand Up @@ -45,23 +46,30 @@ public IAccount GetAccount(Address address)
: new Account(_baseState.GetAccountState(address));
}

/// <inheritdoc cref="IWorld.GetAccountState"/>
/// <inheritdoc cref="IWorldState.GetAccountState"/>
[Pure]
public IAccountState GetAccountState(Address address) => GetAccount(address);

/// <inheritdoc/>
/// <inheritdoc cref="IWorld.SetAccount/>
[Pure]
public IWorld SetAccount(Address address, IAccount account)
{
if (Legacy && !address.Equals(ReservedAddresses.LegacyAccount))
{
throw new ArgumentException(
$"Cannot set a non-legacy account ({address}) to a legacy {nameof(IWorld)}.",
nameof(address));
}

if (!address.Equals(ReservedAddresses.LegacyAccount)
&& account.TotalUpdatedFungibleAssets.Count > 0)
{
return this;
throw new ArgumentException(
$"Cannot set a non-legacy account ({address}) with an updated fungible assets.",
nameof(address));
}

return new World(
_baseState,
Delta.SetAccount(address, account));
return new World(_baseState, Delta.SetAccount(address, account));
}
}
}
2 changes: 1 addition & 1 deletion Libplanet.Action/State/WorldDelta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Libplanet.Action.State
{
public class WorldDelta : IWorldDelta
{
private IImmutableDictionary<Address, AccountItem> _accounts;
private readonly IImmutableDictionary<Address, AccountItem> _accounts;

public WorldDelta()
{
Expand Down
16 changes: 8 additions & 8 deletions Libplanet.Store/Trie/MerkleTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
yield break;
}

var queue = new Queue<INode>();
queue.Enqueue(Root);
var stack = new Stack<INode>();
stack.Push(Root);

while (queue.Count > 0)
while (stack.Count > 0)
{
INode node = queue.Dequeue();
INode node = stack.Pop();
if (node is HashNode dequeuedHashNode)
{
var storedKey = new KeyBytes(dequeuedHashNode.HashDigest.ByteArray);
var storedValue = KeyValueStore.Get(storedKey);
var intermediateEncoding = _codec.Decode(storedValue);
queue.Enqueue(
stack.Push(
NodeDecoder.Decode(
intermediateEncoding,
NodeDecoder.HashEmbeddedNodeType) ??
Expand All @@ -229,21 +229,21 @@ internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
INode? child = fullNode.Children[index];
if (child is HashNode childHashNode)
{
queue.Enqueue(childHashNode);
stack.Push(childHashNode);
}
}

if (fullNode.Value is HashNode fullNodeValueHashNode)
{
queue.Enqueue(fullNodeValueHashNode);
stack.Push(fullNodeValueHashNode);
}

break;

case ShortNode shortNode:
if (shortNode.Value is HashNode shortNodeValueHashNode)
{
queue.Enqueue(shortNodeValueHashNode);
stack.Push(shortNodeValueHashNode);
}

break;
Expand Down
3 changes: 1 addition & 2 deletions Libplanet.Tests/Action/AccountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public abstract class AccountTest
protected readonly Currency[] _currencies;
protected readonly IAccount _initAccount;
protected readonly IActionContext _initContext;
protected readonly Address _accountAddress
= new Address("2000000000000000000000000000000000000000");
protected readonly Address _accountAddress = ReservedAddresses.LegacyAccount;

protected AccountTest(ITestOutputHelper output)
{
Expand Down
5 changes: 0 additions & 5 deletions Libplanet.Tests/Blockchain/BlockChainTest.Stage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Libplanet.Action;
using Libplanet.Action.Tests.Common;
Expand Down Expand Up @@ -60,25 +59,21 @@ public void TransactionsWithDuplicatedNonce()

Transaction tx_0_0 = _fx.MakeTransaction(
new DumbAction[0],
ImmutableHashSet<Address>.Empty,
nonce: 0,
privateKey: key
);
Transaction tx_0_1 = _fx.MakeTransaction(
new DumbAction[0],
ImmutableHashSet<Address>.Empty,
nonce: 0,
privateKey: key
);
Transaction tx_1_0 = _fx.MakeTransaction(
new DumbAction[0],
ImmutableHashSet<Address>.Empty,
nonce: 1,
privateKey: key
);
Transaction tx_1_1 = _fx.MakeTransaction(
new DumbAction[0],
ImmutableHashSet<Address>.Empty,
nonce: 1,
privateKey: key
);
Expand Down
2 changes: 0 additions & 2 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,6 @@ protected virtual StoreFixture GetStoreFixture(IAction blockAction) =>
},
timestamp: epoch,
nonce: 0,
updatedAddresses: new[] { addresses[0], addresses[1] }.ToImmutableHashSet(),
privateKey: privateKey),
_fx.MakeTransaction(
new[]
Expand All @@ -1918,7 +1917,6 @@ protected virtual StoreFixture GetStoreFixture(IAction blockAction) =>
},
timestamp: epoch.AddSeconds(5),
nonce: 1,
updatedAddresses: new[] { addresses[2], addresses[3] }.ToImmutableHashSet(),
privateKey: privateKey),
};

Expand Down
12 changes: 3 additions & 9 deletions Libplanet.Tests/Store/StoreFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Security.Cryptography;
using Libplanet.Action;
using Libplanet.Action.Loader;
Expand Down Expand Up @@ -95,10 +94,6 @@ protected StoreFixture(IAction blockAction = null)

var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var stateRootHashes = new Dictionary<BlockHash, HashDigest<SHA256>>();
Func<BlockHash?, HashDigest<SHA256>?> rootHashGetter = bh =>
bh is BlockHash h && stateRootHashes.TryGetValue(h, out HashDigest<SHA256> rh)
? rh
: (HashDigest<SHA256>?)null;
Proposer = TestUtils.GenesisProposer;
var preEval = TestUtils.ProposeGenesis(
proposer: Proposer.PublicKey,
Expand Down Expand Up @@ -136,9 +131,9 @@ protected StoreFixture(IAction blockAction = null)
Block5 = TestUtils.ProposeNextBlock(Block4, miner: Proposer);
stateRootHashes[Block5.Hash] = Block5.StateRootHash;

Transaction1 = MakeTransaction(new List<DumbAction>(), ImmutableHashSet<Address>.Empty);
Transaction2 = MakeTransaction(new List<DumbAction>(), ImmutableHashSet<Address>.Empty);
Transaction3 = MakeTransaction(new List<DumbAction>(), ImmutableHashSet<Address>.Empty);
Transaction1 = MakeTransaction(new List<DumbAction>());
Transaction2 = MakeTransaction(new List<DumbAction>());
Transaction3 = MakeTransaction(new List<DumbAction>());
}

public string Path { get; set; }
Expand Down Expand Up @@ -203,7 +198,6 @@ protected StoreFixture(IAction blockAction = null)

public Transaction MakeTransaction(
IEnumerable<DumbAction> actions = null,
ImmutableHashSet<Address> updatedAddresses = null,
long nonce = 0,
PrivateKey privateKey = null,
DateTimeOffset? timestamp = null
Expand Down

0 comments on commit 41f1b99

Please sign in to comment.