-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
151 lines (127 loc) · 3.58 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package types
import (
"encoding/binary"
"encoding/json"
"fmt"
dbm "github.com/tendermint/tm-db"
"time"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
tmtypes "github.com/tendermint/tendermint/types"
)
var (
// This is set at compile time. Could be cleveldb, defaults is goleveldb.
DBBackend = ""
backend = dbm.GoLevelDBBackend
)
// NewLevelDB instantiate a new LevelDB instance according to DBBackend.
func NewLevelDB(name, dir string) (db dbm.DB, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("couldn't create db: %v", r)
}
}()
return dbm.NewDB(name, backend, dir)
}
// SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces
// are removed.
// This method can be used to canonicalize JSON to be returned by GetSignBytes,
// e.g. for the ledger integration.
// If the passed JSON isn't valid it will return an error.
func SortJSON(toSortJSON []byte) ([]byte, error) {
var c interface{}
err := json.Unmarshal(toSortJSON, &c)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
js, err := json.Marshal(c)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return js, nil
}
// MustSortJSON is like SortJSON but panic if an error occurs, e.g., if
// the passed JSON isn't valid.
func MustSortJSON(toSortJSON []byte) []byte {
js, err := SortJSON(toSortJSON)
if err != nil {
panic(err)
}
return js
}
// BigEndianToUint64 returns an uint64 from big endian encoded bytes. If encoding
// is empty, zero is returned.
func BigEndianToUint64(bz []byte) uint64 {
if len(bz) == 0 {
return 0
}
return binary.BigEndian.Uint64(bz)
}
// Uint64ToBigEndian - marshals uint64 to a bigendian byte slice so it can be sorted
func Uint64ToBigEndian(i uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, i)
return b
}
// Slight modification of the RFC3339Nano but it right pads all zeros and drops the time zone info
const SortableTimeFormat = "2006-01-02T15:04:05.000000000"
// Formats a time.Time into a []byte that can be sorted
func FormatTimeBytes(t time.Time) []byte {
return []byte(t.UTC().Round(0).Format(SortableTimeFormat))
}
// Parses a []byte encoded using FormatTimeKey back into a time.Time
func ParseTimeBytes(bz []byte) (time.Time, error) {
str := string(bz)
t, err := time.Parse(SortableTimeFormat, str)
if err != nil {
return t, err
}
return t.UTC().Round(0), nil
}
// DefaultChainID returns the chain ID from the genesis file if present. An
// error is returned if the file cannot be read or parsed.
//
// TODO: This should be removed and the chainID should always be provided by
// the end user.
func DefaultChainID() (string, error) {
cfg, err := tcmd.ParseConfig()
if err != nil {
return "", err
}
doc, err := tmtypes.GenesisDocFromFile(cfg.GenesisFile())
if err != nil {
return "", err
}
return doc.ChainID, nil
}
func Paginate(numObjs, page, limit, defLimit int) (start, end int) {
if page == 0 {
// invalid start page
return -1, -1
} else if limit == 0 {
limit = defLimit
}
start = (page - 1) * limit
end = limit + start
if end >= numObjs {
end = numObjs
}
if start >= numObjs {
// page is out of bounds
return -1, -1
}
return start, end
}
//types Heights struct {
// height []int64 `json:"height"`
//}
type Heights []int64
type TxInfo struct {
Hash string `json:"hash"`
Index uint32 `json:"index"`
}
type TxsResult struct {
Height int64 `json:"height"`
Txs []TxInfo `json:"hashes"`
Error string `json:"error"`
}