Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add storage #113

Merged
merged 8 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: move out db from kv store
  • Loading branch information
imotai committed Oct 30, 2022
commit 7826ca961b83b00ecafc76a60aea1497e7723f56
3 changes: 2 additions & 1 deletion src/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ ethereum-types = { version = "0.13.1", default-features = false }
db3-base={ path = "../base" }
db3-proto={ path ="../proto" }
db3-error={ path = "../error" }
merk = "2.0.0"
db3-types={ path = "../types" }
merk = {version= "2.0.0", path = "../../thirdparty/merk"}
hex = "0.4.3"
26 changes: 26 additions & 0 deletions src/storage/src/bill_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// bill_store.rs
// Copyright (C) 2022 db3.network Author imotai <codego.me@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
}
}
29 changes: 11 additions & 18 deletions src/storage/src/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@
use super::key::Key;
use db3_error::{DB3Error, Result};
use db3_proto::db3_mutation_proto::{KvPair, Mutation, MutationAction};
use db3_types::cost;
use ethereum_types::Address as AccountAddress;
use hex;
use merk::{BatchEntry, Merk, Op};
use std::sync::{Arc, Mutex};

pub struct KvStore {
db: Arc<Mutex<Merk>>,
}
pub struct KvStore {}

impl KvStore {
pub fn new(db: Arc<Mutex<Merk>>) -> Self {
Self { db }
pub fn new() -> Self {
Self {}
}

fn convert(kp: &KvPair, account_addr: &AccountAddress, ns: &[u8]) -> Result<BatchEntry> {
Expand All @@ -48,7 +46,7 @@ impl KvStore {
}
}

pub fn apply(&self, account_addr: &AccountAddress, mutation: &Mutation) -> Result<()> {
pub fn apply(db: &mut Merk, account_addr: &AccountAddress, mutation: &Mutation) -> Result<u64> {
let ns = mutation.ns.as_ref();
//TODO avoid copying operation
let mut ordered_kv_pairs = mutation.kv_pairs.to_vec();
Expand All @@ -58,14 +56,10 @@ impl KvStore {
let batch_entry = Self::convert(&kv, account_addr, ns)?;
entries.push(batch_entry);
}
match self.db.lock() {
Ok(mut mdb) => {
mdb.apply(&entries, &[])
.map_err(|e| DB3Error::ApplyMutationError(format!("{}", e)))?;
}
Err(_) => {}
}
Ok(())
let gas = cost::estimate_gas(mutation);
db.apply(&entries, &[])
.map_err(|e| DB3Error::ApplyMutationError(format!("{}", e)))?;
Ok(gas)
}
}

Expand All @@ -79,8 +73,7 @@ mod tests {
fn it_apply_mutation() {
let path = thread::current().name().unwrap().to_owned();
let addr = get_a_static_address();
let merk = Merk::open(path).unwrap();
let kvstore = KvStore::new(Arc::new(Mutex::new(merk)));
let mut merk = Merk::open(path).unwrap();
let kv1 = KvPair {
key: "k1".as_bytes().to_vec(),
value: "value1".as_bytes().to_vec(),
Expand All @@ -100,7 +93,7 @@ mod tests {
gas_price: 1,
gas: 10,
};
let result = kvstore.apply(&addr, &mutation);
let result = KvStore::apply(&mut merk, &addr, &mutation);
assert!(result.is_ok());
}
}
2 changes: 2 additions & 0 deletions src/types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Account {
/// reward for validator
total_reward: u64,
total_storage_usage: u64,
nonce: u64,
next_bill_id: u64,
}

pub struct AccountKey(AccountAddress);
Expand Down
29 changes: 29 additions & 0 deletions src/types/src/coin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// coin.rs
// Copyright (C) 2022 db3.network Author imotai <codego.me@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

pub enum Coin {
DB3(u64),
TAI(u64), // 10^6 tai = 1 db3
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {}
}
61 changes: 61 additions & 0 deletions src/types/src/cost.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// cost.rs
// Copyright (C) 2022 db3.network Author imotai <codego.me@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
use db3_proto::db3_mutation_proto::{Mutation, MutationAction};

const COMPUTAION_GAS_PRICE: u64 = 1000; // unit in tai
const STORAGE_GAS_PRICE: u64 = 100; // unit in tai

pub fn estimate_gas(mutation: &Mutation) -> u64 {
let mut gas: u64 = 0;
gas += mutation.kv_pairs.len() as u64 * COMPUTAION_GAS_PRICE;
for kv in &mutation.kv_pairs {
let action = MutationAction::from_i32(kv.action);
match action {
Some(MutationAction::InsertKv) => {
gas +=
(mutation.ns.len() + kv.key.len() + kv.value.len()) as u64 * STORAGE_GAS_PRICE;
}
_ => {}
}
}
gas
}

#[cfg(test)]
mod tests {
use super::*;
use db3_proto::db3_base_proto::{ChainId, ChainRole};
use db3_proto::db3_mutation_proto::{KvPair, MutationAction};
#[test]
fn it_estimate_gas() {
let kv = KvPair {
key: "k1".as_bytes().to_vec(),
value: "value1".as_bytes().to_vec(),
action: MutationAction::InsertKv.into(),
};
let mutation = Mutation {
ns: "my_twitter".as_bytes().to_vec(),
kv_pairs: vec![kv],
nonce: 1,
chain_id: ChainId::MainNet.into(),
chain_role: ChainRole::StorageShardChain.into(),
gas_price: 1,
gas: 10,
};
let gas = estimate_gas(&mutation);
}
}
2 changes: 2 additions & 0 deletions src/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub fn ensure_len_eq(data: &[u8], len: usize) -> Result<()> {

pub mod account;
pub mod bill;
pub mod coin;
pub mod cost;

#[cfg(test)]
mod tests {
Expand Down