eth_block fetching RPCs
This commit is contained in:
@@ -411,6 +411,28 @@ impl HeaderChain {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a block's chain score.
|
||||
/// Returns nothing for non-canonical blocks.
|
||||
pub fn score(&self, id: BlockId) -> Option<U256> {
|
||||
let genesis_hash = self.genesis_hash();
|
||||
match id {
|
||||
BlockId::Earliest | BlockId::Number(0) => Some(self.genesis_header.difficulty()),
|
||||
BlockId::Hash(hash) if hash == genesis_hash => Some(self.genesis_header.difficulty()),
|
||||
BlockId::Hash(hash) => match self.block_header(BlockId::Hash(hash)) {
|
||||
Some(header) => self.candidates.read().get(&header.number())
|
||||
.and_then(|era| era.candidates.iter().find(|e| e.hash == hash))
|
||||
.map(|c| c.total_difficulty),
|
||||
None => None,
|
||||
},
|
||||
BlockId::Number(num) => {
|
||||
let candidates = self.candidates.read();
|
||||
if self.best_block.read().number < num { return None }
|
||||
candidates.get(&num).map(|era| era.candidates[0].total_difficulty)
|
||||
}
|
||||
BlockId::Latest | BlockId::Pending => Some(self.best_block.read().total_difficulty)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the best block's header.
|
||||
pub fn best_header(&self) -> encoded::Header {
|
||||
self.block_header(BlockId::Latest).expect("Header for best block always stored; qed")
|
||||
|
||||
@@ -31,7 +31,7 @@ use ethcore::service::ClientIoMessage;
|
||||
use ethcore::encoded;
|
||||
use io::IoChannel;
|
||||
|
||||
use util::{H256, Mutex, RwLock};
|
||||
use util::{H256, U256, Mutex, RwLock};
|
||||
use util::kvdb::{KeyValueDB, CompactionProfile};
|
||||
|
||||
use self::header_chain::{AncestryIter, HeaderChain};
|
||||
@@ -74,6 +74,9 @@ pub trait LightChainClient: Send + Sync {
|
||||
/// Get the best block header.
|
||||
fn best_block_header(&self) -> encoded::Header;
|
||||
|
||||
/// Get a block's chain score by ID.
|
||||
fn score(&self, id: BlockId) -> Option<U256>;
|
||||
|
||||
/// Get an iterator over a block and its ancestry.
|
||||
fn ancestry_iter<'a>(&'a self, start: BlockId) -> Box<Iterator<Item=encoded::Header> + 'a>;
|
||||
|
||||
@@ -199,6 +202,11 @@ impl Client {
|
||||
self.chain.best_header()
|
||||
}
|
||||
|
||||
/// Get a block's chain score.
|
||||
pub fn score(&self, id: BlockId) -> Option<U256> {
|
||||
self.chain.score(id)
|
||||
}
|
||||
|
||||
/// Get an iterator over a block and its ancestry.
|
||||
pub fn ancestry_iter(&self, start: BlockId) -> AncestryIter {
|
||||
self.chain.ancestry_iter(start)
|
||||
@@ -328,6 +336,10 @@ impl LightChainClient for Client {
|
||||
Client::best_block_header(self)
|
||||
}
|
||||
|
||||
fn score(&self, id: BlockId) -> Option<U256> {
|
||||
Client::score(self, id)
|
||||
}
|
||||
|
||||
fn ancestry_iter<'a>(&'a self, start: BlockId) -> Box<Iterator<Item=encoded::Header> + 'a> {
|
||||
Box::new(Client::ancestry_iter(self, start))
|
||||
}
|
||||
|
||||
@@ -199,6 +199,12 @@ impl Block {
|
||||
/// Decode to a full block.
|
||||
pub fn decode(&self) -> FullBlock { ::rlp::decode(&self.0) }
|
||||
|
||||
/// Decode the header.
|
||||
pub fn decode_header(&self) -> FullHeader { self.rlp().val_at(0) }
|
||||
|
||||
/// Clone the encoded header.
|
||||
pub fn header(&self) -> Header { Header(self.rlp().at(0).as_raw().to_vec()) }
|
||||
|
||||
/// Get the rlp of this block.
|
||||
#[inline]
|
||||
pub fn rlp(&self) -> Rlp {
|
||||
|
||||
Reference in New Issue
Block a user