Commit 8036646f authored by Dan Gershony's avatar Dan Gershony

Light wallet sync manager

parent 8e6ab15d
...@@ -297,3 +297,4 @@ Thumbs.db ...@@ -297,3 +297,4 @@ Thumbs.db
project.lock.json project.lock.json
/Breeze/src/Breeze.Daemon/Wallets /Breeze/src/Breeze.Daemon/Wallets
/Breeze/src/Breeze.Daemon/Logs /Breeze/src/Breeze.Daemon/Logs
/Breeze.UI/.vscode/.BROWSE.VC.DB-wal
...@@ -27,7 +27,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stratis.Bitcoin", "..\..\St ...@@ -27,7 +27,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stratis.Bitcoin", "..\..\St
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stratis.Bitcoin.Common", "..\..\StratisBitcoinFullNode\Stratis.Bitcoin.Common\Stratis.Bitcoin.Common.csproj", "{4F9F7CF7-326C-4FC0-9EFB-209536A42030}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stratis.Bitcoin.Common", "..\..\StratisBitcoinFullNode\Stratis.Bitcoin.Common\Stratis.Bitcoin.Common.csproj", "{4F9F7CF7-326C-4FC0-9EFB-209536A42030}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Breeze.Daemon", "src\Breeze.Daemon\Breeze.Daemon.csproj", "{3F0937A2-3182-42D9-866F-3DEDEE28EC5A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Breeze.Daemon", "src\Breeze.Daemon\Breeze.Daemon.csproj", "{AAF6163B-1BE2-48CE-9F9F-577C6D7AAB8D}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
...@@ -63,10 +63,10 @@ Global ...@@ -63,10 +63,10 @@ Global
{4F9F7CF7-326C-4FC0-9EFB-209536A42030}.Debug|Any CPU.Build.0 = Debug|Any CPU {4F9F7CF7-326C-4FC0-9EFB-209536A42030}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F9F7CF7-326C-4FC0-9EFB-209536A42030}.Release|Any CPU.ActiveCfg = Release|Any CPU {4F9F7CF7-326C-4FC0-9EFB-209536A42030}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F9F7CF7-326C-4FC0-9EFB-209536A42030}.Release|Any CPU.Build.0 = Release|Any CPU {4F9F7CF7-326C-4FC0-9EFB-209536A42030}.Release|Any CPU.Build.0 = Release|Any CPU
{3F0937A2-3182-42D9-866F-3DEDEE28EC5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AAF6163B-1BE2-48CE-9F9F-577C6D7AAB8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F0937A2-3182-42D9-866F-3DEDEE28EC5A}.Debug|Any CPU.Build.0 = Debug|Any CPU {AAF6163B-1BE2-48CE-9F9F-577C6D7AAB8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F0937A2-3182-42D9-866F-3DEDEE28EC5A}.Release|Any CPU.ActiveCfg = Release|Any CPU {AAF6163B-1BE2-48CE-9F9F-577C6D7AAB8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F0937A2-3182-42D9-866F-3DEDEE28EC5A}.Release|Any CPU.Build.0 = Release|Any CPU {AAF6163B-1BE2-48CE-9F9F-577C6D7AAB8D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
...@@ -77,6 +77,5 @@ Global ...@@ -77,6 +77,5 @@ Global
{D16CD478-9D1E-4C69-91AD-43539E94A215} = {807563C4-7259-434D-B604-A14C3DCF8E30} {D16CD478-9D1E-4C69-91AD-43539E94A215} = {807563C4-7259-434D-B604-A14C3DCF8E30}
{2490DD1A-6C14-47F2-A9C6-56761A52E2D9} = {807563C4-7259-434D-B604-A14C3DCF8E30} {2490DD1A-6C14-47F2-A9C6-56761A52E2D9} = {807563C4-7259-434D-B604-A14C3DCF8E30}
{29E411B1-5687-43EE-A71B-6CCEC2289129} = {1B724678-2B73-483E-B981-3A6733C2194E} {29E411B1-5687-43EE-A71B-6CCEC2289129} = {1B724678-2B73-483E-B981-3A6733C2194E}
{3F0937A2-3182-42D9-866F-3DEDEE28EC5A} = {807563C4-7259-434D-B604-A14C3DCF8E30}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal
...@@ -33,7 +33,7 @@ namespace Breeze.Api ...@@ -33,7 +33,7 @@ namespace Breeze.Api
body = string.Join(Environment.NewLine, arguments.Values); body = string.Join(Environment.NewLine, arguments.Values);
} }
this.logger.LogInformation($"Received {request.Method} {request.GetDisplayUrl()}. Body: '{body}'"); this.logger.LogDebug($"Received {request.Method} {request.GetDisplayUrl()}. Body: '{body}'");
await next(); await next();
} }
} }
......
...@@ -9,8 +9,15 @@ using Stratis.Bitcoin.Builder; ...@@ -9,8 +9,15 @@ using Stratis.Bitcoin.Builder;
using Stratis.Bitcoin.Configuration; using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Logging; using Stratis.Bitcoin.Logging;
using Breeze.Wallet; using Breeze.Wallet;
using NBitcoin;
using NBitcoin.Protocol;
using Stratis.Bitcoin.BlockStore;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.MemoryPool;
using Stratis.Bitcoin.Miner;
using Stratis.Bitcoin.Notifications; using Stratis.Bitcoin.Notifications;
using Stratis.Bitcoin.Utilities; using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Wallet;
namespace Breeze.Daemon namespace Breeze.Daemon
{ {
...@@ -18,29 +25,125 @@ namespace Breeze.Daemon ...@@ -18,29 +25,125 @@ namespace Breeze.Daemon
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
// configure Full Node IFullNodeBuilder fullNodeBuilder = null;
Logs.Configure(Logs.GetLoggerFactory(args));
NodeSettings nodeSettings = NodeSettings.FromArguments(args); if (args.Contains("stratis"))
{
var fullNodeBuilder = new FullNodeBuilder() // configure Full Node
.UseNodeSettings(nodeSettings) Logs.Configure(Logs.GetLoggerFactory(args));
.UseLightWallet() if (NodeSettings.PrintHelp(args, Network.StratisMain))
.UseBlockNotification() return;
.UseTransactionNotification()
.UseApi(); var network = args.Contains("-testnet") ? InitStratisTest() : Network.StratisMain;
var nodeSettings = NodeSettings.FromArguments(args, "stratis", network, ProtocolVersion.ALT_PROTOCOL_VERSION);
// add the tumbler's settings
var tumblerAddress = args.SingleOrDefault(arg => arg.StartsWith("-tumbler-uri=")); if (args.Contains("light"))
if (!string.IsNullOrEmpty(tumblerAddress)) {
{ fullNodeBuilder = new FullNodeBuilder()
tumblerAddress = tumblerAddress.Replace("-tumbler-uri=", string.Empty); .UseNodeSettings(nodeSettings)
fullNodeBuilder.UseTumbleBit(new Uri(tumblerAddress)); .UseLightWallet()
} .UseBlockNotification()
.UseTransactionNotification()
var node = fullNodeBuilder.Build(); .UseApi();
}
// start Full Node - this will also start the API else
{
fullNodeBuilder = new FullNodeBuilder()
.UseNodeSettings(nodeSettings)
.UseStratisConsensus()
.UseBlockStore()
.UseMempool()
.UseWallet()
.AddPowPosMining()
.UseApi();
}
}
else
{
NodeSettings nodeSettings = NodeSettings.FromArguments(args);
if (args.Contains("light"))
{
fullNodeBuilder = new FullNodeBuilder()
.UseNodeSettings(nodeSettings)
.UseLightWallet()
.UseBlockNotification()
.UseTransactionNotification()
.UseApi();
}
else
{
fullNodeBuilder = new FullNodeBuilder()
.UseNodeSettings(nodeSettings)
.UseConsensus()
.UseBlockStore()
.UseMempool()
.UseWallet()
.UseApi();
}
}
// add the tumbler's settings
var tumblerAddress = args.SingleOrDefault(arg => arg.StartsWith("-tumbler-uri="));
if (!string.IsNullOrEmpty(tumblerAddress))
{
tumblerAddress = tumblerAddress.Replace("-tumbler-uri=", string.Empty);
fullNodeBuilder.UseTumbleBit(new Uri(tumblerAddress));
}
var node = fullNodeBuilder.Build();
//start Full Node - this will also start the API
node.Run(); node.Run();
} }
}
private static Network InitStratisTest()
{
Block.BlockSignature = true;
Transaction.TimeStamp = true;
var consensus = Network.StratisMain.Consensus.Clone();
consensus.PowLimit = new Target(uint256.Parse("0000ffff00000000000000000000000000000000000000000000000000000000"));
// The message start string is designed to be unlikely to occur in normal data.
// The characters are rarely used upper ASCII, not valid as UTF-8, and produce
// a large 4-byte int at any alignment.
var pchMessageStart = new byte[4];
pchMessageStart[0] = 0x71;
pchMessageStart[1] = 0x31;
pchMessageStart[2] = 0x21;
pchMessageStart[3] = 0x11;
var magic = BitConverter.ToUInt32(pchMessageStart, 0); //0x5223570;
var genesis = Network.StratisMain.GetGenesis().Clone();
genesis.Header.Time = 1493909211;
genesis.Header.Nonce = 2433759;
genesis.Header.Bits = consensus.PowLimit;
consensus.HashGenesisBlock = genesis.GetHash();
Guard.Assert(consensus.HashGenesisBlock == uint256.Parse("0x00000e246d7b73b88c9ab55f2e5e94d9e22d471def3df5ea448f5576b1d156b9"));
var builder = new NetworkBuilder()
.SetName("StratisTest")
.SetConsensus(consensus)
.SetMagic(magic)
.SetGenesis(genesis)
.SetPort(26178)
.SetRPCPort(26174)
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { (65) })
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { (196) })
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { (65 + 128) })
.SetBase58Bytes(Base58Type.ENCRYPTED_SECRET_KEY_NO_EC, new byte[] { 0x01, 0x42 })
.SetBase58Bytes(Base58Type.ENCRYPTED_SECRET_KEY_EC, new byte[] { 0x01, 0x43 })
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { (0x04), (0x88), (0xB2), (0x1E) })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { (0x04), (0x88), (0xAD), (0xE4) })
.AddDNSSeeds(new[]
{
new DNSSeedData("stratisplatform.com", "testnode1.stratisplatform.com"),
});
return builder.BuildAndRegister();
}
}
} }
...@@ -5,21 +5,22 @@ using Stratis.Bitcoin.Logging; ...@@ -5,21 +5,22 @@ using Stratis.Bitcoin.Logging;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Serilog; using Serilog;
using Stratis.Bitcoin.Wallet; using Stratis.Bitcoin.Wallet;
using Stratis.Bitcoin.Wallet.Controllers;
namespace Breeze.Wallet namespace Breeze.Wallet
{ {
public class LightWalletFeature : FullNodeFeature public class LightWalletFeature : FullNodeFeature
{ {
private readonly TrackNotifier trackNotifier; private readonly LightWalletSyncManager lightWalletSyncManager;
public LightWalletFeature(TrackNotifier trackNotifier) public LightWalletFeature(LightWalletSyncManager lightWalletSyncManager)
{ {
this.trackNotifier = trackNotifier; this.lightWalletSyncManager = lightWalletSyncManager;
} }
public override void Start() public override void Start()
{ {
this.trackNotifier.Initialize().GetAwaiter().GetResult(); this.lightWalletSyncManager.Initialize();
} }
public override void Stop() public override void Stop()
...@@ -28,21 +29,21 @@ namespace Breeze.Wallet ...@@ -28,21 +29,21 @@ namespace Breeze.Wallet
} }
} }
public static class WalletFeatureExtension public static class LightWalletFeatureExtension
{ {
public static IFullNodeBuilder UseLightWallet(this IFullNodeBuilder fullNodeBuilder) public static IFullNodeBuilder UseLightWallet(this IFullNodeBuilder fullNodeBuilder)
{ {
// use the wallet and on top of that start to notifier
fullNodeBuilder.UseWallet();
fullNodeBuilder.ConfigureFeature(features => fullNodeBuilder.ConfigureFeature(features =>
{ {
features features
.AddFeature<LightWalletFeature>() .AddFeature<LightWalletFeature>()
.FeatureServices(services => .FeatureServices(services =>
{ {
services.AddSingleton<TrackNotifier>(); services.AddSingleton<IWalletSyncManager, LightWalletSyncManager>();
}); services.AddSingleton<IWalletManager, WalletManager>();
services.AddSingleton<WalletController>();
});
}); });
return fullNodeBuilder; return fullNodeBuilder;
......
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
...@@ -12,11 +8,10 @@ using Stratis.Bitcoin; ...@@ -12,11 +8,10 @@ using Stratis.Bitcoin;
using Stratis.Bitcoin.Notifications; using Stratis.Bitcoin.Notifications;
using Stratis.Bitcoin.Utilities; using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Wallet; using Stratis.Bitcoin.Wallet;
using Stratis.Bitcoin.Wallet.Notifications;
namespace Breeze.Wallet namespace Breeze.Wallet
{ {
public class TrackNotifier public class LightWalletSyncManager : WalletSyncManager
{ {
private readonly WalletManager walletManager; private readonly WalletManager walletManager;
private readonly ConcurrentChain chain; private readonly ConcurrentChain chain;
...@@ -24,26 +19,21 @@ namespace Breeze.Wallet ...@@ -24,26 +19,21 @@ namespace Breeze.Wallet
private readonly CoinType coinType; private readonly CoinType coinType;
private readonly ILogger logger; private readonly ILogger logger;
public TrackNotifier(ILoggerFactory loggerFactory, IWalletManager walletManager, public LightWalletSyncManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network,
ConcurrentChain chain, BlockNotification blockNotification, Network network) BlockNotification blockNotification):base(loggerFactory, walletManager, chain, network)
{ {
this.walletManager = walletManager as WalletManager;
this.chain = chain;
this.blockNotification = blockNotification; this.blockNotification = blockNotification;
this.coinType = (CoinType)network.Consensus.CoinType;
this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
} }
/// <inheritdoc /> /// <inheritdoc />
public Task Initialize() public override void Initialize()
{ {
// get the chain headers. This needs to be up-to-date before we really do anything // get the chain headers. This needs to be up-to-date before we really do anything
//await this.WaitForChainDownloadAsync(); this.WaitForChainDownloadAsync().GetAwaiter().GetResult();
// start syncing blocks // start syncing blocks
var bestHeightForSyncing = this.FindBestHeightForSyncing(); var bestHeightForSyncing = this.FindBestHeightForSyncing();
this.SyncFrom(bestHeightForSyncing); this.SyncFrom(bestHeightForSyncing);
return Task.CompletedTask;
} }
private int FindBestHeightForSyncing() private int FindBestHeightForSyncing()
...@@ -85,7 +75,7 @@ namespace Breeze.Wallet ...@@ -85,7 +75,7 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public void SyncFrom(DateTime date) public override void SyncFrom(DateTime date)
{ {
int blockSyncStart = this.chain.GetHeightAtTime(date); int blockSyncStart = this.chain.GetHeightAtTime(date);
...@@ -94,7 +84,7 @@ namespace Breeze.Wallet ...@@ -94,7 +84,7 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public void SyncFrom(int height) public override void SyncFrom(int height)
{ {
this.blockNotification.SyncFrom(this.chain.GetBlock(height).HashBlock); this.blockNotification.SyncFrom(this.chain.GetBlock(height).HashBlock);
} }
......
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