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

Resolve datadir through FFI #1925

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions lib/Cargo.lock

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

6 changes: 6 additions & 0 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod ffi {
fn isMining() -> bool;
fn publishEthTransaction(data: Vec<u8>) -> bool;
fn getAccounts() -> Vec<String>;
fn getDatadir() -> String;
}
}

Expand All @@ -31,3 +32,8 @@ pub fn get_accounts() -> Result<Vec<String>, Box<dyn Error>> {
let accounts = ffi::getAccounts();
Ok(accounts)
}

pub fn get_datadir() -> Result<String, Box<dyn Error>> {
let datadir = ffi::getDatadir();
Ok(datadir)
}
3 changes: 3 additions & 0 deletions lib/ain-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
ain-cpp-imports = { path = "../ain-cpp-imports" }

evm = { version = "0.37", default-features = false, features = ["with-serde"] }
primitive-types = { version = "0.12", default-features = false, features = ["serde"] }
log = "0.4"
Expand All @@ -16,6 +18,7 @@ hex-literal = "0.3.4"
anyhow = "1.0"
bincode = "1.3.3"
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }

# Runtime dependencies
lazy_static = "1.4"
Expand Down
46 changes: 2 additions & 44 deletions lib/ain-evm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ use ethereum::BlockAny;
use primitive_types::H256;
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::{Read, Write};

use std::path::Path;
use std::sync::{Arc, RwLock};

pub static BLOCK_MAP_PATH: &str = "block_map.bin";
Expand All @@ -20,47 +16,9 @@ pub struct BlockHandler {
pub blocks: Arc<RwLock<Blocks>>,
}

impl PersistentState for BlockHashtoBlock {
fn save_to_disk(&self, path: &str) -> Result<(), PersistentStateError> {
let serialized_state = bincode::serialize(self)?;
let mut file = File::create(path)?;
file.write_all(&serialized_state)?;
Ok(())
}

fn load_from_disk(path: &str) -> Result<Self, PersistentStateError> {
if Path::new(path).exists() {
let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
let new_state: HashMap<H256, usize> = bincode::deserialize(&data)?;
Ok(new_state)
} else {
Ok(Self::new())
}
}
}
impl PersistentState for BlockHashtoBlock {}

impl PersistentState for Blocks {
fn save_to_disk(&self, path: &str) -> Result<(), PersistentStateError> {
let serialized_state = bincode::serialize(self)?;
let mut file = File::create(path)?;
file.write_all(&serialized_state)?;
Ok(())
}

fn load_from_disk(path: &str) -> Result<Self, PersistentStateError> {
if Path::new(path).exists() {
let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
let new_state: Vec<BlockAny> = bincode::deserialize(&data)?;
Ok(new_state)
} else {
Ok(Self::new())
}
}
}
impl PersistentState for Blocks {}

impl Default for BlockHandler {
fn default() -> Self {
Expand Down
30 changes: 4 additions & 26 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ use hex::FromHex;
use primitive_types::{H160, H256, U256};
use std::collections::BTreeMap;
use std::error::Error;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::sync::{Arc, RwLock};

pub static EVM_STATE_PATH: &str = "evm_state.bin";
pub static EVM_STATE_FILE: &str = "evm_state.bin";

pub type EVMState = BTreeMap<H160, MemoryAccount>;

Expand All @@ -27,26 +24,7 @@ pub struct EVMHandler {
pub tx_queues: Arc<TransactionQueueMap>,
}

impl PersistentState for EVMState {
fn save_to_disk(&self, path: &str) -> Result<(), PersistentStateError> {
let serialized_state = bincode::serialize(self)?;
let mut file = File::create(path)?;
file.write_all(&serialized_state)?;
Ok(())
}

fn load_from_disk(path: &str) -> Result<Self, PersistentStateError> {
if Path::new(path).exists() {
let mut file = File::open(path)?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
let new_state: BTreeMap<H160, MemoryAccount> = bincode::deserialize(&data)?;
Ok(new_state)
} else {
Ok(Self::new())
}
}
}
impl PersistentState for EVMState {}

impl Default for EVMHandler {
fn default() -> Self {
Expand All @@ -58,14 +36,14 @@ impl EVMHandler {
pub fn new() -> Self {
Self {
state: Arc::new(RwLock::new(
EVMState::load_from_disk(EVM_STATE_PATH).expect("Error loading state"),
EVMState::load_from_disk(EVM_STATE_FILE).expect("Error loading state"),
)),
tx_queues: Arc::new(TransactionQueueMap::new()),
}
}

pub fn flush(&self) -> Result<(), PersistentStateError> {
self.state.write().unwrap().save_to_disk(EVM_STATE_PATH)
self.state.write().unwrap().save_to_disk(EVM_STATE_FILE)
}

pub fn call(
Expand Down
43 changes: 38 additions & 5 deletions lib/ain-evm/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{executor::TxResponse, transaction::SignedTx};
use ethereum::AccessList;
use evm::Config;
use primitive_types::{H160, U256};

use crate::{executor::TxResponse, transaction::SignedTx};
use std::fs::File;

pub trait Executor {
const CONFIG: Config = Config::london();
Expand All @@ -22,14 +22,47 @@ pub trait Executor {
}

pub trait PersistentState {
fn save_to_disk(&self, path: &str) -> Result<(), PersistentStateError>;
fn load_from_disk(path: &str) -> Result<Self, PersistentStateError>
fn save_to_disk(&self, file_path: &str) -> Result<(), PersistentStateError>
where
Self: serde::ser::Serialize,
{
// Automatically resolves from datadir for now
let path = match ain_cpp_imports::get_datadir() {
Ok(path) => PathBuf::from(path).join("evm").join(file_path),
_ => PathBuf::from(file_path),
};

let serialized_state = bincode::serialize(self)?;
let mut file = File::create(path)?;
file.write_all(&serialized_state)?;
Ok(())
}

fn load_from_disk(file_path: &str) -> Result<Self, PersistentStateError>
where
Self: Sized;
Self: Sized + serde::de::DeserializeOwned + Default,
{
// Automatically resolves from datadir for now
let path = match ain_cpp_imports::get_datadir() {
Ok(path) => PathBuf::from(path).join("evm").join(file_path),
_ => PathBuf::from(file_path),
};

if Path::new(&path).exists() {
let file = File::open(path)?;
let new_state: Self = bincode::deserialize_from(file)?;
Ok(new_state)
} else {
Ok(Self::default())
}
}
}

use std::fmt;
use std::io;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

#[derive(Debug)]
pub enum PersistentStateError {
Expand Down
13 changes: 4 additions & 9 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::error::Error;

use primitive_types::H160;

pub const WEI_TO_GWEI : u64 = 1000000000;
pub const GWEI_TO_SATS : u64 = 10;
pub const WEI_TO_GWEI: u64 = 1000000000;
pub const GWEI_TO_SATS: u64 = 10;

#[cxx::bridge]
mod ffi {
Expand Down Expand Up @@ -43,14 +43,9 @@ mod ffi {
}
}

pub fn evm_get_balance(
address: &str,
) -> Result<u64, Box<dyn Error>> {
pub fn evm_get_balance(address: &str) -> Result<u64, Box<dyn Error>> {
let account = address.parse()?;
let mut balance = RUNTIME
.handlers
.evm
.get_balance(account);
let mut balance = RUNTIME.handlers.evm.get_balance(account);
balance /= WEI_TO_GWEI;
balance /= GWEI_TO_SATS;
Ok(balance.as_u64())
Expand Down
2 changes: 1 addition & 1 deletion src/defid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ static bool AppInit(int argc, char* argv[])
bool fRet = false;

util::ThreadRename("init");
init_runtime();

//
// Parameters
Expand Down Expand Up @@ -112,6 +111,7 @@ static bool AppInit(int argc, char* argv[])
}

// Start GRPC after BaseParams() has been initialised
init_runtime();
int grpc_port = gArgs.GetArg("-grpcport", BaseParams().GRPCPort());
start_servers("127.0.0.1:" + std::to_string(grpc_port), "127.0.0.1:" + std::to_string(grpc_port + 1));

Expand Down
4 changes: 4 additions & 0 deletions src/ffi/ffiexports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ rust::vec<rust::string> getAccounts() {
}
return addresses;
}

rust::string getDatadir() {
return GetDataDir().c_str();
}
1 change: 1 addition & 0 deletions src/ffi/ffiexports.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ uint64_t getChainId();
bool isMining();
bool publishEthTransaction(rust::Vec<uint8_t> rawTransaction);
rust::vec<rust::string> getAccounts();
rust::string getDatadir();

#endif // DEFI_EVM_FFI_H