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

Decompress in get state #6905

Merged
merged 1 commit into from
Mar 4, 2025
Merged
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
49 changes: 34 additions & 15 deletions nekoyume/Assets/_Scripts/Blockchain/RPCAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public IValue GetState(Address accountAddress, Address address)
accountAddress.ToByteArray(),
address.ToByteArray()
).ResponseAsync.Result;
return _codec.Decode(raw);
return DeCompressState(raw);
}

public IValue GetState(HashDigest<SHA256> stateRootHash, Address accountAddress, Address address)
Expand All @@ -259,7 +259,7 @@ public IValue GetState(HashDigest<SHA256> stateRootHash, Address accountAddress,
accountAddress.ToByteArray(),
address.ToByteArray()
).ResponseAsync.Result;
return _codec.Decode(raw);
return DeCompressState(raw);
}

public async Task<IValue> GetStateAsync(Address accountAddress, Address address)
Expand Down Expand Up @@ -295,7 +295,7 @@ public async Task<IValue> GetStateAsync(BlockHash blockHash, Address accountAddr
blockHash.ToByteArray(),
accountAddress.ToByteArray(),
address.ToByteArray());
var decoded = _codec.Decode(bytes);
var decoded = DeCompressState(bytes);
var game = Game.Game.instance;
if (game.CachedStateAddresses.ContainsKey(accountAddress.Derive(address.ToByteArray())))
{
Expand All @@ -316,7 +316,7 @@ public async Task<IValue> GetStateAsync(HashDigest<SHA256> stateRootHash, Addres
stateRootHash.ToByteArray(),
accountAddress.ToByteArray(),
address.ToByteArray());
var decoded = _codec.Decode(bytes);
var decoded = DeCompressState(bytes);
var game = Game.Game.instance;
if (game.CachedStateAddresses.ContainsKey(accountAddress.Derive(address.ToByteArray())))
{
Expand Down Expand Up @@ -411,7 +411,7 @@ public async Task<FungibleAssetValue> GetBalanceAsync(
BlockTipHash.ToByteArray(),
address.ToByteArray(),
_codec.Encode(currency.Serialize()));
var serialized = (List)_codec.Decode(raw);
var serialized = (List)DeCompressState(raw);
return FungibleAssetValue.FromRawValue(
new Currency(serialized.ElementAt(0)),
serialized.ElementAt(1).ToBigInteger());
Expand All @@ -426,7 +426,7 @@ public async Task<FungibleAssetValue> GetBalanceAsync(
stateRootHash.ToByteArray(),
address.ToByteArray(),
_codec.Encode(currency.Serialize()));
var serialized = (List)_codec.Decode(raw);
var serialized = (List)DeCompressState(raw);
return FungibleAssetValue.FromRawValue(
new Currency(serialized.ElementAt(0)),
serialized.ElementAt(1).ToBigInteger());
Expand All @@ -437,7 +437,7 @@ public async Task<Integer> GetUnbondClaimableHeightByStateRootHashAsync(HashDige
var raw = await _service.GetUnbondClaimableHeightByStateRootHash(
stateRootHash.ToByteArray(),
address.ToByteArray());
return (Integer)_codec.Decode(raw);
return (Integer)DeCompressState(raw);
}

/// <summary>
Expand All @@ -451,7 +451,7 @@ public async Task<List> GetClaimableRewardsByStateRootHashAsync(HashDigest<SHA25
var raw = await _service.GetClaimableRewardsByStateRootHash(
stateRootHash.ToByteArray(),
address.ToByteArray());
return (List)_codec.Decode(raw);
return (List)DeCompressState(raw);
}

/// <summary>
Expand All @@ -469,15 +469,15 @@ public async Task<List> GetDelegationInfoByStateRootHashAsync(HashDigest<SHA256>
var raw = await _service.GetDelegationInfoByStateRootHash(
stateRootHash.ToByteArray(),
address.ToByteArray());
return (List)_codec.Decode(raw);
return (List)DeCompressState(raw);
}

public async Task<FungibleAssetValue> GetStakedByStateRootHashAsync(HashDigest<SHA256> stateRootHash, Address address)
{
var raw = await _service.GetStakedByStateRootHash(
stateRootHash.ToByteArray(),
address.ToByteArray());
var serialized = (List)_codec.Decode(raw);
var serialized = (List)DeCompressState(raw);
return FungibleAssetValue.FromRawValue(
new Currency(serialized.ElementAt(0)),
serialized.ElementAt(1).ToBigInteger());
Expand Down Expand Up @@ -516,7 +516,7 @@ public async Task<AgentState> GetAgentStateAsync(HashDigest<SHA256> stateRootHas

private AgentState ResolveAgentState(byte[] raw)
{
var value = _codec.Decode(raw);
var value = DeCompressState(raw);
if (value is Dictionary dict)
{
return new AgentState(dict);
Expand All @@ -541,6 +541,8 @@ public async Task<Dictionary<Address, AvatarState>> GetAvatarStatesAsync(
var result = new Dictionary<Address, AvatarState>();
foreach (var kv in raw)
{
var size = Buffer.ByteLength(kv.Value);
NcDebug.Log($"[GetAvatarState/{kv.Key}] buffer size: {size}");
result[new Address(kv.Key)] = ResolveAvatarState(kv.Value);
}

Expand Down Expand Up @@ -589,7 +591,7 @@ public async Task<Dictionary<Address, AvatarState>> GetAvatarStatesAsync(
private AvatarState ResolveAvatarState(byte[] raw)
{
AvatarState avatarState;
if (!(_codec.Decode(raw) is List full))
if (!(DeCompressState(raw) is List full))
{
NcDebug.LogError("Given raw is not a format of the AvatarState.");
return null;
Expand Down Expand Up @@ -673,7 +675,7 @@ await _service.GetBulkStateByStateRootHash(
var result = new Dictionary<Address, IValue>();
foreach (var kv in raw)
{
result[new Address(kv.Key)] = _codec.Decode(kv.Value);
result[new Address(kv.Key)] = DeCompressState(kv.Value);
}

return result;
Expand Down Expand Up @@ -1156,7 +1158,7 @@ private async Task Join(bool isRetry = false)

public void OnReorged(byte[] oldTip, byte[] newTip, byte[] branchpoint)
{
var dict = (Dictionary)_codec.Decode(newTip);
var dict = (Dictionary)DeCompressState(newTip);
var newTipBlock = BlockMarshaler.UnmarshalBlock(dict);
BlockIndex = newTipBlock.Index;
BlockIndexSubject.OnNext(BlockIndex);
Expand Down Expand Up @@ -1265,10 +1267,27 @@ public async UniTask<bool> IsTxStagedAsync(TxId txId)
return blockIndex.HasValue
? _blockHashCache.TryGetBlockHash(blockIndex.Value, out var outBlockHash)
? outBlockHash
: _codec.Decode(await _service.GetBlockHash(blockIndex.Value)) is { } rawBlockHash
: DeCompressState(await _service.GetBlockHash(blockIndex.Value)) is { } rawBlockHash
? new BlockHash(rawBlockHash)
: (BlockHash?)null
: BlockTipHash;
}

private IValue DeCompressState(byte[] compressed)
{
using (var cp = new MemoryStream(compressed))
{
using (var decompressed = new MemoryStream())
{
using (var df = new DeflateStream(cp, CompressionMode.Decompress))
{
df.CopyTo(decompressed);
decompressed.Seek(0, SeekOrigin.Begin);
var dec = decompressed.ToArray();
return _codec.Decode(dec);
}
}
}
}
}
}
Loading