Skip to content

Commit

Permalink
Merge pull request #6648 from planetarium/improve/use-state-root-hash
Browse files Browse the repository at this point in the history
Use GetBulkStateByStateRootHash instead of GetBulkStateByBlockHash
  • Loading branch information
ipdae authored Dec 26, 2024
2 parents aea1618 + 6f35d26 commit 5905547
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 43 deletions.
2 changes: 1 addition & 1 deletion nekoyume/Assets/_Scripts/Blockchain/ActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected GoldBalanceState GetGoldBalanceState<T>(ActionEvaluation<T> evaluation
}

var sheetStates = await agent.GetStateBulkAsync(
ReservedAddresses.LegacyAccount, sheetAddrArr);
agent.BlockTipStateRootHash, ReservedAddresses.LegacyAccount, sheetAddrArr);
var stakeRegularFixedRewardSheet = new StakeRegularFixedRewardSheet();
stakeRegularFixedRewardSheet.Set(
sheetStates[sheetAddrArr[0]].ToDotnetString());
Expand Down
12 changes: 0 additions & 12 deletions nekoyume/Assets/_Scripts/Blockchain/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,6 @@ public async Task<Dictionary<Address, AvatarState>> GetAvatarStatesAsync(
return dict;
}

public async Task<Dictionary<Address, IValue>> GetStateBulkAsync(Address accountAddress, IEnumerable<Address> addressList)
{
var dict = new Dictionary<Address, IValue>();
foreach (var address in addressList)
{
var result = await await Task.FromResult(GetStateAsync(accountAddress, address));
dict[address] = result;
}

return dict;
}

public async Task<Dictionary<Address, IValue>> GetStateBulkAsync(
HashDigest<SHA256> stateRootHash,
Address accountAddress,
Expand Down
4 changes: 0 additions & 4 deletions nekoyume/Assets/_Scripts/Blockchain/IAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ Task<Dictionary<Address, AvatarState>> GetAvatarStatesAsync(
HashDigest<SHA256> stateRootHash,
IEnumerable<Address> addressList);

Task<Dictionary<Address, IValue>> GetStateBulkAsync(
Address accountAddress,
IEnumerable<Address> addressList);

Task<Dictionary<Address, IValue>> GetStateBulkAsync(
HashDigest<SHA256> stateRootHash,
Address accountAddress,
Expand Down
16 changes: 0 additions & 16 deletions nekoyume/Assets/_Scripts/Blockchain/RPCAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,22 +607,6 @@ private AvatarState ResolveAvatarState(byte[] raw)
return avatarState;
}

public async Task<Dictionary<Address, IValue>> GetStateBulkAsync(Address accountAddress, IEnumerable<Address> addressList)
{
var raw =
await _service.GetBulkStateByBlockHash(
BlockTipHash.ToByteArray(),
accountAddress.ToByteArray(),
addressList.Select(a => a.ToByteArray()));
var result = new Dictionary<Address, IValue>();
foreach (var kv in raw)
{
result[new Address(kv.Key)] = _codec.Decode(kv.Value);
}

return result;
}

public async Task<Dictionary<Address, IValue>> GetStateBulkAsync(
HashDigest<SHA256> stateRootHash,
Address accountAddress,
Expand Down
6 changes: 4 additions & 2 deletions nekoyume/Assets/_Scripts/Extensions/GetStateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class GetStateExtensions
ItemSlotState.DeriveAddress(avatarAddress, BattleType.Raid)
};
var itemBulk = await agent.GetStateBulkAsync(
agent.BlockTipStateRootHash,
ReservedAddresses.LegacyAccount,
itemAddresses);
var itemSlotStates = new List<ItemSlotState>();
Expand All @@ -47,6 +48,7 @@ public static class GetStateExtensions
RuneSlotState.DeriveAddress(avatarAddress, BattleType.Raid)
};
var runeBulk = await Game.Game.instance.Agent.GetStateBulkAsync(
Game.Game.instance.Agent.BlockTipStateRootHash,
ReservedAddresses.LegacyAccount,
runeAddresses);
var runeSlotStates = new List<RuneSlotState>();
Expand Down Expand Up @@ -78,7 +80,7 @@ public static async Task<AllCombinationSlotState> GetAllCombinationSlotStateAsyn
{
combinationSlotAddresses.Add(CombinationSlotState.DeriveAddress(avatarAddress, i));
}
var bulkStates = await agent.GetStateBulkAsync(ReservedAddresses.LegacyAccount, combinationSlotAddresses);
var bulkStates = await agent.GetStateBulkAsync(agent.BlockTipStateRootHash, ReservedAddresses.LegacyAccount, combinationSlotAddresses);
allCombinationSlotState = new AllCombinationSlotState();

for (var i = 0; i < AvatarState.DefaultCombinationSlotCount; i++)
Expand Down Expand Up @@ -142,7 +144,7 @@ public static async Task<AllRuneState> GetAllRuneStateAsync(
var runeAddresses = runeListSheet.Values.Select(row =>
RuneState.DeriveAddress(avatarAddress, row.Id));
var stateBulk = await Game.Game.instance.Agent.GetStateBulkAsync(
ReservedAddresses.LegacyAccount, runeAddresses);
Game.Game.instance.Agent.BlockTipStateRootHash, ReservedAddresses.LegacyAccount, runeAddresses);
foreach (var runeSerialized in stateBulk.Values.OfType<List>())
{
allRuneState.AddRuneState(new RuneState(runeSerialized));
Expand Down
17 changes: 9 additions & 8 deletions nekoyume/Assets/_Scripts/State/States.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class States
public FungibleAssetValue CrystalBalance { get; private set; }

public AllRuneState AllRuneState { get; private set; }

public AllCombinationSlotState AllCombinationSlotState { get; private set; }

public readonly ConcurrentDictionary<int, Dictionary<BattleType, RuneSlotState>>
Expand All @@ -86,12 +86,12 @@ public readonly ConcurrentDictionary<int, Dictionary<BattleType, ItemSlotState>>
private class Workshop
{
private readonly ConcurrentDictionary<int, CombinationSlotState> _states = new();

public void UpdateCombinationSlotState(int index, CombinationSlotState state)
{
_states[index] = state;
}

/// <summary>
/// If you want to update the state, use <see cref="UpdateCombinationSlotState"/>.
/// </summary>
Expand Down Expand Up @@ -216,7 +216,7 @@ public void SetAllRuneState(AllRuneState allRuneState)
{
AllRuneState = allRuneState;
}

private void SetAllCombinationSlotState(Address avatarAddress, AllCombinationSlotState allCombinationSlotState)
{
LocalLayer.Instance.InitializeCombinationSlots(allCombinationSlotState);
Expand Down Expand Up @@ -251,7 +251,7 @@ public async UniTask InitRuneSlotStates()
RuneSlotState.DeriveAddress(avatarState.address, BattleType.Arena),
RuneSlotState.DeriveAddress(avatarState.address, BattleType.Raid)
};
var stateBulk = await Game.Game.instance.Agent.GetStateBulkAsync(ReservedAddresses.LegacyAccount, addresses);
var stateBulk = await Game.Game.instance.Agent.GetStateBulkAsync(Game.Game.instance.Agent.BlockTipStateRootHash, ReservedAddresses.LegacyAccount, addresses);
foreach (var value in stateBulk.Values)
{
if (value is List list)
Expand Down Expand Up @@ -321,7 +321,7 @@ public async UniTask InitItemSlotStates()
ItemSlotState.DeriveAddress(avatarState.address, BattleType.Arena),
ItemSlotState.DeriveAddress(avatarState.address, BattleType.Raid)
};
var stateBulk = await agent.GetStateBulkAsync(ReservedAddresses.LegacyAccount, addresses);
var stateBulk = await agent.GetStateBulkAsync(agent.BlockTipStateRootHash, ReservedAddresses.LegacyAccount, addresses);
foreach (var value in stateBulk.Values)
{
if (value is List list)
Expand Down Expand Up @@ -359,7 +359,7 @@ private async UniTask InitItemSlotState(int slotIndex, AvatarState avatarState)
ItemSlotState.DeriveAddress(avatarState.address, BattleType.Raid)
};

var stateBulk = await Game.Game.instance.Agent.GetStateBulkAsync(ReservedAddresses.LegacyAccount, addresses);
var stateBulk = await Game.Game.instance.Agent.GetStateBulkAsync(Game.Game.instance.Agent.BlockTipStateRootHash, ReservedAddresses.LegacyAccount, addresses);
foreach (var value in stateBulk.Values)
{
if (value is List list)
Expand Down Expand Up @@ -726,7 +726,7 @@ public IDictionary<int, CombinationSlotState> GetCombinationSlotState(AvatarStat
{
return !_slotStates.ContainsKey(avatarState.address) ? null : _slotStates[avatarState.address].States;
}

public void SetGameConfigState(GameConfigState state)
{
GameConfigState = state;
Expand Down Expand Up @@ -782,6 +782,7 @@ public void UpdateHammerPointStates(IEnumerable<int> recipeIds)
.ToList();
var states =
await Game.Game.instance.Agent.GetStateBulkAsync(
Game.Game.instance.Agent.BlockTipStateRootHash,
ReservedAddresses.LegacyAccount,
hammerPointStateAddresses.Select(tuple => tuple.Item1));
var joinedStates = states.Join(
Expand Down

0 comments on commit 5905547

Please sign in to comment.