Commit e752cba2 authored by Jeremy Bokobza's avatar Jeremy Bokobza

Update the height of last block that was synced without waiting to receive a new block

parent c0f1bac6
......@@ -130,7 +130,7 @@ namespace Breeze.Wallet.Controllers
{
// get the wallet folder
DirectoryInfo walletFolder = GetWalletFolder(request.FolderPath);
Wallet wallet = this.walletManager.RecoverWallet(request.Password, walletFolder.FullName, request.Name, request.Network, request.Mnemonic, null, request.CreationDate);
Wallet wallet = this.walletManager.RecoverWallet(request.Password, walletFolder.FullName, request.Name, request.Network, request.Mnemonic, request.CreationDate, null);
// start syncing the wallet from the creation date
this.tracker.SyncFrom(request.CreationDate);
......
......@@ -22,7 +22,14 @@ namespace Breeze.Wallet
/// Synchronize the wallet starting from the date passed as a parameter.
/// </summary>
/// <param name="date">The date from which to start the sync process.</param>
/// <returns></returns>
/// <returns>The height of the block sync will start from</returns>
void SyncFrom(DateTime date);
/// <summary>
/// Synchronize the wallet starting from the height passed as a parameter.
/// </summary>
/// <param name="height">The height from which to start the sync process.</param>
/// <returns>The height of the block sync will start from</returns>
void SyncFrom(int height);
}
}
......@@ -41,7 +41,7 @@ namespace Breeze.Wallet
/// <param name="passphrase">The passphrase used in the seed.</param>
/// <param name="creationTime">The date and time this wallet was created.</param>
/// <returns>The recovered wallet.</returns>
Wallet RecoverWallet(string password, string folderPath, string name, string network, string mnemonic, string passphrase = null, DateTime? creationTime = null);
Wallet RecoverWallet(string password, string folderPath, string name, string network, string mnemonic, DateTime creationTime, string passphrase = null);
/// <summary>
/// Deletes a wallet.
......@@ -163,5 +163,29 @@ namespace Breeze.Wallet
/// <param name="blockHeight">The height of the block this transaction came from. Null if it was not a transaction included in a block.</param>
/// <param name="blockTime">The block time.</param>
void ProcessTransaction(Transaction transaction, int? blockHeight = null, uint? blockTime = null);
/// <summary>
/// Saves the wallet into the file system.
/// </summary>
/// <param name="wallet">The wallet to save.</param>
void SaveToFile(Wallet wallet);
/// <summary>
/// Saves all the loaded wallets into the file system.
/// </summary>
void SaveToFile();
/// <summary>
/// Updates the wallet with the height of the last block synced.
/// </summary>
/// <param name="wallet">The wallet to update.</param>
/// <param name="height">The height of the last block synced.</param>
void UpdateLastBlockSyncedHeight(Wallet wallet, int height);
/// <summary>
/// Updates all the loaded wallets with the height of the last block synced.
/// </summary>
/// <param name="height">The height of the last block synced.</param>
void UpdateLastBlockSyncedHeight(int height);
}
}
......@@ -90,7 +90,13 @@ namespace Breeze.Wallet
int blockSyncStart = this.chain.GetHeightAtTime(date);
// start syncing blocks
this.blockNotification.SyncFrom(this.chain.GetBlock(blockSyncStart).HashBlock);
this.SyncFrom(blockSyncStart);
}
/// <inheritdoc />
public void SyncFrom(int height)
{
this.blockNotification.SyncFrom(this.chain.GetBlock(height).HashBlock);
}
private bool BlocksSynced()
......
......@@ -33,6 +33,8 @@ namespace Breeze.Wallet
private readonly ConnectionManager connectionManager;
private readonly ConcurrentChain chain;
private Dictionary<Script, HdAddress> keysLookup;
/// <summary>
......@@ -40,7 +42,7 @@ namespace Breeze.Wallet
/// </summary>
public event EventHandler<TransactionFoundEventArgs> TransactionFound;
public WalletManager(ConnectionManager connectionManager, Network network)
public WalletManager(ConnectionManager connectionManager, Network network, ConcurrentChain chain)
{
this.Wallets = new List<Wallet>();
......@@ -53,6 +55,7 @@ namespace Breeze.Wallet
this.connectionManager = connectionManager;
this.network = network;
this.coinType = (CoinType)network.Consensus.CoinType;
this.chain = chain;
// load data in memory for faster lookups
this.LoadKeysLookup();
......@@ -88,11 +91,14 @@ namespace Breeze.Wallet
this.CreateAddressesInAccount(account, coinNetwork, UnusedAddressesBuffer, true);
}
// update the height of the we start syncing from
this.UpdateLastBlockSyncedHeight(wallet, this.chain.Tip.Height);
// save the changes to the file and add addresses to be tracked
this.SaveToFile(wallet);
this.Load(wallet);
this.LoadKeysLookup();
return mnemonic;
}
......@@ -109,7 +115,7 @@ namespace Breeze.Wallet
}
/// <inheritdoc />
public Wallet RecoverWallet(string password, string folderPath, string name, string network, string mnemonic, string passphrase = null, DateTime? creationTime = null)
public Wallet RecoverWallet(string password, string folderPath, string name, string network, string mnemonic, DateTime creationTime, string passphrase = null)
{
// for now the passphrase is set to be the password by default.
if (passphrase == null)
......@@ -133,6 +139,9 @@ namespace Breeze.Wallet
this.CreateAddressesInAccount(account, coinNetwork, UnusedAddressesBuffer, true);
}
int blockSyncStart = this.chain.GetHeightAtTime(creationTime);
this.UpdateLastBlockSyncedHeight(wallet, blockSyncStart);
// save the changes to the file and add addresses to be tracked
this.SaveToFile(wallet);
this.Load(wallet);
......@@ -320,7 +329,7 @@ namespace Breeze.Wallet
Wallet wallet = this.GetWalletByName(walletName);
return wallet;
}
/// <inheritdoc />
public IEnumerable<HdAccount> GetAccountsByCoinType(string walletName, CoinType coinType)
{
......@@ -446,13 +455,7 @@ namespace Breeze.Wallet
}
// update the wallets with the last processed block height
foreach (var wallet in this.Wallets)
{
foreach (var accountRoot in wallet.AccountsRoot.Where(a => a.CoinType == this.coinType))
{
accountRoot.LastBlockSyncedHeight = height;
}
}
this.UpdateLastBlockSyncedHeight(height);
}
/// <inheritdoc />
......@@ -483,7 +486,7 @@ namespace Breeze.Wallet
// We first include the keys we don't hold and then we include the keys we do hold but that are for receiving addresses (which would mean the user paid itself).
IEnumerable<TxOut> paidoutto = transaction.Outputs.Where(o => !this.keysLookup.Keys.Contains(o.ScriptPubKey) || (this.keysLookup.ContainsKey(o.ScriptPubKey) && !this.keysLookup[o.ScriptPubKey].IsChangeAddress()));
AddTransactionToWallet(transaction.GetHash(), transaction.Time, null, -tTx.Amount, keyToSpend, blockHeight, blockTime, tTx.Id, tTx.Index, paidoutto);
AddTransactionToWallet(transaction.GetHash(), transaction.Time, null, -tTx.Amount, keyToSpend, blockHeight, blockTime, tTx.Id, tTx.Index, paidoutto);
}
}
......@@ -560,7 +563,7 @@ namespace Breeze.Wallet
if (blockTime != null)
{
foundTransaction.CreationTime = DateTimeOffset.FromUnixTimeSeconds(blockTime.Value);
}
}
}
// notify a transaction has been found
......@@ -608,6 +611,41 @@ namespace Breeze.Wallet
File.Delete(walletFilePath);
}
/// <inheritdoc />
public void SaveToFile(Wallet wallet)
{
File.WriteAllText(wallet.WalletFilePath, JsonConvert.SerializeObject(wallet, Formatting.Indented));
}
/// <inheritdoc />
public void UpdateLastBlockSyncedHeight(int height)
{
// update the wallets with the last processed block height
foreach (var wallet in this.Wallets)
{
this.UpdateLastBlockSyncedHeight(wallet, height);
}
}
/// <inheritdoc />
public void UpdateLastBlockSyncedHeight(Wallet wallet, int height)
{
// update the wallets with the last processed block height
foreach (var accountRoot in wallet.AccountsRoot.Where(a => a.CoinType == this.coinType))
{
accountRoot.LastBlockSyncedHeight = height;
}
}
/// <inheritdoc />
public void SaveToFile()
{
foreach (var wallet in this.Wallets)
{
this.SaveToFile(wallet);
}
}
/// <inheritdoc />
public void Dispose()
{
......@@ -658,15 +696,6 @@ namespace Breeze.Wallet
return walletFile;
}
/// <summary>
/// Saves the wallet into the file system.
/// </summary>
/// <param name="wallet">The wallet to save.</param>
private void SaveToFile(Wallet wallet)
{
File.WriteAllText(wallet.WalletFilePath, JsonConvert.SerializeObject(wallet, Formatting.Indented));
}
/// <summary>
/// Gets the wallet located at the specified path.
/// </summary>
......
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