-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
- Loading branch information
There are no files selected for viewing
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")] |
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 GitHub Actions / docs
Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / docs
Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)
Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)
Check failure on line 1 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / check-build
|
||
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 GitHub Actions / docs
Check failure on line 15 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / docs
Check failure on line 15 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)
Check failure on line 15 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)
Check failure on line 15 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / check-build
|
||
{ | ||
/// <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 GitHub Actions / docs
Check failure on line 24 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)
Check failure on line 24 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)
|
||
/// <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 GitHub Actions / docs
Check failure on line 44 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / docs
Check failure on line 44 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)
Check failure on line 44 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)
Check failure on line 44 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / check-build
|
||
{ | ||
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 GitHub Actions / docs
Check failure on line 76 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / docs
Check failure on line 76 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)
Check failure on line 76 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)
Check failure on line 76 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / check-build
|
||
{ | ||
var stateRoot = stateStore.GetStateRoot(null); | ||
foreach((var key, var value) in data) | ||
Check failure on line 79 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / docs
Check failure on line 79 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (linux-8cores)
Check failure on line 79 in src/Libplanet/Blockchain/StateStoreExtensions.cs GitHub Actions / Run Benchmark.Net benchmarks (windows-8cores)
|
||
{ | ||
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; | ||
} | ||
} | ||
} |