Documentation
¶
Index ¶
- Constants
- func CalcMerkleRoot(leaves []chainhash.Hash) chainhash.Hash
- func CalcMerkleRootInPlace(leaves []chainhash.Hash) chainhash.Hash
- func CalcWork(diffBits uint32) uint256.Uint256
- func CheckProofOfWork(blockHash *chainhash.Hash, diffBits uint32, powLimit *uint256.Uint256) error
- func CheckProofOfWorkRange(diffBits uint32, powLimit *uint256.Uint256) error
- func DiffBitsToUint256(bits uint32) (n uint256.Uint256, isNegative bool, overflows bool)
- func GenerateInclusionProof(leaves []chainhash.Hash, leafIndex uint32) []chainhash.Hash
- func HashToUint256(hash *chainhash.Hash) uint256.Uint256
- func Uint256ToDiffBits(n *uint256.Uint256) uint32
- func VerifyInclusionProof(root, leaf *chainhash.Hash, leafIndex uint32, proof []chainhash.Hash) bool
- type ErrorKind
- type RuleError
Constants ¶
const ( // ErrUnexpectedDifficulty indicates specified bits do not align with // the expected value either because it doesn't match the calculated // value based on difficulty rules or it is out of the valid range. ErrUnexpectedDifficulty = ErrorKind("ErrUnexpectedDifficulty") // ErrHighHash indicates the block does not hash to a value which is // lower than the required target difficultly. ErrHighHash = ErrorKind("ErrHighHash") )
These constants are used to identify a specific RuleError.
Variables ¶
This section is empty.
Functions ¶
func CalcMerkleRoot ¶
CalcMerkleRoot treats the provided slice of hashes as leaves of a merkle tree and returns the resulting merkle root.
A merkle tree is a tree in which every non-leaf node is the hash of its children nodes. A diagram depicting how this works for Decred transactions where h(x) is a blake256 hash follows:
root = h1234 = h(h12 + h34) / \ h12 = h(h1 + h2) h34 = h(h3 + h4) / \ / \ h1 = h(tx1) h2 = h(tx2) h3 = h(tx3) h4 = h(tx4)
The number of inputs is not always a power of two which results in a balanced tree structure as above. In that case, parent nodes with no children are also zero and parent nodes with only a single left node are calculated by concatenating the left node with itself before hashing.
func CalcMerkleRootInPlace ¶
CalcMerkleRootInPlace is an in-place version of CalcMerkleRoot that reuses the backing array of the provided slice to perform the calculation thereby preventing extra allocations. It is the caller's responsibility to ensure it is safe to mutate the entries in the provided slice.
The function internally appends an additional entry in the case the number of provided leaves is odd, so the caller may wish to pre-allocate space for one additional element in the backing array in that case to ensure it doesn't need to be reallocated to expand it.
For example:
allocLen := len(leaves) + len(leaves)&1 leaves := make([]chainhash.Hash, len(leaves), allocLen) // populate the leaves
See CalcMerkleRoot for more details on how the merkle root is calculated.
func CalcWork ¶
CalcWork calculates a work value from difficulty bits. Decred increases the difficulty for generating a block by decreasing the value which the generated hash must be less than. This difficulty target is stored in each block header using a compact representation as described in the documentation for DiffBitsToUint256. The main chain is selected by choosing the chain that has the most proof of work (highest difficulty). Since a lower target difficulty value equates to higher actual difficulty, the work value which will be accumulated must be the inverse of the difficulty. For legacy reasons, the result is zero when the difficulty is zero. Finally, to avoid really small floating point numbers, the result multiplies the numerator by 2^256 and adds 1 to the denominator.
func CheckProofOfWork ¶
CheckProofOfWork ensures the provided block hash is less than the target difficulty represented by given header bits and that said difficulty is in min/max range per the provided proof-of-work limit.
func CheckProofOfWorkRange ¶
CheckProofOfWorkRange ensures the provided target difficulty represented by the given header bits is in min/max range per the provided proof-of-work limit.
func DiffBitsToUint256 ¶
DiffBitsToUint256 converts the compact representation used to encode difficulty targets to an unsigned 256-bit integer. The representation is similar to IEEE754 floating point numbers.
Like IEEE754 floating point, there are three basic components: the sign, the exponent, and the mantissa. They are broken out as follows:
the most significant 8 bits represent the unsigned base 256 exponent
bit 23 (the 24th bit) represents the sign bit
the least significant 23 bits represent the mantissa
------------------------------------------------- | Exponent | Sign | Mantissa | ------------------------------------------------- | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] | -------------------------------------------------
The formula to calculate N is:
N = (-1^sign) * mantissa * 256^(exponent-3)
Note that this encoding is capable of representing negative numbers as well as numbers much larger than the maximum value of an unsigned 256-bit integer. However, it is only used in Decred to encode unsigned 256-bit integers which represent difficulty targets, so rather than using a much less efficient arbitrary precision big integer, this implementation uses an unsigned 256-bit integer and returns flags to indicate whether or not the encoding was for a negative value and/or overflows a uint256 to enable proper error detection and stay consistent with legacy code.
func GenerateInclusionProof ¶
GenerateInclusionProof treats the provided slice of hashes as leaves of a merkle tree and generates and returns a merkle tree inclusion proof for the given leaf index. The proof can be used to efficiently prove the leaf associated with a given leaf index is a member of the tree.
A merkle tree inclusion proof consists of the ceil(log2(x)) intermediate sibling hashes along the path from the target leaf to prove through the root node. The sibling hashes, along with the original leaf hash (and its original leaf index), can be used to recalculate the merkle root which, in turn, can be verified against a known good merkle root in order to prove the leaf is actually a member of the tree at that position.
For example, consider the following merkle tree:
root = h1234 = h(h12 || h34) / \ h12 = h(h1 || h2) h34 = h(h3 || h4) / \ / \ h1 h2 h3 h4
Further, consider the goal is to prove inclusion of h3 at the 0-based leaf index of 2. The proof will consist of the sibling hashes h4 and h12. On the other hand, if the goal were to prove inclusion of h2 at the 0-based leaf index of 1, the proof would consist of the sibling hashes h1 and h34.
Specifying a leaf index that is out of range will return nil.
func HashToUint256 ¶
HashToUint256 converts the provided hash to an unsigned 256-bit integer that can be used to perform math comparisons.
func Uint256ToDiffBits ¶
Uint256ToDiffBits converts a uint256 to a compact representation using an unsigned 32-bit integer. The compact representation only provides 23 bits of precision, so values larger than (2^23 - 1) only encode the most significant digits of the number. See DiffBitsToUint256 for details.
func VerifyInclusionProof ¶
func VerifyInclusionProof(root, leaf *chainhash.Hash, leafIndex uint32, proof []chainhash.Hash) bool
VerifyInclusionProof returns whether or not the given leaf hash, original leaf index, and inclusion proof result in recalculating a merkle root that matches the provided merkle root. See GenerateInclusionProof for details about the proof.
For example, consider a provided root hash denoted by "h1234o", a leaf hash to verify inclusion for denoted by "h2" with a leaf index of 2, and an inclusion proof consisting of hashes denoted by "h1o", and "h34o". The "o" here stands for original, as in the original hashes calculated while generating the proof.
These values would form the following merkle tree:
root = h1234 = h(h12 || h34o) / \ h12 = h(h1o || h2) h34o / \ h1o h2
The verification will succeed if the root of the new partial merkle tree, "h1234", matches the provided root hash "h1234o".
Types ¶
type ErrorKind ¶
type ErrorKind string
ErrorKind identifies a kind of error. It has full support for errors.Is and errors.As, so the caller can directly check against an error kind when determining the reason for an error.