swarm/bmt, swarm/network, swarm/storage: update BMT hash initialisation

This commit is contained in:
Anton Evangelatov 2018-08-14 10:44:03 +02:00
parent 395c1a2085
commit b029ebdf11
4 changed files with 14 additions and 13 deletions

View file

@ -23,8 +23,6 @@ import (
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/ethereum/go-ethereum/swarm/chunk"
) )
/* /*
@ -57,9 +55,9 @@ Two implementations are provided:
*/ */
const ( const (
// SegmentCount is the maximum number of segments of the underlying chunk // segmentCount is the maximum number of segments of the underlying chunk
// Should be equal to max-chunk-data-size / hash-size // Should be equal to max-chunk-data-size / hash-size
SegmentCount = 128 segmentCount = 128
// PoolSize is the maximum number of bmt trees used by the hashers, i.e, // PoolSize is the maximum number of bmt trees used by the hashers, i.e,
// the maximum number of concurrent BMT hashing operations performed by the same hasher // the maximum number of concurrent BMT hashing operations performed by the same hasher
PoolSize = 8 PoolSize = 8
@ -320,7 +318,7 @@ func (h *Hasher) Sum(b []byte) (s []byte) {
// with every full segment calls writeSection in a go routine // with every full segment calls writeSection in a go routine
func (h *Hasher) Write(b []byte) (int, error) { func (h *Hasher) Write(b []byte) (int, error) {
l := len(b) l := len(b)
if l == 0 || l > chunk.DefaultSize { if l == 0 || l > h.pool.Size {
return 0, nil return 0, nil
} }
t := h.getTree() t := h.getTree()

View file

@ -224,14 +224,14 @@ func TestHasherReuse(t *testing.T) {
// tests if bmt reuse is not corrupting result // tests if bmt reuse is not corrupting result
func testHasherReuse(poolsize int, t *testing.T) { func testHasherReuse(poolsize int, t *testing.T) {
hasher := sha3.NewKeccak256 hasher := sha3.NewKeccak256
pool := NewTreePool(hasher, SegmentCount, poolsize) pool := NewTreePool(hasher, segmentCount, poolsize)
defer pool.Drain(0) defer pool.Drain(0)
bmt := New(pool) bmt := New(pool)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
data := newData(BufferSize) data := newData(BufferSize)
n := rand.Intn(bmt.Size()) n := rand.Intn(bmt.Size())
err := testHasherCorrectness(bmt, hasher, data, n, SegmentCount) err := testHasherCorrectness(bmt, hasher, data, n, segmentCount)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -241,7 +241,7 @@ func testHasherReuse(poolsize int, t *testing.T) {
// Tests if pool can be cleanly reused even in concurrent use by several hasher // Tests if pool can be cleanly reused even in concurrent use by several hasher
func TestBMTConcurrentUse(t *testing.T) { func TestBMTConcurrentUse(t *testing.T) {
hasher := sha3.NewKeccak256 hasher := sha3.NewKeccak256
pool := NewTreePool(hasher, SegmentCount, PoolSize) pool := NewTreePool(hasher, segmentCount, PoolSize)
defer pool.Drain(0) defer pool.Drain(0)
cycles := 100 cycles := 100
errc := make(chan error) errc := make(chan error)
@ -451,7 +451,7 @@ func benchmarkBMTBaseline(t *testing.B, n int) {
func benchmarkBMT(t *testing.B, n int) { func benchmarkBMT(t *testing.B, n int) {
data := newData(n) data := newData(n)
hasher := sha3.NewKeccak256 hasher := sha3.NewKeccak256
pool := NewTreePool(hasher, SegmentCount, PoolSize) pool := NewTreePool(hasher, segmentCount, PoolSize)
bmt := New(pool) bmt := New(pool)
t.ReportAllocs() t.ReportAllocs()
@ -465,7 +465,7 @@ func benchmarkBMT(t *testing.B, n int) {
func benchmarkBMTAsync(t *testing.B, n int, wh whenHash, double bool) { func benchmarkBMTAsync(t *testing.B, n int, wh whenHash, double bool) {
data := newData(n) data := newData(n)
hasher := sha3.NewKeccak256 hasher := sha3.NewKeccak256
pool := NewTreePool(hasher, SegmentCount, PoolSize) pool := NewTreePool(hasher, segmentCount, PoolSize)
bmt := New(pool).NewAsyncWriter(double) bmt := New(pool).NewAsyncWriter(double)
idxs, segments := splitAndShuffle(bmt.SectionSize(), data) idxs, segments := splitAndShuffle(bmt.SectionSize(), data)
shuffle(len(idxs), func(i int, j int) { shuffle(len(idxs), func(i int, j int) {
@ -483,7 +483,7 @@ func benchmarkBMTAsync(t *testing.B, n int, wh whenHash, double bool) {
func benchmarkPool(t *testing.B, poolsize, n int) { func benchmarkPool(t *testing.B, poolsize, n int) {
data := newData(n) data := newData(n)
hasher := sha3.NewKeccak256 hasher := sha3.NewKeccak256
pool := NewTreePool(hasher, SegmentCount, poolsize) pool := NewTreePool(hasher, segmentCount, poolsize)
cycles := 100 cycles := 100
t.ReportAllocs() t.ReportAllocs()

View file

@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
cp "github.com/ethereum/go-ethereum/swarm/chunk"
"github.com/ethereum/go-ethereum/swarm/log" "github.com/ethereum/go-ethereum/swarm/log"
"github.com/ethereum/go-ethereum/swarm/network" "github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/spancontext" "github.com/ethereum/go-ethereum/swarm/spancontext"
@ -244,7 +245,7 @@ R:
continue R continue R
default: default:
} }
if len(req.SData) > chunk.DefaultSize { if len(req.SData) > int(cp.DefaultSize) {
log.Warn("received chunk is bigger than expected", "len", len(req.SData)) log.Warn("received chunk is bigger than expected", "len", len(req.SData))
continue R continue R
} }

View file

@ -115,7 +115,9 @@ func MakeHashFunc(hash string) SwarmHasher {
case "BMT": case "BMT":
return func() SwarmHash { return func() SwarmHash {
hasher := sha3.NewKeccak256 hasher := sha3.NewKeccak256
pool := bmt.NewTreePool(hasher, bmt.SegmentCount, bmt.PoolSize) hasherSize := hasher().Size()
segmentCount := int(chunk.DefaultSize) / hasherSize
pool := bmt.NewTreePool(hasher, segmentCount, bmt.PoolSize)
return bmt.New(pool) return bmt.New(pool)
} }
} }