Skip to content

Commit

Permalink
bump: NineChronicles.RPC.Shared
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Oct 27, 2023
1 parent eee5713 commit 233b1d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
43 changes: 30 additions & 13 deletions NineChronicles.Headless/BlockChainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Bencodex;
using Bencodex.Types;
using Libplanet.Blockchain;
Expand All @@ -19,7 +18,6 @@
using MagicOnion;
using MagicOnion.Server;
using Nekoyume;
using Nekoyume.Helper;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Shared.Services;
Expand Down Expand Up @@ -127,9 +125,9 @@ public UnaryResult<byte[]> GetState(byte[] addressBytes, byte[] accountBytes, by
return new UnaryResult<byte[]>(encoded);
}

public UnaryResult<byte[]> GetStateBySrh(byte[] addressBytes, byte[] stateRootHashBytes)
public UnaryResult<byte[]> GetState(byte[] addressBytes, byte[] accountStateRootHashBytes)
{
var stateRootHash = new HashDigest<SHA256>(stateRootHashBytes);
var stateRootHash = new HashDigest<SHA256>(accountStateRootHashBytes);
var address = new Address(addressBytes);
IValue state = _blockChain.GetAccountState(stateRootHash).GetState(address);
byte[] encoded = _codec.Encode(state ?? Null.Value);
Expand All @@ -148,11 +146,11 @@ public UnaryResult<Dictionary<byte[], byte[]>> GetAvatarStates(
return new UnaryResult<Dictionary<byte[], byte[]>>(result);
}

public UnaryResult<Dictionary<byte[], byte[]>> GetAvatarStatesByOffsetSrh(
public UnaryResult<Dictionary<byte[], byte[]>> GetAvatarStatesByWorldSrh(
IEnumerable<byte[]> addressBytesList,
byte[] stateRootHashBytes)
byte[] worldStateRootHashBytes)
{
var stateRootHash = new HashDigest<SHA256>(stateRootHashBytes);
var stateRootHash = new HashDigest<SHA256>(worldStateRootHashBytes);
var addresses = addressBytesList.Select(a => new Address(a)).ToList();
var worldState = _blockChain.GetWorldState(stateRootHash);
var avatarStates = addresses.Select(address => AvatarModule.GetAvatarState(worldState, address));
Expand All @@ -178,11 +176,11 @@ public UnaryResult<Dictionary<byte[], byte[]>> GetStateBulk(
return new UnaryResult<Dictionary<byte[], byte[]>>(result);
}

public UnaryResult<Dictionary<byte[], byte[]>> GetStateBulkBySrh(
public UnaryResult<Dictionary<byte[], byte[]>> GetStateBulk(
IEnumerable<byte[]> addressBytesList,
byte[] stateRootHashBytes)
byte[] accountStateRootHashBytes)
{
var stateRootHash = new HashDigest<SHA256>(stateRootHashBytes);
var stateRootHash = new HashDigest<SHA256>(accountStateRootHashBytes);
var result = new Dictionary<byte[], byte[]>();
Address[] addresses = addressBytesList.Select(b => new Address(b)).ToArray();
IReadOnlyList<IValue> values = _blockChain.GetAccountState(stateRootHash).GetStates(addresses);
Expand All @@ -194,13 +192,32 @@ public UnaryResult<Dictionary<byte[], byte[]>> GetStateBulkBySrh(
return new UnaryResult<Dictionary<byte[], byte[]>>(result);
}

public UnaryResult<byte[]> GetBalance(byte[] addressBytes, byte[] currencyBytes, byte[] blockHashBytes)
public UnaryResult<Dictionary<byte[], byte[]>> GetStateBulkByWorldSrh(
IEnumerable<byte[]> addressBytesList,
byte[] accountBytes,
byte[] worldtStateRootHashBytes)
{
var stateRootHash = new HashDigest<SHA256>(worldtStateRootHashBytes);
var accountAddress = new Address(accountBytes);
var result = new Dictionary<byte[], byte[]>();
Address[] addresses = addressBytesList.Select(b => new Address(b)).ToArray();
IReadOnlyList<IValue> values = _blockChain.GetWorldState(stateRootHash).GetAccount(accountAddress).GetStates(addresses);
for (int i = 0; i < addresses.Length; i++)
{
result.TryAdd(addresses[i].ToByteArray(), _codec.Encode(values[i] ?? Null.Value));
}

return new UnaryResult<Dictionary<byte[], byte[]>>(result);
}

public UnaryResult<byte[]> GetBalance(byte[] addressBytes, byte[] currencyBytes, byte[] accountBytes, byte[] blockHashBytes)
{
var address = new Address(addressBytes);
var serializedCurrency = (Bencodex.Types.Dictionary)_codec.Decode(currencyBytes);
Currency currency = CurrencyExtensions.Deserialize(serializedCurrency);
var accountAddress = new Address(accountBytes);
var hash = new BlockHash(blockHashBytes);
FungibleAssetValue balance = LegacyModule.GetBalance(_blockChain.GetWorldState(hash), address, currency);
FungibleAssetValue balance = _blockChain.GetWorldState(hash).GetAccount(accountAddress).GetBalance(address, currency);
byte[] encoded = _codec.Encode(
new Bencodex.Types.List(
new IValue[]
Expand All @@ -213,7 +230,7 @@ public UnaryResult<byte[]> GetBalance(byte[] addressBytes, byte[] currencyBytes,
return new UnaryResult<byte[]>(encoded);
}

public UnaryResult<byte[]> GetBalanceBySrh(byte[] addressBytes, byte[] currencyBytes, byte[] stateRootHashBytes)
public UnaryResult<byte[]> GetBalance(byte[] addressBytes, byte[] currencyBytes, byte[] stateRootHashBytes)
{
var address = new Address(addressBytes);
var stateRootHash = new HashDigest<SHA256>(stateRootHashBytes);
Expand Down
2 changes: 1 addition & 1 deletion NineChronicles.RPC.Shared

0 comments on commit 233b1d9

Please sign in to comment.