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

Introduce IWorld #3462

Merged
merged 21 commits into from
Nov 13, 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
94 changes: 94 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,104 @@ To be released.

### Backward-incompatible API changes

- Removed `BlockChainStates` class. [[#3462]]
- (Libplanet.Action) Changed `ActionEvaluator` to accept `IWorld`
instead of `IAccount`. [[#3462]]
- (Libplanet.Action) `IActionEvaluator.OutputState` became `IWorld`.
(was `IAccount`) [[#3462]]
- (Libplanet.Action) Added
`ActionEvaluationsExtensions.GetLegacyRawTotalDelta()` extension method.
[[#3462]]
- (Libplanet.Action) `ActionEvaluationsExtensions.GetRawTotalDelta()`
became to return
`IImmutableDictionary<KeyBytes, IImmutableDictionary<KeyBytes, IValue>>`.
[[#3462]]
- (Libplanet.Action) `IAction.Execute()` became to return `IWorld`.
(was `IAccount`) [[#3462]]
- (Libplanet.Action) `IActionContext.PreviousState` became `IWorld`.
(was `IAccount`) [[#3462]]
- (Libplanet.Action) Following methods in `IFeeCollector` interface
became to accept and return `IWorld`. (was `IAccount`) [[#3462]]
- `IFeeCollector.Mortgage()`
- `IFeeCollector.Refund()`
- `IFeeCollector.Reward()`
- (Libplanet.Action) Renamed `AccountState` class to `AccountBaseState`.
[[#3462]]
- (Libplanet.Action) `IBlockChainStates` interface has modified. [[#3462]]
- Added `IBlockChainStates.GetWorldState()` method.
- Added `IBlockChainStates.GetAccountState(Address, BlockHash?)` method.
- Added `IBlockChainStates.GetState(Address, Address, BlockHash?)` method.
- Added `IBlockChainStates.GetState(Address, HashDigest<SHA256>?)` method.
- Added
`IBlockChainStates.GetBalance(Address, Currency, Address, BlockHash?)`
method.
- Added
`IBlockChainStates.GetBalance(Address, Currency, HashDigest<SHA256>?)`
method.
- Added
`IBlockChainStates.GetTotalSupply(Currency, Address, BlockHash?)`
method.
- Added
`IBlockChainStates.GetTotalSupply(Currency, HashDigest<SHA256>?)`
method.
- Added `IBlockChainStates.GetValidatorSet(Address, BlockHash?)` method.
- Added `IBlockChainStates.GetValidatorSet(HashDigest<SHA256>?)` method.
- Removed `IBlockChainStates.GetAccountState(BlockHash?)` method.
- Removed `IBlockChainStates.GetState(Address, BlockHash?)` method.
- Removed
`IBlockChainStates.GetStates(IReadOnlyList<Address>, BlockHash?)`
method.
- Removed
`IBlockChainStates.GetBalance(Address, Currency, BlockHash?)`
method.
- Removed `IBlockChainStates.GetTotalSupply(Currency, BlockHash?)` method.
- Removed `IBlockChainStates.GetValidatorSet(BlockHash?)` method.
- (Libplanet.Action) `Initialize.Initialize()` method became to accept
`IImmutableDictionary<Address, IImmutableDictionary<Address, IValue>>`.
(was `IImmutableDictionary<Address, IValue>`) [[#3462]]
- (Libplanet.Action) Type of `Initialize.States` property became
`IImmutableDictionary<Address, IImmutableDictionary<Address, IValue>>?`.
(was `IImmutableDictionary<Address, IValue>?`) [[#3462]]


### Backward-incompatible network protocol changes

### Backward-incompatible storage format changes

### Added APIs

- (Libplanet.Action) Added `IWorld` interface and its implementation.
[[#3462]]
- Added `World` class.
- (Libplanet.Action) Added `IWorldDelta` interface. [[#3462]]
- (Libplanet.Action) Added `IWorldState` interface and its implementation.
[[#3462]]
- Added `WorldBaseState` class.
- (Libplanet.Action) Added `ReservedAddresses` static class. [[#3462]]
- (Libplanet.Action) Added `WorldDeltaExtensions` static class. [[#3462]]
- (Libplanet.Explorer) Added `AccountStateType` class. [[#3462]]
- (Libplanet.Explorer) Added `WorldStateType` class. [[#3462]]
- (Libplanet.Explorer) Added `WorldStateType` class. [[#3462]]
- (Libplanet.Explorer) Modified some fields of `StateQuery` query. [[#3462]]
- Added `StateQuery.worldState` field.
- Added `StateQuery.accountState` field.
- Added `accountAddress` of type `Address` argument
for `StateQuery.states` field.
- Added `accountStateRootHash` of type `HashDigest<SHA256>?` argument
for `StateQuery.states` field.
- Added `accountAddress` of type `Address` argument
for `StateQuery.balance` field.
- Added `accountStateRootHash` of type `HashDigest<SHA256>?` argument
for `StateQuery.balance` field.
- Added `accountAddress` of type `Address` argument
for `StateQuery.totalSupply` field.
- Added `accountStateRootHash` of type `HashDigest<SHA256>?` argument
for `StateQuery.totalSupply` field.
- Added `accountAddress` of type `Address` argument
for `StateQuery.validators` field.
- Added `accountStateRootHash` of type `HashDigest<SHA256>?` argument
for `StateQuery.validators` field.

### Behavioral changes

### Bug fixes
Expand All @@ -24,6 +116,8 @@ To be released.

### CLI tools

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


Version 3.7.0
-------------
Expand Down
10 changes: 5 additions & 5 deletions Libplanet.Action.Tests/ActionContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void RandomShouldBeDeterministic()
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
previousState: new World(new MockWorldState()),
randomSeed: seed,
gasLimit: 0
);
Expand All @@ -54,7 +54,7 @@ public void GuidShouldBeDeterministic()
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
previousState: new World(new MockWorldState()),
randomSeed: 0,
gasLimit: 0
);
Expand All @@ -65,7 +65,7 @@ public void GuidShouldBeDeterministic()
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
previousState: new World(new MockWorldState()),
randomSeed: 0,
gasLimit: 0
);
Expand All @@ -76,7 +76,7 @@ public void GuidShouldBeDeterministic()
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
previousState: new World(new MockWorldState()),
randomSeed: 1,
gasLimit: 0
);
Expand Down Expand Up @@ -115,7 +115,7 @@ public void GuidVersionAndVariant()
miner: _address,
blockIndex: 1,
blockProtocolVersion: Block.CurrentProtocolVersion,
previousState: new Account(MockAccountState.Empty),
previousState: new World(new MockWorldState()),
randomSeed: i,
gasLimit: 0
);
Expand Down
12 changes: 9 additions & 3 deletions Libplanet.Action.Tests/ActionEvaluationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ this IEnumerable<IActionEvaluation> evaluations
) =>
evaluations.Aggregate(
ImmutableDictionary<Address, IValue>.Empty,
(dirty, ev) => dirty.SetItems(ev.OutputState.GetUpdatedStates())
(dirty, ev) => dirty.SetItems(
ev.OutputState.GetAccount(
ReservedAddresses.LegacyAccount).GetUpdatedStates())
);

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

public static IImmutableDictionary<Currency, FungibleAssetValue>
GetDirtyTotalSupplies(this IEnumerable<IActionEvaluation> evaluations) =>
evaluations.Aggregate(
ImmutableDictionary<Currency, FungibleAssetValue>.Empty,
(dirty, ev) => dirty.SetItems(ev.OutputState.GetUpdatedTotalSupplies())
(dirty, ev) => dirty.SetItems(
ev.OutputState.GetAccount(
ReservedAddresses.LegacyAccount).GetUpdatedTotalSupplies())
);
}
}
13 changes: 9 additions & 4 deletions Libplanet.Action.Tests/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ public void Constructor()
address,
1,
Block.CurrentProtocolVersion,
new Account(MockAccountState.Empty),
new World(new MockWorldState()),
123,
0,
false),
new Account(MockAccountState.Empty.SetState(address, (Text)"item")));
new World(
new MockWorldState().SetAccountState(
ReservedAddresses.LegacyAccount,
new Account(new MockAccountState().SetState(address, (Text)"item"))))
);
var action = (DumbAction)evaluation.Action;

Assert.Equal(address, action.TargetAddress);
Expand All @@ -51,11 +55,12 @@ public void Constructor()
Assert.Equal(address, evaluation.InputContext.Miner);
Assert.Equal(1, evaluation.InputContext.BlockIndex);
Assert.Null(
evaluation.InputContext.PreviousState.GetState(address)
evaluation.InputContext.PreviousState.GetAccount(
ReservedAddresses.LegacyAccount).GetState(address)
);
Assert.Equal(
(Text)"item",
evaluation.OutputState.GetState(address)
evaluation.OutputState.GetAccount(ReservedAddresses.LegacyAccount).GetState(address)
);
}
}
Expand Down
10 changes: 6 additions & 4 deletions Libplanet.Action.Tests/Common/Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public override void LoadPlainValue(IValue plainValue)
TargetAddress = new Address(values["target_address"]);
}

public override IAccount Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IImmutableSet<string> usedWeapons = ImmutableHashSet<string>.Empty;
IImmutableSet<string> targets = ImmutableHashSet<string>.Empty;
IAccount previousState = context.PreviousState;
IWorld previousState = context.PreviousState;
IAccount legacyAccount = previousState.GetAccount(ReservedAddresses.LegacyAccount);

object value = previousState.GetState(TargetAddress);
object value = legacyAccount.GetState(TargetAddress);
if (!ReferenceEquals(value, null))
{
var previousResult = BattleResult.FromBencodex((Bencodex.Types.Dictionary)value);
Expand All @@ -46,8 +47,9 @@ public override IAccount Execute(IActionContext context)
usedWeapons = usedWeapons.Add(Weapon);
targets = targets.Add(Target);
var result = new BattleResult(usedWeapons, targets);
legacyAccount = legacyAccount.SetState(TargetAddress, result.ToBencodex());

return previousState.SetState(TargetAddress, result.ToBencodex());
return previousState.SetAccount(ReservedAddresses.LegacyAccount, legacyAccount);
}
}
}
2 changes: 1 addition & 1 deletion Libplanet.Action.Tests/Common/BaseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BaseAction : IAction

public abstract IValue PlainValue { get; }

public abstract IAccount Execute(IActionContext context);
public abstract IWorld Execute(IActionContext context);

public abstract void LoadPlainValue(IValue plainValue);

Expand Down
7 changes: 5 additions & 2 deletions Libplanet.Action.Tests/Common/DelayAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public IValue PlainValue
}
}

public IAccount Execute(IActionContext context)
public IWorld Execute(IActionContext context)
{
var state = context.PreviousState;
var started = DateTimeOffset.UtcNow;
Expand All @@ -41,7 +41,10 @@ public IAccount Execute(IActionContext context)
MilliSecond);
Thread.Sleep(MilliSecond);
var ended = DateTimeOffset.UtcNow;
state = state.SetState(TrivialUpdatedAddress, new Bencodex.Types.Integer(MilliSecond));
var delayAccount = state
.GetAccount(ReservedAddresses.LegacyAccount)
.SetState(TrivialUpdatedAddress, new Bencodex.Types.Integer(MilliSecond));
state = state.SetAccount(ReservedAddresses.LegacyAccount, delayAccount);
Log.Debug(
"{MethodName} Total Executed Time: {Elapsed}. Delay target: {MilliSecond}",
nameof(DelayAction),
Expand Down
15 changes: 9 additions & 6 deletions Libplanet.Action.Tests/Common/DetectRehearsal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ public override void LoadPlainValue(IValue plainValue)
TargetAddress = new Address(values["target_address"]);
}

public override IAccount Execute(IActionContext context)
public override IWorld Execute(IActionContext context)
{
IAccount previousState = context.PreviousState;
IWorld previousState = context.PreviousState;
IAccount legacyAccount = previousState.GetAccount(ReservedAddresses.LegacyAccount);
ResultState = context.Rehearsal;
return previousState.SetState(
TargetAddress,
new Bencodex.Types.Boolean(context.Rehearsal)
);
return previousState.SetAccount(
ReservedAddresses.LegacyAccount,
legacyAccount.SetState(
TargetAddress,
new Bencodex.Types.Boolean(context.Rehearsal)
));
}
}
}
23 changes: 12 additions & 11 deletions Libplanet.Action.Tests/Common/DumbAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public IValue PlainValue
}
}

public IAccount Execute(IActionContext context)
public IWorld Execute(IActionContext context)
{
if (RehearsalRecords.Value is null)
{
Expand All @@ -144,13 +144,14 @@ public IAccount Execute(IActionContext context)
RehearsalRecords.Value.Add((TargetAddress, Item));
}

IAccount states = context.PreviousState;
IWorld world = context.PreviousState;
if (Item is null)
{
return states;
return world;
}

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

if (RecordRandom)
{
states = states.SetState(
account = account.SetState(
RandomRecordsAddress,
(Integer)context.GetRandom().Next()
);
Expand All @@ -187,11 +188,11 @@ public IAccount Execute(IActionContext context)
Item = Item.ToUpperInvariant();
}

IAccount nextState = states.SetState(TargetAddress, (Text)items);
account = account.SetState(TargetAddress, (Text)items);

if (!(Transfer is null))
{
nextState = nextState.TransferAsset(
account = account.TransferAsset(
context,
sender: Transfer.Item1,
recipient: Transfer.Item2,
Expand All @@ -202,8 +203,8 @@ public IAccount Execute(IActionContext context)

if (!(Validators is null))
{
nextState = Validators.Aggregate(
nextState,
account = Validators.Aggregate(
account,
(current, validator) =>
current.SetValidator(new Validator(validator, BigInteger.One)));
}
Expand All @@ -216,11 +217,11 @@ public IAccount Execute(IActionContext context)
ExecuteRecords.Value = ExecuteRecords.Value.Add(new ExecuteRecord()
{
Action = this,
NextState = nextState,
NextState = account,
Rehearsal = context.Rehearsal,
});

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

public void LoadPlainValue(IValue plainValue)
Expand Down
Loading
Loading