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

Fix blockstore get gas charge #751

Merged
merged 5 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
10 changes: 4 additions & 6 deletions ipld/blockstore/src/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use super::BlockStore;
use cid::{multihash::MultihashDigest, Cid, Codec};
use db::{Error, Store};
use encoding::{from_slice, ser::Serialize, to_vec};
use encoding::from_slice;
use forest_ipld::Ipld;
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -113,14 +113,12 @@ where
self.base.get_bytes(cid)
}

fn put<S, T>(&self, obj: &S, hash: T) -> Result<Cid, Box<dyn StdError>>
fn put_raw<T>(&self, bytes: Vec<u8>, hash: T) -> Result<Cid, Box<dyn StdError>>
where
S: Serialize,
T: MultihashDigest,
{
let bz = to_vec(obj)?;
let cid = Cid::new_from_cbor(&bz, hash);
self.write.borrow_mut().insert(cid.clone(), bz);
let cid = Cid::new_from_cbor(&bytes, hash);
self.write.borrow_mut().insert(cid.clone(), bytes);
Ok(cid)
}
}
Expand Down
20 changes: 14 additions & 6 deletions ipld/blockstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use db::{RocksDb, WriteBatch};

/// Wrapper for database to handle inserting and retrieving ipld data with Cids
pub trait BlockStore: Store {
/// Get bytes from block store by Cid
/// Get bytes from block store by Cid.
fn get_bytes(&self, cid: &Cid) -> Result<Option<Vec<u8>>, Box<dyn StdError>> {
Ok(self.read(cid.to_bytes())?)
}

/// Get typed object from block store by Cid
/// Get typed object from block store by Cid.
fn get<T>(&self, cid: &Cid) -> Result<Option<T>, Box<dyn StdError>>
where
T: DeserializeOwned,
Expand All @@ -40,15 +40,23 @@ pub trait BlockStore: Store {
}
}

/// Put an object in the block store and return the Cid identifier
/// Put an object in the block store and return the Cid identifier.
fn put<S, T>(&self, obj: &S, hash: T) -> Result<Cid, Box<dyn StdError>>
where
S: Serialize,
T: MultihashDigest,
{
let bz = to_vec(obj)?;
let cid = Cid::new_from_cbor(&bz, hash);
self.write(cid.to_bytes(), bz)?;
let bytes = to_vec(obj)?;
self.put_raw(bytes, hash)
}

/// Put raw bytes in the block store and return the Cid identifier.
fn put_raw<T>(&self, bytes: Vec<u8>, hash: T) -> Result<Cid, Box<dyn StdError>>
where
T: MultihashDigest,
{
let cid = Cid::new_from_cbor(&bytes, hash);
self.write(cid.to_bytes(), bytes)?;
Ok(cid)
}

Expand Down
4 changes: 1 addition & 3 deletions ipld/blockstore/src/tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ where
Ok(bytes)
}

fn put<S, T>(&self, obj: &S, hash: T) -> Result<Cid, Box<dyn StdError>>
fn put_raw<T>(&self, bytes: Vec<u8>, hash: T) -> Result<Cid, Box<dyn StdError>>
where
S: Serialize,
T: MultihashDigest,
{
let bytes = to_vec(obj)?;
self.stats.borrow_mut().w += 1;
self.stats.borrow_mut().bw += bytes.len();
let cid = Cid::new_from_cbor(&bytes, hash);
Expand Down
3 changes: 0 additions & 3 deletions tests/conformance_tests/tests/conformance_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ lazy_static! {
// Extracted miner faults
Regex::new(r"fil_1_storageminer-DeclareFaults-Ok-3").unwrap(),
Regex::new(r"fil_1_storageminer-DeclareFaults-Ok-7").unwrap(),
Regex::new(r"fil_1_storageminer-PreCommitSector-SysErrOutOfGas").unwrap(),
Regex::new(r"fil_1_storageminer-DeclareFaultsRecovered-SysErrOutOfGas-1").unwrap(),
Regex::new(r"fil_1_storageminer-DeclareFaultsRecovered-SysErrOutOfGas-2").unwrap(),

// Extracted market faults
Regex::new(r"fil_1_storagemarket-PublishStorageDeals-").unwrap(),
Expand Down
35 changes: 8 additions & 27 deletions vm/interpreter/src/gas_block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
use super::gas_tracker::{GasTracker, PriceList};
use cid::{multihash::MultihashDigest, Cid};
use db::{Error, Store};
use forest_encoding::{de::DeserializeOwned, from_slice, ser::Serialize, to_vec};
use forest_encoding::{de::DeserializeOwned, ser::Serialize, to_vec};
use ipld_blockstore::BlockStore;
use std::cell::RefCell;
use std::error::Error as StdError;
use std::rc::Rc;
use vm::{actor_error, ActorError};

/// Blockstore wrapper to charge gas on reads and writes
pub(crate) struct GasBlockStore<'bs, BS> {
Expand All @@ -22,45 +21,27 @@ impl<BS> BlockStore for GasBlockStore<'_, BS>
where
BS: BlockStore,
{
/// Get bytes from block store by Cid
fn get_bytes(&self, cid: &Cid) -> Result<Option<Vec<u8>>, Box<dyn StdError>> {
let ret = self
.store
.get_bytes(cid)
.map_err(|e| actor_error!(fatal("failed to get block from blockstore: {}", e)))?;
if let Some(bz) = &ret {
self.gas
.borrow_mut()
.charge_gas(self.price_list.on_ipld_get(bz.len()))?;
}
Ok(ret)
}

/// Get typed object from block store by Cid
fn get<T>(&self, cid: &Cid) -> Result<Option<T>, Box<dyn StdError>>
where
T: DeserializeOwned,
{
match self.get_bytes(cid)? {
Some(bz) => Ok(Some(from_slice(&bz)?)),
None => Ok(None),
}
self.gas
.borrow_mut()
.charge_gas(self.price_list.on_ipld_get())?;
self.store.get(cid)
}

/// Put an object in the block store and return the Cid identifier
fn put<S, T>(&self, obj: &S, hash: T) -> Result<Cid, Box<dyn StdError>>
where
S: Serialize,
T: MultihashDigest,
{
let bytes = to_vec(obj)?;
self.gas
.borrow_mut()
.charge_gas(self.price_list.on_ipld_put(to_vec(obj).unwrap().len()))?;
.charge_gas(self.price_list.on_ipld_put(bytes.len()))?;

Ok(self
.store
.put(obj, hash)
.map_err(|e| actor_error!(fatal("failed to write to store {}", e)))?)
Ok(self.store.put_raw(bytes, hash)?)
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/interpreter/src/gas_tracker/price_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl PriceList {
}
/// Returns the gas required for storing an object
#[inline]
pub fn on_ipld_get(&self, _: usize) -> GasCharge {
pub fn on_ipld_get(&self) -> GasCharge {
GasCharge::new("on_ipld_get", self.ipld_get_base, 0)
}
/// Returns the gas required for storing an object
Expand Down