-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
322 lines (277 loc) · 7.89 KB
/
context.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package context
import (
"fmt"
"io"
"path/filepath"
"sync"
"time"
"github.com/corestario/cosmos-client/keys"
"github.com/cosmos/cosmos-sdk/codec"
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
tmlite "github.com/tendermint/tendermint/lite"
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"
)
// Context implements a typical CLI context created in SDK modules for
// transaction handling and queries.
type Context struct {
Codec *codec.Codec
Client rpcclient.Client
Keybase cryptokeys.Keybase
Output io.Writer
OutputFormat string
Height int64
NodeURI string
From string
AccountStore string
TrustNode bool
UseLedger bool
BroadcastMode string
PrintResponse bool
verifier tmlite.Verifier
VerifierHome string
Simulate bool
GenerateOnly bool
FromAddress sdk.AccAddress
FromName string
Indent bool
SkipConfirm bool
Home string
Passphrase string
PrivKey crypto.PrivKey
mtx sync.RWMutex
}
// NewContext returns a new initialized Context
func NewContext(chainID string, nodeURI string, home string) (*Context, error) {
var (
rpc rpcclient.Client
err error
)
if nodeURI != "" {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
}
verifier, err := createVerifier(chainID, home, nodeURI)
if err != nil {
return nil, err
}
return &Context{
Client: rpc,
NodeURI: nodeURI,
AccountStore: AccountStoreKey,
verifier: verifier,
Home: home,
BroadcastMode: BroadcastSync,
}, nil
}
func NewContextWithDelay(chainID string, nodeURI string, home string) (*Context, error) {
var (
rpc rpcclient.Client
ctx *Context
)
//t := strconv.FormatInt(time.Now().UnixNano(), 10)
//home += t
if nodeURI != "" {
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
} else {
return nil, fmt.Errorf("no nodeURI specified")
}
var verifier tmlite.Verifier
ctx = &Context{
Client: rpc,
NodeURI: nodeURI,
AccountStore: AccountStoreKey,
verifier: verifier,
Home: home,
BroadcastMode: BroadcastSync,
}
go func() {
for {
node := rpcclient.NewHTTP(nodeURI, "/websocket")
st, err := node.Status()
if err != nil {
fmt.Printf("node is not running, status: %#+v", st)
time.Sleep(time.Second * 4)
} else {
break
}
}
verifier, err := createVerifier(chainID, home, nodeURI)
if err != nil {
fmt.Printf("could not create verifier, error: %v", err)
}
ctx.WithVerifier(verifier)
}()
return ctx, nil
}
func createVerifier(chainID string, home string, nodeURI string) (tmlite.Verifier, error) {
if chainID == "" {
return nil, errors.New("Invalid chainID")
}
if home == "" {
return nil, errors.New("Invalid home")
}
if nodeURI == "" {
return nil, errors.New("Invalid nodeURI")
}
node := rpcclient.NewHTTP(nodeURI, "/websocket")
cacheSize := 10 // TODO: determine appropriate cache size
verifier, err := tmliteProxy.NewVerifier(
chainID, filepath.Join(home, ".gaialite"),
node, log.NewNopLogger(), cacheSize,
)
if err != nil {
return nil, err
}
return verifier, nil
}
// WithCodec returns a copy of the context with an updated codec.
func (ctx *Context) WithCodec(cdc *codec.Codec) *Context {
ctx.Codec = cdc
return ctx
}
// WithHeight returns a copy of the context with an updated height.
func (ctx *Context) WithHeight(height int64) *Context {
ctx.Height = height
return ctx
}
// WithHome returns a copy of the context with an updated home.
func (ctx *Context) WithHome(home string) *Context {
ctx.Home = home
return ctx
}
// WithOutput returns a copy of the context with an updated output writer (e.g. stdout).
func (ctx *Context) WithOutput(w io.Writer) *Context {
ctx.Output = w
return ctx
}
// WithAccountStore returns a copy of the context with an updated AccountStore.
func (ctx *Context) WithAccountStore(accountStore string) *Context {
ctx.AccountStore = accountStore
return ctx
}
// WithFrom returns a copy of the context with an updated from address or name.
func (ctx *Context) WithFrom(from string) *Context {
ctx.From = from
return ctx
}
// WithTrustNode returns a copy of the context with an updated TrustNode flag.
func (ctx *Context) WithTrustNode(trustNode bool) *Context {
ctx.TrustNode = trustNode
return ctx
}
// WithNodeURI returns a copy of the context with an updated node URI.
func (ctx *Context) WithNodeURI(nodeURI string) *Context {
ctx.NodeURI = nodeURI
ctx.Client = rpcclient.NewHTTP(nodeURI, "/websocket")
return ctx
}
// WithClient returns a copy of the context with an updated RPC client
// instance.
func (ctx *Context) WithClient(client rpcclient.Client) *Context {
ctx.Client = client
return ctx
}
// WithUseLedger returns a copy of the context with an updated UseLedger flag.
func (ctx *Context) WithUseLedger(useLedger bool) *Context {
ctx.UseLedger = useLedger
return ctx
}
// WithPassphrase returns a copy of the context with an passphrase for signing tx.
func (ctx *Context) WithPassphrase(passphrase string) *Context {
ctx.Passphrase = passphrase
return ctx
}
// WithPassphrase returns a copy of the context with an private key for signing tx.
func (ctx *Context) WithPrivKey(privKey crypto.PrivKey) *Context {
ctx.PrivKey = privKey
return ctx
}
// WithVerifier - return a copy of the context with an updated Verifier
func (ctx *Context) WithVerifier(verifier tmlite.Verifier) *Context {
ctx.mtx.Lock()
ctx.mtx.Unlock()
ctx.verifier = verifier
return ctx
}
func (ctx *Context) GetVerifier() tmlite.Verifier {
ctx.mtx.RLock()
defer ctx.mtx.RUnlock()
return ctx.verifier
}
// WithGenerateOnly returns a copy of the context with updated GenerateOnly value
func (ctx *Context) WithGenerateOnly(generateOnly bool) *Context {
ctx.GenerateOnly = generateOnly
return ctx
}
// WithSimulation returns a copy of the context with updated Simulate value
func (ctx *Context) WithSimulation(simulate bool) *Context {
ctx.Simulate = simulate
return ctx
}
// WithFromName returns a copy of the context with an updated from account name.
func (ctx *Context) WithFromName(name string) *Context {
ctx.FromName = name
return ctx
}
// WithFromAddress returns a copy of the context with an updated from account
// address.
func (ctx *Context) WithFromAddress(addr sdk.AccAddress) *Context {
ctx.FromAddress = addr
return ctx
}
// WithBroadcastMode returns a copy of the context with an updated broadcast
// mode.
func (ctx *Context) WithBroadcastMode(mode string) *Context {
ctx.BroadcastMode = mode
return ctx
}
// PrintOutput prints output while respecting output and indent flags
// NOTE: pass in marshalled structs that have been unmarshaled
// because this function will panic on marshaling errors
func (ctx *Context) PrintOutput(toPrint fmt.Stringer) (err error) {
var out []byte
switch ctx.OutputFormat {
case "text":
out = []byte(toPrint.String())
case "json":
if ctx.Indent {
out, err = ctx.Codec.MarshalJSONIndent(toPrint, "", " ")
} else {
out, err = ctx.Codec.MarshalJSON(toPrint)
}
}
if err != nil {
return
}
fmt.Println(string(out))
return
}
// GetFromFields returns a from account address and Keybase name given either
// an address or key name. If genOnly is true, only a valid Bech32 cosmos
// address is returned.
func GetFromFields(from string, home string) (sdk.AccAddress, string, error) {
if from == "" {
return nil, "", nil
}
keybase, err := keys.NewKeyBaseFromDir(home)
if err != nil {
return nil, "", err
}
var info cryptokeys.Info
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
info, err = keybase.GetByAddress(addr)
if err != nil {
return nil, "", err
}
} else {
info, err = keybase.Get(from)
if err != nil {
return nil, "", err
}
}
return info.GetAddress(), info.GetName(), nil
}