diff --git a/NineChronicles.Headless/BlockChainService.cs b/NineChronicles.Headless/BlockChainService.cs index ec1619c52..d672bedc6 100644 --- a/NineChronicles.Headless/BlockChainService.cs +++ b/NineChronicles.Headless/BlockChainService.cs @@ -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; @@ -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; @@ -116,21 +114,41 @@ public UnaryResult PutTransaction(byte[] txBytes) } } - public UnaryResult GetState(byte[] addressBytes, byte[] accountBytes, byte[] blockHashBytes) + public UnaryResult 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(encoded); } - public UnaryResult GetStateBySrh(byte[] addressBytes, byte[] stateRootHashBytes) + public UnaryResult GetStateByWorldSrh( + byte[] addressBytes, + byte[] accountBytes, + byte[] worldStateRootHashBytes) { - var stateRootHash = new HashDigest(stateRootHashBytes); var address = new Address(addressBytes); + var accountAddress = new Address(accountBytes); + var stateRootHash = new HashDigest(worldStateRootHashBytes); + IValue state = _blockChain.GetWorldState(stateRootHash) + .GetAccount(accountAddress).GetState(address); + byte[] encoded = _codec.Encode(state ?? Null.Value); + return new UnaryResult(encoded); + } + + public UnaryResult GetStateByAccountSrh( + byte[] addressBytes, + byte[] accountStateRootHashBytes) + { + var address = new Address(addressBytes); + var stateRootHash = new HashDigest(accountStateRootHashBytes); IValue state = _blockChain.GetAccountState(stateRootHash).GetState(address); byte[] encoded = _codec.Encode(state ?? Null.Value); return new UnaryResult(encoded); @@ -140,23 +158,27 @@ public UnaryResult> GetAvatarStates( IEnumerable 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>(result); } - public UnaryResult> GetAvatarStatesByOffsetSrh( + public UnaryResult> GetAvatarStatesByWorldSrh( IEnumerable addressBytesList, - byte[] stateRootHashBytes) + byte[] worldStateRootHashBytes) { - var stateRootHash = new HashDigest(stateRootHashBytes); var addresses = addressBytesList.Select(a => new Address(a)).ToList(); + var stateRootHash = new HashDigest(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>(result); } @@ -165,11 +187,32 @@ public UnaryResult> 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(); + IReadOnlyList 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>(result); + } + + public UnaryResult> GetStateBulkByWorldSrh( + IEnumerable addressBytesList, + byte[] accountBytes, + byte[] worldtStateRootHashBytes) + { Address[] addresses = addressBytesList.Select(b => new Address(b)).ToArray(); - IReadOnlyList values = _blockChain.GetWorldState(hash).GetAccount(accountAddress).GetStates(addresses); + var accountAddress = new Address(accountBytes); + var stateRootHash = new HashDigest(worldtStateRootHashBytes); + var result = new Dictionary(); + IReadOnlyList 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)); @@ -178,11 +221,11 @@ public UnaryResult> GetStateBulk( return new UnaryResult>(result); } - public UnaryResult> GetStateBulkBySrh( + public UnaryResult> GetStateBulkByAccountSrh( IEnumerable addressBytesList, - byte[] stateRootHashBytes) + byte[] accountStateRootHashBytes) { - var stateRootHash = new HashDigest(stateRootHashBytes); + var stateRootHash = new HashDigest(accountStateRootHashBytes); var result = new Dictionary(); Address[] addresses = addressBytesList.Select(b => new Address(b)).ToArray(); IReadOnlyList values = _blockChain.GetAccountState(stateRootHash).GetStates(addresses); @@ -194,13 +237,46 @@ public UnaryResult> GetStateBulkBySrh( return new UnaryResult>(result); } - public UnaryResult GetBalance(byte[] addressBytes, byte[] currencyBytes, byte[] blockHashBytes) + + + public UnaryResult 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(encoded); + } + + public UnaryResult 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(worldStateRootHashBytes); + FungibleAssetValue balance = _blockChain.GetWorldState(stateRootHash) + .GetAccount(accountAddress).GetBalance(address, currency); byte[] encoded = _codec.Encode( new Bencodex.Types.List( new IValue[] @@ -213,12 +289,15 @@ public UnaryResult GetBalance(byte[] addressBytes, byte[] currencyBytes, return new UnaryResult(encoded); } - public UnaryResult GetBalanceBySrh(byte[] addressBytes, byte[] currencyBytes, byte[] stateRootHashBytes) + public UnaryResult GetBalanceByAccountSrh( + byte[] addressBytes, + byte[] currencyBytes, + byte[] accountStateRootHashBytes) { var address = new Address(addressBytes); - var stateRootHash = new HashDigest(stateRootHashBytes); var serializedCurrency = (Bencodex.Types.Dictionary)_codec.Decode(currencyBytes); Currency currency = CurrencyExtensions.Deserialize(serializedCurrency); + var stateRootHash = new HashDigest(accountStateRootHashBytes); FungibleAssetValue balance = _blockChain.GetAccountState(stateRootHash).GetBalance(address, currency); byte[] encoded = _codec.Encode( new Bencodex.Types.List( diff --git a/NineChronicles.RPC.Shared b/NineChronicles.RPC.Shared index 067cdadd9..084d5ef04 160000 --- a/NineChronicles.RPC.Shared +++ b/NineChronicles.RPC.Shared @@ -1 +1 @@ -Subproject commit 067cdadd97124c726823281e6b12c976d4c9eebe +Subproject commit 084d5ef041a55aa683d44bddf6d8193c59e33e69