Skip to content

Commit 7aacc02

Browse files
committed
rebased ocean-archive
1 parent 0e7efd8 commit 7aacc02

16 files changed

Lines changed: 408 additions & 412 deletions

lib/Cargo.lock

Lines changed: 151 additions & 142 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ain-ocean/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ tarpc = { version = "0.33", features = ["tokio1", "serde1", "serde-transport","t
1919
tokio = { version = "1", features = ["full"] }
2020
serde_json = "1.0"
2121
json = "0.12.4"
22-
tokio-serde = {version="0.8.0", features = ["full"]}
22+
tokio-serde = {version="0.8.0"}
2323
futures = "0.3.29"
2424
jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] }
2525
rocksdb = "0.21.0"
@@ -28,3 +28,4 @@ tempdir = "0.3.7"
2828
bitcoin_hashes = "0.12.0"
2929
structopt = { version = "0.3", default-features = false }
3030
tempfile = "3.8.1"
31+
anyhow.workspace = true

lib/ain-ocean/src/api/block.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use axum::{debug_handler, extract::{Path, Query}, Json, routing::get, Router};
1+
use axum::{
2+
debug_handler,
3+
extract::{Path, Query},
4+
routing::get,
5+
Json, Router,
6+
};
27
use serde::{Deserialize, Serialize};
38

49
use crate::api_paged_response::ApiPagedResponse;
5-
use axum::{extract::Path, routing::get, Router};
6-
use serde::Deserialize;
710

811
#[derive(Deserialize)]
912
struct BlockId {
@@ -18,7 +21,7 @@ struct BlockHash {
1821
#[derive(Deserialize)]
1922
pub struct ListBlocksQuery {
2023
pub size: usize,
21-
pub next: Option<String>
24+
pub next: Option<String>,
2225
}
2326

2427
#[debug_handler]
@@ -31,15 +34,9 @@ async fn list_blocks(Query(query): Query<ListBlocksQuery>) -> Json<ApiPagedRespo
3134
Block { id: "2".into() },
3235
];
3336

34-
Json(ApiPagedResponse
35-
::of(
36-
blocks,
37-
query.size,
38-
|block| block.clone().id
39-
)
40-
)
41-
async fn list_blocks() -> String {
42-
"List of blocks".to_string()
37+
Json(ApiPagedResponse::of(blocks, query.size, |block| {
38+
block.clone().id
39+
}))
4340
}
4441

4542
async fn get_block(Path(BlockId { id }): Path<BlockId>) -> String {

lib/ain-ocean/src/api_paged_response.rs

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ use serde::Serialize;
55
/// Each ApiPagedResponse holds the data array and the "token" for next part of the slice.
66
/// The next token should be passed via query 'next' and only used when getting the next slice.
77
/// Hence the first request, the next token is always empty and not provided.
8-
///
8+
///
99
/// With ascending sorted list and a limit of 3 items per slice will have the behaviour as such.
10-
///
10+
///
1111
/// SORTED : | [1] [2] [3] | [4] [5] [6] | [7] [8] [9] | [10]
1212
/// Query 1 : Data: [1] [2] [3], Next: 3, Operator: GT (>)
1313
/// Query 2 : Data: [4] [5] [6], Next: 6, Operator: GT (>)
1414
/// Query 3 : Data: [7] [8] [9], Next: 3, Operator: GT (>)
1515
/// Query 4 : Data: [10], Next: undefined
16-
///
16+
///
1717
/// This design is resilient also mutating sorted list, where pagination is not.
18-
///
18+
///
1919
/// SORTED : [2] [4] [6] [8] [10] [12] [14]
2020
/// Query 1 : Data: [2] [4] [6], Next: 6, Operator: GT (>)
2121
///
@@ -27,7 +27,7 @@ use serde::Serialize;
2727
/// Limitations of this requires your dat astructure to always be sorted in one direction and your sort
2828
/// indexes always fixed. Hence the moving down of the slice window, your operator will be greater than (GT).
2929
/// While moving up your operator will be less than (GT).
30-
///
30+
///
3131
/// ASC : | [1] [2] [3] | [4] [5] [6] | [7] [8] [9] |
3232
/// >3 >6 >9
3333
/// DESC : | [9] [8] [7] | [6] [5] [4] | [3] [2] [1] |
@@ -37,15 +37,15 @@ use serde::Serialize;
3737
/// when the usage narrative is clear and so will the use of ease. LIST query must be dead simple.
3838
/// Image travelling down the path, and getting a "next token" to get the next set of itmes to
3939
/// continue walking.
40-
///
40+
///
4141
/// Because the limit is not part of the slice window your query mechanism should support varying size windows.
42-
///
42+
///
4343
/// DATA: | [1] [2] [3] | [4] [5] [6] [7] | [8] [9] | ...
4444
/// | limit 3, >3 | limit 4, >7 | limit 2, >9
4545
/// For simplicity your API should not attempt to allow access to different sort indexes, be cognizant of
4646
/// how our APIs are consumed. If we create a GET /blocks operation to list blocks what would the correct indexes
4747
/// be 99% of the time?
48-
///
48+
///
4949
/// Answer: Blocks sorted by height in descending order, that's your sorted list and your slice window.
5050
/// : <- Latest | [100] [99] [98] [97] [...] | Oldest ->
5151
///
@@ -62,7 +62,12 @@ struct ApiPage {
6262

6363
impl<T> ApiPagedResponse<T> {
6464
pub fn new(data: Vec<T>, next: Option<&str>) -> Self {
65-
Self { data, page: ApiPage{ next: next.map(Into::into) } } // Option<&str> -> Option<String>
65+
Self {
66+
data,
67+
page: ApiPage {
68+
next: next.map(Into::into),
69+
},
70+
} // Option<&str> -> Option<String>
6671
}
6772

6873
pub fn next(data: Vec<T>, next: Option<&str>) -> Self {
@@ -77,22 +82,22 @@ impl<T> ApiPagedResponse<T> {
7782
Self::next(data, None)
7883
}
7984
}
80-
85+
8186
pub fn empty() -> Self {
8287
Self::new(Vec::new(), None)
8388
}
8489
}
8590

8691
#[cfg(test)]
8792
mod tests {
88-
use super::{ApiPagedResponse, ApiPage};
89-
93+
use super::{ApiPage, ApiPagedResponse};
94+
9095
#[derive(Clone, Debug)]
9196
struct Item {
9297
id: String,
9398
sort: String,
9499
}
95-
100+
96101
impl Item {
97102
fn new(id: &str, sort: &str) -> Self {
98103
Self {
@@ -101,25 +106,19 @@ mod tests {
101106
}
102107
}
103108
}
104-
109+
105110
#[test]
106111
fn should_next_with_none() {
107-
let items: Vec<Item> = vec![
108-
Item::new("0", "a"),
109-
Item::new("1", "b"),
110-
];
111-
112+
let items: Vec<Item> = vec![Item::new("0", "a"), Item::new("1", "b")];
113+
112114
let next = ApiPagedResponse::next(items, None).page.next;
113115
assert_eq!(next, None);
114116
}
115117

116118
#[test]
117119
fn should_next_with_value() {
118-
let items: Vec<Item> = vec![
119-
Item::new("0", "a"),
120-
Item::new("1", "b"),
121-
];
122-
120+
let items: Vec<Item> = vec![Item::new("0", "a"), Item::new("1", "b")];
121+
123122
let next = ApiPagedResponse::next(items, Some("b")).page.next;
124123
assert_eq!(next, Some("b".into()));
125124
}
@@ -131,20 +130,18 @@ mod tests {
131130
Item::new("1", "b"),
132131
Item::new("2", "c"),
133132
];
134-
135-
let next = ApiPagedResponse::of(items, 3, |item| item.clone().sort).page.next;
133+
134+
let next = ApiPagedResponse::of(items, 3, |item| item.clone().sort)
135+
.page
136+
.next;
136137
assert_eq!(next, Some("c".into()))
137138
}
138-
139+
139140
#[test]
140141
fn should_not_create_with_limit_3_while_size_2() {
141-
let items: Vec<Item> = vec![
142-
Item::new("0", "a"),
143-
Item::new("1", "b"),
144-
];
145-
142+
let items: Vec<Item> = vec![Item::new("0", "a"), Item::new("1", "b")];
143+
146144
let page = ApiPagedResponse::of(items, 3, |item| item.clone().sort).page;
147-
assert_eq!(page, ApiPage{next: None})
145+
assert_eq!(page, ApiPage { next: None })
148146
}
149-
150147
}

lib/ain-ocean/src/data_acces/masternode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::database::db_manger::RocksDB;
33
use crate::model::masternode::Masternode;
44
use anyhow::{anyhow, Result};
55
use serde::{Deserialize, Serialize};
6+
use serde_json;
67

78
pub struct MasterNodeDB {
89
pub db: RocksDB,

0 commit comments

Comments
 (0)