Fixing minimal transaction queue price (#4204)

* Fixing minimal transaction queue price

* Fixing tests
This commit is contained in:
Tomasz Drwięga 2017-01-18 19:44:24 +01:00 committed by Arkadiy Paronyan
parent 73b80869f5
commit 24aec5191a
6 changed files with 46 additions and 19 deletions

View File

@ -250,7 +250,7 @@ impl TestBlockChainClient {
value: U256::from(100), value: U256::from(100),
data: "3331600055".from_hex().unwrap(), data: "3331600055".from_hex().unwrap(),
gas: U256::from(100_000), gas: U256::from(100_000),
gas_price: U256::one(), gas_price: U256::from(200_000_000_000u64),
nonce: U256::zero() nonce: U256::zero()
}; };
let signed_tx = tx.sign(keypair.secret(), None); let signed_tx = tx.sign(keypair.secret(), None);
@ -316,11 +316,11 @@ impl TestBlockChainClient {
value: U256::from(100), value: U256::from(100),
data: "3331600055".from_hex().unwrap(), data: "3331600055".from_hex().unwrap(),
gas: U256::from(100_000), gas: U256::from(100_000),
gas_price: U256::one(), gas_price: U256::from(20_000_000_000u64),
nonce: U256::zero() nonce: U256::zero()
}; };
let signed_tx = tx.sign(keypair.secret(), None); let signed_tx = tx.sign(keypair.secret(), None);
self.set_balance(signed_tx.sender(), 10_000_000.into()); self.set_balance(signed_tx.sender(), U256::from(10_000_000_000_000_000_000u64));
let res = self.miner.import_external_transactions(self, vec![signed_tx.into()]); let res = self.miner.import_external_transactions(self, vec![signed_tx.into()]);
let res = res.into_iter().next().unwrap().expect("Successful import"); let res = res.into_iter().next().unwrap().expect("Successful import");
assert_eq!(res, TransactionImportResult::Current); assert_eq!(res, TransactionImportResult::Current);

View File

@ -303,16 +303,6 @@ impl Miner {
#[cfg_attr(feature="dev", allow(match_same_arms))] #[cfg_attr(feature="dev", allow(match_same_arms))]
/// Prepares new block for sealing including top transactions from queue. /// Prepares new block for sealing including top transactions from queue.
fn prepare_block(&self, chain: &MiningBlockChainClient) -> (ClosedBlock, Option<H256>) { fn prepare_block(&self, chain: &MiningBlockChainClient) -> (ClosedBlock, Option<H256>) {
{
trace!(target: "miner", "prepare_block: recalibrating...");
let txq = self.transaction_queue.clone();
self.gas_pricer.lock().recalibrate(move |price| {
trace!(target: "miner", "prepare_block: Got gas price! {}", price);
txq.lock().set_minimal_gas_price(price);
});
trace!(target: "miner", "prepare_block: done recalibration.");
}
let _timer = PerfTimer::new("prepare_block"); let _timer = PerfTimer::new("prepare_block");
let chain_info = chain.chain_info(); let chain_info = chain.chain_info();
let (transactions, mut open_block, original_work_hash) = { let (transactions, mut open_block, original_work_hash) = {
@ -427,6 +417,16 @@ impl Miner {
(block, original_work_hash) (block, original_work_hash)
} }
/// Asynchronously updates minimal gas price for transaction queue
pub fn recalibrate_minimal_gas_price(&self) {
debug!(target: "miner", "minimal_gas_price: recalibrating...");
let txq = self.transaction_queue.clone();
self.gas_pricer.lock().recalibrate(move |price| {
debug!(target: "miner", "minimal_gas_price: Got gas price! {}", price);
txq.lock().set_minimal_gas_price(price);
});
}
/// Check is reseal is allowed and necessary. /// Check is reseal is allowed and necessary.
fn requires_reseal(&self, best_block: BlockNumber) -> bool { fn requires_reseal(&self, best_block: BlockNumber) -> bool {
let has_local_transactions = self.transaction_queue.lock().has_local_pending_transactions(); let has_local_transactions = self.transaction_queue.lock().has_local_pending_transactions();
@ -1120,6 +1120,9 @@ impl MinerService for Miner {
// First update gas limit in transaction queue // First update gas limit in transaction queue
self.update_gas_limit(chain); self.update_gas_limit(chain);
// Update minimal gas price
self.recalibrate_minimal_gas_price();
// Then import all transactions... // Then import all transactions...
{ {

View File

@ -532,22 +532,32 @@ impl Configuration {
} }
fn gas_pricer_config(&self) -> Result<GasPricerConfig, String> { fn gas_pricer_config(&self) -> Result<GasPricerConfig, String> {
fn wei_per_gas(usd_per_tx: f32, usd_per_eth: f32) -> U256 {
let wei_per_usd: f32 = 1.0e18 / usd_per_eth;
let gas_per_tx: f32 = 21000.0;
let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx;
U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap()
}
if let Some(d) = self.args.flag_gasprice.as_ref() { if let Some(d) = self.args.flag_gasprice.as_ref() {
return Ok(GasPricerConfig::Fixed(to_u256(d)?)); return Ok(GasPricerConfig::Fixed(to_u256(d)?));
} }
let usd_per_tx = to_price(&self.args.flag_usd_per_tx)?; let usd_per_tx = to_price(&self.args.flag_usd_per_tx)?;
if "auto" == self.args.flag_usd_per_eth.as_str() { if "auto" == self.args.flag_usd_per_eth.as_str() {
// Just a very rough estimate to avoid accepting
// ZGP transactions before the price is fetched
// if user does not want it.
let last_known_usd_per_eth = 10.0;
return Ok(GasPricerConfig::Calibrated { return Ok(GasPricerConfig::Calibrated {
initial_minimum: wei_per_gas(usd_per_tx, last_known_usd_per_eth),
usd_per_tx: usd_per_tx, usd_per_tx: usd_per_tx,
recalibration_period: to_duration(self.args.flag_price_update_period.as_str())?, recalibration_period: to_duration(self.args.flag_price_update_period.as_str())?,
}); });
} }
let usd_per_eth = to_price(&self.args.flag_usd_per_eth)?; let usd_per_eth = to_price(&self.args.flag_usd_per_eth)?;
let wei_per_usd: f32 = 1.0e18 / usd_per_eth; let wei_per_gas = wei_per_gas(usd_per_tx, usd_per_eth);
let gas_per_tx: f32 = 21000.0;
let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx;
info!( info!(
"Using a fixed conversion rate of Ξ1 = {} ({} wei/gas)", "Using a fixed conversion rate of Ξ1 = {} ({} wei/gas)",
@ -555,7 +565,7 @@ impl Configuration {
Colour::Yellow.bold().paint(format!("{}", wei_per_gas)) Colour::Yellow.bold().paint(format!("{}", wei_per_gas))
); );
Ok(GasPricerConfig::Fixed(U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap())) Ok(GasPricerConfig::Fixed(wei_per_gas))
} }
fn extra_data(&self) -> Result<Bytes, String> { fn extra_data(&self) -> Result<Bytes, String> {

View File

@ -192,14 +192,25 @@ impl Default for AccountsConfig {
pub enum GasPricerConfig { pub enum GasPricerConfig {
Fixed(U256), Fixed(U256),
Calibrated { Calibrated {
initial_minimum: U256,
usd_per_tx: f32, usd_per_tx: f32,
recalibration_period: Duration, recalibration_period: Duration,
} }
} }
impl GasPricerConfig {
pub fn initial_min(&self) -> U256 {
match *self {
GasPricerConfig::Fixed(ref min) => min.clone(),
GasPricerConfig::Calibrated { ref initial_minimum, .. } => initial_minimum.clone(),
}
}
}
impl Default for GasPricerConfig { impl Default for GasPricerConfig {
fn default() -> Self { fn default() -> Self {
GasPricerConfig::Calibrated { GasPricerConfig::Calibrated {
initial_minimum: 11904761856u64.into(),
usd_per_tx: 0.0025f32, usd_per_tx: 0.0025f32,
recalibration_period: Duration::from_secs(3600), recalibration_period: Duration::from_secs(3600),
} }
@ -210,7 +221,7 @@ impl Into<GasPricer> for GasPricerConfig {
fn into(self) -> GasPricer { fn into(self) -> GasPricer {
match self { match self {
GasPricerConfig::Fixed(u) => GasPricer::Fixed(u), GasPricerConfig::Fixed(u) => GasPricer::Fixed(u),
GasPricerConfig::Calibrated { usd_per_tx, recalibration_period } => { GasPricerConfig::Calibrated { usd_per_tx, recalibration_period, .. } => {
GasPricer::new_calibrated(GasPriceCalibratorOptions { GasPricer::new_calibrated(GasPriceCalibratorOptions {
usd_per_tx: usd_per_tx, usd_per_tx: usd_per_tx,
recalibration_period: recalibration_period, recalibration_period: recalibration_period,

View File

@ -233,12 +233,15 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R
let account_provider = Arc::new(prepare_account_provider(&cmd.spec, &cmd.dirs, &spec.data_dir, cmd.acc_conf, &passwords)?); let account_provider = Arc::new(prepare_account_provider(&cmd.spec, &cmd.dirs, &spec.data_dir, cmd.acc_conf, &passwords)?);
// create miner // create miner
let initial_min_gas_price = cmd.gas_pricer.initial_min();
let miner = Miner::new(cmd.miner_options, cmd.gas_pricer.into(), &spec, Some(account_provider.clone())); let miner = Miner::new(cmd.miner_options, cmd.gas_pricer.into(), &spec, Some(account_provider.clone()));
miner.set_author(cmd.miner_extras.author); miner.set_author(cmd.miner_extras.author);
miner.set_gas_floor_target(cmd.miner_extras.gas_floor_target); miner.set_gas_floor_target(cmd.miner_extras.gas_floor_target);
miner.set_gas_ceil_target(cmd.miner_extras.gas_ceil_target); miner.set_gas_ceil_target(cmd.miner_extras.gas_ceil_target);
miner.set_extra_data(cmd.miner_extras.extra_data); miner.set_extra_data(cmd.miner_extras.extra_data);
miner.set_transactions_limit(cmd.miner_extras.transactions_limit); miner.set_transactions_limit(cmd.miner_extras.transactions_limit);
miner.set_minimal_gas_price(initial_min_gas_price);
miner.recalibrate_minimal_gas_price();
let engine_signer = cmd.miner_extras.engine_signer; let engine_signer = cmd.miner_extras.engine_signer;
if engine_signer != Default::default() { if engine_signer != Default::default() {

View File

@ -2782,7 +2782,7 @@ mod tests {
for h in &[good_blocks[0], retracted_blocks[0]] { for h in &[good_blocks[0], retracted_blocks[0]] {
let block = client.block(BlockId::Hash(*h)).unwrap(); let block = client.block(BlockId::Hash(*h)).unwrap();
let sender = sender(&block.transactions()[0]);; let sender = sender(&block.transactions()[0]);;
client.set_balance(sender, U256::from(1_000_000_000)); client.set_balance(sender, U256::from(10_000_000_000_000_000_000u64));
client.set_nonce(sender, U256::from(0)); client.set_nonce(sender, U256::from(0));
} }