-
Notifications
You must be signed in to change notification settings - Fork 15
/
query.go
54 lines (45 loc) · 1.37 KB
/
query.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
package query
import (
"encoding/binary"
"fmt"
"github.com/AccumulateNetwork/accumulate/smt/common"
"github.com/AccumulateNetwork/accumulate/types"
)
//Query will route a query message to a chain state
type Query struct {
Type types.QueryType
RouteId uint64
Content []byte //currently this is a transaction hash, but will make it more focused to the chain it is querying, so this will probably act more like general transaction does for transactions.
}
func Type(data []byte) (qt types.QueryType) {
defer func() {
if r := recover(); r != nil {
qt = types.QueryTypeUnknown
}
}()
qt = types.QueryTypeUnknown
if len(data) != 8 {
qt = types.QueryType(binary.LittleEndian.Uint64(data))
}
return qt
}
func (t *Query) MarshalBinary() (data []byte, err error) {
var d [8]byte
binary.LittleEndian.PutUint64(d[:], t.Type.AsUint64())
data = append(data, d[:]...)
binary.LittleEndian.PutUint64(d[:], t.RouteId)
data = append(data, d[:]...)
data = append(data, common.SliceBytes(t.Content)...)
return data, nil
}
func (t *Query) UnmarshalBinary(data []byte) (err error) {
defer func() {
if rErr := recover(); rErr != nil {
err = fmt.Errorf("insufficent data to unmarshal query %v", rErr)
}
}()
t.Type = types.QueryType(binary.LittleEndian.Uint64(data))
t.RouteId = binary.LittleEndian.Uint64(data[8:])
t.Content, data = common.BytesSlice(data[16:])
return nil
}