Use AddOrUpdate instead of TryAdd, as Bitcoin Core is using that logic, too.
MODIFY
WalletWasabi.Tests/UnitTests/BitcoinCore/ConfigTests.cs
WalletWasabi.Tests/UnitTests/BitcoinCore/ConfigTests.cs
@@ -8,6 +8,24 @@ namespace WalletWasabi.Tests.UnitTests.BitcoinCore
{
public class ConfigTests
{
+ [Fact]
+ 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());
+ }
+
[Fact]
public void CanParse()
{
@@ -27,10 +45,10 @@ foo
bar
#qoo=boo";
var coreConfig = new CoreConfig();
- coreConfig.TryAdd(testConfig);
+ coreConfig.AddOrUpdate(testConfig);
var expectedConfig =
-@"foo = buz
+@"foo = bar
foo bar = buz quxx
@@ -50,7 +68,7 @@ bar
Assert.False(configDic.TryGetValue("bar", out _));
Assert.True(configDic.TryGetValue("foo bar", out string v3));
- Assert.Equal("buz", v1);
+ Assert.Equal("bar", v1);
Assert.Equal("1", v2);
Assert.Equal("buz quxx", v3);
@@ -73,11 +91,11 @@ bar
var add2 = "foo=bar";
var add3 = "too=0";
- coreConfig.TryAdd(add1);
+ coreConfig.AddOrUpdate(add1);
coreConfig2.AddOrUpdate(add1);
- coreConfig.TryAdd(add2);
+ coreConfig.AddOrUpdate(add2);
coreConfig2.AddOrUpdate(add2);
- coreConfig.TryAdd(add3);
+ coreConfig.AddOrUpdate(add3);
coreConfig2.AddOrUpdate(add3);
configDic = coreConfig.ToDictionary();
@@ -87,8 +105,8 @@ bar
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.Equal("bar", fooValue);
+ Assert.Equal("0", tooValue);
Assert.True(configDic2.TryGetValue("moo", out mooValue));
Assert.True(configDic2.TryGetValue("foo", out fooValue));
@@ -98,15 +116,15 @@ bar
Assert.Equal("0", tooValue);
expectedConfig =
-@"foo = buz
+@"foo = bar
foo bar = buz quxx
-too = 1
foo
bar
#qoo=boo
moo = 1
+too = 0
";
Assert.Equal(expectedConfig, coreConfig.ToString());
@@ -132,7 +150,7 @@ too = 0
@"foo=bar
buz=qux";
var coreConfig = new CoreConfig();
- coreConfig.TryAdd(testConfig);
+ coreConfig.AddOrUpdate(testConfig);
var expectedConfig =
@"foo = bar
@@ -165,7 +183,7 @@ bar=4
buz=1
test.buz=2";
var coreConfig = new CoreConfig();
- coreConfig.TryAdd(testConfig);
+ coreConfig.AddOrUpdate(testConfig);
var expectedConfig =
@"qux = 1

MODIFY
WalletWasabi/BitcoinCore/Configuration/CoreConfig.cs
WalletWasabi/BitcoinCore/Configuration/CoreConfig.cs
@@ -38,32 +38,6 @@ namespace WalletWasabi.BitcoinCore.Configuration
public override string ToString() => $"{Guard.Correct(string.Join(Environment.NewLine, Lines))}{Environment.NewLine}"; // Good practice to end with a newline.
- /// <summary>
- /// TryAdd and AddOrUpdate are sisters. TryAdd always considers the first occurrence of a key as valid, while AddOrUpdate considers the last occurrence of it.
- /// </summary>
- public void TryAdd(string configString)
- {
- foreach (var line in ParseConfigLines(configString))
- {
- if (line.HasKeyValuePair)
- {
- var foundLine = Lines.FirstOrDefault(x => x.Key == line.Key);
- if (foundLine is null)
- {
- Lines.Add(line);
- }
- }
- else
- {
- Lines.Add(line);
- }
- RemoveFirstEmptyDuplication();
- }
- }
-
- /// <summary>
- /// TryAdd and AddOrUpdate are sisters. TryAdd always considers the first occurrence of a key as valid, while AddOrUpdate considers the last occurrence of it.
- /// </summary>
public bool AddOrUpdate(string configString)
{
var ret = false;
@@ -89,9 +63,20 @@ namespace WalletWasabi.BitcoinCore.Configuration
Lines.Add(line);
ret = true;
}
- ret = ret || RemoveFirstEmptyDuplication();
}
+ ret = RemoveEmptyDuplications() || ret;
+
+ return ret;
+ }
+
+ private bool RemoveEmptyDuplications()
+ {
+ var ret = false;
+ while (RemoveFirstEmptyDuplication())
+ {
+ ret = true;
+ }
return ret;
}

MODIFY
WalletWasabi/BitcoinCore/CoreNode.cs
WalletWasabi/BitcoinCore/CoreNode.cs
@@ -57,7 +57,7 @@ namespace WalletWasabi.BitcoinCore
if (File.Exists(configPath))
{
var configString = await File.ReadAllTextAsync(configPath).ConfigureAwait(false);
- coreNode.Config.TryAdd(configString);
+ coreNode.Config.AddOrUpdate(configString); // Bitcoin Core considers the last entry to be valid.
}
cancel.ThrowIfCancellationRequested();

Use AddOrUpdate instead of TryAdd, as Bitcoin Core is using that logic, too.