generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exchange: create a providing.Exchange which provides on NotifyNewBlocks.
- Loading branch information
Showing
7 changed files
with
123 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Package providing implements an exchange wrapper which | ||
// does content providing for new blocks. | ||
package providing | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ipfs/boxo/exchange" | ||
"github.com/ipfs/boxo/provider" | ||
blocks "github.com/ipfs/go-block-format" | ||
) | ||
|
||
// Exchange is an exchange wrapper that calls ProvideMany for blocks received | ||
// over NotifyNewBlocks. | ||
type Exchange struct { | ||
exchange.Interface | ||
provider provider.Provider | ||
} | ||
|
||
// New creates a new providing Exchange with the given exchange and provider. | ||
func New(base exchange.Interface, provider provider.Provider) *Exchange { | ||
return &Exchange{ | ||
Interface: base, | ||
provider: provider, | ||
} | ||
} | ||
|
||
// NotifyNewBlocks calls provider.ProvideMany. | ||
func (ex *Exchange) NotifyNewBlocks(ctx context.Context, blocks ...blocks.Block) error { | ||
for _, b := range blocks { | ||
if err := ex.provider.Provide(ctx, b.Cid(), true); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package providing | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
testinstance "github.com/ipfs/boxo/bitswap/testinstance" | ||
tn "github.com/ipfs/boxo/bitswap/testnet" | ||
"github.com/ipfs/boxo/blockservice" | ||
"github.com/ipfs/boxo/provider" | ||
mockrouting "github.com/ipfs/boxo/routing/mock" | ||
delay "github.com/ipfs/go-ipfs-delay" | ||
"github.com/ipfs/go-test/random" | ||
) | ||
|
||
func TestExchange(t *testing.T) { | ||
ctx := context.Background() | ||
net := tn.VirtualNetwork(delay.Fixed(0)) | ||
routing := mockrouting.NewServer() | ||
sg := testinstance.NewTestInstanceGenerator(net, routing, nil, nil) | ||
i := sg.Next() | ||
provFinder := routing.Client(i.Identity) | ||
prov, err := provider.New(i.Datastore, | ||
provider.Online(provFinder), | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
provExchange := New(i.Exchange, prov) | ||
// write-through so that we notify when re-adding block | ||
bs := blockservice.New(i.Blockstore, provExchange, | ||
blockservice.WriteThrough()) | ||
block := random.BlocksOfSize(1, 10)[0] | ||
// put it on the blockstore of the first instance | ||
err = i.Blockstore.Put(ctx, block) | ||
if err != nil { | ||
t.Fatal() | ||
} | ||
|
||
providersChan := provFinder.FindProvidersAsync(ctx, block.Cid(), 1) | ||
_, ok := <-providersChan | ||
if ok { | ||
t.Fatal("there should be no providers yet for block") | ||
} | ||
|
||
// Now add it via BlockService. It should trigger NotifyNewBlocks | ||
// on the exchange and thus they should get announced. | ||
err = bs.AddBlock(ctx, block) | ||
if err != nil { | ||
t.Fatal() | ||
} | ||
// Trigger reproviding, otherwise it's not really provided. | ||
err = prov.Reprovide(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
providersChan = provFinder.FindProvidersAsync(ctx, block.Cid(), 1) | ||
_, ok = <-providersChan | ||
if !ok { | ||
t.Fatal("there should be one provider for the block") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters