Commit 116d95e1 authored by lash's avatar lash Committed by GitHub

Fix deadlock in concurrent uploads (#256)

parent 42c4eb40
...@@ -9,9 +9,7 @@ import ( ...@@ -9,9 +9,7 @@ import (
"encoding/binary" "encoding/binary"
"hash" "hash"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/swarm" "github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bmt"
bmtlegacy "github.com/ethersphere/bmt/legacy" bmtlegacy "github.com/ethersphere/bmt/legacy"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
...@@ -25,21 +23,18 @@ func hashFunc() hash.Hash { ...@@ -25,21 +23,18 @@ func hashFunc() hash.Hash {
// ContentAddressValidator validates that the address of a given chunk // ContentAddressValidator validates that the address of a given chunk
// is the content address of its contents // is the content address of its contents
type ContentAddressValidator struct { type ContentAddressValidator struct {
hasher bmt.Hash
logger logging.Logger
} }
// New constructs a new ContentAddressValidator // New constructs a new ContentAddressValidator
func NewContentAddressValidator() swarm.ChunkValidator { func NewContentAddressValidator() swarm.ChunkValidator {
p := bmtlegacy.NewTreePool(hashFunc, swarm.Branches, bmtlegacy.PoolSize)
return &ContentAddressValidator{ return &ContentAddressValidator{}
hasher: bmtlegacy.New(p),
}
} }
// Validate performs the validation check // Validate performs the validation check
func (v *ContentAddressValidator) Validate(ch swarm.Chunk) (valid bool) { func (v *ContentAddressValidator) Validate(ch swarm.Chunk) (valid bool) {
p := bmtlegacy.NewTreePool(hashFunc, swarm.Branches, bmtlegacy.PoolSize)
hasher := bmtlegacy.New(p)
// prepare data // prepare data
data := ch.Data() data := ch.Data()
...@@ -47,18 +42,16 @@ func (v *ContentAddressValidator) Validate(ch swarm.Chunk) (valid bool) { ...@@ -47,18 +42,16 @@ func (v *ContentAddressValidator) Validate(ch swarm.Chunk) (valid bool) {
span := binary.LittleEndian.Uint64(data[:8]) span := binary.LittleEndian.Uint64(data[:8])
// execute hash, compare and return result // execute hash, compare and return result
v.hasher.Reset() hasher.Reset()
err := v.hasher.SetSpan(int64(span)) err := hasher.SetSpan(int64(span))
if err != nil { if err != nil {
v.logger.Debugf("SetSpan on bmt legacy hasher gave error: %v", err)
return false return false
} }
_, err = v.hasher.Write(data[8:]) _, err = hasher.Write(data[8:])
if err != nil { if err != nil {
v.logger.Debugf("Write on bmt legacy hasher gave error: %v", err)
return false return false
} }
s := v.hasher.Sum(nil) s := hasher.Sum(nil)
return address.Equal(swarm.NewAddress(s)) return address.Equal(swarm.NewAddress(s))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment