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 86a7854
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 27 deletions.
131 changes: 105 additions & 26 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 @@ -116,21 +114,41 @@ public UnaryResult<bool> PutTransaction(byte[] txBytes)
}
}

public UnaryResult<byte[]> GetState(byte[] addressBytes, byte[] accountBytes, byte[] blockHashBytes)
public UnaryResult<byte[]> GetState(
byte[] addressBytes,
byte[] accountBytes,
byte[] blockHashBytes)
{
var address = new Address(addressBytes);
var accountAddress = new Address(accountBytes);
var hash = new BlockHash(blockHashBytes);
IValue state = _blockChain.GetWorldState(hash).GetAccount(accountAddress).GetState(address);
var blockHash = new BlockHash(blockHashBytes);
IValue state = _blockChain.GetWorldState(blockHash)
.GetAccount(accountAddress).GetState(address);
// FIXME: Null과 null 구분해서 반환해야 할 듯
byte[] encoded = _codec.Encode(state ?? Null.Value);
return new UnaryResult<byte[]>(encoded);
}

public UnaryResult<byte[]> GetStateBySrh(byte[] addressBytes, byte[] stateRootHashBytes)
public UnaryResult<byte[]> GetStateByWorldSrh(
byte[] addressBytes,
byte[] accountBytes,
byte[] worldStateRootHashBytes)
{
var stateRootHash = new HashDigest<SHA256>(stateRootHashBytes);
var address = new Address(addressBytes);
var accountAddress = new Address(accountBytes);
var stateRootHash = new HashDigest<SHA256>(worldStateRootHashBytes);
IValue state = _blockChain.GetWorldState(stateRootHash)
.GetAccount(accountAddress).GetState(address);
byte[] encoded = _codec.Encode(state ?? Null.Value);
return new UnaryResult<byte[]>(encoded);
}

public UnaryResult<byte[]> GetStateByAccountSrh(
byte[] addressBytes,
byte[] accountStateRootHashBytes)
{
var address = new Address(addressBytes);
var stateRootHash = new HashDigest<SHA256>(accountStateRootHashBytes);
IValue state = _blockChain.GetAccountState(stateRootHash).GetState(address);
byte[] encoded = _codec.Encode(state ?? Null.Value);
return new UnaryResult<byte[]>(encoded);
Expand All @@ -140,23 +158,27 @@ public UnaryResult<Dictionary<byte[], byte[]>> GetAvatarStates(
IEnumerable<byte[]> addressBytesList,
byte[] blockHashBytes)
{
var hash = new BlockHash(blockHashBytes);
var addresses = addressBytesList.Select(a => new Address(a)).ToList();
var worldState = _blockChain.GetWorldState(hash);
var avatarStates = addresses.Select(address => AvatarModule.GetAvatarState(worldState, address));
var result = addresses.Zip(avatarStates).ToDictionary(pair => pair.First.ToByteArray(), pair => pair.Second.ToRaw());
var blockHash = new BlockHash(blockHashBytes);
var worldState = _blockChain.GetWorldState(blockHash);
var avatarStates = addresses.Select(
address => AvatarModule.GetAvatarState(worldState, address));
var result = addresses.Zip(avatarStates).ToDictionary(
pair => pair.First.ToByteArray(), pair => pair.Second.ToRaw());
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 addresses = addressBytesList.Select(a => new Address(a)).ToList();
var stateRootHash = new HashDigest<SHA256>(worldStateRootHashBytes);
var worldState = _blockChain.GetWorldState(stateRootHash);
var avatarStates = addresses.Select(address => AvatarModule.GetAvatarState(worldState, address));
var result = addresses.Zip(avatarStates).ToDictionary(pair => pair.First.ToByteArray(), pair => pair.Second.ToRaw());
var avatarStates = addresses.Select(
address => AvatarModule.GetAvatarState(worldState, address));
var result = addresses.Zip(avatarStates).ToDictionary(
pair => pair.First.ToByteArray(), pair => pair.Second.ToRaw());
return new UnaryResult<Dictionary<byte[], byte[]>>(result);
}

Expand All @@ -165,11 +187,32 @@ public UnaryResult<Dictionary<byte[], byte[]>> GetStateBulk(
byte[] accountBytes,
byte[] blockHashBytes)
{
var hash = new BlockHash(blockHashBytes);
Address[] addresses = addressBytesList.Select(b => new Address(b)).ToArray();
var accountAddress = new Address(accountBytes);
var blockHash = new BlockHash(blockHashBytes);
var result = new Dictionary<byte[], byte[]>();
IReadOnlyList<IValue> values = _blockChain.GetWorldState(blockHash)
.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<Dictionary<byte[], byte[]>> GetStateBulkByWorldSrh(
IEnumerable<byte[]> addressBytesList,
byte[] accountBytes,
byte[] worldtStateRootHashBytes)
{
Address[] addresses = addressBytesList.Select(b => new Address(b)).ToArray();
IReadOnlyList<IValue> values = _blockChain.GetWorldState(hash).GetAccount(accountAddress).GetStates(addresses);
var accountAddress = new Address(accountBytes);
var stateRootHash = new HashDigest<SHA256>(worldtStateRootHashBytes);
var result = new Dictionary<byte[], byte[]>();
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));
Expand All @@ -178,11 +221,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[]>> GetStateBulkByAccountSrh(
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 +237,46 @@ 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<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 blockHash = new BlockHash(blockHashBytes);
FungibleAssetValue balance = _blockChain.GetWorldState(blockHash)
.GetAccount(accountAddress).GetBalance(address, currency);
byte[] encoded = _codec.Encode(
new Bencodex.Types.List(
new IValue[]
{
balance.Currency.Serialize(),
(Integer) balance.RawValue,
}
)
);
return new UnaryResult<byte[]>(encoded);
}

public UnaryResult<byte[]> GetBalanceByWorldSrh(
byte[] addressBytes,
byte[] currencyBytes,
byte[] accountBytes,
byte[] worldStateRootHashBytes)
{
var address = new Address(addressBytes);
var serializedCurrency = (Bencodex.Types.Dictionary)_codec.Decode(currencyBytes);
Currency currency = CurrencyExtensions.Deserialize(serializedCurrency);
var hash = new BlockHash(blockHashBytes);
FungibleAssetValue balance = LegacyModule.GetBalance(_blockChain.GetWorldState(hash), address, currency);
var accountAddress = new Address(accountBytes);
var stateRootHash = new HashDigest<SHA256>(worldStateRootHashBytes);
FungibleAssetValue balance = _blockChain.GetWorldState(stateRootHash)
.GetAccount(accountAddress).GetBalance(address, currency);
byte[] encoded = _codec.Encode(
new Bencodex.Types.List(
new IValue[]
Expand All @@ -213,12 +289,15 @@ 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[]> GetBalanceByAccountSrh(
byte[] addressBytes,
byte[] currencyBytes,
byte[] accountStateRootHashBytes)
{
var address = new Address(addressBytes);
var stateRootHash = new HashDigest<SHA256>(stateRootHashBytes);
var serializedCurrency = (Bencodex.Types.Dictionary)_codec.Decode(currencyBytes);
Currency currency = CurrencyExtensions.Deserialize(serializedCurrency);
var stateRootHash = new HashDigest<SHA256>(accountStateRootHashBytes);
FungibleAssetValue balance = _blockChain.GetAccountState(stateRootHash).GetBalance(address, currency);
byte[] encoded = _codec.Encode(
new Bencodex.Types.List(
Expand Down
2 changes: 1 addition & 1 deletion NineChronicles.RPC.Shared

0 comments on commit 86a7854

Please sign in to comment.