Skip to content

Commit bafa55e

Browse files
committed
feat: add ability to use local config file instead of downloading it from internet every time
1 parent 6c32b19 commit bafa55e

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

TonLibDotNet/TonClient.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public TonClient(ILogger<TonClient> logger, Microsoft.Extensions.Options.IOption
5353
/// <inheritdoc />
5454
public int SyncStateCurrentSeqno { get; private set; }
5555

56-
/// <summary>
57-
/// Add assembly with additional <see cref="TypeBase"/> classes for LiteServer interaction.
58-
/// </summary>
59-
/// <param name="assembly">Assembly to add</param>
60-
public static void RegisterAssembly(System.Reflection.Assembly assembly)
56+
/// <summary>
57+
/// Add assembly with additional <see cref="TypeBase"/> classes for LiteServer interaction.
58+
/// </summary>
59+
/// <param name="assembly">Assembly to add</param>
60+
public static void RegisterAssembly(System.Reflection.Assembly assembly)
6161
{
6262
TonTypeResolver.AdditionalAsseblies.Add(assembly);
6363
}
@@ -90,8 +90,20 @@ public static void RegisterAssembly(System.Reflection.Assembly assembly)
9090
return null;
9191
}
9292

93-
var httpClient = new HttpClient();
94-
var fullConfig = await httpClient.GetStringAsync(tonOptions.UseMainnet ? tonOptions.ConfigPathMainnet : tonOptions.ConfigPathTestnet).ConfigureAwait(false);
93+
string fullConfig;
94+
var localConfigSource = tonOptions.UseMainnet ? tonOptions.ConfigPathLocalMainnet : tonOptions.ConfigPathLocalTestnet;
95+
if (!string.IsNullOrEmpty(localConfigSource))
96+
{
97+
fullConfig = await File.ReadAllTextAsync(localConfigSource);
98+
logger.LogDebug("Used local config file: {Name}", localConfigSource);
99+
}
100+
else
101+
{
102+
var remoteConfigSource = tonOptions.UseMainnet ? tonOptions.ConfigPathMainnet : tonOptions.ConfigPathTestnet;
103+
using var httpClient = new HttpClient();
104+
fullConfig = await httpClient.GetStringAsync(remoteConfigSource).ConfigureAwait(false);
105+
logger.LogDebug("Used internet config file: {Url}", remoteConfigSource);
106+
}
95107

96108
var jdoc = JsonNode.Parse(fullConfig);
97109
var servers = jdoc["liteservers"].AsArray();

TonLibDotNet/TonOptions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,35 @@ public class TonOptions
88
/// <summary>
99
/// Path to JSON config for testnet (default value is from https://ton.org/docs/develop/dapps/apis/adnl).
1010
/// </summary>
11+
/// <remarks>
12+
/// Unless <see cref="ConfigPathLocalTestnet"/> is set, this url will be used to obtain config file on every TonClient init.
13+
/// </remarks>
1114
public Uri ConfigPathTestnet { get; set; } = new ("https://ton.org/testnet-global.config.json");
1215

1316
/// <summary>
1417
/// Path to JSON config for mainnet (default value is from https://ton.org/docs/develop/dapps/apis/adnl).
1518
/// </summary>
19+
/// <remarks>
20+
/// Unless <see cref="ConfigPathLocalMainnet"/> is set, this url will be used to obtain config file on every TonClient init.
21+
/// </remarks>
1622
public Uri ConfigPathMainnet { get; set; } = new("https://ton.org/global-config.json");
1723

24+
/// <summary>
25+
/// Path to local file with JSON config for testnet (default value is empty string).
26+
/// </summary>
27+
/// <remarks>
28+
/// Download config file and set this property to prevent downloading config from internet (see <see cref="ConfigPathLocalTestnet"/>) every time.
29+
/// </remarks>
30+
public string ConfigPathLocalTestnet { get; set; } = string.Empty;
31+
32+
/// <summary>
33+
/// Path to local file with JSON config for mainnet (default value is empty string).
34+
/// </summary>
35+
/// <remarks>
36+
/// Download config file and set this property to prevent downloading config from internet (see <see cref="ConfigPathLocalMainnet"/>) every time.
37+
/// </remarks>
38+
public string ConfigPathLocalMainnet { get; set; } = string.Empty;
39+
1840
/// <summary>
1941
/// 'True' by default. Set to 'false' to use testnet config.
2042
/// </summary>

0 commit comments

Comments
 (0)