-
Notifications
You must be signed in to change notification settings - Fork 17.4k
Expand file tree
/
Copy pathquery.go
More file actions
1036 lines (953 loc) · 37.6 KB
/
Copy pathquery.go
File metadata and controls
1036 lines (953 loc) · 37.6 KB
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rpc
import (
"bytes"
"encoding/json"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"slices"
"strconv"
"github.com/canopy-network/canopy/fsm"
"github.com/canopy-network/canopy/lib"
"github.com/canopy-network/canopy/lib/crypto"
"github.com/canopy-network/canopy/store"
"github.com/julienschmidt/httprouter"
"github.com/nsf/jsondiff"
)
// Version writes Canopy software's version information
func (s *Server) Version(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
write(w, SoftwareVersion, http.StatusOK)
}
// Transaction submits a transaction
func (s *Server) Transaction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Create a new instance of lib.Transaction to hold the incoming transaction data.
tx := new(lib.Transaction)
// Unmarshal the HTTP request body into the transaction instance.
if ok := unmarshal(w, r, tx); !ok {
return
}
// Submit transaction to RPC server
s.submitTxs(w, []lib.TransactionI{tx})
}
// Transactions handles multiple transactions in a single request
func (s *Server) Transactions(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// create a slice to hold the incoming transactions
var txs []lib.Transaction
// unmarshal the HTTP request body into the transactions slice
if ok := unmarshal(w, r, &txs); !ok {
return
}
// cast txs to lib.TransactionI
txsI := make([]lib.TransactionI, len(txs))
for i := range txs {
txsI[i] = &txs[i]
}
// submit transactions to RPC server
s.submitTxs(w, txsI)
}
// Height responds with the next block version
func (s *Server) Height(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
// Create a read-only state for the latest block and write the height
s.readOnlyState(0, func(state *fsm.StateMachine) lib.ErrorI {
write(w, &lib.HeightResult{
Height: state.Height(),
}, http.StatusOK)
return nil
})
}
// Account responds with an account for the specified address
func (s *Server) Account(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndAddressParams(w, r, func(s *fsm.StateMachine, a lib.HexBytes) (interface{}, lib.ErrorI) {
return s.GetAccount(crypto.NewAddressFromBytes(a))
})
}
// Accounts responds with accounts based on the page parameters
func (s *Server) Accounts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightPaginated(w, r, func(s *fsm.StateMachine, p *paginatedHeightRequest) (interface{}, lib.ErrorI) {
return s.GetAccountsPaginated(p.PageParams)
})
}
// Pool returns a Pool structure for a specific ID
func (s *Server) Pool(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndIdParams(w, r, func(s *fsm.StateMachine, id uint64) (interface{}, lib.ErrorI) {
return s.GetPool(id)
})
}
// Pools returns a page of pools
func (s *Server) Pools(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightPaginated(w, r, func(s *fsm.StateMachine, p *paginatedHeightRequest) (interface{}, lib.ErrorI) {
return s.GetPoolsPaginated(p.PageParams)
})
}
// Validator gets the validator at the specified address
func (s *Server) Validator(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndAddressParams(w, r, func(s *fsm.StateMachine, a lib.HexBytes) (interface{}, lib.ErrorI) {
return s.GetValidator(crypto.NewAddressFromBytes(a))
})
}
// Validators returns a page of filtered validators
func (s *Server) Validators(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightPaginated(w, r, func(s *fsm.StateMachine, p *paginatedHeightRequest) (interface{}, lib.ErrorI) {
return s.GetValidatorsPaginated(p.PageParams, p.ValidatorFilters)
})
}
// Committee returns a page of committee members ordered from the highest stake to lowest
func (s *Server) Committee(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightPaginated(w, r, func(s *fsm.StateMachine, p *paginatedHeightRequest) (interface{}, lib.ErrorI) {
return s.GetCommitteePaginated(p.PageParams, p.ValidatorFilters.Committee)
})
}
// ValidatorSet retrieves the ValidatorSet that is responsible for the chainId
func (s *Server) ValidatorSet(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndIdParams(w, r, func(s *fsm.StateMachine, id uint64) (interface{}, lib.ErrorI) {
members, err := s.GetCommitteeMembers(id)
if err != nil {
return nil, err
}
return members.ValidatorSet, nil
})
}
// Checkpoint retrieves the checkpoint block hash for a certain committee and height combination
func (s *Server) Checkpoint(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndIdIndexer(w, r, func(s lib.StoreI, height, id uint64) (interface{}, lib.ErrorI) {
return s.GetCheckpoint(id, height)
})
}
// RootChainInfo retrieves the root chain info for the specified chain
func (s *Server) RootChainInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
req := new(heightAndIdRequest)
// unmarshal request parameters
if ok := unmarshal(w, r, req); !ok {
return
}
// if height is 0; set to the latest height
if req.Height == 0 {
req.Height = s.controller.FSM.Height()
}
// load the root chain info directly
got, err := s.controller.FSM.LoadRootChainInfo(req.ID, req.Height)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, got, http.StatusOK)
}
// CommitteeData retrieves the committee data for the specified chain id
func (s *Server) CommitteeData(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndIdParams(w, r, func(s *fsm.StateMachine, id uint64) (interface{}, lib.ErrorI) {
return s.GetCommitteeData(id)
})
}
// CommitteesData retrieves all committee data
func (s *Server) CommitteesData(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightPaginated(w, r, func(s *fsm.StateMachine, p *paginatedHeightRequest) (interface{}, lib.ErrorI) {
return s.GetCommitteesData() // consider pagination
})
}
// SubsidizedCommittees returns a list of chainIds that receive a portion of the 'block reward'
func (s *Server) SubsidizedCommittees(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) { return s.GetSubsidizedCommittees() })
}
// RetiredCommittees returns a list of the retired chainIds
func (s *Server) RetiredCommittees(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) { return s.GetRetiredCommittees() })
}
// NonSigners returns all non-quorum-certificate-signers
func (s *Server) NonSigners(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) {
return s.GetNonSigners()
})
}
// Params returns the aggregated ParamSpaces in a single Params object
func (s *Server) Params(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) { return s.GetParams() })
}
// FeeParams returns the current state of the governance params in the Fee space
func (s *Server) FeeParams(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (any, lib.ErrorI) { return s.GetParamsFee() })
}
// ValParams returns the current state of the governance params in the Validator space
func (s *Server) ValParams(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (any, lib.ErrorI) { return s.GetParamsVal() })
}
// ConParams returns the current state of the governance params in the Consensus space
func (s *Server) ConParams(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (any, lib.ErrorI) { return s.GetParamsCons() })
}
// GovParams returns the current state of the governance params in the Governance space
func (s *Server) GovParams(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (any, lib.ErrorI) { return s.GetParamsGov() })
}
// EcoParameters economic governance parameters
func (s *Server) EcoParameters(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// unmarshal the requested chain id
post := new(chainRequest)
if ok := unmarshal(w, r, post); !ok {
return
}
// create a read-only state for the latest block and determine economic parameters
s.readOnlyState(0, func(state *fsm.StateMachine) lib.ErrorI {
// get the root id
rootChainId, err := state.GetRootChainId()
if err != nil {
return err
}
// get the lottery winner
s.rcManager.l.Lock()
delegate, err := s.rcManager.GetLotteryWinner(rootChainId, 0, s.config.ChainId)
s.rcManager.l.Unlock()
// if an error occurred
if err != nil {
return err
}
// ensure non-nil delegate
if delegate == nil {
return lib.ErrEmptyLotteryWinner()
}
// find proposer cut
proposerCut := 100 - delegate.Cut
// remove sub-validator and sub-delegate cuts if requested chain id is non-root id
if post.ChainId != rootChainId {
proposerCut -= delegate.Cut // sub-validator
proposerCut -= delegate.Cut // sub-delegate
}
_, daoCut, totalMint, committeeMint, err := state.GetBlockMintStats(post.ChainId)
if err != nil {
write(w, err.Error(), http.StatusBadRequest)
return nil
}
write(w, economicParameterResponse{
DAOCut: daoCut,
MintPerBlock: totalMint,
MintPerCommittee: committeeMint,
ProposerCut: proposerCut,
DelegateCut: delegate.Cut,
}, http.StatusOK)
return nil
})
}
// Order gets an order for the specified committee
func (s *Server) Order(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.orderParams(w, r, func(s *fsm.StateMachine, p *orderRequest) (any, lib.ErrorI) {
orderId, err := lib.StringToBytes(p.OrderId)
if err != nil {
return nil, err
}
return s.GetOrder(orderId, p.Committee)
})
}
// Orders retrieves the order book for a committee with optional filters and pagination
func (s *Server) Orders(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.ordersParams(w, r, func(s *fsm.StateMachine, req *ordersRequest) (any, lib.ErrorI) {
// validate mutual exclusion: cannot filter by both seller and buyer address
if req.SellersSendAddress != "" && req.BuyerSendAddress != "" {
return nil, lib.NewError(lib.CodeInvalidArgument, lib.RPCModule, "cannot filter by both sellersSendAddress and buyerSendAddress")
}
// convert seller address if provided
var sellerAddr []byte
if req.SellersSendAddress != "" {
var err lib.ErrorI
sellerAddr, err = lib.StringToBytes(req.SellersSendAddress)
if err != nil {
return nil, err
}
}
// convert buyer address if provided
var buyerAddr []byte
if req.BuyerSendAddress != "" {
var err lib.ErrorI
buyerAddr, err = lib.StringToBytes(req.BuyerSendAddress)
if err != nil {
return nil, err
}
}
// use paginated query
return s.GetOrdersPaginated(sellerAddr, buyerAddr, req.Committee, req.PageParams)
})
}
// DexPrice retrieves the latest dex price for a committee
func (s *Server) DexPrice(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndIdParams(w, r, func(s *fsm.StateMachine, id uint64) (any, lib.ErrorI) {
if id == 0 {
return s.GetDexPrices()
}
// return the dex price
return s.GetDexPrice(id)
})
}
// DexBatch retrieves the 'locked' dex batch for a committee
func (s *Server) DexBatch(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIdAndPointsParams(w, r, func(s *fsm.StateMachine, id uint64, points bool) (any, lib.ErrorI) {
if id == 0 {
return s.GetDexBatches(true)
}
// return the locked batch
return s.GetDexBatch(id, true, points) // points augmentation used for liveness safety mirrors
})
}
// NextDexBatch retrieves the 'up-next' dex batch for a committee
func (s *Server) NextDexBatch(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIdAndPointsParams(w, r, func(s *fsm.StateMachine, id uint64, points bool) (any, lib.ErrorI) {
if id == 0 {
return s.GetDexBatches(false)
}
// return the locked batch
return s.GetDexBatch(id, false, points)
})
}
// LastProposers returns the last Proposer addresses
func (s *Server) LastProposers(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) {
return s.GetLastProposers()
})
}
// MinimumEvidenceHeight returns the minimum height the evidence must be to still be usable
func (s *Server) MinimumEvidenceHeight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) {
return s.LoadMinimumEvidenceHeight()
})
}
// Lottery selects a validator/delegate randomly weighted based on their stake within a committee
func (s *Server) Lottery(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndIdParams(w, r, func(s *fsm.StateMachine, id uint64) (interface{}, lib.ErrorI) {
return s.LotteryWinner(id)
})
}
// IsValidDoubleSigner returns if the DoubleSigner is already set for a height
func (s *Server) IsValidDoubleSigner(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightAndAddrIndexer(w, r, func(s lib.StoreI, h uint64, a lib.HexBytes) (interface{}, lib.ErrorI) {
// ensure the last quorum certificate doesn't expose any valid double signers that aren't yet indexed
qc, err := s.GetQCByHeight(s.Version() - 1)
if err != nil {
return nil, err
}
if qc.Results != nil && qc.Results.SlashRecipients != nil {
for _, ds := range qc.Results.SlashRecipients.DoubleSigners {
// get the public key from the address
pk, e := crypto.NewPublicKeyFromBytes(ds.Id)
if e != nil {
continue
}
// if contains height, return not valid signer
if bytes.Equal(pk.Address().Bytes(), a) && slices.Contains(ds.Heights, h) {
return false, nil
}
}
}
// check the indexer
return s.IsValidDoubleSigner(a, h)
})
}
// DoubleSigners returns all double signers in the indexer
func (s *Server) DoubleSigners(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIndexer(w, r, func(s lib.StoreI, _ uint64, _ lib.PageParams) (interface{}, lib.ErrorI) {
return s.GetDoubleSigners()
})
}
// BlockByHeight responds with the block data found at a specific height
func (s *Server) BlockByHeight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIndexer(w, r, func(s lib.StoreI, h uint64, _ lib.PageParams) (any, lib.ErrorI) { return s.GetBlockByHeight(h) })
}
// CertByHeight response with the quorum certificate at height h
func (s *Server) CertByHeight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIndexer(w, r, func(s lib.StoreI, h uint64, _ lib.PageParams) (any, lib.ErrorI) { return s.GetQCByHeight(h) })
}
// BlockByHash responds with block with hash h
func (s *Server) BlockByHash(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.hashIndexer(w, r, func(s lib.StoreI, h lib.HexBytes) (any, lib.ErrorI) { return s.GetBlockByHash(h) })
}
// Blocks responds with a page of blocks based on the page parameters
func (s *Server) Blocks(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIndexer(w, r, func(s lib.StoreI, _ uint64, p lib.PageParams) (any, lib.ErrorI) { return s.GetBlocks(p) })
}
// TransactionByHash responds with a transaction with the hash h
func (s *Server) TransactionByHash(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.hashIndexer(w, r, func(s lib.StoreI, h lib.HexBytes) (any, lib.ErrorI) { return s.GetTxByHash(h) })
}
// TransactionsByHeight response with the transactions at block height h
func (s *Server) TransactionsByHeight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIndexer(w, r, func(s lib.StoreI, h uint64, p lib.PageParams) (any, lib.ErrorI) { return s.GetTxsByHeight(h, true, p) })
}
// EventsByHeight response with the events at block height h
func (s *Server) EventsByHeight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightIndexer(w, r, func(s lib.StoreI, h uint64, p lib.PageParams) (any, lib.ErrorI) {
return s.GetEventsByBlockHeight(h, true, p)
})
}
// EventsByAddress response with the events of address a
func (s *Server) EventsByAddress(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.addrIndexer(w, r, func(s lib.StoreI, a crypto.AddressI, p lib.PageParams) (any, lib.ErrorI) {
return s.GetEventsByAddress(a, true, p)
})
}
// EventsByChain response with the events for a certain chain id
func (s *Server) EventsByChain(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.idIndexer(w, r, func(s lib.StoreI, id uint64, p lib.PageParams) (any, lib.ErrorI) {
return s.GetEventsByChainId(id, true, p)
})
}
// Pending responds with a page of unconfirmed mempool transactions
func (s *Server) Pending(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.pageIndexer(w, r, func(_ lib.StoreI, _ crypto.AddressI, p lib.PageParams) (any, lib.ErrorI) {
return s.controller.GetPendingPage(p)
})
}
// Supply returns the Supply structure
func (s *Server) Supply(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.heightParams(w, r, func(s *fsm.StateMachine) (interface{}, lib.ErrorI) {
return s.GetSupply()
})
}
// State exports the blockchain state at the requested height
func (s *Server) State(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
request := new(heightsRequest)
if err := r.ParseForm(); err != nil {
write(w, err, http.StatusBadRequest)
return
}
request.Height = parseUint64FromString(r.Form.Get("height"))
s.readOnlyState(request.Height, func(state *fsm.StateMachine) (err lib.ErrorI) {
exported, err := state.ExportState()
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, exported, http.StatusOK)
return
})
}
// StateDiff returns the different between the state at two block heights
func (s *Server) StateDiff(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
sm1, sm2, opts, ok := s.getDoubleStateMachineFromHeightParams(w, r, p)
if !ok {
return
}
defer sm1.Discard()
defer sm2.Discard()
state1, e := sm1.ExportState()
if e != nil {
write(w, e.Error(), http.StatusInternalServerError)
return
}
state2, e := sm2.ExportState()
if e != nil {
write(w, e.Error(), http.StatusInternalServerError)
return
}
j1, _ := json.Marshal(state1)
j2, _ := json.Marshal(state2)
_, differ := jsondiff.Compare(j1, j2, opts)
if r.Method == http.MethodGet {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
differ = "<pre>" + differ + "</pre>"
}
if _, err := w.Write([]byte(differ)); err != nil {
s.logger.Error(err.Error())
}
}
// TransactionsBySender returns transactions for the specified sender address
func (s *Server) TransactionsBySender(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.addrIndexer(w, r, func(s lib.StoreI, a crypto.AddressI, p lib.PageParams) (any, lib.ErrorI) {
return s.GetTxsBySender(a, true, p)
})
}
// TransactionsByRecipient returns transactions for the specified recipient address
func (s *Server) TransactionsByRecipient(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.addrIndexer(w, r, func(s lib.StoreI, a crypto.AddressI, p lib.PageParams) (any, lib.ErrorI) {
return s.GetTxsByRecipient(a, true, p)
})
}
// FailedTxs returns a list of failed mempool transactions for the specified address
func (s *Server) FailedTxs(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Invoke helper with the HTTP request, response writer and an inline callback
s.addrIndexer(w, r, func(_ lib.StoreI, address crypto.AddressI, p lib.PageParams) (any, lib.ErrorI) {
return s.controller.GetFailedTxsPage(address.String(), p)
})
}
// Proposals returns the proposals present
func (s *Server) Proposals(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
bz, err := os.ReadFile(filepath.Join(s.config.DataDirPath, lib.ProposalsFilePath))
if err != nil {
write(w, err, http.StatusInternalServerError)
return
}
w.Header().Set(ContentType, ApplicationJSON)
if _, err = w.Write(bz); err != nil {
s.logger.Error(err.Error())
}
}
// Poll returns poll results
func (s *Server) Poll(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
s.pollMux.Lock()
bz, e := lib.MarshalJSONIndent(s.poll)
s.pollMux.Unlock()
if e != nil {
write(w, e, http.StatusInternalServerError)
return
}
if _, err := w.Write(bz); err != nil {
s.logger.Error(err.Error())
}
}
// IndexerBlobs returns the current and previous indexer blobs as protobuf bytes
func (s *Server) IndexerBlobs(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
req := new(indexerBlobsRequest)
if ok := unmarshal(w, r, req); !ok {
return
}
_, bz, err := s.IndexerBlobsCached(req.Height, req.Delta)
if err != nil {
status := http.StatusBadRequest
if err.Code() == lib.CodeMarshal {
status = http.StatusInternalServerError
}
write(w, err, status)
return
}
w.Header().Set("Content-Type", "application/x-protobuf")
w.Header().Set(ContentType, "application/x-protobuf")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(bz); err != nil {
s.logger.Error(err.Error())
}
}
// IndexerBlobsCached() is a helper function for the indexer blobs implementation
func (s *Server) IndexerBlobsCached(height uint64, delta bool) (*fsm.IndexerBlobs, []byte, lib.ErrorI) {
currentHeight := s.controller.FSM.Height()
if height == 0 || height > currentHeight {
height = currentHeight
}
if entry, ok := s.indexerBlobCache.get(height); ok && entry != nil && entry.blobs != nil && entry.protoBytes != nil {
if !delta {
return entry.blobs, entry.protoBytes, nil
}
blobDelta, err := fsm.DeltaIndexerBlobs(entry.blobs)
if err != nil {
return nil, nil, err
}
deltaBytes, err := lib.Marshal(blobDelta)
if err != nil {
return nil, nil, err
}
return blobDelta, deltaBytes, nil
}
current, err := s.controller.FSM.IndexerBlob(height)
if err != nil {
return nil, nil, err
}
var previous *fsm.IndexerBlob
// IndexerBlob(height) is only valid for height >= 2 (it pairs state@height with block height-1).
// Therefore "previous" exists only when (height-1) >= 2, i.e. height >= 3.
if height > 2 {
if cachedPrev, ok := s.indexerBlobCache.getCurrent(height - 1); ok {
previous = cachedPrev
} else {
prev, prevErr := s.controller.FSM.IndexerBlob(height - 1)
if prevErr != nil {
return nil, nil, prevErr
}
previous = prev
}
}
blobs := &fsm.IndexerBlobs{
Current: current,
Previous: previous,
}
protoBytes, err := lib.Marshal(blobs)
if err != nil {
return nil, nil, err
}
s.indexerBlobCache.put(height, &indexerBlobCacheEntry{
height: height,
blobs: blobs,
protoBytes: protoBytes,
})
if !delta {
return blobs, protoBytes, nil
}
blobDelta, err := fsm.DeltaIndexerBlobs(blobs)
if err != nil {
return nil, nil, err
}
deltaBytes, err := lib.Marshal(blobDelta)
if err != nil {
return nil, nil, err
}
return blobDelta, deltaBytes, nil
}
// orderParams is a helper function to abstract common workflows around a callback requiring a state machine and order request
func (s *Server) orderParams(w http.ResponseWriter, r *http.Request, callback func(s *fsm.StateMachine, request *orderRequest) (any, lib.ErrorI)) {
// initialize a new orderRequest object
req := new(orderRequest)
// execute the callback with the state machine and request
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
p, err := callback(state, req)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// ordersParams is a helper function to abstract common workflows around a callback requiring a state machine and orders request
func (s *Server) ordersParams(w http.ResponseWriter, r *http.Request, callback func(s *fsm.StateMachine, request *ordersRequest) (any, lib.ErrorI)) {
// initialize a new ordersRequest object
req := new(ordersRequest)
// execute the callback with the state machine and request
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
p, err := callback(state, req)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// heightParams is a helper function to abstract common workflows around a callback requiring a state machine
func (s *Server) heightParams(w http.ResponseWriter, r *http.Request, callback func(s *fsm.StateMachine) (any, lib.ErrorI)) {
req := new(heightRequest)
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
p, err := callback(state)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// heightPaginated is a helper function to abstract common workflows around a callback requiring a state machine and page parameters
func (s *Server) heightPaginated(w http.ResponseWriter, r *http.Request, callback func(s *fsm.StateMachine, p *paginatedHeightRequest) (any, lib.ErrorI)) {
req := new(paginatedHeightRequest)
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
p, err := callback(state, req)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// heightAndAddressParams is a helper function to execute a callback with a state machine and address as parameters
func (s *Server) heightAndAddressParams(w http.ResponseWriter, r *http.Request, callback func(*fsm.StateMachine, lib.HexBytes) (any, lib.ErrorI)) {
req := new(heightAndAddressRequest)
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
if req.Address == nil {
write(w, fsm.ErrAddressEmpty(), http.StatusBadRequest)
return
}
p, err := callback(state, req.Address)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// heightAndIdParams is a helper function to execute a callback with a state machine and ID as parameters
func (s *Server) heightAndIdParams(w http.ResponseWriter, r *http.Request, callback func(*fsm.StateMachine, uint64) (any, lib.ErrorI)) {
req := new(heightAndIdRequest)
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
p, err := callback(state, req.ID)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// heightIdAndPointsParams is a helper function to execute a callback with a state machine, ID and points as parameters
func (s *Server) heightIdAndPointsParams(w http.ResponseWriter, r *http.Request, callback func(*fsm.StateMachine, uint64, bool) (any, lib.ErrorI)) {
req := new(heightIdAndPointsRequest)
s.readOnlyStateFromHeightParams(w, r, req, func(state *fsm.StateMachine) (err lib.ErrorI) {
p, err := callback(state, req.ID, req.Points)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
return
})
}
// getDoubleStateMachineFromHeightParams is a helper function to get two read-only state machines at the specified heights
func (s *Server) getDoubleStateMachineFromHeightParams(w http.ResponseWriter, r *http.Request, p httprouter.Params) (sm1, sm2 *fsm.StateMachine, o *jsondiff.Options, ok bool) {
request, opts := new(heightsRequest), jsondiff.Options{}
switch r.Method {
case http.MethodGet:
opts = jsondiff.DefaultHTMLOptions()
opts.ChangedSeparator = " <- "
if err := r.ParseForm(); err != nil {
ok = false
write(w, err, http.StatusBadRequest)
return
}
request.Height = parseUint64FromString(r.Form.Get("height"))
request.StartHeight = parseUint64FromString(r.Form.Get("startHeight"))
case http.MethodPost:
opts = jsondiff.DefaultConsoleOptions()
if ok = unmarshal(w, r, request); !ok {
return
}
}
sm1, ok = s.getStateMachineWithHeight(request.Height, w)
if !ok {
return
}
if request.StartHeight == 0 {
request.StartHeight = sm1.Height() - 1
}
sm2, ok = s.getStateMachineWithHeight(request.StartHeight, w)
o = &opts
return
}
// heightIndexer is a helper function to abstract common workflows around a callback requiring height and page params
func (s *Server) heightIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, h uint64, p lib.PageParams) (any, lib.ErrorI)) {
// Initialize a new paginatedHeightRequest
req := new(paginatedHeightRequest)
// Attempt to unmarshal the request into the req object
if ok := unmarshal(w, r, req); !ok {
return
}
// Set up the store for the request context
st, ok := s.setupStore(w)
if !ok {
return
}
// Ensure that the store is discarded safely after processing
defer st.Discard()
if req.Height == 0 {
req.Height = st.Version() - 1
}
// Execute callback with store, height, and pagination parameters
p, err := callback(st, req.Height, req.PageParams)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
// Write the successful result to the response
write(w, p, http.StatusOK)
}
// idIndexer is a helper function to abstract common workflows around a callback requiring id and page params
func (s *Server) idIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, id uint64, p lib.PageParams) (any, lib.ErrorI)) {
// Initialize a new paginatedHeightRequest
req := new(paginatedIdRequest)
// Attempt to unmarshal the request into the req object
if ok := unmarshal(w, r, req); !ok {
return
}
// Set up the store for the request context
st, ok := s.setupStore(w)
if !ok {
return
}
// Ensure that the store is discarded safely after processing
defer st.Discard()
// Execute callback with store, height, and pagination parameters
p, err := callback(st, req.ID, req.PageParams)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
// Write the successful result to the response
write(w, p, http.StatusOK)
}
// heightAndAddrIndexer is a helper function to abstract common workflows around a callback requiring height and address parameters
func (s *Server) heightAndAddrIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, h uint64, address lib.HexBytes) (any, lib.ErrorI)) {
req := new(heightAndAddressRequest)
if ok := unmarshal(w, r, req); !ok {
return
}
st, ok := s.setupStore(w)
if !ok {
return
}
defer st.Discard()
if req.Height == 0 {
req.Height = st.Version() - 1
}
p, err := callback(st, req.Height, req.Address)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
}
// heightAndIdIndexer is a helper function to abstract common workflows around a callback requiring height and ID parameters
func (s *Server) heightAndIdIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, h, id uint64) (any, lib.ErrorI)) {
req := new(heightAndIdRequest)
if ok := unmarshal(w, r, req); !ok {
return
}
st, ok := s.setupStore(w)
if !ok {
return
}
defer st.Discard()
if req.Height == 0 {
req.Height = st.Version() - 1
}
p, err := callback(st, req.Height, req.ID)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
}
// hashIndexer is a helper function to abstract common workflows around a callback requiring a hash parameter
func (s *Server) hashIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, h lib.HexBytes) (any, lib.ErrorI)) {
req := new(hashRequest)
if ok := unmarshal(w, r, req); !ok {
return
}
st, ok := s.setupStore(w)
if !ok {
return
}
defer st.Discard()
bz, err := lib.StringToBytes(req.Hash)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
p, err := callback(st, bz)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
}
// addrIndexer is a helper function to abstract common workflows around a callback requiring an address and page parameterse
func (s *Server) addrIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, a crypto.AddressI, p lib.PageParams) (any, lib.ErrorI)) {
req := new(paginatedAddressRequest)
if ok := unmarshal(w, r, req); !ok {
return
}
st, ok := s.setupStore(w)
if !ok {
return
}
defer st.Discard()
if req.Address == nil {
write(w, fsm.ErrAddressEmpty(), http.StatusBadRequest)
return
}
p, err := callback(st, crypto.NewAddressFromBytes(req.Address), req.PageParams)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
}
// pageIndexer is a helper function to abstract common workflows around a callback requiring an address and page parameterse
// TODO very similar to above
func (s *Server) pageIndexer(w http.ResponseWriter, r *http.Request, callback func(s lib.StoreI, a crypto.AddressI, p lib.PageParams) (any, lib.ErrorI)) {
req := new(paginatedAddressRequest)
if ok := unmarshal(w, r, req); !ok {
return
}
st, ok := s.setupStore(w)
if !ok {
return
}
defer st.Discard()
p, err := callback(st, crypto.NewAddressFromBytes(req.Address), req.PageParams)
if err != nil {
write(w, err, http.StatusBadRequest)
return
}
write(w, p, http.StatusOK)
}
// setupStore creates a new store from the state machine's database. This store must be closed safely with Discard()
func (s *Server) setupStore(w http.ResponseWriter) (lib.StoreI, bool) {
db := s.controller.FSM.Store().(lib.StoreI).DB()
st, err := store.NewStoreWithDB(s.config, db, nil, s.logger)
if err != nil {
write(w, lib.ErrNewStore(err), http.StatusInternalServerError)
return nil, false
}
return st, true
}
// withStore() executes a read only store function
func (s *Server) withStore(fn func(st *store.Store) (any, error)) (any, error) {