Simplify mining header serialization (#1509)

This commit is contained in:
Ignotus Peverell 2018-09-11 20:31:05 -07:00 committed by GitHub
parent 97be1bf93e
commit a83404b22e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 50 deletions

View file

@ -403,10 +403,16 @@ impl Writeable for Signature {
/// Utility wrapper for an underlying byte Writer. Defines higher level methods /// Utility wrapper for an underlying byte Writer. Defines higher level methods
/// to write numbers, byte vectors, hashes, etc. /// to write numbers, byte vectors, hashes, etc.
struct BinWriter<'a> { pub struct BinWriter<'a> {
sink: &'a mut Write, sink: &'a mut Write,
} }
impl<'a> BinWriter<'a> {
pub fn new(write: &'a mut Write) -> BinWriter<'a> {
BinWriter{sink: write}
}
}
impl<'a> Writer for BinWriter<'a> { impl<'a> Writer for BinWriter<'a> {
fn serialization_mode(&self) -> SerializationMode { fn serialization_mode(&self) -> SerializationMode {
SerializationMode::Full SerializationMode::Full

View file

@ -16,7 +16,6 @@
//! them into a block and returns it. //! them into a block and returns it.
use chrono::prelude::{DateTime, NaiveDateTime, Utc}; use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use itertools::Itertools;
use rand::{self, Rng}; use rand::{self, Rng};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::thread; use std::thread;
@ -25,52 +24,12 @@ use std::time::Duration;
use chain; use chain;
use common::types::Error; use common::types::Error;
use core::core::verifier_cache::VerifierCache; use core::core::verifier_cache::VerifierCache;
use core::ser::{self, AsFixedBytes}; use core::{consensus, core, ser};
use core::{consensus, core};
use keychain::{ExtKeychain, Identifier, Keychain}; use keychain::{ExtKeychain, Identifier, Keychain};
use pool; use pool;
use util::{self, LOGGER}; use util::{self, LOGGER};
use wallet::{self, BlockFees}; use wallet::{self, BlockFees};
/// Serializer that outputs the pre-pow part of the header,
/// including the nonce (last 8 bytes) that can be sent off
/// to the miner to mutate at will
pub struct HeaderPrePowWriter {
pub pre_pow: Vec<u8>,
}
impl Default for HeaderPrePowWriter {
fn default() -> HeaderPrePowWriter {
HeaderPrePowWriter {
pre_pow: Vec::new(),
}
}
}
impl HeaderPrePowWriter {
pub fn as_hex_string(&self, include_nonce: bool) -> String {
let mut result = String::from(format!("{:02x}", self.pre_pow.iter().format("")));
if !include_nonce {
let l = result.len() - 16;
result.truncate(l);
}
result
}
}
impl ser::Writer for HeaderPrePowWriter {
fn serialization_mode(&self) -> ser::SerializationMode {
ser::SerializationMode::Full
}
fn write_fixed_bytes<T: AsFixedBytes>(&mut self, bytes_in: &T) -> Result<(), ser::Error> {
for i in 0..bytes_in.len() {
self.pre_pow.push(bytes_in.as_ref()[i])
}
Ok(())
}
}
// Ensure a block suitable for mining is built and returned // Ensure a block suitable for mining is built and returned
// If a wallet listener URL is not provided the reward will be "burnt" // If a wallet listener URL is not provided the reward will be "burnt"
// Warning: This call does not return until/unless a new block can be built // Warning: This call does not return until/unless a new block can be built

View file

@ -30,11 +30,11 @@ use common::stats::{StratumStats, WorkerStats};
use common::types::{StratumServerConfig, SyncState}; use common::types::{StratumServerConfig, SyncState};
use core::core::verifier_cache::VerifierCache; use core::core::verifier_cache::VerifierCache;
use core::core::Block; use core::core::Block;
use core::{global, pow}; use core::{global, pow, ser};
use keychain; use keychain;
use mining::mine_block; use mining::mine_block;
use pool; use pool;
use util::LOGGER; use util::{LOGGER, self};
// ---------------------------------------- // ----------------------------------------
// http://www.jsonrpc.org/specification // http://www.jsonrpc.org/specification
@ -266,15 +266,17 @@ impl StratumServer {
fn build_block_template(&self) -> JobTemplate { fn build_block_template(&self) -> JobTemplate {
let bh = self.current_block_versions.last().unwrap().header.clone(); let bh = self.current_block_versions.last().unwrap().header.clone();
// Serialize the block header into pre and post nonce strings // Serialize the block header into pre and post nonce strings
let mut pre_pow_writer = mine_block::HeaderPrePowWriter::default(); let mut header_buf = vec![];
bh.write_pre_pow(&mut pre_pow_writer).unwrap(); {
bh.pow.write_pre_pow(bh.version, &mut pre_pow_writer).unwrap(); let mut writer = ser::BinWriter::new(&mut header_buf);
let pre = pre_pow_writer.as_hex_string(false); bh.write_pre_pow(&mut writer).unwrap();
}
let pre_pow = util::to_hex(header_buf);
let job_template = JobTemplate { let job_template = JobTemplate {
height: bh.height, height: bh.height,
job_id: (self.current_block_versions.len() - 1) as u64, job_id: (self.current_block_versions.len() - 1) as u64,
difficulty: self.minimum_share_difficulty, difficulty: self.minimum_share_difficulty,
pre_pow: pre, pre_pow,
}; };
return job_template; return job_template;
} }