public void RemovesEmptyDuplications() { var configStringBuilder = new StringBuilder("foo=bar"); configStringBuilder.Append(Environment.NewLine); configStringBuilder.Append(Environment.NewLine); configStringBuilder.Append(Environment.NewLine); configStringBuilder.Append("bar=bar"); var config = new CoreConfig(); config.AddOrUpdate(configStringBuilder.ToString()); var expectedConfig = @"foo = bar bar = bar "; Assert.Equal(expectedConfig, config.ToString()); }
public void KeepsOrder() { var testConfig = @"foo=bar buz=qux"; var coreConfig = new CoreConfig(); coreConfig.TryAdd(testConfig); var expectedConfig = @"foo = bar buz = qux "; Assert.Equal(expectedConfig, coreConfig.ToString()); var add1 = "foo=bar"; coreConfig.AddOrUpdate(add1); Assert.Equal(expectedConfig, coreConfig.ToString()); }
public void TryGetRpcUserTests() { var config = new CoreConfig(); var translatorMain = new CoreConfigTranslator(config, Network.Main); var translatorTest = new CoreConfigTranslator(config, Network.TestNet); var translatorReg = new CoreConfigTranslator(config, Network.RegTest); Assert.Null(translatorMain.TryGetRpcUser()); Assert.Null(translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("rpcuser"); Assert.Null(translatorMain.TryGetRpcUser()); Assert.Null(translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("rpcuser=foo"); Assert.Equal("foo", translatorMain.TryGetRpcUser()); Assert.Null(translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("rpcuser=boo"); Assert.Equal("boo", translatorMain.TryGetRpcUser()); Assert.Null(translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("main.rpcuser=ooh"); Assert.Equal("ooh", translatorMain.TryGetRpcUser()); Assert.Null(translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("rpcuser=boo"); Assert.Equal("boo", translatorMain.TryGetRpcUser()); Assert.Null(translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("test.rpcuser=boo"); Assert.Equal("boo", translatorMain.TryGetRpcUser()); Assert.Equal("boo", translatorTest.TryGetRpcUser()); Assert.Null(translatorReg.TryGetRpcUser()); config.AddOrUpdate("regtest.rpcuser=boo"); Assert.Equal("boo", translatorMain.TryGetRpcUser()); Assert.Equal("boo", translatorTest.TryGetRpcUser()); Assert.Equal("boo", translatorReg.TryGetRpcUser()); }
public void TryGetWhiteBindTests() { var config = new CoreConfig(); var translator = new CoreConfigTranslator(config, Network.Main); Assert.Null(translator.TryGetWhiteBind()); config.AddOrUpdate("whitebind"); Assert.Null(translator.TryGetWhiteBind()); config.AddOrUpdate("main.whitebind=127.0.0.1:18444"); WhiteBind?whiteBind = translator.TryGetWhiteBind(); Assert.NotNull(whiteBind); var ipEndPoint = whiteBind !.EndPoint as IPEndPoint; Assert.NotNull(ipEndPoint); Assert.Equal(IPAddress.Loopback, ipEndPoint !.Address); Assert.Equal(18444, ipEndPoint.Port); Assert.Equal("", whiteBind.Permissions); config.AddOrUpdate("whitebind=127.0.0.1:0"); whiteBind = translator.TryGetWhiteBind(); Assert.NotNull(whiteBind); ipEndPoint = whiteBind !.EndPoint as IPEndPoint; Assert.NotNull(ipEndPoint); Assert.Equal(IPAddress.Loopback, ipEndPoint !.Address); Assert.Equal(0, ipEndPoint.Port); Assert.Equal("", whiteBind.Permissions); config.AddOrUpdate("whitebind=127.0.0.1"); whiteBind = translator.TryGetWhiteBind(); Assert.NotNull(whiteBind); ipEndPoint = whiteBind !.EndPoint as IPEndPoint; Assert.NotNull(ipEndPoint); Assert.Equal(IPAddress.Loopback, ipEndPoint !.Address); // Default port. Assert.Equal(8333, ipEndPoint.Port); Assert.Equal("", whiteBind.Permissions); config.AddOrUpdate("[email protected]"); whiteBind = translator.TryGetWhiteBind(); Assert.NotNull(whiteBind); ipEndPoint = whiteBind !.EndPoint as IPEndPoint; Assert.NotNull(ipEndPoint); Assert.Equal(IPAddress.Loopback, ipEndPoint !.Address); Assert.Equal(8333, ipEndPoint.Port); Assert.Equal("foo", whiteBind.Permissions); config.AddOrUpdate("whitebind=foo,[email protected]"); whiteBind = translator.TryGetWhiteBind(); Assert.NotNull(whiteBind); ipEndPoint = whiteBind !.EndPoint as IPEndPoint; Assert.NotNull(ipEndPoint); Assert.Equal(IPAddress.Loopback, ipEndPoint !.Address); Assert.Equal(8333, ipEndPoint.Port); Assert.Equal("foo,boo", whiteBind.Permissions); config.AddOrUpdate("main.whitebind=@@@"); Assert.Null(translator.TryGetWhiteBind()); }
public static async Task <CoreNode> CreateAsync(CoreNodeParams coreNodeParams, CancellationToken cancel) { Guard.NotNull(nameof(coreNodeParams), coreNodeParams); using (BenchmarkLogger.Measure()) { var coreNode = new CoreNode(); coreNode.DataDir = coreNodeParams.DataDir; coreNode.Network = coreNodeParams.Network; coreNode.MempoolService = coreNodeParams.MempoolService; var configPath = Path.Combine(coreNode.DataDir, "bitcoin.conf"); coreNode.Config = new CoreConfig(); if (File.Exists(configPath)) { var configString = await File.ReadAllTextAsync(configPath).ConfigureAwait(false); coreNode.Config.TryAdd(configString); } var configDic = coreNode.Config.ToDictionary(); string rpcUser = null; string rpcPassword = null; EndPoint whitebind = null; string rpcHost = null; int? rpcPort = null; string rpcCookieFilePath = null; foreach (var networkPrefixWithDot in NetworkTranslator.GetConfigPrefixesWithDots(coreNode.Network)) { var rpcc = configDic.TryGet($"{networkPrefixWithDot}rpccookiefile"); var ru = configDic.TryGet($"{networkPrefixWithDot}rpcuser"); var rp = configDic.TryGet($"{networkPrefixWithDot}rpcpassword"); var wbs = configDic.TryGet($"{networkPrefixWithDot}whitebind"); var rpst = configDic.TryGet($"{networkPrefixWithDot}rpchost"); var rpts = configDic.TryGet($"{networkPrefixWithDot}rpcport"); if (rpcc != null) { rpcCookieFilePath = rpcc; } if (ru != null) { rpcUser = ru; } if (rp != null) { rpcPassword = rp; } if (wbs != null && EndPointParser.TryParse(wbs, coreNode.Network.DefaultPort, out EndPoint wb)) { whitebind = wb; } if (rpst != null) { rpcHost = rpst; } if (rpts != null && int.TryParse(rpts, out int rpt)) { rpcPort = rpt; } } string authString; bool cookieAuth = rpcCookieFilePath != null; if (cookieAuth) { authString = $"cookiefile={rpcCookieFilePath}"; } else { rpcUser ??= Encoders.Hex.EncodeData(RandomUtils.GetBytes(21)); rpcPassword ??= Encoders.Hex.EncodeData(RandomUtils.GetBytes(21)); authString = $"{rpcUser}:{rpcPassword}"; } coreNode.P2pEndPoint = whitebind ?? coreNodeParams.P2pEndPointStrategy.EndPoint; rpcHost ??= coreNodeParams.RpcEndPointStrategy.EndPoint.GetHostOrDefault(); rpcPort ??= coreNodeParams.RpcEndPointStrategy.EndPoint.GetPortOrDefault(); EndPointParser.TryParse($"{rpcHost}:{rpcPort}", coreNode.Network.RPCPort, out EndPoint rpce); coreNode.RpcEndPoint = rpce; coreNode.RpcClient = new RPCClient($"{authString}", coreNode.RpcEndPoint.ToString(coreNode.Network.DefaultPort), coreNode.Network); if (coreNodeParams.TryRestart) { await coreNode.TryStopAsync(false).ConfigureAwait(false); } if (coreNodeParams.TryDeleteDataDir) { await IoHelpers.DeleteRecursivelyWithMagicDustAsync(coreNode.DataDir).ConfigureAwait(false); } IoHelpers.EnsureDirectoryExists(coreNode.DataDir); var configPrefix = NetworkTranslator.GetConfigPrefix(coreNode.Network); var desiredConfigLines = new List <string>() { $"{configPrefix}.server = 1", $"{configPrefix}.listen = 1", $"{configPrefix}.whitebind = {coreNode.P2pEndPoint.ToString(coreNode.Network.DefaultPort)}", $"{configPrefix}.rpchost = {coreNode.RpcEndPoint.GetHostOrDefault()}", $"{configPrefix}.rpcport = {coreNode.RpcEndPoint.GetPortOrDefault()}" }; if (!cookieAuth) { desiredConfigLines.Add($"{configPrefix}.rpcuser = {coreNode.RpcClient.CredentialString.UserPassword.UserName}"); desiredConfigLines.Add($"{configPrefix}.rpcpassword = {coreNode.RpcClient.CredentialString.UserPassword.Password}"); } if (coreNodeParams.TxIndex != null) { desiredConfigLines.Add($"{configPrefix}.txindex = {coreNodeParams.TxIndex}"); } if (coreNodeParams.Prune != null) { desiredConfigLines.Add($"{configPrefix}.prune = {coreNodeParams.Prune}"); } var sectionComment = $"# The following configuration options were added or modified by Wasabi Wallet."; // If the comment is not already present. // And there would be new config entries added. var throwAwayConfig = new CoreConfig(coreNode.Config); throwAwayConfig.AddOrUpdate(string.Join(Environment.NewLine, desiredConfigLines)); if (!coreNode.Config.ToString().Contains(sectionComment, StringComparison.Ordinal) && throwAwayConfig.Count != coreNode.Config.Count) { desiredConfigLines.Insert(0, sectionComment); } if (coreNode.Config.AddOrUpdate(string.Join(Environment.NewLine, desiredConfigLines)) || !File.Exists(configPath)) { IoHelpers.EnsureContainingDirectoryExists(configPath); await File.WriteAllTextAsync(configPath, coreNode.Config.ToString()); } // If it isn't already running, then we run it. if (await coreNode.RpcClient.TestAsync().ConfigureAwait(false) is null) { Logger.LogInfo("Bitcoin Core is already running."); }
public void CanParse() { var testConfig = @"foo=buz foo = bar"; testConfig += Environment.NewLine; testConfig += Environment.NewLine; testConfig += Environment.NewLine; testConfig += @" foo = bar foo bar = buz quxx too =1 foo bar #qoo=boo"; var coreConfig = new CoreConfig(); coreConfig.TryAdd(testConfig); var expectedConfig = @"foo = buz foo bar = buz quxx too = 1 foo bar #qoo=boo "; Assert.Equal(expectedConfig, coreConfig.ToString()); var configDic = coreConfig.ToDictionary(); Assert.True(configDic.TryGetValue("foo", out string v1)); Assert.True(configDic.TryGetValue("too", out string v2)); Assert.False(configDic.TryGetValue("qoo", out _)); Assert.False(configDic.TryGetValue("bar", out _)); Assert.True(configDic.TryGetValue("foo bar", out string v3)); Assert.Equal("buz", v1); Assert.Equal("1", v2); Assert.Equal("buz quxx", v3); var coreConfig2 = new CoreConfig(); coreConfig2.AddOrUpdate(testConfig); var configDic2 = coreConfig2.ToDictionary(); Assert.True(configDic2.TryGetValue("foo", out string v1_2)); Assert.True(configDic2.TryGetValue("too", out string v2_2)); Assert.False(configDic2.TryGetValue("qoo", out _)); Assert.False(configDic2.TryGetValue("bar", out _)); Assert.True(configDic2.TryGetValue("foo bar", out string v3_3)); Assert.Equal("bar", v1_2); Assert.Equal("1", v2_2); Assert.Equal("buz quxx", v3_3); var add1 = "moo=1"; var add2 = "foo=bar"; var add3 = "too=0"; coreConfig.TryAdd(add1); coreConfig2.AddOrUpdate(add1); coreConfig.TryAdd(add2); coreConfig2.AddOrUpdate(add2); coreConfig.TryAdd(add3); coreConfig2.AddOrUpdate(add3); configDic = coreConfig.ToDictionary(); configDic2 = coreConfig2.ToDictionary(); Assert.True(configDic.TryGetValue("moo", out string mooValue)); Assert.True(configDic.TryGetValue("foo", out string fooValue)); Assert.True(configDic.TryGetValue("too", out string tooValue)); Assert.Equal("1", mooValue); Assert.Equal("buz", fooValue); Assert.Equal("1", tooValue); Assert.True(configDic2.TryGetValue("moo", out mooValue)); Assert.True(configDic2.TryGetValue("foo", out fooValue)); Assert.True(configDic2.TryGetValue("too", out tooValue)); Assert.Equal("1", mooValue); Assert.Equal("bar", fooValue); Assert.Equal("0", tooValue); expectedConfig = @"foo = buz foo bar = buz quxx too = 1 foo bar #qoo=boo moo = 1 "; Assert.Equal(expectedConfig, coreConfig.ToString()); var expectedConfig2 = @"foo = bar foo bar = buz quxx foo bar #qoo=boo moo = 1 too = 0 "; Assert.Equal(expectedConfig2, coreConfig2.ToString()); }