diff --git a/core/src/ser.rs b/core/src/ser.rs index 90af57a50..67d1c06a5 100644 --- a/core/src/ser.rs +++ b/core/src/ser.rs @@ -403,10 +403,16 @@ impl Writeable for Signature { /// Utility wrapper for an underlying byte Writer. Defines higher level methods /// to write numbers, byte vectors, hashes, etc. -struct BinWriter<'a> { +pub struct BinWriter<'a> { 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> { fn serialization_mode(&self) -> SerializationMode { SerializationMode::Full diff --git a/servers/src/mining/mine_block.rs b/servers/src/mining/mine_block.rs index 123459494..c7509954e 100644 --- a/servers/src/mining/mine_block.rs +++ b/servers/src/mining/mine_block.rs @@ -16,7 +16,6 @@ //! them into a block and returns it. use chrono::prelude::{DateTime, NaiveDateTime, Utc}; -use itertools::Itertools; use rand::{self, Rng}; use std::sync::{Arc, RwLock}; use std::thread; @@ -25,52 +24,12 @@ use std::time::Duration; use chain; use common::types::Error; use core::core::verifier_cache::VerifierCache; -use core::ser::{self, AsFixedBytes}; -use core::{consensus, core}; +use core::{consensus, core, ser}; use keychain::{ExtKeychain, Identifier, Keychain}; use pool; use util::{self, LOGGER}; 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, -} - -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(&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 // 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 diff --git a/servers/src/mining/stratumserver.rs b/servers/src/mining/stratumserver.rs index 76f9941fb..fbb850942 100644 --- a/servers/src/mining/stratumserver.rs +++ b/servers/src/mining/stratumserver.rs @@ -30,11 +30,11 @@ use common::stats::{StratumStats, WorkerStats}; use common::types::{StratumServerConfig, SyncState}; use core::core::verifier_cache::VerifierCache; use core::core::Block; -use core::{global, pow}; +use core::{global, pow, ser}; use keychain; use mining::mine_block; use pool; -use util::LOGGER; +use util::{LOGGER, self}; // ---------------------------------------- // http://www.jsonrpc.org/specification @@ -266,15 +266,17 @@ impl StratumServer { fn build_block_template(&self) -> JobTemplate { let bh = self.current_block_versions.last().unwrap().header.clone(); // Serialize the block header into pre and post nonce strings - let mut pre_pow_writer = mine_block::HeaderPrePowWriter::default(); - bh.write_pre_pow(&mut pre_pow_writer).unwrap(); - bh.pow.write_pre_pow(bh.version, &mut pre_pow_writer).unwrap(); - let pre = pre_pow_writer.as_hex_string(false); + let mut header_buf = vec![]; + { + let mut writer = ser::BinWriter::new(&mut header_buf); + bh.write_pre_pow(&mut writer).unwrap(); + } + let pre_pow = util::to_hex(header_buf); let job_template = JobTemplate { height: bh.height, job_id: (self.current_block_versions.len() - 1) as u64, difficulty: self.minimum_share_difficulty, - pre_pow: pre, + pre_pow, }; return job_template; }