Skip to content
Snippets Groups Projects
Commit e27f88ad authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist:
Browse files

[feat] gva: TxsHistoryBc from, to

parent 1f8e5cff
Branches
Tags
1 merge request!1363[feat] gva: TxsHistoryBc from, to
...@@ -49,11 +49,37 @@ impl FromStr for TxBcCursor { ...@@ -49,11 +49,37 @@ impl FromStr for TxBcCursor {
impl DbsReader { impl DbsReader {
pub fn get_txs_history_bc_received( pub fn get_txs_history_bc_received(
&self, &self,
from: Option<u64>,
page_info: PageInfo<TxBcCursor>, page_info: PageInfo<TxBcCursor>,
script_hash: Hash, script_hash: Hash,
to: Option<u64>,
) -> KvResult<PagedData<VecDeque<GvaTxDbV1>>> { ) -> KvResult<PagedData<VecDeque<GvaTxDbV1>>> {
let mut start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(0)); let mut start_k = WalletHashWithBnV1Db::new(
let mut end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(u32::MAX)); script_hash,
BlockNumber(if let Some(from) = from {
self.0
.blocks_by_common_time()
.iter(U64BE(from).., |it| it)
.values()
.next_res()?
.unwrap_or(u32::MAX)
} else {
0
}),
);
let mut end_k = WalletHashWithBnV1Db::new(
script_hash,
BlockNumber(if let Some(to) = to {
self.0
.blocks_by_common_time()
.iter_rev(..U64BE(to), |it| it)
.values()
.next_res()?
.unwrap_or(0)
} else {
u32::MAX
}),
);
let first_cursor_opt = if page_info.not_all() { let first_cursor_opt = if page_info.not_all() {
self.0 self.0
.txs_by_recipient() .txs_by_recipient()
...@@ -168,11 +194,37 @@ impl DbsReader { ...@@ -168,11 +194,37 @@ impl DbsReader {
} }
pub fn get_txs_history_bc_sent( pub fn get_txs_history_bc_sent(
&self, &self,
from: Option<u64>,
page_info: PageInfo<TxBcCursor>, page_info: PageInfo<TxBcCursor>,
script_hash: Hash, script_hash: Hash,
to: Option<u64>,
) -> KvResult<PagedData<VecDeque<GvaTxDbV1>>> { ) -> KvResult<PagedData<VecDeque<GvaTxDbV1>>> {
let mut start_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(0)); let mut start_k = WalletHashWithBnV1Db::new(
let mut end_k = WalletHashWithBnV1Db::new(script_hash, BlockNumber(u32::MAX)); script_hash,
BlockNumber(if let Some(from) = from {
self.0
.blocks_by_common_time()
.iter(U64BE(from).., |it| it)
.values()
.next_res()?
.unwrap_or(u32::MAX)
} else {
0
}),
);
let mut end_k = WalletHashWithBnV1Db::new(
script_hash,
BlockNumber(if let Some(to) = to {
self.0
.blocks_by_common_time()
.iter_rev(..U64BE(to), |it| it)
.values()
.next_res()?
.unwrap_or(0)
} else {
u32::MAX
}),
);
let first_cursor_opt = if page_info.not_all() { let first_cursor_opt = if page_info.not_all() {
self.0 self.0
.txs_by_issuer() .txs_by_issuer()
...@@ -525,6 +577,7 @@ mod tests { ...@@ -525,6 +577,7 @@ mod tests {
WalletHashWithBnV1Db::new(s1_hash, BlockNumber(1)), WalletHashWithBnV1Db::new(s1_hash, BlockNumber(1)),
btreeset![Hash::default(), Hash([1; 32]), Hash([2; 32]), Hash([3; 32])], btreeset![Hash::default(), Hash([1; 32]), Hash([2; 32]), Hash([3; 32])],
)?; )?;
gva_db.blocks_by_common_time_write().upsert(U64BE(1), 1)?;
gva_db.txs_write().upsert( gva_db.txs_write().upsert(
HashKeyV2(Hash([4; 32])), HashKeyV2(Hash([4; 32])),
gen_tx(Hash([4; 32]), BlockNumber(2)), gen_tx(Hash([4; 32]), BlockNumber(2)),
...@@ -537,6 +590,34 @@ mod tests { ...@@ -537,6 +590,34 @@ mod tests {
WalletHashWithBnV1Db::new(s1_hash, BlockNumber(2)), WalletHashWithBnV1Db::new(s1_hash, BlockNumber(2)),
btreeset![Hash([4; 32]), Hash([5; 32])], btreeset![Hash([4; 32]), Hash([5; 32])],
)?; )?;
gva_db.blocks_by_common_time_write().upsert(U64BE(2), 2)?;
gva_db.txs_write().upsert(
HashKeyV2(Hash([6; 32])),
gen_tx(Hash([6; 32]), BlockNumber(3)),
)?;
gva_db.txs_by_recipient_write().upsert(
WalletHashWithBnV1Db::new(s1_hash, BlockNumber(3)),
btreeset![Hash([6; 32])],
)?;
gva_db.blocks_by_common_time_write().upsert(U64BE(3), 3)?;
gva_db.txs_write().upsert(
HashKeyV2(Hash([7; 32])),
gen_tx(Hash([7; 32]), BlockNumber(4)),
)?;
gva_db.txs_by_recipient_write().upsert(
WalletHashWithBnV1Db::new(s1_hash, BlockNumber(4)),
btreeset![Hash([7; 32])],
)?;
gva_db.blocks_by_common_time_write().upsert(U64BE(4), 4)?;
gva_db.txs_write().upsert(
HashKeyV2(Hash([8; 32])),
gen_tx(Hash([8; 32]), BlockNumber(5)),
)?;
gva_db.txs_by_recipient_write().upsert(
WalletHashWithBnV1Db::new(s1_hash, BlockNumber(5)),
btreeset![Hash([8; 32])],
)?;
gva_db.blocks_by_common_time_write().upsert(U64BE(5), 5)?;
/*let received = db_reader.get_txs_history_bc_received( /*let received = db_reader.get_txs_history_bc_received(
PageInfo { PageInfo {
...@@ -581,6 +662,7 @@ mod tests { ...@@ -581,6 +662,7 @@ mod tests {
assert!(!received.has_previous_page);*/ assert!(!received.has_previous_page);*/
let received = db_reader.get_txs_history_bc_received( let received = db_reader.get_txs_history_bc_received(
None,
PageInfo { PageInfo {
order: false, order: false,
limit_opt: None, limit_opt: None,
...@@ -590,6 +672,7 @@ mod tests { ...@@ -590,6 +672,7 @@ mod tests {
}), }),
}, },
s1_hash, s1_hash,
None,
)?; )?;
assert_eq!( assert_eq!(
received received
...@@ -608,6 +691,27 @@ mod tests { ...@@ -608,6 +691,27 @@ mod tests {
assert!(!received.has_next_page); assert!(!received.has_next_page);
assert!(!received.has_previous_page); assert!(!received.has_previous_page);
let received = db_reader.get_txs_history_bc_received(
Some(2),
PageInfo {
order: true,
limit_opt: None,
pos: None,
},
s1_hash,
Some(5),
)?;
assert_eq!(
received
.data
.into_iter()
.map(|tx_db| tx_db.tx.get_hash())
.collect::<Vec<_>>(),
vec![Hash([4; 32]), Hash([5; 32]), Hash([6; 32]), Hash([7; 32])],
);
assert!(!received.has_next_page);
assert!(!received.has_previous_page);
Ok(()) Ok(())
} }
...@@ -653,6 +757,7 @@ mod tests { ...@@ -653,6 +757,7 @@ mod tests {
)?; )?;
let sent = db_reader.get_txs_history_bc_sent( let sent = db_reader.get_txs_history_bc_sent(
None,
PageInfo { PageInfo {
order: true, order: true,
limit_opt: None, limit_opt: None,
...@@ -662,6 +767,7 @@ mod tests { ...@@ -662,6 +767,7 @@ mod tests {
}), }),
}, },
s1_hash, s1_hash,
None,
)?; )?;
assert_eq!( assert_eq!(
sent.data sent.data
...@@ -674,6 +780,7 @@ mod tests { ...@@ -674,6 +780,7 @@ mod tests {
assert!(!sent.has_previous_page); assert!(!sent.has_previous_page);
let sent = db_reader.get_txs_history_bc_sent( let sent = db_reader.get_txs_history_bc_sent(
None,
PageInfo { PageInfo {
order: false, order: false,
limit_opt: None, limit_opt: None,
...@@ -683,6 +790,7 @@ mod tests { ...@@ -683,6 +790,7 @@ mod tests {
}), }),
}, },
s1_hash, s1_hash,
None,
)?; )?;
assert_eq!( assert_eq!(
sent.data sent.data
...@@ -695,6 +803,7 @@ mod tests { ...@@ -695,6 +803,7 @@ mod tests {
assert!(!sent.has_previous_page); assert!(!sent.has_previous_page);
let sent = db_reader.get_txs_history_bc_sent( let sent = db_reader.get_txs_history_bc_sent(
None,
PageInfo { PageInfo {
order: false, order: false,
limit_opt: None, limit_opt: None,
...@@ -704,6 +813,7 @@ mod tests { ...@@ -704,6 +813,7 @@ mod tests {
}), }),
}, },
s1_hash, s1_hash,
None,
)?; )?;
assert_eq!( assert_eq!(
sent.data sent.data
......
...@@ -15,6 +15,12 @@ ...@@ -15,6 +15,12 @@
use crate::*; use crate::*;
#[derive(async_graphql::InputObject, Clone, Copy, Default)]
pub(crate) struct TimeInterval {
pub(crate) from: Option<u64>,
pub(crate) to: Option<u64>,
}
#[derive(async_graphql::InputObject)] #[derive(async_graphql::InputObject)]
pub(crate) struct TxIssuer { pub(crate) struct TxIssuer {
/// Account script (default is a script needed all provided signers) /// Account script (default is a script needed all provided signers)
......
...@@ -150,13 +150,17 @@ mod tests { ...@@ -150,13 +150,17 @@ mod tests {
) -> KvResult<Option<SourceAmount>>; ) -> KvResult<Option<SourceAmount>>;
fn get_txs_history_bc_received( fn get_txs_history_bc_received(
&self, &self,
from: Option<u64>,
page_info: PageInfo<duniter_gva_dbs_reader::txs_history::TxBcCursor>, page_info: PageInfo<duniter_gva_dbs_reader::txs_history::TxBcCursor>,
script_hash: Hash, script_hash: Hash,
to: Option<u64>,
) -> KvResult<PagedData<VecDeque<duniter_gva_db::GvaTxDbV1>>>; ) -> KvResult<PagedData<VecDeque<duniter_gva_db::GvaTxDbV1>>>;
fn get_txs_history_bc_sent( fn get_txs_history_bc_sent(
&self, &self,
from: Option<u64>,
page_info: PageInfo<duniter_gva_dbs_reader::txs_history::TxBcCursor>, page_info: PageInfo<duniter_gva_dbs_reader::txs_history::TxBcCursor>,
script_hash: Hash, script_hash: Hash,
to: Option<u64>,
) -> KvResult<PagedData<VecDeque<duniter_gva_db::GvaTxDbV1>>>; ) -> KvResult<PagedData<VecDeque<duniter_gva_db::GvaTxDbV1>>>;
fn get_txs_history_mempool<TxsMpDb: 'static + TxsMpV2DbReadable>( fn get_txs_history_mempool<TxsMpDb: 'static + TxsMpV2DbReadable>(
&self, &self,
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::inputs::TimeInterval;
use crate::*; use crate::*;
use duniter_gva_db::GvaTxDbV1; use duniter_gva_db::GvaTxDbV1;
use duniter_gva_dbs_reader::txs_history::TxBcCursor; use duniter_gva_dbs_reader::txs_history::TxBcCursor;
...@@ -29,6 +30,7 @@ impl TxsHistoryBlockchainQuery { ...@@ -29,6 +30,7 @@ impl TxsHistoryBlockchainQuery {
ctx: &async_graphql::Context<'_>, ctx: &async_graphql::Context<'_>,
#[graphql(desc = "pagination", default)] pagination: Pagination, #[graphql(desc = "pagination", default)] pagination: Pagination,
script: PkOrScriptGva, script: PkOrScriptGva,
#[graphql(default)] time_interval: TimeInterval,
) -> async_graphql::Result<TxsHistoryBlockchainQueryInner> { ) -> async_graphql::Result<TxsHistoryBlockchainQueryInner> {
let QueryContext { is_whitelisted } = ctx.data::<QueryContext>()?; let QueryContext { is_whitelisted } = ctx.data::<QueryContext>()?;
let pagination = Pagination::convert_to_page_info(pagination, *is_whitelisted)?; let pagination = Pagination::convert_to_page_info(pagination, *is_whitelisted)?;
...@@ -36,6 +38,7 @@ impl TxsHistoryBlockchainQuery { ...@@ -36,6 +38,7 @@ impl TxsHistoryBlockchainQuery {
Ok(TxsHistoryBlockchainQueryInner { Ok(TxsHistoryBlockchainQueryInner {
pagination, pagination,
script_hash, script_hash,
time_interval,
}) })
} }
} }
...@@ -43,6 +46,7 @@ impl TxsHistoryBlockchainQuery { ...@@ -43,6 +46,7 @@ impl TxsHistoryBlockchainQuery {
pub(crate) struct TxsHistoryBlockchainQueryInner { pub(crate) struct TxsHistoryBlockchainQueryInner {
pub(crate) pagination: PageInfo<TxBcCursor>, pub(crate) pagination: PageInfo<TxBcCursor>,
pub(crate) script_hash: Hash, pub(crate) script_hash: Hash,
pub(crate) time_interval: TimeInterval,
} }
#[async_graphql::Object] #[async_graphql::Object]
...@@ -59,14 +63,25 @@ impl TxsHistoryBlockchainQueryInner { ...@@ -59,14 +63,25 @@ impl TxsHistoryBlockchainQueryInner {
let db_reader = data.dbs_reader(); let db_reader = data.dbs_reader();
let pagination = self.pagination; let pagination = self.pagination;
let script_hash = self.script_hash; let script_hash = self.script_hash;
let sent_fut = data let time_interval = self.time_interval;
.dbs_pool let sent_fut = data.dbs_pool.execute(move |_| {
.execute(move |_| db_reader.get_txs_history_bc_sent(pagination, script_hash)); db_reader.get_txs_history_bc_sent(
time_interval.from,
pagination,
script_hash,
time_interval.to,
)
});
let db_reader = data.dbs_reader(); let db_reader = data.dbs_reader();
let script_hash = self.script_hash; let script_hash = self.script_hash;
let received_fut = data let received_fut = data.dbs_pool.execute(move |_| {
.dbs_pool db_reader.get_txs_history_bc_received(
.execute(move |_| db_reader.get_txs_history_bc_received(pagination, script_hash)); time_interval.from,
pagination,
script_hash,
time_interval.to,
)
});
let (sent_res, received_res) = join(sent_fut, received_fut).await; let (sent_res, received_res) = join(sent_fut, received_fut).await;
let (sent, received) = (sent_res??, received_res??); let (sent, received) = (sent_res??, received_res??);
...@@ -136,9 +151,17 @@ impl TxsHistoryBlockchainQueryInner { ...@@ -136,9 +151,17 @@ impl TxsHistoryBlockchainQueryInner {
let db_reader = data.dbs_reader(); let db_reader = data.dbs_reader();
let pagination = self.pagination; let pagination = self.pagination;
let script_hash = self.script_hash; let script_hash = self.script_hash;
let time_interval = self.time_interval;
let received = data let received = data
.dbs_pool .dbs_pool
.execute(move |_| db_reader.get_txs_history_bc_received(pagination, script_hash)) .execute(move |_| {
db_reader.get_txs_history_bc_received(
time_interval.from,
pagination,
script_hash,
time_interval.to,
)
})
.await??; .await??;
let mut conn = Connection::new(received.has_previous_page, received.has_next_page); let mut conn = Connection::new(received.has_previous_page, received.has_next_page);
conn.append(received.data.into_iter().map(|db_tx| { conn.append(received.data.into_iter().map(|db_tx| {
...@@ -163,9 +186,17 @@ impl TxsHistoryBlockchainQueryInner { ...@@ -163,9 +186,17 @@ impl TxsHistoryBlockchainQueryInner {
let db_reader = data.dbs_reader(); let db_reader = data.dbs_reader();
let pagination = self.pagination; let pagination = self.pagination;
let script_hash = self.script_hash; let script_hash = self.script_hash;
let time_interval = self.time_interval;
let sent = data let sent = data
.dbs_pool .dbs_pool
.execute(move |_| db_reader.get_txs_history_bc_sent(pagination, script_hash)) .execute(move |_| {
db_reader.get_txs_history_bc_sent(
time_interval.from,
pagination,
script_hash,
time_interval.to,
)
})
.await??; .await??;
let mut conn = Connection::new(sent.has_previous_page, sent.has_next_page); let mut conn = Connection::new(sent.has_previous_page, sent.has_next_page);
conn.append(sent.data.into_iter().map(|db_tx| { conn.append(sent.data.into_iter().map(|db_tx| {
...@@ -232,11 +263,11 @@ mod tests { ...@@ -232,11 +263,11 @@ mod tests {
dbs_reader dbs_reader
.expect_get_txs_history_bc_received() .expect_get_txs_history_bc_received()
.times(1) .times(1)
.returning(|_, _| Ok(PagedData::empty())); .returning(|_, _, _, _| Ok(PagedData::empty()));
dbs_reader dbs_reader
.expect_get_txs_history_bc_sent() .expect_get_txs_history_bc_sent()
.times(1) .times(1)
.returning(|_, _| { .returning(|_, _, _, _| {
let tx = TransactionDocumentV10::from_string_object( let tx = TransactionDocumentV10::from_string_object(
&TransactionDocumentV10Stringified { &TransactionDocumentV10Stringified {
currency: "test".to_owned(), currency: "test".to_owned(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment