func (b *DirectoryBlock) BuildKeyMerkleRoot() (keyMR interfaces.IHash, err error) { // Create the Entry Block Key Merkle Root from the hash of Header and the Body Merkle Root hashes := make([]interfaces.IHash, 0, 2) binaryEBHeader, _ := b.GetHeader().MarshalBinary() hashes = append(hashes, primitives.Sha(binaryEBHeader)) hashes = append(hashes, b.GetHeader().GetBodyMR()) merkle := primitives.BuildMerkleTreeStore(hashes) keyMR = merkle[len(merkle)-1] // MerkleRoot is not marshalized in Dir Block b.KeyMR = keyMR return keyMR, nil }
func (b *DirectoryBlock) BuildBodyMR() (mr interfaces.IHash, err error) { hashes := make([]interfaces.IHash, len(b.GetDBEntries())) for i, entry := range b.GetDBEntries() { data, _ := entry.MarshalBinary() hashes[i] = primitives.Sha(data) } if len(hashes) == 0 { hashes = append(hashes, primitives.Sha(nil)) } merkle := primitives.BuildMerkleTreeStore(hashes) return merkle[len(merkle)-1], nil }
func (b *DirectoryBlock) BuildKeyMerkleRoot() (keyMR interfaces.IHash, err error) { // Create the Entry Block Key Merkle Root from the hash of Header and the Body Merkle Root hashes := make([]interfaces.IHash, 0, 2) bodyKeyMR := b.BodyKeyMR() //This needs to be called first to build the header properly!! headerHash, err := b.HeaderHash() if err != nil { return nil, err } hashes = append(hashes, headerHash) hashes = append(hashes, bodyKeyMR) merkle := primitives.BuildMerkleTreeStore(hashes) keyMR = merkle[len(merkle)-1] // MerkleRoot is not marshalized in Dir Block b.KeyMR = keyMR b.GetFullHash() // Create the Full Hash when we create the keyMR return primitives.NewHash(keyMR.Bytes()), nil }
func (b *DirectoryBlock) BuildBodyMR() (interfaces.IHash, error) { hashes := make([]interfaces.IHash, len(b.GetDBEntries())) for i, entry := range b.GetDBEntries() { data, err := entry.MarshalBinary() if err != nil { return nil, err } hashes[i] = primitives.Sha(data) } if len(hashes) == 0 { hashes = append(hashes, primitives.Sha(nil)) } merkleTree := primitives.BuildMerkleTreeStore(hashes) merkleRoot := merkleTree[len(merkleTree)-1] b.GetHeader().SetBodyMR(merkleRoot) return merkleRoot, nil }
// MR calculates the Merkle Root of the Entry Block Body. See func // primitives.BuildMerkleTreeStore(hashes []interfaces.IHash) (merkles []interfaces.IHash) in common/merkle.go. func (e *EBlockBody) MR() interfaces.IHash { mrs := primitives.BuildMerkleTreeStore(e.EBEntries) r := mrs[len(mrs)-1] return r }