stream: give access to tree aspects

This commit is contained in:
Vincent Batts 2015-03-30 15:19:46 -04:00
parent 1959058d6a
commit 3cc77c8073
1 changed files with 22 additions and 1 deletions

View File

@ -9,7 +9,7 @@ import (
// NewHash provides a hash.Hash to generate a merkle.Tree checksum, given a
// HashMaker for the checksums of the blocks written and the blockSize of each
// block per node in the tree.
func NewHash(hm HashMaker, merkleBlockLength int) hash.Hash {
func NewHash(hm HashMaker, merkleBlockLength int) HashTreeer {
mh := new(merkleHash)
mh.blockSize = merkleBlockLength
mh.hm = hm
@ -18,6 +18,19 @@ func NewHash(hm HashMaker, merkleBlockLength int) hash.Hash {
return mh
}
// Treeer (Tree-er) provides access to the Merkle tree internals
type Treeer interface {
Nodes() []*Node
Root() *Node
}
// HashTreeer can be used as a hash.Hash but also provide access to the Merkle
// tree internals
type HashTreeer interface {
hash.Hash
Treeer
}
// TODO make a similar hash.Hash, that accepts an argument of a merkle.Tree,
// that will validate nodes as the new bytes are written. If a new written
// block fails checksum, then return an error on the io.Writer
@ -31,6 +44,14 @@ type merkleHash struct {
partialLastNode bool // true when Sum() has appended a Node for a partial block
}
func (mh merkleHash) Nodes() []*Node {
return mh.tree.Nodes
}
func (mh merkleHash) Root() *Node {
return mh.tree.Root()
}
// XXX this will be tricky, as the last block can be less than the BlockSize.
// if they get the sum, it will be mh.tree.Root().Checksum() at that point.
//