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
// Copyright 2020 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! HTTP API for the supervisor service. Supervisor API is divided into public and private
//! parts, with public part intended for unauthorized use, and private parts intended to be
//! used by network administrator for the Exonum blockchain configuration.
//!
//! # Table of Contents
//!
//! - Public API:
//!
//!     - [Obtain consensus configuration](#obtain-consensus-configuration)
//!     - [Obtain pending configuration proposal](#obtain-pending-configuration-proposal)
//!     - [Obtain deployed artifacts and services](#obtain-deployed-artifacts-and-services)
//!
//! - Private API:
//!
//!     - [Request to deploy an artifact](#request-to-deploy-an-artifact)
//!     - [Request service migration](#request-service-migration)
//!     - [Request to accept new configuration](#request-to-accept-new-configuration)
//!     - [Vote for configuration proposal](#vote-for-configuration-proposal)
//!     - [Obtain current configuration number](#obtain-current-configuration-number)
//!     - [Obtain supervisor configuration](#obtain-supervisor-configuration)
//!     - [Check deployment status](#check-deployment-status)
//!     - [Check migration status](#check-migration-status)
//!
//! # Public API
//!
//! ## Obtain Consensus Configuration
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/consensus-config` |
//! | Method      | GET   |
//! | Query type  | - |
//! | Return type | [`ConsensusConfig`] |
//!
//! Returns the current consensus configuration.
//!
//! [`ConsensusConfig`]: https://docs.rs/exonum/latest/exonum/blockchain/config/struct.ConsensusConfig.html
//!
//! ```
//! use exonum::blockchain::ConsensusConfig;
//! use exonum_rust_runtime::ServiceFactory;
//! use exonum_supervisor::Supervisor;
//! use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = TestKitBuilder::validator()
//!     .with(Supervisor::simple())
//!     .build();
//!
//! let consensus_config: ConsensusConfig = testkit
//!     .api()
//!     .public(ApiKind::Service("supervisor"))
//!     .get("consensus-config")
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Obtain Pending Configuration Proposal
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/config-proposal` |
//! | Method      | GET   |
//! | Query type  | - |
//! | Return type | `Option<[ConfigProposalWithHash]>` |
//!
//! Returns the configuration proposal which is currently pending. Returns `None` if there is no
//! pending configuration at the moment.
//!
//! [ConfigProposalWithHash]: ../struct.ConfigProposalWithHash.html
//!
//! ```
//! # use exonum_rust_runtime::ServiceFactory;
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//! use exonum_supervisor::{ConfigProposalWithHash, Supervisor};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//!
//! let pending_proposal: Option<ConfigProposalWithHash> = testkit
//!     .api()
//!     .public(ApiKind::Service("supervisor"))
//!     .get("config-proposal")
//!     .await?;
//!
//! // Will be none, since we did not send a proposal.
//! assert!(pending_proposal.is_none());
//! # Ok(())
//! # }
//! ```
//!
//! ## Obtain Deployed Artifacts And Services
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/services` |
//! | Method      | GET   |
//! | Query type  | - |
//! | Return type | [`DispatcherInfo`] |
//!
//! Returns information about services available in the network.
//!
//! [`DispatcherInfo`]: struct.DispatcherInfo.html
//!
//! ```
//! # use exonum_rust_runtime::ServiceFactory;
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//! use exonum_supervisor::{api::DispatcherInfo, Supervisor};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//!
//! let services_info: DispatcherInfo = testkit
//!     .api()
//!     .public(ApiKind::Service("supervisor"))
//!     .get("services")
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Private API
//!
//! ## Request to Deploy an Artifact
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/deploy-artifact` |
//! | Method      | POST   |
//! | Body type   | [`DeployRequest`] |
//! | Return type | [`Hash`] |
//!
//! Requests the deployment of a certain artifact.
//!
//! Depending on the supervisor operating mode, it may be required to send such a request to
//! majority of nodes (in "decentralized" mode), or one request will be enough (in "simple" mode).
//!
//! After receiving a deployment request, supervisor creates a corresponding transaction, signs it
//! with node's keys and broadcasts the transaction within the network. The hash of broadcast
//! transaction is returned from the endpoint.
//!
//! For more details on deploy requests, see [crate documentation](../index.html).
//!
//! **Warning:** `DeployRequest` structure should be serialized using corresponding protobuf message,
//! and represented as a hexadecimal string.
//!
//! [`DeployRequest`]: ../struct.DeployRequest.html
//! [`Hash`]: https://docs.rs/exonum-crypto/latest/exonum_crypto/struct.Hash.html
//!
//! ```
//! use exonum::{crypto::Hash, helpers::Height, merkledb::BinaryValue};
//! use exonum_supervisor::{DeployRequest, Supervisor};
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//! # use exonum_rust_runtime::ServiceFactory;
//!
//! # use exonum_derive::*;
//! # use exonum_rust_runtime::Service;
//! #
//! # #[derive(Debug, ServiceFactory, ServiceDispatcher)]
//! # #[service_factory(artifact_name = "exonum.doc.SomeService", artifact_version = "0.1.0")]
//! # pub struct SomeService;
//! #
//! # impl Service for SomeService {}
//! #
//! # fn config_for_artifact() -> Vec<u8> { Vec::new() }
//! #
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//!
//! // In this example, we will try to deploy `SomeService` artifact.
//! let deploy_request = DeployRequest::new(SomeService.artifact_id(), Height(10))
//!     .with_spec(config_for_artifact());
//!
//! // `deploy_request` will be automatically serialized to hexadecimal string.
//! let tx_hash: Hash = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .query(&deploy_request)
//!     .post("deploy-artifact")
//!     .await?;
//!
//! let block = testkit.create_block();
//! let result = block[tx_hash].status();
//! # // Call `expect` for a better error reporting if test will fail.
//! # result.expect("Deploy request failed");
//! assert!(result.is_ok());
//! # Ok(())
//! # }
//! ```
//!
//! ## Request Service Migration
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/migrate` |
//! | Method      | POST   |
//! | Body type   | [`MigrationRequest`] |
//! | Return type | [`Hash`] |
//!
//! Requests the migration of certain service to a newer artifact version.
//!
//! Depending on the supervisor operating mode, it may be required to send such a request to
//! majority of nodes (in "decentralized" mode), or one request will be enough (in "simple" mode).
//!
//! After receiving a migration request, supervisor creates a corresponding transaction, signs it
//! with node's keys and broadcasts the transaction within the network. The hash of broadcast
//! transaction is returned from the endpoint.
//!
//! For more details on migration requests, see [crate documentation](../index.html).
//!
//! **Warning:** `MigrationRequest` structure should be serialized using corresponding protobuf message,
//! and represented as a hexadecimal string.
//!
//! [`MigrationRequest`]: ../struct.MigrationRequest.html
//! [`Hash`]: https://docs.rs/exonum-crypto/latest/exonum_crypto/struct.Hash.html
//!
//! ```
//! use exonum::crypto::Hash;
//! use exonum_supervisor::{MigrationRequest, Supervisor};
//! # use exonum::helpers::Height;
//! # use exonum_rust_runtime::ServiceFactory;
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = TestKitBuilder::validator()
//!     .with(Supervisor::simple())
//!     // Add some service that supports migrations...
//!     .build();
//!
//! // Migration request creation skipped...
//! let migration_request = // Migration of some service.
//! #     // Request migration of supervisor for simplicity.
//! #     MigrationRequest::new(
//! #         Supervisor.artifact_id(),
//! #         Supervisor::NAME,
//! #         Height(10),
//! #     );
//!
//! // `migration_request` will be automatically serialized to hexadecimal string.
//! let tx_hash: Hash = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .query(&migration_request)
//!     .post("migrate")
//!     .await?;
//!
//! let block = testkit.create_block();
//! let result = block[tx_hash].status();
//! assert!(result.is_ok());
//! # Ok(())
//! # }
//! ```
//!
//! ## Request to Accept New Configuration
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/propose-config` |
//! | Method      | POST   |
//! | Body type   | [`ConfigPropose`] |
//! | Return type | [`Hash`] |
//!
//! Proposes the new configuration for the Exonum blockchain.
//!
//! Configuration changes may include the following items:
//!
//! - Request to change the consensus configuration.
//! - Request to start a new service instance.
//! - Request to stop an existing service instance.
//! - Request to resume a previously stopped service.
//! - Request to change the configuration of an existing service instance.
//!
//! Configuration proposal does not cause the configuration change itself, instead it
//! initializes a voting process: if node administrators of the network agree on the
//! suggested proposal, the configuration is applies. Otherwise, no changes in the
//! network configuration are performed.
//!
//! Voting for a configuration is performed via [`confirm-config`](#vote-for-configuration-proposal)
//! endpoint.
//!
//! For more details on configuration proposals, see [crate documentation](../index.html).
//!
//! **Warning:** `ConfigPropose` structure should be serialized using corresponding protobuf message,
//! and represented as a hexadecimal string.
//!
//! [`ConfigPropose`]: ../struct.ConfigPropose.html
//! [`Hash`]: https://docs.rs/exonum-crypto/latest/exonum_crypto/struct.Hash.html
//!
//! ```
//! use exonum::crypto::Hash;
//! use exonum_supervisor::{ConfigPropose, Supervisor};
//! # use exonum::helpers::Height;
//! # use exonum_rust_runtime::ServiceFactory;
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//!
//! let proposal: ConfigPropose = // Proposal creation skipped...
//! # ConfigPropose::new(0, Height(0));
//!
//! // `proposal` will be automatically serialized to hexadecimal string.
//! let tx_hash: Hash = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .query(&proposal)
//!     .post("propose-config")
//!     .await?;
//!
//! // Create a block, so the proposal transaction will appear in the blockchain.
//! let block = testkit.create_block();
//!
//! // Verify that transaction was executed successfully.
//! assert!(block[tx_hash].status().is_ok());
//! # Ok(())
//! # }
//! ```
//!
//! ## Vote for Configuration Proposal
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/confirm-config` |
//! | Method      | POST   |
//! | Query type  | [`DeployRequest`] |
//! | Return type | [`Hash`] |
//!
//! Votes for a pending configuration with a certain hash.
//!
//! Depending on the supervisor operating mode, it may be required to vote by majority of
//! nodes (in "decentralized" mode), or one vote will be enough (in "simple" mode).
//!
//! The node that broadcast the proposal is considered to have voted for it, there is
//! no need to send vote request for this node manually.
//!
//! After receiving a vote, supervisor creates a corresponding transaction, signs it
//! with node's keys and broadcasts the transaction within the network. The hash of broadcast
//! transaction is returned from the endpoint.
//!
//! For more details on voting, see [crate documentation](../index.html).
//!
//! **Warning:** `ConfigVote` structure should be serialized using corresponding protobuf message,
//! and represented as a hexadecimal string.
//!
//! [`ConfigVote`]: ../struct.ConfigVote.html
//! [`Hash`]: https://docs.rs/exonum-crypto/latest/exonum_crypto/struct.Hash.html
//!
//! ```
//! use exonum::crypto::Hash;
//! use exonum_supervisor::{ConfigPropose, ConfigVote, Supervisor, SupervisorInterface};
//! # use exonum::helpers::{Height, ValidatorId};
//! # use exonum::runtime::SUPERVISOR_INSTANCE_ID;
//! # use exonum_rust_runtime::ServiceFactory;
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example (but with several validators)...
//! #     TestKitBuilder::validator()
//! #         .with_validators(2) // 2 validators to create a config to vote for.
//! #         .with(Supervisor::simple())
//! #         .build();
//! let proposal: ConfigPropose = // Proposal creation skipped...
//! # ConfigPropose::new(0, Height(10));
//!
//! // Assuming that config proposal was broadcast by other validator...
//! # let keys = testkit.validator(ValidatorId(1)).service_keypair();
//! # let tx = keys.propose_config_change(SUPERVISOR_INSTANCE_ID, proposal.clone());
//! # testkit.create_block_with_transaction(tx).transactions[0]
//! #     .status()
//! #     .expect("Transaction with change propose discarded.");
//! // Create a vote.
//! let config_vote = ConfigVote::from(proposal);
//!
//! // Send it.
//! // In this example, query is serialized to hexadecimal string automatically.
//! let tx_hash: Hash = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .query(&config_vote)
//!     .post("confirm-config")
//!     .await?;
//!
//! // Create a block, so the proposal transaction will appear in the blockchain.
//! let block = testkit.create_block();
//! // Verify that transaction was executed successfully.
//! assert!(block[tx_hash].status().is_ok());
//! # Ok(())
//! # }
//! ```
//!
//! ## Obtain Current Configuration Number
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/configuration-number` |
//! | Method      | GET   |
//! | Query type  | - |
//! | Return type | `u64` |
//!
//! To avoid the situation when several conflicting configuration proposals are broadcast
//! within the network, `ConfigPropose` contains a `configuration_number` field, which
//! should be equal to the amount of configurations, processed by supervisor (only configurations
//! that did participate in voting are counted, incorrect configurations are not).
//!
//! This field acts like a [nonce], approving the fact that node broadcasting proposal is
//! aware of the last accepted configuration.
//!
//! `configuration-number` endpoint allows requester to obtain the current number of processed
//! configurations.
//!
//! [nonce]: https://en.wikipedia.org/wiki/Cryptographic_nonce
//!
//! ```
//! use exonum_rust_runtime::ServiceFactory;
//! use exonum_supervisor::Supervisor;
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//!
//! let configuration_number: u64 = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .get("configuration-number")
//!     .await?;
//!
//! // There was no configuration proposals, so configuration number is 0.
//! assert_eq!(configuration_number, 0);
//! # Ok(())
//! # }
//! ```
//!
//! ## Obtain Supervisor Configuration
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/supervisor-config` |
//! | Method      | GET   |
//! | Query type  | - |
//! | Return type | [`SupervisorConfig`] |
//!
//! Returns the current supervisor configuration, which includes the supervisor operating mode.
//!
//! [`SupervisorConfig`]: ../struct.SupervisorConfig.html
//!
//! ```
//! use exonum_rust_runtime::ServiceFactory;
//! use exonum_supervisor::{mode::Mode, Supervisor, SupervisorConfig};
//! use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//!
//! let config: SupervisorConfig = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .get("supervisor-config")
//!     .await?;
//!
//! assert_eq!(config.mode, Mode::Simple);
//! # Ok(())
//! # }
//! ```
//!
//! ## Check Deployment Status
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/deploy-status` |
//! | Method      | GET   |
//! | Query type  | [`DeployInfoQuery`] |
//! | Return type | [`AsyncEventState`] |
//!
//! Returns the state of the deployment for a certain `ArtifactId`.
//!
//! [`DeployInfoQuery`]: struct.DeployInfoQuery.html
//! [`AsyncEventState`]: ../enum.AsyncEventState.html
//!
//! ```
//! # use exonum::{crypto::Hash, helpers::Height, merkledb::BinaryValue};
//! # use exonum_rust_runtime::{spec::{JustFactory, Spec}, ServiceFactory};
//! use exonum_supervisor::{
//!     api::DeployInfoQuery, DeployRequest, AsyncEventState, Supervisor,
//! };
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # use exonum_derive::*;
//! # use exonum_rust_runtime::Service;
//! #
//! # #[derive(Debug, ServiceFactory, ServiceDispatcher)]
//! # #[service_factory(artifact_name = "exonum.doc.SomeService", artifact_version = "0.1.0")]
//! # pub struct SomeService;
//! #
//! # impl Service for SomeService {}
//! #
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator()
//! #         .with(Supervisor::simple())
//! #         .with(JustFactory::new(SomeService))
//! #         .build();
//!
//! let deploy_request: DeployRequest = // Some previously performed deploy request.
//! #     DeployRequest::new(SomeService.artifact_id(), Height(10));
//! # // Request deploy, so we will be able to request its state.
//! # let _hash: Hash = testkit
//! #     .api()
//! #     .private(ApiKind::Service("supervisor"))
//! #     .query(&deploy_request)
//! #     .post("deploy-artifact")
//! #     .await?;
//! # testkit.create_block();
//! let query = DeployInfoQuery::from(deploy_request);
//!
//! let deploy_state: AsyncEventState = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .query(&query)
//!     .get("deploy-status")
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Check Migration Status
//!
//! | Property    | Value |
//! |-------------|-------|
//! | Path        | `/api/services/supervisor/migration-status` |
//! | Method      | GET   |
//! | Query type  | [`MigrationInfoQuery`] |
//! | Return type | [`MigrationState`] |
//!
//! Returns the state of the migration for a certain service instance.
//!
//! [`MigrationInfoQuery`]: struct.MigrationInfoQuery.html
//! [`MigrationState`]: ../struct.MigrationState.html
//!
//! ```
//! # use exonum::{crypto::Hash, helpers::Height, merkledb::BinaryValue};
//! # use exonum_rust_runtime::ServiceFactory;
//! use exonum_supervisor::{
//!     api::MigrationInfoQuery, MigrationRequest, MigrationState, Supervisor,
//! };
//! # use exonum_testkit::{ApiKind, TestKitBuilder};
//!
//! # #[tokio::main]
//! # async fn main() -> anyhow::Result<()> {
//! let mut testkit = // Same as in previous example...
//! #     TestKitBuilder::validator().with(Supervisor::simple()).build();
//! let migration_request: MigrationRequest = // Some previously performed migration request.
//! #     MigrationRequest::new(
//! #         Supervisor.artifact_id(),
//! #         Supervisor::NAME,
//! #         Height(10),
//! #     );
//! # // Request migration. It will fail, but we'll be able to request its state.
//! # let _hash: Hash = testkit
//! #     .api()
//! #     .private(ApiKind::Service("supervisor"))
//! #     .query(&migration_request)
//! #     .post("migrate")
//! #     .await?;
//! # testkit.create_block();
//! let query = MigrationInfoQuery::from(migration_request);
//!
//! let migration_state: MigrationState = testkit
//!     .api()
//!     .private(ApiKind::Service("supervisor"))
//!     .query(&query)
//!     .get("migration-status")
//!     .await?;
//! # Ok(())
//! # }
//! ```

use exonum::{
    blockchain::ConsensusConfig,
    crypto::Hash,
    helpers::Height,
    merkledb::AsReadonly,
    runtime::{ArtifactId, DispatcherSchema, InstanceState},
};
use exonum_rust_runtime::{
    api::{self, ServiceApiBuilder, ServiceApiState},
    Broadcaster,
};
use serde_derive::{Deserialize, Serialize};

use std::convert::TryFrom;

use super::{
    schema::SchemaImpl, transactions::SupervisorInterface, AsyncEventState, ConfigProposalWithHash,
    ConfigPropose, ConfigVote, DeployRequest, MigrationRequest, MigrationState, SupervisorConfig,
};
use exonum_proto::ProtobufBase64;

/// Query for retrieving information about deploy state.
/// This is flattened version of `DeployRequest` which can be
/// encoded via URL query parameters.
#[derive(Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize)]
pub struct DeployInfoQuery {
    /// Artifact identifier as string, e.g. `0:exonum-supervisor:1.0.0".
    pub artifact: String,
    /// Artifact spec bytes serialized as any of four base64 variations supported by Protobuf
    /// (standard or URL-safe, with or without padding).
    pub spec: String,
    /// Deadline height.
    pub deadline_height: u64,
    /// Seed to distinguish among deploys with the same params.
    #[serde(default)]
    pub seed: u64,
}

impl TryFrom<DeployInfoQuery> for DeployRequest {
    type Error = api::Error;

    fn try_from(query: DeployInfoQuery) -> Result<Self, Self::Error> {
        let artifact = query.artifact.parse::<ArtifactId>().map_err(|err| {
            api::Error::bad_request()
                .title("Invalid deploy request query")
                .detail(err.to_string())
        })?;
        let spec = ProtobufBase64::decode(&query.spec).map_err(|err| {
            api::Error::bad_request()
                .title("Invalid deploy request query")
                .detail(err.to_string())
        })?;
        let deadline_height = Height(query.deadline_height);

        let request = Self {
            artifact,
            spec,
            deadline_height,
            seed: query.seed,
        };

        Ok(request)
    }
}

impl From<DeployRequest> for DeployInfoQuery {
    fn from(request: DeployRequest) -> Self {
        let artifact = request.artifact.to_string();
        let spec = base64::encode_config(&request.spec, base64::URL_SAFE_NO_PAD);
        let deadline_height = request.deadline_height.0;

        Self {
            artifact,
            spec,
            deadline_height,
            seed: request.seed,
        }
    }
}

/// Query for retrieving information about migration state.
/// This is flattened version of `MigrationRequest` which can be
/// encoded via URL query parameters.
#[derive(Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize)]
pub struct MigrationInfoQuery {
    /// Artifact identifier as string, e.g. `0:exonum-supervisor:1.0.0"
    pub new_artifact: String,
    /// Target service name.
    pub service: String,
    /// Deadline height.
    pub deadline_height: u64,
    /// Seed to allow several migrations with the same params.
    #[serde(default)]
    pub seed: u64,
}

impl TryFrom<MigrationInfoQuery> for MigrationRequest {
    type Error = api::Error;

    fn try_from(query: MigrationInfoQuery) -> Result<Self, Self::Error> {
        let new_artifact = query.new_artifact.parse::<ArtifactId>().map_err(|err| {
            api::Error::bad_request()
                .title("Invalid migration request query")
                .detail(err.to_string())
        })?;
        let deadline_height = Height(query.deadline_height);

        let request = Self {
            new_artifact,
            service: query.service,
            deadline_height,
            seed: query.seed,
        };

        Ok(request)
    }
}

impl From<MigrationRequest> for MigrationInfoQuery {
    fn from(request: MigrationRequest) -> Self {
        let new_artifact = request.new_artifact.to_string();
        let deadline_height = request.deadline_height.0;

        Self {
            new_artifact,
            service: request.service,
            deadline_height,
            seed: request.seed,
        }
    }
}

/// Services info response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[non_exhaustive]
pub struct DispatcherInfo {
    /// List of deployed artifacts.
    pub artifacts: Vec<ArtifactId>,
    /// List of services.
    pub services: Vec<InstanceState>,
}

impl DispatcherInfo {
    /// Loads dispatcher information from database.
    fn load<T: AsReadonly>(schema: &DispatcherSchema<T>) -> Self {
        Self {
            artifacts: schema.service_artifacts().keys().collect(),
            services: schema.service_instances().values().collect(),
        }
    }
}

/// Public API specification of the supervisor service.
struct PublicApi;

impl PublicApi {
    /// Returns an actual consensus configuration of the blockchain.
    async fn consensus_config(
        state: ServiceApiState,
        _query: (),
    ) -> Result<ConsensusConfig, api::Error> {
        Ok(state.data().for_core().consensus_config())
    }

    /// Returns a pending propose config change.
    async fn config_proposal(
        state: ServiceApiState,
        _query: (),
    ) -> Result<Option<ConfigProposalWithHash>, api::Error> {
        Ok(SchemaImpl::new(state.service_data())
            .public
            .pending_proposal
            .get())
    }

    /// Returns a list of deployed artifacts and initialized services.
    async fn services(state: ServiceApiState, _query: ()) -> Result<DispatcherInfo, api::Error> {
        Ok(DispatcherInfo::load(&state.data().for_dispatcher()))
    }
}

/// Private API specification of the supervisor service.
struct PrivateApi;

impl PrivateApi {
    fn broadcaster(state: &ServiceApiState) -> Result<Broadcaster, api::Error> {
        state.broadcaster().ok_or_else(|| {
            api::Error::bad_request()
                .title("Invalid broadcast request")
                .detail("Nod is not a validator")
        })
    }

    /// Creates and broadcasts the `DeployArtifact` transaction, which is signed
    /// by the current node, and returns its hash.
    async fn deploy_artifact(
        state: ServiceApiState,
        request: DeployRequest,
    ) -> Result<Hash, api::Error> {
        Self::broadcaster(&state)?
            .request_artifact_deploy((), request)
            .await
            .map_err(|err| api::Error::internal(err).title("Artifact deploy request failed"))
    }

    /// Creates and broadcasts the `MigrationRequest` transaction, which is signed
    /// by the current node, and returns its hash.
    async fn migrate(
        state: ServiceApiState,
        request: MigrationRequest,
    ) -> Result<Hash, api::Error> {
        Self::broadcaster(&state)?
            .request_migration((), request)
            .await
            .map_err(|err| api::Error::internal(err).title("Migration start request failed"))
    }

    /// Creates and broadcasts the `ConfigPropose` transaction, which is signed
    /// by the current node, and returns its hash.
    async fn propose_config(
        state: ServiceApiState,
        proposal: ConfigPropose,
    ) -> Result<Hash, api::Error> {
        Self::broadcaster(&state)?
            .propose_config_change((), proposal)
            .await
            .map_err(|err| api::Error::internal(err).title("Config propose failed"))
    }

    /// Creates and broadcasts the `ConfigVote` transaction, which is signed
    /// by the current node, and returns its hash.
    async fn confirm_config(state: ServiceApiState, vote: ConfigVote) -> Result<Hash, api::Error> {
        Self::broadcaster(&state)?
            .confirm_config_change((), vote)
            .await
            .map_err(|err| api::Error::internal(err).title("Config vote failed"))
    }

    /// Returns the number of processed configurations.
    async fn configuration_number(state: ServiceApiState, _query: ()) -> Result<u64, api::Error> {
        let configuration_number = SchemaImpl::new(state.service_data()).get_configuration_number();
        Ok(configuration_number)
    }

    /// Returns an actual supervisor config.
    async fn supervisor_config(
        state: ServiceApiState,
        _query: (),
    ) -> Result<SupervisorConfig, api::Error> {
        let config = SchemaImpl::new(state.service_data()).supervisor_config();
        Ok(config)
    }

    /// Returns the state of deployment for the given deploy request.
    async fn deploy_status(
        state: ServiceApiState,
        query: DeployInfoQuery,
    ) -> Result<AsyncEventState, api::Error> {
        let request = DeployRequest::try_from(query)?;
        let schema = SchemaImpl::new(state.service_data());
        let status = schema.deploy_states.get(&request).ok_or_else(|| {
            api::Error::not_found().title("No corresponding deploy request found")
        })?;

        Ok(status)
    }

    /// Returns the state of migration for the given migration request.
    async fn migration_status(
        state: ServiceApiState,
        query: MigrationInfoQuery,
    ) -> Result<MigrationState, api::Error> {
        let request = MigrationRequest::try_from(query)?;
        let schema = SchemaImpl::new(state.service_data());
        let status = schema.migration_states.get(&request).ok_or_else(|| {
            api::Error::not_found().title("No corresponding migration request found")
        })?;

        Ok(status)
    }
}

/// Wires supervisor API endpoints.
pub(crate) fn wire(builder: &mut ServiceApiBuilder) {
    builder
        .private_scope()
        .pb_endpoint_mut("deploy-artifact", PrivateApi::deploy_artifact)
        .pb_endpoint_mut("migrate", PrivateApi::migrate)
        .pb_endpoint_mut("propose-config", PrivateApi::propose_config)
        .pb_endpoint_mut("confirm-config", PrivateApi::confirm_config)
        .endpoint("configuration-number", PrivateApi::configuration_number)
        .endpoint("supervisor-config", PrivateApi::supervisor_config)
        .endpoint("deploy-status", PrivateApi::deploy_status)
        .endpoint("migration-status", PrivateApi::migration_status);
    builder
        .public_scope()
        .endpoint("consensus-config", PublicApi::consensus_config)
        .endpoint("config-proposal", PublicApi::config_proposal)
        .endpoint("services", PublicApi::services);
}