Minor API change to PoW verify, to use the size provided by the block header by default.

This commit is contained in:
Ignotus Peverell 2016-12-20 17:32:19 -08:00
parent 0702ab1829
commit c1340223de
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
4 changed files with 10 additions and 15 deletions

View file

@ -51,7 +51,7 @@ impl Writeable for TxProof {
impl Readable<TxProof> for TxProof {
fn read(reader: &mut Reader) -> Result<TxProof, ser::Error> {
let remainder = try!(Commitment::read (reader));
let remainder = try!(Commitment::read(reader));
let (sig, fee) = ser_multiread!(reader, read_vec, read_u64);
Ok(TxProof {
remainder: remainder,
@ -258,8 +258,7 @@ impl Writeable for Input {
/// an Input from a binary stream.
impl Readable<Input> for Input {
fn read(reader: &mut Reader) -> Result<Input, ser::Error> {
Hash::read(reader)
.map(|h| Input::BareInput { output: h })
Hash::read(reader).map(|h| Input::BareInput { output: h })
}
}

View file

@ -35,8 +35,8 @@ pub fn genesis() -> core::Block {
},
cuckoo_len: DEFAULT_SIZESHIFT,
target: MAX_TARGET,
utxo_merkle: [].hash(),
tx_merkle: [].hash(),
utxo_merkle: [].hash(),
tx_merkle: [].hash(),
nonce: 0,
pow: core::Proof::zero(), // TODO get actual PoW solution
},

View file

@ -92,20 +92,14 @@ pub fn verify(b: &Block) -> bool {
verify_size(b, b.header.cuckoo_len as u32)
}
/// Same as default verify function but uses the much easier Cuckoo20 (mostly
/// for tests).
pub fn verify20(b: &Block) -> bool {
verify_size(b, 20)
}
pub fn verify_size(b: &Block, sizeshift: u32) -> bool {
pub fn verify_size(b: &Block, cuckoo_sz: u32) -> bool {
let hash = PowHeader::from_block(b).hash();
// make sure the hash is smaller than our target before going into more
// expensive validation
if b.header.target < b.header.pow.to_target() {
return false;
}
Cuckoo::new(hash.to_slice(), sizeshift).verify(b.header.pow, EASINESS as u64)
Cuckoo::new(hash.to_slice(), cuckoo_sz).verify(b.header.pow, EASINESS as u64)
}
/// Runs a naive single-threaded proof of work computation over the provided
@ -165,6 +159,7 @@ mod test {
assert!(proof.to_target() < MAX_TARGET);
b.header.pow = proof;
b.header.nonce = nonce;
assert!(verify20(&b));
b.header.cuckoo_len = 20;
assert!(verify(&b));
}
}

View file

@ -237,7 +237,8 @@ impl<'a> Reader for BinReader<'a> {
let len = try!(self.read_u64());
self.read_fixed_bytes(len as usize)
}
/// Read limited variable size vector from the underlying Read. Expects a usize
/// Read limited variable size vector from the underlying Read. Expects a
/// usize
fn read_limited_vec(&mut self, max: usize) -> Result<Vec<u8>, Error> {
let len = cmp::min(max, try!(self.read_u64()) as usize);
self.read_fixed_bytes(len as usize)