Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/system accounts #3494

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 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. [[#3494]]
- (Libplanet.Action) New property `SystemAccountsGetter` added to
`IBlockPolicy` interface. [[#3494]]


### 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. [[#3494]
- (Libplanet.Action) Added `ISystemAccountsGetter` interface. [[#3494]]
- (Libplanet.Explorer) Added `AccountStateType` class. [[#3462]]
- (Libplanet.Explorer) Added `WorldStateType` class. [[#3462]]
- (Libplanet.Explorer) Added `WorldStateType` class. [[#3462]]
Expand Down Expand Up @@ -117,6 +123,7 @@ To be released.
### CLI tools

[#3462]: https://github.com/planetarium/libplanet/pull/3462
[#3494]: https://github.com/planetarium/libplanet/pull/3494


Version 3.7.0
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())
);
}
}
14 changes: 9 additions & 5 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 @@ -27,18 +29,19 @@ public ActionEvaluationTest(ITestOutputHelper output)
[Fact]
public void Constructor()
{
var txid = new System.Random().NextTxId();
Address address = new PrivateKey().ToAddress();
IWorld world = new World(new MockWorldState());
world = world.SetAccount(
ReservedAddresses.LegacyAccount,
world.GetAccount(ReservedAddresses.LegacyAccount).SetState(address, (Text)"item"));
var txid = new System.Random().NextTxId();
ReservedAddresses.DefaultAccount,
world.GetAccount(ReservedAddresses.DefaultAccount).SetState(address, (Text)"item"));
var evaluation = new ActionEvaluation(
new DumbAction(address, "item"),
new ActionContext(
address,
txid,
address,
_systemAccounts,
1,
Block.CurrentProtocolVersion,
new World(new MockWorldState()),
Expand All @@ -57,11 +60,12 @@ 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)
);
}
}
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 @@ -27,7 +27,7 @@ public MockWorldState(
Legacy = Trie
.Get(new[]
{
ToStateKey(ReservedAddresses.LegacyAccount),
ToStateKey(ReservedAddresses.DefaultAccount),
})
.Any(v => v == null);
}
Expand All @@ -37,7 +37,7 @@ 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,
Expand All @@ -46,7 +46,7 @@ public IAccount GetAccount(Address address)
: null));

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
10 changes: 7 additions & 3 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,10 +67,10 @@ public void Execute()

Assert.Equal(
_validatorSet,
nextState.GetAccount(ReservedAddresses.LegacyAccount).GetValidatorSet());
nextState.GetAccount(ReservedAddresses.DefaultAccount).GetValidatorSet());
Assert.Equal(
_states[default],
nextState.GetAccount(ReservedAddresses.LegacyAccount).GetState(default));
nextState.GetAccount(ReservedAddresses.DefaultAccount).GetState(default));
}

[Fact]
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
Loading