Refactor transaction sending and add SmartBitClient
MODIFY
HiddenWallet.FullSpvWallet/WalletJob.cs
HiddenWallet.FullSpvWallet/WalletJob.cs
@@ -21,6 +21,7 @@ using HiddenWallet.KeyManagement;
using HiddenWallet.FullSpv.MemPool;
using HiddenWallet.FullSpv.Fees;
using Nito.AsyncEx;
+using HiddenWallet.WebClients.SmartBit;
namespace HiddenWallet.FullSpv
{
@@ -34,9 +35,7 @@ namespace HiddenWallet.FullSpv
public bool TracksDefaultSafe { get; private set; }
public ConcurrentHashSet<SafeAccount> SafeAccounts { get; private set; }
- public QBitNinjaClient TorQBitClient { get; private set; }
- public QBitNinjaClient NoTorQBitClient { get; private set; }
- public HttpClient TorHttpClient { get; private set; }
+ public SmartBitClient TorSmartBitClient { get; private set; }
public DotNetTor.ControlPort.Client ControlPortClient { get; private set; }
public BlockDownloader BlockDownloader;
@@ -237,11 +236,8 @@ namespace HiddenWallet.FullSpv
Enabled = false
};
- NoTorQBitClient = new QBitNinjaClient(safeToTrack.Network);
- TorQBitClient = new QBitNinjaClient(safeToTrack.Network);
- TorQBitClient.SetHttpMessageHandler(handler);
- TorHttpClient = new HttpClient(handler);
ControlPortClient = controlPortClient;
+ TorSmartBitClient = new SmartBitClient(safeToTrack.Network, handler, false);
FeeService = new FeeService(safeToTrack.Network, ControlPortClient, disposeTorControl: false, handler: handler, disposeHandler: false);
@@ -374,7 +370,7 @@ namespace HiddenWallet.FullSpv
task?.Dispose();
}
FeeService?.Dispose();
- TorHttpClient?.Dispose();
+ TorSmartBitClient?.Dispose();
try
{
ControlPortClient?.DisconnectDisposeSocket();
@@ -1228,6 +1224,8 @@ namespace HiddenWallet.FullSpv
public async Task<SendTransactionResult> SendTransactionAsync(Transaction tx)
{
+ Debug.WriteLine($"Broadcasting Transaction: {tx.GetHash()}");
+
if (State < WalletState.SyncingMemPool)
{
return new SendTransactionResult
@@ -1238,47 +1236,29 @@ namespace HiddenWallet.FullSpv
};
}
- try
+ var failureResult = new SendTransactionResult
{
- await ControlPortClient.ChangeCircuitAsync();
+ Success = false,
+ FailingReason =
+ $"The transaction might not have been successfully broadcasted. Check the Transaction ID in a block explorer. Transaction Id: {tx.GetHash()}"
+ };
+
var successfulResult = new SendTransactionResult
{
Success = true,
FailingReason = ""
};
- Debug.WriteLine($"Transaction Id: {tx.GetHash()}");
- // times out at 21sec, last is smartbit, doesn't check for responses, they are sometimes buggy
- var counter = 0;
- while (true)
- {
- var smartBitResponse = new HttpResponseMessage();
- var qbitResponse = new BroadcastResponse();
try
{
- Debug.Write("Broadcasting with ");
- if (counter % 2 == 0)
- {
- Debug.WriteLine("QBit...");
- qbitResponse = await TorQBitClient.Broadcast(tx);
- }
- else
- {
- Debug.WriteLine("SmartBit...");
- var post = "https://testnet-api.smartbit.com.au/v1/blockchain/pushtx";
- if (CurrentNetwork == Network.Main)
- post = "https://api.smartbit.com.au/v1/blockchain/pushtx";
+ Debug.WriteLine($"Changing Tor circuit: {tx.GetHash()}");
+ await ControlPortClient.ChangeCircuitAsync();
- var content = new StringContent(new JObject(new JProperty("hex", tx.ToHex())).ToString(), Encoding.UTF8,
- "application/json");
- smartBitResponse = await TorHttpClient.PostAsync(post, content);
- }
- }
- catch (Exception)
+ await Task.Delay(100);
+ await TorSmartBitClient.PushTransactionAsync(tx, CancellationToken.None);
+
+ for (int i = 0; i < 21; i++)
{
- counter++;
- continue;
- }
await Task.Delay(1000);
var arrived = MemPoolJob.Transactions.Contains(tx.GetHash());
if (arrived)
@@ -1286,53 +1266,13 @@ namespace HiddenWallet.FullSpv
Debug.WriteLine("Transaction is successfully propagated on the network.");
return successfulResult;
}
-
- if (counter >= 21) // keep it odd, smartbit has more reliable error messages
- {
- string reason = "";
- if (counter % 2 == 0) //qbit
- {
- reason = $"Success: {qbitResponse.Success}";
- if (qbitResponse.Error != null)
- {
- reason += $", ErrorCode: {qbitResponse.Error.ErrorCode}, Reason: {qbitResponse.Error.Reason}";
- }
- }
- else //smartbit
- {
- var json = JObject.Parse(await smartBitResponse.Content.ReadAsStringAsync());
- if (json.Value<bool>("success"))
- {
- Debug.WriteLine("Transaction is successfully propagated on the network.");
- return successfulResult;
- }
- else
- {
- Debug.WriteLine(
- $"Error code: {json["error"].Value<string>("code")} Reason: {json["error"].Value<string>("message")}");
- }
- reason = $"Success: { json.Value<bool>("success") } Error code: {json["error"].Value<string>("code")} Reason: {json["error"].Value<string>("message")}";
- }
- return new SendTransactionResult
- {
- Success = false,
- FailingReason =
- $"The transaction might not have been successfully broadcasted. Check the Transaction ID in a block explorer. Details: {reason}"
- };
- }
- counter++;
- smartBitResponse?.Dispose();
}
}
catch(Exception ex)
{
- return new SendTransactionResult
- {
- Success = false,
- FailingReason =
- "The transaction might not have been successfully broadcasted. Check the Transaction ID in a block explorer. Details:" + ex.ToString()
- };
+ failureResult.FailingReason += $" Details: {ex.ToString()}";
}
+ return failureResult;
}
public struct SendTransactionResult

MODIFY
HiddenWallet/HiddenWallet.csproj
HiddenWallet/HiddenWallet.csproj
<ItemGroup>
<Folder Include="Extensions\" />
- <Folder Include="WebClients\SmartBit\Models\" />
</ItemGroup>
<ItemGroup>

Refactor transaction sending and add SmartBitClient