-
Notifications
You must be signed in to change notification settings - Fork 536
/
consensus.go
66 lines (56 loc) · 1.81 KB
/
consensus.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
package cosmosclient
import (
"context"
"encoding/base64"
"time"
"github.com/cometbft/cometbft/libs/bytes"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
)
// ConsensusInfo is the validator consensus info.
type ConsensusInfo struct {
Timestamp string `json:"Timestamp"`
Root string `json:"Root"`
NextValidatorsHash string `json:"NextValidatorsHash"`
ValidatorSet *tmproto.ValidatorSet `json:"ValidatorSet"`
}
// ConsensusInfo returns the appropriate tendermint consensus state by given height
// and the validator set for the next height.
func (c Client) ConsensusInfo(ctx context.Context, height int64) (ConsensusInfo, error) {
node, err := c.Context().GetNode()
if err != nil {
return ConsensusInfo{}, err
}
commit, err := node.Commit(ctx, &height)
if err != nil {
return ConsensusInfo{}, err
}
var (
page = 1
count = 10_000
)
validators, err := node.Validators(ctx, &height, &page, &count)
if err != nil {
return ConsensusInfo{}, err
}
protoValset, err := tmtypes.NewValidatorSet(validators.Validators).ToProto()
if err != nil {
return ConsensusInfo{}, err
}
heightNext := height + 1
validatorsNext, err := node.Validators(ctx, &heightNext, &page, &count)
if err != nil {
return ConsensusInfo{}, err
}
var (
hash = tmtypes.NewValidatorSet(validatorsNext.Validators).Hash()
root = commitmenttypes.NewMerkleRoot(commit.AppHash)
)
return ConsensusInfo{
Timestamp: commit.Time.Format(time.RFC3339Nano),
NextValidatorsHash: bytes.HexBytes(hash).String(),
Root: base64.StdEncoding.EncodeToString(root.Hash),
ValidatorSet: protoValset,
}, nil
}