Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Add bloxroute #151

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
dec6b92
add bloxroute v1
Lanikean Aug 16, 2024
6f9430c
change url
Lanikean Aug 16, 2024
a688cc6
refactor
Lanikean Aug 19, 2024
03975b1
priority fee -> tip
Lanikean Aug 19, 2024
03c51b5
switch url remote
Lanikean Aug 19, 2024
1ccd987
adjust re-submit logic
Lanikean Aug 20, 2024
f1c6620
err 6 refactor
Lanikean Aug 20, 2024
9f09333
fix build
Lanikean Aug 20, 2024
af4d40e
add more logging
Lanikean Aug 20, 2024
ec74f1a
retry on endpoint fail
Lanikean Aug 21, 2024
a74899b
switch endpoint
Lanikean Aug 21, 2024
a3c4301
add rpc fallback & refactor
Lanikean Aug 21, 2024
ab2696f
rm hanging log
Lanikean Aug 22, 2024
121c143
add more logging
Lanikean Aug 22, 2024
278e534
add more logging
Lanikean Aug 22, 2024
d7689b3
fix attempts logic
Lanikean Aug 22, 2024
0be6343
mv tx outside of loop
Lanikean Aug 22, 2024
5d2f31a
add jito + rpc backup
Lanikean Aug 23, 2024
a0d1f03
Merge branch 'master' into feat/ore-2.3-blxr
Lanikean Aug 23, 2024
e4113cd
refactor endpoint code outer loop
Lanikean Aug 25, 2024
330168f
more logging
Lanikean Aug 25, 2024
896065c
refactor jito & fallback submissions, only update hash/balance on suc…
Lanikean Aug 26, 2024
c787d3b
Merge branch 'master' into feat/ore-2.3-blxr
Lanikean Aug 30, 2024
67dcae1
cleanup
Lanikean Aug 30, 2024
b45cbce
cleanup
Lanikean Aug 30, 2024
21a6ff1
cleanup
Lanikean Aug 30, 2024
367ead3
cleanup
Lanikean Aug 30, 2024
b22cd6f
cleanup
Lanikean Aug 30, 2024
e4145c6
cleanup
Lanikean Aug 30, 2024
2aab858
Merge pull request #1 from bloXroute-Labs/add-blxr
Lanikean Aug 30, 2024
678ca47
add sample command
Lanikean Sep 4, 2024
af27638
Merge pull request #2 from bloXroute-Labs/add-blxr
Lanikean Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor jito & fallback submissions, only update hash/balance on suc…
…cess, rm continue on err blxr response
  • Loading branch information
Lanikean committed Aug 26, 2024
commit 896065ca85f6440a361d32e0a2598dcfc66a76ae
29 changes: 18 additions & 11 deletions src/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl Miner {
let mut last_balance = 0;
let mut loop_count = 0;
loop {
loop_count += 1;
println!("Starting mining loop iteration {}", loop_count);

// Fetch proof
Expand All @@ -65,8 +64,6 @@ impl Miner {
},
calculate_multiplier(proof.balance, config.top_balance)
);
last_hash_at = proof.last_hash_at;
last_balance = proof.balance;

// Calculate cutoff time
let cutoff_time = self.get_cutoff(proof, args.buffer_time).await;
Expand Down Expand Up @@ -108,10 +105,15 @@ impl Miner {
.send_and_confirm_bx(&ixs, ComputeBudget::Fixed(compute_budget))
.await
{
Ok(signature) => println!(
"Transaction submitted successfully. Signature: {}",
signature
),
Ok(signature) => {
last_hash_at = proof.last_hash_at;
last_balance = proof.balance;
loop_count += 1;
println!(
"Transaction submitted successfully. Signature: {}",
signature
)
}
Err(e) => println!("Error submitting transaction: {:?}", e),
}
} else {
Expand All @@ -121,10 +123,15 @@ impl Miner {
.send_and_confirm(&ixs, ComputeBudget::Fixed(compute_budget), false)
.await
{
Ok(signature) => println!(
"Transaction submitted successfully. Signature: {}",
signature
),
Ok(signature) => {
last_hash_at = proof.last_hash_at;
last_balance = proof.balance;
loop_count += 1;
println!(
"Transaction submitted successfully. Signature: {}",
signature
)
}
Err(e) => println!("Error submitting transaction: {:?}", e),
}
}
Expand Down
128 changes: 88 additions & 40 deletions src/send_and_confirm_bx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use base64::Engine;
use chrono::Local;
use colored::Colorize;
use futures::TryFutureExt;
use ore_api::error::OreError;
use serde::Serialize;
use serde_json::Value;
Expand All @@ -17,18 +16,18 @@ use solana_sdk::{
};
use solana_transaction_status::{TransactionConfirmationStatus, UiTransactionEncoding};
use std::str::FromStr;
use std::time::Duration;

use crate::{
send_and_confirm::{log_error, log_warning, ComputeBudget},
utils::get_latest_blockhash_with_retries,
Miner,
};
use tokio::time::{sleep, Duration};

const GATEWAY_RETRIES: usize = 50;
const GATEWAY_RETRIES: usize = 150;
const GATEWAY_DELAY: u64 = 0;
const CONFIRM_DELAY: u64 = 500;
const CONFIRM_RETRIES: usize = 8;
const CONFIRM_RETRIES: usize = 12;
const BLOXROUTE_URL: &str = "https://ore-ny.solana.dex.blxrbdn.com/api/v2/mine-ore";
// const BLOXROUTE_URL: &str = "http://localhost:9000/api/v2/mine-ore";

Expand Down Expand Up @@ -100,7 +99,6 @@ impl Miner {

loop {
progress_bar.println(format!("Attempt {} of {}", attempts + 1, GATEWAY_RETRIES));
progress_bar.println(format!("skip_submit is currently {}", skip_submit));
if attempts > GATEWAY_RETRIES {
progress_bar.println("Max gateway retries reached. Exiting send_and_confirm_bx.");
return Err(ClientError::from(ClientErrorKind::Custom(
Expand Down Expand Up @@ -176,26 +174,33 @@ impl Miner {
// Submit transaction
progress_bar.set_message("Submitting transaction to Bloxroute...");
let client = reqwest::Client::new();
let response = match client.post(BLOXROUTE_URL).json(&request).send().await {
Ok(response) => response,

let response = client.post(BLOXROUTE_URL).json(&request).send().await;

let (status, response_text) = match response {
Ok(response) => {
let status = response.status();
match response.text().await {
Ok(text) => (status, text),
Err(e) => {
progress_bar.println(format!(
"Failed to get response text: {}. Continuing...",
e
));
(status, String::from("{}"))
}
}
}
Err(e) => {
progress_bar
.println(format!("Bloxroute request error: {}. Retrying...", e));
std::thread::sleep(Duration::from_millis(GATEWAY_DELAY));
attempts += 1;
continue;
.println(format!("Bloxroute request error: {}. Continuing...", e));
(
reqwest::StatusCode::INTERNAL_SERVER_ERROR,
String::from("{}"),
)
}
};

let status = response.status();
let response_text = response.text().await.map_err(|e| {
progress_bar.println(format!("Failed to get response text: {}", e));
ClientError::from(ClientErrorKind::Custom(format!(
"Failed to get response text: {}",
e
)))
})?;

println!("Response Status: {}", status);
println!("Bloxroute Endpoint Response: {}", response_text);

Expand All @@ -221,38 +226,81 @@ impl Miner {
e
)))
})?);
skip_submit = true;
} else {
match &json_response["code"] {
Value::Number(n) => {
let should_retry_rpc = match &json_response.get("code") {
Some(Value::Number(n)) => {
if let Some(code) = n.as_u64() {
if code == 6 {
progress_bar.println(
"Transaction already submitted. Skipping submission...",
);
skip_submit = true;
false // Don't retry with RPC if code is 6
} else {
progress_bar.println("Sending via fallback RPC...");
// attempt to send via RPC client
let send_cfg = RpcSendTransactionConfig {
skip_preflight: true,
preflight_commitment: Some(CommitmentLevel::Confirmed),
encoding: Some(UiTransactionEncoding::Base64),
max_retries: Some(0),
min_context_slot: None,
};
true // Retry with RPC for any other code
}
} else {
true // Retry with RPC if code is not a u64
}
}
_ => true, // Retry with RPC if there's no 'code' field or it's not a number
};

if should_retry_rpc {
progress_bar.println("Attempting via fallback Jito...");
// attempt to send via RPC client
let send_cfg = RpcSendTransactionConfig {
skip_preflight: true,
preflight_commitment: Some(CommitmentLevel::Confirmed),
encoding: Some(UiTransactionEncoding::Base64),
max_retries: Some(0),
min_context_slot: None,
};

self.jito_client
match self
.jito_client
.send_transaction_with_config(&tx, send_cfg)
.await
{
Ok(sig) => {
signature = Some(sig);
skip_submit = true;
progress_bar.println(format!(
"Transaction sent via Jito. Signature: {}",
sig
));
}
Err(e) => {
progress_bar
.println(format!("Failed to send transaction via Jito: {}", e));

// Only try the fallback RPC if we still don't have a signature
if signature.is_none() {
progress_bar.println("Attempting via fallback RPC...");
match self
.rpc_client
.send_transaction_with_config(&tx, send_cfg)
.or_else(|_| {
self.rpc_client
.send_transaction_with_config(&tx, send_cfg)
})
.await
.ok();
{
Ok(sig) => {
signature = Some(sig);
skip_submit = true;
progress_bar.println(format!(
"Transaction sent via fallback RPC. Signature: {}",
sig
));
}
Err(e) => {
progress_bar.println(format!(
"Failed to send transaction via fallback RPC: {}",
e
));
}
}
}
}
}
_ => {}
}
}
} else {
Expand All @@ -264,7 +312,7 @@ impl Miner {

if let Some(sig) = signature {
'confirm: for _ in 0..CONFIRM_RETRIES {
std::thread::sleep(Duration::from_millis(CONFIRM_DELAY));
sleep(Duration::from_millis(CONFIRM_DELAY)).await;
match self.rpc_client.get_signature_statuses(&[sig]).await {
Ok(signature_statuses) => {
for status in signature_statuses.value {
Expand Down Expand Up @@ -347,7 +395,7 @@ impl Miner {

// If we've exhausted all confirmation retries, continue to the next submission attempt
progress_bar.println("Confirmation attempts exhausted. Retrying submission...");
std::thread::sleep(Duration::from_millis(GATEWAY_DELAY));
sleep(Duration::from_millis(GATEWAY_DELAY)).await;
attempts += 1;
}
}
Expand Down