Skip to content

Commit

Permalink
feat: Introduce ISystemAccounts
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Nov 13, 2023
1 parent c0c0284 commit 6fb4b41
Show file tree
Hide file tree
Showing 71 changed files with 673 additions and 318 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ To be released.
- (Libplanet.Action) Type of `Initialize.States` property became
`IImmutableDictionary<Address, IImmutableDictionary<Address, IValue>>?`.
(was `IImmutableDictionary<Address, IValue>?`) [[#3462]]
- (Libplanet.Action) New property `SystemAccounts` added to
`IActionContext` interface. [[#3462]]
- (Libplanet.Action) New property `SystemAccountsGetter` added to
`IBlockPolicy` interface. [[#3462]]


### Backward-incompatible network protocol changes
Expand All @@ -85,6 +89,8 @@ To be released.
- Added `WorldBaseState` class.
- (Libplanet.Action) Added `ReservedAddresses` static class. [[#3462]]
- (Libplanet.Action) Added `WorldDeltaExtensions` static class. [[#3462]]
- (Libplanet.Action) Added `ISystemAccounts` interface. [[#3462]]
- (Libplanet.Action) Added `ISystemAccountsGetter` interface. [[#3462]]
- (Libplanet.Explorer) Added `AccountStateType` class. [[#3462]]
- (Libplanet.Explorer) Added `WorldStateType` class. [[#3462]]
- (Libplanet.Explorer) Added `WorldStateType` class. [[#3462]]
Expand Down
8 changes: 8 additions & 0 deletions Libplanet.Action.Tests/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public class ActionContextTest
private readonly System.Random _random;
private readonly Address _address;
private readonly TxId _txid;
private readonly ISystemAccounts _systemAccounts;

public ActionContextTest()
{
_random = new System.Random();
_address = _random.NextAddress();
_txid = _random.NextTxId();
_systemAccounts = new SystemAccounts(
new SystemAccountsGetter(_ => ReservedAddresses.DefaultAccount), null);
}

[Fact]
Expand All @@ -34,6 +37,7 @@ public void RandomShouldBeDeterministic()
signer: _address,
txid: _txid,
miner: _address,
systemAccounts: _systemAccounts,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new World(new MockWorldState()),
Expand All @@ -52,6 +56,7 @@ public void GuidShouldBeDeterministic()
signer: _address,
txid: _txid,
miner: _address,
systemAccounts: _systemAccounts,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new World(new MockWorldState()),
Expand All @@ -63,6 +68,7 @@ public void GuidShouldBeDeterministic()
signer: _address,
txid: _txid,
miner: _address,
systemAccounts: _systemAccounts,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new World(new MockWorldState()),
Expand All @@ -74,6 +80,7 @@ public void GuidShouldBeDeterministic()
signer: _address,
txid: _txid,
miner: _address,
systemAccounts: _systemAccounts,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new World(new MockWorldState()),
Expand Down Expand Up @@ -113,6 +120,7 @@ public void GuidVersionAndVariant()
signer: _address,
txid: _txid,
miner: _address,
systemAccounts: _systemAccounts,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new World(new MockWorldState()),
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Action.Tests/ActionEvaluationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ this IEnumerable<IActionEvaluation> evaluations
ImmutableDictionary<Address, IValue>.Empty,
(dirty, ev) => dirty.SetItems(
ev.OutputState.GetAccount(
ReservedAddresses.LegacyAccount).GetUpdatedStates())
ReservedAddresses.DefaultAccount).GetUpdatedStates())
);

public static IImmutableDictionary<(Address, Currency), FungibleAssetValue>
Expand All @@ -26,7 +26,7 @@ this IEnumerable<IActionEvaluation> evaluations
ImmutableDictionary<(Address, Currency), FungibleAssetValue>.Empty,
(dirty, ev) => dirty.SetItems(
ev.OutputState.GetAccount(
ReservedAddresses.LegacyAccount).GetUpdatedBalances())
ReservedAddresses.DefaultAccount).GetUpdatedBalances())
);

public static IImmutableDictionary<Currency, FungibleAssetValue>
Expand All @@ -35,7 +35,7 @@ public static IImmutableDictionary<Currency, FungibleAssetValue>
ImmutableDictionary<Currency, FungibleAssetValue>.Empty,
(dirty, ev) => dirty.SetItems(
ev.OutputState.GetAccount(
ReservedAddresses.LegacyAccount).GetUpdatedTotalSupplies())
ReservedAddresses.DefaultAccount).GetUpdatedTotalSupplies())
);
}
}
9 changes: 6 additions & 3 deletions Libplanet.Action.Tests/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Libplanet.Action.Tests
public class ActionEvaluationTest
{
private readonly ILogger _logger;
private readonly ISystemAccounts _systemAccounts = new SystemAccounts(
new SystemAccountsGetter(_ => ReservedAddresses.DefaultAccount), null);

public ActionEvaluationTest(ITestOutputHelper output)
{
Expand All @@ -35,6 +37,7 @@ public void Constructor()
address,
txid,
address,
_systemAccounts,
1,
Block.CurrentProtocolVersion,
new World(new MockWorldState()),
Expand All @@ -43,7 +46,7 @@ public void Constructor()
false),
new World(
new MockWorldState().SetAccountState(
ReservedAddresses.LegacyAccount,
ReservedAddresses.DefaultAccount,
new Account(new MockAccountState().SetState(address, (Text)"item"))))
);
var action = (DumbAction)evaluation.Action;
Expand All @@ -56,11 +59,11 @@ public void Constructor()
Assert.Equal(1, evaluation.InputContext.BlockIndex);
Assert.Null(
evaluation.InputContext.PreviousState.GetAccount(
ReservedAddresses.LegacyAccount).GetState(address)
ReservedAddresses.DefaultAccount).GetState(address)
);
Assert.Equal(
(Text)"item",
evaluation.OutputState.GetAccount(ReservedAddresses.LegacyAccount).GetState(address)
evaluation.OutputState.GetAccount(ReservedAddresses.DefaultAccount).GetState(address)

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (macos-latest)

Line must be no longer than 100 characters (now 101).

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / check-build

Line must be no longer than 100 characters (now 101).

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / check-build

Line must be no longer than 100 characters (now 101).

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / docs

Line must be no longer than 100 characters (now 101).

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / docs

Line must be no longer than 100 characters (now 101).

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (ubuntu-latest)

Line must be no longer than 100 characters (now 101).

Check warning on line 66 in Libplanet.Action.Tests/ActionEvaluationTest.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (windows-latest)

Line must be no longer than 100 characters (now 101).
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/Common/Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override IWorld Execute(IActionContext context)
IImmutableSet<string> usedWeapons = ImmutableHashSet<string>.Empty;
IImmutableSet<string> targets = ImmutableHashSet<string>.Empty;
IWorld previousState = context.PreviousState;
IAccount legacyAccount = previousState.GetAccount(ReservedAddresses.LegacyAccount);
IAccount legacyAccount = previousState.GetAccount(ReservedAddresses.DefaultAccount);

object value = legacyAccount.GetState(TargetAddress);
if (!ReferenceEquals(value, null))
Expand All @@ -49,7 +49,7 @@ public override IWorld Execute(IActionContext context)
var result = new BattleResult(usedWeapons, targets);
legacyAccount = legacyAccount.SetState(TargetAddress, result.ToBencodex());

return previousState.SetAccount(ReservedAddresses.LegacyAccount, legacyAccount);
return previousState.SetAccount(ReservedAddresses.DefaultAccount, legacyAccount);
}
}
}
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/Common/DelayAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public IWorld Execute(IActionContext context)
Thread.Sleep(MilliSecond);
var ended = DateTimeOffset.UtcNow;
var delayAccount = state
.GetAccount(ReservedAddresses.LegacyAccount)
.GetAccount(ReservedAddresses.DefaultAccount)
.SetState(TrivialUpdatedAddress, new Bencodex.Types.Integer(MilliSecond));
state = state.SetAccount(ReservedAddresses.LegacyAccount, delayAccount);
state = state.SetAccount(ReservedAddresses.DefaultAccount, delayAccount);
Log.Debug(
"{MethodName} Total Executed Time: {Elapsed}. Delay target: {MilliSecond}",
nameof(DelayAction),
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/Common/DetectRehearsal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public override void LoadPlainValue(IValue plainValue)
public override IWorld Execute(IActionContext context)
{
IWorld previousState = context.PreviousState;
IAccount legacyAccount = previousState.GetAccount(ReservedAddresses.LegacyAccount);
IAccount legacyAccount = previousState.GetAccount(ReservedAddresses.DefaultAccount);
ResultState = context.Rehearsal;
return previousState.SetAccount(
ReservedAddresses.LegacyAccount,
ReservedAddresses.DefaultAccount,
legacyAccount.SetState(
TargetAddress,
new Bencodex.Types.Boolean(context.Rehearsal)
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/Common/DumbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public IWorld Execute(IActionContext context)
return world;
}

IAccount account = world.GetAccount(ReservedAddresses.LegacyAccount);
IAccount account = world.GetAccount(ReservedAddresses.DefaultAccount);
string items = (Text?)account.GetState(TargetAddress);
string item = RecordRehearsal
? $"{Item}:{context.Rehearsal}"
Expand Down Expand Up @@ -221,7 +221,7 @@ public IWorld Execute(IActionContext context)
Rehearsal = context.Rehearsal,
});

return world.SetAccount(ReservedAddresses.LegacyAccount, account);
return world.SetAccount(ReservedAddresses.DefaultAccount, account);
}

public void LoadPlainValue(IValue plainValue)
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/Common/MinerReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void LoadPlainValue(Dictionary plainValue)
public IWorld Execute(IActionContext ctx)
{
IWorld states = ctx.PreviousState;
IAccount legacyAccount = states.GetAccount(ReservedAddresses.LegacyAccount);
IAccount legacyAccount = states.GetAccount(ReservedAddresses.DefaultAccount);

string rewardRecord = (Text?)legacyAccount.GetState(RewardRecordAddress);

Expand All @@ -54,7 +54,7 @@ public IWorld Execute(IActionContext ctx)
int reward = previousReward + Reward;

legacyAccount = legacyAccount.SetState(ctx.Miner, (Integer)reward);
return states.SetAccount(ReservedAddresses.LegacyAccount, legacyAccount);
return states.SetAccount(ReservedAddresses.DefaultAccount, legacyAccount);
}
}
}
6 changes: 3 additions & 3 deletions Libplanet.Action.Tests/Common/RandomAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public void LoadPlainValue(IValue plainValue)
public IWorld Execute(IActionContext context)
{
IWorld states = context.PreviousState;
IAccount legacyAccount = states.GetAccount(ReservedAddresses.LegacyAccount);
IAccount legacyAccount = states.GetAccount(ReservedAddresses.DefaultAccount);
if (context.Rehearsal)
{
return states.SetAccount(
ReservedAddresses.LegacyAccount,
ReservedAddresses.DefaultAccount,
legacyAccount.SetState(Address, Null.Value));
}

legacyAccount = legacyAccount.SetState(Address, (Integer)context.GetRandom().Next());
return states.SetAccount(ReservedAddresses.LegacyAccount, legacyAccount);
return states.SetAccount(ReservedAddresses.DefaultAccount, legacyAccount);
}
}
}
4 changes: 2 additions & 2 deletions Libplanet.Action.Tests/Common/SetValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void LoadPlainValue(IValue plainValue)
public IWorld Execute(IActionContext context)
{
IWorld states = context.PreviousState;
IAccount legacyAccount = states.GetAccount(ReservedAddresses.LegacyAccount);
IAccount legacyAccount = states.GetAccount(ReservedAddresses.DefaultAccount);
return states.SetAccount(
ReservedAddresses.LegacyAccount, legacyAccount.SetValidator(Validator));
ReservedAddresses.DefaultAccount, legacyAccount.SetValidator(Validator));
}

/// <inheritdoc cref="IEquatable{T}.Equals(T)"/>
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/Loader/TypedActionLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void LoadAction()
new List<Validator>()
{ new Validator(new PrivateKey().PublicKey, 1) }).Bencoded,
Dictionary.Empty.Add(
ReservedAddresses.LegacyAccount.ToByteArray(),
ReservedAddresses.DefaultAccount.ToByteArray(),
Dictionary.Empty.Add(
default(Address).ToByteArray(), "initial value"))));
var action = new Initialize();
Expand Down
6 changes: 3 additions & 3 deletions Libplanet.Action.Tests/Mocks/MockWorldState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public MockWorldState(
Legacy = Trie
.Get(new[]
{
ToStateKey(ReservedAddresses.LegacyAccount),
ToStateKey(ReservedAddresses.DefaultAccount),
})
.Any(v => v == null);
}
Expand All @@ -36,13 +36,13 @@ public MockWorldState(
public bool Legacy { get; private set; }

public IAccount GetAccount(Address address)
=> Legacy && address.Equals(ReservedAddresses.LegacyAccount)
=> Legacy && address.Equals(ReservedAddresses.DefaultAccount)
? new Account(new MockAccountState(_stateStore, Trie.Hash))
: new Account(new MockAccountState(
_stateStore, new HashDigest<SHA256>(Trie.Get(ToStateKey(address)))));

public IWorldState SetAccountState(Address address, IAccount account)
=> Legacy && address.Equals(ReservedAddresses.LegacyAccount)
=> Legacy && address.Equals(ReservedAddresses.DefaultAccount)
? new MockWorldState(_stateStore, account.Trie.Hash)
: new MockWorldState(
_stateStore,
Expand Down
8 changes: 6 additions & 2 deletions Libplanet.Action.Tests/Sys/InitializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ private static readonly IImmutableDictionary<Address, IValue>
[default] = (Text)"initial value",
}.ToImmutableDictionary();

private readonly ISystemAccounts _systemAccounts = new SystemAccounts(
new SystemAccountsGetter(_ => ReservedAddresses.DefaultAccount), null);

[Fact]
public void Constructor()
{
Expand All @@ -44,11 +47,11 @@ public void Execute()
var random = new System.Random();
Address signer = random.NextAddress();
var prevState = new World(new MockWorldState());
BlockHash genesisHash = random.NextBlockHash();
var context = new ActionContext(
signer: signer,
txid: random.NextTxId(),
miner: random.NextAddress(),
systemAccounts: _systemAccounts,
blockIndex: 0,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: prevState,
Expand All @@ -64,7 +67,7 @@ public void Execute()

Assert.Equal(
_validatorSet,
nextState.GetAccount(ReservedAddresses.LegacyAccount).GetValidatorSet());
nextState.GetAccount(ReservedAddresses.DefaultAccount).GetValidatorSet());
Assert.Equal(
_states[default],
nextState.GetAccount(default).GetState(default));
Expand All @@ -81,6 +84,7 @@ public void ExecuteInNonGenesis()
signer: signer,
txid: random.NextTxId(),
miner: random.NextAddress(),
systemAccounts: _systemAccounts,
blockIndex: 10,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: prevState,
Expand Down
6 changes: 5 additions & 1 deletion Libplanet.Action/ActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics.Contracts;
using System.Threading;
using Libplanet.Action.State;
using Libplanet.Crypto;
Expand All @@ -17,6 +16,7 @@ public ActionContext(
Address signer,
TxId? txid,
Address miner,
ISystemAccounts systemAccounts,
long blockIndex,
int blockProtocolVersion,
IWorld previousState,
Expand All @@ -27,6 +27,7 @@ public ActionContext(
Signer = signer;
TxId = txid;
Miner = miner;
SystemAccounts = systemAccounts;
BlockIndex = blockIndex;
BlockProtocolVersion = blockProtocolVersion;
Rehearsal = rehearsal;
Expand All @@ -46,6 +47,9 @@ public ActionContext(
/// <inheritdoc cref="IActionContext.Miner"/>
public Address Miner { get; }

/// <inheritdoc cref="IActionContext.SystemAccounts"/>
public ISystemAccounts SystemAccounts { get; }

/// <inheritdoc cref="IActionContext.BlockIndex"/>
public long BlockIndex { get; }

Expand Down
Loading

0 comments on commit 6fb4b41

Please sign in to comment.