-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrpc.go
88 lines (76 loc) · 1.97 KB
/
rpc.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
package cborutil
import (
"bytes"
"encoding/hex"
"io"
"math"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
cbg "github.com/whyrusleeping/cbor-gen"
)
var log = logging.Logger("cborrrpc")
// Debug will produce more debugging messages
const Debug = false
func init() {
if Debug {
log.Warn("CBOR-RPC Debugging enabled")
}
}
// WriteCborRPC with encode an object to cbor, opting for fast path if possible
// and then write it into the given io.Writer
func WriteCborRPC(w io.Writer, obj interface{}) error {
if m, ok := obj.(cbg.CBORMarshaler); ok {
// TODO: impl debug
return m.MarshalCBOR(w)
}
data, err := cbor.DumpObject(obj)
if err != nil {
return err
}
if Debug {
log.Infof("> %s", hex.EncodeToString(data))
}
_, err = w.Write(data)
return err
}
// ReadCborRPC will read an object from the given io.Reader
// opting for fast path if possible
func ReadCborRPC(r io.Reader, out interface{}) error {
if um, ok := out.(cbg.CBORUnmarshaler); ok {
return um.UnmarshalCBOR(r)
}
return cbor.DecodeReader(r, out)
}
// Dump returns the cbor bytes representation of an object
func Dump(obj interface{}) ([]byte, error) {
var out bytes.Buffer
if err := WriteCborRPC(&out, obj); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// AsIpld converts an object to an ipld.Node interface
// TODO: this is a bit ugly, and this package is not exactly the best place
func AsIpld(obj interface{}) (ipld.Node, error) {
if m, ok := obj.(cbg.CBORMarshaler); ok {
b, err := Dump(m)
if err != nil {
return nil, err
}
return cbor.Decode(b, math.MaxUint64, -1)
}
return cbor.WrapObject(obj, math.MaxUint64, -1)
}
// Equals is true if two objects have the same cbor representation
func Equals(a cbg.CBORMarshaler, b cbg.CBORMarshaler) (bool, error) {
ab, err := Dump(a)
if err != nil {
return false, err
}
bb, err := Dump(b)
if err != nil {
return false, err
}
return bytes.Equal(ab, bb), nil
}