From de59ac1b44d6f3e8deaed0b2cbddb4d09a6bb11a Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Fri, 2 Jun 2023 10:35:31 -0400 Subject: [PATCH] feat(client/rpc): switch rpc client to use go-ipld-prime global decoders via go-ipld-legacy instead of go-ipld-format ones --- client/rpc/api.go | 15 +++++++++++++++ client/rpc/dag.go | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/client/rpc/api.go b/client/rpc/api.go index 4df4dfde123..404f4b312c8 100644 --- a/client/rpc/api.go +++ b/client/rpc/api.go @@ -10,6 +10,12 @@ import ( iface "github.com/ipfs/boxo/coreiface" caopts "github.com/ipfs/boxo/coreiface/options" + "github.com/ipfs/boxo/ipld/merkledag" + "github.com/ipfs/go-cid" + legacy "github.com/ipfs/go-ipld-legacy" + dagpb "github.com/ipld/go-codec-dagpb" + _ "github.com/ipld/go-ipld-prime/codec/dagcbor" + "github.com/ipld/go-ipld-prime/node/basicnode" "github.com/mitchellh/go-homedir" ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" @@ -35,6 +41,7 @@ type HttpApi struct { httpcli http.Client Headers http.Header applyGlobal func(*requestBuilder) + ipldDecoder *legacy.Decoder } // NewLocalApi tries to construct new HttpApi instance communicating with local @@ -125,11 +132,19 @@ func NewApiWithClient(a ma.Multiaddr, c *http.Client) (*HttpApi, error) { } func NewURLApiWithClient(url string, c *http.Client) (*HttpApi, error) { + decoder := legacy.NewDecoder() + // Add support for these codecs to match what is done in the merkledag library + // Note: to match prior behavior the go-ipld-prime CBOR decoder is manually included + // TODO: allow the codec registry used to be configured by the caller not through a global variable + decoder.RegisterCodec(cid.DagProtobuf, dagpb.Type.PBNode, merkledag.ProtoNodeConverter) + decoder.RegisterCodec(cid.Raw, basicnode.Prototype.Bytes, merkledag.RawNodeConverter) + api := &HttpApi{ url: url, httpcli: *c, Headers: make(map[string][]string), applyGlobal: func(*requestBuilder) {}, + ipldDecoder: decoder, } // We don't support redirects. diff --git a/client/rpc/dag.go b/client/rpc/dag.go index 62b54697e63..69f83abfed8 100644 --- a/client/rpc/dag.go +++ b/client/rpc/dag.go @@ -34,7 +34,7 @@ func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error) return nil, err } - return format.DefaultBlockDecoder.Decode(blk) + return api.ipldDecoder.DecodeNode(ctx, blk) } func (api *HttpDagServ) GetMany(ctx context.Context, cids []cid.Cid) <-chan *format.NodeOption {