Skip to content

Commit

Permalink
test: Update tests
Browse files Browse the repository at this point in the history
Co-authored-by: Chanhyuck Ko <[email protected]>
Co-authored-by: Lee, Suho <[email protected]>
(cherry picked from commit 9ab368f)
  • Loading branch information
OnedgeLee committed Oct 18, 2023
1 parent 4050dd3 commit 5827699
Show file tree
Hide file tree
Showing 48 changed files with 1,182 additions and 361 deletions.
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: World.Create(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: World.Create(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: World.Create(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: World.Create(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: World.Create(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),
World.Create(new MockWorldState()),
123,
0,
false),
new Account(MockAccountState.Empty.SetState(address, (Text)"item")));
World.Create(
new MockWorldState().SetAccount(
ReservedAddresses.LegacyAccount,
new Account(MockAccountState.Empty.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

0 comments on commit 5827699

Please sign in to comment.