Skip to content

Commit

Permalink
blockservice: don't store allowlist in Session
Browse files Browse the repository at this point in the history
This is accessible through the blockservice object.
  • Loading branch information
Jorropo committed Jan 15, 2024
1 parent ea04c77 commit 2f67c04
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ func NewSession(ctx context.Context, bs BlockService) *Session {

// newSession is like [NewSession] but it does not attempt to reuse session from the existing context.
func newSession(ctx context.Context, bs BlockService) *Session {
var allowlist verifcid.Allowlist = verifcid.DefaultAllowlist
if bbs, ok := bs.(BoundedBlockService); ok {
allowlist = bbs.Allowlist()
}

return &Session{bs: bs, allowlist: allowlist, sesctx: ctx}
return &Session{bs: bs, sesctx: ctx}
}

// AddBlock adds a particular block to the service, Putting it into the datastore.
Expand Down Expand Up @@ -250,16 +245,16 @@ func (s *blockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, e
ctx, span := internal.StartSpan(ctx, "blockService.GetBlock", trace.WithAttributes(attribute.Stringer("CID", c)))
defer span.End()

return getBlock(ctx, c, s, s.allowlist, s.getExchangeFetcher)
return getBlock(ctx, c, s, s.getExchangeFetcher)
}

// Look at what I have to do, no interface covariance :'(
func (s *blockService) getExchangeFetcher() exchange.Fetcher {
return s.exchange
}

func getBlock(ctx context.Context, c cid.Cid, bs BlockService, allowlist verifcid.Allowlist, fetchFactory func() exchange.Fetcher) (blocks.Block, error) {
err := verifcid.ValidateCid(allowlist, c) // hash security
func getBlock(ctx context.Context, c cid.Cid, bs BlockService, fetchFactory func() exchange.Fetcher) (blocks.Block, error) {
err := verifcid.ValidateCid(grabAllowlistFromBlockservice(bs), c) // hash security
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -313,15 +308,17 @@ func (s *blockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan block
ctx, span := internal.StartSpan(ctx, "blockService.GetBlocks")
defer span.End()

return getBlocks(ctx, ks, s, s.allowlist, s.getExchangeFetcher)
return getBlocks(ctx, ks, s, s.getExchangeFetcher)
}

func getBlocks(ctx context.Context, ks []cid.Cid, blockservice BlockService, allowlist verifcid.Allowlist, fetchFactory func() exchange.Fetcher) <-chan blocks.Block {
func getBlocks(ctx context.Context, ks []cid.Cid, blockservice BlockService, fetchFactory func() exchange.Fetcher) <-chan blocks.Block {
out := make(chan blocks.Block)

go func() {
defer close(out)

allowlist := grabAllowlistFromBlockservice(blockservice)

allValid := true
for _, c := range ks {
if err := verifcid.ValidateCid(allowlist, c); err != nil {
Expand Down Expand Up @@ -439,7 +436,6 @@ type Session struct {
bs BlockService
ses exchange.Fetcher
sesctx context.Context
allowlist verifcid.Allowlist
}

// grabSession is used to lazily create sessions.
Expand Down Expand Up @@ -470,15 +466,15 @@ func (s *Session) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error)
ctx, span := internal.StartSpan(ctx, "Session.GetBlock", trace.WithAttributes(attribute.Stringer("CID", c)))
defer span.End()

return getBlock(ctx, c, s.bs, s.allowlist, s.grabSession)
return getBlock(ctx, c, s.bs, s.grabSession)
}

// GetBlocks gets blocks in the context of a request session
func (s *Session) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
ctx, span := internal.StartSpan(ctx, "Session.GetBlocks")
defer span.End()

return getBlocks(ctx, ks, s.bs, s.allowlist, s.grabSession)
return getBlocks(ctx, ks, s.bs, s.grabSession)
}

var _ BlockGetter = (*Session)(nil)
Expand Down Expand Up @@ -519,3 +515,11 @@ func grabSessionFromContext(ctx context.Context, bs BlockService) *Session {

return ss
}

// grabAllowlistFromBlockservice never returns nil
func grabAllowlistFromBlockservice(bs BlockService) verifcid.Allowlist {
if bbs, ok := bs.(BoundedBlockService); ok {
return bbs.Allowlist()
}
return verifcid.DefaultAllowlist
}

0 comments on commit 2f67c04

Please sign in to comment.