/// <inheritdoc /> public Task TumbleAsync(string originWalletName, string destinationWalletName) { // make sure the tumbler service is initialized if (this.TumblerParameters == null || this.runtime == null) { throw new Exception("Please connect to the tumbler first."); } // TODO: Remove wallet logic while getting tumbler interaction to work /* * // make sure that the user is not trying to resume the process with a different wallet * if (!string.IsNullOrEmpty(this.tumblingState.DestinationWalletName) && this.tumblingState.DestinationWalletName != destinationWalletName) * { * throw new Exception("Please use the same destination wallet until the end of this tumbling session."); * } * * Wallet destinationWallet = this.walletManager.GetWallet(destinationWalletName); * if (destinationWallet == null) * { * throw new Exception($"Destination wallet not found. Have you created a wallet with name {destinationWalletName}?"); * } * * Wallet originWallet = this.walletManager.GetWallet(originWalletName); * if (originWallet == null) * { * throw new Exception($"Origin wallet not found. Have you created a wallet with name {originWalletName}?"); * } * * // update the state and save * this.tumblingState.DestinationWallet = destinationWallet; * this.tumblingState.DestinationWalletName = destinationWalletName; * this.tumblingState.OriginWallet = originWallet; * this.tumblingState.OriginWalletName = originWalletName; */ this.tumblingState.Save(); // Subscribe to receive new block notifications // TODO: Is this the right BlockObserver or should the one used by the Wallet feature be used? this.blockReceiver = this.signals.SubscribeForBlocks(new BlockObserver(this.chain, this)); this.stateMachine = new StateMachinesExecutor(this.runtime); this.stateMachine.Start(); return(Task.CompletedTask); }
/// <inheritdoc /> public async Task TumbleAsync(string originWalletName, string destinationWalletName, string originWalletPassword) { // Make sure it won't start new tumbling round if already started if (this.State == TumbleState.Tumbling) { this.logger.LogDebug("Tumbler is already running"); throw new Exception("Tumbling is already running"); } this.tumblingState.TumblerUri = new Uri(this.TumblerAddress); // Check if in initial block download if (!this.chain.IsDownloaded()) { this.logger.LogDebug("Chain is still being downloaded: " + this.chain.Tip); throw new Exception("Chain is still being downloaded"); } Wallet destinationWallet = this.walletManager.GetWallet(destinationWalletName); Wallet originWallet = this.walletManager.GetWallet(originWalletName); // Check if origin wallet has a sufficient balance to begin tumbling at least 1 cycle Money originBalance = this.walletManager.GetSpendableTransactionsInWallet(this.tumblingState.OriginWalletName) .Sum(s => s.Transaction.Amount); // Should ideally take network's transaction fee into account too, but that is dynamic if (originBalance <= (this.TumblerParameters.Denomination + this.TumblerParameters.Fee)) { this.logger.LogDebug("Insufficient funds in origin wallet"); throw new Exception("Insufficient funds in origin wallet"); } // Check if password is valid before starting any cycles try { HdAddress tempAddress = originWallet.GetAccountsByCoinType(this.tumblingState.CoinType).First() .GetFirstUnusedReceivingAddress(); originWallet.GetExtendedPrivateKeyForAddress(originWalletPassword, tempAddress); } catch (Exception) { this.logger.LogDebug("Origin wallet password appears to be invalid"); throw new Exception("Origin wallet password appears to be invalid"); } // Update the state and save this.tumblingState.DestinationWallet = destinationWallet ?? throw new Exception($"Destination wallet not found. Have you created a wallet with name {destinationWalletName}?"); this.tumblingState.DestinationWalletName = destinationWalletName; this.tumblingState.OriginWallet = originWallet ?? throw new Exception($"Origin wallet not found. Have you created a wallet with name {originWalletName}?"); this.tumblingState.OriginWalletName = originWalletName; this.tumblingState.OriginWalletPassword = originWalletPassword; var accounts = this.tumblingState.DestinationWallet.GetAccountsByCoinType(this.tumblingState.CoinType); // TODO: Possibly need to preserve destination account name in tumbling state. Default to first account for now string accountName = accounts.First().Name; HdAccount destAccount = this.tumblingState.DestinationWallet.GetAccountByCoinType(accountName, this.tumblingState.CoinType); string key = destAccount.ExtendedPubKey; KeyPath keyPath = new KeyPath("0"); // Stop and dispose onlymonitor if (this.broadcasterJob != null && this.broadcasterJob.Started) { await this.broadcasterJob.Stop().ConfigureAwait(false); } this.runtime?.Dispose(); // Bypass Tor for integration tests FullNodeTumblerClientConfiguration config; if (this.TumblerAddress.Contains("127.0.0.1")) { config = new FullNodeTumblerClientConfiguration(this.tumblingState, onlyMonitor: false, connectionTest: false, useProxy: false); } else { config = new FullNodeTumblerClientConfiguration(this.tumblingState, onlyMonitor: false, connectionTest: false, useProxy: true); } this.runtime = await TumblerClientRuntime.FromConfigurationAsync(config).ConfigureAwait(false); BitcoinExtPubKey extPubKey = new BitcoinExtPubKey(key, this.runtime.Network); if (key != null) { this.runtime.DestinationWallet = new ClientDestinationWallet(extPubKey, keyPath, this.runtime.Repository, this.runtime.Network); } this.TumblerParameters = this.runtime.TumblerParameters; // Run onlymonitor mode this.broadcasterJob = this.runtime.CreateBroadcasterJob(); this.broadcasterJob.Start(); // Run tumbling mode this.stateMachine = new StateMachinesExecutor(this.runtime); this.stateMachine.Start(); this.State = TumbleState.Tumbling; }
/// <inheritdoc /> public async Task TumbleAsync(string originWalletName, string destinationWalletName, string originWalletPassword) { // Make sure it won't start new tumbling round if already started if (this.State == TumbleState.Tumbling) { this.logger.LogDebug("Tumbler is already running"); throw new Exception("Tumbling is already running"); } this.tumblingState.TumblerUri = new Uri(this.TumblerAddress); // Check if in initial block download if (!this.chain.IsDownloaded()) { this.logger.LogDebug("Chain is still being downloaded: " + this.chain.Tip); throw new Exception("Chain is still being downloaded"); } Wallet destinationWallet = this.walletManager.GetWallet(destinationWalletName); Wallet originWallet = this.walletManager.GetWallet(originWalletName); // Check if origin wallet has a balance WalletBalanceModel model = new WalletBalanceModel(); var originWalletAccounts = this.walletManager.GetAccounts(originWallet.Name).ToList(); var originConfirmed = new Money(0); var originUnconfirmed = new Money(0); foreach (var originAccount in originWallet.GetAccountsByCoinType(this.tumblingState.CoinType)) { var result = originAccount.GetSpendableAmount(); originConfirmed += result.ConfirmedAmount; originUnconfirmed += result.UnConfirmedAmount; } // Should ideally take network transaction fee into account too, but that is dynamic if ((originConfirmed + originUnconfirmed) <= (this.TumblerParameters.Denomination + this.TumblerParameters.Fee)) { this.logger.LogDebug("Insufficient funds in origin wallet"); throw new Exception("Insufficient funds in origin wallet"); } // TODO: Check if password is valid before starting any cycles // Update the state and save this.tumblingState.DestinationWallet = destinationWallet ?? throw new Exception($"Destination wallet not found. Have you created a wallet with name {destinationWalletName}?"); this.tumblingState.DestinationWalletName = destinationWalletName; this.tumblingState.OriginWallet = originWallet ?? throw new Exception($"Origin wallet not found. Have you created a wallet with name {originWalletName}?"); this.tumblingState.OriginWalletName = originWalletName; this.tumblingState.OriginWalletPassword = originWalletPassword; var accounts = this.tumblingState.DestinationWallet.GetAccountsByCoinType(this.tumblingState.CoinType); // TODO: Possibly need to preserve destination account name in tumbling state. Default to first account for now string accountName = null; foreach (var account in accounts) { if (account.Index == 0) { accountName = account.Name; } } var destAccount = this.tumblingState.DestinationWallet.GetAccountByCoinType(accountName, this.tumblingState.CoinType); var key = destAccount.ExtendedPubKey; var keyPath = new KeyPath("0"); // Stop and dispose onlymonitor if (this.broadcasterJob != null && this.broadcasterJob.Started) { await this.broadcasterJob.Stop().ConfigureAwait(false); } this.runtime?.Dispose(); // Bypass Tor for integration tests FullNodeTumblerClientConfiguration config; if (this.TumblerAddress.Contains("127.0.0.1")) { config = new FullNodeTumblerClientConfiguration(this.tumblingState, onlyMonitor: false, connectionTest: false, useProxy: false); } else { config = new FullNodeTumblerClientConfiguration(this.tumblingState, onlyMonitor: false, connectionTest: false, useProxy: true); } this.runtime = await TumblerClientRuntime.FromConfigurationAsync(config).ConfigureAwait(false); var extPubKey = new BitcoinExtPubKey(key, this.runtime.Network); if (key != null) { this.runtime.DestinationWallet = new ClientDestinationWallet(extPubKey, keyPath, this.runtime.Repository, this.runtime.Network); } this.TumblerParameters = this.runtime.TumblerParameters; // Run onlymonitor mode this.broadcasterJob = this.runtime.CreateBroadcasterJob(); this.broadcasterJob.Start(); // Run tumbling mode this.stateMachine = new StateMachinesExecutor(this.runtime); this.stateMachine.Start(); this.State = TumbleState.Tumbling; return; }
private static void StartTumbler(string[] args) { Logs.Configure(new FuncLoggerFactory(i => new ConsoleLogger(i, (a, b) => true, false))); CancellationTokenSource broadcasterCancel = new CancellationTokenSource(); DBreezeRepository dbreeze = null; try { var network = args.Contains("-testnet", StringComparer.OrdinalIgnoreCase) ? Network.TestNet : args.Contains("-regtest", StringComparer.OrdinalIgnoreCase) ? Network.RegTest : Network.Main; Logs.Configuration.LogInformation("Network: " + network); var dataDir = DefaultDataDirectory.GetDefaultDirectory("NTumbleBit", network); var consoleArgs = new TextFileConfiguration(args); var configFile = GetDefaultConfigurationFile(dataDir, network); var config = TextFileConfiguration.Parse(File.ReadAllText(configFile)); consoleArgs.MergeInto(config, true); config.AddAlias("server", "tumbler.server"); var onlymonitor = config.GetOrDefault <bool>("onlymonitor", false); RPCClient rpc = null; try { rpc = RPCArgs.ConfigureRPCClient(config, network); } catch { throw new ConfigException("Please, fix rpc settings in " + configFile); } dbreeze = new DBreezeRepository(Path.Combine(dataDir, "db")); var services = ExternalServices.CreateFromRPCClient(rpc, dbreeze); var broadcaster = new BroadcasterJob(services, Logs.Main); broadcaster.Start(broadcasterCancel.Token); Logs.Main.LogInformation("BroadcasterJob started"); if (!onlymonitor) { var server = config.GetOrDefault("tumbler.server", null as Uri); if (server == null) { Logs.Main.LogError("tumbler.server not configured"); throw new ConfigException(); } var client = new TumblerClient(network, server); Logs.Configuration.LogInformation("Downloading tumbler information of " + server.AbsoluteUri); var parameters = Retry(3, () => client.GetTumblerParameters()); Logs.Configuration.LogInformation("Tumbler Server Connection successfull"); var existingConfig = dbreeze.Get <ClassicTumbler.ClassicTumblerParameters>("Configuration", client.Address.AbsoluteUri); if (existingConfig != null) { if (Serializer.ToString(existingConfig) != Serializer.ToString(parameters)) { Logs.Configuration.LogError("The configuration file of the tumbler changed since last connection, it should never happen"); throw new ConfigException(); } } else { dbreeze.UpdateOrInsert("Configuration", client.Address.AbsoluteUri, parameters, (o, n) => n); } if (parameters.Network != rpc.Network) { throw new ConfigException("The tumbler server run on a different network than the local rpc server"); } IDestinationWallet destinationWallet = null; try { destinationWallet = GetDestinationWallet(config, rpc.Network, dbreeze); } catch (Exception ex) { Logs.Main.LogInformation("outputwallet.extpubkey is not configured, trying to use outputwallet.rpc settings."); try { destinationWallet = GetRPCDestinationWallet(config, rpc.Network); } catch { throw ex; } //Not a bug, want to throw the other exception } var stateMachine = new StateMachinesExecutor(parameters, client, destinationWallet, services, dbreeze, Logs.Main); stateMachine.Start(broadcasterCancel.Token); Logs.Main.LogInformation("State machines started"); } Logs.Main.LogInformation("Press enter to stop"); Console.ReadLine(); broadcasterCancel.Cancel(); } catch (ConfigException ex) { if (!string.IsNullOrEmpty(ex.Message)) { Logs.Configuration.LogError(ex.Message); } } catch (Exception ex) { Logs.Configuration.LogError(ex.Message); Logs.Configuration.LogDebug(ex.StackTrace); } finally { if (!broadcasterCancel.IsCancellationRequested) { broadcasterCancel.Cancel(); } dbreeze?.Dispose(); } }