From 3cc77c80736ef7c4af440eb864a946e0a616e13d Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 30 Mar 2015 15:19:46 -0400 Subject: [PATCH] stream: give access to tree aspects --- stream.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/stream.go b/stream.go index 2951145..84e3033 100644 --- a/stream.go +++ b/stream.go @@ -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. //