Skip to content

Commit

Permalink
Merge pull request #3821 from greymistcube/chore/cleanup
Browse files Browse the repository at this point in the history
🧹 Removed `GetNextWorldState()` from `IBlockChainStates`
  • Loading branch information
OnedgeLee authored Jun 12, 2024
2 parents bbd8e6e + 004385a commit ebb5e84
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
4 changes: 2 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ To be released.
parameter `IActionEvaluator actionEvaluator` any more. [[#3811]]
- (Libplanet) `BlockChain.ProposeBlock()` receives parameter
`HashDigest<SHA256> stateRootHash`. [[#3811]]
- (Libplanet) Added `BlockChain.GetNextWorldState()` and
`BlockChain.GetNextWorldState(BlockHash?)` methods. [[#3821]]

### Backward-incompatible network protocol changes

Expand All @@ -41,8 +43,6 @@ To be released.
[[#3811]]
- (Libplanet.Store) Added `IStore.DeleteNextStateRootHash()` method.
[[#3811]]
- (Libplanet.Action) Added
`IBlockChainStates.GetNextWorldState()` method. [[#3811]]
- (Libplanet) Added `BlockChain.DetermineNextBlockStateRootHash()`
method. [[#3811]]

Expand Down
15 changes: 0 additions & 15 deletions Libplanet.Action/State/IBlockChainStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,5 @@ public interface IBlockChainStates
/// </exception>
/// <seealso cref="IWorldState"/>
IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash);

/// <summary>
/// Returns the <see cref="IWorldState"/> in the BlockChain at <paramref name="offset"/>.
/// </summary>
/// <param name="offset">The <see cref="BlockHash"/> of the <see cref="Block"/> to create
/// for which to create an <see cref="IWorldState"/>.</param>
/// <returns>
/// The <see cref="IWorldState"/> at <paramref name="offset"/>.
/// Returns <see langword="null"/> if next state root hash does not exists.
/// </returns>
/// <exception cref="ArgumentException">Thrown when next state root hash exists,
/// but corresponding state root is not found in the <see cref="IStateStore"/>.
/// </exception>
/// <seealso cref="IWorldState"/>
IWorldState? GetNextWorldState(BlockHash offset);
}
}
4 changes: 0 additions & 4 deletions Libplanet.Mocks/MockBlockChainStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,5 @@ public IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash)
$"Could not find state root {stateRootHash} in {nameof(IStateStore)}.",
nameof(stateRootHash));
}

/// <inheritdoc cref="IBlockChainStates.GetNextWorldState(BlockHash)"/>
public IWorldState GetNextWorldState(BlockHash offset) =>
throw new InvalidOperationException();
}
}
6 changes: 3 additions & 3 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public void EvaluateActions()
ActionEvaluation[] evalsA = ActionEvaluator.EvaluateActions(
block: blockA,
tx: txA,
previousState: fx.CreateWorld(blockA.PreviousHash),
previousState: fx.StateStore.GetWorld(blockA.StateRootHash),
actions: txA.Actions
.Select(action => (IAction)ToAction<Arithmetic>(action))
.ToImmutableArray(),
Expand Down Expand Up @@ -755,7 +755,7 @@ public void EvaluateActions()
ActionEvaluation[] evalsB = ActionEvaluator.EvaluateActions(
block: blockB,
tx: txB,
previousState: fx.CreateWorld(blockB.PreviousHash),
previousState: fx.StateStore.GetWorld(blockB.StateRootHash),
actions: txB.Actions
.Select(action => (IAction)ToAction<Arithmetic>(action))
.ToImmutableArray(),
Expand Down Expand Up @@ -1201,7 +1201,7 @@ public void CheckRandomSeedInAction()
ActionEvaluation[] evalsA = ActionEvaluator.EvaluateActions(
block: blockA,
tx: txA,
previousState: fx.CreateWorld(blockA.PreviousHash),
previousState: fx.StateStore.GetWorld(blockA.StateRootHash),
actions: txA.Actions
.Select(action => (IAction)ToAction<Arithmetic>(action))
.ToImmutableArray(),
Expand Down
3 changes: 0 additions & 3 deletions Libplanet.Tests/Fixtures/IntegerSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ public Block Propose() => Chain.ProposeBlock(
public void Append(Block block) =>
Chain.Append(block, TestUtils.CreateBlockCommit(block));

public IWorld CreateWorld(BlockHash? offset = null)
=> new World(Chain.GetNextWorldState(offset ?? Tip.Hash));

public ITrie GetTrie(BlockHash? blockHash)
{
return (blockHash is BlockHash h &&
Expand Down
34 changes: 32 additions & 2 deletions Libplanet/Blockchain/BlockChain.States.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Security.Cryptography;
using Libplanet.Action.State;
using Libplanet.Common;
using Libplanet.Store;
using Libplanet.Types.Blocks;

namespace Libplanet.Blockchain
Expand All @@ -27,8 +29,36 @@ public IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash)
/// <returns>The next world state. If it does not exist, returns null.</returns>
public IWorldState? GetNextWorldState() => GetNextWorldState(Tip.Hash);

/// <inheritdoc cref="IBlockChainStates.GetNextWorldState(BlockHash)" />
/// <summary>
/// Returns the next <see cref="IWorldState"/> in the BlockChain
/// at <paramref name="offset"/>.
/// </summary>
/// <param name="offset">The <see cref="BlockHash"/> of the <see cref="Block"/> to create
/// for which to create an <see cref="IWorldState"/>.</param>
/// <returns>
/// The next <see cref="IWorldState"/> at <paramref name="offset"/>.
/// Returns <see langword="null"/> if next state root hash does not exists.
/// </returns>
/// <exception cref="ArgumentException">Thrown when next state root hash exists,
/// but corresponding state root is not found in the <see cref="IStateStore"/>.
/// </exception>
/// <seealso cref="IWorldState"/>
public IWorldState? GetNextWorldState(BlockHash offset)
=> _blockChainStates.GetNextWorldState(offset);
{
var nextSrh = Store.GetNextStateRootHash(offset);
if (nextSrh is { } srh)
{
var trie = StateStore.GetStateRoot(srh);
return trie.Recorded
? new WorldBaseState(StateStore.GetStateRoot(nextSrh), StateStore)
: throw new ArgumentException(
$"Could not find state root {srh} in {nameof(StateStore)}.",
nameof(offset));
}
else
{
return null;
}
}
}
}
6 changes: 0 additions & 6 deletions Libplanet/Blockchain/BlockChainStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ public IWorldState GetWorldState(BlockHash offset)
public IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash)
=> new WorldBaseState(GetTrie(stateRootHash), _stateStore);

/// <inheritdoc cref="IBlockChainStates.GetNextWorldState(BlockHash)"/>
public IWorldState? GetNextWorldState(BlockHash offset)
=> _store.GetNextStateRootHash(offset) is HashDigest<SHA256> nextSrh
? new WorldBaseState(GetTrie(nextSrh), _stateStore)
: null;

/// <summary>
/// Returns the state root associated with <see cref="BlockHash"/>
/// <paramref name="offset"/>.
Expand Down

0 comments on commit ebb5e84

Please sign in to comment.