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 13, 2023
1 parent 344431d commit f6164e2
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 580 deletions.
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
2 changes: 1 addition & 1 deletion 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.RoutingKey(pid)

pkk, err := escapeDhtKey("/pk/" + pid.Pretty())
if err != nil {
Expand Down
39 changes: 24 additions & 15 deletions core/commands/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,17 @@ Passing --verify will verify signature against provided public key.
return err
}

var entry ipns_pb.IpnsEntry
err = proto.Unmarshal(b.Bytes(), &entry)
// Here we use the old school raw Protobuf pbRecord because we want to inspect it,
// aka, we need to be able to see all of its contents inside. While the boxo/ipns
// provides a good abstraction over the Record type, it doesn't allow for introspection
// of the raw value of the IpnsEntry.
var pbRecord ipns_pb.IpnsEntry
err = proto.Unmarshal(b.Bytes(), &pbRecord)
if err != nil {
return err
}

rec, err := ipns.UnmarshalRecord(b.Bytes())
if err != nil {
return err
}
Expand All @@ -164,24 +173,24 @@ Passing --verify will verify signature against provided public key.

result := &IpnsInspectResult{
Entry: IpnsInspectEntry{
Value: string(entry.Value),
ValidityType: entry.ValidityType,
Sequence: *entry.Sequence,
TTL: entry.Ttl,
PublicKey: encoder.Encode(entry.PubKey),
SignatureV1: encoder.Encode(entry.SignatureV1),
SignatureV2: encoder.Encode(entry.SignatureV2),
Value: string(pbRecord.Value),
ValidityType: pbRecord.ValidityType,
Sequence: *pbRecord.Sequence,
TTL: pbRecord.Ttl,
PublicKey: encoder.Encode(pbRecord.PubKey),
SignatureV1: encoder.Encode(pbRecord.SignatureV1),
SignatureV2: encoder.Encode(pbRecord.SignatureV2),
Data: nil,
},
}

if len(entry.Data) != 0 {
if len(pbRecord.Data) != 0 {
// This is hacky. The variable node (datamodel.Node) doesn't directly marshal
// to JSON. Therefore, we need to first decode from DAG-CBOR, then encode in
// DAG-JSON and finally unmarshal it from JSON. Since DAG-JSON is a subset
// of JSON, that should work. Then, we can store the final value in the
// result.Entry.Data for further inspection.
node, err := ipld.Decode(entry.Data, dagcbor.Decode)
node, err := ipld.Decode(pbRecord.Data, dagcbor.Decode)
if err != nil {
return err
}
Expand All @@ -198,24 +207,24 @@ Passing --verify will verify signature against provided public key.
}
}

validity, err := ipns.GetEOL(&entry)
validity, err := rec.Validity()
if err == nil {
result.Entry.Validity = &validity
}

verify, ok := req.Options["verify"].(string)
if ok {
key := strings.TrimPrefix(verify, "/ipns/")
id, err := peer.Decode(key)
pid, err := peer.Decode(key)
if err != nil {
return err
}

result.Validation = &IpnsInspectValidation{
PublicKey: id,
PublicKey: pid,
}

err = ipns.ValidateWithPeerID(id, &entry)
err = ipns.ValidateWithPeerID(rec, pid)
if err == nil {
result.Validation.Valid = true
} else {
Expand Down
4 changes: 4 additions & 0 deletions core/commands/name/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
ttlOptionName = "ttl"
keyOptionName = "key"
quieterOptionName = "quieter"
compatibleWithV1 = "compatible-v1"
)

var PublishCmd = &cmds.Command{
Expand Down Expand Up @@ -83,6 +84,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
cmds.StringOption(ttlOptionName, "Time duration this record should be cached for. Uses the same syntax as the lifetime option. (caution: experimental)"),
cmds.StringOption(keyOptionName, "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'.").WithDefault("self"),
cmds.BoolOption(quieterOptionName, "Q", "Write only final hash."),
cmds.BoolOption(compatibleWithV1, "Create a V1-compatible IPNS Record.").WithDefault(true),
ke.OptionIPNSBase,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Expand All @@ -96,6 +98,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
}

allowOffline, _ := req.Options[allowOfflineOptionName].(bool)
compatibleWithV1, _ := req.Options[compatibleWithV1].(bool)
kname, _ := req.Options[keyOptionName].(string)

validTimeOpt, _ := req.Options[lifeTimeOptionName].(string)
Expand All @@ -108,6 +111,7 @@ Alternatively, publish an <ipfs-path> using a valid PeerID (as listed by
options.Name.AllowOffline(allowOffline),
options.Name.Key(kname),
options.Name.ValidTime(validTime),
options.Name.CompatibleWithV1(compatibleWithV1),
}

if ttl, found := req.Options[ttlOptionName].(string); found {
Expand Down
Loading

0 comments on commit f6164e2

Please sign in to comment.