Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockservice: remove inner fields access methods #564

Open
wants to merge 1 commit into
base: 01-15-blockservice_move_session_handling_as_part_of_the_interface
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@
io.Closer
BlockGetter

// Blockstore returns a reference to the underlying blockstore
Blockstore() blockstore.Blockstore

// Exchange returns a reference to the underlying exchange (usually bitswap)
Exchange() exchange.Interface
// Has indicates if a block is present locally.
Has(ctx context.Context, c cid.Cid) (bool, error)

// AddBlock puts a given block to the underlying datastore
AddBlock(ctx context.Context, o blocks.Block) error
Expand Down Expand Up @@ -125,20 +122,6 @@
return service
}

// Blockstore returns the blockstore behind this blockservice.
func (s *blockService) Blockstore() blockstore.Blockstore {
return s.blockstore
}

// Exchange returns the exchange behind this blockservice.
func (s *blockService) Exchange() exchange.Interface {
return s.exchange
}

func (s *blockService) Allowlist() verifcid.Allowlist {
return s.allowlist
}

func (s *blockService) NewSession(ctx context.Context) BlockGetter {
ses := s.grabSessionFromContext(ctx)
if ses != nil {
Expand Down Expand Up @@ -439,14 +422,13 @@
s.sesctx = nil // early gc
}()

ex := s.bs.Exchange()
if ex == nil {
if s.bs.exchange == nil {
return
}
s.ses = ex // always fallback to non session fetches

sesEx, ok := ex.(exchange.SessionExchange)
sesEx, ok := s.bs.exchange.(exchange.SessionExchange)
if !ok {
s.ses = s.bs.exchange // always fallback to non session fetches
return
}
s.ses = sesEx.NewSession(s.sesctx)
Expand Down Expand Up @@ -491,3 +473,11 @@

return sss
}

func (s *blockService) Has(ctx context.Context, c cid.Cid) (bool, error) {
if err := verifcid.ValidateCid(s.allowlist, c); err != nil {
return false, err
}

Check warning on line 480 in blockservice/blockservice.go

View check run for this annotation

Codecov / codecov/patch

blockservice/blockservice.go#L477-L480

Added lines #L477 - L480 were not covered by tests

return s.blockstore.Has(ctx, c)

Check warning on line 482 in blockservice/blockservice.go

View check run for this annotation

Codecov / codecov/patch

blockservice/blockservice.go#L482

Added line #L482 was not covered by tests
}
5 changes: 1 addition & 4 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"time"

"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/fetcher"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
"github.com/ipfs/boxo/files"
Expand Down Expand Up @@ -51,7 +50,6 @@

// BlocksBackend is an [IPFSBackend] implementation based on a [blockservice.BlockService].
type BlocksBackend struct {
blockStore blockstore.Blockstore
blockService blockservice.BlockService
dagService format.DAGService
resolver resolver.Resolver
Expand Down Expand Up @@ -143,7 +141,6 @@
}

return &BlocksBackend{
blockStore: blockService.Blockstore(),
blockService: blockService,
dagService: dagService,
resolver: r,
Expand Down Expand Up @@ -680,7 +677,7 @@
return false
}

has, _ := bb.blockStore.Has(ctx, rp.RootCid())
has, _ := bb.blockService.Has(ctx, rp.RootCid())

Check warning on line 680 in gateway/blocks_backend.go

View check run for this annotation

Codecov / codecov/patch

gateway/blocks_backend.go#L680

Added line #L680 was not covered by tests
return has
}

Expand Down
28 changes: 18 additions & 10 deletions ipld/merkledag/merkledag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import (
"testing"
"time"

testinstance "github.com/ipfs/boxo/bitswap/testinstance"
tn "github.com/ipfs/boxo/bitswap/testnet"
. "github.com/ipfs/boxo/ipld/merkledag"
mdpb "github.com/ipfs/boxo/ipld/merkledag/pb"
dstest "github.com/ipfs/boxo/ipld/merkledag/test"
mockrouting "github.com/ipfs/boxo/routing/mock"
delay "github.com/ipfs/go-ipfs-delay"

bserv "github.com/ipfs/boxo/blockservice"
bstest "github.com/ipfs/boxo/blockservice/test"
Expand Down Expand Up @@ -507,10 +511,12 @@ func TestCantGet(t *testing.T) {
}

func TestFetchGraph(t *testing.T) {
var dservs []ipld.DAGService
bsis := bstest.Mocks(2)
for _, bsi := range bsis {
dservs = append(dservs, NewDAGService(bsi))
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
sg := testinstance.NewTestInstanceGenerator(net, nil, nil)
instances := sg.Instances(2)
dservs := [2]ipld.DAGService{
NewDAGService(bserv.New(instances[0].Blockstore(), instances[0].Exchange)),
NewDAGService(bserv.New(instances[1].Blockstore(), instances[1].Exchange)),
}

read := io.LimitReader(u.NewTimeSeededRand(), 1024*32)
Expand All @@ -522,7 +528,7 @@ func TestFetchGraph(t *testing.T) {
}

// create an offline dagstore and ensure all blocks were fetched
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore()))
bs := bserv.New(instances[1].Blockstore(), nil)

offlineDS := NewDAGService(bs)

Expand All @@ -547,10 +553,12 @@ func TestFetchGraphWithDepthLimit(t *testing.T) {
}

testF := func(t *testing.T, tc testcase) {
var dservs []ipld.DAGService
bsis := bstest.Mocks(2)
for _, bsi := range bsis {
dservs = append(dservs, NewDAGService(bsi))
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(0))
sg := testinstance.NewTestInstanceGenerator(net, nil, nil)
instances := sg.Instances(2)
dservs := [2]ipld.DAGService{
NewDAGService(bserv.New(instances[0].Blockstore(), instances[0].Exchange)),
NewDAGService(bserv.New(instances[1].Blockstore(), instances[1].Exchange)),
}

root := makeDepthTestingGraph(t, dservs[0])
Expand All @@ -561,7 +569,7 @@ func TestFetchGraphWithDepthLimit(t *testing.T) {
}

// create an offline dagstore and ensure all blocks were fetched
bs := bserv.New(bsis[1].Blockstore(), offline.Exchange(bsis[1].Blockstore()))
bs := bserv.New(instances[1].Blockstore(), offline.Exchange(instances[1].Blockstore()))

offlineDS := NewDAGService(bs)

Expand Down
Loading