Skip to content

Commit

Permalink
fix: check before writing root address
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Jan 25, 2024
1 parent 657f7ab commit bf608d2
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions pkg/storer/internal/pinning/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"encoding/binary"
"errors"
"fmt"
"log"
"os"
"sync"

"github.com/ethersphere/bee/pkg/encryption"
Expand Down Expand Up @@ -42,6 +44,13 @@ var (
errCollectionRootAddressIsZero = errors.New("pin store: collection root address is zero")
)

func init() {

Check failure on line 47 in pkg/storer/internal/pinning/pinning.go

View workflow job for this annotation

GitHub Actions / Lint

don't use `init` function (gochecknoinits)
f, _ := os.OpenFile("/tmp/testlogfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defer f.Close()

log.SetOutput(f)
}

// creates a new UUID and returns it as a byte slice
func newUUID() []byte {
id := uuid.New()
Expand Down Expand Up @@ -243,6 +252,16 @@ func (c *collectionPutter) Put(ctx context.Context, st internal.Storage, writer
return nil
}

rootCollection := &pinCollectionItem{Addr: ch.Address()}
has, err := st.IndexStore().Has(rootCollection)
if err != nil {
return fmt.Errorf("pin store: check previous root: %w", err)
}
if has {
// duplicate PUT of the same root address
return nil
}

err = writer.Put(collectionChunk)
if err != nil {
return fmt.Errorf("pin store: failed putting collection chunk: %w", err)
Expand All @@ -264,11 +283,19 @@ func (c *collectionPutter) Close(st internal.Storage, writer storage.Writer, roo
c.mtx.Lock()
defer c.mtx.Unlock()

// Save the root pin reference.
c.collection.Addr = root
err := writer.Put(c.collection)
collection := &pinCollectionItem{Addr: root}
has, err := st.IndexStore().Has(collection)
if err != nil {
return fmt.Errorf("pin store: failed updating collection: %w", err)
return fmt.Errorf("pin store: check previous root: %w", err)
}

if !has {
// Save the root pin reference.
c.collection.Addr = root
err := writer.Put(c.collection)
if err != nil {
return fmt.Errorf("pin store: failed updating collection: %w", err)
}
}

err = writer.Delete(&dirtyCollection{UUID: c.collection.UUID})
Expand Down Expand Up @@ -347,6 +374,7 @@ func Pins(st storage.Store) ([]swarm.Address, error) {
Factory: func() storage.Item { return new(pinCollectionItem) },
ItemProperty: storage.QueryItemID,
}, func(r storage.Result) (bool, error) {
log.Println("god r.", r.Entry)
addr := swarm.NewAddress([]byte(r.ID))
pins = append(pins, addr)
return false, nil
Expand Down

0 comments on commit bf608d2

Please sign in to comment.