github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/blockchain/merkle.go (about) 1 // Copyright (c) 2013-2014 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package blockchain 7 8 import ( 9 "math" 10 11 "github.com/BlockABC/godash/wire" 12 "github.com/BlockABC/godashutil" 13 ) 14 15 // nextPowerOfTwo returns the next highest power of two from a given number if 16 // it is not already a power of two. This is a helper function used during the 17 // calculation of a merkle tree. 18 func nextPowerOfTwo(n int) int { 19 // Return the number if it's already a power of 2. 20 if n&(n-1) == 0 { 21 return n 22 } 23 24 // Figure out and return the next power of two. 25 exponent := uint(math.Log2(float64(n))) + 1 26 return 1 << exponent // 2^exponent 27 } 28 29 // HashMerkleBranches takes two hashes, treated as the left and right tree 30 // nodes, and returns the hash of their concatenation. This is a helper 31 // function used to aid in the generation of a merkle tree. 32 func HashMerkleBranches(left *wire.ShaHash, right *wire.ShaHash) *wire.ShaHash { 33 // Concatenate the left and right nodes. 34 var sha [wire.HashSize * 2]byte 35 copy(sha[:wire.HashSize], left[:]) 36 copy(sha[wire.HashSize:], right[:]) 37 38 newSha := wire.DoubleSha256SH(sha[:]) 39 return &newSha 40 } 41 42 // BuildMerkleTreeStore creates a merkle tree from a slice of transactions, 43 // stores it using a linear array, and returns a slice of the backing array. A 44 // linear array was chosen as opposed to an actual tree structure since it uses 45 // about half as much memory. The following describes a merkle tree and how it 46 // is stored in a linear array. 47 // 48 // A merkle tree is a tree in which every non-leaf node is the hash of its 49 // children nodes. A diagram depicting how this works for bitcoin transactions 50 // where h(x) is a double sha256 follows: 51 // 52 // root = h1234 = h(h12 + h34) 53 // / \ 54 // h12 = h(h1 + h2) h34 = h(h3 + h4) 55 // / \ / \ 56 // h1 = h(tx1) h2 = h(tx2) h3 = h(tx3) h4 = h(tx4) 57 // 58 // The above stored as a linear array is as follows: 59 // 60 // [h1 h2 h3 h4 h12 h34 root] 61 // 62 // As the above shows, the merkle root is always the last element in the array. 63 // 64 // The number of inputs is not always a power of two which results in a 65 // balanced tree structure as above. In that case, parent nodes with no 66 // children are also zero and parent nodes with only a single left node 67 // are calculated by concatenating the left node with itself before hashing. 68 // Since this function uses nodes that are pointers to the hashes, empty nodes 69 // will be nil. 70 func BuildMerkleTreeStore(transactions []*godashutil.Tx) []*wire.ShaHash { 71 // Calculate how many entries are required to hold the binary merkle 72 // tree as a linear array and create an array of that size. 73 nextPoT := nextPowerOfTwo(len(transactions)) 74 arraySize := nextPoT*2 - 1 75 merkles := make([]*wire.ShaHash, arraySize) 76 77 // Create the base transaction shas and populate the array with them. 78 for i, tx := range transactions { 79 merkles[i] = tx.Sha() 80 } 81 82 // Start the array offset after the last transaction and adjusted to the 83 // next power of two. 84 offset := nextPoT 85 for i := 0; i < arraySize-1; i += 2 { 86 switch { 87 // When there is no left child node, the parent is nil too. 88 case merkles[i] == nil: 89 merkles[offset] = nil 90 91 // When there is no right child, the parent is generated by 92 // hashing the concatenation of the left child with itself. 93 case merkles[i+1] == nil: 94 newSha := HashMerkleBranches(merkles[i], merkles[i]) 95 merkles[offset] = newSha 96 97 // The normal case sets the parent node to the double sha256 98 // of the concatentation of the left and right children. 99 default: 100 newSha := HashMerkleBranches(merkles[i], merkles[i+1]) 101 merkles[offset] = newSha 102 } 103 offset++ 104 } 105 106 return merkles 107 }