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 96a304b
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 / 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.

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.
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 15 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 15 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 15 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 15 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.

Check failure on line 15 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 15 in src/Libplanet/Blockchain/StateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

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></returns>

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

View workflow job for this annotation

GitHub Actions / docs

Element return value documentation should have text

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

View workflow job for this annotation

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

Element return value documentation should have text

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

View workflow job for this annotation

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

Element return value documentation should have text

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

View workflow job for this annotation

GitHub Actions / check-build

Element return value documentation should have text
/// <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)
{
var stateRoot = stateStore.GetStateRoot(null);
stateRoot = stateRoot.SetMetadata(
new TrieMetadata(BlockMetadata.CurrentProtocolVersion));
foreach((var key, var value) in data)

Check failure on line 44 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 44 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 44 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 44 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

Check failure on line 44 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 44 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
{
if (key is not Binary binary)
{
throw new ArgumentException(
$"Given {data} contains a key that is of invalid type: {key.GetType()}",
nameof(data));
}

if (binary.ByteArray.Length != Address.Size)
{
throw new ArgumentException(
$"Given {data} contains a key that is of invalid length that is " +
$"not {Address.Size}: {binary.ByteArray.Length}",
nameof(data));
}

if (value is not Dictionary dict)
{
throw new ArgumentException(
$"Given {data} contains a value that is of invalid type: {value.GetType()}",
nameof(data));
}

stateRoot = stateRoot.Set(
KeyConverters.ToStateKey(new Address(binary.ByteArray)),
new Binary(stateStore.CommitAccount(dict).ByteArray));
}

return stateStore.Commit(stateRoot).Hash;
}

private static HashDigest<SHA256> CommitAccount(this IStateStore stateStore, Dictionary data)

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

View workflow job for this annotation

GitHub Actions / docs

Line must be no longer than 100 characters (now 101).

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

View workflow job for this annotation

GitHub Actions / docs

Line must be no longer than 100 characters (now 101).

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

View workflow job for this annotation

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

Line must be no longer than 100 characters (now 101).

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

View workflow job for this annotation

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

Line must be no longer than 100 characters (now 101).

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

View workflow job for this annotation

GitHub Actions / check-build

Line must be no longer than 100 characters (now 101).

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

View workflow job for this annotation

GitHub Actions / check-build

Line must be no longer than 100 characters (now 101).
{
var stateRoot = stateStore.GetStateRoot(null);
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 / docs

The keyword 'foreach' should be followed by a space

Check failure on line 79 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 79 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

Check failure on line 79 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
{
if (key is not Binary binary)
{
throw new ArgumentException(
$"Given {data} contains a key that is of invalid type: {key.GetType()}",
nameof(data));
}

if (binary.ByteArray.Length != Address.Size)
{
throw new ArgumentException(
$"Given {data} contains a key that is of invalid length that is " +
$"not {Address.Size}: {binary.ByteArray.Length}",
nameof(data));
}

stateRoot = stateRoot.Set(
KeyConverters.ToStateKey(new Address(binary.ByteArray)),
value);
}

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

0 comments on commit 96a304b

Please sign in to comment.