Skip to content

Commit

Permalink
HSDBidder: Update logging and added some error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathanwoodburn committed Feb 23, 2023
1 parent a5f70ec commit 5b20a5a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 33 deletions.
52 changes: 49 additions & 3 deletions BidderGUI/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 84 additions & 30 deletions BidderGUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ void refreshdomain(string file)
}
else
{
logtextBox.Text += "Domain already in list: " + domain + Environment.NewLine;

addlog("Domain already in list: " + domain);
}
}
filereader.Close();
}
// Log errors to log textbox
catch (Exception error)
{
logtextBox.Text += "Error: " + error.Message + Environment.NewLine;

addlog("Error: " + error.Message);
}


Expand All @@ -62,7 +63,7 @@ async void test()
// This will curl the below URL and return the result
//curl http://x:[email protected]:12039/wallet/$id/account

logtextBox.Text = logtextBox.Text + "Testing: http://x:" + apitextBox.Text + "@"+ipporttextBox.Text+ Environment.NewLine;
addlog("Testing: http://x:" + apitextBox.Text + "@"+ipporttextBox.Text);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://"+ ipporttextBox.Text + "/wallet/"+wallettextBox.Text+ "/account");
// Add API key to header
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:"+apitextBox.Text)));
Expand All @@ -73,12 +74,14 @@ async void test()
HttpResponseMessage response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine;
addlog(responseBody);

}
// Log errors to log textbox
catch (Exception error)
{
logtextBox.Text = logtextBox.Text + "Error: "+ error.Message+ Environment.NewLine;

addlog("Error: " + error.Message);
}
}

Expand Down Expand Up @@ -184,7 +187,8 @@ async void sendtransaction()
// Get domain
string domain = domainslistBox.Items[0].ToString();
// Log transaction attempt
logtextBox.Text = logtextBox.Text + "Sending " + modecomboBox.Text + " for: " + domain + Environment.NewLine;
addlog("Sending " + modecomboBox.Text + " for: " + domain);


// Select mode
if (modecomboBox.Text == "BID")
Expand Down Expand Up @@ -236,7 +240,7 @@ async void sendtransaction()
else
{
// Log error
logtextBox.Text = logtextBox.Text + "No Domains Found. Cancelled Sending" + Environment.NewLine;
addlog("No Domains Found. Cancelled Sending");
// Stop timers
stopbutton.PerformClick();

Expand All @@ -246,7 +250,7 @@ async void sendtransaction()
else
{
// Log error
logtextBox.Text = logtextBox.Text + "Invalid Mode. Cancelled Sending" + Environment.NewLine;
addlog("Invalid Mode.Cancelled Sending");
// Stop timers
stopbutton.PerformClick();
}
Expand All @@ -273,7 +277,7 @@ async void sendbatchbid(string[] domains)
batch = batch.Substring(0, batch.Length - 2) + "]";

// Log transaction attempt
logtextBox.Text = logtextBox.Text + "Sending: " + batch + Environment.NewLine;
addlog("Sending: " + batch);

// Create the API call
request.Content = new StringContent("{\"method\": \"sendbatch\",\"params\":[ " + batch + "]}");
Expand All @@ -284,26 +288,32 @@ async void sendbatchbid(string[] domains)
string responseBody = await response.Content.ReadAsStringAsync();

// Log response
logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine;
addlog(responseBody);

// Remove domains from list
foreach (string domain in domains)
// Check for errors
if (!Checkerrors(responseBody))
{
domainslistBox.Items.Remove(domain);
// Remove domains from list
foreach (string domain in domains)
{
domainslistBox.Items.Remove(domain);
}
}


// If there are no domains left in the list
// Stop timers
if (domainslistBox.Items.Count == 0)
{
logtextBox.Text = logtextBox.Text + "All domains sent" + Environment.NewLine;
addlog("All domains sent");
stopbutton.PerformClick();
}
}
// If there is an error
catch (Exception ex)
{
// Log error
logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine;
addlog("Error: " + ex.Message);
// Stop timers
stopbutton.PerformClick();
}
Expand Down Expand Up @@ -331,7 +341,7 @@ async void sendbatch(string[] domains, string method)
batch = batch.Substring(0, batch.Length - 2) + "]";

// Log transaction attempt
logtextBox.Text = logtextBox.Text + "Sending: " + batch + Environment.NewLine;
addlog("Sending: " + batch);

// Create the API call
request.Content = new StringContent("{\"method\": \"sendbatch\",\"params\":[ " + batch + "]}");
Expand All @@ -342,26 +352,30 @@ async void sendbatch(string[] domains, string method)
string responseBody = await response.Content.ReadAsStringAsync();

// Log response
logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine;
addlog(responseBody);

// Remove domains from list
foreach (string domain in domains)
if (!Checkerrors(responseBody))
{
domainslistBox.Items.Remove(domain);
// Remove domains from list
foreach (string domain in domains)
{
domainslistBox.Items.Remove(domain);
}
}
// If there are no domains left in the list
// Stop timers
if (domainslistBox.Items.Count == 0)
{
logtextBox.Text = logtextBox.Text + "All domains sent" + Environment.NewLine;
addlog("All domains sent");
stopbutton.PerformClick();
}
}
// If there is an error
catch (Exception ex)
{
// Log error
logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine;
addlog("Error: " + ex.Message);
// Stop timers
stopbutton.PerformClick();
}
Expand All @@ -371,7 +385,7 @@ async void sendbatch(string[] domains, string method)
async Task unlockwallet()
{
// Unlocking wallet
logtextBox.Text = logtextBox.Text + "Unlocking Wallet using passphrase" + Environment.NewLine;
addlog("Unlocking Wallet using passphrase");
HttpRequestMessage unlockwalletreq = new HttpRequestMessage(HttpMethod.Post, "http://" + ipporttextBox.Text + "/wallet/" + wallettextBox.Text + "/unlock");
unlockwalletreq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:" + apitextBox.Text)));
unlockwalletreq.Content = new StringContent("{\"passphrase\": \"" + passtextBox.Text + "\",\"timeout\": 60}");
Expand All @@ -383,7 +397,7 @@ async Task unlockwallet()
unlockwalletresp.EnsureSuccessStatusCode();

// Select wallet
logtextBox.Text = logtextBox.Text + "Selecting Wallet" + Environment.NewLine;
addlog("Selecting Wallet");
HttpRequestMessage selectwalletreq = new HttpRequestMessage(HttpMethod.Post, "http://" + ipporttextBox.Text);
selectwalletreq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:" + apitextBox.Text)));
selectwalletreq.Content = new StringContent("{\"method\": \"selectwallet\",\"params\":[ \"" + wallettextBox.Text + "\"]}");
Expand All @@ -405,16 +419,21 @@ async void sendapicall(HttpRequestMessage request,string domain)
string responseBody = await response.Content.ReadAsStringAsync();

// Log response
logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine;

addlog(responseBody);
// Remove domain from list
domainslistBox.Items.Remove(domain);
if (!Checkerrors(responseBody))
{
// Remove domains from list
domainslistBox.Items.Remove(domain);

}

}
// If there is an error
catch (Exception ex)
{
// Log error
logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine;
addlog("Error: " + ex.Message);
// Stop timers
stopbutton.PerformClick();
}
Expand Down Expand Up @@ -448,8 +467,7 @@ private void removebutton_Click(object sender, EventArgs e)
}
catch (Exception ex)
{
logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine;

addlog("Error: " + ex.Message);
}

}
Expand Down Expand Up @@ -506,7 +524,7 @@ private void export_button_Click(object sender, EventArgs e)
catch (Exception ex)
{
// Log error
logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine;
addlog("Error: " + ex.Message);
}

}
Expand Down Expand Up @@ -553,5 +571,41 @@ private void button_cleardomains_Click(object sender, EventArgs e)
{
domainslistBox.Items.Clear();
}
string[] errors = { "tx exceeds maximum unconfirmed ancestors" ,"error\":{\"message"};
public bool Checkerrors(string log)
{
log = log.ToLower();
// If there is an error in the log
foreach (string error in errors)
{
if (log.Contains(error))
{
stopbutton.PerformClick();
// Log error
addlog("Errors Found");
return true;
}
}
return false;
}
public void addlog(string log)
{
// Count lines in log textbox
string[] lines = logtextBox.Text.Split(Environment.NewLine);
int numlines = lines.Length;
int maxlines = Convert.ToInt32(loglinesnumeric.Value) - 1;

string[] newlines = new string[maxlines];
if (numlines > maxlines)
{
Array.Copy(lines, numlines- maxlines, newlines, 0, maxlines);
}
else
{
newlines = lines;
}

logtextBox.Text = string.Join(Environment.NewLine, newlines) + Environment.NewLine + log;
}
}
}

0 comments on commit 5b20a5a

Please sign in to comment.