forked from CyberMiles/travis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
51 lines (45 loc) · 1.23 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
package api
import (
"encoding/json"
"github.com/spf13/cast"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/CyberMiles/travis/sdk/client"
"github.com/CyberMiles/travis/types"
)
func (s *CmtRPCService) getParsedFromJson(path string, key []byte, ptr interface{}, height uint64) (int64, error) {
bs, h, err := s.get(path, key, cast.ToInt64(height))
if err != nil {
return 0, err
}
if len(bs) == 0 {
return h, client.ErrNoData()
}
err = json.Unmarshal(bs, ptr)
if err != nil {
return 0, err
}
return h, nil
}
func (s *CmtRPCService) getParsedFromCdc(path string, key []byte, ptr interface{}, height uint64) (int64, error) {
bs, h, err := s.get(path, key, cast.ToInt64(height))
if err != nil {
return 0, err
}
if len(bs) == 0 {
return h, client.ErrNoData()
}
err = types.Cdc.UnmarshalBinary(bs, ptr)
if err != nil {
return 0, err
}
return h, nil
}
func (s *CmtRPCService) get(path string, key []byte, height int64) ([]byte, int64, error) {
node := s.backend.GetLocalClient()
resp, err := node.ABCIQueryWithOptions(path, key,
rpcclient.ABCIQueryOptions{Trusted: true, Height: int64(height)})
if resp == nil {
return nil, height, err
}
return resp.Response.Value, resp.Response.Height, err
}