Overview
csc207-01-grader@grinnell.edu
What’s a nonce?
Short for non-sense?
An integer that you use in a block to compute a hash.
You also use the hash of the previous block, an int that represents the data (for our assignment, the data are the money transfered between Alice/Anna and Bob) and an int that represents the block number.
The nonce helps us know whether or not a block is valid.
The nonce is a value that allows the hash to match a desired hash pattern.
This makes it harder to mess with the data.
How does mining relate to nonces?
Only certain nonces can make valid hashes for a particular set of data (prev block, money transferred, valid block).
We mine for nonces.
We mine for nonces by trying lots and lots of values until we find one that works. (a) random; (b) sequentially (Works = hash is valid)
What makes a hash valid? (At least for this HW?)
One that starts with three 0’s (hex digits)
What’s a byte?
A small integer, between 0 and 255.
Something that happens after you’ve been bit?
How much effort to mine, assuming three bytes?
1/256 for the first 0, 1/256 for the second 0, 1/256 for the third 0
About 256^3 prospective nonces must be generated
Or 16^3
Traditionally, we make it expensive to mine so that people can’t easily change the chain.
Why do we put nonces in a byte buffer?
Because we’re using MessageDigest objects to hash, and those reqire arrays of bytes. byte buffers provide the bridge.
What’s a hex value?
Four bits, 0 through 15.
How do we convert our byte to a a hex string?
String.format("%x", (int) b)
If you finish early, take the time to sit quietly. Alternately, get a drink of water or use the restroom.
What is an iterator?
An object that provides
next
andhasNext
.
Used to step through the values in a collection.
Through repeated calls to
next
Why do we use them?
Encapuslation: Client can see all values without knowing structure.
Generality: Client can write code to work with any collection.
What is an anonymous inner class?
A helper class that is unnamed.
Why do we use them?
Similar to the reasons we have anonymous helper functions: Use them in only one place, so there’s no need to name them.
Give access to the fields of the enclosing (outer) class.
How do we write them?
new Interface() { BODY; }
E.g.,
new Predicate<String>() {
boolean holds(String str) {
return string.length() < 5;
}
}
Writeup: Exercise 4