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

Fix deadlock in concurrent uploads (#256)

parent 42c4eb40
......@@ -9,9 +9,7 @@ import (
"encoding/binary"
"hash"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bmt"
bmtlegacy "github.com/ethersphere/bmt/legacy"
"golang.org/x/crypto/sha3"
)
......@@ -25,21 +23,18 @@ func hashFunc() hash.Hash {
// ContentAddressValidator validates that the address of a given chunk
// is the content address of its contents
type ContentAddressValidator struct {
hasher bmt.Hash
logger logging.Logger
}
// New constructs a new ContentAddressValidator
func NewContentAddressValidator() swarm.ChunkValidator {
p := bmtlegacy.NewTreePool(hashFunc, swarm.Branches, bmtlegacy.PoolSize)
return &ContentAddressValidator{
hasher: bmtlegacy.New(p),
}
return &ContentAddressValidator{}
}
// Validate performs the validation check
func (v *ContentAddressValidator) Validate(ch swarm.Chunk) (valid bool) {
p := bmtlegacy.NewTreePool(hashFunc, swarm.Branches, bmtlegacy.PoolSize)
hasher := bmtlegacy.New(p)
// prepare data
data := ch.Data()
......@@ -47,18 +42,16 @@ func (v *ContentAddressValidator) Validate(ch swarm.Chunk) (valid bool) {
span := binary.LittleEndian.Uint64(data[:8])
// execute hash, compare and return result
v.hasher.Reset()
err := v.hasher.SetSpan(int64(span))
hasher.Reset()
err := hasher.SetSpan(int64(span))
if err != nil {
v.logger.Debugf("SetSpan on bmt legacy hasher gave error: %v", err)
return false
}
_, err = v.hasher.Write(data[8:])
_, err = hasher.Write(data[8:])
if err != nil {
v.logger.Debugf("Write on bmt legacy hasher gave error: %v", err)
return false
}
s := v.hasher.Sum(nil)
s := hasher.Sum(nil)
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