MCPcopy Create free account
hub / github.com/CovenantSQL/CovenantSQL / buildMerkleTreeStore

Function buildMerkleTreeStore

merkle/merkletrie_test.go:67–108  ·  view source on GitHub ↗
(hashes []*hash.Hash)

Source from the content-addressed store, hash-verified

65}
66
67func buildMerkleTreeStore(hashes []*hash.Hash) []*hash.Hash {
68 // Calculate how many entries are required to hold the binary merkle
69 // tree as a linear array and create an array of that size.
70 if hashes == nil || len(hashes) == 0 {
71 hashes = []*hash.Hash{{}}
72 }
73
74 nextPoT := nextPowerOfTwo(len(hashes))
75 arraySize := nextPoT*2 - 1
76 merkles := make([]*hash.Hash, arraySize)
77
78 // Create the base transaction hashes and populate the array with them.
79 for i, h := range hashes {
80 merkles[i] = h
81 }
82
83 // Start the array offset after the last transaction and adjusted to the
84 // next power of two.
85 offset := nextPoT
86 for i := 0; i < arraySize-1; i += 2 {
87 switch {
88 // When there is no left child node, the parent is nil too.
89 case merkles[i] == nil:
90 merkles[offset] = nil
91
92 // When there is no right child, the parent is generated by
93 // hashing the concatenation of the left child with itself.
94 case merkles[i+1] == nil:
95 newHash := mergeHash(merkles[i], merkles[i])
96 merkles[offset] = newHash
97
98 // The normal case sets the parent node to the double sha256
99 // of the concatentation of the left and right children.
100 default:
101 newHash := mergeHash(merkles[i], merkles[i+1])
102 merkles[offset] = newHash
103 }
104 offset++
105 }
106
107 return merkles
108}

Callers 1

TestNewMerkleFunction · 0.85

Calls 2

nextPowerOfTwoFunction · 0.85
mergeHashFunction · 0.85

Tested by

no test coverage detected