Commit 680ba989 authored by acud's avatar acud Committed by GitHub

fix(api): dont stamp dupes (#1772)

parent af1a1015
...@@ -329,16 +329,39 @@ func newStamperPutter(s storage.Storer, post postage.Service, signer crypto.Sign ...@@ -329,16 +329,39 @@ func newStamperPutter(s storage.Storer, post postage.Service, signer crypto.Sign
return &stamperPutter{Storer: s, stamper: stamper}, nil return &stamperPutter{Storer: s, stamper: stamper}, nil
} }
func (p *stamperPutter) Put(ctx context.Context, mode storage.ModePut, chs ...swarm.Chunk) (exist []bool, err error) { func (p *stamperPutter) Put(ctx context.Context, mode storage.ModePut, chs ...swarm.Chunk) (exists []bool, err error) {
var (
ctp []swarm.Chunk
idx []int
)
exists = make([]bool, len(chs))
for i, c := range chs { for i, c := range chs {
has, err := p.Storer.Has(ctx, c.Address())
if err != nil {
return nil, err
}
if has || containsChunk(c.Address(), chs[:i]...) {
exists[i] = true
continue
}
stamp, err := p.stamper.Stamp(c.Address()) stamp, err := p.stamper.Stamp(c.Address())
if err != nil { if err != nil {
return nil, err return nil, err
} }
chs[i] = c.WithStamp(stamp) chs[i] = c.WithStamp(stamp)
ctp = append(ctp, chs[i])
idx = append(idx, i)
} }
return p.Storer.Put(ctx, mode, chs...) exists2, err := p.Storer.Put(ctx, mode, ctp...)
if err != nil {
return nil, err
}
for i, v := range idx {
exists[v] = exists2[i]
}
return exists, nil
} }
type pipelineFunc func(context.Context, io.Reader) (swarm.Address, error) type pipelineFunc func(context.Context, io.Reader) (swarm.Address, error)
...@@ -380,3 +403,14 @@ func requestCalculateNumberOfChunks(r *http.Request) int64 { ...@@ -380,3 +403,14 @@ func requestCalculateNumberOfChunks(r *http.Request) int64 {
} }
return 0 return 0
} }
// containsChunk returns true if the chunk with a specific address
// is present in the provided chunk slice.
func containsChunk(addr swarm.Address, chs ...swarm.Chunk) bool {
for _, c := range chs {
if addr.Equal(c.Address()) {
return true
}
}
return false
}
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