Skip to content

Commit 795edad

Browse files
committed
Ocean API: rocks db implementation for ocean API (#2728)
* Added `rockdb` to ocean api. * Implementation of ocean-api database method all module. * Added data_access layer * Added test case for module.
1 parent ae48ea3 commit 795edad

62 files changed

Lines changed: 2058 additions & 190 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/Cargo.lock

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

lib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ jsonrpsee-server = "0.16"
7979
jsonrpsee-types = "0.16"
8080

8181
axum = { version = "0.7.1", features = ["macros"] }
82-
8382
tempdir = "0.3"
8483

8584
rocksdb = { version = "0.21", default-features = false }

lib/Makefile.am

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ check:
7171
test:
7272
$(CARGO) test $(CARGO_MANIFEST_ARG) $(CARGO_EXTRA_ARGS)
7373

74-
.PHONY: clippy
75-
clippy:
76-
$(CARGO) clippy $(CARGO_MANIFEST_ARG) $(CARGO_EXTRA_ARGS) -- -Dwarnings
77-
7874
.PHONY: doc
7975
doc:
8076
$(CARGO) doc $(CARGO_MANIFEST_ARG)

lib/ain-evm/src/storage/cache.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,30 @@ use std::{borrow::ToOwned, num::NonZeroUsize, sync::RwLock};
33
use ethereum::{BlockAny, TransactionV2};
44
use ethereum_types::{H256, U256};
55
use lru::LruCache;
6-
use parking_lot::Mutex;
76

87
use super::traits::{BlockStorage, Rollback, TransactionStorage};
98
use crate::Result;
109

1110
#[derive(Debug)]
1211
pub struct Cache {
13-
transactions: Mutex<LruCache<H256, TransactionV2>>,
14-
blocks: Mutex<LruCache<U256, BlockAny>>,
15-
block_hashes: Mutex<LruCache<H256, U256>>,
12+
transactions: RwLock<LruCache<H256, TransactionV2>>,
13+
blocks: RwLock<LruCache<U256, BlockAny>>,
14+
block_hashes: RwLock<LruCache<H256, U256>>,
1615
latest_block: RwLock<Option<BlockAny>>,
17-
contract_code: Mutex<LruCache<H256, Vec<u8>>>,
1816
}
1917

2018
impl Cache {
2119
const DEFAULT_CACHE_SIZE: usize = 1000;
2220

2321
pub fn new(cache_size: Option<usize>) -> Self {
2422
Cache {
25-
transactions: Mutex::new(LruCache::new(
23+
transactions: RwLock::new(LruCache::new(
2624
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(),
2725
)),
28-
blocks: Mutex::new(LruCache::new(
26+
blocks: RwLock::new(LruCache::new(
2927
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(),
3028
)),
31-
block_hashes: Mutex::new(LruCache::new(
32-
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(),
33-
)),
34-
contract_code: Mutex::new(LruCache::new(
29+
block_hashes: RwLock::new(LruCache::new(
3530
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(),
3631
)),
3732
latest_block: RwLock::new(None),
@@ -41,13 +36,19 @@ impl Cache {
4136

4237
impl BlockStorage for Cache {
4338
fn get_block_by_number(&self, number: &U256) -> Result<Option<BlockAny>> {
44-
let block = self.blocks.lock().get(number).map(ToOwned::to_owned);
39+
let block = self
40+
.blocks
41+
.write()
42+
.unwrap()
43+
.get(number)
44+
.map(ToOwned::to_owned);
4545
Ok(block)
4646
}
4747

4848
fn get_block_by_hash(&self, block_hash: &H256) -> Result<Option<BlockAny>> {
4949
self.block_hashes
50-
.lock()
50+
.write()
51+
.unwrap()
5152
.get(block_hash)
5253
.map_or(Ok(None), |block_number| {
5354
self.get_block_by_number(block_number)
@@ -59,8 +60,11 @@ impl BlockStorage for Cache {
5960

6061
let block_number = block.header.number;
6162
let hash = block.header.hash();
62-
self.blocks.lock().put(block_number, block.clone());
63-
self.block_hashes.lock().put(hash, block_number);
63+
self.blocks
64+
.write()
65+
.unwrap()
66+
.put(block_number, block.clone());
67+
self.block_hashes.write().unwrap().put(hash, block_number);
6468
Ok(())
6569
}
6670

@@ -83,7 +87,7 @@ impl BlockStorage for Cache {
8387

8488
impl TransactionStorage for Cache {
8589
fn extend_transactions_from_block(&self, block: &BlockAny) -> Result<()> {
86-
let mut cache = self.transactions.lock();
90+
let mut cache = self.transactions.write().unwrap();
8791

8892
for transaction in &block.transactions {
8993
let hash = transaction.hash();
@@ -93,7 +97,12 @@ impl TransactionStorage for Cache {
9397
}
9498

9599
fn get_transaction_by_hash(&self, hash: &H256) -> Result<Option<TransactionV2>> {
96-
let transaction = self.transactions.lock().get(hash).map(ToOwned::to_owned);
100+
let transaction = self
101+
.transactions
102+
.write()
103+
.unwrap()
104+
.get(hash)
105+
.map(ToOwned::to_owned);
97106
Ok(transaction)
98107
}
99108

@@ -103,7 +112,8 @@ impl TransactionStorage for Cache {
103112
index: usize,
104113
) -> Result<Option<TransactionV2>> {
105114
self.block_hashes
106-
.lock()
115+
.write()
116+
.unwrap()
107117
.get(block_hash)
108118
.map_or(Ok(None), |block_number| {
109119
self.get_transaction_by_block_number_and_index(block_number, index)
@@ -117,15 +127,17 @@ impl TransactionStorage for Cache {
117127
) -> Result<Option<TransactionV2>> {
118128
let transaction = self
119129
.blocks
120-
.lock()
130+
.write()
131+
.unwrap()
121132
.get(block_number)
122133
.and_then(|block| block.transactions.get(index).map(ToOwned::to_owned));
123134
Ok(transaction)
124135
}
125136

126137
fn put_transaction(&self, transaction: &TransactionV2) -> Result<()> {
127138
self.transactions
128-
.lock()
139+
.write()
140+
.unwrap()
129141
.put(transaction.hash(), transaction.clone());
130142
Ok(())
131143
}
@@ -134,28 +146,17 @@ impl TransactionStorage for Cache {
134146
impl Rollback for Cache {
135147
fn disconnect_latest_block(&self) -> Result<()> {
136148
if let Some(block) = self.get_latest_block()? {
137-
let mut transaction_cache = self.transactions.lock();
149+
let mut transaction_cache = self.transactions.write().unwrap();
138150
for tx in &block.transactions {
139151
transaction_cache.pop(&tx.hash());
140152
}
141153

142-
self.block_hashes.lock().pop(&block.header.hash());
143-
self.blocks.lock().pop(&block.header.number);
154+
self.block_hashes.write().unwrap().pop(&block.header.hash());
155+
self.blocks.write().unwrap().pop(&block.header.number);
144156

145157
let previous_block = self.get_block_by_hash(&block.header.parent_hash)?;
146158
self.put_latest_block(previous_block.as_ref())?;
147159
}
148160
Ok(())
149161
}
150162
}
151-
152-
impl Cache {
153-
pub fn get_code_by_hash(&self, hash: &H256) -> Result<Option<Vec<u8>>> {
154-
Ok(self.contract_code.lock().get(hash).map(ToOwned::to_owned))
155-
}
156-
157-
pub fn put_code(&self, hash: H256, code: &[u8]) -> Result<()> {
158-
self.contract_code.lock().put(hash, code.to_vec());
159-
Ok(())
160-
}
161-
}

lib/ain-evm/src/storage/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,7 @@ impl FlushableStorage for Storage {
193193

194194
impl Storage {
195195
pub fn get_code_by_hash(&self, address: H160, hash: H256) -> Result<Option<Vec<u8>>> {
196-
match self.cache.get_code_by_hash(&hash) {
197-
Ok(Some(code)) => Ok(Some(code)),
198-
Ok(None) => {
199-
let code = self.blockstore.get_code_by_hash(address, &hash);
200-
if let Ok(Some(ref code)) = code {
201-
self.cache.put_code(hash, code)?;
202-
}
203-
code
204-
}
205-
Err(e) => Err(e),
206-
}
196+
self.blockstore.get_code_by_hash(address, &hash)
207197
}
208198

209199
pub fn put_code(

lib/ain-ocean/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ thiserror.workspace = true
1515
hex.workspace = true
1616
dftx-rs.workspace = true
1717
bitcoin.workspace = true
18+
tarpc = { version = "0.33", features = ["tokio1", "serde1", "serde-transport","tcp"] }
19+
tokio = { version = "1", features = ["full"] }
20+
serde_json = "1.0"
21+
json = "0.12.4"
22+
tokio-serde = {version="0.8.0"}
23+
futures = "0.3.29"
24+
jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] }
25+
rocksdb = "0.21.0"
26+
ctrlc = "3.1.9"
27+
tempdir = "0.3.7"
28+
bitcoin_hashes = "0.12.0"
29+
structopt = { version = "0.3", default-features = false }
30+
tempfile = "3.8.1"
31+
anyhow.workspace = true

lib/ain-ocean/README.md

Whitespace-only changes.

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
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;
@@ -16,7 +21,7 @@ struct BlockHash {
1621
#[derive(Deserialize)]
1722
pub struct ListBlocksQuery {
1823
pub size: usize,
19-
pub next: Option<String>
24+
pub next: Option<String>,
2025
}
2126

2227
#[debug_handler]
@@ -28,14 +33,10 @@ async fn list_blocks(Query(query): Query<ListBlocksQuery>) -> Json<ApiPagedRespo
2833
Block { id: "1".into() },
2934
Block { id: "2".into() },
3035
];
31-
32-
Json(ApiPagedResponse
33-
::of(
34-
blocks,
35-
query.size,
36-
|block| block.clone().id
37-
)
38-
)
36+
37+
Json(ApiPagedResponse::of(blocks, query.size, |block| {
38+
block.clone().id
39+
}))
3940
}
4041

4142
async fn get_block(Path(BlockId { id }): Path<BlockId>) -> String {
@@ -60,24 +61,24 @@ pub struct Block {
6061
// TODO(): type mapping
6162
// hash: H256,
6263
// previous_hash: H256,
63-
64+
6465
// height: u64,
6566
// version: u64,
6667
// time: u64, // ---------------| block time in seconds since epoch
6768
// median_time: u64, // --------| meidan time of the past 11 block timestamps
68-
69+
6970
// transaction_count: u64,
7071

7172
// difficulty: u64,
72-
73+
7374
// masternode: H256,
7475
// minter: H256,
7576
// minter_block_count: u64,
7677
// reward: f64
77-
78+
7879
// state_modifier: H256,
7980
// merkle_root: H256,
80-
81+
8182
// size: u64,
8283
// size_stripped: u64,
8384
// weight: u64,

0 commit comments

Comments
 (0)