/
utils.go
86 lines (70 loc) · 1.98 KB
/
utils.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
package lightclient
import (
"fmt"
rpcclient "github.com/tendermint/tendermint/rpc/client"
tmtypes "github.com/tendermint/tendermint/types"
)
func GetInitConsensusState(node rpcclient.Client, height int64) (*ConsensusState, error) {
status, err := node.Status()
if err != nil {
return nil, err
}
nextValHeight := height + 1
nextValidatorSet, err := node.Validators(&nextValHeight)
if err != nil {
return nil, err
}
header, err := node.Block(&height)
if err != nil {
return nil, err
}
appHash := header.BlockMeta.Header.AppHash
curValidatorSetHash := header.BlockMeta.Header.ValidatorsHash
cs := &ConsensusState{
ChainID: status.NodeInfo.Network,
Height: uint64(height),
AppHash: appHash,
CurValidatorSetHash: curValidatorSetHash,
NextValidatorSet: &tmtypes.ValidatorSet{
Validators: nextValidatorSet.Validators,
},
}
return cs, nil
}
func QueryTendermintHeader(node rpcclient.Client, height int64) (*Header, error) {
nextHeight := height + 1
commit, err := node.Commit(&height)
if err != nil {
return nil, err
}
validators, err := node.Validators(&height)
if err != nil {
return nil, err
}
nextvalidators, err := node.Validators(&nextHeight)
if err != nil {
return nil, err
}
header := &Header{
SignedHeader: commit.SignedHeader,
ValidatorSet: tmtypes.NewValidatorSet(validators.Validators),
NextValidatorSet: tmtypes.NewValidatorSet(nextvalidators.Validators),
}
return header, nil
}
func QueryKeyWithProof(node rpcclient.Client, key []byte, storeName string, height int64) ([]byte, []byte, []byte, error) {
opts := rpcclient.ABCIQueryOptions{
Height: height,
Prove: true,
}
path := fmt.Sprintf("/store/%s/%s", storeName, "key")
result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
return nil, nil, nil, err
}
proofBytes, err := result.Response.Proof.Marshal()
if err != nil {
return nil, nil, nil, err
}
return key, result.Response.Value, proofBytes, nil
}