Skip to content

Commit

Permalink
Accommodate libplanet changes
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Jul 19, 2023
1 parent 5909ce4 commit 9497647
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 149 deletions.
3 changes: 1 addition & 2 deletions .Lib9c.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ static void Main(string[] args)
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
new BlockChainStates(store, stateStore),
new NCActionLoader(),
null);
new NCActionLoader());
var chain = new BlockChain(
policy,
stagePolicy,
Expand Down
4 changes: 0 additions & 4 deletions .Lib9c.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,5 @@ public IActionContext GetUnconsumedContext()
public long GasUsed() => _gasUsed;

public long GasLimit() => 0;

public void PutLog(string log)
{
}
}
}
13 changes: 5 additions & 8 deletions .Lib9c.Tests/Action/Factory/ClaimStakeRewardFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ public static IEnumerable<object[]> GetAllClaimStakeRewardV1()
var arr = Assembly.GetAssembly(typeof(ClaimRaidReward))?.GetTypes()
.Where(type =>
type.IsClass &&
type.GetInterfaces().Contains(typeof(IClaimStakeRewardV1)))
.Select(ActionTypeAttribute.ValueOf)
.ToArray() ?? Array.Empty<IValue?>();
typeof(IClaimStakeRewardV1).IsAssignableFrom(type))
.Select(type =>
type.GetCustomAttribute<ActionTypeAttribute>()?.TypeIdentifier)
.OfType<IValue>()
.ToArray() ?? Array.Empty<IValue>();

foreach (var value in arr)
{
if (value is null)
{
continue;
}

var str = (string)(Text)value;
var verStr = str.Replace("claim_stake_reward", string.Empty);
var ver = string.IsNullOrEmpty(verStr)
Expand Down
110 changes: 31 additions & 79 deletions .Lib9c.Tests/Action/MockStateDelta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,28 @@ namespace Lib9c.Tests.Action
using Libplanet.State;

/// <summary>
/// <para>
/// A rough replica of https://github.com/planetarium/libplanet/blob/main/Libplanet/State/AccountStateDelta.cs
/// except this has its constructors exposed as public for testing with many of
/// checks removed.
/// </para>
/// <para>
/// Notable differences from the original are:
/// <type list="bullet">
/// <item><description>
/// There is no authority check for <see cref="MintAsset"/> and <see cref="BurnAsset"/>,
/// i.e. any <see cref="Address"/> can mint and burn.
/// </description></item>
/// <item><description>
/// There is no amount check for <see cref="MintAsset"/>, <see cref="BurnAsset"/>,
/// i.e. 0 or even a negative amount is allowed.
/// </description></item>
/// <item><description>
/// The <see cref="TransferAsset"/> behaves as if any given <see cref="IActionContext"/>
/// has <see cref="IActionContext.BlockProtocolVersion"/> value equal to 0.
/// </description></item>
/// </list>
/// </para>
/// except this has its constructors exposed as public for testing.
/// </summary>
[Pure]
public class MockStateDelta : IAccountStateDelta
{
private readonly IAccountState _baseState;

public MockStateDelta()
: this(MockState.Empty)
{
}

public MockStateDelta(IAccountState mockState)
: this(
mockState.GetStates,
mockState.GetBalance,
mockState.GetTotalSupply,
mockState.GetValidatorSet)
public MockStateDelta(IAccountState baseState)
: this(baseState, new MockDelta())
{
}

private MockStateDelta(
AccountStateGetter accountStateGetter,
AccountBalanceGetter accountBalanceGetter,
TotalSupplyGetter totalSupplyGetter,
ValidatorSetGetter validatorSetGetter)
private MockStateDelta(IAccountState baseState, IAccountDelta delta)
{
Delta = new MockDelta();
StateGetter = accountStateGetter;
BalanceGetter = accountBalanceGetter;
TotalSupplyGetter = totalSupplyGetter;
ValidatorSetGetter = validatorSetGetter;
_baseState = baseState;
Delta = delta;
TotalUpdatedFungibles = ImmutableDictionary<(Address, Currency), BigInteger>.Empty;
}

Expand All @@ -78,15 +49,7 @@ private MockStateDelta(
TotalUpdatedFungibles.Keys.ToImmutableHashSet();

public IImmutableDictionary<(Address, Currency), BigInteger> TotalUpdatedFungibles
{ get; protected set; }

private AccountStateGetter StateGetter { get; set; }

private AccountBalanceGetter BalanceGetter { get; set; }

private TotalSupplyGetter TotalSupplyGetter { get; set; }

private ValidatorSetGetter ValidatorSetGetter { get; set; }
{ get; private set; }

/// <inheritdoc/>
[Pure]
Expand Down Expand Up @@ -118,7 +81,7 @@ private MockStateDelta(

if (notFoundIndices.Count > 0)
{
IReadOnlyList<IValue?> restValues = StateGetter(
IReadOnlyList<IValue?> restValues = _baseState.GetStates(
notFoundIndices.Select(index => addresses[index]).ToArray());
foreach ((var v, var i) in notFoundIndices.Select((v, i) => (v, i)))
{
Expand Down Expand Up @@ -158,13 +121,13 @@ public FungibleAssetValue GetTotalSupply(Currency currency)
return FungibleAssetValue.FromRawValue(currency, totalSupplyValue);
}

return TotalSupplyGetter(currency);
return _baseState.GetTotalSupply(currency);
}

/// <inheritdoc/>
[Pure]
public ValidatorSet GetValidatorSet() =>
Delta.ValidatorSet ?? ValidatorSetGetter();
Delta.ValidatorSet ?? _baseState.GetValidatorSet();

/// <inheritdoc/>
[Pure]
Expand Down Expand Up @@ -293,28 +256,24 @@ private FungibleAssetValue GetBalance(
IImmutableDictionary<(Address, Currency), BigInteger> balances) =>
balances.TryGetValue((address, currency), out BigInteger balance)
? FungibleAssetValue.FromRawValue(currency, balance)
: BalanceGetter(address, currency);
: _baseState.GetBalance(address, currency);

[Pure]
private IAccountStateDelta UpdateStates(
IImmutableDictionary<Address, IValue> updatedStates
) =>
private MockStateDelta UpdateStates(
IImmutableDictionary<Address, IValue> updatedStates) =>
new MockStateDelta(
StateGetter,
BalanceGetter,
TotalSupplyGetter,
ValidatorSetGetter)
{
Delta = new MockDelta(
_baseState,
new MockDelta(
updatedStates,
Delta.Fungibles,
Delta.TotalSupplies,
Delta.ValidatorSet),
Delta.ValidatorSet))
{
TotalUpdatedFungibles = TotalUpdatedFungibles,
};

[Pure]
private IAccountStateDelta UpdateFungibleAssets(
private MockStateDelta UpdateFungibleAssets(
IImmutableDictionary<(Address, Currency), BigInteger> updatedFungibleAssets,
IImmutableDictionary<(Address, Currency), BigInteger> totalUpdatedFungibles
) =>
Expand All @@ -324,40 +283,33 @@ private IAccountStateDelta UpdateFungibleAssets(
Delta.TotalSupplies);

[Pure]
private IAccountStateDelta UpdateFungibleAssets(
private MockStateDelta UpdateFungibleAssets(
IImmutableDictionary<(Address, Currency), BigInteger> updatedFungibleAssets,
IImmutableDictionary<(Address, Currency), BigInteger> totalUpdatedFungibles,
IImmutableDictionary<Currency, BigInteger> updatedTotalSupply
) =>
new MockStateDelta(
StateGetter,
BalanceGetter,
TotalSupplyGetter,
ValidatorSetGetter)
{
Delta = new MockDelta(
_baseState,
new MockDelta(
Delta.States,
updatedFungibleAssets,
updatedTotalSupply,
Delta.ValidatorSet),
Delta.ValidatorSet))
{
TotalUpdatedFungibles = totalUpdatedFungibles,
};

[Pure]
private IAccountStateDelta UpdateValidatorSet(
ValidatorSet updatedValidatorSet
) =>
private MockStateDelta UpdateValidatorSet(
ValidatorSet updatedValidatorSet) =>
new MockStateDelta(
StateGetter,
BalanceGetter,
TotalSupplyGetter,
ValidatorSetGetter)
{
Delta = new MockDelta(
_baseState,
new MockDelta(
Delta.States,
Delta.Fungibles,
Delta.TotalSupplies,
updatedValidatorSet),
updatedValidatorSet))
{
TotalUpdatedFungibles = TotalUpdatedFungibles,
};

Expand Down
6 changes: 2 additions & 4 deletions .Lib9c.Tests/Action/RewardGoldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,7 @@ public async Task Genesis_StateRootHash(bool mainnet)
blockChainStates: new BlockChainStates(
new MemoryStore(),
new TrieStateStore(new MemoryKeyValueStore())),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null);
actionTypeLoader: new NCActionLoader());
genesis = BlockChain.ProposeGenesisBlock(
tempActionEvaluator,
transactions: ImmutableList<Transaction>.Empty
Expand All @@ -554,8 +553,7 @@ public async Task Genesis_StateRootHash(bool mainnet)
actionEvaluator: new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
),
renderers: blockPolicySource.GetRenderers()
);
Expand Down
21 changes: 7 additions & 14 deletions .Lib9c.Tests/Policy/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public void ValidateNextBlockTx()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
),
renderers: new[] { blockPolicySource.BlockRenderer }
);
Expand Down Expand Up @@ -177,8 +176,7 @@ public void ValidateNextBlockTx_Mead()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
),
renderers: new[] { blockPolicySource.BlockRenderer }
);
Expand Down Expand Up @@ -266,8 +264,7 @@ public void BlockCommitFromNonValidator()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
),
renderers: new[] { blockPolicySource.BlockRenderer }
);
Expand Down Expand Up @@ -321,8 +318,7 @@ public void MustNotIncludeBlockActionAtTransaction()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: actionLoader,
feeCalculator: null
actionTypeLoader: actionLoader
),
renderers: new[] { blockPolicySource.BlockRenderer }
);
Expand Down Expand Up @@ -375,8 +371,7 @@ public void EarnMiningGoldWhenSuccessMining()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
),
renderers: new[] { blockPolicySource.BlockRenderer }
);
Expand Down Expand Up @@ -425,8 +420,7 @@ public void ValidateNextBlockWithManyTransactions()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
)
);

Expand Down Expand Up @@ -527,8 +521,7 @@ public void ValidateNextBlockWithManyTransactionsPerSigner()
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
)
);

Expand Down
3 changes: 1 addition & 2 deletions .Lib9c.Tests/TestHelper/BlockChainHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public static BlockChain MakeBlockChain(
new ActionEvaluator(
policyBlockActionGetter: _ => policy.BlockAction,
blockChainStates: new BlockChainStates(store, stateStore),
actionTypeLoader: new NCActionLoader(),
feeCalculator: null
actionTypeLoader: new NCActionLoader()
),
renderers: blockRenderers);
}
Expand Down
3 changes: 1 addition & 2 deletions .Lib9c.Tools/SubCommand/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ IStateStore stateStore
var actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
new BlockChainStates(store, stateStore),
actionLoader,
null);
actionLoader);
HashDigest<SHA256> stateRootHash = block.Index < 1
? BlockChain.DetermineGenesisStateRootHash(
actionEvaluator,
Expand Down
3 changes: 1 addition & 2 deletions Lib9c.DevExtensions/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ Guid chainIdValue
ActionEvaluator actionEvaluator = new ActionEvaluator(
_ => policy.BlockAction,
blockChainStates,
actionLoader,
null);
actionLoader);

BlockChain chain;
if (store.GetCanonicalChainId() is null)
Expand Down
9 changes: 0 additions & 9 deletions Lib9c.MessagePack/Formatters/NCActionFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ public class NCActionFormatter : IMessagePackFormatter<ActionBase?>
{
private readonly IActionLoader _actionLoader;

private static readonly IDictionary<IValue, Type> Types = typeof(ActionBase)
.Assembly
.GetTypes()
.Where(t => t.IsDefined(typeof(ActionTypeAttribute)))
.ToDictionary(
t => ActionTypeAttribute.ValueOf(t)
?? throw new InvalidOperationException("Unreachable code."),
t => t);

public NCActionFormatter()
{
_actionLoader = new NCActionLoader();
Expand Down
2 changes: 0 additions & 2 deletions Lib9c.Policy/Policy/DebugPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public DebugPolicy()

public IAction BlockAction { get; } = new RewardGold();

public IFeeCalculator? FeeCalculator { get; }

public TxPolicyViolationException ValidateNextBlockTx(
BlockChain blockChain, Transaction transaction)
{
Expand Down
3 changes: 1 addition & 2 deletions Lib9c.Utils/BlockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public static Block ProposeGenesisBlock(
var actionEvaluator = new ActionEvaluator(
_ => blockAction,
new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())),
actionLoader,
null);
actionLoader);
return
BlockChain.ProposeGenesisBlock(
actionEvaluator,
Expand Down
Loading

0 comments on commit 9497647

Please sign in to comment.