/
example_test.go
100 lines (87 loc) · 3.28 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package engine_test
import (
"context"
"fmt"
"io"
provider "github.com/filecoin-project/index-provider"
"github.com/filecoin-project/index-provider/engine"
"github.com/filecoin-project/index-provider/metadata"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multihash"
)
// Example_advertiseHelloWorld shows an example of instantiating an engine.Engine and publishing
// and advertisement for a sample content.
//
// Note that the advertisement published uses metadata.BitswapMetadata. This is for demonstrative
// purposes only. The example does not set up the retrieval side for the content.
func Example_advertiseHelloWorld() {
// Get the multihash of content to advertise
content := "Hello World!"
sayHelloCtxID := "Say hello"
fmt.Printf("Preparing to advertise content: '%s'\n", string(content))
mh, err := multihash.Sum([]byte(content), multihash.SHA2_256, -1)
if err != nil {
panic(err)
}
fmt.Printf("✓ Generated content multihash: %s\n", mh.B58String())
// Create a new libp2p host
h, err := libp2p.New()
if err != nil {
panic(err)
}
// Only print the first three characters to keep golang example output happy.
fmt.Printf("✓ Instantiated new libp2p host with peer ID: %s...\n", h.ID().String()[:4])
// Construct a new provider engine with given libp2p host that announces advertisements over
// gossipsub and datatrasfer/graphsync.
engine, err := engine.New(engine.WithHost(h), engine.WithPublisherKind(engine.DataTransferPublisher))
if err != nil {
panic(err)
}
fmt.Println("✓ Instantiated provider engine")
defer engine.Shutdown()
engine.RegisterMultihashLister(func(ctx context.Context, provider peer.ID, contextID []byte) (provider.MultihashIterator, error) {
if string(contextID) == sayHelloCtxID {
return &singleMhIterator{mh: mh}, nil
}
return nil, fmt.Errorf("no content is found for context ID: %v", contextID)
})
fmt.Printf("✓ Registered lister for context ID: %s\n", sayHelloCtxID)
// Start the engine
if err = engine.Start(context.Background()); err != nil {
panic(err)
}
fmt.Println("✓ Provider engine started.")
// Multiple transports can be included in metadata.
md := metadata.Default.New(metadata.Bitswap{})
// Note that this example publishes an ad with bitswap metadata as an example.
// But it does not instantiate a bitswap server to serve retrievals.
adCid, err := engine.NotifyPut(context.Background(), nil, []byte(sayHelloCtxID), md)
if err != nil {
panic(err)
}
// Only print the first three characters to keep golang example output happy.
fmt.Printf("✓ Published advertisement for content with CID: %s...\n", adCid.String()[:3])
if err := engine.Shutdown(); err != nil {
panic(err)
}
//Output:
//Preparing to advertise content: 'Hello World!'
//✓ Generated content multihash: QmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e
//✓ Instantiated new libp2p host with peer ID: 12D3...
//✓ Instantiated provider engine
//✓ Registered lister for context ID: Say hello
//✓ Provider engine started.
//✓ Published advertisement for content with CID: bag...
}
type singleMhIterator struct {
offset int
mh multihash.Multihash
}
func (s *singleMhIterator) Next() (multihash.Multihash, error) {
if s.offset == 0 {
s.offset++
return s.mh, nil
}
return nil, io.EOF
}