Skip to content

Commit

Permalink
feat: upgrade boxo for refactored boxo/ipns package
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 14, 2023
1 parent c93e267 commit e5ffac7
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 672 deletions.
24 changes: 7 additions & 17 deletions client/rpc/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,20 @@ import (
caopts "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/coreiface/path"
"github.com/ipfs/boxo/ipns"
)

type NameAPI HttpApi

type ipnsEntry struct {
JName string `json:"Name"`
JValue string `json:"Value"`

path path.Path
}

func (e *ipnsEntry) Name() string {
return e.JName
Name string `json:"Name"`
Value string `json:"Value"`
}

func (e *ipnsEntry) Value() path.Path {
return e.path
}

func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.NamePublishOption) (iface.IpnsEntry, error) {
func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.NamePublishOption) (ipns.Name, error) {
options, err := caopts.NamePublishOptions(opts...)
if err != nil {
return nil, err
return ipns.Name{}, err
}

req := api.core().Request("name/publish", p.String()).
Expand All @@ -47,10 +38,9 @@ func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.Nam

var out ipnsEntry
if err := req.Exec(ctx, &out); err != nil {
return nil, err
return ipns.Name{}, err
}
out.path = path.New(out.JValue)
return &out, out.path.IsValid()
return ipns.NameFromString(out.Name)
}

func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan iface.IpnsResult, error) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
fsrepo "github.com/ipfs/kubo/repo/fsrepo"

options "github.com/ipfs/boxo/coreiface/options"
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
"github.com/ipfs/boxo/files"
cmds "github.com/ipfs/go-ipfs-cmds"
config "github.com/ipfs/kubo/config"
Expand Down Expand Up @@ -262,5 +263,5 @@ func initializeIpnsKeyspace(repoRoot string) error {
return err
}

return nd.Namesys.Publish(ctx, nd.PrivateKey, path.FromCid(emptyDir.Cid()))
return nd.Namesys.Publish(ctx, nd.PrivateKey, path.FromCid(emptyDir.Cid()), nsopts.PublishCompatibleWithV1(true))
}
11 changes: 1 addition & 10 deletions config/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Router struct {
Type RouterType

// Parameters are extra configuration that this router might need.
// A common one for reframe router is "Endpoint".
// A common one for HTTP router is "Endpoint".
Parameters interface{}
}

Expand Down Expand Up @@ -81,8 +81,6 @@ func (r *RouterParser) UnmarshalJSON(b []byte) error {
switch out.Type {
case RouterTypeHTTP:
p = &HTTPRouterParams{}
case RouterTypeReframe:
p = &ReframeRouterParams{}
case RouterTypeDHT:
p = &DHTRouterParams{}
case RouterTypeSequential:
Expand All @@ -106,7 +104,6 @@ func (r *RouterParser) UnmarshalJSON(b []byte) error {
type RouterType string

const (
RouterTypeReframe RouterType = "reframe" // More info here: https://github.com/ipfs/specs/tree/main/reframe . Actually deprecated.
RouterTypeHTTP RouterType = "http" // HTTP JSON API for delegated routing systems (IPIP-337).
RouterTypeDHT RouterType = "dht" // DHT router.
RouterTypeSequential RouterType = "sequential" // Router helper to execute several routers sequentially.
Expand All @@ -133,12 +130,6 @@ const (

var MethodNameList = []MethodName{MethodNameProvide, MethodNameFindPeers, MethodNameFindProviders, MethodNameGetIPNS, MethodNamePutIPNS}

type ReframeRouterParams struct {
// Endpoint is the URL where the routing implementation will point to get the information.
// Usually used for reframe Routers.
Endpoint string
}

type HTTPRouterParams struct {
// Endpoint is the URL where the routing implementation will point to get the information.
Endpoint string
Expand Down
77 changes: 12 additions & 65 deletions config/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ func TestRouterParameters(t *testing.T) {
PublicIPNetwork: false,
},
}},
"router-reframe": {Router{
Type: RouterTypeReframe,
Parameters: ReframeRouterParams{
Endpoint: "reframe-endpoint",
},
}},
"router-parallel": {Router{
Type: RouterTypeParallel,
Parameters: ComposableRouterParams{
Expand All @@ -39,7 +33,7 @@ func TestRouterParameters(t *testing.T) {
IgnoreErrors: true,
},
{
RouterName: "router-reframe",
RouterName: "router-dht",
Timeout: Duration{10 * time.Second},
IgnoreErrors: false,
ExecuteAfter: &OptionalDuration{&sec},
Expand All @@ -58,7 +52,7 @@ func TestRouterParameters(t *testing.T) {
IgnoreErrors: true,
},
{
RouterName: "router-reframe",
RouterName: "router-dht",
Timeout: Duration{10 * time.Second},
IgnoreErrors: false,
},
Expand All @@ -69,7 +63,7 @@ func TestRouterParameters(t *testing.T) {
},
Methods: Methods{
MethodNameFindPeers: {
RouterName: "router-reframe",
RouterName: "router-dht",
},
MethodNameFindProviders: {
RouterName: "router-dht",
Expand Down Expand Up @@ -99,95 +93,48 @@ func TestRouterParameters(t *testing.T) {
dhtp := r2.Routers["router-dht"].Parameters
require.IsType(&DHTRouterParams{}, dhtp)

rp := r2.Routers["router-reframe"].Parameters
require.IsType(&ReframeRouterParams{}, rp)

sp := r2.Routers["router-sequential"].Parameters
require.IsType(&ComposableRouterParams{}, sp)

pp := r2.Routers["router-parallel"].Parameters
require.IsType(&ComposableRouterParams{}, pp)
}

func TestRouterMissingParameters(t *testing.T) {
require := require.New(t)

r := Routing{
Type: NewOptionalString("custom"),
Routers: map[string]RouterParser{
"router-wrong-reframe": {Router{
Type: RouterTypeReframe,
Parameters: DHTRouterParams{
Mode: "auto",
AcceleratedDHTClient: true,
PublicIPNetwork: false,
},
}},
},
Methods: Methods{
MethodNameFindPeers: {
RouterName: "router-wrong-reframe",
},
MethodNameFindProviders: {
RouterName: "router-wrong-reframe",
},
MethodNameGetIPNS: {
RouterName: "router-wrong-reframe",
},
MethodNameProvide: {
RouterName: "router-wrong-reframe",
},
MethodNamePutIPNS: {
RouterName: "router-wrong-reframe",
},
},
}

out, err := json.Marshal(r)
require.NoError(err)

r2 := &Routing{}

err = json.Unmarshal(out, r2)
require.NoError(err)
require.Empty(r2.Routers["router-wrong-reframe"].Parameters.(*ReframeRouterParams).Endpoint)
}

func TestMethods(t *testing.T) {
require := require.New(t)

methodsOK := Methods{
MethodNameFindPeers: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNameFindProviders: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNameGetIPNS: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNameProvide: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNamePutIPNS: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
}

require.NoError(methodsOK.Check())

methodsMissing := Methods{
MethodNameFindPeers: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNameGetIPNS: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNameProvide: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
MethodNamePutIPNS: {
RouterName: "router-wrong-reframe",
RouterName: "router-wrong",
},
}

Expand Down
4 changes: 2 additions & 2 deletions core/commands/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestKeyTranslation(t *testing.T) {
pid := test.RandPeerIDFatal(t)
pkname := namesys.PkKeyForID(pid)
ipnsname := ipns.RecordKey(pid)
ipnsname := ipns.NameFromPeer(pid).RoutingKey()

pkk, err := escapeDhtKey("/pk/" + pid.Pretty())
if err != nil {
Expand All @@ -28,7 +28,7 @@ func TestKeyTranslation(t *testing.T) {
t.Fatal("keys didn't match!")
}

if ipnsk != ipnsname {
if ipnsk != string(ipnsname) {
t.Fatal("keys didn't match!")
}
}
29 changes: 6 additions & 23 deletions core/commands/name/ipnsps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package name
import (
"fmt"
"io"
"strings"

"github.com/ipfs/boxo/ipns"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/kubo/core/commands/cmdenv"
ke "github.com/ipfs/kubo/core/commands/keyencode"
record "github.com/libp2p/go-libp2p-record"
"github.com/libp2p/go-libp2p/core/peer"
)

type ipnsPubsubState struct {
Expand Down Expand Up @@ -76,15 +73,7 @@ var ipnspsSubsCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show current name subscriptions.",
},
Options: []cmds.Option{
ke.OptionIPNSBase,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
keyEnc, err := ke.KeyEncoderFromString(req.Options[ke.OptionIPNSBase.Name()].(string))
if err != nil {
return err
}

n, err := cmdenv.GetNode(env)
if err != nil {
return err
Expand All @@ -95,17 +84,12 @@ var ipnspsSubsCmd = &cmds.Command{
}
var paths []string
for _, key := range n.PSRouter.GetSubscriptions() {
ns, k, err := record.SplitKey(key)
if err != nil || ns != "ipns" {
// Not necessarily an error.
continue
}
pid, err := peer.IDFromBytes([]byte(k))
name, err := ipns.NameFromRoutingKey([]byte(key))
if err != nil {
log.Errorf("ipns key not a valid peer ID: %s", err)
continue
}
paths = append(paths, "/ipns/"+keyEnc.FormatID(pid))
paths = append(paths, name.String())
}

return cmds.EmitOnce(res, &stringList{paths})
Expand All @@ -131,17 +115,16 @@ var ipnspsCancelCmd = &cmds.Command{
return cmds.Errorf(cmds.ErrClient, "IPNS pubsub subsystem is not enabled")
}

name := req.Arguments[0]
name = strings.TrimPrefix(name, "/ipns/")
pid, err := peer.Decode(name)
name, err := ipns.NameFromString(req.Arguments[0])
if err != nil {
return cmds.Errorf(cmds.ErrClient, err.Error())
}

ok, err := n.PSRouter.Cancel("/ipns/" + string(pid))
ok, err := n.PSRouter.Cancel(string(name.RoutingKey()))
if err != nil {
return err
}

return cmds.EmitOnce(res, &ipnsPubsubCancel{ok})
},
Arguments: []cmds.Argument{
Expand Down
Loading

0 comments on commit e5ffac7

Please sign in to comment.