Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #164 from dangershony/wallet-broadcast-mempool
Browse files Browse the repository at this point in the history
Send a broadcast trx to local mempool
  • Loading branch information
bokobza authored Jun 16, 2017
2 parents a898a2c + ce571b8 commit 73ca05a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
44 changes: 38 additions & 6 deletions Stratis.Bitcoin/Wallet/WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Newtonsoft.Json;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.MemoryPool;
using Transaction = NBitcoin.Transaction;

namespace Stratis.Bitcoin.Wallet
Expand All @@ -29,6 +30,7 @@ public class WalletManager : IWalletManager
private readonly ConcurrentChain chain;
private readonly NodeSettings settings;
private readonly DataFolder dataFolder;
private readonly MempoolValidator mempoolValidator;

public uint256 WalletTipHash { get; set; }

Expand All @@ -46,7 +48,8 @@ public class WalletManager : IWalletManager
public event EventHandler<TransactionFoundEventArgs> TransactionFound;

public WalletManager(ILoggerFactory loggerFactory, ConnectionManager connectionManager, Network network, ConcurrentChain chain,
NodeSettings settings, DataFolder dataFolder)
NodeSettings settings, DataFolder dataFolder,
MempoolValidator mempoolValidator = null) // mempool does not exist in a light wallet
{
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
this.Wallets = new List<Wallet>();
Expand All @@ -57,6 +60,7 @@ public WalletManager(ILoggerFactory loggerFactory, ConnectionManager connectionM
this.chain = chain;
this.settings = settings;
this.dataFolder = dataFolder;
this.mempoolValidator = mempoolValidator;

// register events
this.TransactionFound += this.OnTransactionFound;
Expand Down Expand Up @@ -445,6 +449,17 @@ public ISecret GetKeyForAddress(string password, HdAddress address)
Wallet wallet = this.GetWalletByName(walletName);
HdAccount account = wallet.AccountsRoot.Single(a => a.CoinType == this.coinType).GetAccountByName(accountName);

// get script destination address
Script destinationScript = null;
try
{
destinationScript = PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey(new BitcoinPubKeyAddress(destinationAddress, wallet.Network));
}
catch
{
throw new WalletException("Invalid address.");
}

// get a list of transactions outputs that have not been spent
var spendableTransactions = account.GetSpendableTransactions().ToList();

Expand Down Expand Up @@ -483,9 +498,6 @@ public ISecret GetKeyForAddress(string password, HdAddress address)
// get address to send the change to
var changeAddress = account.GetFirstUnusedChangeAddress();

// get script destination address
Script destinationScript = PayToPubkeyHashTemplate.Instance.GenerateScriptPubKey(new BitcoinPubKeyAddress(destinationAddress, wallet.Network));

// build transaction
var builder = new TransactionBuilder();
Transaction tx = builder
Expand Down Expand Up @@ -530,16 +542,36 @@ public ISecret GetKeyForAddress(string password, HdAddress address)
/// <inheritdoc />
public bool SendTransaction(string transactionHex)
{
// TODO move this to a behavior on the full node
// TODO move this to a behavior to a dedicated interface
// parse transaction
Transaction transaction = Transaction.Parse(transactionHex);
TxPayload payload = new TxPayload(transaction);

// replace this we a dedicated WalletBroadcast interface
// in a fullnode implementation this will validate with the
// mempool and broadcast, in a lightnode this will push to
// the wallet and then broadcast (we might add some basic validation
if (this.mempoolValidator == null)
{
this.ProcessTransaction(transaction);
}
else
{
var state = new MempoolValidationState(false);
if (!this.mempoolValidator.AcceptToMemoryPool(state, transaction).GetAwaiter().GetResult())
{
return false;
}
}

// broadcast to peers
TxPayload payload = new TxPayload(transaction);
foreach (var node in this.connectionManager.ConnectedNodes)
{
node.SendMessage(payload);
}

// we might want to create a behaviour that tracks how many times
// the broadcast trasnactions was sent back to us by other peers
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion Stratis.Bitcoin/Wallet/WalletSyncManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public virtual void ProcessBlock(Block block)

next = this.chain.GetBlock(next.Height +1);
var nextblock = this.blockStoreCache.GetBlockAsync(next.HashBlock).GetAwaiter().GetResult();
Guard.Assert(nextblock != null);
if(nextblock == null)
return; // temporary to allow wallet ot recover when store is behind
this.walletManager.ProcessBlock(nextblock, next);
}
}
Expand Down

0 comments on commit 73ca05a

Please sign in to comment.