Skip to content

Commit

Permalink
chore: minor improve for rpc (paritytech#557)
Browse files Browse the repository at this point in the history
* chore: minor improve for rpc

* Fix libsecp256k1 crate name reference

Co-authored-by: Wei Tang <wei@that.world>
Co-authored-by: Wei Tang <accounts@that.world>
  • Loading branch information
3 people authored Feb 24, 2022
1 parent 9a15700 commit 3366a1d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion client/rpc-core/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub trait EthApi {
#[rpc(name = "eth_submitHashrate")]
fn submit_hashrate(&self, _: U256, _: H256) -> Result<bool>;

/// Introduced in EIP-1159 for getting information on the appropiate priority fee to use.
/// Introduced in EIP-1159 for getting information on the appropriate priority fee to use.
#[rpc(name = "eth_feeHistory")]
fn fee_history(
&self,
Expand Down
2 changes: 1 addition & 1 deletion client/rpc-core/src/types/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use serde::{Serialize, Serializer};

/// The result of an `eth_getWork` call: it differs based on an option
/// whether to send the block number.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Default)]
pub struct Work {
/// The proof-of-work hash.
pub pow_hash: H256,
Expand Down
39 changes: 17 additions & 22 deletions client/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2232,12 +2232,7 @@ where
}

fn work(&self) -> Result<Work> {
Ok(Work {
pow_hash: H256::default(),
seed_hash: H256::default(),
target: H256::default(),
number: None,
})
Ok(Work::default())
}

fn submit_work(&self, _: H64, _: H256, _: H256) -> Result<bool> {
Expand Down Expand Up @@ -2296,7 +2291,7 @@ where
// If the request includes reward percentiles, get them from the cache.
if let Some(ref requested_percentiles) = reward_percentiles {
let mut block_rewards = Vec::new();
// Resoltion is half a point. I.e. 1.0,1.5
// Resolution is half a point. I.e. 1.0,1.5
let resolution_per_percentile: f64 = 2.0;
// Get cached reward for each provided percentile.
for p in requested_percentiles {
Expand Down Expand Up @@ -2403,8 +2398,14 @@ where
C: Send + Sync + 'static,
B: BlockT<Hash = H256> + Send + Sync + 'static,
{
fn is_listening(&self) -> Result<bool> {
Ok(true)
fn version(&self) -> Result<String> {
let hash = self.client.info().best_hash;
Ok(self
.client
.runtime_api()
.chain_id(&BlockId::Hash(hash))
.map_err(|_| internal_err("fetch runtime chain id failed"))?
.to_string())
}

fn peer_count(&self) -> Result<PeerCount> {
Expand All @@ -2415,14 +2416,8 @@ where
})
}

fn version(&self) -> Result<String> {
let hash = self.client.info().best_hash;
Ok(self
.client
.runtime_api()
.chain_id(&BlockId::Hash(hash))
.map_err(|_| internal_err("fetch runtime chain id failed"))?
.to_string())
fn is_listening(&self) -> Result<bool> {
Ok(true)
}
}

Expand All @@ -2434,7 +2429,7 @@ pub struct Web3Api<B, C> {
impl<B, C> Web3Api<B, C> {
pub fn new(client: Arc<C>) -> Self {
Self {
client: client,
client,
_marker: PhantomData,
}
}
Expand Down Expand Up @@ -2542,7 +2537,7 @@ where
key,
FilterPoolItem {
last_poll: BlockNumber::Num(block_number),
filter_type: filter_type,
filter_type,
at_block: block_number,
},
);
Expand Down Expand Up @@ -2616,7 +2611,7 @@ where
key,
FilterPoolItem {
last_poll: BlockNumber::Num(next),
filter_type: pool_item.clone().filter_type,
filter_type: pool_item.filter_type.clone(),
at_block: pool_item.at_block,
},
);
Expand All @@ -2630,7 +2625,7 @@ where
key,
FilterPoolItem {
last_poll: BlockNumber::Num(block_number + 1),
filter_type: pool_item.clone().filter_type,
filter_type: pool_item.filter_type.clone(),
at_block: pool_item.at_block,
},
);
Expand Down Expand Up @@ -3161,7 +3156,7 @@ enum EthBlockDataCacheMessage<B: BlockT> {
},
}

/// Manage LRU cachse for block data and their transaction statuses.
/// Manage LRU caches for block data and their transaction statuses.
/// These are large and take a lot of time to fetch from the database.
/// Storing them in an LRU cache will allow to reduce database accesses
/// when many subsequent requests are related to the same blocks.
Expand Down
29 changes: 13 additions & 16 deletions client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,20 @@ impl EthDevSigner {
}
}

fn secret_key_address(secret: &libsecp256k1::SecretKey) -> H160 {
let public = libsecp256k1::PublicKey::from_secret_key(secret);
public_key_address(&public)
}

fn public_key_address(public: &libsecp256k1::PublicKey) -> H160 {
let mut res = [0u8; 64];
res.copy_from_slice(&public.serialize()[1..65]);
H160::from(H256::from_slice(Keccak256::digest(&res).as_slice()))
}

impl EthSigner for EthDevSigner {
fn accounts(&self) -> Vec<H160> {
self.keys
.iter()
.map(|secret| {
let public = libsecp256k1::PublicKey::from_secret_key(secret);
let mut res = [0u8; 64];
res.copy_from_slice(&public.serialize()[1..65]);

H160::from(H256::from_slice(Keccak256::digest(&res).as_slice()))
})
.collect()
self.keys.iter().map(secret_key_address).collect()
}

fn sign(
Expand All @@ -323,12 +325,7 @@ impl EthSigner for EthDevSigner {
let mut transaction = None;

for secret in &self.keys {
let key_address = {
let public = libsecp256k1::PublicKey::from_secret_key(secret);
let mut res = [0u8; 64];
res.copy_from_slice(&public.serialize()[1..65]);
H160::from(H256::from_slice(Keccak256::digest(&res).as_slice()))
};
let key_address = secret_key_address(secret);

if &key_address == address {
match message {
Expand Down

0 comments on commit 3366a1d

Please sign in to comment.