Skip to content

Commit

Permalink
Add eth_accounts JSON RPC functions implementation (#4016)
Browse files Browse the repository at this point in the history
* add evm keys into the FullContext and NodeSecret

* implement eth_accounts endpoint

* update to the latest chain-libs

* fix clippy
  • Loading branch information
Mr-Leshiy authored Jun 29, 2022
1 parent 3756504 commit 3b9a81a
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 51 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion jcli/src/jcli_lib/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Utils {
pub fn exec(self) -> Result<(), Error> {
match self {
Utils::Bech32Convert(convert_args) => {
convert_prefix(convert_args.from_bech32, convert_args.new_hrp).map_err(|e| e)
convert_prefix(convert_args.from_bech32, convert_args.new_hrp)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion jormungandr-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ chain-core = { git = "https://github.com/input-output-hk/chain-libs.git", b
chain-crypto = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
chain-time = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master"}
chain-vote = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
chain-evm = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", optional = true }
cardano-legacy-address = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
typed-bytes = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
rand = "0.8"
Expand Down Expand Up @@ -43,4 +44,4 @@ serde_yaml = "0.8"
serde_json = "1.0"

[features]
evm = ["chain-impl-mockchain/evm"]
evm = ["chain-impl-mockchain/evm", "chain-evm"]
2 changes: 2 additions & 0 deletions jormungandr/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ pub struct FullContext {
pub transaction_task: MessageBox<TransactionMsg>,
pub leadership_logs: LeadershipLogs,
pub enclave: Enclave,
#[cfg(feature = "evm")]
pub evm_keys: Arc<Vec<chain_evm::util::Secret>>,
pub network_state: NetworkStateR,
#[cfg(feature = "prometheus-metrics")]
pub prometheus: Option<Arc<crate::metrics::backends::Prometheus>>,
Expand Down
16 changes: 11 additions & 5 deletions jormungandr/src/jrpc/eth_account/logic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use super::Error;
use crate::{
context::Context,
jrpc::eth_types::{block_number::BlockNumber, bytes::Bytes, number::Number},
jrpc::{
eth_types::{block_number::BlockNumber, bytes::Bytes, number::Number},
Error,
},
};
use chain_evm::ethereum_types::{H160, H256};

pub fn accounts(_context: &Context) -> Result<Vec<H160>, Error> {
// TODO implement
Ok(vec![H160::zero()])
pub fn accounts(context: &Context) -> Result<Vec<H160>, Error> {
Ok(context
.try_full()?
.evm_keys
.iter()
.map(|secret_key| secret_key.address())
.collect())
}

pub fn get_transaction_count(
Expand Down
3 changes: 0 additions & 3 deletions jormungandr/src/jrpc/eth_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use jsonrpsee_http_server::RpcModule;

mod logic;

#[derive(Debug, thiserror::Error)]
pub enum Error {}

pub fn eth_account_module(context: ContextLock) -> RpcModule<ContextLock> {
let mut module = RpcModule::new(context);

Expand Down
8 changes: 4 additions & 4 deletions jormungandr/src/jrpc/eth_block_info/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub async fn get_block_by_hash(
context: &Context,
) -> Result<Option<Block>, Error> {
let blockchain_tip = context.blockchain_tip()?.get_ref().await;
let gas_limit = blockchain_tip.ledger().evm_block_gas_limit();
let gas_price = blockchain_tip.ledger().evm_gas_price();
let gas_limit = blockchain_tip.ledger().get_evm_block_gas_limit();
let gas_price = blockchain_tip.ledger().get_evm_gas_price();
let block = context.blockchain()?.storage().get(hash.0.into())?;
Ok(block.map(|block| Block::build(block, full, gas_limit, gas_price)))
}
Expand All @@ -55,8 +55,8 @@ pub async fn get_block_by_number(
) -> Result<Option<Block>, Error> {
let blockchain = context.blockchain()?;
let blockchain_tip = context.blockchain_tip()?.get_ref().await;
let gas_limit = blockchain_tip.ledger().evm_block_gas_limit();
let gas_price = blockchain_tip.ledger().evm_gas_price();
let gas_limit = blockchain_tip.ledger().get_evm_block_gas_limit();
let gas_price = blockchain_tip.ledger().get_evm_gas_price();
Ok(
get_block_by_number_from_context(number, blockchain, blockchain_tip)?
.map(|block| Block::build(block, full, gas_limit, gas_price)),
Expand Down
4 changes: 2 additions & 2 deletions jormungandr/src/jrpc/eth_transaction/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub async fn get_transaction_by_block_hash_and_index(
context: &Context,
) -> Result<Option<Transaction>, Error> {
let blockchain_tip = context.blockchain_tip()?.get_ref().await;
let gas_price = blockchain_tip.ledger().evm_gas_price();
let gas_price = blockchain_tip.ledger().get_evm_gas_price();
let block = context.blockchain()?.storage().get(hash.0.into())?;
Ok(get_transaction_from_block_by_index(block, index, gas_price))
}
Expand All @@ -112,7 +112,7 @@ pub async fn get_transaction_by_block_number_and_index(
context: &Context,
) -> Result<Option<Transaction>, Error> {
let blockchain_tip = context.blockchain_tip()?.get_ref().await;
let gas_price = blockchain_tip.ledger().evm_gas_price();
let gas_price = blockchain_tip.ledger().get_evm_gas_price();
let blockchain = context.blockchain()?;
let block = get_block_by_number_from_context(number, blockchain, blockchain_tip).unwrap();
Ok(get_transaction_from_block_by_index(block, index, gas_price))
Expand Down
43 changes: 28 additions & 15 deletions jormungandr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,40 @@ fn start_services(bootstrapped_node: BootstrappedNode) -> Result<(), start_up::E
None
}
});
let leader_secret = bootstrapped_node
let node_secret = bootstrapped_node
.settings
.secret
.map::<Result<_, start_up::Error>, _>(|secret_path| {
let secret = secure::NodeSecret::load_from_file(secret_path.as_path())?;
if let (Some(leaders), Some(leader)) = (&bft_leaders, secret.bft()) {
let public_key = &leader.sig_key.to_public();
if !leaders.contains(public_key) {
tracing::warn!(
"node was started with a BFT secret key but the corresponding \
public key {} is not listed among consensus leaders",
public_key
);
}
};
Ok(Leader {
bft_leader: secret.bft(),
genesis_leader: secret.genesis(),
})
Ok(secret)
})
.transpose()?;

let leader_secret = node_secret.as_ref().map(|secret| {
if let (Some(leaders), Some(leader)) = (&bft_leaders, secret.bft()) {
let public_key = &leader.sig_key.to_public();
if !leaders.contains(public_key) {
tracing::warn!(
"node was started with a BFT secret key but the corresponding \
public key {} is not listed among consensus leaders",
public_key
);
}
};
Leader {
bft_leader: secret.bft(),
genesis_leader: secret.genesis(),
}
});
let enclave = Enclave::new(leader_secret);

#[cfg(feature = "evm")]
let evm_keys = Arc::new(
node_secret
.map(|secret| secret.evm_keys())
.unwrap_or_default(),
);

{
let logs = leadership_logs.clone();
let block_message = block_msgbox;
Expand Down Expand Up @@ -310,6 +321,8 @@ fn start_services(bootstrapped_node: BootstrappedNode) -> Result<(), start_up::E
transaction_task: fragment_msgbox,
topology_task: topology_msgbox,
leadership_logs,
#[cfg(feature = "evm")]
evm_keys,
enclave,
network_state,
#[cfg(feature = "prometheus-metrics")]
Expand Down
4 changes: 2 additions & 2 deletions jormungandr/src/rest/v0/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ pub async fn get_jor_address(context: &Context, evm_id_hex: &str) -> Result<Stri
.get_ref()
.await
.ledger()
.jormungandr_mapped_address(
.get_jormungandr_mapped_address(
&chain_evm::Address::from_str(evm_id_hex)
.map_err(|e| Error::AddressParseError(e.to_string()))?,
)
Expand All @@ -615,7 +615,7 @@ pub async fn get_evm_address(context: &Context, jor_id_hex: &str) -> Result<Opti
.get_ref()
.await
.ledger()
.evm_mapped_address(
.get_evm_mapped_address(
&PublicKey::<AccountAlg>::from_str(jor_id_hex)
.map_err(|e| Error::AddressParseError(e.to_string()))?
.into(),
Expand Down
14 changes: 12 additions & 2 deletions jormungandr/src/secure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pub struct OwnerKey(Identifier<Ed25519>);
/// Node Secret(s)
#[derive(Clone, Deserialize)]
pub struct NodeSecret {
pub bft: Option<Bft>,
pub genesis: Option<GenesisPraos>,
bft: Option<Bft>,
genesis: Option<GenesisPraos>,
#[cfg(feature = "evm")]
evm_keys: Vec<chain_evm::ethereum_types::H256>,
}

/// Node Secret's Public parts
Expand Down Expand Up @@ -68,4 +70,12 @@ impl NodeSecret {
vrf_key: genesis.vrf_key.into_secret_key(),
})
}

#[cfg(feature = "evm")]
pub fn evm_keys(&self) -> Vec<chain_evm::util::Secret> {
self.evm_keys
.iter()
.map(chain_evm::util::Secret::from_hash)
.collect()
}
}
1 change: 0 additions & 1 deletion jormungandr/src/settings/start/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use tracing::level_filters::LevelFilter;
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
#[serde(default)]
pub secret_file: Option<PathBuf>,
pub storage: Option<PathBuf>,
pub log: Option<ConfigLogSettings>,
Expand Down

0 comments on commit 3b9a81a

Please sign in to comment.