-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/evm' into canonbrother/ci-evm
- Loading branch information
Showing
20 changed files
with
941 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,3 +156,4 @@ src/secp256k1/src/ecmult_static_context.h | |
# EVM | ||
src/evm/Cargo.lock | ||
src/evm/target/ | ||
*.bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use crate::traits::PersistentState; | ||
use ethereum::BlockAny; | ||
use primitive_types::{H256, U256}; | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
use std::fs::File; | ||
use std::io::{Read, Write}; | ||
use std::ops::Index; | ||
use std::path::Path; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
pub static BLOCK_MAP_PATH: &str = "block_map.bin"; | ||
pub static BLOCK_DATA_PATH: &str = "block_data.bin"; | ||
|
||
type BlockHashtoBlock = HashMap<H256, U256>; | ||
type Blocks = Vec<BlockAny>; | ||
|
||
pub struct BlockHandler { | ||
pub block_map: Arc<RwLock<BlockHashtoBlock>>, | ||
pub blocks: Arc<RwLock<Blocks>>, | ||
} | ||
|
||
impl PersistentState for BlockHashtoBlock { | ||
fn save_to_disk(&self, path: &str) -> Result<(), String> { | ||
let serialized_state = bincode::serialize(self).map_err(|e| e.to_string())?; | ||
let mut file = File::create(path).map_err(|e| e.to_string())?; | ||
file.write_all(&serialized_state).map_err(|e| e.to_string()) | ||
} | ||
|
||
fn load_from_disk(path: &str) -> Result<Self, String> { | ||
if Path::new(path).exists() { | ||
let mut file = File::open(path).map_err(|e| e.to_string())?; | ||
let mut data = Vec::new(); | ||
file.read_to_end(&mut data).map_err(|e| e.to_string())?; | ||
let new_state: HashMap<H256, U256> = | ||
bincode::deserialize(&data).map_err(|e| e.to_string())?; | ||
Ok(new_state) | ||
} else { | ||
Ok(Self::new()) | ||
} | ||
} | ||
} | ||
|
||
impl PersistentState for Blocks { | ||
fn save_to_disk(&self, path: &str) -> Result<(), String> { | ||
let serialized_state = bincode::serialize(self).map_err(|e| e.to_string())?; | ||
let mut file = File::create(path).map_err(|e| e.to_string())?; | ||
file.write_all(&serialized_state).map_err(|e| e.to_string()) | ||
} | ||
|
||
fn load_from_disk(path: &str) -> Result<Self, String> { | ||
if Path::new(path).exists() { | ||
let mut file = File::open(path).map_err(|e| e.to_string())?; | ||
let mut data = Vec::new(); | ||
file.read_to_end(&mut data).map_err(|e| e.to_string())?; | ||
let new_state: Vec<BlockAny> = | ||
bincode::deserialize(&data).map_err(|e| e.to_string())?; | ||
Ok(new_state) | ||
} else { | ||
Ok(Self::new()) | ||
} | ||
} | ||
} | ||
|
||
impl BlockHandler { | ||
pub fn new() -> Self { | ||
Self { | ||
block_map: Arc::new(RwLock::new( | ||
BlockHashtoBlock::load_from_disk(BLOCK_MAP_PATH).unwrap(), | ||
)), | ||
blocks: Arc::new(RwLock::new( | ||
Blocks::load_from_disk(BLOCK_DATA_PATH).unwrap(), | ||
)), | ||
} | ||
} | ||
|
||
pub fn connect_block(&self, block: BlockAny) { | ||
let mut blocks = self.blocks.write().unwrap(); | ||
blocks.push(block.clone()); | ||
|
||
let mut blockhash = self.block_map.write().unwrap(); | ||
blockhash.insert(block.header.hash(), block.header.number); | ||
} | ||
|
||
pub fn flush(&self) { | ||
let _ = self | ||
.block_map | ||
.write() | ||
.unwrap() | ||
.save_to_disk(BLOCK_MAP_PATH) | ||
.unwrap(); | ||
let _ = self | ||
.blocks | ||
.write() | ||
.unwrap() | ||
.save_to_disk(BLOCK_DATA_PATH) | ||
.unwrap(); | ||
} | ||
|
||
pub fn get_block_hash(&self, hash: H256) -> Result<BlockAny, Box<dyn Error>> { | ||
let block_map = self.block_map.read().unwrap(); | ||
let block_number = block_map.get(&hash).unwrap().clone(); | ||
|
||
let blocks = self.blocks.read().unwrap(); | ||
let block = blocks.get(block_number.as_usize()).unwrap().clone(); | ||
|
||
Ok(block) | ||
} | ||
} |
Oops, something went wrong.