| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // Copyright 2016 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package storage | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | 	"crypto/rand" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"encoding/binary" | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/crypto/sha3" | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | Tests TreeChunker by splitting and joining a random byte slice | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type test interface { | 
					
						
							|  |  |  | 	Fatalf(string, ...interface{}) | 
					
						
							|  |  |  | 	Logf(string, ...interface{}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type chunkerTester struct { | 
					
						
							|  |  |  | 	inputs map[uint64][]byte | 
					
						
							|  |  |  | 	t      test | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // fakeChunkStore doesn't store anything, just implements the ChunkStore interface | 
					
						
							|  |  |  | // It can be used to inject into a hasherStore if you don't want to actually store data just do the | 
					
						
							|  |  |  | // hashing | 
					
						
							|  |  |  | type fakeChunkStore struct { | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // Put doesn't store anything it is just here to implement ChunkStore | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | func (f *fakeChunkStore) Put(context.Context, *Chunk) { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // Gut doesn't store anything it is just here to implement ChunkStore | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | func (f *fakeChunkStore) Get(context.Context, Address) (*Chunk, error) { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	return nil, errors.New("FakeChunkStore doesn't support Get") | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // Close doesn't store anything it is just here to implement ChunkStore | 
					
						
							|  |  |  | func (f *fakeChunkStore) Close() { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func newTestHasherStore(chunkStore ChunkStore, hash string) *hasherStore { | 
					
						
							|  |  |  | 	return NewHasherStore(chunkStore, MakeHashFunc(hash), false) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func testRandomBrokenData(n int, tester *chunkerTester) { | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | 	data := io.LimitReader(rand.Reader, int64(n)) | 
					
						
							|  |  |  | 	brokendata := brokenLimitReader(data, n, n/2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf := make([]byte, n) | 
					
						
							|  |  |  | 	_, err := brokendata.Read(buf) | 
					
						
							|  |  |  | 	if err == nil || err.Error() != "Broken reader" { | 
					
						
							|  |  |  | 		tester.t.Fatalf("Broken reader is not broken, hence broken. Returns: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data = io.LimitReader(rand.Reader, int64(n)) | 
					
						
							|  |  |  | 	brokendata = brokenLimitReader(data, n, n/2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	putGetter := newTestHasherStore(NewMapChunkStore(), SHA3Hash) | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	expectedError := fmt.Errorf("Broken reader") | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 	addr, _, err := TreeSplit(context.TODO(), brokendata, int64(n), putGetter) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	if err == nil || err.Error() != expectedError.Error() { | 
					
						
							|  |  |  | 		tester.t.Fatalf("Not receiving the correct error! Expected %v, received %v", expectedError, err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	tester.t.Logf(" Key = %v\n", addr) | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func testRandomData(usePyramid bool, hash string, n int, tester *chunkerTester) Address { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if tester.inputs == nil { | 
					
						
							|  |  |  | 		tester.inputs = make(map[uint64][]byte) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	input, found := tester.inputs[uint64(n)] | 
					
						
							|  |  |  | 	var data io.Reader | 
					
						
							|  |  |  | 	if !found { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		data, input = generateRandomData(n) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		tester.inputs[uint64(n)] = input | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | 		data = io.LimitReader(bytes.NewReader(input), int64(n)) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	putGetter := newTestHasherStore(NewMapChunkStore(), hash) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	var addr Address | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 	var wait func(context.Context) error | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 	ctx := context.TODO() | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	if usePyramid { | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		addr, wait, err = PyramidSplit(ctx, data, putGetter, putGetter) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		addr, wait, err = TreeSplit(ctx, data, int64(n), putGetter) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		tester.t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	tester.t.Logf(" Key = %v\n", addr) | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 	err = wait(ctx) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		tester.t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 	reader := TreeJoin(context.TODO(), addr, putGetter, 0) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	output := make([]byte, n) | 
					
						
							|  |  |  | 	r, err := reader.Read(output) | 
					
						
							|  |  |  | 	if r != n || err != io.EOF { | 
					
						
							|  |  |  | 		tester.t.Fatalf("read error  read: %v  n = %v  err = %v\n", r, n, err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if input != nil { | 
					
						
							|  |  |  | 		if !bytes.Equal(output, input) { | 
					
						
							|  |  |  | 			tester.t.Fatalf("input and output mismatch\n IN: %v\nOUT: %v\n", input, output) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	// testing partial read | 
					
						
							|  |  |  | 	for i := 1; i < n; i += 10000 { | 
					
						
							|  |  |  | 		readableLength := n - i | 
					
						
							|  |  |  | 		output := make([]byte, readableLength) | 
					
						
							|  |  |  | 		r, err := reader.ReadAt(output, int64(i)) | 
					
						
							|  |  |  | 		if r != readableLength || err != io.EOF { | 
					
						
							|  |  |  | 			tester.t.Fatalf("readAt error with offset %v read: %v  n = %v  err = %v\n", i, r, readableLength, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if input != nil { | 
					
						
							|  |  |  | 			if !bytes.Equal(output, input[i:]) { | 
					
						
							|  |  |  | 				tester.t.Fatalf("input and output mismatch\n IN: %v\nOUT: %v\n", input[i:], output) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	return addr | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestSha3ForCorrectness(t *testing.T) { | 
					
						
							|  |  |  | 	tester := &chunkerTester{t: t} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size := 4096 | 
					
						
							|  |  |  | 	input := make([]byte, size+8) | 
					
						
							|  |  |  | 	binary.LittleEndian.PutUint64(input[:8], uint64(size)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	io.LimitReader(bytes.NewReader(input[8:]), int64(size)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rawSha3 := sha3.NewKeccak256() | 
					
						
							|  |  |  | 	rawSha3.Reset() | 
					
						
							|  |  |  | 	rawSha3.Write(input) | 
					
						
							|  |  |  | 	rawSha3Output := rawSha3.Sum(nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sha3FromMakeFunc := MakeHashFunc(SHA3Hash)() | 
					
						
							|  |  |  | 	sha3FromMakeFunc.ResetWithLength(input[:8]) | 
					
						
							|  |  |  | 	sha3FromMakeFunc.Write(input[8:]) | 
					
						
							|  |  |  | 	sha3FromMakeFuncOutput := sha3FromMakeFunc.Sum(nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(rawSha3Output) != len(sha3FromMakeFuncOutput) { | 
					
						
							|  |  |  | 		tester.t.Fatalf("Original SHA3 and abstracted Sha3 has different length %v:%v\n", len(rawSha3Output), len(sha3FromMakeFuncOutput)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !bytes.Equal(rawSha3Output, sha3FromMakeFuncOutput) { | 
					
						
							|  |  |  | 		tester.t.Fatalf("Original SHA3 and abstracted Sha3 mismatch %v:%v\n", rawSha3Output, sha3FromMakeFuncOutput) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDataAppend(t *testing.T) { | 
					
						
							|  |  |  | 	sizes := []int{1, 1, 1, 4095, 4096, 4097, 1, 1, 1, 123456, 2345678, 2345678} | 
					
						
							|  |  |  | 	appendSizes := []int{4095, 4096, 4097, 1, 1, 1, 8191, 8192, 8193, 9000, 3000, 5000} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tester := &chunkerTester{t: t} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	for i := range sizes { | 
					
						
							|  |  |  | 		n := sizes[i] | 
					
						
							|  |  |  | 		m := appendSizes[i] | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		if tester.inputs == nil { | 
					
						
							|  |  |  | 			tester.inputs = make(map[uint64][]byte) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		input, found := tester.inputs[uint64(n)] | 
					
						
							|  |  |  | 		var data io.Reader | 
					
						
							|  |  |  | 		if !found { | 
					
						
							|  |  |  | 			data, input = generateRandomData(n) | 
					
						
							|  |  |  | 			tester.inputs[uint64(n)] = input | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			data = io.LimitReader(bytes.NewReader(input), int64(n)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		chunkStore := NewMapChunkStore() | 
					
						
							|  |  |  | 		putGetter := newTestHasherStore(chunkStore, SHA3Hash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		ctx := context.TODO() | 
					
						
							|  |  |  | 		addr, wait, err := PyramidSplit(ctx, data, putGetter, putGetter) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			tester.t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = wait(ctx) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			tester.t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//create a append data stream | 
					
						
							|  |  |  | 		appendInput, found := tester.inputs[uint64(m)] | 
					
						
							|  |  |  | 		var appendData io.Reader | 
					
						
							|  |  |  | 		if !found { | 
					
						
							|  |  |  | 			appendData, appendInput = generateRandomData(m) | 
					
						
							|  |  |  | 			tester.inputs[uint64(m)] = appendInput | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			appendData = io.LimitReader(bytes.NewReader(appendInput), int64(m)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		putGetter = newTestHasherStore(chunkStore, SHA3Hash) | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		newAddr, wait, err := PyramidAppend(ctx, addr, appendData, putGetter, putGetter) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			tester.t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = wait(ctx) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			tester.t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		reader := TreeJoin(ctx, newAddr, putGetter, 0) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		newOutput := make([]byte, n+m) | 
					
						
							|  |  |  | 		r, err := reader.Read(newOutput) | 
					
						
							|  |  |  | 		if r != (n + m) { | 
					
						
							|  |  |  | 			tester.t.Fatalf("read error  read: %v  n = %v  m = %v  err = %v\n", r, n, m, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		newInput := append(input, appendInput...) | 
					
						
							|  |  |  | 		if !bytes.Equal(newOutput, newInput) { | 
					
						
							|  |  |  | 			tester.t.Fatalf("input and output mismatch\n IN: %v\nOUT: %v\n", newInput, newOutput) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestRandomData(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	// This test can validate files up to a relatively short length, as tree chunker slows down drastically. | 
					
						
							|  |  |  | 	// Validation of longer files is done by TestLocalStoreAndRetrieve in swarm package. | 
					
						
							|  |  |  | 	sizes := []int{1, 60, 83, 179, 253, 1024, 4095, 4096, 4097, 8191, 8192, 8193, 12287, 12288, 12289, 524288, 524288 + 1, 524288 + 4097, 7 * 524288, 7*524288 + 1, 7*524288 + 4097} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	tester := &chunkerTester{t: t} | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	for _, s := range sizes { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		treeChunkerKey := testRandomData(false, SHA3Hash, s, tester) | 
					
						
							|  |  |  | 		pyramidChunkerKey := testRandomData(true, SHA3Hash, s, tester) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if treeChunkerKey.String() != pyramidChunkerKey.String() { | 
					
						
							|  |  |  | 			tester.t.Fatalf("tree chunker and pyramid chunker key mismatch for size %v\n TC: %v\n PC: %v\n", s, treeChunkerKey.String(), pyramidChunkerKey.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	for _, s := range sizes { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		treeChunkerKey := testRandomData(false, BMTHash, s, tester) | 
					
						
							|  |  |  | 		pyramidChunkerKey := testRandomData(true, BMTHash, s, tester) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if treeChunkerKey.String() != pyramidChunkerKey.String() { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			tester.t.Fatalf("tree chunker and pyramid chunker key mismatch for size %v\n TC: %v\n PC: %v\n", s, treeChunkerKey.String(), pyramidChunkerKey.String()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func TestRandomBrokenData(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	sizes := []int{1, 60, 83, 179, 253, 1024, 4095, 4096, 4097, 8191, 8192, 8193, 12287, 12288, 12289, 123456, 2345678} | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | 	tester := &chunkerTester{t: t} | 
					
						
							|  |  |  | 	for _, s := range sizes { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		testRandomBrokenData(s, tester) | 
					
						
							| 
									
										
										
										
											2016-10-08 12:33:52 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | func benchReadAll(reader LazySectionReader) { | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | 	size, _ := reader.Size(context.TODO(), nil) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	output := make([]byte, 1000) | 
					
						
							|  |  |  | 	for pos := int64(0); pos < size; pos += 1000 { | 
					
						
							|  |  |  | 		reader.ReadAt(output, pos) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func benchmarkSplitJoin(n int, t *testing.B) { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	t.ReportAllocs() | 
					
						
							|  |  |  | 	for i := 0; i < t.N; i++ { | 
					
						
							|  |  |  | 		data := testDataReader(n) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		putGetter := newTestHasherStore(NewMapChunkStore(), SHA3Hash) | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		ctx := context.TODO() | 
					
						
							|  |  |  | 		key, wait, err := PyramidSplit(ctx, data, putGetter, putGetter) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		err = wait(ctx) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		reader := TreeJoin(ctx, key, putGetter, 0) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 		benchReadAll(reader) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | func benchmarkSplitTreeSHA3(n int, t *testing.B) { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	t.ReportAllocs() | 
					
						
							|  |  |  | 	for i := 0; i < t.N; i++ { | 
					
						
							|  |  |  | 		data := testDataReader(n) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		putGetter := newTestHasherStore(&fakeChunkStore{}, SHA3Hash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		_, _, err := TreeSplit(context.TODO(), data, int64(n), putGetter) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | func benchmarkSplitTreeBMT(n int, t *testing.B) { | 
					
						
							|  |  |  | 	t.ReportAllocs() | 
					
						
							|  |  |  | 	for i := 0; i < t.N; i++ { | 
					
						
							|  |  |  | 		data := testDataReader(n) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		putGetter := newTestHasherStore(&fakeChunkStore{}, BMTHash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		_, _, err := TreeSplit(context.TODO(), data, int64(n), putGetter) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func benchmarkSplitPyramidSHA3(n int, t *testing.B) { | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	t.ReportAllocs() | 
					
						
							|  |  |  | 	for i := 0; i < t.N; i++ { | 
					
						
							|  |  |  | 		data := testDataReader(n) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		putGetter := newTestHasherStore(&fakeChunkStore{}, SHA3Hash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		_, _, err := PyramidSplit(context.TODO(), data, putGetter, putGetter) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func benchmarkSplitPyramidBMT(n int, t *testing.B) { | 
					
						
							|  |  |  | 	t.ReportAllocs() | 
					
						
							|  |  |  | 	for i := 0; i < t.N; i++ { | 
					
						
							|  |  |  | 		data := testDataReader(n) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		putGetter := newTestHasherStore(&fakeChunkStore{}, BMTHash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		_, _, err := PyramidSplit(context.TODO(), data, putGetter, putGetter) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func benchmarkSplitAppendPyramid(n, m int, t *testing.B) { | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 	t.ReportAllocs() | 
					
						
							|  |  |  | 	for i := 0; i < t.N; i++ { | 
					
						
							|  |  |  | 		data := testDataReader(n) | 
					
						
							|  |  |  | 		data1 := testDataReader(m) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		chunkStore := NewMapChunkStore() | 
					
						
							|  |  |  | 		putGetter := newTestHasherStore(chunkStore, SHA3Hash) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		ctx := context.TODO() | 
					
						
							|  |  |  | 		key, wait, err := PyramidSplit(ctx, data, putGetter, putGetter) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = wait(ctx) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		putGetter = newTestHasherStore(chunkStore, SHA3Hash) | 
					
						
							| 
									
										
										
										
											2018-07-09 14:11:49 +02:00
										 |  |  | 		_, wait, err = PyramidAppend(ctx, key, data1, putGetter, putGetter) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatalf(err.Error()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		err = wait(ctx) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 			t.Fatalf(err.Error()) | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func BenchmarkSplitJoin_2(t *testing.B) { benchmarkSplitJoin(100, t) } | 
					
						
							|  |  |  | func BenchmarkSplitJoin_3(t *testing.B) { benchmarkSplitJoin(1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitJoin_4(t *testing.B) { benchmarkSplitJoin(10000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitJoin_5(t *testing.B) { benchmarkSplitJoin(100000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitJoin_6(t *testing.B) { benchmarkSplitJoin(1000000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitJoin_7(t *testing.B) { benchmarkSplitJoin(10000000, t) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // func BenchmarkSplitJoin_8(t *testing.B) { benchmarkJoin(100000000, t) } | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | func BenchmarkSplitTreeSHA3_2(t *testing.B)  { benchmarkSplitTreeSHA3(100, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_2h(t *testing.B) { benchmarkSplitTreeSHA3(500, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_3(t *testing.B)  { benchmarkSplitTreeSHA3(1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_3h(t *testing.B) { benchmarkSplitTreeSHA3(5000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_4(t *testing.B)  { benchmarkSplitTreeSHA3(10000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_4h(t *testing.B) { benchmarkSplitTreeSHA3(50000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_5(t *testing.B)  { benchmarkSplitTreeSHA3(100000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_6(t *testing.B)  { benchmarkSplitTreeSHA3(1000000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeSHA3_7(t *testing.B)  { benchmarkSplitTreeSHA3(10000000, t) } | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // func BenchmarkSplitTreeSHA3_8(t *testing.B)  { benchmarkSplitTreeSHA3(100000000, t) } | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_2(t *testing.B)  { benchmarkSplitTreeBMT(100, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_2h(t *testing.B) { benchmarkSplitTreeBMT(500, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_3(t *testing.B)  { benchmarkSplitTreeBMT(1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_3h(t *testing.B) { benchmarkSplitTreeBMT(5000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_4(t *testing.B)  { benchmarkSplitTreeBMT(10000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_4h(t *testing.B) { benchmarkSplitTreeBMT(50000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_5(t *testing.B)  { benchmarkSplitTreeBMT(100000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_6(t *testing.B)  { benchmarkSplitTreeBMT(1000000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitTreeBMT_7(t *testing.B)  { benchmarkSplitTreeBMT(10000000, t) } | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // func BenchmarkSplitTreeBMT_8(t *testing.B)  { benchmarkSplitTreeBMT(100000000, t) } | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_2(t *testing.B)  { benchmarkSplitPyramidSHA3(100, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_2h(t *testing.B) { benchmarkSplitPyramidSHA3(500, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_3(t *testing.B)  { benchmarkSplitPyramidSHA3(1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_3h(t *testing.B) { benchmarkSplitPyramidSHA3(5000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_4(t *testing.B)  { benchmarkSplitPyramidSHA3(10000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_4h(t *testing.B) { benchmarkSplitPyramidSHA3(50000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_5(t *testing.B)  { benchmarkSplitPyramidSHA3(100000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_6(t *testing.B)  { benchmarkSplitPyramidSHA3(1000000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidSHA3_7(t *testing.B)  { benchmarkSplitPyramidSHA3(10000000, t) } | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // func BenchmarkSplitPyramidSHA3_8(t *testing.B)  { benchmarkSplitPyramidSHA3(100000000, t) } | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_2(t *testing.B)  { benchmarkSplitPyramidBMT(100, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_2h(t *testing.B) { benchmarkSplitPyramidBMT(500, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_3(t *testing.B)  { benchmarkSplitPyramidBMT(1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_3h(t *testing.B) { benchmarkSplitPyramidBMT(5000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_4(t *testing.B)  { benchmarkSplitPyramidBMT(10000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_4h(t *testing.B) { benchmarkSplitPyramidBMT(50000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_5(t *testing.B)  { benchmarkSplitPyramidBMT(100000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_6(t *testing.B)  { benchmarkSplitPyramidBMT(1000000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitPyramidBMT_7(t *testing.B)  { benchmarkSplitPyramidBMT(10000000, t) } | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // func BenchmarkSplitPyramidBMT_8(t *testing.B)  { benchmarkSplitPyramidBMT(100000000, t) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_2(t *testing.B)  { benchmarkSplitAppendPyramid(100, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_2h(t *testing.B) { benchmarkSplitAppendPyramid(500, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_3(t *testing.B)  { benchmarkSplitAppendPyramid(1000, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_4(t *testing.B)  { benchmarkSplitAppendPyramid(10000, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_4h(t *testing.B) { benchmarkSplitAppendPyramid(50000, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_5(t *testing.B)  { benchmarkSplitAppendPyramid(1000000, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_6(t *testing.B)  { benchmarkSplitAppendPyramid(1000000, 1000, t) } | 
					
						
							|  |  |  | func BenchmarkSplitAppendPyramid_7(t *testing.B)  { benchmarkSplitAppendPyramid(10000000, 1000, t) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // func BenchmarkAppendPyramid_8(t *testing.B)  { benchmarkAppendPyramid(100000000, 1000, t) } | 
					
						
							| 
									
										
										
										
											2017-09-22 01:52:51 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | // go test -timeout 20m -cpu 4 -bench=./swarm/storage -run no | 
					
						
							|  |  |  | // If you dont add the timeout argument above .. the benchmark will timeout and dump |