Skip to content

Commit

Permalink
Merge branch 'master' into pvl/master-revert-2
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl authored Jul 27, 2023
2 parents 4e5d647 + 7bbd4e7 commit dae87ce
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 96 deletions.
3 changes: 1 addition & 2 deletions lib/Cargo.lock

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

1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ lto = true
[patch.crates-io]
vsdb = { git = 'https://github.com/defich/vsdb.git' }
vsdbsled = { git = 'https://github.com/defich/vsdbsled.git' }
ethereum = { git = 'https://github.com/defich/ethereum.git' }
18 changes: 5 additions & 13 deletions lib/ain-evm/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ impl BlockService {
.unwrap_or_default()
}

pub fn connect_block(&self, block: BlockAny, base_fee: U256) {
pub fn connect_block(&self, block: BlockAny) {
self.storage.put_latest_block(Some(&block));
self.storage.put_block(&block);
self.storage.set_base_fee(block.header.hash(), base_fee);
}

fn pre_changi_intermediate_2_base_fee_calculation(
Expand Down Expand Up @@ -169,10 +168,7 @@ impl BlockService {
.storage
.get_block_by_hash(&parent_hash)
.expect("Parent block not found");
let parent_base_fee = self
.storage
.get_base_fee(&parent_block.header.hash())
.expect("Parent base fee not found");
let parent_base_fee = parent_block.header.base_fee;
let parent_gas_used = parent_block.header.gas_used.as_u64();
let parent_gas_target =
parent_block.header.gas_limit.as_u64() / elasticity_multiplier.as_u64();
Expand Down Expand Up @@ -221,10 +217,7 @@ impl BlockService {
.iter()
.map(|block| {
debug!("Processing block {}", block.header.number);
let base_fee = self
.storage
.get_base_fee(&block.header.hash())
.unwrap_or_else(|| panic!("No base fee for block {}", block.header.number));
let base_fee = block.header.base_fee;

let gas_ratio = if block.header.gas_limit == U256::zero() {
f64::default() // empty block
Expand Down Expand Up @@ -352,13 +345,12 @@ impl BlockService {

pub fn get_legacy_fee(&self) -> U256 {
let priority_fee = self.suggested_priority_fee();
let latest_block_hash = self
let base_fee = self
.storage
.get_latest_block()
.expect("Unable to get latest block")
.header
.hash();
let base_fee = self.storage.get_base_fee(&latest_block_hash).unwrap();
.base_fee;

base_fee + priority_fee
}
Expand Down
4 changes: 1 addition & 3 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ impl EVMCoreService {
nonce: genesis.nonce.unwrap_or_default(),
timestamp: genesis.timestamp.unwrap_or_default().as_u64(),
difficulty: genesis.difficulty.unwrap_or_default(),
base_fee: genesis.base_fee.unwrap_or(INITIAL_BASE_FEE),
},
Vec::new(),
Vec::new(),
);
storage.put_latest_block(Some(&block));
storage.put_block(&block);
// NOTE(canonbrother): set an initial base fee for genesis block
// https://github.com/ethereum/go-ethereum/blob/46ec972c9c56a4e0d97d812f2eaf9e3657c66276/params/protocol_params.go#LL125C2-L125C16
storage.set_base_fee(block.header.hash(), INITIAL_BASE_FEE);

handler
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ impl EVMServices {
extra_data: Vec::default(),
mix_hash: H256::default(),
nonce: H64::default(),
base_fee,
},
all_transactions
.iter()
Expand All @@ -270,7 +271,7 @@ impl EVMServices {
block.header.number, block.header.state_root
);

self.block.connect_block(block.clone(), base_fee);
self.block.connect_block(block.clone());
self.logs
.generate_logs_from_receipts(&receipts, block.header.number);
self.receipt.put_receipts(receipts);
Expand Down
1 change: 1 addition & 0 deletions lib/ain-evm/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ pub struct GenesisData {
pub alloc: Option<HashMap<H160, Alloc>>,
pub parent_hash: Option<H256>,
pub mix_hash: Option<H256>,
pub base_fee: Option<U256>,
}
17 changes: 0 additions & 17 deletions lib/ain-evm/src/storage/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub struct Cache {
transactions: RwLock<LruCache<H256, TransactionV2>>,
blocks: RwLock<LruCache<U256, BlockAny>>,
block_hashes: RwLock<LruCache<H256, U256>>,
base_fee: RwLock<LruCache<H256, U256>>,
latest_block: RwLock<Option<BlockAny>>,
}

Expand All @@ -30,9 +29,6 @@ impl Cache {
block_hashes: RwLock::new(LruCache::new(
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(),
)),
base_fee: RwLock::new(LruCache::new(
NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(),
)),
latest_block: RwLock::new(None),
}
}
Expand Down Expand Up @@ -79,19 +75,6 @@ impl BlockStorage for Cache {
let mut cache = self.latest_block.write().unwrap();
*cache = block.cloned();
}

fn get_base_fee(&self, block_hash: &H256) -> Option<U256> {
self.base_fee
.write()
.unwrap()
.get(block_hash)
.map(ToOwned::to_owned)
}

fn set_base_fee(&self, block_hash: H256, base_fee: U256) {
let mut cache = self.base_fee.write().unwrap();
cache.put(block_hash, base_fee);
}
}

impl TransactionStorage for Cache {
Expand Down
27 changes: 0 additions & 27 deletions lib/ain-evm/src/storage/data_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ pub static LATEST_BLOCK_DATA_PATH: &str = "latest_block_data.bin";
pub static RECEIPT_MAP_PATH: &str = "receipt_map.bin";
pub static CODE_MAP_PATH: &str = "code_map.bin";
pub static TRANSACTION_DATA_PATH: &str = "transaction_data.bin";
pub static BASE_FEE_MAP_PATH: &str = "base_fee_map.bin";
pub static ADDRESS_LOGS_MAP_PATH: &str = "address_logs_map.bin";

type BlockHashtoBlock = HashMap<H256, U256>;
type Blocks = HashMap<U256, BlockAny>;
type TxHashToTx = HashMap<H256, TransactionV2>;
type LatestBlockNumber = U256;
type TransactionHashToReceipt = HashMap<H256, Receipt>;
type BlockHashtoBaseFee = HashMap<H256, U256>;
type AddressToLogs = HashMap<U256, HashMap<H160, Vec<LogIndex>>>;

impl PersistentState for BlockHashtoBlock {}
Expand All @@ -50,7 +48,6 @@ pub struct BlockchainDataHandler {
block_map: RwLock<BlockHashtoBlock>,
blocks: RwLock<Blocks>,
latest_block_number: RwLock<Option<LatestBlockNumber>>,
base_fee_map: RwLock<BlockHashtoBaseFee>,

code_map: RwLock<CodeHistory>,

Expand Down Expand Up @@ -78,9 +75,6 @@ impl BlockchainDataHandler {
TransactionHashToReceipt::load_from_disk(RECEIPT_MAP_PATH)
.expect("Error loading receipts data"),
),
base_fee_map: RwLock::new(
BlockHashtoBaseFee::load_from_disk(BASE_FEE_MAP_PATH).unwrap_or_default(),
),
code_map: RwLock::new(CodeHistory::load_from_disk(CODE_MAP_PATH).unwrap_or_default()),
address_logs_map: RwLock::new(
AddressToLogs::load_from_disk(ADDRESS_LOGS_MAP_PATH).unwrap_or_default(),
Expand Down Expand Up @@ -185,19 +179,6 @@ impl BlockStorage for BlockchainDataHandler {
let mut latest_block_number = self.latest_block_number.write().unwrap();
*latest_block_number = block.map(|b| b.header.number);
}

fn get_base_fee(&self, block_hash: &H256) -> Option<U256> {
self.base_fee_map
.read()
.unwrap()
.get(block_hash)
.map(ToOwned::to_owned)
}

fn set_base_fee(&self, block_hash: H256, base_fee: U256) {
let mut base_fee_map = self.base_fee_map.write().unwrap();
base_fee_map.insert(block_hash, base_fee);
}
}

impl ReceiptStorage for BlockchainDataHandler {
Expand Down Expand Up @@ -253,10 +234,6 @@ impl FlushableStorage for BlockchainDataHandler {
.unwrap()
.save_to_disk(TRANSACTION_DATA_PATH)?;
self.code_map.write().unwrap().save_to_disk(CODE_MAP_PATH)?;
self.base_fee_map
.write()
.unwrap()
.save_to_disk(BASE_FEE_MAP_PATH)?;
self.address_logs_map
.write()
.unwrap()
Expand Down Expand Up @@ -299,10 +276,6 @@ impl Rollback for BlockchainDataHandler {
}

self.block_map.write().unwrap().remove(&block.header.hash());
self.base_fee_map
.write()
.unwrap()
.remove(&block.header.hash());
self.blocks.write().unwrap().remove(&block.header.number);
self.code_map.write().unwrap().rollback(block.header.number);

Expand Down
16 changes: 0 additions & 16 deletions lib/ain-evm/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,6 @@ impl BlockStorage for Storage {
self.cache.put_latest_block(block);
self.blockchain_data_handler.put_latest_block(block);
}

fn get_base_fee(&self, block_hash: &H256) -> Option<U256> {
self.cache.get_base_fee(block_hash).or_else(|| {
let base_fee = self.blockchain_data_handler.get_base_fee(block_hash);
if let Some(base_fee) = base_fee {
self.cache.set_base_fee(*block_hash, base_fee);
}
base_fee
})
}

fn set_base_fee(&self, block_hash: H256, base_fee: U256) {
self.cache.set_base_fee(block_hash, base_fee);
self.blockchain_data_handler
.set_base_fee(block_hash, base_fee);
}
}

impl TransactionStorage for Storage {
Expand Down
3 changes: 0 additions & 3 deletions lib/ain-evm/src/storage/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub trait BlockStorage {
fn put_block(&self, block: &BlockAny);
fn get_latest_block(&self) -> Option<BlockAny>;
fn put_latest_block(&self, block: Option<&BlockAny>);

fn get_base_fee(&self, block_hash: &H256) -> Option<U256>;
fn set_base_fee(&self, block_hash: H256, base_fee: U256);
}

pub trait TransactionStorage {
Expand Down
8 changes: 2 additions & 6 deletions lib/ain-grpc/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ pub struct RpcBlock {
}

impl RpcBlock {
pub fn from_block_with_tx_and_base_fee(
block: BlockAny,
full_transactions: bool,
base_fee: U256,
) -> Self {
pub fn from_block_with_tx(block: BlockAny, full_transactions: bool) -> Self {
let header_size = block.header.rlp_bytes().len();
RpcBlock {
hash: block.header.hash(),
Expand Down Expand Up @@ -87,7 +83,7 @@ impl RpcBlock {
)),
logs_bloom: format!("{:#x}", block.header.logs_bloom),
size: format!("{header_size:#x}"),
base_fee_per_gas: base_fee,
base_fee_per_gas: block.header.base_fee,
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions lib/ain-grpc/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,9 @@ impl MetachainRPCServer for MetachainRPCModule {
.storage
.get_block_by_hash(&hash)
.map_or(Ok(None), |block| {
Ok(Some(RpcBlock::from_block_with_tx_and_base_fee(
Ok(Some(RpcBlock::from_block_with_tx(
block,
full_transactions.unwrap_or_default(),
self.handler.storage.get_base_fee(&hash).unwrap_or_default(),
)))
})
}
Expand Down Expand Up @@ -462,14 +461,9 @@ impl MetachainRPCServer for MetachainRPCModule {
.storage
.get_block_by_number(&block_number)
.map_or(Ok(None), |block| {
let tx_hash = &block.header.hash();
Ok(Some(RpcBlock::from_block_with_tx_and_base_fee(
Ok(Some(RpcBlock::from_block_with_tx(
block,
full_transactions.unwrap_or_default(),
self.handler
.storage
.get_base_fee(tx_hash)
.unwrap_or_default(),
)))
})
}
Expand Down

0 comments on commit dae87ce

Please sign in to comment.