Commit 6f126a35 authored by Sergei Zubov's avatar Sergei Zubov

Modify wallet resync

Ensure that correct data about spending transaction is present in JSON
parent 9c1440c9
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Configuration;
......@@ -41,5 +43,79 @@ namespace Stratis.Bitcoin.Features.Wallet
this.network.GetGenesis());
}
}
protected override void AddSpendingTransactionToWallet(Transaction transaction,
IEnumerable<TxOut> paidToOutputs, uint256 spendingTransactionId,
int? spendingTransactionIndex, int? blockHeight = null, Block block = null)
{
Guard.NotNull(transaction, nameof(transaction));
Guard.NotNull(paidToOutputs, nameof(paidToOutputs));
this.logger.LogTrace("({0}:'{1}',{2}:'{3}',{4}:{5},{6}:'{7}')", nameof(transaction), transaction.GetHash(),
nameof(spendingTransactionId), spendingTransactionId, nameof(spendingTransactionIndex),
spendingTransactionIndex, nameof(blockHeight), blockHeight);
// Get the transaction being spent.
TransactionData spentTransaction = this.keysLookup.Values.Distinct().SelectMany(v => v.Transactions)
.SingleOrDefault(t => t.Id == spendingTransactionId && t.Index == spendingTransactionIndex);
if (spentTransaction == null)
{
// Strange, why would it be null?
this.logger.LogTrace("(-)[TX_NULL]");
return;
}
this.logger.LogTrace(spentTransaction.SpendingDetails == null
? $"Spending UTXO '{spendingTransactionId}-{spendingTransactionIndex}' is new."
: $"Spending transaction ID '{spendingTransactionId}' is being confirmed, updating.");
var payments = new List<PaymentDetails>();
foreach (TxOut paidToOutput in paidToOutputs)
{
// Figure out how to retrieve the destination address.
string destinationAddress = string.Empty;
ScriptTemplate scriptTemplate = paidToOutput.ScriptPubKey.FindTemplate(this.network);
switch (scriptTemplate.Type)
{
// Pay to PubKey can be found in outputs of staking transactions.
case TxOutType.TX_PUBKEY:
PubKey pubKey =
PayToPubkeyTemplate.Instance.ExtractScriptPubKeyParameters(paidToOutput.ScriptPubKey);
destinationAddress = pubKey.GetAddress(this.network).ToString();
break;
// Pay to PubKey hash is the regular, most common type of output.
case TxOutType.TX_PUBKEYHASH:
destinationAddress = paidToOutput.ScriptPubKey.GetDestinationAddress(this.network).ToString();
break;
case TxOutType.TX_NONSTANDARD:
case TxOutType.TX_SCRIPTHASH:
case TxOutType.TX_MULTISIG:
case TxOutType.TX_NULL_DATA:
case TxOutType.TX_SEGWIT:
break;
}
payments.Add(new PaymentDetails
{
DestinationScriptPubKey = paidToOutput.ScriptPubKey,
DestinationAddress = destinationAddress,
Amount = paidToOutput.Value
});
}
var spendingDetails = new SpendingDetails
{
TransactionId = transaction.GetHash(),
Payments = payments,
CreationTime = DateTimeOffset.FromUnixTimeSeconds(block?.Header.Time ?? transaction.Time),
BlockHeight = blockHeight,
Hex = this.walletSettings.SaveTransactionHex ? transaction.ToHex() : null,
IsCoinStake = transaction.IsCoinStake == false ? (bool?) null : true
};
spentTransaction.SpendingDetails = spendingDetails;
spentTransaction.MerkleProof = null;
this.logger.LogTrace("(-)");
}
}
}
\ No newline at end of file
......@@ -61,7 +61,7 @@ namespace Stratis.Bitcoin.Features.Wallet
private readonly INodeLifetime nodeLifetime;
/// <summary>Instance logger.</summary>
private readonly ILogger logger;
protected readonly ILogger logger;
/// <summary>An object capable of storing <see cref="Wallet"/>s to the file system.</summary>
private readonly FileStorage<Wallet> fileStorage;
......@@ -73,7 +73,7 @@ namespace Stratis.Bitcoin.Features.Wallet
private readonly IDateTimeProvider dateTimeProvider;
/// <summary>The settings for the wallet feature.</summary>
private readonly WalletSettings walletSettings;
protected readonly WalletSettings walletSettings;
public uint256 WalletTipHash { get; set; }
......@@ -987,7 +987,7 @@ namespace Stratis.Bitcoin.Features.Wallet
/// <param name="spendingTransactionIndex">The index of the output in the transaction being referenced, if this is a spending transaction.</param>
/// <param name="blockHeight">Height of the block.</param>
/// <param name="block">The block containing the transaction to add.</param>
private void AddSpendingTransactionToWallet(Transaction transaction, IEnumerable<TxOut> paidToOutputs,
protected virtual void AddSpendingTransactionToWallet(Transaction transaction, IEnumerable<TxOut> paidToOutputs,
uint256 spendingTransactionId, int? spendingTransactionIndex, int? blockHeight = null, Block block = null)
{
Guard.NotNull(transaction, nameof(transaction));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment