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

example for hardfork DailyReward #2492

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
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
10 changes: 5 additions & 5 deletions .Lib9c.Tests/Action/DailyRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public void Execute(bool legacy)
var nextState = ExecuteInternal(previousStates, 2448);
var nextGameConfigState = nextState.GetGameConfigState();
nextState.TryGetAvatarState(_agentAddress, _avatarAddress, out var nextAvatarState);
Assert.NotNull(nextAvatarState);
Assert.NotNull(nextAvatarState.inventory);
Assert.NotNull(nextAvatarState.questList);
Assert.NotNull(nextAvatarState.worldInformation);
Assert.Equal(nextGameConfigState.ActionPointMax, nextAvatarState.actionPoint);
var dailyRewardInfo = nextState.GetDailyRewardInfo(_avatarAddress);
Assert.NotNull(dailyRewardInfo);
Assert.Equal(2448L, dailyRewardInfo.ReceivedBlockIndex);
var actionPoint = nextState.GetActionPoint(_avatarAddress);
Assert.Equal(nextGameConfigState.ActionPointMax, actionPoint);

var avatarRuneAmount = nextState.GetBalance(_avatarAddress, RuneHelper.DailyRewardRune);
var expectedRune = RuneHelper.DailyRewardRune * nextGameConfigState.DailyRuneRewardAmount;
Expand Down
25 changes: 17 additions & 8 deletions Lib9c/Action/DailyReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ public override IWorld Execute(IActionContext context)
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}DailyReward exec started", addressesHex);

if (!states.TryGetAvatarState(context.Signer, avatarAddress, out AvatarState avatarState))
if (!states.TryGetDailyRewardInfo(context.Signer, avatarAddress, out var dailyRewardInfo))
{
throw new FailedLoadStateException(
$"{addressesHex}Aborted as the avatar state of the signer was failed to load.");
var agentState = states.GetAgentState(context.Signer);
if (agentState is not null && agentState.avatarAddresses.ContainsValue(avatarAddress))
{
dailyRewardInfo =
new DailyRewardModule.DailyRewardInfo(context.Signer, 0L);
}
else
{
throw new FailedLoadStateException("failed to load agent state.");
}
}

var gameConfigState = states.GetGameConfigState();
Expand All @@ -48,18 +56,17 @@ public override IWorld Execute(IActionContext context)
throw new FailedLoadStateException($"{addressesHex}Aborted as the game config was failed to load.");
}

if (context.BlockIndex < avatarState.dailyRewardReceivedIndex + gameConfigState.DailyRewardInterval)
if (context.BlockIndex < dailyRewardInfo.ReceivedBlockIndex + gameConfigState.DailyRewardInterval)
{
var sb = new StringBuilder()
.Append($"{addressesHex}Not enough block index to receive daily rewards.")
.Append(
$" Expected: Equals or greater than ({avatarState.dailyRewardReceivedIndex + gameConfigState.DailyRewardInterval}).")
$" Expected: Equals or greater than ({dailyRewardInfo.ReceivedBlockIndex + gameConfigState.DailyRewardInterval}).")
.Append($" Actual: ({context.BlockIndex})");
throw new RequiredBlockIndexException(sb.ToString());
}

avatarState.dailyRewardReceivedIndex = context.BlockIndex;
avatarState.actionPoint = gameConfigState.ActionPointMax;
dailyRewardInfo.ReceivedBlockIndex = context.BlockIndex;

if (gameConfigState.DailyRuneRewardAmount > 0)
{
Expand All @@ -71,7 +78,9 @@ public override IWorld Execute(IActionContext context)

var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}DailyReward Total Executed Time: {Elapsed}", addressesHex, ended - started);
return states.SetAvatarState(avatarAddress, avatarState);
return states
.SetDailyRewardInfo(avatarAddress, dailyRewardInfo)
.SetActionPoint(avatarAddress, gameConfigState.ActionPointMax);
}

protected override IImmutableDictionary<string, IValue> PlainValueInternal => new Dictionary<string, IValue>
Expand Down
2 changes: 2 additions & 0 deletions Lib9c/Addresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static class Addresses
public static readonly Address WorldInformation = new Address("000000000000000000000000000000000000001d");
public static readonly Address QuestList = new Address("000000000000000000000000000000000000001e");
public static readonly Address Collection = new Address("000000000000000000000000000000000000001f");
public static readonly Address DailyReward = new Address("0000000000000000000000000000000000000020");
public static readonly Address ActionPoint = new Address("0000000000000000000000000000000000000021");

public static Address GetSheetAddress<T>() where T : ISheet => GetSheetAddress(typeof(T).Name);

Expand Down
27 changes: 27 additions & 0 deletions Lib9c/Module/ActionPointModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;

namespace Nekoyume.Module
{
public static class ActionPointModule
{
public static long GetActionPoint(this IWorldState worldState, Address address)
{
var value = worldState.GetAccountState(Addresses.ActionPoint).GetState(address);
if (value is Integer integer)
{
return integer;
}

return 0;
}

public static IWorld SetActionPoint(this IWorld world, Address address, long actionPoint)
{
var account = world.GetAccount(Addresses.ActionPoint);
account = account.SetState(address, (Integer)actionPoint);
return world.SetAccount(Addresses.ActionPoint, account);
}
}
}
69 changes: 69 additions & 0 deletions Lib9c/Module/DailyRewardModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Action;
using Nekoyume.Model.State;

namespace Nekoyume.Module
{
public static class DailyRewardModule
{
public class DailyRewardInfo : IBencodable
{
public Address AgentAddress;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어차피 이 IValue의 Address를 AvatarAddress로 가져올거면 굳이 AgentAddress를 들고있을 이유가 있을까요?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 statestore 에서 직접 값 읽어보고 할 때 편하긴 할 겁니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상태만 덜렁 들고있을때 이게 어떤 아바타의 DailyRewardInfo인지 헷갈릴 것 같다 싶긴했는데, 음... 네 상관없을 것 같네요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제약조건(아바타어드레스가 에이전트어드레스종속적인지)를 확인할 방법이 AgentState나 AvatarState를 직접 확인하는 방법밖에 없었어서 DailyRewardInfo에 넣어놨는데, 생각해보니 별도 어카운트에 넣어두거나(다른 액션들도 확인할 수 있으니) 액션안에서는 Derive를 해보는게 낫겠다 싶네요.

public long ReceivedBlockIndex;

public DailyRewardInfo(Address address, long blockIndex)
{
AgentAddress = address;
ReceivedBlockIndex = blockIndex;
}

public IValue Bencoded => List.Empty.Add(AgentAddress.Serialize()).Add(ReceivedBlockIndex);
}

public static DailyRewardInfo GetDailyRewardInfo(this IWorldState worldState,
Address avatarAddress)
{
IAccountState account = worldState.GetAccountState(Addresses.DailyReward);
var value = account.GetState(avatarAddress);
if (value is List l)
{
return new DailyRewardInfo(l[0].ToAddress(), (Integer)l[1]);
}

throw new FailedLoadStateException("");
}

public static bool TryGetDailyRewardInfo(this IWorldState worldState,
Address agentAddress, Address avatarAddress, out DailyRewardInfo dailyRewardInfo)
{
dailyRewardInfo = null;
try
{
var temp= GetDailyRewardInfo(worldState, avatarAddress);
if (!temp.AgentAddress.Equals(agentAddress))
{
return false;
}

dailyRewardInfo = temp;
return true;
}
catch (FailedLoadStateException)
{
return false;
}
}

public static IWorld SetDailyRewardInfo(this IWorld world, Address avatarAddress,
DailyRewardInfo dailyRewardInfo)
{
IAccount account = world.GetAccount(Addresses.DailyReward);
account = account.SetState(avatarAddress, dailyRewardInfo.Bencoded);
return world.SetAccount(Addresses.DailyReward, account);
}
}
}
Loading