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
#![allow(clippy::field_reassign_with_default)]
use std::str;
use futures::{future::BoxFuture, FutureExt};
use http::Response;
use hyper::Body;
use once_cell::sync::Lazy;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::{error, info};
use warp_json_rpc::Builder;
use casper_execution_engine::core::engine_state::{BalanceResult, GetBidsResult};
use casper_types::{
bytesrepr::ToBytes, CLType, CLValue, Key, ProtocolVersion, PublicKey, SecretKey, URef, U512,
};
use super::{
docs::{DocExample, DOCS_EXAMPLE_PROTOCOL_VERSION},
Error, ErrorCode, ReactorEventT, RpcRequest, RpcWithParams, RpcWithParamsExt,
};
use crate::{
components::rpc_server::rpcs::RpcWithOptionalParams,
crypto::hash::Digest,
effect::EffectBuilder,
reactor::QueueKind,
rpcs::{
chain::BlockIdentifier,
common::{self, MERKLE_PROOF},
RpcWithOptionalParamsExt,
},
types::{
json_compatibility::{Account, AuctionState, StoredValue},
Block,
},
};
static GET_ITEM_PARAMS: Lazy<GetItemParams> = Lazy::new(|| GetItemParams {
state_root_hash: *Block::doc_example().header().state_root_hash(),
key: "deploy-af684263911154d26fa05be9963171802801a0b6aff8f199b7391eacb8edc9e1".to_string(),
path: vec!["inner".to_string()],
});
static GET_ITEM_RESULT: Lazy<GetItemResult> = Lazy::new(|| GetItemResult {
api_version: DOCS_EXAMPLE_PROTOCOL_VERSION,
stored_value: StoredValue::CLValue(CLValue::from_t(1u64).unwrap()),
merkle_proof: MERKLE_PROOF.clone(),
});
static GET_BALANCE_PARAMS: Lazy<GetBalanceParams> = Lazy::new(|| GetBalanceParams {
state_root_hash: *Block::doc_example().header().state_root_hash(),
purse_uref: "uref-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db-007"
.to_string(),
});
static GET_BALANCE_RESULT: Lazy<GetBalanceResult> = Lazy::new(|| GetBalanceResult {
api_version: DOCS_EXAMPLE_PROTOCOL_VERSION,
balance_value: U512::from(123_456),
merkle_proof: MERKLE_PROOF.clone(),
});
static GET_AUCTION_INFO_PARAMS: Lazy<GetAuctionInfoParams> = Lazy::new(|| GetAuctionInfoParams {
block_identifier: BlockIdentifier::Hash(*Block::doc_example().hash()),
});
static GET_AUCTION_INFO_RESULT: Lazy<GetAuctionInfoResult> = Lazy::new(|| GetAuctionInfoResult {
api_version: DOCS_EXAMPLE_PROTOCOL_VERSION,
auction_state: AuctionState::doc_example().clone(),
});
static GET_ACCOUNT_INFO_PARAMS: Lazy<GetAccountInfoParams> = Lazy::new(|| {
let secret_key = SecretKey::ed25519_from_bytes([0; 32]).unwrap();
let public_key = PublicKey::from(&secret_key);
GetAccountInfoParams {
public_key,
block_identifier: Some(BlockIdentifier::Hash(*Block::doc_example().hash())),
}
});
static GET_ACCOUNT_INFO_RESULT: Lazy<GetAccountInfoResult> = Lazy::new(|| GetAccountInfoResult {
api_version: DOCS_EXAMPLE_PROTOCOL_VERSION,
account: Account::doc_example().clone(),
merkle_proof: MERKLE_PROOF.clone(),
});
static GET_DICTIONARY_ITEM_PARAMS: Lazy<GetDictionaryItemParams> =
Lazy::new(|| GetDictionaryItemParams {
state_root_hash: *Block::doc_example().header().state_root_hash(),
dictionary_identifier: DictionaryIdentifier::URef {
seed_uref: "uref-09480c3248ef76b603d386f3f4f8a5f87f597d4eaffd475433f861af187ab5db-007"
.to_string(),
dictionary_item_key: "a_unique_entry_identifier".to_string(),
},
});
static GET_DICTIONARY_ITEM_RESULT: Lazy<GetDictionaryItemResult> =
Lazy::new(|| GetDictionaryItemResult {
api_version: DOCS_EXAMPLE_PROTOCOL_VERSION,
dictionary_key:
"dictionary-67518854aa916c97d4e53df8570c8217ccc259da2721b692102d76acd0ee8d1f"
.to_string(),
stored_value: StoredValue::CLValue(CLValue::from_t(1u64).unwrap()),
merkle_proof: MERKLE_PROOF.clone(),
});
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetItemParams {
pub state_root_hash: Digest,
pub key: String,
#[serde(default)]
pub path: Vec<String>,
}
impl DocExample for GetItemParams {
fn doc_example() -> &'static Self {
&*GET_ITEM_PARAMS
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetItemResult {
#[schemars(with = "String")]
pub api_version: ProtocolVersion,
pub stored_value: StoredValue,
pub merkle_proof: String,
}
impl DocExample for GetItemResult {
fn doc_example() -> &'static Self {
&*GET_ITEM_RESULT
}
}
pub struct GetItem {}
impl RpcWithParams for GetItem {
const METHOD: &'static str = "state_get_item";
type RequestParams = GetItemParams;
type ResponseResult = GetItemResult;
}
impl RpcWithParamsExt for GetItem {
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
params: Self::RequestParams,
api_version: ProtocolVersion,
) -> BoxFuture<'static, Result<Response<Body>, Error>> {
async move {
let base_key = match Key::from_formatted_str(¶ms.key)
.map_err(|error| format!("failed to parse key: {}", error))
{
Ok(key) => key,
Err(error_msg) => {
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::ParseQueryKey as i64,
error_msg,
))?);
}
};
let query_result = effect_builder
.make_request(
|responder| RpcRequest::QueryGlobalState {
state_root_hash: params.state_root_hash,
base_key,
path: params.path,
responder,
},
QueueKind::Api,
)
.await;
let (stored_value, proof_bytes) = match common::extract_query_result(query_result) {
Ok(tuple) => tuple,
Err((error_code, error_msg)) => {
info!("{}", error_msg);
return Ok(response_builder
.error(warp_json_rpc::Error::custom(error_code as i64, error_msg))?);
}
};
let result = Self::ResponseResult {
api_version,
stored_value,
merkle_proof: hex::encode(proof_bytes),
};
Ok(response_builder.success(result)?)
}
.boxed()
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetBalanceParams {
pub state_root_hash: Digest,
pub purse_uref: String,
}
impl DocExample for GetBalanceParams {
fn doc_example() -> &'static Self {
&*GET_BALANCE_PARAMS
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetBalanceResult {
#[schemars(with = "String")]
pub api_version: ProtocolVersion,
pub balance_value: U512,
pub merkle_proof: String,
}
impl DocExample for GetBalanceResult {
fn doc_example() -> &'static Self {
&*GET_BALANCE_RESULT
}
}
pub struct GetBalance {}
impl RpcWithParams for GetBalance {
const METHOD: &'static str = "state_get_balance";
type RequestParams = GetBalanceParams;
type ResponseResult = GetBalanceResult;
}
impl RpcWithParamsExt for GetBalance {
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
params: Self::RequestParams,
api_version: ProtocolVersion,
) -> BoxFuture<'static, Result<Response<Body>, Error>> {
async move {
let purse_uref = match URef::from_formatted_str(¶ms.purse_uref)
.map_err(|error| format!("failed to parse purse_uref: {:?}", error))
{
Ok(uref) => uref,
Err(error_msg) => {
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::ParseGetBalanceURef as i64,
error_msg,
))?);
}
};
let balance_result = effect_builder
.make_request(
|responder| RpcRequest::GetBalance {
state_root_hash: params.state_root_hash,
purse_uref,
responder,
},
QueueKind::Api,
)
.await;
let (balance_value, balance_proof) = match balance_result {
Ok(BalanceResult::Success { motes, proof }) => (motes, proof),
Ok(balance_result) => {
let error_msg = format!("get-balance failed: {:?}", balance_result);
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::GetBalanceFailed as i64,
error_msg,
))?);
}
Err(error) => {
let error_msg = format!("get-balance failed to execute: {}", error);
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::GetBalanceFailedToExecute as i64,
error_msg,
))?);
}
};
let proof_bytes = match balance_proof.to_bytes() {
Ok(proof_bytes) => proof_bytes,
Err(error) => {
info!("failed to encode stored value: {}", error);
return Ok(response_builder.error(warp_json_rpc::Error::INTERNAL_ERROR)?);
}
};
let merkle_proof = hex::encode(proof_bytes);
let result = Self::ResponseResult {
api_version,
balance_value,
merkle_proof,
};
Ok(response_builder.success(result)?)
}
.boxed()
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetAuctionInfoParams {
pub block_identifier: BlockIdentifier,
}
impl DocExample for GetAuctionInfoParams {
fn doc_example() -> &'static Self {
&*GET_AUCTION_INFO_PARAMS
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetAuctionInfoResult {
#[schemars(with = "String")]
pub api_version: ProtocolVersion,
pub auction_state: AuctionState,
}
impl DocExample for GetAuctionInfoResult {
fn doc_example() -> &'static Self {
&*GET_AUCTION_INFO_RESULT
}
}
pub struct GetAuctionInfo {}
impl RpcWithOptionalParams for GetAuctionInfo {
const METHOD: &'static str = "state_get_auction_info";
type OptionalRequestParams = GetAuctionInfoParams;
type ResponseResult = GetAuctionInfoResult;
}
impl RpcWithOptionalParamsExt for GetAuctionInfo {
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
maybe_params: Option<Self::OptionalRequestParams>,
api_version: ProtocolVersion,
) -> BoxFuture<'static, Result<Response<Body>, Error>> {
async move {
let maybe_id = maybe_params.map(|params| params.block_identifier);
let block: Block = {
let maybe_block = effect_builder
.make_request(
|responder| RpcRequest::GetBlock {
maybe_id,
responder,
},
QueueKind::Api,
)
.await;
match maybe_block {
None => {
let error_msg = if maybe_id.is_none() {
"get-auction-info failed to get last added block".to_string()
} else {
"get-auction-info failed to get specified block".to_string()
};
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::NoSuchBlock as i64,
error_msg,
))?);
}
Some((block, _)) => block,
}
};
let protocol_version = api_version;
let state_root_hash = *block.header().state_root_hash();
let block_height = block.header().height();
let get_bids_result = effect_builder
.make_request(
|responder| RpcRequest::GetBids {
state_root_hash,
responder,
},
QueueKind::Api,
)
.await;
let maybe_bids = if let Ok(GetBidsResult::Success { bids, .. }) = get_bids_result {
Some(bids)
} else {
None
};
let era_validators_result = effect_builder
.make_request(
|responder| RpcRequest::QueryEraValidators {
state_root_hash,
protocol_version,
responder,
},
QueueKind::Api,
)
.await;
let era_validators = era_validators_result.ok();
let auction_state =
AuctionState::new(state_root_hash, block_height, era_validators, maybe_bids);
let result = Self::ResponseResult {
api_version,
auction_state,
};
Ok(response_builder.success(result)?)
}
.boxed()
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetAccountInfoParams {
pub public_key: PublicKey,
pub block_identifier: Option<BlockIdentifier>,
}
impl DocExample for GetAccountInfoParams {
fn doc_example() -> &'static Self {
&*GET_ACCOUNT_INFO_PARAMS
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetAccountInfoResult {
#[schemars(with = "String")]
pub api_version: ProtocolVersion,
pub account: Account,
pub merkle_proof: String,
}
impl DocExample for GetAccountInfoResult {
fn doc_example() -> &'static Self {
&*GET_ACCOUNT_INFO_RESULT
}
}
pub struct GetAccountInfo {}
impl RpcWithParams for GetAccountInfo {
const METHOD: &'static str = "state_get_account_info";
type RequestParams = GetAccountInfoParams;
type ResponseResult = GetAccountInfoResult;
}
impl RpcWithParamsExt for GetAccountInfo {
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
params: Self::RequestParams,
api_version: ProtocolVersion,
) -> BoxFuture<'static, Result<Response<Body>, Error>> {
async move {
let base_key = {
let account_hash = params.public_key.to_account_hash();
Key::Account(account_hash)
};
let block: Block = {
let maybe_id = params.block_identifier;
let maybe_block = effect_builder
.make_request(
|responder| RpcRequest::GetBlock {
maybe_id,
responder,
},
QueueKind::Api,
)
.await;
match maybe_block {
None => {
let error_msg = if maybe_id.is_none() {
"get-account-info failed to get last added block".to_string()
} else {
"get-account-info failed to get specified block".to_string()
};
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::NoSuchBlock as i64,
error_msg,
))?);
}
Some((block, _)) => block,
}
};
let state_root_hash = *block.header().state_root_hash();
let query_result = effect_builder
.make_request(
|responder| RpcRequest::QueryGlobalState {
state_root_hash,
base_key,
path: vec![],
responder,
},
QueueKind::Api,
)
.await;
let (stored_value, proof_bytes) = match common::extract_query_result(query_result) {
Ok(tuple) => tuple,
Err((error_code, error_msg)) => {
info!("{}", error_msg);
return Ok(response_builder
.error(warp_json_rpc::Error::custom(error_code as i64, error_msg))?);
}
};
let account = if let StoredValue::Account(account) = stored_value {
account
} else {
let error_msg = "get-account-info failed to get specified account".to_string();
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::NoSuchAccount as i64,
error_msg,
))?);
};
let result = Self::ResponseResult {
api_version,
account,
merkle_proof: hex::encode(proof_bytes),
};
Ok(response_builder.success(result)?)
}
.boxed()
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema, Clone)]
pub enum DictionaryIdentifier {
AccountNamedKey {
key: String,
dictionary_name: String,
dictionary_item_key: String,
},
ContractNamedKey {
key: String,
dictionary_name: String,
dictionary_item_key: String,
},
URef {
seed_uref: String,
dictionary_item_key: String,
},
Dictionary(String),
}
impl DictionaryIdentifier {
fn get_dictionary_base_key(&self) -> Result<Option<Key>, Error> {
match self {
DictionaryIdentifier::AccountNamedKey { ref key, .. }
| DictionaryIdentifier::ContractNamedKey { ref key, .. } => {
match Key::from_formatted_str(key) {
Ok(key) => Ok(Some(key)),
Err(error) => Err(Error(format!("failed to parse key: {}", error))),
}
}
DictionaryIdentifier::URef { .. } | DictionaryIdentifier::Dictionary(_) => Ok(None),
}
}
fn get_base_query_path(&self) -> Result<Option<Vec<String>>, Error> {
match self {
DictionaryIdentifier::AccountNamedKey {
dictionary_name, ..
}
| DictionaryIdentifier::ContractNamedKey {
dictionary_name, ..
} => Ok(Some(vec![dictionary_name.clone()])),
DictionaryIdentifier::URef { .. } | DictionaryIdentifier::Dictionary(_) => Ok(None),
}
}
fn get_dictionary_address(
&self,
maybe_stored_value: Option<StoredValue>,
) -> Result<Key, Error> {
match self {
DictionaryIdentifier::AccountNamedKey {
dictionary_item_key,
..
}
| DictionaryIdentifier::ContractNamedKey {
dictionary_item_key,
..
} => match maybe_stored_value {
Some(StoredValue::CLValue(value)) => {
if *value.cl_type() == CLType::URef {
let seed: URef = value
.into_t()
.map_err(|_| Error("Failed to parse URef".to_string()))?;
let key_bytes = dictionary_item_key.as_str().as_bytes();
Ok(Key::dictionary(seed, key_bytes))
} else {
Err(Error("Failed create dictionary address".to_string()))
}
}
Some(_) | None => Err(Error("Failed to create dictionary address".to_string())),
},
DictionaryIdentifier::URef {
seed_uref,
dictionary_item_key,
} => {
let key_bytes = dictionary_item_key.as_str().as_bytes();
let seed_uref = URef::from_formatted_str(seed_uref)
.map_err(|_| Error("Failed to parse URef".to_string()))?;
Ok(Key::dictionary(seed_uref, key_bytes))
}
DictionaryIdentifier::Dictionary(address) => Key::from_formatted_str(address)
.map_err(|_| Error("Failed to parse Dictionary key".to_string())),
}
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetDictionaryItemParams {
pub state_root_hash: Digest,
pub dictionary_identifier: DictionaryIdentifier,
}
impl DocExample for GetDictionaryItemParams {
fn doc_example() -> &'static Self {
&*GET_DICTIONARY_ITEM_PARAMS
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct GetDictionaryItemResult {
#[schemars(with = "String")]
pub api_version: ProtocolVersion,
pub dictionary_key: String,
pub stored_value: StoredValue,
pub merkle_proof: String,
}
impl DocExample for GetDictionaryItemResult {
fn doc_example() -> &'static Self {
&*GET_DICTIONARY_ITEM_RESULT
}
}
pub struct GetDictionaryItem {}
impl RpcWithParams for GetDictionaryItem {
const METHOD: &'static str = "state_get_dictionary_item";
type RequestParams = GetDictionaryItemParams;
type ResponseResult = GetDictionaryItemResult;
}
impl RpcWithParamsExt for GetDictionaryItem {
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
params: Self::RequestParams,
api_version: ProtocolVersion,
) -> BoxFuture<'static, Result<Response<Body>, Error>> {
async move {
let dictionary_address = match params.dictionary_identifier {
DictionaryIdentifier::AccountNamedKey { .. }
| DictionaryIdentifier::ContractNamedKey { .. } => {
let base_key = match params.dictionary_identifier.get_dictionary_base_key() {
Ok(Some(key)) => key,
Err(_) | Ok(None) => {
error!("Failed to parse key");
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::ParseQueryKey as i64,
"Failed to parse key",
))?);
}
};
let path = match params.dictionary_identifier.get_base_query_path() {
Ok(Some(path)) => path,
Err(_) | Ok(None) => {
error!("Failed to execute query");
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::NoDictionaryName as i64,
"Failed to execute query",
))?);
}
};
let query_result = effect_builder
.make_request(
|responder| RpcRequest::QueryGlobalState {
state_root_hash: params.state_root_hash,
base_key,
path,
responder,
},
QueueKind::Api,
)
.await;
let (stored_value, _) = match common::extract_query_result(query_result) {
Ok(tuple) => tuple,
Err((error_code, error_msg)) => {
info!("{}", error_msg);
return Ok(response_builder.error(warp_json_rpc::Error::custom(
error_code as i64,
error_msg,
))?);
}
};
params
.dictionary_identifier
.get_dictionary_address(Some(stored_value))
}
DictionaryIdentifier::URef { .. } | DictionaryIdentifier::Dictionary(_) => {
params.dictionary_identifier.get_dictionary_address(None)
}
};
let dictionary_query_key = match dictionary_address {
Ok(key) => key,
Err(Error(message)) => {
return Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::FailedToGetDictionaryURef as i64,
message,
))?)
}
};
let query_result = effect_builder
.make_request(
|responder| RpcRequest::QueryGlobalState {
state_root_hash: params.state_root_hash,
base_key: dictionary_query_key,
path: vec![],
responder,
},
QueueKind::Api,
)
.await;
let (stored_value, proof_bytes) = match common::extract_query_result(query_result) {
Ok(tuple) => tuple,
Err((error_code, error_msg)) => {
info!("{}", error_msg);
return Ok(response_builder
.error(warp_json_rpc::Error::custom(error_code as i64, error_msg))?);
}
};
let result = Self::ResponseResult {
api_version,
dictionary_key: dictionary_query_key.to_formatted_string(),
stored_value,
merkle_proof: hex::encode(proof_bytes),
};
Ok(response_builder.success(result)?)
}
.boxed()
}
}