-
Notifications
You must be signed in to change notification settings - Fork 17
/
example_test.go
148 lines (131 loc) Β· 4.68 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package engine_test
import (
"context"
crand "crypto/rand"
"fmt"
"io"
"github.com/ipni/go-libipni/metadata"
provider "github.com/ipni/index-provider"
"github.com/ipni/index-provider/engine"
"github.com/ipni/index-provider/engine/xproviders"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"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.
engine, err := engine.New(engine.WithHost(h), engine.WithPublisherKind(engine.Libp2pPublisher))
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])
// Create an advertisement with ExtendedProviders
providerID := h.ID()
privKey := h.Peerstore().PrivKey(providerID)
addrs := h.Addrs()
mdBytes, err := md.MarshalBinary()
if err != nil {
panic(err)
}
// Generate random keys and identity for a new ExtendedProvider
xPrivKey, _, err := crypto.GenerateEd25519Key(crand.Reader)
if err != nil {
panic(err)
}
xProviderID, err := peer.IDFromPrivateKey(xPrivKey)
if err != nil {
panic(err)
}
xAddr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/9000")
if err != nil {
panic(err)
}
// Build and sign ExtendedProviders advertisement
xAd, err := xproviders.NewAdBuilder(providerID, privKey, addrs).
WithLastAdID(adCid).
WithExtendedProviders(xproviders.NewInfo(xProviderID, xPrivKey, mdBytes, []multiaddr.Multiaddr{xAddr})).
WithOverride(true).
WithContextID([]byte("sample-context-id")).
WithMetadata(mdBytes).
BuildAndSign()
if err != nil {
panic(err)
}
// Publish the advertisement using engine
xAdCid, err := engine.Publish(context.Background(), *xAd)
if err != nil {
panic(err)
}
// Only print the first three characters to keep golang example output happy.
fmt.Printf("β Published ExtendedProviders advertisement for content with CID: %s...\n", xAdCid.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...
//β Published ExtendedProviders 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
}