Skip to content

Commit

Permalink
Accommodate API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube authored and riemannulus committed Sep 5, 2023
1 parent 05e3886 commit 28f36d2
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private class DummyAction : IAction
{
IValue IAction.PlainValue => Dictionary.Empty;

IAccountStateDelta IAction.Execute(IActionContext context)
IAccount IAction.Execute(IActionContext context)
{
return context.PreviousState;
}
Expand Down
1 change: 0 additions & 1 deletion Libplanet.Headless/ReducedStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public void PutTxExecution(TxSuccess txSuccess)
txSuccess.BlockHash,
txSuccess.TxId,
updatedStates: txSuccess.UpdatedStates.ToImmutableDictionary(pair => pair.Key, _ => (IValue)Null.Value),
fungibleAssetsDelta: txSuccess.FungibleAssetsDelta,
updatedFungibleAssets: txSuccess.UpdatedFungibleAssets
);
InternalStore.PutTxExecution(reducedTxSuccess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class ReplayCommand : CoconaLiteConsoleAppBase
/// Almost duplicate https://github.com/planetarium/libplanet/blob/main/Libplanet/State/AccountStateDelta.cs.
/// </summary>
[Pure]
private sealed class AccountStateDelta : IAccountStateDelta
private sealed class AccountStateDelta : IAccount
{
private readonly IAccountState _baseState;

Expand Down Expand Up @@ -97,7 +97,7 @@ private AccountStateDelta(IAccountState baseState, IAccountDelta delta)

/// <inheritdoc/>
[Pure]
public IAccountStateDelta SetState(Address address, IValue state) =>
public IAccount SetState(Address address, IValue state) =>
UpdateStates(Delta.States.SetItem(address, state));

/// <inheritdoc/>
Expand Down Expand Up @@ -132,7 +132,7 @@ public ValidatorSet GetValidatorSet() =>

/// <inheritdoc/>
[Pure]
public IAccountStateDelta MintAsset(
public IAccount MintAsset(
IActionContext context, Address recipient, FungibleAssetValue value)
{
if (value.Sign <= 0)
Expand Down Expand Up @@ -183,7 +183,7 @@ public IAccountStateDelta MintAsset(

/// <inheritdoc/>
[Pure]
public IAccountStateDelta TransferAsset(
public IAccount TransferAsset(
IActionContext context,
Address sender,
Address recipient,
Expand All @@ -194,7 +194,7 @@ public IAccountStateDelta TransferAsset(

/// <inheritdoc/>
[Pure]
public IAccountStateDelta BurnAsset(
public IAccount BurnAsset(
IActionContext context, Address owner, FungibleAssetValue value)
{
string msg;
Expand Down Expand Up @@ -245,7 +245,7 @@ public IAccountStateDelta BurnAsset(

/// <inheritdoc/>
[Pure]
public IAccountStateDelta SetValidator(Validator validator)
public IAccount SetValidator(Validator validator)
{
return UpdateValidatorSet(GetValidatorSet().Update(validator));
}
Expand All @@ -257,24 +257,24 @@ public IAccountStateDelta SetValidator(Validator validator)
/// a basis.</param>
/// <returns>A null state delta created from <paramref name="previousState"/>.
/// </returns>
internal static IAccountStateDelta Create(IAccountState previousState) =>
internal static IAccount Create(IAccountState previousState) =>
new AccountStateDelta(previousState);

/// <summary>
/// Creates a null state delta while inheriting <paramref name="stateDelta"/>s
/// total updated fungibles.
/// </summary>
/// <param name="stateDelta">The previous <see cref="IAccountStateDelta"/> to use.</param>
/// <param name="stateDelta">The previous <see cref="IAccount"/> to use.</param>
/// <returns>A null state delta that is of the same type as <paramref name="stateDelta"/>.
/// </returns>
/// <exception cref="ArgumentException">Thrown if given <paramref name="stateDelta"/>
/// is not <see cref="AccountStateDelta"/>.
/// </exception>
/// <remarks>
/// This inherits <paramref name="stateDelta"/>'s
/// <see cref="IAccountStateDelta.TotalUpdatedFungibleAssets"/>.
/// <see cref="IAccount.TotalUpdatedFungibleAssets"/>.
/// </remarks>
internal static IAccountStateDelta Flush(IAccountStateDelta stateDelta) =>
internal static IAccount Flush(IAccount stateDelta) =>
stateDelta is AccountStateDelta impl
? new AccountStateDelta(stateDelta)
{
Expand Down Expand Up @@ -348,7 +348,7 @@ private AccountStateDelta UpdateValidatorSet(
};

[Pure]
private IAccountStateDelta TransferAssetV0(
private IAccount TransferAssetV0(
Address sender,
Address recipient,
FungibleAssetValue value,
Expand Down Expand Up @@ -384,7 +384,7 @@ private IAccountStateDelta TransferAssetV0(
}

[Pure]
private IAccountStateDelta TransferAssetV1(
private IAccount TransferAssetV1(
Address sender,
Address recipient,
FungibleAssetValue value,
Expand Down Expand Up @@ -501,7 +501,7 @@ public ActionContext(
Address miner,
long blockIndex,
int blockProtocolVersion,
IAccountStateDelta previousState,
IAccount previousState,
int randomSeed,
bool rehearsal = false)
{
Expand All @@ -528,7 +528,7 @@ public ActionContext(

public bool Rehearsal { get; }

public IAccountStateDelta PreviousState { get; }
public IAccount PreviousState { get; }

public IRandom Random { get; }

Expand Down Expand Up @@ -746,15 +746,15 @@ private static IEnumerable<ActionEvaluation> EvaluateActions(
long blockIndex,
int blockProtocolVersion,
TxId? txid,
IAccountStateDelta previousStates,
IAccount previousStates,
Address miner,
Address signer,
byte[] signature,
IImmutableList<IAction> actions,
ILogger? logger = null)
{
ActionContext CreateActionContext(
IAccountStateDelta prevState,
IAccount prevState,
int randomSeed)
{
return new ActionContext(
Expand All @@ -776,11 +776,11 @@ ActionContext CreateActionContext(
byte[] preEvaluationHashBytes = preEvaluationHash.ToByteArray();
int seed = ActionEvaluator.GenerateRandomSeed(preEvaluationHashBytes, hashedSignature, signature, 0);

IAccountStateDelta states = previousStates;
IAccount states = previousStates;
foreach (IAction action in actions)
{
Exception? exc = null;
IAccountStateDelta nextStates = states;
IAccount nextStates = states;
ActionContext context = CreateActionContext(nextStates, seed);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public int Tx(

// Evaluate tx.
IAccountState previousBlockStates = blockChain.GetBlockState(previousBlock.Hash);
IAccountStateDelta previousStates = AccountStateDelta.Create(previousBlockStates);
IAccount previousStates = AccountStateDelta.Create(previousBlockStates);
var actions = tx.Actions.Select(a => ToAction(a));
var actionEvaluations = EvaluateActions(
preEvaluationHash: targetBlock.PreEvaluationHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ private static ImmutableDictionary<string, IValue> GetTotalDelta(
return ImmutableDictionary<string, IValue>.Empty;
}

IAccountStateDelta lastStates = actionEvaluations[actionEvaluations.Count - 1].OutputState;
IAccount lastStates = actionEvaluations[actionEvaluations.Count - 1].OutputState;

ImmutableDictionary<string, IValue> totalDelta =
stateUpdatedAddresses.ToImmutableDictionary(
Expand Down
6 changes: 3 additions & 3 deletions NineChronicles.Headless.Tests/Common/Actions/EmptyAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ public void LoadPlainValue(IValue plainValue)
{
}

public IAccountStateDelta Execute(IActionContext context)
public IAccount Execute(IActionContext context)
{
return context.PreviousState;
}

public void Render(IActionContext context, IAccountStateDelta nextStates)
public void Render(IActionContext context, IAccount nextStates)
{
}

public void RenderError(IActionContext context, Exception exception)
{
}

public void Unrender(IActionContext context, IAccountStateDelta nextStates)
public void Unrender(IActionContext context, IAccount nextStates)
{
}

Expand Down
49 changes: 21 additions & 28 deletions NineChronicles.Headless/GraphTypes/StandaloneQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,39 +116,32 @@ public StandaloneQuery(StandaloneContext standaloneContext, IConfiguration confi
var recipient = context.GetArgument<Address?>("recipient");

IEnumerable<Transaction> txs = digest.TxIds
.Select(b => new TxId(b.ToBuilder().ToArray()))
.Select(bytes => new TxId(bytes))
.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)

var pairs = txs
.Where(tx =>
tx.Actions!.Count == 1 &&
store.GetTxExecution(blockHash, tx.Id) is TxSuccess)
.Select(tx => (tx.Id, ToAction(tx.Actions.First())))
.Where(pair =>
pair.Item2 is ITransferAsset transferAssset &&
transferAssset.Amount.Currency.Ticker == "NCG")
.Select(pair => (pair.Item1, (ITransferAsset)pair.Item2))
.Where(pair => (!(recipient is { } r) || pair.Item2.Recipient == r));

TransferNCGHistory ToTransferNCGHistory((TxId TxId, ITransferAsset Transfer) pair)
{
var rawTransferNcgHistories = txSuccess.FungibleAssetsDelta
.Where(pair => pair.Value.Values.Any(fav => fav.Currency.Ticker == "NCG"))
.Select(pair =>
(pair.Key, pair.Value.Values.First(fav => fav.Currency.Ticker == "NCG")))
.ToArray();
var ((senderAddress, _), (recipientAddress, amount)) =
rawTransferNcgHistories[0].Item2.RawValue > rawTransferNcgHistories[1].Item2.RawValue
? (rawTransferNcgHistories[1], rawTransferNcgHistories[0])
: (rawTransferNcgHistories[0], rawTransferNcgHistories[1]);
return new TransferNCGHistory(
txSuccess.BlockHash,
txSuccess.TxId,
senderAddress,
recipientAddress,
amount,
memo);
blockHash,
pair.TxId,
pair.Transfer.Sender,
pair.Transfer.Recipient,
pair.Transfer.Amount,
pair.Transfer.Memo);
}

var histories = filteredTransactions.Select(tx =>
ToTransferNCGHistory((TxSuccess)store.GetTxExecution(blockHash, tx.Id),
((ITransferAsset)ToAction(tx.Actions!.Single())).Memo));

var histories = pairs.Select(ToTransferNCGHistory);
return histories;
});

Expand Down
4 changes: 0 additions & 4 deletions NineChronicles.Headless/GraphTypes/StandaloneSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,21 +329,17 @@ private IObservable<Tx> SubscribeTx(IResolveFieldContext context)
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
null,
null,
success.UpdatedStates
.Select(kv => new KeyValuePair<Address, IValue>(
kv.Key,
kv.Value))
.ToImmutableDictionary(),
success.FungibleAssetsDelta,
success.UpdatedFungibleAssets),
TxFailure failure => new TxResult(
TxStatus.FAILURE,
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
failure.ExceptionName,
failure.ExceptionMetadata,
null,
null,
null),
_ => null
Expand Down
10 changes: 3 additions & 7 deletions NineChronicles.Headless/GraphTypes/TransactionHeadlessQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext)
if (!(store.GetFirstTxIdBlockHashIndex(txId) is { } txExecutedBlockHash))
{
return blockChain.GetStagedTransactionIds().Contains(txId)
? new TxResult(TxStatus.STAGING, null, null, null, null, null, null, null)
: new TxResult(TxStatus.INVALID, null, null, null, null, null, null, null);
? new TxResult(TxStatus.STAGING, null, null, null, null, null)
: new TxResult(TxStatus.INVALID, null, null, null, null, null);
}

try
Expand All @@ -222,21 +222,17 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext)
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
null,
null,
txSuccess.UpdatedStates
.Select(kv => new KeyValuePair<Address, IValue>(
kv.Key,
kv.Value))
.ToImmutableDictionary(),
txSuccess.FungibleAssetsDelta,
txSuccess.UpdatedFungibleAssets),
TxFailure txFailure => new TxResult(
TxStatus.FAILURE,
txExecutedBlock.Index,
txExecutedBlock.Hash.ToString(),
txFailure.ExceptionName,
txFailure.ExceptionMetadata,
null,
null,
null),
_ => throw new NotImplementedException(
Expand All @@ -245,7 +241,7 @@ public TransactionHeadlessQuery(StandaloneContext standaloneContext)
}
catch (Exception)
{
return new TxResult(TxStatus.INVALID, null, null, null, null, null, null, null);
return new TxResult(TxStatus.INVALID, null, null, null, null, null);
}
}
);
Expand Down

0 comments on commit 28f36d2

Please sign in to comment.