Skip to content

Commit

Permalink
refactor: improve code layout and tests
Browse files Browse the repository at this point in the history
- also remove the now extraneous subpackage readmes
  • Loading branch information
frrist authored and frrist committed Feb 17, 2025
1 parent 4eb30d8 commit 5a41085
Show file tree
Hide file tree
Showing 43 changed files with 361 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A unified Go library (monorepo) for Storacha functionality. This repository host
- **Description:** A reliable and parallelizable job queue.
- [ipnipublisher](./ipnipublisher)
- **Description**: A library to create, sign, and publish adverts to a local IPNI chain, then announce them to other indexers.
- **Usage**: See the [example_test.go](./ipnipublisher/example_test.go) file within this subpackage for a quick start.
- **Usage**: See the [example_test.go](./ipnipublisher/examples/example_test.go) file within this subpackage for a quick start.
- [piece](./piece)
- **Description**: TODO

Expand Down
12 changes: 0 additions & 12 deletions capabilities/README.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package assert

import (
"fmt"
"net/url"

// for schema embed
_ "embed"
"fmt"
"net/url"

ipldprime "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
ipldschema "github.com/ipld/go-ipld-prime/schema"
"github.com/storacha/go-libstoracha/capabilities/pkg/types"
"github.com/storacha/go-libstoracha/capabilities/types"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/result/failure"
"github.com/storacha/go-ucanto/core/schema"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package assert
package assert_test

import (
"bytes"
Expand All @@ -8,8 +8,9 @@ import (

"github.com/ipld/go-ipld-prime/codec/dagjson"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/storacha/go-libstoracha/capabilities/pkg/internal/testutil"
"github.com/storacha/go-libstoracha/capabilities/pkg/types"
"github.com/storacha/go-libstoracha/capabilities/assert"
"github.com/storacha/go-libstoracha/capabilities/internal/testutil"
"github.com/storacha/go-libstoracha/capabilities/types"
"github.com/stretchr/testify/require"
)

Expand All @@ -19,11 +20,11 @@ func TestRoundTripLocationCaveats(t *testing.T) {
require.NoError(t, err)

length := uint64(20)
nb := LocationCaveats{
nb := assert.LocationCaveats{
Content: types.FromHash(digest),
Space: testutil.RandomPrincipal(t).DID(),
Location: []url.URL{*location},
Range: &Range{
Range: &assert.Range{
Offset: 100,
Length: &length,
},
Expand All @@ -42,7 +43,7 @@ func TestRoundTripLocationCaveats(t *testing.T) {
err = dagjson.Decode(builder, buf)
require.NoError(t, err)

rnb, err := LocationCaveatsReader.Read(builder.Build())
rnb, err := assert.LocationCaveatsReader.Read(builder.Build())
require.NoError(t, err)
require.Equal(t, nb, rnb)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package blob

import (
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/storacha/go-libstoracha/capabilities/pkg/types"
"github.com/storacha/go-libstoracha/capabilities/types"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/schema"
"github.com/storacha/go-ucanto/did"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package blob
package blob_test

import (
"testing"

"github.com/storacha/go-libstoracha/capabilities/pkg/internal/testutil"
"github.com/storacha/go-libstoracha/capabilities/blob"
"github.com/storacha/go-libstoracha/capabilities/internal/testutil"
"github.com/stretchr/testify/require"
)

func TestRoundTripAcceptCaveats(t *testing.T) {
digest, bytes := testutil.RandomBytes(t, 256)
nb := AcceptCaveats{
nb := blob.AcceptCaveats{
Space: testutil.RandomPrincipal(t).DID(),
Blob: Blob{
Blob: blob.Blob{
Digest: digest,
Size: uint64(len(bytes)),
},
Put: Promise{
UcanAwait: Await{
Put: blob.Promise{
UcanAwait: blob.Await{
Selector: ".out.ok",
Link: testutil.RandomCID(t),
},
Expand All @@ -26,7 +27,7 @@ func TestRoundTripAcceptCaveats(t *testing.T) {
node, err := nb.ToIPLD()
require.NoError(t, err)

rnb, err := AcceptCaveatsReader.Read(node)
rnb, err := blob.AcceptCaveatsReader.Read(node)
require.NoError(t, err)
require.Equal(t, nb, rnb)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ipld/go-ipld-prime/datamodel"
"github.com/multiformats/go-multihash"
"github.com/storacha/go-libstoracha/capabilities/pkg/types"
"github.com/storacha/go-libstoracha/capabilities/types"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/schema"
"github.com/storacha/go-ucanto/did"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package blob
package blob_test

import (
"testing"

"github.com/storacha/go-libstoracha/capabilities/pkg/internal/testutil"
"github.com/storacha/go-libstoracha/capabilities/blob"
"github.com/storacha/go-libstoracha/capabilities/internal/testutil"
"github.com/stretchr/testify/require"
)

func TestRoundTripAllocateCaveats(t *testing.T) {
digest, bytes := testutil.RandomBytes(t, 256)
nb := AllocateCaveats{
nb := blob.AllocateCaveats{
Space: testutil.RandomPrincipal(t).DID(),
Blob: Blob{
Blob: blob.Blob{
Digest: digest,
Size: uint64(len(bytes)),
},
Expand All @@ -21,7 +22,7 @@ func TestRoundTripAllocateCaveats(t *testing.T) {
node, err := nb.ToIPLD()
require.NoError(t, err)

rnb, err := AllocateCaveatsReader.Read(node)
rnb, err := blob.AllocateCaveatsReader.Read(node)
require.NoError(t, err)
require.Equal(t, nb, rnb)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package blob

import (
"fmt"
// for schema embed
_ "embed"
"fmt"

"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/schema"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ipld/go-ipld-prime/datamodel"
ipldschema "github.com/ipld/go-ipld-prime/schema"
"github.com/multiformats/go-multiaddr"
"github.com/storacha/go-libstoracha/capabilities/pkg/types"
"github.com/storacha/go-libstoracha/capabilities/types"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/schema"
"github.com/storacha/go-ucanto/validator"
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions capabilities/pkg/pdp/pdp.go → capabilities/pdp/pdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
ipldprime "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
ipldschema "github.com/ipld/go-ipld-prime/schema"
"github.com/storacha/go-libstoracha/capabilities/pkg/types"
"github.com/storacha/go-libstoracha/piece/pkg/piece"
"github.com/storacha/go-libstoracha/capabilities/types"
"github.com/storacha/go-libstoracha/piece/piece"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/schema"
"github.com/storacha/go-ucanto/validator"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/multiformats/go-multiaddr"
"github.com/storacha/go-libstoracha/piece/pkg/piece"
"github.com/storacha/go-libstoracha/piece/piece"
"github.com/storacha/go-ucanto/core/ipld"
"github.com/storacha/go-ucanto/core/schema"
"github.com/storacha/go-ucanto/core/schema/options"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package types

import (
"fmt"
// for schema embed
_ "embed"
"fmt"

"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions ipnipublisher/README.md

This file was deleted.

55 changes: 0 additions & 55 deletions ipnipublisher/example_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions ipnipublisher/examples/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"context"
"fmt"
"log"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/ipld/go-ipld-prime"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/storacha/go-libstoracha/ipnipublisher/notifier"
"github.com/storacha/go-libstoracha/ipnipublisher/publisher"
"github.com/storacha/go-libstoracha/ipnipublisher/server"
"github.com/storacha/go-libstoracha/ipnipublisher/store"
)

var ipniNamespace = datastore.NewKey("ipni/")
var publisherNamespace = ipniNamespace.ChildString("publisher/")
var notifierNamespace = ipniNamespace.ChildString("notifier/")

func main() {
ctx := context.Background()

// generate a key pair
priv, _, err := crypto.GenerateEd25519Key(nil)
if err != nil {
log.Fatalf("generating key pair: %s", err)
}

// setup datastore(s)
ds := datastore.NewMapDatastore()
publisherStore := store.FromDatastore(namespace.Wrap(ds, publisherNamespace))

// Setup publisher
p, err := publisher.New(
priv,
publisherStore,
publisher.WithDirectAnnounce("https://cid.contact/announce"),
publisher.WithAnnounceAddrs("/dns4/localhost/tcp/3000/https"),
)
if err != nil {
log.Fatalf("creating publisher: %s", err)
}

// Setup and start HTTP server (optional, but required if announce addresses configured).
encodableStore, _ := publisherStore.(store.EncodeableStore)
srv, err := server.NewServer(encodableStore, server.WithHTTPListenAddrs("localhost:3000"))
if err != nil {
log.Fatalf("creating server: %s", err)
}
if err := srv.Start(ctx); err != nil {
log.Fatalf("starting server: %s", err)
}
defer func() {
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("shutting down server: %s", err)
}
}()

// Setup remote sync notifications (optional).
notifierStore := store.SimpleStoreFromDatastore(namespace.Wrap(ds, notifierNamespace))
notif, err := notifier.NewNotifierWithStorage("https://cid.contact/", priv, notifierStore)
if err != nil {
log.Fatalf("creating notifier: %s", err)
}
notif.Start(ctx)
defer notif.Stop()

notif.Notify(func(ctx context.Context, head, prev ipld.Link) {
fmt.Printf("remote sync from %s to %s\n", prev, head)
})

// Setup complete! Now publish an advert via the `Publish` method.
// p.Publish(ctx, ...)

fmt.Printf("Publisher: %+v\n", p)
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/storacha/go-libstoracha/ipnipublisher/pkg/store"
"github.com/storacha/go-libstoracha/ipnipublisher/store"
"github.com/storacha/go-ucanto/core/ipld"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
ipnifind "github.com/ipni/go-libipni/find/client"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/storacha/go-libstoracha/ipnipublisher/pkg/store"
"github.com/storacha/go-libstoracha/ipnipublisher/store"
)

var log = logging.Logger("notifier")
Expand Down
Loading

0 comments on commit 5a41085

Please sign in to comment.