Skip to content

Commit

Permalink
refactored part transactions to a function.
Browse files Browse the repository at this point in the history
  • Loading branch information
SurfingNerd committed Oct 30, 2024
1 parent 3e6e9c6 commit 65d32e8
Showing 1 changed file with 41 additions and 49 deletions.
90 changes: 41 additions & 49 deletions crates/ethcore/src/engines/hbbft/keygen_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use parking_lot::RwLock;
use std::{collections::BTreeMap, sync::Arc};
use types::ids::BlockId;

use crate::client::BlockChainClient;

pub struct KeygenTransactionSender {
last_keygen_mode: KeyGenMode,
keygen_mode_counter: u64,
Expand Down Expand Up @@ -177,13 +179,6 @@ impl KeygenTransactionSender {
ShouldSendKeyAnswer::NoNotThisKeyGenMode => return Err(KeyGenError::Unexpected),
ShouldSendKeyAnswer::NoWaiting => return Err(KeyGenError::Unexpected),
ShouldSendKeyAnswer::Yes => {

// the required gas values have been approximated by
// experimenting and it's a very rough estimation.
// it can be further fine tuned to be just above the real consumption.
// ACKs require much more gas,
// and usually run into the gas limit problems.
let gas: usize = failure_pub_keys.len() * 800 + 100_000;

let serialized_part = match bincode::serialize(&failure_pub_keys) {
Ok(part) => part,
Expand All @@ -192,22 +187,9 @@ impl KeygenTransactionSender {
return Err(KeyGenError::Unexpected);
}
};

let nonce = full_client.nonce(&address, BlockId::Latest).unwrap();

let part_transaction =
TransactionRequest::call(*KEYGEN_HISTORY_ADDRESS, serialized_part)
.gas(U256::from(gas))
.nonce(nonce)
.gas_price(U256::from(10000000000u64));
full_client
.transact_silently(part_transaction)
.map_err(|e| {
warn!(target:"engine", "could not transact_silently: {:?}", e);
CallError::ReturnValueInvalid
})?;

trace!(target:"engine", "PART Transaction send for moving forward key gen phase with nonce: {}", nonce);

send_part_transaction(full_client, client, &address, upcoming_epoch, serialized_part)?;
trace!(target:"engine", "PART Transaction send for moving forward key gen phase");
return Ok(());
},
}
Expand All @@ -229,40 +211,16 @@ impl KeygenTransactionSender {
// Check if we already sent our part.
match self.should_send_part(client, &address)? {
ShouldSendKeyAnswer::Yes => {

let serialized_part = match bincode::serialize(&part_data) {
Ok(part) => part,
Err(e) => {
warn!(target:"engine", "could not serialize part: {:?}", e);
return Err(KeyGenError::Unexpected);
}
};
let serialized_part_len = serialized_part.len();
let current_round = get_current_key_gen_round(client)?;
let write_part_data = key_history_contract::functions::write_part::call(
upcoming_epoch,
current_round,
serialized_part,
);

// the required gas values have been approximated by
// experimenting and it's a very rough estimation.
// it can be further fine tuned to be just above the real consumption.
// ACKs require much more gas,
// and usually run into the gas limit problems.
let gas: usize = serialized_part_len * 800 + 100_000;

let part_transaction =
TransactionRequest::call(*KEYGEN_HISTORY_ADDRESS, write_part_data.0)
.gas(U256::from(gas))
.nonce(full_client.nonce(&address, BlockId::Latest).unwrap())
.gas_price(U256::from(10000000000u64));
full_client
.transact_silently(part_transaction)
.map_err(|e| {
warn!(target:"engine", "could not transact_silently: {:?}", e);
CallError::ReturnValueInvalid
})?;

send_part_transaction(full_client, client, &address, upcoming_epoch, serialized_part)?;
trace!(target:"engine", "PART Transaction send.");
return Ok(());
}
Expand Down Expand Up @@ -341,3 +299,37 @@ impl KeygenTransactionSender {
Ok(())
}
}


fn send_part_transaction(full_client: &dyn BlockChainClient, client: &dyn EngineClient, mining_address: &Address, upcoming_epoch: U256 , data: Vec<u8>) -> Result<(), KeyGenError> {

// the required gas values have been approximated by
// experimenting and it's a very rough estimation.
// it can be further fine tuned to be just above the real consumption.
// ACKs require much more gas,
// and usually run into the gas limit problems.
let gas: usize = data.len() * 800 + 100_000;

let nonce = full_client.nonce(&mining_address, BlockId::Latest).unwrap();
let current_round = get_current_key_gen_round(client)?;
let write_part_data = key_history_contract::functions::write_part::call(
upcoming_epoch,
current_round,
data,
);

let part_transaction =
TransactionRequest::call(*KEYGEN_HISTORY_ADDRESS, write_part_data.0)
.gas(U256::from(gas))
.nonce(nonce)
.gas_price(U256::from(10000000000u64));
full_client
.transact_silently(part_transaction)
.map_err(|e| {
warn!(target:"engine", "could not transact_silently: {:?}", e);
CallError::ReturnValueInvalid
})?;

return Ok(());

}

0 comments on commit 65d32e8

Please sign in to comment.