mirror of
https://github.com/vbatts/merkle.git
synced 2024-12-12 06:58:04 +00:00
stream: give access to tree aspects
This commit is contained in:
parent
1959058d6a
commit
3cc77c8073
1 changed files with 22 additions and 1 deletions
23
stream.go
23
stream.go
|
@ -9,7 +9,7 @@ import (
|
||||||
// NewHash provides a hash.Hash to generate a merkle.Tree checksum, given a
|
// 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
|
// HashMaker for the checksums of the blocks written and the blockSize of each
|
||||||
// block per node in the tree.
|
// 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 := new(merkleHash)
|
||||||
mh.blockSize = merkleBlockLength
|
mh.blockSize = merkleBlockLength
|
||||||
mh.hm = hm
|
mh.hm = hm
|
||||||
|
@ -18,6 +18,19 @@ func NewHash(hm HashMaker, merkleBlockLength int) hash.Hash {
|
||||||
return mh
|
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,
|
// 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
|
// 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
|
// 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
|
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.
|
// 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.
|
// if they get the sum, it will be mh.tree.Root().Checksum() at that point.
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in a new issue