Skip to content

Commit d020ad9

Browse files
authored
feat: add get mutation header (#474)
* feat: add get mutation header * fix: improve the var name * feat: add scan and get mutation * feat: add scan mutation headers
1 parent 5c0d765 commit d020ad9

22 files changed

+717
-434
lines changed

bridge/contracts/DB3Rollup.sol

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ contract DB3Rollup {
88
// the locked balance of an address
99
mapping(address => uint256) private _balances;
1010
event Deposit(address _from, uint256 amount);
11+
1112
constructor(Db3Token tokenContract) {
1213
_tokenContract = tokenContract;
1314
}

format.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /bin/bash
22

33
cargo fmt
4+
npx buf format -w src/proto/proto
45
cd bridge && npx prettier --write 'contracts/**/*.sol'
56

67

src/base/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717

1818
pub mod bson_util;
1919
pub mod strings;
20+
pub mod times;

src/base/src/times.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// times.rs
3+
// Copyright (C) 2023 db3.network Author imotai <[email protected]>
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
//
17+
18+
use std::time::SystemTime;
19+
20+
pub fn get_current_time_in_secs() -> u64 {
21+
match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
22+
Ok(n) => n.as_secs(),
23+
Err(_) => 0,
24+
}
25+
}

src/crypto/src/id.rs

+13
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ impl TxId {
8585
Self::try_from_bytes(base64ct::Base64::decode_vec(input).unwrap().as_slice())
8686
}
8787

88+
pub fn try_from_hex(input: &str) -> std::result::Result<Self, DB3Error> {
89+
if input.starts_with("0x") {
90+
let new_input = &input[2..];
91+
let data = hex::decode(new_input)
92+
.map_err(|e| DB3Error::KeyCodecError(format!("fail to decode tx id for {e}")))?;
93+
Self::try_from_bytes(data.as_slice())
94+
} else {
95+
let data = hex::decode(input)
96+
.map_err(|e| DB3Error::KeyCodecError(format!("fail to decode tx id for {e}")))?;
97+
Self::try_from_bytes(data.as_slice())
98+
}
99+
}
100+
88101
pub fn try_from_bytes(data: &[u8]) -> std::result::Result<Self, DB3Error> {
89102
let arr: [u8; TX_ID_LENGTH] = data.try_into().map_err(|_| DB3Error::InvalidAddress)?;
90103
Ok(Self { data: arr })

src/node/src/command.rs

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ impl DB3Command {
413413
block_store_cf_name: "block_store_cf".to_string(),
414414
tx_store_cf_name: "tx_store_cf".to_string(),
415415
message_max_buffer: 4 * 1024,
416+
scan_max_limit: 50,
416417
};
417418
let state_config = StateStoreConfig {
418419
db_path: state_db_path.to_string(),

src/node/src/storage_node_light_impl.rs

+56-7
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
use crate::mutation_utils::MutationUtil;
1919
use db3_crypto::db3_address::DB3Address;
20-
use db3_crypto::id::DbId;
20+
use db3_crypto::id::{DbId, TxId};
2121
use db3_error::Result;
22+
use db3_proto::db3_mutation_v2_proto::{MutationAction, MutationRollupStatus};
2223
use db3_proto::db3_storage_proto::{
23-
storage_node_server::StorageNode, ExtraItem, GetNonceRequest, GetNonceResponse,
24-
SendMutationRequest, SendMutationResponse,
24+
storage_node_server::StorageNode, ExtraItem, GetMutationBodyRequest, GetMutationBodyResponse,
25+
GetMutationHeaderRequest, GetMutationHeaderResponse, GetNonceRequest, GetNonceResponse,
26+
ScanMutationHeaderRequest, ScanMutationHeaderResponse, SendMutationRequest,
27+
SendMutationResponse,
2528
};
26-
27-
use db3_proto::db3_mutation_v2_proto::MutationAction;
2829
use db3_storage::mutation_store::{MutationStore, MutationStoreConfig};
2930
use db3_storage::state_store::{StateStore, StateStoreConfig};
3031
use tonic::{Request, Response, Status};
@@ -56,6 +57,47 @@ impl StorageNodeV2Impl {
5657

5758
#[tonic::async_trait]
5859
impl StorageNode for StorageNodeV2Impl {
60+
async fn get_mutation_body(
61+
&self,
62+
request: Request<GetMutationBodyRequest>,
63+
) -> std::result::Result<Response<GetMutationBodyResponse>, Status> {
64+
let r = request.into_inner();
65+
let tx_id =
66+
TxId::try_from_hex(r.id.as_str()).map_err(|e| Status::internal(format!("{e}")))?;
67+
let body = self
68+
.storage
69+
.get_mutation(&tx_id)
70+
.map_err(|e| Status::internal(format!("{e}")))?;
71+
Ok(Response::new(GetMutationBodyResponse { body }))
72+
}
73+
74+
async fn scan_mutation_header(
75+
&self,
76+
request: Request<ScanMutationHeaderRequest>,
77+
) -> std::result::Result<Response<ScanMutationHeaderResponse>, Status> {
78+
let r = request.into_inner();
79+
let headers = self
80+
.storage
81+
.scan_mutation_headers(r.start, r.limit)
82+
.map_err(|e| Status::internal(format!("{e}")))?;
83+
Ok(Response::new(ScanMutationHeaderResponse { headers }))
84+
}
85+
86+
async fn get_mutation_header(
87+
&self,
88+
request: Request<GetMutationHeaderRequest>,
89+
) -> std::result::Result<Response<GetMutationHeaderResponse>, Status> {
90+
let r = request.into_inner();
91+
let header = self
92+
.storage
93+
.get_mutation_header(r.block_id, r.order_id)
94+
.map_err(|e| Status::internal(format!("{e}")))?;
95+
Ok(Response::new(GetMutationHeaderResponse {
96+
header,
97+
status: MutationRollupStatus::Pending.into(),
98+
rollup_tx: vec![],
99+
}))
100+
}
59101
async fn get_nonce(
60102
&self,
61103
request: Request<GetNonceRequest>,
@@ -88,9 +130,9 @@ impl StorageNode for StorageNodeV2Impl {
88130
match self.state_store.incr_nonce(&address, nonce) {
89131
Ok(_) => {
90132
// mutation id
91-
let id = self
133+
let (id, block, order) = self
92134
.storage
93-
.add_mutation(&r.payload, r.signature.as_bytes(), &address)
135+
.add_mutation(&r.payload, r.signature.as_str(), &address)
94136
.map_err(|e| Status::internal(format!("{e}")))?;
95137
match action {
96138
MutationAction::CreateDocumentDb => {
@@ -100,18 +142,23 @@ impl StorageNode for StorageNodeV2Impl {
100142
key: "db_addr".to_string(),
101143
value: db_addr,
102144
};
145+
103146
Ok(Response::new(SendMutationResponse {
104147
id,
105148
code: 0,
106149
msg: "ok".to_string(),
107150
items: vec![item],
151+
block,
152+
order,
108153
}))
109154
}
110155
_ => Ok(Response::new(SendMutationResponse {
111156
id,
112157
code: 0,
113158
msg: "ok".to_string(),
114159
items: vec![],
160+
block,
161+
order,
115162
})),
116163
}
117164
}
@@ -120,6 +167,8 @@ impl StorageNode for StorageNodeV2Impl {
120167
code: 1,
121168
msg: "bad nonce".to_string(),
122169
items: vec![],
170+
block: 0,
171+
order: 0,
123172
})),
124173
}
125174
}

src/proto/proto/db3_account.proto

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ package db3_account_proto;
2222
//
2323
//
2424
message Account {
25-
uint64 bills = 1;
26-
uint64 credits = 2;
27-
uint64 total_storage_in_bytes = 3;
28-
uint64 total_mutation_count = 4;
29-
uint64 total_session_count = 5;
30-
uint64 nonce = 6;
25+
uint64 bills = 1;
26+
uint64 credits = 2;
27+
uint64 total_storage_in_bytes = 3;
28+
uint64 total_mutation_count = 4;
29+
uint64 total_session_count = 5;
30+
uint64 nonce = 6;
3131
}
32-
33-

src/proto/proto/db3_account_v2.proto

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
// limitations under the License.
1616
//
1717

18-
1918
syntax = "proto3";
2019

2120
package db3_account_v2_proto;
2221

2322
message Account {
24-
uint64 nonce = 1;
25-
uint64 total_storage_in_bytes = 2;
23+
uint64 nonce = 1;
24+
uint64 total_storage_in_bytes = 2;
2625
}

src/proto/proto/db3_base.proto

+11-12
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,25 @@
1616
// limitations under the License.
1717
//
1818

19-
2019
syntax = "proto3";
2120
package db3_base_proto;
2221

2322
enum ChainRole {
24-
SettlementChain = 0;
25-
StorageShardChain = 10;
26-
DvmComputingChain = 20;
23+
SettlementChain = 0;
24+
StorageShardChain = 10;
25+
DvmComputingChain = 20;
2726
}
2827

2928
enum ChainId {
30-
MainNet = 0;
31-
TestNet = 10;
32-
DevNet = 20;
29+
MainNet = 0;
30+
TestNet = 10;
31+
DevNet = 20;
3332
}
3433

3534
message BroadcastMeta {
36-
uint64 nonce = 1;
37-
// the chain id of db3
38-
ChainId chain_id = 2;
39-
// the chain role of db3
40-
ChainRole chain_role = 3;
35+
uint64 nonce = 1;
36+
// the chain id of db3
37+
ChainId chain_id = 2;
38+
// the chain role of db3
39+
ChainRole chain_role = 3;
4140
}

src/proto/proto/db3_bill.proto

+18-18
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ syntax = "proto3";
2020
package db3_bill_proto;
2121

2222
enum BillType {
23-
BillForMutation = 0;
24-
BillForQuery = 1;
25-
BillForMint = 2;
23+
BillForMutation = 0;
24+
BillForQuery = 1;
25+
BillForMint = 2;
2626
}
2727

2828
message Bill {
29-
// uint256
30-
uint64 gas_fee = 1;
31-
// the block id
32-
uint64 block_id = 2;
33-
// query session or mutation
34-
BillType bill_type = 4;
35-
// time on block created
36-
uint64 time = 5;
37-
// mutation id or query session id
38-
bytes tx_id = 6;
39-
// owner address
40-
bytes owner = 8;
41-
// to address
42-
bytes to = 9;
29+
// uint256
30+
uint64 gas_fee = 1;
31+
// the block id
32+
uint64 block_id = 2;
33+
// query session or mutation
34+
BillType bill_type = 4;
35+
// time on block created
36+
uint64 time = 5;
37+
// mutation id or query session id
38+
bytes tx_id = 6;
39+
// owner address
40+
bytes owner = 8;
41+
// to address
42+
bytes to = 9;
4343
}
4444

4545
message BillQueryRequest {
46-
uint64 block_height = 1;
46+
uint64 block_height = 1;
4747
}

src/proto/proto/db3_database.proto

+10-14
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ package db3_database_proto;
2323
// the definition of database
2424
//
2525
message Database {
26-
bytes address = 1;
27-
// the owner of the Database
28-
bytes sender = 2;
29-
// the history of database modification
30-
repeated bytes tx = 3;
31-
repeated Collection collections = 4;
32-
string desc =5;
26+
bytes address = 1;
27+
// the owner of the Database
28+
bytes sender = 2;
29+
// the history of database modification
30+
repeated bytes tx = 3;
31+
repeated Collection collections = 4;
32+
string desc = 5;
3333
}
3434

3535
message Collection {
36-
bytes id = 1;
37-
string name = 2;
38-
repeated Index index_list = 3;
36+
bytes id = 1;
37+
string name = 2;
38+
repeated Index index_list = 3;
3939
}
4040

4141
message Document {
@@ -46,7 +46,6 @@ message Document {
4646
}
4747

4848
message Index {
49-
5049
// A field in an index.
5150
// The field_path describes which field is indexed, the value_mode describes
5251
// how the field value is indexed.
@@ -432,7 +431,4 @@ message StructuredQuery {
432431
// * The number of values cannot be greater than the number of fields
433432
// specified in the `ORDER BY` clause.
434433
// Cursor end_at = 8;
435-
436-
437434
}
438-

0 commit comments

Comments
 (0)