--author and --extra-data options. Fixed null parent-hash.

This commit is contained in:
Gav Wood
2016-03-01 16:58:14 +01:00
parent 1eca9acffb
commit 48df869202
6 changed files with 95 additions and 18 deletions

View File

@@ -182,6 +182,7 @@ impl<'x> OpenBlock<'x> {
r.block.base.header.set_author(author);
r.block.base.header.set_extra_data(extra_data);
r.block.base.header.set_timestamp_now(parent.timestamp());
r.block.base.header.set_parent_hash(parent.hash());
engine.populate_from_parent(&mut r.block.base.header, parent);
engine.on_new_block(&mut r.block);
@@ -308,10 +309,15 @@ impl ClosedBlock {
pub fn try_seal(self, engine: &Engine, seal: Vec<Bytes>) -> Result<SealedBlock, ClosedBlock> {
let mut s = self;
s.block.base.header.set_seal(seal);
match engine.verify_block_basic(&s.block.base.header, None).is_err() || engine.verify_block_unordered(&s.block.base.header, None).is_err() {
false => Err(s),
true => Ok(SealedBlock { block: s.block, uncle_bytes: s.uncle_bytes }),
if let Err(e) = engine.verify_block_basic(&s.block.base.header, None) {
debug!("Failed to try_seal: {:?}", e);
return Err(s);
}
if let Err(e) = engine.verify_block_unordered(&s.block.base.header, None) {
debug!("Failed to try_seal: {:?}", e);
return Err(s);
}
Ok(SealedBlock { block: s.block, uncle_bytes: s.uncle_bytes })
}
/// Drop this object and return the underlieing database.

View File

@@ -196,6 +196,8 @@ pub struct Client {
// for sealing...
sealing_block: Mutex<Option<ClosedBlock>>,
author: RwLock<Address>,
extra_data: RwLock<Bytes>,
}
const HISTORY: u64 = 1000;
@@ -233,6 +235,8 @@ impl Client {
import_lock: Mutex::new(()),
panic_handler: panic_handler,
sealing_block: Mutex::new(None),
author: RwLock::new(Address::new()),
extra_data: RwLock::new(Vec::new()),
}))
}
@@ -364,7 +368,7 @@ impl Client {
}
if self.chain_info().best_block_hash != original_best {
self.new_chain_head();
self.prepare_sealing();
}
imported
@@ -414,27 +418,55 @@ impl Client {
}
}
/// New chain head event.
pub fn new_chain_head(&self) {
/// Set the author that we will seal blocks as.
pub fn author(&self) -> Address {
self.author.read().unwrap().clone()
}
/// Set the author that we will seal blocks as.
pub fn set_author(&self, author: Address) {
*self.author.write().unwrap() = author;
}
/// Set the extra_data that we will seal blocks wuth.
pub fn extra_data(&self) -> Bytes {
self.extra_data.read().unwrap().clone()
}
/// Set the extra_data that we will seal blocks with.
pub fn set_extra_data(&self, extra_data: Bytes) {
*self.extra_data.write().unwrap() = extra_data;
}
/// New chain head event. Restart mining operation.
pub fn prepare_sealing(&self) {
let h = self.chain.read().unwrap().best_block_hash();
debug!("New best block: #{}: {}", self.chain.read().unwrap().best_block_number(), h);
let b = OpenBlock::new(
self.engine.deref().deref(),
self.state_db.lock().unwrap().clone(),
match self.chain.read().unwrap().block_header(&h) { Some(ref x) => x, None => {return;} },
self.build_last_hashes(h.clone()),
x!("0037a6b811ffeb6e072da21179d11b1406371c63"),
b"Parity".to_vec()
self.build_last_hashes(h),
self.author(),
self.extra_data()
);
// TODO: push uncles.
// TODO: push transactions.
let b = b.close();
debug!("Sealing: hash={}, diff={}, number={}", b.hash(), b.block().header().difficulty(), b.block().header().number());
trace!("Sealing: number={}, hash={}, diff={}", b.hash(), b.block().header().difficulty(), b.block().header().number());
debug!("Header: {:?}", b.block().header());
*self.sealing_block.lock().unwrap() = Some(b);
}
/// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock.
pub fn sealing_block(&self) -> &Mutex<Option<ClosedBlock>> { &self.sealing_block }
pub fn sealing_block(&self) -> &Mutex<Option<ClosedBlock>> {
if self.sealing_block.lock().unwrap().is_none() {
self.prepare_sealing();
}
&self.sealing_block
}
/// Submit `seal` as a valid solution for the header of `pow_hash`.
/// Will check the seal, but not actually insert the block into the chain.
pub fn submit_seal(&self, pow_hash: H256, seal: Vec<Bytes>) -> Result<(), Error> {
let mut maybe_b = self.sealing_block.lock().unwrap();
match *maybe_b {

View File

@@ -102,10 +102,12 @@ impl Header {
Self::default()
}
/// Get the number field of the header.
pub fn number(&self) -> BlockNumber { self.number }
/// Get the parent_hash field of the header.
pub fn parent_hash(&self) -> &H256 { &self.parent_hash }
/// Get the timestamp field of the header.
pub fn timestamp(&self) -> u64 { self.timestamp }
/// Get the number field of the header.
pub fn number(&self) -> BlockNumber { self.number }
/// Get the author field of the header.
pub fn author(&self) -> &Address { &self.author }
@@ -127,11 +129,13 @@ impl Header {
// TODO: seal_at, set_seal_at &c.
/// Set the number field of the header.
pub fn set_number(&mut self, a: BlockNumber) { self.number = a; self.note_dirty(); }
pub fn set_parent_hash(&mut self, a: H256) { self.parent_hash = a; self.note_dirty(); }
/// Set the timestamp field of the header.
pub fn set_timestamp(&mut self, a: u64) { self.timestamp = a; self.note_dirty(); }
/// Set the timestamp field of the header to the current time.
pub fn set_timestamp_now(&mut self, but_later_than: u64) { self.timestamp = max(now_utc().to_timespec().sec as u64, but_later_than + 1); self.note_dirty(); }
/// Set the number field of the header.
pub fn set_number(&mut self, a: BlockNumber) { self.number = a; self.note_dirty(); }
/// Set the author field of the header.
pub fn set_author(&mut self, a: Address) { if a != self.author { self.author = a; self.note_dirty(); } }