@@ -27,11 +27,12 @@ import (
27
27
)
28
28
29
29
var (
30
- ErrNotRunning = errors .New ("tx client: not running" )
31
- ErrSyncTimedOut = errors .New ("tx client: timed-out waiting for sequence sync" )
32
- ErrNodeCatchingUp = errors .New ("tx client: cannot sync from catching up node" )
33
- ErrAdjustGas = errors .New ("tx client: couldn't adjust gas" )
34
- ErrOffline = errors .New ("tx client: cannot broadcast in offline mode" )
30
+ ErrNotRunning = errors .New ("tx client: not running" )
31
+ ErrSyncTimedOut = errors .New ("tx client: timed-out waiting for sequence sync" )
32
+ ErrNodeCatchingUp = errors .New ("tx client: cannot sync from catching up node" )
33
+ ErrAdjustGas = errors .New ("tx client: couldn't adjust gas" )
34
+ ErrSimulateOffline = errors .New ("tx client: cannot simulate tx in offline mode" )
35
+ ErrBroadcastOffline = errors .New ("tx client: cannot broadcast tx in offline mode" )
35
36
)
36
37
37
38
const (
@@ -118,13 +119,11 @@ func newSerialTx(ctx context.Context, cctx sdkclient.Context, flags *pflag.FlagS
118
119
}
119
120
120
121
// populate account number, current sequence number
121
- poptxf , err : = PrepareFactory (cctx , txf )
122
+ txf , err = PrepareFactory (cctx , txf )
122
123
if err != nil {
123
124
return nil , err
124
125
}
125
126
126
- poptxf = poptxf .WithSimulateAndExecute (true )
127
-
128
127
client := & serialBroadcaster {
129
128
ctx : ctx ,
130
129
cctx : cctx ,
@@ -140,7 +139,7 @@ func newSerialTx(ctx context.Context, cctx sdkclient.Context, flags *pflag.FlagS
140
139
141
140
go client .lc .WatchContext (ctx )
142
141
go client .run ()
143
- go client .broadcaster (poptxf )
142
+ go client .broadcaster (txf )
144
143
145
144
if ! client .cctx .Offline {
146
145
go client .sequenceSync ()
@@ -234,20 +233,20 @@ loop:
234
233
}
235
234
}
236
235
237
- func (c * serialBroadcaster ) broadcaster (txf tx.Factory ) {
238
- syncSequence := func (rErr error ) bool {
236
+ func (c * serialBroadcaster ) broadcaster (ptxf tx.Factory ) {
237
+ syncSequence := func (f tx. Factory , rErr error ) ( uint64 , bool ) {
239
238
if rErr != nil {
240
239
if sdkerrors .ErrWrongSequence .Is (rErr ) {
241
240
// attempt to sync account sequence
242
- if rSeq , err := c .syncAccountSequence (txf .Sequence ()); err == nil {
243
- txf = txf . WithSequence ( rSeq + 1 )
241
+ if rSeq , err := c .syncAccountSequence (f .Sequence ()); err == nil {
242
+ return rSeq + 1 , true
244
243
}
245
244
246
- return true
245
+ return f . Sequence (), true
247
246
}
248
247
}
249
248
250
- return false
249
+ return f . Sequence (), false
251
250
}
252
251
253
252
for {
@@ -258,12 +257,19 @@ func (c *serialBroadcaster) broadcaster(txf tx.Factory) {
258
257
var err error
259
258
var resp interface {}
260
259
261
- resp , txf , err = AdjustGas (c .cctx , txf , req .msgs ... )
262
- if err == nil && ! c .cctx .Simulate {
260
+ if c .cctx .GenerateOnly {
261
+ resp , err = c .generateTxs (ptxf , req .msgs ... )
262
+ } else {
263
263
done:
264
264
for i := 0 ; i < 2 ; i ++ {
265
- resp , txf , err = c .broadcastTxs (txf , req .msgs ... )
266
- if ! syncSequence (err ) {
265
+ var rseq uint64
266
+ resp , rseq , err = c .broadcastTxs (ptxf , req .msgs ... )
267
+ ptxf = ptxf .WithSequence (rseq )
268
+
269
+ rSeq , synced := syncSequence (ptxf , err )
270
+ ptxf = ptxf .WithSequence (rSeq )
271
+
272
+ if ! synced {
267
273
break done
268
274
}
269
275
}
@@ -275,8 +281,9 @@ func (c *serialBroadcaster) broadcaster(txf tx.Factory) {
275
281
}
276
282
277
283
terr := & sdkerrors.Error {}
278
- if errors .Is (err , terr ) {
279
- _ = syncSequence (err )
284
+ if ! c .cctx .GenerateOnly && errors .Is (err , terr ) {
285
+ rSeq , _ := syncSequence (ptxf , err )
286
+ ptxf = ptxf .WithSequence (rSeq )
280
287
}
281
288
282
289
select {
@@ -326,51 +333,84 @@ func (c *serialBroadcaster) sequenceSync() {
326
333
}
327
334
}
328
335
329
- func (c * serialBroadcaster ) broadcastTxs (txf tx.Factory , msgs ... sdk.Msg ) (interface {}, tx.Factory , error ) {
330
- var err error
336
+ func (c * serialBroadcaster ) generateTxs (txf tx.Factory , msgs ... sdk.Msg ) ([]byte , error ) {
337
+ if txf .SimulateAndExecute () {
338
+ if c .cctx .Offline {
339
+ return nil , ErrSimulateOffline
340
+ }
331
341
332
- txn , err := tx .BuildUnsignedTx (txf , msgs ... )
342
+ _ , adjusted , err := tx .CalculateGas (c .cctx , txf , msgs ... )
343
+ if err != nil {
344
+ return nil , err
345
+ }
346
+
347
+ txf = txf .WithGas (adjusted )
348
+ }
349
+
350
+ utx , err := tx .BuildUnsignedTx (txf , msgs ... )
333
351
if err != nil {
334
- return nil , txf , err
352
+ return nil , err
335
353
}
336
354
337
- txn .SetFeeGranter (c .cctx .GetFeeGranterAddress ())
355
+ data , err := c .cctx .TxConfig .TxJSONEncoder ()(utx .GetTx ())
356
+ if err != nil {
357
+ return nil , err
358
+ }
359
+
360
+ return data , nil
361
+ }
338
362
339
- if c .cctx .GenerateOnly {
340
- bytes , err := c .cctx .TxConfig .TxJSONEncoder ()(txn .GetTx ())
363
+ func (c * serialBroadcaster ) broadcastTxs (txf tx.Factory , msgs ... sdk.Msg ) (interface {}, uint64 , error ) {
364
+ var err error
365
+ var resp proto.Message
366
+
367
+ if txf .SimulateAndExecute () || c .cctx .Simulate {
368
+ var adjusted uint64
369
+ resp , adjusted , err = tx .CalculateGas (c .cctx , txf , msgs ... )
341
370
if err != nil {
342
- return nil , txf , err
371
+ return nil , txf . Sequence () , err
343
372
}
344
373
345
- return bytes , txf , nil
374
+ txf = txf .WithGas (adjusted )
375
+ }
376
+
377
+ if c .cctx .Simulate {
378
+ return resp , txf .Sequence (), nil
379
+ }
380
+
381
+ txn , err := tx .BuildUnsignedTx (txf , msgs ... )
382
+ if err != nil {
383
+ return nil , txf .Sequence (), err
346
384
}
347
385
348
386
if c .cctx .Offline {
349
- return nil , txf , ErrOffline
387
+ return nil , txf . Sequence (), ErrBroadcastOffline
350
388
}
351
389
390
+ txn .SetFeeGranter (c .cctx .GetFeeGranterAddress ())
391
+
352
392
err = tx .Sign (txf , c .info .GetName (), txn , true )
353
393
if err != nil {
354
- return nil , txf , err
394
+ return nil , txf . Sequence () , err
355
395
}
356
396
357
397
bytes , err := c .cctx .TxConfig .TxEncoder ()(txn .GetTx ())
358
398
if err != nil {
359
- return nil , txf , err
399
+ return nil , txf . Sequence () , err
360
400
}
361
401
362
402
response , err := c .doBroadcast (c .cctx , bytes , c .broadcastTimeout )
363
403
if err != nil {
364
- return response , txf , err
404
+ return response , txf . Sequence () , err
365
405
}
366
406
367
407
if response .Code != 0 {
368
- return response , txf , sdkerrors .ABCIError (response .Codespace , response .Code , response .RawLog )
408
+ return response , txf . Sequence () , sdkerrors .ABCIError (response .Codespace , response .Code , response .RawLog )
369
409
}
370
410
371
411
txf = txf .WithSequence (txf .Sequence () + 1 )
372
412
373
- return response , txf , nil
413
+ return response , txf . Sequence () , nil
374
414
}
375
415
376
416
func (c * serialBroadcaster ) syncAccountSequence (lSeq uint64 ) (uint64 , error ) {
0 commit comments