Skip to content

Wallet Balance + Difficulty

David Vorick requested to merge scan into master

Genesis state now includes timestamps, and difficulty.

I added a current path map to the state. I could have used a slice instead of a map, but since we'll be appending and stuff to it a lot, idk I figured the internals of a map would be more efficient than anything I created myself. It's functionally the same thing as a slice.

I added some functionality to the wallet so that it can scan the ConsensusState and find all of the outputs that it knows how to spend, creating a known volume of spendable coins. I also added the groundwork for spending those coins - you add inputs until you've got enough to send them somewhere, and then you create 1 output for yourself (you get the remainder coins), 1 output for the person you are sending coins to.

The biggest thing here is the obnoxious code for adjusting the difficulty. It compares the amount of time it took to create the most recent 5000 blocks to the expected amount of time to create 5000 blocks, and creates a ratio based on that. The ratio is how much the difficulty needs to be adjusted. Since we adjust the difficulty every block, we divide that ratio by 5000, and this gives us a smoothly adjusting difficulty function.

BUT, all this adjusting needs to happen at an integer level so that the behavior is well defined. No floating points are allowed in consensus technology, because someone always gets the floating points wrong. SO, I start by multiplying the amount of time stuff took by 1 billion. This gives us some extra precision. Then we do normal integer division, and end up with a ratio that's 1 billion times to large. We convert the target difficulty to a big int, multiply it by the ratio, and then divide the whole thing by 1 billion.

The result should be something that's well defined. I think that we could just as easily multiply stuff by 1 trillion if we wanted, int64 gives us a lot of room to work with. I'm not really sure how to use big ints, and my web browser stopped working (>.< webgtk failed to emerge), so I was trying to figure things out on my phone and it wasn't working well.

I added some constants related to difficulty adjustments.

Merge request reports