Skip to content

Commit

Permalink
Merge pull request #3528 from limebell/refactor/restore-blockchainstate
Browse files Browse the repository at this point in the history
Restore `BlockChainState`
  • Loading branch information
limebell authored Dec 4, 2023
2 parents a3db149 + b93143b commit 60fe431
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 121 deletions.
1 change: 0 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ To be released.

### Backward-incompatible API changes

- Removed `BlockChainStates` class. [[#3462]]
- Bumped `BlockMetadata.CurrentProtocolVersion` to 5. [[#3524]]
- (Libplanet.Action) Changed `ActionEvaluator` to accept `IWorld`
instead of `IAccount`. [[#3462]]
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,15 @@ If omitted (default) explorer only the local blockchain store.")]
new DumbBlockPolicy(LoadBlockPolicy(options));
IStagePolicy stagePolicy =
new VolatileStagePolicy();
var blockChainStates = new BlockChainStates(store, stateStore);
var blockChain =
new BlockChain(
policy,
stagePolicy,
store,
stateStore,
options.GetGenesisBlock(policy),
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
stateStore,
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Tests/Blockchain/BlockChainTest.Append.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,14 @@ public void AppendValidatesBlock()
{
var policy = new NullBlockPolicy(
new BlockPolicyViolationException(string.Empty));
var blockChainStates = new BlockChainStates(_fx.Store, _fx.StateStore);
var blockChain = new BlockChain(
policy,
new VolatileStagePolicy(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_fx.StateStore,
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Tests/Blockchain/BlockChainTest.ProposeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,15 @@ public void ProposeBlockWithBlockAction()

var blockAction = new DumbAction(address1, "foo");
var policy = new BlockPolicy(blockAction);
var blockChainStates = new BlockChainStates(_fx.Store, _fx.StateStore);

var blockChain = new BlockChain(
policy,
new VolatileStagePolicy(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_fx.StateStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ public void ValidateNextBlockInvalidStateRootHash()
new SetStatesAtBlock(default, (Text)"foo", default, 1),
policy.BlockInterval
);
var blockChainStates = new BlockChainStates(store, stateStore);
var chain2 = new BlockChain(
policyWithBlockAction,
new VolatileStagePolicy(),
store,
stateStore,
genesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policyWithBlockAction.BlockAction,
stateStore,
Expand Down
15 changes: 15 additions & 0 deletions Libplanet.Tests/Blockchain/BlockChainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ public void CanonicalId()
Assert.Equal(chain1.Id, _fx.Store.GetCanonicalChainId());

var policy = new BlockPolicy(new MinerReward(1));
var blockChainStates = new BlockChainStates(_fx.Store, _fx.StateStore);
var z = new BlockChain(
policy,
new VolatileStagePolicy(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_fx.StateStore,
Expand Down Expand Up @@ -176,6 +178,7 @@ public void ProcessActions()
{
var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var blockChainStates = new BlockChainStates(store, stateStore);
var policy = new BlockPolicy();
var actionLoader = TypedActionLoader.Create(
typeof(BaseAction).Assembly, typeof(BaseAction));
Expand Down Expand Up @@ -1110,12 +1113,14 @@ public void GetStateOnlyDrillsDownUntilRequestedAddressesAreFound()
{
var policy = new NullBlockPolicy();
var tracker = new StoreTracker(_fx.Store);
var blockChainStates = new BlockChainStates(tracker, _fx.StateStore);
var chain = new BlockChain(
policy,
new VolatileStagePolicy(),
tracker,
_fx.StateStore,
_fx.GenesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_fx.StateStore,
Expand Down Expand Up @@ -1165,12 +1170,14 @@ public void GetStateReturnsEarlyForNonexistentAccount()
{
var policy = new NullBlockPolicy();
var tracker = new StoreTracker(_fx.Store);
var blockChainStates = new BlockChainStates(tracker, _fx.StateStore);
var chain = new BlockChain(
policy,
new VolatileStagePolicy(),
tracker,
_fx.StateStore,
_fx.GenesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_fx.StateStore,
Expand Down Expand Up @@ -1244,12 +1251,14 @@ public void GetStateReturnsLatestStatesWhenMultipleAddresses()
var privateKeys = Enumerable.Range(1, 10).Select(_ => new PrivateKey()).ToList();
var addresses = privateKeys.Select(AddressExtensions.ToAddress).ToList();
var policy = new NullBlockPolicy();
var blockChainStates = new BlockChainStates(_fx.Store, _fx.StateStore);
var chain = new BlockChain(
policy,
new VolatileStagePolicy(),
_fx.Store,
_fx.StateStore,
_fx.GenesisBlock,
blockChainStates,
new ActionEvaluator(
_ => policy.BlockAction,
_fx.StateStore,
Expand Down Expand Up @@ -1695,6 +1704,7 @@ internal static (Address, Address[] Addresses, BlockChain Chain)
IBlockPolicy blockPolicy = new NullBlockPolicy();
store = new StoreTracker(store);
Guid chainId = Guid.NewGuid();
var chainStates = new BlockChainStates(store, stateStore);
var actionEvaluator = new ActionEvaluator(
_ => blockPolicy.BlockAction,
stateStore: stateStore,
Expand All @@ -1710,6 +1720,7 @@ internal static (Address, Address[] Addresses, BlockChain Chain)
stateStore,
genesisBlock,
renderers: renderer is null ? null : new[] { renderer },
blockChainStates: chainStates,
actionEvaluator: actionEvaluator);
var privateKey = new PrivateKey();
Address signer = privateKey.ToAddress();
Expand Down Expand Up @@ -1916,6 +1927,8 @@ private void CreateWithGenesisBlock()
),
};
var txs = systemTxs.Concat(customTxs).ToImmutableList();
var blockChainStates = new BlockChainStates(
storeFixture.Store, storeFixture.StateStore);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
storeFixture.StateStore,
Expand Down Expand Up @@ -1956,6 +1969,7 @@ private void ConstructWithUnexpectedGenesisBlock()
var stagePolicy = new VolatileStagePolicy();
IStore store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var blockChainStates = new BlockChainStates(store, stateStore);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
stateStore,
Expand All @@ -1979,6 +1993,7 @@ private void ConstructWithUnexpectedGenesisBlock()
store,
stateStore,
genesisBlockB,
blockChainStates,
actionEvaluator);
});
}
Expand Down
2 changes: 2 additions & 0 deletions Libplanet.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ public static (BlockChain BlockChain, ActionEvaluator ActionEvaluator)
timestamp: timestamp ?? DateTimeOffset.MinValue),
};

var blockChainStates = new BlockChainStates(store, stateStore);
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
stateStore: stateStore,
Expand Down Expand Up @@ -643,6 +644,7 @@ public static (BlockChain BlockChain, ActionEvaluator ActionEvaluator)
stateStore,
genesisBlock,
renderers: renderers ?? new[] { validator = new ValidatingActionRenderer() },
blockChainStates: blockChainStates,
actionEvaluator: actionEvaluator
);
#pragma warning restore S1121
Expand Down
89 changes: 40 additions & 49 deletions Libplanet/Blockchain/BlockChain.States.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@ namespace Libplanet.Blockchain
{
public partial class BlockChain
{
/// <inheritdoc cref="IBlockChainStates.GetWorldState(HashDigest{SHA256}?)" />
public IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash)
=> new WorldBaseState(GetTrie(stateRootHash), StateStore);

/// <inheritdoc cref="IBlockChainStates.GetWorldState(BlockHash?)" />
public IWorldState GetWorldState(BlockHash? offset)
=> new WorldBaseState(GetTrie(offset), StateStore);
=> _blockChainStates.GetWorldState(offset);

/// <inheritdoc cref="IBlockChainStates.GetWorldState(HashDigest{SHA256}?)" />
public IWorldState GetWorldState(HashDigest<SHA256>? stateRootHash)
=> _blockChainStates.GetWorldState(stateRootHash);

/// <summary>
/// Gets the current world state in the <see cref="BlockChain"/>.
/// </summary>
/// <returns>The current world state.</returns>
public IWorldState GetWorldState() => GetWorldState(Tip.Hash);

/// <inheritdoc cref="IBlockChainStates.GetAccountState(HashDigest{SHA256}?)"/>
public IAccountState GetAccountState(HashDigest<SHA256>? stateRootHash) =>
new AccountBaseState(GetTrie(stateRootHash));

/// <inheritdoc cref="IBlockChainStates.GetAccountState(Address, BlockHash?)"/>
public IAccountState GetAccountState(Address address, BlockHash? offset) =>
GetWorldState(offset).GetAccount(address);
public IAccountState GetAccountState(Address address, BlockHash? offset)
=> _blockChainStates.GetAccountState(address, offset);

/// <inheritdoc cref="IBlockChainStates.GetAccountState(HashDigest{SHA256}?)"/>
public IAccountState GetAccountState(HashDigest<SHA256>? stateRootHash)
=> _blockChainStates.GetAccountState(stateRootHash);

/// <summary>
/// Gets the current account state of given <paramref name="address"/> in the
/// <see cref="BlockChain"/>.
/// </summary>
/// <param name="address">An <see cref="Address"/> to get the account states of.</param>
/// <returns>The current account state of given <paramref name="address"/>.</returns>
public IAccountState GetAccountState(Address address) =>
GetWorldState().GetAccount(address);

/// <inheritdoc cref="IBlockChainStates.GetState(Address, HashDigest{SHA256}?)"/>
public IValue GetState(Address address, HashDigest<SHA256>? stateRootHash) =>
GetAccountState(stateRootHash).GetState(address);
public IAccountState GetAccountState(Address address)
=> GetAccountState(address, Tip.Hash);

/// <inheritdoc cref="IBlockChainStates.GetState(Address, Address, BlockHash?)"/>
public IValue GetState(Address address, Address accountAddress, BlockHash? offset) =>
GetAccountState(accountAddress, offset).GetState(address);
public IValue GetState(Address address, Address accountAddress, BlockHash? offset)
=> _blockChainStates.GetState(address, accountAddress, offset);

/// <inheritdoc cref="IBlockChainStates.GetState(Address, HashDigest{SHA256}?)"/>
public IValue GetState(Address address, HashDigest<SHA256>? stateRootHash)
=> _blockChainStates.GetState(address, stateRootHash);

/// <summary>
/// Gets the current state of given <paramref name="address"/> and
Expand All @@ -62,28 +62,25 @@ public IValue GetState(Address address, Address accountAddress, BlockHash? offse
/// <paramref name="accountAddress"/>. This can be <see langword="null"/>
/// if <paramref name="address"/> or <paramref name="accountAddress"/> has no value.
/// </returns>
public IValue GetState(Address address, Address accountAddress) =>
GetState(address, accountAddress, Tip.Hash);
public IValue GetState(Address address, Address accountAddress)
=> GetState(address, accountAddress, Tip.Hash);

/// <inheritdoc cref=
/// "IBlockChainStates.GetBalance(Address, Currency, HashDigest{SHA256}?)"/>
/// "IBlockChainStates.GetBalance(Address, Currency, Address, BlockHash?)"/>
public FungibleAssetValue GetBalance(
Address address,
Currency currency,
HashDigest<SHA256>? stateRootHash)
=> GetAccountState(stateRootHash)
.GetBalance(address, currency);
Address accountAddress,
BlockHash? offset)
=> _blockChainStates.GetBalance(address, currency, accountAddress, offset);

/// <inheritdoc cref=
/// "IBlockChainStates.GetBalance(Address, Currency, Address, BlockHash?)"/>
/// "IBlockChainStates.GetBalance(Address, Currency, HashDigest{SHA256}?)"/>
public FungibleAssetValue GetBalance(
Address address,
Currency currency,
Address accountAddress,
BlockHash? offset)
=> GetWorldState(offset)
.GetAccount(accountAddress)
.GetBalance(address, currency);
HashDigest<SHA256>? stateRootHash)
=> _blockChainStates.GetBalance(address, currency, stateRootHash);

/// <summary>
/// Queries <paramref name="address"/>'s current balance of the <paramref name="currency"/>
Expand All @@ -103,21 +100,18 @@ public FungibleAssetValue GetBalance(
Address accountAddress)
=> GetBalance(address, currency, accountAddress, Tip.Hash);

/// <inheritdoc cref="IBlockChainStates.GetTotalSupply(Currency, HashDigest{SHA256}?)"/>
public FungibleAssetValue GetTotalSupply(
Currency currency,
HashDigest<SHA256>? stateRootHash)
=> GetAccountState(stateRootHash)
.GetTotalSupply(currency);

/// <inheritdoc cref="IBlockChainStates.GetTotalSupply(Currency, Address, BlockHash?)"/>
public FungibleAssetValue GetTotalSupply(
Currency currency,
Address accountAddress,
BlockHash? offset)
=> GetWorldState(offset)
.GetAccount(accountAddress)
.GetTotalSupply(currency);
=> _blockChainStates.GetTotalSupply(currency, accountAddress, offset);

/// <inheritdoc cref="IBlockChainStates.GetTotalSupply(Currency, HashDigest{SHA256}?)"/>
public FungibleAssetValue GetTotalSupply(
Currency currency,
HashDigest<SHA256>? stateRootHash)
=> _blockChainStates.GetTotalSupply(currency, stateRootHash);

/// <summary>
/// Gets the current total supply of a <paramref name="currency"/> in the
Expand All @@ -134,16 +128,13 @@ public FungibleAssetValue GetTotalSupply(
public FungibleAssetValue GetTotalSupply(Currency currency, Address accountAddress)
=> GetTotalSupply(currency, accountAddress, Tip.Hash);

/// <inheritdoc cref="IBlockChainStates.GetValidatorSet(HashDigest{SHA256}?)" />
public ValidatorSet GetValidatorSet(HashDigest<SHA256>? stateRootHash)
=> GetAccountState(stateRootHash)
.GetValidatorSet();

/// <inheritdoc cref="IBlockChainStates.GetValidatorSet(Address, BlockHash?)" />
public ValidatorSet GetValidatorSet(Address accountAddress, BlockHash? offset)
=> GetWorldState(offset)
.GetAccount(accountAddress)
.GetValidatorSet();
=> _blockChainStates.GetValidatorSet(accountAddress, offset);

/// <inheritdoc cref="IBlockChainStates.GetValidatorSet(HashDigest{SHA256}?)" />
public ValidatorSet GetValidatorSet(HashDigest<SHA256>? stateRootHash)
=> _blockChainStates.GetValidatorSet(stateRootHash);

/// <summary>
/// Returns the current validator set in the <see cref="BlockChain"/>.
Expand Down
Loading

0 comments on commit 60fe431

Please sign in to comment.