@@ -10,20 +10,22 @@ use std::{
10
10
11
11
use anyhow:: Result ;
12
12
use bincode:: { config, Decode , Encode } ;
13
- use protocol:: {
14
- bitcoin:: OutPoint ,
15
- constants:: { ChainAnchor , ROLLOUT_BATCH_SIZE } ,
16
- hasher:: { BidKey , KeyHash , OutpointKey , SpaceKey } ,
17
- prepare:: DataSource ,
18
- FullSpaceOut , SpaceOut ,
19
- } ;
13
+ use jsonrpsee:: core:: Serialize ;
14
+ use serde:: Deserialize ;
15
+ use protocol:: { bitcoin:: OutPoint , constants:: { ChainAnchor , ROLLOUT_BATCH_SIZE } , hasher:: { BidKey , KeyHash , OutpointKey , SpaceKey } , prepare:: DataSource , Covenant , FullSpaceOut , SpaceOut } ;
20
16
use spacedb:: {
21
17
db:: { Database , SnapshotIterator } ,
22
18
fs:: FileBackend ,
23
19
tx:: { KeyIterator , ReadTransaction , WriteTransaction } ,
24
20
Configuration , Hash , NodeHasher , Sha256Hasher ,
25
21
} ;
26
22
23
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
24
+ pub struct RolloutEntry {
25
+ pub space : String ,
26
+ pub value : u32
27
+ }
28
+
27
29
type SpaceDb = Database < Sha256Hasher > ;
28
30
type ReadTx = ReadTransaction < Sha256Hasher > ;
29
31
pub type WriteTx < ' db > = WriteTransaction < ' db , Sha256Hasher > ;
@@ -256,7 +258,7 @@ impl LiveSnapshot {
256
258
. insert ( key, Some ( value) ) ;
257
259
}
258
260
259
- fn update_snapshot ( & mut self , version : u32 ) -> anyhow :: Result < ( ) > {
261
+ fn update_snapshot ( & mut self , version : u32 ) -> Result < ( ) > {
260
262
if self . snapshot . 0 != version {
261
263
self . snapshot . 1 = self . db . begin_read ( ) ?;
262
264
let anchor: ChainAnchor = self . snapshot . 1 . metadata ( ) . try_into ( ) . map_err ( |_| {
@@ -315,27 +317,49 @@ impl LiveSnapshot {
315
317
Ok ( ( ) )
316
318
}
317
319
318
- pub fn estimate_bid ( & mut self , target : usize ) -> anyhow :: Result < u64 > {
320
+ pub fn estimate_bid ( & mut self , target : usize ) -> Result < u64 > {
319
321
let rollout = self . get_rollout ( target) ?;
320
322
if rollout. is_empty ( ) {
321
323
return Ok ( 0 ) ;
322
324
}
323
- let ( priority , _ ) = rollout. last ( ) . unwrap ( ) ;
324
- Ok ( * priority as u64 )
325
+ let entry = rollout. last ( ) . unwrap ( ) ;
326
+ Ok ( entry . value as u64 )
325
327
}
326
328
327
- pub fn get_rollout ( & mut self , target : usize ) -> anyhow :: Result < Vec < ( u32 , SpaceKey ) > > {
329
+ pub fn get_rollout ( & mut self , target : usize ) -> Result < Vec < RolloutEntry > > {
328
330
let skip = target * ROLLOUT_BATCH_SIZE ;
329
- let entries = self . get_rollout_entries ( Some ( ROLLOUT_BATCH_SIZE ) , skip) ?;
331
+ let rollouts = self . get_rollout_entries ( Some ( ROLLOUT_BATCH_SIZE ) , skip) ?;
332
+ let mut spaceouts = Vec :: with_capacity ( rollouts. len ( ) ) ;
333
+ for ( priority, spacehash) in rollouts {
334
+ let outpoint = self . get_space_outpoint ( & spacehash) ?;
335
+ if let Some ( outpoint) = outpoint {
336
+ if let Some ( spaceout) = self . get_spaceout ( & outpoint) ? {
337
+ spaceouts. push ( ( priority, FullSpaceOut { txid : outpoint. txid , spaceout } ) ) ;
338
+ }
339
+ }
340
+ }
330
341
331
- Ok ( entries)
342
+ let data: Vec < _ > = spaceouts
343
+ . into_iter ( )
344
+ . map ( |( priority, spaceout) | {
345
+ let space = spaceout. spaceout . space . unwrap ( ) ;
346
+ RolloutEntry {
347
+ space : space. name . to_string ( ) ,
348
+ value : match space. covenant {
349
+ Covenant :: Bid { .. } => priority,
350
+ _ => 0 ,
351
+ } ,
352
+ }
353
+ } )
354
+ . collect ( ) ;
355
+ Ok ( data)
332
356
}
333
357
334
358
pub fn get_rollout_entries (
335
359
& mut self ,
336
360
limit : Option < usize > ,
337
361
skip : usize ,
338
- ) -> anyhow :: Result < Vec < ( u32 , SpaceKey ) > > {
362
+ ) -> Result < Vec < ( u32 , SpaceKey ) > > {
339
363
// TODO: this could use some clean up
340
364
let rlock = self . staged . read ( ) . expect ( "acquire lock" ) ;
341
365
let mut deleted = BTreeSet :: new ( ) ;
@@ -387,10 +411,10 @@ impl LiveSnapshot {
387
411
}
388
412
}
389
413
390
- impl protocol :: prepare :: DataSource for LiveSnapshot {
414
+ impl DataSource for LiveSnapshot {
391
415
fn get_space_outpoint (
392
416
& mut self ,
393
- space_hash : & protocol :: hasher :: SpaceKey ,
417
+ space_hash : & SpaceKey ,
394
418
) -> protocol:: errors:: Result < Option < OutPoint > > {
395
419
let result: Option < EncodableOutpoint > = self
396
420
. get ( * space_hash)
0 commit comments