Skip to content

Commit

Permalink
embed the allowlist into boundedblockservice
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Aug 16, 2023
1 parent 836a67c commit ae7441a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
25 changes: 10 additions & 15 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ type BlockService interface {
// BoundedBlockService is a Blockservice bounded via strict multihash Allowlist.
type BoundedBlockService interface {
BlockService

Allowlist() verifcid.Allowlist
verifcid.Allowlist
}

type blockService struct {
allowlist verifcid.Allowlist
verifcid.Allowlist
blockstore blockstore.Blockstore
exchange exchange.Interface
// If checkFirst is true then first check that a block doesn't
Expand All @@ -93,7 +92,7 @@ func WriteThrough() Option {
// WithAllowlist sets a custom [verifcid.Allowlist] which will be used
func WithAllowlist(allowlist verifcid.Allowlist) Option {
return func(bs *blockService) {
bs.allowlist = allowlist
bs.Allowlist = allowlist
}
}

Expand All @@ -104,7 +103,7 @@ func New(bs blockstore.Blockstore, exchange exchange.Interface, opts ...Option)
}

service := &blockService{
allowlist: verifcid.DefaultAllowlist,
Allowlist: verifcid.DefaultAllowlist,
blockstore: bs,
exchange: exchange,
checkFirst: true,
Expand Down Expand Up @@ -135,19 +134,15 @@ func (s *blockService) Exchange() exchange.Interface {
return s.exchange
}

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

// NewSession creates a new session that allows for
// controlled exchange of wantlists to decrease the bandwidth overhead.
// If the current exchange is a SessionExchange, a new exchange
// session will be created. Otherwise, the current exchange will be used
// directly.
func NewSession(ctx context.Context, bs BlockService) *Session {
allowlist := verifcid.DefaultAllowlist
allowlist := verifcid.Allowlist(verifcid.DefaultAllowlist)
if bbs, ok := bs.(BoundedBlockService); ok {
allowlist = bbs.Allowlist()
allowlist = bbs
}
exch := bs.Exchange()
if sessEx, ok := exch.(exchange.SessionExchange); ok {
Expand Down Expand Up @@ -175,7 +170,7 @@ func (s *blockService) AddBlock(ctx context.Context, o blocks.Block) error {
defer span.End()

c := o.Cid()
err := verifcid.ValidateCid(s.allowlist, c) // hash security
err := verifcid.ValidateCid(s.Allowlist, c) // hash security
if err != nil {
return err
}
Expand Down Expand Up @@ -206,7 +201,7 @@ func (s *blockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {

// hash security
for _, b := range bs {
err := verifcid.ValidateCid(s.allowlist, b.Cid())
err := verifcid.ValidateCid(s.Allowlist, b.Cid())
if err != nil {
return err
}
Expand Down Expand Up @@ -256,7 +251,7 @@ func (s *blockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, e
f = s.getExchange
}

return getBlock(ctx, c, s.blockstore, s.allowlist, f)
return getBlock(ctx, c, s.blockstore, s.Allowlist, f)
}

func (s *blockService) getExchange() notifiableFetcher {
Expand Down Expand Up @@ -313,7 +308,7 @@ func (s *blockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan block
f = s.getExchange
}

return getBlocks(ctx, ks, s.blockstore, s.allowlist, f)
return getBlocks(ctx, ks, s.blockstore, s.Allowlist, f)
}

func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, allowlist verifcid.Allowlist, fget func() notifiableFetcher) <-chan blocks.Block {
Expand Down
2 changes: 1 addition & 1 deletion verifcid/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// DefaultAllowlist is the default list of hashes allowed in IPFS.
var DefaultAllowlist Allowlist = defaultAllowlist{}
var DefaultAllowlist defaultAllowlist

// Allowlist defines an interface containing list of allowed multihashes.
type Allowlist interface {
Expand Down

0 comments on commit ae7441a

Please sign in to comment.