Skip to content

Commit

Permalink
Added direct commit of world to IStateStore
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Sep 12, 2024
1 parent 3eec515 commit e244608
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Libplanet.Action/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Libplanet.Tests")]
[assembly: InternalsVisibleTo("Libplanet")]
[assembly: InternalsVisibleTo("Libplanet.Action.Tests")]
[assembly: InternalsVisibleTo("Libplanet.Explorer.Tests")]
[assembly: InternalsVisibleTo("Libplanet.Mocks")]
[assembly: InternalsVisibleTo("Libplanet.Tests")]
104 changes: 104 additions & 0 deletions src/Libplanet/Blockchain/StateStoreExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

File name StateStoreExtensions.cs doesn't match the name of a contained type.

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

File name StateStoreExtensions.cs doesn't match the name of a contained type.

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

File name StateStoreExtensions.cs doesn't match the name of a contained type.

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / docs

File name StateStoreExtensions.cs doesn't match the name of a contained type.

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / docs

File name StateStoreExtensions.cs doesn't match the name of a contained type.

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)

File name StateStoreExtensions.cs doesn't match the name of a contained type.

Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)

File name StateStoreExtensions.cs doesn't match the name of a contained type.
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;

namespace Libplanet.Store
{
/// <summary>
/// Convenient extension methods for <see cref="IStateStore"/>.
/// </summary>
public static class IStateStoreExtensions

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

File name should match first type name.

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

File name should match first type name.

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

File name should match first type name.

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / docs

File name should match first type name.

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / docs

File name should match first type name.

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)

File name should match first type name.

Check failure on line 17 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)

File name should match first type name.
{
/// <summary>
/// Commits <paramref name="data"/> representing a world state directly
/// to <see cref="IStateStore"/> and returns its state root hash.
/// The world state created is set to <see cref="BlockMetadata.CurrentProtocolVersion"/>.
/// </summary>
/// <param name="stateStore">The <see cref="IStateStore"/> to commit to.</param>
/// <param name="data">The data representing a world state to commit.</param>
/// <returns>The state root hash of the <paramref name="data"/> committed.</returns>
/// <exception cref="ArgumentException">Thrown if given <paramref name="data"/>
/// is not in the right format.
/// <list type="bullet">
/// <item><description>
/// Every key in <paramref name="data"/> must be a <see cref="Binary"/> of length
/// <see cref="Address.Size"/>.
/// </description></item>
/// <item><description>
/// Every value in <paramref name="data"/> must be a <see cref="Dictionary"/> with
/// each key in the <see cref="Dictionary"/> being a <see cref="Binary"/> of length
/// <see cref="Address.Size"/>.
/// </description></item>
/// </list>
/// </exception>
public static HashDigest<SHA256> CommitWorld(
this IStateStore stateStore,
Dictionary data)
{
try
{
var dictionary = data.ToDictionary(
outerPair => new Address(((Binary)outerPair.Key).ByteArray),
outerPair => ((Dictionary)outerPair.Value).ToDictionary(
innerPair => new Address(((Binary)innerPair.Key).ByteArray),
innerPair => innerPair.Value));
return stateStore.CommitWorld(dictionary);
}
catch (Exception e)
{
throw new ArgumentException(
$"Could not convert {nameof(data)} to a proper format",
nameof(data),
e);
}
}

/// <summary>
/// Commits <paramref name="data"/> representing a world state directly
/// to <see cref="IStateStore"/> and returns its state root hash.
/// The world state created is set to <see cref="BlockMetadata.CurrentProtocolVersion"/>.
/// </summary>
/// <param name="stateStore">The <see cref="IStateStore"/> to commit to.</param>
/// <param name="data">The data representing a world state to commit.</param>
/// <returns>The state root hash of the <paramref name="data"/> committed.</returns>
public static HashDigest<SHA256> CommitWorld(
this IStateStore stateStore,
Dictionary<Address, Dictionary<Address, IValue>> data)
{
var stateRoot = stateStore.GetStateRoot(null);
stateRoot = stateRoot.SetMetadata(
new TrieMetadata(BlockMetadata.CurrentProtocolVersion));
stateRoot = stateStore.Commit(stateRoot);
foreach ((var key, var value) in data)

Check failure on line 79 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

'KeyValuePair<Address, Dictionary<Address, IValue>>' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair<Address, Dictionary<Address, IValue>>' could be found (are you missing a using directive or an assembly reference?)
{
stateRoot = stateRoot.Set(
KeyConverters.ToStateKey(key),
new Binary(stateStore.CommitAccount(value).ByteArray));
}

return stateStore.Commit(stateRoot).Hash;
}

private static HashDigest<SHA256> CommitAccount(
this IStateStore stateStore,
Dictionary<Address, IValue> data)
{
var stateRoot = stateStore.GetStateRoot(null);
foreach((var key, var value) in data)

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

The keyword 'foreach' should be followed by a space

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

The keyword 'foreach' should be followed by a space

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

The keyword 'foreach' should be followed by a space

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / docs

The keyword 'foreach' should be followed by a space

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / docs

The keyword 'foreach' should be followed by a space

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)

The keyword 'foreach' should be followed by a space

Check failure on line 94 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)

The keyword 'foreach' should be followed by a space
{
stateRoot = stateRoot.Set(
KeyConverters.ToStateKey(key),
value);
}

return stateStore.Commit(stateRoot).Hash;
}
}
}

0 comments on commit e244608

Please sign in to comment.