Skip to content

Commit c305d1e

Browse files
buffrrXimik
andcommitted
Update getrollout method
Incorporate changes from PR #20 Closes #19 Co-authored-by: Alex Tsokurov <[email protected]>
1 parent 524ce01 commit c305d1e

File tree

3 files changed

+55
-63
lines changed

3 files changed

+55
-63
lines changed

node/src/bin/space-cli.rs

+7-42
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use protocol::{
1111
bitcoin::{Amount, FeeRate, OutPoint, Txid},
1212
hasher::{KeyHasher},
1313
slabel::SLabel,
14-
Covenant, FullSpaceOut,
1514
};
1615
use serde::{Deserialize, Serialize};
1716
use spaced::{
@@ -153,8 +152,8 @@ enum Commands {
153152
#[command(name = "balance")]
154153
Balance,
155154
/// Pre-create outputs that can be auctioned off during the bidding process
156-
#[command(name = "createbidout")]
157-
CreateBidOut {
155+
#[command(name = "createbidouts")]
156+
CreateBidOuts {
158157
/// Number of output pairs to create
159158
/// Each pair can be used to make a bid
160159
pairs: u8,
@@ -177,8 +176,8 @@ enum Commands {
177176
outpoint: OutPoint,
178177
},
179178
/// Get the estimated rollout batch for the specified interval
180-
#[command(name = "getrolloutestimate")]
181-
GetRolloutEstimate {
179+
#[command(name = "getrollout")]
180+
GetRollout {
182181
// Get the estimated rollout for the target interval. Every ~144 blocks (a rollout interval),
183182
// 10 spaces are released for auction. Specify 0 [default] for the coming interval, 1
184183
// for the interval after and so on.
@@ -377,44 +376,10 @@ async fn handle_commands(
377376
command: Commands,
378377
) -> std::result::Result<(), ClientError> {
379378
match command {
380-
Commands::GetRolloutEstimate {
379+
Commands::GetRollout {
381380
target_interval: target,
382381
} => {
383-
let hashes = cli.client.get_rollout(target).await?;
384-
let mut spaceouts = Vec::with_capacity(hashes.len());
385-
for (priority, spacehash) in hashes {
386-
let outpoint = cli
387-
.client
388-
.get_space_owner(&hex::encode(spacehash.as_slice()))
389-
.await?;
390-
391-
if let Some(outpoint) = outpoint {
392-
if let Some(spaceout) = cli.client.get_spaceout(outpoint).await? {
393-
spaceouts.push((
394-
priority,
395-
FullSpaceOut {
396-
txid: outpoint.txid,
397-
spaceout,
398-
},
399-
));
400-
}
401-
}
402-
}
403-
404-
let data: Vec<_> = spaceouts
405-
.into_iter()
406-
.map(|(priority, spaceout)| {
407-
let space = spaceout.spaceout.space.unwrap();
408-
(
409-
space.name.to_string(),
410-
match space.covenant {
411-
Covenant::Bid { .. } => priority,
412-
_ => 0,
413-
},
414-
)
415-
})
416-
.collect();
417-
382+
let data = cli.client.get_rollout(target).await?;
418383
println!("{}", serde_json::to_string_pretty(&data)?);
419384
}
420385
Commands::EstimateBid { target } => {
@@ -486,7 +451,7 @@ async fn handle_commands(
486451
)
487452
.await?
488453
}
489-
Commands::CreateBidOut { pairs, fee_rate } => {
454+
Commands::CreateBidOuts { pairs, fee_rate } => {
490455
cli.send_request(None, Some(pairs), fee_rate).await?
491456
}
492457
Commands::Register {

node/src/rpc.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::store::RolloutEntry;
12
use std::{
23
collections::BTreeMap, fs, io::Write, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc,
34
};
@@ -58,6 +59,8 @@ pub struct ServerInfo {
5859
pub tip: ChainAnchor,
5960
}
6061

62+
63+
6164
pub enum ChainStateCommand {
6265
GetTip {
6366
resp: Responder<anyhow::Result<ChainAnchor>>,
@@ -89,7 +92,7 @@ pub enum ChainStateCommand {
8992
},
9093
GetRollout {
9194
target: usize,
92-
resp: Responder<anyhow::Result<Vec<(u32, SpaceKey)>>>,
95+
resp: Responder<anyhow::Result<Vec<RolloutEntry>>>,
9396
},
9497
}
9598

@@ -117,7 +120,7 @@ pub trait Rpc {
117120
async fn estimate_bid(&self, target: usize) -> Result<u64, ErrorObjectOwned>;
118121

119122
#[method(name = "getrollout")]
120-
async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceKey)>, ErrorObjectOwned>;
123+
async fn get_rollout(&self, target: usize) -> Result<Vec<RolloutEntry>, ErrorObjectOwned>;
121124

122125
#[method(name = "getblockmeta")]
123126
async fn get_block_meta(
@@ -613,7 +616,7 @@ impl RpcServer for RpcServerImpl {
613616
Ok(info)
614617
}
615618

616-
async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceKey)>, ErrorObjectOwned> {
619+
async fn get_rollout(&self, target: usize) -> Result<Vec<RolloutEntry>, ErrorObjectOwned> {
617620
let rollouts = self
618621
.store
619622
.get_rollout(target)
@@ -932,7 +935,7 @@ impl AsyncChainState {
932935
resp_rx.await?
933936
}
934937

935-
pub async fn get_rollout(&self, target: usize) -> anyhow::Result<Vec<(u32, SpaceKey)>> {
938+
pub async fn get_rollout(&self, target: usize) -> anyhow::Result<Vec<RolloutEntry>> {
936939
let (resp, resp_rx) = oneshot::channel();
937940
self.sender
938941
.send(ChainStateCommand::GetRollout { target, resp })

node/src/store.rs

+41-17
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ use std::{
1010

1111
use anyhow::Result;
1212
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};
2016
use spacedb::{
2117
db::{Database, SnapshotIterator},
2218
fs::FileBackend,
2319
tx::{KeyIterator, ReadTransaction, WriteTransaction},
2420
Configuration, Hash, NodeHasher, Sha256Hasher,
2521
};
2622

23+
#[derive(Debug, Clone, Serialize, Deserialize)]
24+
pub struct RolloutEntry {
25+
pub space: String,
26+
pub value: u32
27+
}
28+
2729
type SpaceDb = Database<Sha256Hasher>;
2830
type ReadTx = ReadTransaction<Sha256Hasher>;
2931
pub type WriteTx<'db> = WriteTransaction<'db, Sha256Hasher>;
@@ -256,7 +258,7 @@ impl LiveSnapshot {
256258
.insert(key, Some(value));
257259
}
258260

259-
fn update_snapshot(&mut self, version: u32) -> anyhow::Result<()> {
261+
fn update_snapshot(&mut self, version: u32) -> Result<()> {
260262
if self.snapshot.0 != version {
261263
self.snapshot.1 = self.db.begin_read()?;
262264
let anchor: ChainAnchor = self.snapshot.1.metadata().try_into().map_err(|_| {
@@ -315,27 +317,49 @@ impl LiveSnapshot {
315317
Ok(())
316318
}
317319

318-
pub fn estimate_bid(&mut self, target: usize) -> anyhow::Result<u64> {
320+
pub fn estimate_bid(&mut self, target: usize) -> Result<u64> {
319321
let rollout = self.get_rollout(target)?;
320322
if rollout.is_empty() {
321323
return Ok(0);
322324
}
323-
let (priority, _) = rollout.last().unwrap();
324-
Ok(*priority as u64)
325+
let entry = rollout.last().unwrap();
326+
Ok(entry.value as u64)
325327
}
326328

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>> {
328330
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+
}
330341

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)
332356
}
333357

334358
pub fn get_rollout_entries(
335359
&mut self,
336360
limit: Option<usize>,
337361
skip: usize,
338-
) -> anyhow::Result<Vec<(u32, SpaceKey)>> {
362+
) -> Result<Vec<(u32, SpaceKey)>> {
339363
// TODO: this could use some clean up
340364
let rlock = self.staged.read().expect("acquire lock");
341365
let mut deleted = BTreeSet::new();
@@ -387,10 +411,10 @@ impl LiveSnapshot {
387411
}
388412
}
389413

390-
impl protocol::prepare::DataSource for LiveSnapshot {
414+
impl DataSource for LiveSnapshot {
391415
fn get_space_outpoint(
392416
&mut self,
393-
space_hash: &protocol::hasher::SpaceKey,
417+
space_hash: &SpaceKey,
394418
) -> protocol::errors::Result<Option<OutPoint>> {
395419
let result: Option<EncodableOutpoint> = self
396420
.get(*space_hash)

0 commit comments

Comments
 (0)