-
Notifications
You must be signed in to change notification settings - Fork 15
/
chain.go
84 lines (71 loc) · 2.01 KB
/
chain.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
package query
import (
"bytes"
"fmt"
"github.com/AccumulateNetwork/accumulate/internal/encoding"
"github.com/AccumulateNetwork/accumulate/types"
)
type RequestByUrl struct {
Url types.String
}
type RequestDirectory struct {
RequestByUrl
Start uint64
Limit uint64
ExpandChains bool
}
func (*RequestByUrl) Type() types.QueryType { return types.QueryTypeUrl }
func (r *RequestByUrl) MarshalBinary() ([]byte, error) {
return r.Url.MarshalBinary()
}
func (r *RequestByUrl) UnmarshalBinary(data []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("error unmarshaling RequestByUrl data %v", r)
}
}()
return r.Url.UnmarshalBinary(data)
}
func (*RequestDirectory) Type() types.QueryType { return types.QueryTypeDirectoryUrl }
func (r *RequestDirectory) MarshalBinary() ([]byte, error) {
var buffer bytes.Buffer
binary, err := r.Url.MarshalBinary()
if err != nil {
return nil, err
}
buffer.Write(binary)
buffer.Write(encoding.UvarintMarshalBinary(r.Start))
buffer.Write(encoding.UvarintMarshalBinary(r.Limit))
buffer.Write(encoding.BoolMarshalBinary(r.ExpandChains))
return buffer.Bytes(), nil
}
func (r *RequestDirectory) UnmarshalBinary(data []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("error unmarshaling RequestDirectory data %v", r)
}
}()
err = r.Url.UnmarshalBinary(data)
if err != nil {
return err
}
data = data[r.Url.Size(nil):]
if start, err := encoding.UvarintUnmarshalBinary(data); err != nil {
return fmt.Errorf("error decoding Start: %w", err)
} else {
r.Start = start
}
data = data[encoding.UvarintBinarySize(r.Start):]
if limit, err := encoding.UvarintUnmarshalBinary(data); err != nil {
return fmt.Errorf("error decoding Limit: %w", err)
} else {
r.Limit = limit
}
data = data[encoding.UvarintBinarySize(r.Limit):]
if expand, err := encoding.BoolUnmarshalBinary(data); err != nil {
return fmt.Errorf("error decoding ExpandChains: %w", err)
} else {
r.ExpandChains = expand
}
return err
}