-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
historical_info.go
57 lines (48 loc) · 1.77 KB
/
historical_info.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
package types
import (
"sort"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// HistoricalInfo contains the historical information that gets stored at each height
type HistoricalInfo struct {
Header abci.Header `json:"header" yaml:"header"`
ValSet []Validator `json:"valset" yaml:"valset"`
}
// NewHistoricalInfo will create a historical information struct from header and valset
// it will first sort valset before inclusion into historical info
func NewHistoricalInfo(header abci.Header, valSet []Validator) HistoricalInfo {
sort.Sort(Validators(valSet))
return HistoricalInfo{
Header: header,
ValSet: valSet,
}
}
// MustMarshalHistoricalInfo wll marshal historical info and panic on error
func MustMarshalHistoricalInfo(cdc *codec.Codec, hi HistoricalInfo) []byte {
return cdc.MustMarshalBinaryLengthPrefixed(hi)
}
// MustUnmarshalHistoricalInfo wll unmarshal historical info and panic on error
func MustUnmarshalHistoricalInfo(cdc *codec.Codec, value []byte) HistoricalInfo {
hi, err := UnmarshalHistoricalInfo(cdc, value)
if err != nil {
panic(err)
}
return hi
}
// UnmarshalHistoricalInfo will unmarshal historical info and return any error
func UnmarshalHistoricalInfo(cdc *codec.Codec, value []byte) (hi HistoricalInfo, err error) {
err = cdc.UnmarshalBinaryLengthPrefixed(value, &hi)
return hi, err
}
// ValidateBasic will ensure HistoricalInfo is not nil and sorted
func ValidateBasic(hi HistoricalInfo) error {
if len(hi.ValSet) == 0 {
return sdkerrors.Wrap(ErrInvalidHistoricalInfo, "validator set is empty")
}
if !sort.IsSorted(Validators(hi.ValSet)) {
return sdkerrors.Wrap(ErrInvalidHistoricalInfo, "validator set is not sorted by address")
}
return nil
}