casper-client 1.5.0

A client library and binary for interacting with the Casper network
Documentation
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
use std::{convert::TryInto, fs::File};

use async_trait::async_trait;
use jsonrpc_lite::{Id, JsonRpc, Params};
use rand::Rng;
use reqwest::Client;
use serde::Serialize;
use serde_json::{json, Map, Value};

use casper_execution_engine::core::engine_state::ExecutableDeployItem;
use casper_hashing::Digest;
use casper_node::{
    crypto,
    rpcs::{
        account::{PutDeploy, PutDeployParams},
        chain::{
            BlockIdentifier, GetBlock, GetBlockParams, GetBlockTransfers, GetBlockTransfersParams,
            GetEraInfoBySwitchBlock, GetEraInfoParams, GetStateRootHash, GetStateRootHashParams,
        },
        docs::ListRpcs,
        info::{GetDeploy, GetDeployParams, GetValidatorChanges},
        state::{
            GetAccountInfo, GetAccountInfoParams, GetAuctionInfo, GetAuctionInfoParams, GetBalance,
            GetBalanceParams, GetDictionaryItem, GetDictionaryItemParams, GetItem, GetItemParams,
            GlobalStateIdentifier, QueryGlobalState, QueryGlobalStateParams,
        },
        RpcWithOptionalParams, RpcWithParams, RpcWithoutParams, RPC_API_PATH,
    },
    types::{BlockHash, Deploy, DeployHash},
};
use casper_types::{AsymmetricType, Key, PublicKey, URef};

use crate::{
    deploy::{DeployExt, DeployParams, SendDeploy, Transfer},
    error::{Error, Result},
    validation, DictionaryItemStrParams, GlobalStateStrParams,
};

/// Struct representing a single JSON-RPC call to the casper node.
#[derive(Debug)]
pub(crate) struct RpcCall {
    rpc_id: Id,
    node_address: String,
    verbosity_level: u64,
}

/// `RpcCall` encapsulates calls made to the casper node service via JSON-RPC.
impl RpcCall {
    /// Creates a new RPC instance.
    ///
    /// `rpc_id` is used for RPC-ID as required by the JSON-RPC specification, and is returned by
    /// the node in the corresponding response.
    ///
    /// `node_address` identifies the network address of the target node's HTTP server, e.g.
    /// `"http://127.0.0.1:7777"`.
    ///
    /// When `verbosity_level` is `1`, the request will be printed to `stdout` with long string
    /// fields (e.g. hex-formatted raw Wasm bytes) shortened to a string indicating the char count
    /// of the field.  When `verbosity_level` is greater than `1`, the request will be printed to
    /// `stdout` with no abbreviation of long fields.  When `verbosity_level` is `0`, the request
    /// will not be printed to `stdout`.
    pub(crate) fn new(maybe_rpc_id: &str, node_address: &str, verbosity_level: u64) -> Self {
        let rpc_id = if maybe_rpc_id.is_empty() {
            Id::from(rand::thread_rng().gen::<i64>())
        } else if let Ok(i64_id) = maybe_rpc_id.parse::<i64>() {
            Id::from(i64_id)
        } else {
            Id::from(maybe_rpc_id.to_string())
        };

        Self {
            rpc_id,
            node_address: node_address.trim_end_matches('/').to_string(),
            verbosity_level,
        }
    }

    pub(crate) async fn get_deploy(self, deploy_hash: &str) -> Result<JsonRpc> {
        let hash =
            Digest::from_hex(deploy_hash).map_err(|error| map_hashing_error(error)("deploy"))?;
        let params = GetDeployParams {
            deploy_hash: DeployHash::new(hash),
            finalized_approvals: false,
        };
        GetDeploy::request_with_map_params(self, params).await
    }

    pub(crate) async fn get_item(
        self,
        state_root_hash: &str,
        key: &str,
        path: &str,
    ) -> Result<JsonRpc> {
        let state_root_hash = Digest::from_hex(state_root_hash)
            .map_err(|error| map_hashing_error(error)("state_root_hash"))?;

        let key = {
            if let Ok(key) = Key::from_formatted_str(key) {
                key
            } else if let Ok(public_key) = PublicKey::from_hex(key) {
                Key::Account(public_key.to_account_hash())
            } else {
                return Err(Error::FailedToParseKey);
            }
        };

        let path = if path.is_empty() {
            vec![]
        } else {
            path.split('/').map(ToString::to_string).collect()
        };

        let params = GetItemParams {
            state_root_hash,
            key: key.to_formatted_string(),
            path: path.clone(),
        };
        let response = GetItem::request_with_map_params(self, params).await?;
        validation::validate_query_response(&response, &state_root_hash, &key, &path)?;
        Ok(response)
    }

    pub(crate) async fn get_dictionary_item(
        self,
        state_root_hash: &str,
        dictionary_str_params: DictionaryItemStrParams<'_>,
    ) -> Result<JsonRpc> {
        let state_root_hash = Digest::from_hex(state_root_hash)
            .map_err(|error| map_hashing_error(error)("state_root_hash"))?;

        let dictionary_identifier = dictionary_str_params.try_into()?;

        let params = GetDictionaryItemParams {
            state_root_hash,
            dictionary_identifier,
        };

        let response = GetDictionaryItem::request_with_map_params(self, params).await?;
        Ok(response)
    }

    pub(crate) async fn get_state_root_hash(self, maybe_block_identifier: &str) -> Result<JsonRpc> {
        match Self::block_identifier(maybe_block_identifier)? {
            Some(block_identifier) => {
                let params = GetStateRootHashParams { block_identifier };
                GetStateRootHash::request_with_map_params(self, params).await
            }
            None => GetStateRootHash::request(self).await,
        }
    }

    pub(crate) async fn get_balance(
        self,
        state_root_hash: &str,
        purse_uref: &str,
    ) -> Result<JsonRpc> {
        // state_root_hash
        let state_root_hash = Digest::from_hex(state_root_hash)
            .map_err(|error| map_hashing_error(error)("state_root_hash"))?;
        let uref =
            URef::from_formatted_str(purse_uref).map_err(|error| Error::FailedToParseURef {
                context: "purse_uref",
                error,
            })?;
        let key = Key::from(uref);

        let params = GetBalanceParams {
            state_root_hash,
            purse_uref: purse_uref.to_string(),
        };
        let response = GetBalance::request_with_map_params(self, params).await?;
        validation::validate_get_balance_response(&response, &state_root_hash, &key)?;
        Ok(response)
    }

    pub(crate) async fn get_era_info_by_switch_block(
        self,
        maybe_block_identifier: &str,
    ) -> Result<JsonRpc> {
        let response = match Self::block_identifier(maybe_block_identifier)? {
            None => GetEraInfoBySwitchBlock::request(self).await,
            Some(block_identifier) => {
                let params = GetEraInfoParams { block_identifier };
                GetEraInfoBySwitchBlock::request_with_map_params(self, params).await
            }
        }?;
        validation::validate_get_era_info_response(&response)?;
        Ok(response)
    }

    pub(crate) async fn get_auction_info(self, maybe_block_identifier: &str) -> Result<JsonRpc> {
        let response = match Self::block_identifier(maybe_block_identifier)? {
            None => GetAuctionInfo::request(self).await,
            Some(block_identifier) => {
                let params = GetAuctionInfoParams { block_identifier };
                GetAuctionInfo::request_with_map_params(self, params).await
            }
        }?;
        Ok(response)
    }

    pub(crate) async fn get_validator_changes(self) -> Result<JsonRpc> {
        GetValidatorChanges::request(self).await
    }

    pub(crate) async fn list_rpcs(self) -> Result<JsonRpc> {
        ListRpcs::request(self).await
    }

    pub(crate) async fn transfer(
        self,
        amount: &str,
        source_purse: Option<URef>,
        target_account: &str,
        transfer_id: &str,
        deploy_params: DeployParams,
        payment: ExecutableDeployItem,
    ) -> Result<JsonRpc> {
        let deploy = Deploy::new_transfer(
            amount,
            source_purse,
            target_account,
            transfer_id,
            deploy_params,
            payment,
        )?;
        let params = PutDeployParams { deploy };
        Transfer::request_with_map_params(self, params).await
    }

    pub(crate) async fn send_deploy_file(self, input_path: &str) -> Result<JsonRpc> {
        let input = File::open(input_path).map_err(|error| Error::IoError {
            context: format!("unable to read input file '{}'", input_path),
            error,
        })?;
        let deploy = Deploy::read_deploy(input)?;
        let params = PutDeployParams { deploy };
        SendDeploy::request_with_map_params(self, params).await
    }

    pub(crate) async fn put_deploy(self, deploy: Deploy) -> Result<JsonRpc> {
        let params = PutDeployParams { deploy };
        PutDeploy::request_with_map_params(self, params).await
    }

    pub(crate) async fn get_block(self, maybe_block_identifier: &str) -> Result<JsonRpc> {
        let maybe_block_identifier = Self::block_identifier(maybe_block_identifier)?;
        let response = match maybe_block_identifier {
            Some(block_identifier) => {
                let params = GetBlockParams { block_identifier };
                GetBlock::request_with_map_params(self, params).await
            }
            None => GetBlock::request(self).await,
        }?;
        validation::validate_get_block_response(&response, &maybe_block_identifier)?;
        Ok(response)
    }

    pub(crate) async fn get_block_transfers(self, maybe_block_identifier: &str) -> Result<JsonRpc> {
        let maybe_block_identifier = Self::block_identifier(maybe_block_identifier)?;
        let response = match maybe_block_identifier {
            Some(block_identifier) => {
                let params = GetBlockTransfersParams { block_identifier };
                GetBlockTransfers::request_with_map_params(self, params).await
            }
            None => GetBlockTransfers::request(self).await,
        }?;
        Ok(response)
    }

    pub(crate) async fn get_account_info(
        self,
        public_key: &str,
        maybe_block_identifier: &str,
    ) -> Result<JsonRpc> {
        let key = if let Ok(public_key) = PublicKey::from_hex(public_key) {
            public_key
        } else {
            return Err(Error::FailedToParseKey);
        };
        let block_identifier = Self::block_identifier(maybe_block_identifier)?;
        let params = GetAccountInfoParams {
            public_key: key,
            block_identifier,
        };
        GetAccountInfo::request_with_map_params(self, params).await
    }

    pub(crate) async fn query_global_state(
        self,
        global_state_str_params: GlobalStateStrParams<'_>,
        key: &str,
        path: &str,
    ) -> Result<JsonRpc> {
        let global_state_identifier: GlobalStateIdentifier = global_state_str_params.try_into()?;

        let key = {
            if let Ok(key) = Key::from_formatted_str(key) {
                key
            } else if let Ok(public_key) = PublicKey::from_hex(key) {
                Key::Account(public_key.to_account_hash())
            } else {
                return Err(Error::FailedToParseKey);
            }
        };

        let path = if path.is_empty() {
            vec![]
        } else {
            path.split('/').map(ToString::to_string).collect()
        };

        let params = QueryGlobalStateParams {
            state_identifier: global_state_identifier.clone(),
            key: key.to_formatted_string(),
            path: path.clone(),
        };

        let response = QueryGlobalState::request_with_map_params(self, params).await?;
        validation::validate_query_global_state(&response, global_state_identifier, &key, &path)?;
        Ok(response)
    }

    fn block_identifier(maybe_block_identifier: &str) -> Result<Option<BlockIdentifier>> {
        if maybe_block_identifier.is_empty() {
            return Ok(None);
        }

        if maybe_block_identifier.len() == (Digest::LENGTH * 2) {
            let hash = Digest::from_hex(maybe_block_identifier)
                .map_err(|error| map_hashing_error(error)("block_identifier"))?;
            Ok(Some(BlockIdentifier::Hash(BlockHash::new(hash))))
        } else {
            let height =
                maybe_block_identifier
                    .parse()
                    .map_err(|error| Error::FailedToParseInt {
                        context: "block_identifier",
                        error,
                    })?;
            Ok(Some(BlockIdentifier::Height(height)))
        }
    }

    async fn request(self, method: &str, params: Params) -> Result<JsonRpc> {
        let url = format!("{}/{}", self.node_address, RPC_API_PATH);
        let rpc_req = JsonRpc::request_with_params(self.rpc_id, method, params);

        crate::pretty_print_at_level(&rpc_req, self.verbosity_level);

        let client = Client::new();
        let response = client
            .post(&url)
            .json(&rpc_req)
            .send()
            .await
            .map_err(Error::FailedToGetResponse)?;

        if let Err(error) = response.error_for_status_ref() {
            if self.verbosity_level > 0 {
                println!("Failed Sending {}", error);
            }
            return Err(Error::FailedSending(rpc_req));
        }

        let rpc_response = response.json().await.map_err(Error::FailedToParseResponse);

        if let Err(error) = rpc_response {
            if self.verbosity_level > 0 {
                println!("Failed parsing as a JSON-RPC response: {}", error);
            }
            return Err(error);
        }

        let rpc_response: JsonRpc = rpc_response?;

        if rpc_response.get_result().is_some() {
            if self.verbosity_level > 0 {
                println!("Received successful response:");
            }
            return Ok(rpc_response);
        }

        if let Some(error) = rpc_response.get_error() {
            if self.verbosity_level > 0 {
                println!("Response returned an error");
            }
            return Err(Error::ResponseIsError(error.clone()));
        }

        if self.verbosity_level > 0 {
            println!("Invalid response returned");
        }
        Err(Error::InvalidRpcResponse(rpc_response))
    }
}

/// Takes an external error and returns a [`casper_hashing::Error`] variant and allows you to pass
/// along error context.
pub fn map_hashing_error(hashing_error: casper_hashing::Error) -> impl Fn(&'static str) -> Error {
    move |context: &'static str| match &hashing_error {
        casper_hashing::Error::Base16DecodeError(decode_error) => Error::CryptoError {
            context,
            error: crypto::Error::FromHex(decode_error.clone()),
        },
        casper_hashing::Error::IncorrectDigestLength(length) => Error::InvalidArgument {
            context,
            error: format!("the hash provided had an invalid length of {}", length),
        },
    }
}

/// General purpose client trait for making requests to casper node's HTTP endpoints.
#[async_trait]
pub(crate) trait RpcClient {
    const RPC_METHOD: &'static str;

    /// Calls a casper node's JSON-RPC endpoint.
    async fn request(rpc_call: RpcCall) -> Result<JsonRpc> {
        rpc_call.request(Self::RPC_METHOD, Params::None(())).await
    }

    /// Calls a casper node's JSON-RPC endpoint with parameters.
    async fn request_with_map_params<T: IntoJsonMap + Send>(
        rpc_call: RpcCall,
        params: T,
    ) -> Result<JsonRpc> {
        rpc_call
            .request(Self::RPC_METHOD, Params::from(params.into_json_map()))
            .await
    }
}

impl RpcClient for GetBalance {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetBlock {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetBlockTransfers {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetStateRootHash {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetItem {
    const RPC_METHOD: &'static str = <Self as RpcWithParams>::METHOD;
}

impl RpcClient for GetEraInfoBySwitchBlock {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetAuctionInfo {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for ListRpcs {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetAccountInfo {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetDictionaryItem {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for QueryGlobalState {
    const RPC_METHOD: &'static str = Self::METHOD;
}

impl RpcClient for GetValidatorChanges {
    const RPC_METHOD: &'static str = Self::METHOD;
}

pub(crate) trait IntoJsonMap: Serialize {
    fn into_json_map(self) -> Map<String, Value>
    where
        Self: Sized,
    {
        json!(self)
            .as_object()
            .unwrap_or_else(|| panic!("should be a JSON object"))
            .clone()
    }
}

impl IntoJsonMap for PutDeployParams {}
impl IntoJsonMap for GetBlockParams {}
impl IntoJsonMap for GetBlockTransfersParams {}
impl IntoJsonMap for GetStateRootHashParams {}
impl IntoJsonMap for GetDeployParams {}
impl IntoJsonMap for GetBalanceParams {}
impl IntoJsonMap for GetItemParams {}
impl IntoJsonMap for GetEraInfoParams {}
impl IntoJsonMap for ListRpcs {}
impl IntoJsonMap for GetAuctionInfoParams {}
impl IntoJsonMap for GetAccountInfoParams {}
impl IntoJsonMap for GetDictionaryItemParams {}
impl IntoJsonMap for QueryGlobalStateParams {}