From 22249ac2b92b1a17b7c8b971058ff0190451c958 Mon Sep 17 00:00:00 2001 From: zelig Date: Sat, 7 Feb 2015 07:09:43 +0100 Subject: [PATCH] chunker and root key fix - chunker panics if root key buffer is not allocated or have proper length - rename HashSize->KeySize and add it to Chunker interface --- bzz/bzzhash/bzzhash.go | 2 +- bzz/chunker.go | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/bzz/bzzhash/bzzhash.go b/bzz/bzzhash/bzzhash.go index 3adecd36b3..92f917c60e 100644 --- a/bzz/bzzhash/bzzhash.go +++ b/bzz/bzzhash/bzzhash.go @@ -26,7 +26,7 @@ func main() { sr := io.NewSectionReader(f, 0, stat.Size()) chunker := &bzz.TreeChunker{} chunker.Init() - hash := make([]byte, chunker.HashSize()) + hash := make([]byte, chunker.KeySize()) errC := chunker.Split(hash, sr, nil) err, ok := <-errC if err != nil { diff --git a/bzz/chunker.go b/bzz/chunker.go index 9169177590..c559a2e057 100644 --- a/bzz/chunker.go +++ b/bzz/chunker.go @@ -72,6 +72,9 @@ type Chunker interface { Lifecycle of the reader can be modified with SetTimeout() */ Join(key Key, chunkC chan *Chunk) (LazySectionReader, chan error) + + // returns the key length + KeySize() int64 } /* @@ -111,7 +114,7 @@ func (self *TreeChunker) Init() { } -func (self *TreeChunker) HashSize() int64 { +func (self *TreeChunker) KeySize() int64 { return self.hashSize } @@ -143,13 +146,20 @@ func (self *TreeChunker) Hash(size int64, input SectionReader) []byte { } func (self *TreeChunker) Split(key Key, data SectionReader, chunkC chan *Chunk) (errC chan error) { + + if self.chunkSize <= 0 { + panic("chunker must be initialised") + } + + if int64(len(key)) != self.hashSize { + panic(fmt.Sprintf("root key buffer must be allocated byte slice of length %d", self.hashSize)) + } + wg := &sync.WaitGroup{} errC = make(chan error) rerrC := make(chan error) timeout := time.After(self.SplitTimeout) - if key == nil { - key = make([]byte, self.hashSize) - } + wg.Add(1) go func() { @@ -159,10 +169,6 @@ func (self *TreeChunker) Split(key Key, data SectionReader, chunkC chan *Chunk) // takes lowest depth such that chunksize*HashCount^(depth+1) > size // power series, will find the order of magnitude of the data size in base hashCount or numbers of levels of branching in the resulting tree. - if self.Branches <= 1 || self.chunkSize <= 0 { - panic("chunker must be initialised") - } - for ; treeSize < size; treeSize *= self.Branches { depth++ }