Skip to content

Commit

Permalink
Accommodate libplanet API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Sep 25, 2023
1 parent 5041bd7 commit cd70f9f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Lib9c
16 changes: 2 additions & 14 deletions Libplanet.Headless/ReducedStore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Bencodex.Types;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Types.Blocks;
Expand Down Expand Up @@ -106,21 +104,11 @@ public void PutBlock(Block block) =>
public void PutTransaction(Transaction tx) =>
InternalStore.PutTransaction(tx);

public void PutTxExecution(TxSuccess txSuccess)
public void PutTxExecution(TxExecution txExecution)
{
// Omit TxSuccess.UpdatedStates as it is unused by Nine Chronicles and too big.
TxSuccess reducedTxSuccess = new TxSuccess(
txSuccess.BlockHash,
txSuccess.TxId,
updatedStates: txSuccess.UpdatedStates.ToImmutableDictionary(pair => pair.Key, _ => (IValue)Null.Value),
updatedFungibleAssets: txSuccess.UpdatedFungibleAssets
);
InternalStore.PutTxExecution(reducedTxSuccess);
InternalStore.PutTxExecution(txExecution);
}

public void PutTxExecution(TxFailure txFailure) =>
InternalStore.PutTxExecution(txFailure);

public void SetCanonicalChainId(Guid chainId) =>
InternalStore.SetCanonicalChainId(chainId);

Expand Down
4 changes: 2 additions & 2 deletions NineChronicles.Headless.Executable/Commands/MarketCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void Query(
IEnumerable<(Transaction, ActionBase)> actions = block.Transactions
.Reverse()
.Where(tx => includeFails ||
!(chain.GetTxExecution(block.Hash, tx.Id) is { } e) ||
e is TxSuccess)
!(chain.GetTxExecution(block.Hash, tx.Id) is { } e) ||
!e.Fail)
.SelectMany(tx => tx.Actions is { } ca
? ca.Reverse().Select(a => (tx, ToAction(a)))
: Enumerable.Empty<(Transaction, ActionBase)>());
Expand Down
3 changes: 0 additions & 3 deletions NineChronicles.Headless/GraphQLServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Reflection;
using GraphQL.Types;
using Libplanet.Action;
using Libplanet.Explorer.GraphTypes;
using Libplanet.Explorer.Interfaces;
using Libplanet.Explorer.Queries;
Expand Down Expand Up @@ -36,8 +35,6 @@ public static IServiceCollection AddLibplanetScalarTypes(this IServiceCollection
services.TryAddSingleton<ByteStringType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.PublicKeyType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.TxResultType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.TxResultType.FungibleAssetBalancesType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.TxResultType.UpdatedStateType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.TxStatusType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.BencodexValueType>();
services.TryAddSingleton<Libplanet.Explorer.GraphTypes.FungibleAssetValueType>();
Expand Down
39 changes: 18 additions & 21 deletions NineChronicles.Headless/GraphTypes/StandaloneQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,27 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi

var recipient = context.GetArgument<Address?>("recipient");

IEnumerable<Transaction> txs = digest.TxIds
IEnumerable<Transaction> blockTxs = digest.TxIds
.Select(b => new TxId(b.ToBuilder().ToArray()))
.Select(store.GetTransaction);
var filteredTransactions = txs.Where(tx =>
tx.Actions!.Count == 1 &&
ToAction(tx.Actions.First()) is ITransferAsset transferAsset &&
(!recipient.HasValue || transferAsset.Recipient == recipient) &&
transferAsset.Amount.Currency.Ticker == "NCG" &&
store.GetTxExecution(blockHash, tx.Id) is TxSuccess);

TransferNCGHistory ToTransferNCGHistory(TxSuccess txSuccess, string? memo)
{
return new TransferNCGHistory(
txSuccess.BlockHash,
txSuccess.TxId,
default,
default,
new FungibleAssetValue(),
memo);
}

var histories = filteredTransactions.Select(tx =>
ToTransferNCGHistory((TxSuccess)store.GetTxExecution(blockHash, tx.Id),
((ITransferAsset)ToAction(tx.Actions!.Single())).Memo));
var filtered = blockTxs
.Where(tx => tx.Actions.Count == 1)
.Select(tx => (store.GetTxExecution(blockHash, tx.Id), ToAction(tx.Actions[0])))
.Where(pair => pair.Item1 is { } && pair.Item2 is ITransferAsset)
.Select(pair => (pair.Item1, (ITransferAsset)pair.Item2))
.Where(pair => !pair.Item1.Fail &&
(!recipient.HasValue || pair.Item2.Recipient == recipient) &&
pair.Item2.Amount.Currency.Ticker == "NCG");

var histories = filtered.Select(pair =>
new TransferNCGHistory(
pair.Item1.BlockHash,
pair.Item1.TxId,
pair.Item2.Sender,
pair.Item2.Recipient,
pair.Item2.Amount,
pair.Item2.Memo));

return histories;
});
Expand Down
26 changes: 7 additions & 19 deletions NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,29 +321,17 @@ private IObservable<Tx> SubscribeTx(IResolveFieldContext context)
}
var txExecution = store.GetTxExecution(blockHash, transaction.Id);
var txExecutedBlock = chain[blockHash];

return txExecution switch
{
TxSuccess success => new TxResult(
TxStatus.SUCCESS,
return txExecution.Fail
? new TxResult(
TxStatus.FAILURE,
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
null,
success.UpdatedStates
.Select(kv => new KeyValuePair<Address, IValue>(
kv.Key,
kv.Value))
.ToImmutableDictionary(),
success.UpdatedFungibleAssets),
TxFailure failure => new TxResult(
TxStatus.FAILURE,
txExecution.ExceptionNames)
: new TxResult(
TxStatus.SUCCESS,
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
failure.ExceptionName,
null,
null),
_ => null
};
txExecution.ExceptionNames);
}

private void RenderBlock((Block OldTip, Block NewTip) pair)
Expand Down
32 changes: 10 additions & 22 deletions NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,41 +207,29 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext)
if (!(store.GetFirstTxIdBlockHashIndex(txId) is { } txExecutedBlockHash))
{
return blockChain.GetStagedTransactionIds().Contains(txId)
? new TxResult(TxStatus.STAGING, null, null, null, null, null)
: new TxResult(TxStatus.INVALID, null, null, null, null, null);
? new TxResult(TxStatus.STAGING, null, null, null)
: new TxResult(TxStatus.INVALID, null, null, null);
}

try
{
TxExecution execution = blockChain.GetTxExecution(txExecutedBlockHash, txId);
Block txExecutedBlock = blockChain[txExecutedBlockHash];
return execution switch
{
TxSuccess txSuccess => new TxResult(
TxStatus.SUCCESS,
return execution.Fail
? new TxResult(
TxStatus.FAILURE,
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
null,
txSuccess.UpdatedStates
.Select(kv => new KeyValuePair<Address, IValue>(
kv.Key,
kv.Value))
.ToImmutableDictionary(),
txSuccess.UpdatedFungibleAssets),
TxFailure txFailure => new TxResult(
TxStatus.FAILURE,
execution.ExceptionNames)
: new TxResult(
TxStatus.SUCCESS,
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
txFailure.ExceptionName,
null,
null),
_ => throw new NotImplementedException(
$"{nameof(execution)} is not expected concrete class.")
};
execution.ExceptionNames);
}
catch (Exception)
{
return new TxResult(TxStatus.INVALID, null, null, null, null, null);
return new TxResult(TxStatus.INVALID, null, null, null);
}
}
);
Expand Down

0 comments on commit cd70f9f

Please sign in to comment.