Skip to content

Commit

Permalink
basic get_pending_block
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Jul 21, 2023
1 parent aa189ce commit 286824f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
42 changes: 41 additions & 1 deletion lib/ain-evm/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ethereum::{BlockAny, TransactionAny};
use ethereum::{BlockAny, PartialHeader, TransactionAny};
use keccak_hash::H256;
use log::debug;
use primitive_types::U256;
Expand All @@ -10,6 +10,7 @@ use std::sync::Arc;
use thiserror::Error;

use crate::storage::{traits::BlockStorage, Storage};
use crate::transaction::SignedTx;

#[derive(Debug, Error)]
pub enum BlockError {
Expand Down Expand Up @@ -64,6 +65,45 @@ impl BlockService {
.unwrap_or_default()
}

pub fn get_pending_block(&self) -> Option<BlockAny> {
let raw_txs = ain_cpp_imports::get_pool_transactions().unwrap();
let pending_transactions: Vec<TransactionAny> = raw_txs
.into_iter()
.map(|raw_tx| SignedTx::try_from(raw_tx.as_str()))
.map(|signed_tx| signed_tx.unwrap().transaction)
.collect();

if let Some((hash, block_number)) = self.get_latest_block_hash_and_number() {
let pending_block = BlockAny::new(
PartialHeader {
parent_hash: hash,
number: block_number.saturating_add(U256::one()),
difficulty: Default::default(), // 0x0
nonce: Default::default(), // 0x0

// wip
gas_limit: Default::default(),
gas_used: Default::default(),
beneficiary: Default::default(),
timestamp: 0,
extra_data: vec![],

// none before block mined
state_root: Default::default(),
receipts_root: Default::default(),
logs_bloom: Default::default(),
mix_hash: Default::default(),
},
pending_transactions,
Vec::<ethereum::Header>::new(),
);

Some(pending_block)
} else {
None
}
}

pub fn connect_block(&self, block: BlockAny, base_fee: U256) {
self.storage.put_latest_block(Some(&block));
self.storage.put_block(&block);
Expand Down
23 changes: 4 additions & 19 deletions lib/ain-grpc/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,11 @@ impl MetachainRPCModule {

fn get_block(&self, id: Option<BlockNumber>) -> Option<BlockAny> {
match id.unwrap_or_default() {
BlockNumber::Hash {
hash,
..
} => {
self.handler
.storage
.get_block_by_hash(&hash)
},
BlockNumber::Num(id) => {
self.handler
.storage
.get_block_by_number(&U256::from(id))
},
_ => {
self.handler
.storage
.get_latest_block()
}
BlockNumber::Hash { hash, .. } => self.handler.storage.get_block_by_hash(&hash),
BlockNumber::Num(id) => self.handler.storage.get_block_by_number(&U256::from(id)),
BlockNumber::Pending => self.handler.block.get_pending_block(),
_ => self.handler.storage.get_latest_block(),
// BlockNumber::Earliest => todo!(),
// BlockNumber::Pending => todo!(),
// BlockNumber::Safe => todo!(),
// BlockNumber::Finalized => todo!(),
}
Expand Down

0 comments on commit 286824f

Please sign in to comment.