Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
feat: add context to interfaces (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
guseggert authored Nov 10, 2021
1 parent 542e40d commit 285f9bc
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 75 deletions.
30 changes: 15 additions & 15 deletions blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ type BlockService interface {
Exchange() exchange.Interface

// AddBlock puts a given block to the underlying datastore
AddBlock(o blocks.Block) error
AddBlock(ctx context.Context, o blocks.Block) error

// AddBlocks adds a slice of blocks at the same time using batching
// capabilities of the underlying datastore whenever possible.
AddBlocks(bs []blocks.Block) error
AddBlocks(ctx context.Context, bs []blocks.Block) error

// DeleteBlock deletes the given block from the blockservice.
DeleteBlock(o cid.Cid) error
DeleteBlock(ctx context.Context, o cid.Cid) error
}

type blockService struct {
Expand Down Expand Up @@ -130,35 +130,35 @@ func NewSession(ctx context.Context, bs BlockService) *Session {

// AddBlock adds a particular block to the service, Putting it into the datastore.
// TODO pass a context into this if the remote.HasBlock is going to remain here.
func (s *blockService) AddBlock(o blocks.Block) error {
func (s *blockService) AddBlock(ctx context.Context, o blocks.Block) error {
c := o.Cid()
// hash security
err := verifcid.ValidateCid(c)
if err != nil {
return err
}
if s.checkFirst {
if has, err := s.blockstore.Has(c); has || err != nil {
if has, err := s.blockstore.Has(ctx, c); has || err != nil {
return err
}
}

if err := s.blockstore.Put(o); err != nil {
if err := s.blockstore.Put(ctx, o); err != nil {
return err
}

log.Debugf("BlockService.BlockAdded %s", c)

if s.exchange != nil {
if err := s.exchange.HasBlock(o); err != nil {
if err := s.exchange.HasBlock(ctx, o); err != nil {
log.Errorf("HasBlock: %s", err.Error())
}
}

return nil
}

func (s *blockService) AddBlocks(bs []blocks.Block) error {
func (s *blockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
// hash security
for _, b := range bs {
err := verifcid.ValidateCid(b.Cid())
Expand All @@ -170,7 +170,7 @@ func (s *blockService) AddBlocks(bs []blocks.Block) error {
if s.checkFirst {
toput = make([]blocks.Block, 0, len(bs))
for _, b := range bs {
has, err := s.blockstore.Has(b.Cid())
has, err := s.blockstore.Has(ctx, b.Cid())
if err != nil {
return err
}
Expand All @@ -186,15 +186,15 @@ func (s *blockService) AddBlocks(bs []blocks.Block) error {
return nil
}

err := s.blockstore.PutMany(toput)
err := s.blockstore.PutMany(ctx, toput)
if err != nil {
return err
}

if s.exchange != nil {
for _, o := range toput {
log.Debugf("BlockService.BlockAdded %s", o.Cid())
if err := s.exchange.HasBlock(o); err != nil {
if err := s.exchange.HasBlock(ctx, o); err != nil {
log.Errorf("HasBlock: %s", err.Error())
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, fget fun
return nil, err
}

block, err := bs.Get(c)
block, err := bs.Get(ctx, c)
if err == nil {
return block, nil
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget

var misses []cid.Cid
for _, c := range ks {
hit, err := bs.Get(c)
hit, err := bs.Get(ctx, c)
if err != nil {
misses = append(misses, c)
continue
Expand Down Expand Up @@ -332,8 +332,8 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget
}

// DeleteBlock deletes a block in the blockservice from the datastore
func (s *blockService) DeleteBlock(c cid.Cid) error {
err := s.blockstore.DeleteBlock(c)
func (s *blockService) DeleteBlock(ctx context.Context, c cid.Cid) error {
err := s.blockstore.DeleteBlock(ctx, c)
if err == nil {
log.Debugf("BlockService.BlockDeleted %s", c)
}
Expand Down
14 changes: 7 additions & 7 deletions blockservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func TestWriteThroughWorks(t *testing.T) {
block := bgen.Next()

t.Logf("PutCounter: %d", bstore.PutCounter)
err := bserv.AddBlock(block)
err := bserv.AddBlock(context.Background(), block)
if err != nil {
t.Fatal(err)
}
if bstore.PutCounter != 1 {
t.Fatalf("expected just one Put call, have: %d", bstore.PutCounter)
}

err = bserv.AddBlock(block)
err = bserv.AddBlock(context.Background(), block)
if err != nil {
t.Fatal(err)
}
Expand All @@ -58,12 +58,12 @@ func TestLazySessionInitialization(t *testing.T) {
bgen := butil.NewBlockGenerator()

block := bgen.Next()
err := bstore.Put(block)
err := bstore.Put(ctx, block)
if err != nil {
t.Fatal(err)
}
block2 := bgen.Next()
err = session.HasBlock(block2)
err = session.HasBlock(ctx, block2)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -101,9 +101,9 @@ type PutCountingBlockstore struct {
PutCounter int
}

func (bs *PutCountingBlockstore) Put(block blocks.Block) error {
func (bs *PutCountingBlockstore) Put(ctx context.Context, block blocks.Block) error {
bs.PutCounter++
return bs.Blockstore.Put(block)
return bs.Blockstore.Put(ctx, block)
}

var _ exchange.SessionExchange = (*fakeSessionExchange)(nil)
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestNilExchange(t *testing.T) {
if err != ErrNotFound {
t.Fatal("expected block to not be found")
}
err = bs.Put(block)
err = bs.Put(ctx, block)
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 6 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ module github.com/ipfs/go-blockservice
go 1.16

require (
github.com/google/uuid v1.2.0 // indirect
github.com/ipfs/go-bitswap v0.3.4
github.com/ipfs/go-bitswap v0.5.0
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-ipfs-blockstore v0.1.6
github.com/ipfs/go-datastore v0.5.0
github.com/ipfs/go-ipfs-blockstore v0.2.0
github.com/ipfs/go-ipfs-blocksutil v0.0.1
github.com/ipfs/go-ipfs-delay v0.0.1
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-routing v0.1.0
github.com/ipfs/go-ipfs-exchange-interface v0.1.0
github.com/ipfs/go-ipfs-exchange-offline v0.1.0
github.com/ipfs/go-ipfs-routing v0.2.0
github.com/ipfs/go-ipfs-util v0.0.2
github.com/ipfs/go-log/v2 v2.3.0
github.com/ipfs/go-verifcid v0.0.1
github.com/klauspost/cpuid/v2 v2.0.6 // indirect
github.com/libp2p/go-libp2p v0.14.0 // indirect
github.com/multiformats/go-multihash v0.0.15 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
Expand Down
Loading

0 comments on commit 285f9bc

Please sign in to comment.