mirror of
https://github.com/0glabs/0g-storage-node.git
synced 2025-04-02 22:45:41 +00:00
add api of getting available file info by root
This commit is contained in:
parent
d43a616b56
commit
e7d538e681
@ -63,7 +63,11 @@ pub trait Rpc {
|
|||||||
async fn check_file_finalized(&self, tx_seq_or_root: TxSeqOrRoot) -> RpcResult<Option<bool>>;
|
async fn check_file_finalized(&self, tx_seq_or_root: TxSeqOrRoot) -> RpcResult<Option<bool>>;
|
||||||
|
|
||||||
#[method(name = "getFileInfo")]
|
#[method(name = "getFileInfo")]
|
||||||
async fn get_file_info(&self, data_root: DataRoot) -> RpcResult<Option<FileInfo>>;
|
async fn get_file_info(
|
||||||
|
&self,
|
||||||
|
data_root: DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> RpcResult<Option<FileInfo>>;
|
||||||
|
|
||||||
#[method(name = "getFileInfoByTxSeq")]
|
#[method(name = "getFileInfoByTxSeq")]
|
||||||
async fn get_file_info_by_tx_seq(&self, tx_seq: u64) -> RpcResult<Option<FileInfo>>;
|
async fn get_file_info_by_tx_seq(&self, tx_seq: u64) -> RpcResult<Option<FileInfo>>;
|
||||||
|
@ -95,7 +95,7 @@ impl RpcServer for RpcServerImpl {
|
|||||||
let tx_seq = try_option!(
|
let tx_seq = try_option!(
|
||||||
self.ctx
|
self.ctx
|
||||||
.log_store
|
.log_store
|
||||||
.get_tx_seq_by_data_root(&data_root)
|
.get_tx_seq_by_data_root(&data_root, true)
|
||||||
.await?
|
.await?
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -121,7 +121,12 @@ impl RpcServer for RpcServerImpl {
|
|||||||
) -> RpcResult<Option<SegmentWithProof>> {
|
) -> RpcResult<Option<SegmentWithProof>> {
|
||||||
info!(%data_root, %index, "zgs_downloadSegmentWithProof");
|
info!(%data_root, %index, "zgs_downloadSegmentWithProof");
|
||||||
|
|
||||||
let tx = try_option!(self.ctx.log_store.get_tx_by_data_root(&data_root).await?);
|
let tx = try_option!(
|
||||||
|
self.ctx
|
||||||
|
.log_store
|
||||||
|
.get_tx_by_data_root(&data_root, true)
|
||||||
|
.await?
|
||||||
|
);
|
||||||
|
|
||||||
self.get_segment_with_proof_by_tx(tx, index).await
|
self.get_segment_with_proof_by_tx(tx, index).await
|
||||||
}
|
}
|
||||||
@ -144,7 +149,12 @@ impl RpcServer for RpcServerImpl {
|
|||||||
let seq = match tx_seq_or_root {
|
let seq = match tx_seq_or_root {
|
||||||
TxSeqOrRoot::TxSeq(v) => v,
|
TxSeqOrRoot::TxSeq(v) => v,
|
||||||
TxSeqOrRoot::Root(v) => {
|
TxSeqOrRoot::Root(v) => {
|
||||||
try_option!(self.ctx.log_store.get_tx_seq_by_data_root(&v).await?)
|
try_option!(
|
||||||
|
self.ctx
|
||||||
|
.log_store
|
||||||
|
.get_tx_seq_by_data_root(&v, false)
|
||||||
|
.await?
|
||||||
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -163,10 +173,19 @@ impl RpcServer for RpcServerImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_file_info(&self, data_root: DataRoot) -> RpcResult<Option<FileInfo>> {
|
async fn get_file_info(
|
||||||
|
&self,
|
||||||
|
data_root: DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> RpcResult<Option<FileInfo>> {
|
||||||
debug!(%data_root, "zgs_getFileInfo");
|
debug!(%data_root, "zgs_getFileInfo");
|
||||||
|
|
||||||
let tx = try_option!(self.ctx.log_store.get_tx_by_data_root(&data_root).await?);
|
let tx = try_option!(
|
||||||
|
self.ctx
|
||||||
|
.log_store
|
||||||
|
.get_tx_by_data_root(&data_root, need_available)
|
||||||
|
.await?
|
||||||
|
);
|
||||||
|
|
||||||
Ok(Some(self.get_file_info_by_tx(tx).await?))
|
Ok(Some(self.get_file_info_by_tx(tx).await?))
|
||||||
}
|
}
|
||||||
@ -288,7 +307,7 @@ impl RpcServerImpl {
|
|||||||
let maybe_tx = self
|
let maybe_tx = self
|
||||||
.ctx
|
.ctx
|
||||||
.log_store
|
.log_store
|
||||||
.get_tx_by_data_root(&segment.root)
|
.get_tx_by_data_root(&segment.root, false)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.put_segment_with_maybe_tx(segment, maybe_tx).await
|
self.put_segment_with_maybe_tx(segment, maybe_tx).await
|
||||||
|
@ -59,15 +59,23 @@ impl Store {
|
|||||||
delegate!(fn get_proof_at_root(root: Option<DataRoot>, index: u64, length: u64) -> Result<FlowRangeProof>);
|
delegate!(fn get_proof_at_root(root: Option<DataRoot>, index: u64, length: u64) -> Result<FlowRangeProof>);
|
||||||
delegate!(fn get_context() -> Result<(DataRoot, u64)>);
|
delegate!(fn get_context() -> Result<(DataRoot, u64)>);
|
||||||
|
|
||||||
pub async fn get_tx_seq_by_data_root(&self, data_root: &DataRoot) -> Result<Option<u64>> {
|
pub async fn get_tx_seq_by_data_root(
|
||||||
|
&self,
|
||||||
|
data_root: &DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> Result<Option<u64>> {
|
||||||
let root = *data_root;
|
let root = *data_root;
|
||||||
self.spawn(move |store| store.get_tx_seq_by_data_root(&root))
|
self.spawn(move |store| store.get_tx_seq_by_data_root(&root, need_available))
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_tx_by_data_root(&self, data_root: &DataRoot) -> Result<Option<Transaction>> {
|
pub async fn get_tx_by_data_root(
|
||||||
|
&self,
|
||||||
|
data_root: &DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> Result<Option<Transaction>> {
|
||||||
let root = *data_root;
|
let root = *data_root;
|
||||||
self.spawn(move |store| store.get_tx_by_data_root(&root))
|
self.spawn(move |store| store.get_tx_by_data_root(&root, need_available))
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -511,7 +511,7 @@ impl LogStoreChunkRead for LogManager {
|
|||||||
index_start: usize,
|
index_start: usize,
|
||||||
index_end: usize,
|
index_end: usize,
|
||||||
) -> crate::error::Result<Option<ChunkArray>> {
|
) -> crate::error::Result<Option<ChunkArray>> {
|
||||||
let tx_seq = try_option!(self.get_tx_seq_by_data_root(data_root)?);
|
let tx_seq = try_option!(self.get_tx_seq_by_data_root(data_root, false)?);
|
||||||
self.get_chunks_by_tx_and_index_range(tx_seq, index_start, index_end)
|
self.get_chunks_by_tx_and_index_range(tx_seq, index_start, index_end)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -536,7 +536,11 @@ impl LogStoreRead for LogManager {
|
|||||||
self.tx_store.get_tx_by_seq_number(seq)
|
self.tx_store.get_tx_by_seq_number(seq)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tx_seq_by_data_root(&self, data_root: &DataRoot) -> crate::error::Result<Option<u64>> {
|
fn get_tx_seq_by_data_root(
|
||||||
|
&self,
|
||||||
|
data_root: &DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> crate::error::Result<Option<u64>> {
|
||||||
let seq_list = self.tx_store.get_tx_seq_list_by_data_root(data_root)?;
|
let seq_list = self.tx_store.get_tx_seq_list_by_data_root(data_root)?;
|
||||||
for tx_seq in &seq_list {
|
for tx_seq in &seq_list {
|
||||||
if self.tx_store.check_tx_completed(*tx_seq)? {
|
if self.tx_store.check_tx_completed(*tx_seq)? {
|
||||||
@ -544,6 +548,9 @@ impl LogStoreRead for LogManager {
|
|||||||
return Ok(Some(*tx_seq));
|
return Ok(Some(*tx_seq));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if need_available {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
// No tx is finalized, return the first one.
|
// No tx is finalized, return the first one.
|
||||||
Ok(seq_list.first().cloned())
|
Ok(seq_list.first().cloned())
|
||||||
}
|
}
|
||||||
|
@ -31,14 +31,22 @@ pub trait LogStoreRead: LogStoreChunkRead {
|
|||||||
fn get_tx_by_seq_number(&self, seq: u64) -> Result<Option<Transaction>>;
|
fn get_tx_by_seq_number(&self, seq: u64) -> Result<Option<Transaction>>;
|
||||||
|
|
||||||
/// Get a transaction by the data root of its data.
|
/// Get a transaction by the data root of its data.
|
||||||
/// If all txs are not finalized, return the first one.
|
/// If all txs are not finalized, return the first one if need available is false.
|
||||||
/// Otherwise, return the first finalized tx.
|
/// Otherwise, return the first finalized tx.
|
||||||
fn get_tx_seq_by_data_root(&self, data_root: &DataRoot) -> Result<Option<u64>>;
|
fn get_tx_seq_by_data_root(
|
||||||
|
&self,
|
||||||
|
data_root: &DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> Result<Option<u64>>;
|
||||||
|
|
||||||
/// If all txs are not finalized, return the first one.
|
/// If all txs are not finalized, return the first one if need available is false.
|
||||||
/// Otherwise, return the first finalized tx.
|
/// Otherwise, return the first finalized tx.
|
||||||
fn get_tx_by_data_root(&self, data_root: &DataRoot) -> Result<Option<Transaction>> {
|
fn get_tx_by_data_root(
|
||||||
match self.get_tx_seq_by_data_root(data_root)? {
|
&self,
|
||||||
|
data_root: &DataRoot,
|
||||||
|
need_available: bool,
|
||||||
|
) -> Result<Option<Transaction>> {
|
||||||
|
match self.get_tx_seq_by_data_root(data_root, need_available)? {
|
||||||
Some(seq) => self.get_tx_by_seq_number(seq),
|
Some(seq) => self.get_tx_by_seq_number(seq),
|
||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user