Skip to content

Commit

Permalink
Revert "Revert #2337 (#2346)". Restore RocksDB as BlockStore backend
Browse files Browse the repository at this point in the history
This reverts commit a9bfaac.
  • Loading branch information
Jouzo committed Aug 18, 2023
1 parent 28044c6 commit bb8a8eb
Show file tree
Hide file tree
Showing 11 changed files with 703 additions and 514 deletions.
1 change: 1 addition & 0 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/ain-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ num = { version = "0.4", default-features = false, features = ["alloc"] }
ripemd = { version = "0.1", default-features = false }
substrate-bn = "0.6.0"

# Database dependencies
rocksdb = { version = "0.20.1", default-features = false }

[dev-dependencies]
tempdir = "0.3.7"
once_cell = "1.17.1"
152 changes: 76 additions & 76 deletions lib/ain-evm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,80 +321,80 @@ impl BlockService {

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

#[test]
fn test_base_fee_equal() {
let block = BlockService::new(Arc::new(Storage::new())).unwrap();
assert_eq!(
U256::from(20_000_000_000u64),
block.base_fee_calculation(
15_000_000,
15_000_000,
U256::from(20_000_000_000u64),
U256::from(8),
U256::from(10_000_000_000u64)
)
)
}

#[test]
fn test_base_fee_max_increase() {
let block = BlockService::new(Arc::new(Storage::new())).unwrap();
assert_eq!(
U256::from(22_500_000_000u64), // should increase by 12.5%
block.base_fee_calculation(
30_000_000,
15_000_000,
U256::from(20_000_000_000u64),
U256::from(8),
U256::from(10_000_000_000u64)
)
)
}

#[test]
fn test_base_fee_increase() {
let block = BlockService::new(Arc::new(Storage::new())).unwrap();
assert_eq!(
U256::from(20_833_333_333u64), // should increase by ~4.15%
block.base_fee_calculation(
20_000_000,
15_000_000,
U256::from(20_000_000_000u64),
U256::from(8),
U256::from(10_000_000_000u64)
)
)
}

#[test]
fn test_base_fee_max_decrease() {
let block = BlockService::new(Arc::new(Storage::new())).unwrap();
assert_eq!(
U256::from(17_500_000_000u64), // should decrease by 12.5%
block.base_fee_calculation(
0,
15_000_000,
U256::from(20_000_000_000u64),
U256::from(8),
U256::from(10_000_000_000u64)
)
)
}

#[test]
fn test_base_fee_decrease() {
let block = BlockService::new(Arc::new(Storage::new())).unwrap();
assert_eq!(
U256::from(19_166_666_667u64), // should increase by ~4.15%
block.base_fee_calculation(
10_000_000,
15_000_000,
U256::from(20_000_000_000u64),
U256::from(8),
U256::from(10_000_000_000u64)
)
)
}
// use super::*;

// #[test]
// fn test_base_fee_equal() {
// let block = BlockService::new(Arc::new(Storage::new()?)).unwrap();
// assert_eq!(
// U256::from(20_000_000_000u64),
// block.base_fee_calculation(
// 15_000_000,
// 15_000_000,
// U256::from(20_000_000_000u64),
// U256::from(8),
// U256::from(10_000_000_000u64)
// )
// )
// }

// #[test]
// fn test_base_fee_max_increase() {
// let block = BlockService::new(Arc::new(Storage::new()?)).unwrap();
// assert_eq!(
// U256::from(22_500_000_000u64), // should increase by 12.5%
// block.base_fee_calculation(
// 30_000_000,
// 15_000_000,
// U256::from(20_000_000_000u64),
// U256::from(8),
// U256::from(10_000_000_000u64)
// )
// )
// }

// #[test]
// fn test_base_fee_increase() {
// let block = BlockService::new(Arc::new(Storage::new()?)).unwrap();
// assert_eq!(
// U256::from(20_833_333_333u64), // should increase by ~4.15%
// block.base_fee_calculation(
// 20_000_000,
// 15_000_000,
// U256::from(20_000_000_000u64),
// U256::from(8),
// U256::from(10_000_000_000u64)
// )
// )
// }

// #[test]
// fn test_base_fee_max_decrease() {
// let block = BlockService::new(Arc::new(Storage::new()?)).unwrap();
// assert_eq!(
// U256::from(17_500_000_000u64), // should decrease by 12.5%
// block.base_fee_calculation(
// 0,
// 15_000_000,
// U256::from(20_000_000_000u64),
// U256::from(8),
// U256::from(10_000_000_000u64)
// )
// )
// }

// #[test]
// fn test_base_fee_decrease() {
// let block = BlockService::new(Arc::new(Storage::new()?)).unwrap();
// assert_eq!(
// U256::from(19_166_666_667u64), // should increase by ~4.15%
// block.base_fee_calculation(
// 10_000_000,
// 15_000_000,
// U256::from(20_000_000_000u64),
// U256::from(8),
// U256::from(10_000_000_000u64)
// )
// )
// }
}
28 changes: 15 additions & 13 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,16 @@ pub struct ValidateTxInfo {
pub used_gas: u64,
}

fn init_vsdb() {
fn init_vsdb(path: PathBuf) {
debug!(target: "vsdb", "Initializating VSDB");
let datadir = ain_cpp_imports::get_datadir();
let path = PathBuf::from(datadir).join("evm");
if !path.exists() {
std::fs::create_dir(&path).expect("Error creating `evm` dir");
}
let vsdb_dir_path = path.join(".vsdb");
vsdb_set_base_dir(&vsdb_dir_path).expect("Could not update vsdb base dir");
debug!(target: "vsdb", "VSDB directory : {}", vsdb_dir_path.display());
}

impl EVMCoreService {
pub fn restore(storage: Arc<Storage>) -> Self {
init_vsdb();
pub fn restore(storage: Arc<Storage>, path: PathBuf) -> Self {
init_vsdb(path);

Self {
tx_queues: Arc::new(TransactionQueueMap::new()),
Expand All @@ -76,17 +71,24 @@ impl EVMCoreService {
}
}

pub fn new_from_json(storage: Arc<Storage>, path: PathBuf) -> Result<Self> {
debug!("Loading genesis state from {}", path.display());
init_vsdb();
pub fn new_from_json(
storage: Arc<Storage>,
genesis_path: PathBuf,
evm_datadir: PathBuf,
) -> Result<Self> {
debug!("Loading genesis state from {}", genesis_path.display());
init_vsdb(evm_datadir);

let handler = Self {
tx_queues: Arc::new(TransactionQueueMap::new()),
trie_store: Arc::new(TrieDBStore::new()),
storage: Arc::clone(&storage),
};
let (state_root, genesis) =
TrieDBStore::genesis_state_root_from_json(&handler.trie_store, &handler.storage, path)?;
let (state_root, genesis) = TrieDBStore::genesis_state_root_from_json(
&handler.trie_store,
&handler.storage,
genesis_path,
)?;

let gas_limit = storage.get_attributes_or_default()?.block_gas_limit;
let block: Block<TransactionV2> = Block::new(
Expand Down
20 changes: 15 additions & 5 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,36 @@ impl EVMServices {
///
/// Returns an instance of the struct, either restored from storage or created from a JSON file.
pub fn new() -> Result<Self> {
if let Some(path) = ain_cpp_imports::get_state_input_json() {
let datadir = ain_cpp_imports::get_datadir();
let path = PathBuf::from(datadir).join("evm");
if !path.exists() {
std::fs::create_dir(&path)?
}

if let Some(state_input_path) = ain_cpp_imports::get_state_input_json() {
if ain_cpp_imports::get_network() != "regtest" {
return Err(format_err!(
"Loading a genesis from JSON file is restricted to regtest network"
)
.into());
}
let storage = Arc::new(Storage::new());
let storage = Arc::new(Storage::new(&path)?);
Ok(Self {
core: EVMCoreService::new_from_json(Arc::clone(&storage), PathBuf::from(path))?,
core: EVMCoreService::new_from_json(
Arc::clone(&storage),
PathBuf::from(state_input_path),
path,
)?,
block: BlockService::new(Arc::clone(&storage))?,
receipt: ReceiptService::new(Arc::clone(&storage)),
logs: LogService::new(Arc::clone(&storage)),
filters: FilterService::new(),
storage,
})
} else {
let storage = Arc::new(Storage::restore());
let storage = Arc::new(Storage::restore(&path)?);
Ok(Self {
core: EVMCoreService::restore(Arc::clone(&storage)),
core: EVMCoreService::restore(Arc::clone(&storage), path),
block: BlockService::new(Arc::clone(&storage))?,
receipt: ReceiptService::new(Arc::clone(&storage)),
logs: LogService::new(Arc::clone(&storage)),
Expand Down
2 changes: 2 additions & 0 deletions lib/ain-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub enum EVMError {
StorageError(String),
#[error("EVM: serde_json error")]
JsonError(#[from] serde_json::Error),
#[error("EVM: rocksdb error")]
RocksDBError(#[from] rocksdb::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
Loading

0 comments on commit bb8a8eb

Please sign in to comment.