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 f6cd63d commit 67390d9
Show file tree
Hide file tree
Showing 3 changed files with 123 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")]
16 changes: 16 additions & 0 deletions src/Libplanet/Blockchain/BlockChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ private BlockChain(
);
}

if (!StateStore.GetStateRoot(Tip.StateRootHash).Recorded)
{
throw new ArgumentException(
$"Given {nameof(stateStore)} does not contain the latest state " +
$"corresponding to state root hash {Tip.StateRootHash}",
nameof(stateStore));
}

if (Tip.ProtocolVersion < BlockMetadata.SlothProtocolVersion)
{
_nextStateRootHash = Tip.StateRootHash;
Expand Down Expand Up @@ -393,6 +401,14 @@ public static BlockChain Create(
$"Given {nameof(store)} already has its canonical chain id set: {canonId}",
nameof(store));
}
else if (!stateStore.GetStateRoot(genesisBlock.StateRootHash).Recorded)
{
throw new ArgumentException(
$"Given {nameof(stateStore)} does not contain the state root " +
$"corresponding to the state root hash of {nameof(genesisBlock)} " +
$"{genesisBlock.StateRootHash}",
nameof(stateStore));
}

var id = Guid.NewGuid();

Expand Down
105 changes: 105 additions & 0 deletions src/Libplanet/Blockchain/IStateStoreExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
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;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;

namespace Libplanet.Blockchain
{
/// <summary>
/// Convenient extension methods for <see cref="IStateStore"/>.
/// </summary>
public static class IStateStoreExtensions
{
/// <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 80 in src/Libplanet/Blockchain/IStateStoreExtensions.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?)

Check failure on line 80 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair<Address, Dictionary<Address, IValue>>', with 2 out parameters and a void return type.

Check failure on line 80 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

Cannot infer the type of implicitly-typed deconstruction variable 'key'.

Check failure on line 80 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

Cannot infer the type of implicitly-typed deconstruction variable 'value'.
{
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 95 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

'KeyValuePair<Address, IValue>' does not contain a definition for 'Deconstruct' and no accessible extension method 'Deconstruct' accepting a first argument of type 'KeyValuePair<Address, IValue>' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 95 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

No suitable 'Deconstruct' instance or extension method was found for type 'KeyValuePair<Address, IValue>', with 2 out parameters and a void return type.

Check failure on line 95 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

Cannot infer the type of implicitly-typed deconstruction variable 'key'.

Check failure on line 95 in src/Libplanet/Blockchain/IStateStoreExtensions.cs

View workflow job for this annotation

GitHub Actions / check-build

Cannot infer the type of implicitly-typed deconstruction variable 'value'.
{
stateRoot = stateRoot.Set(
KeyConverters.ToStateKey(key),
value);
}

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

0 comments on commit 67390d9

Please sign in to comment.