@@ -7,12 +7,13 @@ use drillx::{
7
7
} ;
8
8
use ore_api:: {
9
9
consts:: { BUS_ADDRESSES , BUS_COUNT , EPOCH_DURATION } ,
10
- state:: { Config , Proof } ,
10
+ state:: { Bus , Config , Proof } ,
11
11
} ;
12
+ use ore_utils:: AccountDeserialize ;
12
13
use rand:: Rng ;
13
14
use solana_program:: pubkey:: Pubkey ;
14
15
use solana_rpc_client:: spinner;
15
- use solana_sdk:: signer:: Signer ;
16
+ use solana_sdk:: { account , signer:: Signer } ;
16
17
17
18
use crate :: {
18
19
args:: MineArgs ,
@@ -59,7 +60,7 @@ impl Miner {
59
60
ixs. push ( ore_api:: instruction:: mine (
60
61
signer. pubkey ( ) ,
61
62
signer. pubkey ( ) ,
62
- find_bus ( ) ,
63
+ self . find_bus ( ) . await ,
63
64
solution,
64
65
) ) ;
65
66
self . send_and_confirm ( & ixs, ComputeBudget :: Fixed ( compute_budget) , false )
@@ -194,14 +195,31 @@ impl Miner {
194
195
. saturating_sub ( clock. unix_timestamp )
195
196
. max ( 0 ) as u64
196
197
}
198
+
199
+ async fn find_bus ( & self ) -> Pubkey {
200
+ // Fetch the bus with the largest balance
201
+ if let Ok ( accounts) = self . rpc_client . get_multiple_accounts ( & BUS_ADDRESSES ) . await {
202
+ let mut top_bus_balance: u64 = 0 ;
203
+ let mut top_bus = BUS_ADDRESSES [ 0 ] ;
204
+ for account in accounts {
205
+ if let Some ( account) = account {
206
+ if let Ok ( bus) = Bus :: try_from_bytes ( & account. data ) {
207
+ if bus. rewards . gt ( & top_bus_balance) {
208
+ top_bus_balance = bus. rewards ;
209
+ top_bus = BUS_ADDRESSES [ bus. id as usize ] ;
210
+ }
211
+ }
212
+ }
213
+ }
214
+ return top_bus;
215
+ }
216
+
217
+ // Otherwise return a random bus
218
+ let i = rand:: thread_rng ( ) . gen_range ( 0 ..BUS_COUNT ) ;
219
+ BUS_ADDRESSES [ i]
220
+ }
197
221
}
198
222
199
223
fn calculate_multiplier ( balance : u64 , top_balance : u64 ) -> f64 {
200
224
1.0 + ( balance as f64 / top_balance as f64 ) . min ( 1.0f64 )
201
225
}
202
-
203
- // TODO Pick a better strategy (avoid draining bus)
204
- fn find_bus ( ) -> Pubkey {
205
- let i = rand:: thread_rng ( ) . gen_range ( 0 ..BUS_COUNT ) ;
206
- BUS_ADDRESSES [ i]
207
- }
0 commit comments