/
client.go
971 lines (852 loc) · 28.2 KB
/
client.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
package client
import (
"context"
"errors"
"fmt"
"sort"
"time"
"github.com/dymensionxyz/dymint/types"
"github.com/dymensionxyz/dymint/version"
sdkerrors "cosmossdk.io/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/config"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmmath "github.com/tendermint/tendermint/libs/math"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/p2p"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proxy"
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
tm_version "github.com/tendermint/tendermint/version"
"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/node"
)
const (
defaultPerPage = 30
maxPerPage = 100
// TODO(tzdybal): make this configurable
subscribeTimeout = 5 * time.Second
)
// ErrConsensusStateNotAvailable is returned because Dymint doesn't use Tendermint consensus.
var ErrConsensusStateNotAvailable = errors.New("consensus state not available in Dymint")
var _ rpcclient.Client = &Client{}
// Client implements tendermint RPC client interface.
//
// This is the type that is used in communication between cosmos-sdk app and Dymint.
type Client struct {
*tmtypes.EventBus
config *config.RPCConfig
node *node.Node
}
// NewClient returns Client working with given node.
func NewClient(node *node.Node) *Client {
return &Client{
EventBus: node.EventBus(),
config: config.DefaultRPCConfig(),
node: node,
}
}
// ABCIInfo returns basic information about application state.
func (c *Client) ABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error) {
resInfo, err := c.query().InfoSync(proxy.RequestInfo)
if err != nil {
return nil, err
}
return &ctypes.ResultABCIInfo{Response: *resInfo}, nil
}
// ABCIQuery queries for data from application.
func (c *Client) ABCIQuery(ctx context.Context, path string, data tmbytes.HexBytes) (*ctypes.ResultABCIQuery, error) {
return c.ABCIQueryWithOptions(ctx, path, data, rpcclient.DefaultABCIQueryOptions)
}
// ABCIQueryWithOptions queries for data from application.
func (c *Client) ABCIQueryWithOptions(ctx context.Context, path string, data tmbytes.HexBytes, opts rpcclient.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error) {
resQuery, err := c.query().QuerySync(abci.RequestQuery{
Path: path,
Data: data,
Height: opts.Height,
Prove: opts.Prove,
})
if err != nil {
return nil, err
}
c.Logger.Debug("ABCIQuery", "path", path, "height", resQuery.Height)
return &ctypes.ResultABCIQuery{Response: *resQuery}, nil
}
// BroadcastTxCommit returns with the responses from CheckTx and DeliverTx.
// More: https://docs.tendermint.com/master/rpc/#/Tx/broadcast_tx_commit
func (c *Client) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
// This implementation corresponds to Tendermints implementation from rpc/core/mempool.go.
// ctx.RemoteAddr godoc: If neither HTTPReq nor WSConn is set, an empty string is returned.
// This code is a local client, so we can assume that subscriber is ""
subscriber := "" // ctx.RemoteAddr()
if err := c.IsSubscriptionAllowed(subscriber); err != nil {
return nil, sdkerrors.Wrap(err, "subscription not allowed")
}
// Subscribe to tx being committed in block.
subCtx, cancel := context.WithTimeout(ctx, subscribeTimeout)
defer cancel()
q := tmtypes.EventQueryTxFor(tx)
deliverTxSub, err := c.EventBus.Subscribe(subCtx, subscriber, q)
if err != nil {
err = fmt.Errorf("subscribe to tx: %w", err)
c.Logger.Error("on broadcast_tx_commit", "err", err)
return nil, err
}
defer func() {
if err := c.EventBus.Unsubscribe(context.Background(), subscriber, q); err != nil {
c.Logger.Error("unsubscribing from eventBus", "err", err)
}
}()
// add to mempool and wait for CheckTx result
checkTxResCh := make(chan *abci.Response, 1)
err = c.node.Mempool.CheckTx(tx, func(res *abci.Response) {
select {
case <-ctx.Done():
case checkTxResCh <- res:
}
}, mempool.TxInfo{})
if err != nil {
c.Logger.Error("on broadcastTxCommit", "err", err)
return nil, fmt.Errorf("on broadcastTxCommit: %w", err)
}
select {
case <-ctx.Done():
return nil, fmt.Errorf("broadcast confirmation not received: %w", ctx.Err())
case checkTxResMsg := <-checkTxResCh:
checkTxRes := checkTxResMsg.GetCheckTx()
if checkTxRes.Code != abci.CodeTypeOK {
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxRes,
DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(),
}, nil
}
// broadcast tx
err = c.node.P2P.GossipTx(ctx, tx)
if err != nil {
return nil, fmt.Errorf("tx added to local mempool but failure to broadcast: %w", err)
}
// Wait for the tx to be included in a block or timeout.
select {
case msg := <-deliverTxSub.Out(): // The tx was included in a block.
deliverTxRes := msg.Data().(tmtypes.EventDataTx)
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxRes,
DeliverTx: deliverTxRes.Result,
Hash: tx.Hash(),
Height: deliverTxRes.Height,
}, nil
case <-deliverTxSub.Cancelled():
var reason string
if deliverTxSub.Err() == nil {
reason = "Dymint exited"
} else {
reason = deliverTxSub.Err().Error()
}
err = fmt.Errorf("deliverTxSub was cancelled (reason: %s)", reason)
c.Logger.Error("on broadcastTxCommit", "err", err)
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxRes,
DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(),
}, err
case <-time.After(c.config.TimeoutBroadcastTxCommit):
err = errors.New("timed out waiting for tx to be included in a block")
c.Logger.Error("on broadcastTxCommit", "err", err)
return &ctypes.ResultBroadcastTxCommit{
CheckTx: *checkTxRes,
DeliverTx: abci.ResponseDeliverTx{},
Hash: tx.Hash(),
}, err
}
}
}
// BroadcastTxAsync returns right away, with no response. Does not wait for
// CheckTx nor DeliverTx results.
// More: https://docs.tendermint.com/master/rpc/#/Tx/broadcast_tx_async
func (c *Client) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, error) {
err := c.node.Mempool.CheckTx(tx, nil, mempool.TxInfo{})
if err != nil {
return nil, err
}
// gossipTx optimistically
err = c.node.P2P.GossipTx(ctx, tx)
if err != nil {
return nil, fmt.Errorf("tx added to local mempool but failed to gossip: %w", err)
}
return &ctypes.ResultBroadcastTx{Hash: tx.Hash()}, nil
}
// BroadcastTxSync returns with the response from CheckTx. Does not wait for
// DeliverTx result.
// More: https://docs.tendermint.com/master/rpc/#/Tx/broadcast_tx_sync
func (c *Client) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, error) {
resCh := make(chan *abci.Response, 1)
err := c.node.Mempool.CheckTx(tx, func(res *abci.Response) {
resCh <- res
}, mempool.TxInfo{})
if err != nil {
return nil, err
}
res := <-resCh
r := res.GetCheckTx()
// gossip the transaction if it's in the mempool.
// Note: we have to do this here because, unlike the tendermint mempool reactor, there
// is no routine that gossips transactions after they enter the pool
if r.Code == abci.CodeTypeOK {
err = c.node.P2P.GossipTx(ctx, tx)
if err != nil {
// the transaction must be removed from the mempool if it cannot be gossiped.
// if this does not occur, then the user will not be able to try again using
// this node, as the CheckTx call above will return an error indicating that
// the tx is already in the mempool
_ = c.node.Mempool.RemoveTxByKey(tx.Key())
return nil, fmt.Errorf("gossip tx: %w", err)
}
}
return &ctypes.ResultBroadcastTx{
Code: r.Code,
Data: r.Data,
Log: r.Log,
Codespace: r.Codespace,
Hash: tx.Hash(),
}, nil
}
// Subscribe subscribe given subscriber to a query.
func (c *Client) Subscribe(ctx context.Context, subscriber, query string, outCapacity ...int) (out <-chan ctypes.ResultEvent, err error) {
q, err := tmquery.New(query)
if err != nil {
return nil, fmt.Errorf("parse query: %w", err)
}
outCap := 1
if len(outCapacity) > 0 {
outCap = outCapacity[0]
}
var sub tmtypes.Subscription
if outCap > 0 {
sub, err = c.EventBus.Subscribe(ctx, subscriber, q, outCap)
} else {
sub, err = c.EventBus.SubscribeUnbuffered(ctx, subscriber, q)
}
if err != nil {
return nil, fmt.Errorf("subscribe: %w", err)
}
outc := make(chan ctypes.ResultEvent, outCap)
go c.eventsRoutine(sub, subscriber, q, outc)
return outc, nil
}
// Unsubscribe unsubscribes given subscriber from a query.
func (c *Client) Unsubscribe(ctx context.Context, subscriber, query string) error {
q, err := tmquery.New(query)
if err != nil {
return fmt.Errorf("parse query: %w", err)
}
return c.EventBus.Unsubscribe(ctx, subscriber, q)
}
// Genesis returns entire genesis.
func (c *Client) Genesis(_ context.Context) (*ctypes.ResultGenesis, error) {
return &ctypes.ResultGenesis{Genesis: c.node.GetGenesis()}, nil
}
// GenesisChunked returns given chunk of genesis.
func (c *Client) GenesisChunked(context context.Context, id uint) (*ctypes.ResultGenesisChunk, error) {
genChunks, err := c.node.GetGenesisChunks()
if err != nil {
return nil, fmt.Errorf("while creating chunks of the genesis document: %w", err)
}
if genChunks == nil {
return nil, fmt.Errorf("service configuration error, genesis chunks are not initialized")
}
chunkLen := len(genChunks)
if chunkLen == 0 {
return nil, fmt.Errorf("service configuration error, there are no chunks")
}
if int(id) > chunkLen-1 {
return nil, fmt.Errorf("there are %d chunks, %d is invalid", chunkLen-1, id)
}
return &ctypes.ResultGenesisChunk{
TotalChunks: chunkLen,
ChunkNumber: int(id),
Data: genChunks[id],
}, nil
}
// BlockchainInfo returns ABCI block meta information for given height range.
func (c *Client) BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, error) {
const limit int64 = 20
// Currently blocks are not pruned and are synced linearly so the base height is 0
minHeight, maxHeight, err := filterMinMax(
0,
int64(c.node.Store.Height()),
minHeight,
maxHeight,
limit)
if err != nil {
return nil, err
}
c.Logger.Debug("BlockchainInfo", "maxHeight", maxHeight, "minHeight", minHeight)
blocks := make([]*tmtypes.BlockMeta, 0, maxHeight-minHeight+1)
for height := maxHeight; height >= minHeight; height-- {
block, err := c.node.Store.LoadBlock(uint64(height))
if err != nil {
return nil, err
}
if block != nil {
tmblockmeta, err := types.ToABCIBlockMeta(block)
if err != nil {
return nil, err
}
blocks = append(blocks, tmblockmeta)
}
}
return &ctypes.ResultBlockchainInfo{
LastHeight: int64(c.node.Store.Height()),
BlockMetas: blocks,
}, nil
}
// NetInfo returns basic information about client P2P connections.
func (c *Client) NetInfo(ctx context.Context) (*ctypes.ResultNetInfo, error) {
res := ctypes.ResultNetInfo{
Listening: true,
}
for _, ma := range c.node.P2P.Addrs() {
res.Listeners = append(res.Listeners, ma.String())
}
peers := c.node.P2P.Peers()
res.NPeers = len(peers)
for _, peer := range peers {
res.Peers = append(res.Peers, ctypes.Peer{
NodeInfo: peer.NodeInfo,
IsOutbound: peer.IsOutbound,
ConnectionStatus: peer.ConnectionStatus,
RemoteIP: peer.RemoteIP,
})
}
return &res, nil
}
// DumpConsensusState always returns error as there is no consensus state in Dymint.
func (c *Client) DumpConsensusState(ctx context.Context) (*ctypes.ResultDumpConsensusState, error) {
return nil, ErrConsensusStateNotAvailable
}
// ConsensusState always returns error as there is no consensus state in Dymint.
func (c *Client) ConsensusState(ctx context.Context) (*ctypes.ResultConsensusState, error) {
return nil, ErrConsensusStateNotAvailable
}
// ConsensusParams returns consensus params at given height.
//
// Currently, consensus params changes are not supported and this method returns params as defined in genesis.
func (c *Client) ConsensusParams(ctx context.Context, height *int64) (*ctypes.ResultConsensusParams, error) {
// TODO(tzdybal): implement consensus params handling: https://github.com/dymensionxyz/dymint/issues/291
params := c.node.GetGenesis().ConsensusParams
return &ctypes.ResultConsensusParams{
BlockHeight: int64(c.normalizeHeight(height)),
ConsensusParams: tmproto.ConsensusParams{
Block: tmproto.BlockParams{
MaxBytes: params.Block.MaxBytes,
MaxGas: params.Block.MaxGas,
TimeIotaMs: params.Block.TimeIotaMs,
},
Evidence: tmproto.EvidenceParams{
MaxAgeNumBlocks: params.Evidence.MaxAgeNumBlocks,
MaxAgeDuration: params.Evidence.MaxAgeDuration,
MaxBytes: params.Evidence.MaxBytes,
},
Validator: tmproto.ValidatorParams{
PubKeyTypes: params.Validator.PubKeyTypes,
},
Version: tmproto.VersionParams{
AppVersion: params.Version.AppVersion,
},
},
}, nil
}
// Health endpoint returns empty value. It can be used to monitor service availability.
func (c *Client) Health(ctx context.Context) (*ctypes.ResultHealth, error) {
return &ctypes.ResultHealth{}, nil
}
// Block method returns BlockID and block itself for given height.
//
// If height is nil, it returns information about last known block.
func (c *Client) Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) {
heightValue := c.normalizeHeight(height)
block, err := c.node.Store.LoadBlock(heightValue)
if err != nil {
return nil, err
}
hash := block.Hash()
abciBlock, err := types.ToABCIBlock(block)
if err != nil {
return nil, err
}
return &ctypes.ResultBlock{
BlockID: tmtypes.BlockID{
Hash: hash[:],
PartSetHeader: tmtypes.PartSetHeader{
Total: 1,
Hash: hash[:],
},
},
Block: abciBlock,
}, nil
}
// BlockByHash returns BlockID and block itself for given hash.
func (c *Client) BlockByHash(ctx context.Context, hash []byte) (*ctypes.ResultBlock, error) {
var h [32]byte
copy(h[:], hash)
block, err := c.node.Store.LoadBlockByHash(h)
if err != nil {
return nil, err
}
abciBlock, err := types.ToABCIBlock(block)
if err != nil {
return nil, err
}
return &ctypes.ResultBlock{
BlockID: tmtypes.BlockID{
Hash: h[:],
PartSetHeader: tmtypes.PartSetHeader{
Total: 1,
Hash: h[:],
},
},
Block: abciBlock,
}, nil
}
// BlockResults returns information about transactions, events and updates of validator set and consensus params.
func (c *Client) BlockResults(ctx context.Context, height *int64) (*ctypes.ResultBlockResults, error) {
var h uint64
if height == nil {
h = c.node.Store.Height()
} else {
h = uint64(*height)
}
resp, err := c.node.Store.LoadBlockResponses(h)
if err != nil {
return nil, err
}
return &ctypes.ResultBlockResults{
Height: int64(h),
TxsResults: resp.DeliverTxs,
BeginBlockEvents: resp.BeginBlock.Events,
EndBlockEvents: resp.EndBlock.Events,
ValidatorUpdates: resp.EndBlock.ValidatorUpdates,
ConsensusParamUpdates: resp.EndBlock.ConsensusParamUpdates,
}, nil
}
// Commit returns signed header (aka commit) at given height.
func (c *Client) Commit(ctx context.Context, height *int64) (*ctypes.ResultCommit, error) {
heightValue := c.normalizeHeight(height)
com, err := c.node.Store.LoadCommit(heightValue)
if err != nil {
return nil, err
}
b, err := c.node.Store.LoadBlock(heightValue)
if err != nil {
return nil, err
}
commit := types.ToABCICommit(com, &b.Header)
block, err := types.ToABCIBlock(b)
if err != nil {
return nil, err
}
return ctypes.NewResultCommit(&block.Header, commit, true), nil
}
// Validators returns paginated list of validators at given height.
func (c *Client) Validators(ctx context.Context, heightPtr *int64, pagePtr, perPagePtr *int) (*ctypes.ResultValidators, error) {
height := c.normalizeHeight(heightPtr)
validators, err := c.node.Store.LoadValidators(height)
if err != nil {
return nil, fmt.Errorf("load validators for height %d: %w", height, err)
}
totalCount := len(validators.Validators)
perPage := validatePerPage(perPagePtr)
page, err := validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}
skipCount := validateSkipCount(page, perPage)
v := validators.Validators[skipCount : skipCount+tmmath.MinInt(perPage, totalCount-skipCount)]
return &ctypes.ResultValidators{
BlockHeight: int64(height),
Validators: v,
Count: len(v),
Total: totalCount,
}, nil
}
// Tx returns detailed information about transaction identified by its hash.
func (c *Client) Tx(ctx context.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) {
res, err := c.node.TxIndexer.Get(hash)
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("tx (%X) not found", hash)
}
height := res.Height
index := res.Index
var proof tmtypes.TxProof
if prove {
block, _ := c.node.Store.LoadBlock(uint64(height))
blockProof := block.Data.Txs.Proof(int(index)) // XXX: overflow on 32-bit machines
proof = tmtypes.TxProof{
RootHash: blockProof.RootHash,
Data: tmtypes.Tx(blockProof.Data),
Proof: blockProof.Proof,
}
}
return &ctypes.ResultTx{
Hash: hash,
Height: height,
Index: index,
TxResult: res.Result,
Tx: res.Tx,
Proof: proof,
}, nil
}
// TxSearch returns detailed information about transactions matching query.
func (c *Client) TxSearch(ctx context.Context, query string, prove bool, pagePtr, perPagePtr *int, orderBy string) (*ctypes.ResultTxSearch, error) {
q, err := tmquery.New(query)
if err != nil {
return nil, err
}
results, err := c.node.TxIndexer.Search(ctx, q)
if err != nil {
return nil, err
}
// sort results (must be done before pagination)
switch orderBy {
case "desc":
sort.Slice(results, func(i, j int) bool {
if results[i].Height == results[j].Height {
return results[i].Index > results[j].Index
}
return results[i].Height > results[j].Height
})
case "asc", "":
sort.Slice(results, func(i, j int) bool {
if results[i].Height == results[j].Height {
return results[i].Index < results[j].Index
}
return results[i].Height < results[j].Height
})
default:
return nil, errors.New("expected order_by to be either `asc` or `desc` or empty")
}
// paginate results
totalCount := len(results)
perPage := validatePerPage(perPagePtr)
page, err := validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}
skipCount := validateSkipCount(page, perPage)
pageSize := tmmath.MinInt(perPage, totalCount-skipCount)
apiResults := make([]*ctypes.ResultTx, 0, pageSize)
for i := skipCount; i < skipCount+pageSize; i++ {
r := results[i]
var proof tmtypes.TxProof
/*if prove {
block := nil //env.BlockStore.LoadBlock(r.Height)
proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines
}*/
apiResults = append(apiResults, &ctypes.ResultTx{
Hash: tmtypes.Tx(r.Tx).Hash(),
Height: r.Height,
Index: r.Index,
TxResult: r.Result,
Tx: r.Tx,
Proof: proof,
})
}
return &ctypes.ResultTxSearch{Txs: apiResults, TotalCount: totalCount}, nil
}
// BlockSearch defines a method to search for a paginated set of blocks by
// BeginBlock and EndBlock event search criteria.
func (c *Client) BlockSearch(ctx context.Context, query string, page, perPage *int, orderBy string) (*ctypes.ResultBlockSearch, error) {
q, err := tmquery.New(query)
if err != nil {
return nil, err
}
results, err := c.node.BlockIndexer.Search(ctx, q)
if err != nil {
return nil, err
}
// Sort the results
switch orderBy {
case "desc":
sort.Slice(results, func(i, j int) bool {
return results[i] > results[j]
})
case "asc", "":
sort.Slice(results, func(i, j int) bool {
return results[i] < results[j]
})
default:
return nil, errors.New("expected order_by to be either `asc` or `desc` or empty")
}
// Paginate
totalCount := len(results)
perPageVal := validatePerPage(perPage)
pageVal, err := validatePage(page, perPageVal, totalCount)
if err != nil {
return nil, err
}
skipCount := validateSkipCount(pageVal, perPageVal)
pageSize := tmmath.MinInt(perPageVal, totalCount-skipCount)
// Fetch the blocks
blocks := make([]*ctypes.ResultBlock, 0, pageSize)
for i := skipCount; i < skipCount+pageSize; i++ {
b, err := c.node.Store.LoadBlock(uint64(results[i]))
if err != nil {
return nil, err
}
block, err := types.ToABCIBlock(b)
if err != nil {
return nil, err
}
blocks = append(blocks, &ctypes.ResultBlock{
Block: block,
BlockID: tmtypes.BlockID{
Hash: block.Hash(),
},
})
}
return &ctypes.ResultBlockSearch{Blocks: blocks, TotalCount: totalCount}, nil
}
// Status returns detailed information about current status of the node.
func (c *Client) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
latest, err := c.node.Store.LoadBlock(c.node.Store.Height())
if err != nil {
// TODO(tzdybal): extract error
return nil, fmt.Errorf("find latest block: %w", err)
}
latestBlockHash := latest.Header.DataHash
latestAppHash := latest.Header.AppHash
latestHeight := latest.Header.Height
latestBlockTimeNano := latest.Header.Time
validators, err := c.node.Store.LoadValidators(latest.Header.Height)
if err != nil {
return nil, fmt.Errorf("fetch the validator info at latest block: %w", err)
}
_, validator := validators.GetByAddress(latest.Header.ProposerAddress)
if validator == nil {
return nil, fmt.Errorf("find proposer %s in the valSet", string(latest.Header.ProposerAddress))
}
state, err := c.node.Store.LoadState()
if err != nil {
return nil, fmt.Errorf("load the last saved state: %w", err)
}
defaultProtocolVersion := p2p.NewProtocolVersion(
tm_version.P2PProtocol,
state.Version.Consensus.Block,
state.Version.Consensus.App,
)
id, addr, network := c.node.P2P.Info()
txIndexerStatus := "on"
result := &ctypes.ResultStatus{
// TODO(ItzhakBokris): update NodeInfo fields
NodeInfo: p2p.DefaultNodeInfo{
ProtocolVersion: defaultProtocolVersion,
DefaultNodeID: id,
ListenAddr: addr,
Network: network,
Version: version.BuildVersion,
Channels: []byte{0x1},
Moniker: config.DefaultBaseConfig().Moniker,
Other: p2p.DefaultNodeInfoOther{
TxIndex: txIndexerStatus,
RPCAddress: c.config.ListenAddress,
},
},
SyncInfo: ctypes.SyncInfo{
LatestBlockHash: latestBlockHash[:],
LatestAppHash: latestAppHash[:],
LatestBlockHeight: int64(latestHeight),
LatestBlockTime: time.Unix(0, int64(latestBlockTimeNano)),
// TODO(tzdybal): add missing fields
// EarliestBlockHash: earliestBlockHash,
// EarliestAppHash: earliestAppHash,
// EarliestBlockHeight: earliestBloc
// kHeight,
// EarliestBlockTime: time.Unix(0, earliestBlockTimeNano),
// CatchingUp: env.ConsensusReactor.WaitSync(),
},
// TODO(ItzhakBokris): update ValidatorInfo fields
ValidatorInfo: ctypes.ValidatorInfo{
Address: validator.Address,
PubKey: validator.PubKey,
VotingPower: validator.VotingPower,
},
}
return result, nil
}
// BroadcastEvidence is not yet implemented.
func (c *Client) BroadcastEvidence(ctx context.Context, evidence tmtypes.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
return &ctypes.ResultBroadcastEvidence{
Hash: evidence.Hash(),
}, nil
}
// NumUnconfirmedTxs returns information about transactions in mempool.
func (c *Client) NumUnconfirmedTxs(ctx context.Context) (*ctypes.ResultUnconfirmedTxs, error) {
return &ctypes.ResultUnconfirmedTxs{
Count: c.node.Mempool.Size(),
Total: c.node.Mempool.Size(),
TotalBytes: c.node.Mempool.SizeBytes(),
}, nil
}
// UnconfirmedTxs returns transactions in mempool.
func (c *Client) UnconfirmedTxs(ctx context.Context, limitPtr *int) (*ctypes.ResultUnconfirmedTxs, error) {
// reuse per_page validator
limit := validatePerPage(limitPtr)
txs := c.node.Mempool.ReapMaxTxs(limit)
return &ctypes.ResultUnconfirmedTxs{
Count: len(txs),
Total: c.node.Mempool.Size(),
TotalBytes: c.node.Mempool.SizeBytes(),
Txs: txs,
}, nil
}
// CheckTx executes a new transaction against the application to determine its validity.
//
// If valid, the tx is automatically added to the mempool.
func (c *Client) CheckTx(ctx context.Context, tx tmtypes.Tx) (*ctypes.ResultCheckTx, error) {
res, err := c.mempool().CheckTxSync(abci.RequestCheckTx{Tx: tx})
if err != nil {
return nil, err
}
return &ctypes.ResultCheckTx{ResponseCheckTx: *res}, nil
}
func (c *Client) eventsRoutine(sub tmtypes.Subscription, subscriber string, q tmpubsub.Query, outc chan<- ctypes.ResultEvent) {
defer close(outc)
for {
select {
case msg := <-sub.Out():
result := ctypes.ResultEvent{Query: q.String(), Data: msg.Data(), Events: msg.Events()}
if cap(outc) == 0 {
outc <- result
} else {
select {
case outc <- result:
default:
c.Logger.Error("wanted to publish ResultEvent, but out channel is full", "result", result, "query", result.Query)
}
}
case <-sub.Cancelled():
if errors.Is(sub.Err(), tmpubsub.ErrUnsubscribed) {
return
}
c.Logger.Error("subscription was cancelled, resubscribing...", "err", sub.Err(), "query", q.String())
sub = c.resubscribe(subscriber, q)
if sub == nil { // client was stopped
return
}
case <-c.Quit():
return
}
}
}
// Try to resubscribe with exponential backoff.
func (c *Client) resubscribe(subscriber string, q tmpubsub.Query) tmtypes.Subscription {
attempts := 0
for {
if !c.IsRunning() {
return nil
}
sub, err := c.EventBus.Subscribe(context.Background(), subscriber, q)
if err == nil {
return sub
}
attempts++
time.Sleep((10 << uint(attempts)) * time.Millisecond) // 10ms -> 20ms -> 40ms
}
}
func (c *Client) consensus() proxy.AppConnConsensus {
return c.node.ProxyApp().Consensus()
}
func (c *Client) mempool() proxy.AppConnMempool {
return c.node.ProxyApp().Mempool()
}
func (c *Client) query() proxy.AppConnQuery {
return c.node.ProxyApp().Query()
}
func (c *Client) snapshot() proxy.AppConnSnapshot {
return c.node.ProxyApp().Snapshot()
}
func (c *Client) normalizeHeight(height *int64) uint64 {
var heightValue uint64
if height == nil || *height == 0 {
heightValue = c.node.Store.Height()
} else {
heightValue = uint64(*height)
}
return heightValue
}
func (c *Client) IsSubscriptionAllowed(subscriber string) error {
if c.EventBus.NumClients() >= c.config.MaxSubscriptionClients {
return fmt.Errorf("max_subscription_clients %d reached", c.config.MaxSubscriptionClients)
} else if c.EventBus.NumClientSubscriptions(subscriber) >= c.config.MaxSubscriptionsPerClient {
return fmt.Errorf("max_subscriptions_per_client %d reached", c.config.MaxSubscriptionsPerClient)
}
return nil
}
func validatePerPage(perPagePtr *int) int {
if perPagePtr == nil { // no per_page parameter
return defaultPerPage
}
perPage := *perPagePtr
if perPage < 1 {
return defaultPerPage
} else if perPage > maxPerPage {
return maxPerPage
}
return perPage
}
func validatePage(pagePtr *int, perPage, totalCount int) (int, error) {
if perPage < 1 {
panic(fmt.Sprintf("zero or negative perPage: %d", perPage))
}
if pagePtr == nil || *pagePtr <= 0 { // no page parameter
return 1, nil
}
pages := ((totalCount - 1) / perPage) + 1
if pages == 0 {
pages = 1 // one page (even if it's empty)
}
page := *pagePtr
if page > pages {
return 1, fmt.Errorf("page should be within [1, %d] range, given %d", pages, page)
}
return page, nil
}
func validateSkipCount(page, perPage int) int {
skipCount := (page - 1) * perPage
if skipCount < 0 {
return 0
}
return skipCount
}
func filterMinMax(base, height, min, max, limit int64) (int64, int64, error) {
// filter negatives
if min < 0 || max < 0 {
return min, max, errors.New("height must be greater than zero")
}
// adjust for default values
if min == 0 {
min = 1
}
if max == 0 {
max = height
}
// limit max to the height
max = tmmath.MinInt64(height, max)
// limit min to the base
min = tmmath.MaxInt64(base, min)
// limit min to within `limit` of max
// so the total number of blocks returned will be `limit`
min = tmmath.MaxInt64(min, max-limit+1)
if min > max {
return min, max, fmt.Errorf("%w: min height %d can't be greater than max height %d",
errors.New("invalid request"), min, max)
}
return min, max, nil
}