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
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! The purpose of the JsonRpcClient presented here is to provide a lightweight and secure
//! JSON RPC client to talk to the JSON RPC service offered by Libra Full Nodes. This is useful
//! for various security-critical components (e.g., the secure key manager), as it allows
//! interaction with the Libra blockchain in a minimal and secure manner.
//!
//! Note: While a JSON RPC client implementation already exists in the Libra codebase, this
//! provides a simpler and (hopefully) more secure implementation with fewer dependencies.
#![forbid(unsafe_code)]

use hex::FromHexError;
use libra_types::{
    account_address::AccountAddress, account_state::AccountState,
    account_state_blob::AccountStateBlob, transaction::SignedTransaction,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::{convert::TryFrom, env, io};
use thiserror::Error;
use ureq::Response;

/// Various constants for the JSON RPC client implementation
const JSON_RPC_VERSION: &str = "2.0";
const REQUEST_TIMEOUT: u64 = 10_000;

#[derive(Debug, Error, PartialEq)]
pub enum Error {
    #[error("JSON RPC call returned a custom internal error: {0}")]
    InternalRPCError(String),
    #[error("Data does not exist. Missing data: {0}")]
    MissingData(String),
    #[error("JSON RPC call failed with response: {0}")]
    RPCFailure(String),
    #[error("Serialization error: {0}")]
    SerializationError(String),
    #[error("Unknown error: {0}")]
    UnknownError(String),
}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Self::UnknownError(format!("{}", error))
    }
}

impl From<lcs::Error> for Error {
    fn from(error: lcs::Error) -> Self {
        Self::SerializationError(format!("{}", error))
    }
}

impl From<serde_json::Error> for Error {
    fn from(error: serde_json::Error) -> Self {
        Self::SerializationError(format!("{}", error))
    }
}

impl From<FromHexError> for Error {
    fn from(error: FromHexError) -> Self {
        Self::SerializationError(format!("{}", error))
    }
}

/// Provides a lightweight JsonRpcClient implementation.
#[derive(Clone)]
pub struct JsonRpcClient {
    host: String,
}

impl JsonRpcClient {
    pub fn new(host: String) -> Self {
        Self { host }
    }

    /// Submits a signed transaction to the Libra blockchain. This is done by sending a submit()
    /// request to the JSON RPC server using the given transaction.
    pub fn submit_transaction(&self, signed_transaction: SignedTransaction) -> Result<(), Error> {
        let method = "submit".into();
        let params = vec![Value::String(hex::encode(lcs::to_bytes(
            &signed_transaction,
        )?))];
        let response = self.execute_request(method, params);

        process_submit_transaction_response(response)
    }

    /// Returns the associated AccountState for a specific account at a given version height.
    /// This is done by sending a get_account_state_with_proof() request to the JSON RPC server
    /// for the specified account, optional version height and optional ledger_version height
    /// (for the proof).
    pub fn get_account_state(
        &self,
        account: AccountAddress,
        version: Option<u64>,
    ) -> Result<AccountState, Error> {
        let method = "get_account_state_with_proof".into();
        let params = vec![
            Value::String(account.to_string()),
            json!(version),
            json!(version),
        ];
        let response = self.execute_request(method, params);

        process_account_state_response(response)
    }

    /// Retrieves the status of a transaction on a given account.
    pub fn get_transaction_status(
        &self,
        account: AccountAddress,
        sequence_number: u64,
    ) -> Result<Option<TransactionView>, Error> {
        let method = "get_account_transaction".into();
        let params = vec![
            Value::String(account.to_string()),
            json!(sequence_number),
            json!(false),
        ];
        let response = self.execute_request(method, params);

        process_transaction_status_response(response)
    }

    // Executes the specified request method using the given parameters by contacting the JSON RPC
    // server. If the 'http_proxy' or 'https_proxy' environment variable is set, enable the proxy.
    fn execute_request(&self, method: String, params: Vec<Value>) -> Response {
        let mut request = ureq::post(&self.host)
            .timeout_connect(REQUEST_TIMEOUT)
            .build();

        let scheme = request
            .get_scheme()
            .expect("Unable to get the scheme from the host");
        match scheme.as_str() {
            "http" => {
                if let Ok(proxy) = env::var("http_proxy") {
                    request.set_proxy(
                        ureq::Proxy::new(proxy)
                            .expect("Unable to parse http_proxy environment variable"),
                    );
                };
            }
            "https" => {
                if let Ok(proxy) = env::var("https_proxy") {
                    request.set_proxy(
                        ureq::Proxy::new(proxy)
                            .expect("Unable to parse https_proxy environment variable"),
                    );
                };
            }
            _ => {}
        }

        request.send_json(
            json!({"jsonrpc": JSON_RPC_VERSION, "method": method, "params": params, "id": 0}),
        )
    }
}

/// Processes the response from a submit_transaction() JSON RPC request.
pub fn process_submit_transaction_response(response: Response) -> Result<(), Error> {
    match response.status() {
        200 => {
            let response = response.into_string()?;
            if let Ok(failure_response) = serde_json::from_str::<JSONRpcFailureResponse>(&response)
            {
                Err(Error::InternalRPCError(format!("{:?}", failure_response)))
            } else {
                let _submit_response =
                    serde_json::from_str::<SubmitTransactionResponse>(&response)?;
                Ok(())
            }
        }
        _ => Err(Error::RPCFailure(response.into_string()?)),
    }
}

/// Processes the response from a get_account_state_with_proof() JSON RPC request.
pub fn process_account_state_response(response: Response) -> Result<AccountState, Error> {
    match response.status() {
        200 => {
            let response = &response.into_string()?;
            if let Ok(failure_response) = serde_json::from_str::<JSONRpcFailureResponse>(&response)
            {
                Err(Error::InternalRPCError(format!("{:?}", failure_response)))
            } else if let Some(blob_bytes) =
                serde_json::from_str::<AccountStateWithProofResponse>(&response)?
                    .result
                    .blob
            {
                let account_state_blob =
                    AccountStateBlob::from(lcs::from_bytes::<Vec<u8>>(&*blob_bytes.into_bytes()?)?);
                if let Ok(account_state) = AccountState::try_from(&account_state_blob) {
                    Ok(account_state)
                } else {
                    Err(Error::SerializationError(format!(
                        "Unable to convert account_state_blob to AccountState: {:?}",
                        account_state_blob
                    )))
                }
            } else {
                Err(Error::MissingData("AccountState".into()))
            }
        }
        _ => Err(Error::RPCFailure(response.into_string()?)),
    }
}

/// Processes the response from a get_transaction_status() JSON RPC request.
pub fn process_transaction_status_response(
    response: Response,
) -> Result<Option<TransactionView>, Error> {
    match response.status() {
        200 => {
            let response = response.into_string()?;
            if let Ok(response) = serde_json::from_str::<JSONRpcFailureResponse>(&response) {
                return Err(Error::InternalRPCError(format!("{:?}", response)));
            }

            let view = serde_json::from_str::<TransactionViewResponse>(&response)?;
            Ok(view.result)
        }
        _ => Err(Error::RPCFailure(response.into_string()?)),
    }
}

/// Below is a sample response from a successful submit() JSON RPC call:
/// "{
///   "id": 0,
///   "jsonrpc": "2.0",
///   "result": null
/// }"
///
///
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct SubmitTransactionResponse {
    id: u64,
    jsonrpc: String,
    result: Option<Value>,
}

/// Below is a sample response from a successful get_account_state_with_proof_call() JSON RPC call.
/// "{
///   "id": 0,
///   "jsonrpc": "2.0",
///   "result": {
///     "blob": "0100...",
///     "proof": {
///       "ledger_info_to_transaction_info_proof": "00..",
///       "transaction_info": "200000000000000000...<truncated>...ffffffffffffffff",
///       "transaction_info_to_account_proof": "0000..."
///     },
///     "version": 0
///   }
/// }"
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct AccountStateWithProofResponse {
    id: u64,
    jsonrpc: String,
    result: AccountStateResponse,
}

/// In practice this represents an AccountStateWithProof, however, we only decode the relevant
/// fields here.
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct AccountStateResponse {
    version: u64,
    blob: Option<Bytes>,
}

/// Below is a sample response from a successful get_account_state_with_proof_call() JSON RPC call.
/// "{
///   "id": 0,
///   "jsonrpc": "2.0",
///   "result": ...
/// }"
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct TransactionViewResponse {
    id: u64,
    jsonrpc: String,
    result: Option<TransactionView>,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct TransactionView {
    pub vm_status: VMStatusView,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum VMStatusView {
    Executed,
    OutOfGas,
    MoveAbort {
        location: String,
        abort_code: u64,
    },
    ExecutionFailure {
        location: String,
        function_index: u16,
        code_offset: u16,
    },
    VerificationError,
    DeserializationError,
    PublishingFailure,
}

impl std::fmt::Display for VMStatusView {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VMStatusView::Executed => write!(f, "Executed"),
            VMStatusView::OutOfGas => write!(f, "Out of Gas"),
            VMStatusView::MoveAbort {
                location,
                abort_code,
            } => write!(f, "Move Abort: {} at {}", abort_code, location),
            VMStatusView::ExecutionFailure {
                location,
                function_index,
                code_offset,
            } => write!(
                f,
                "Execution failure: {} {} {}",
                location, function_index, code_offset
            ),
            VMStatusView::VerificationError => write!(f, "Verification Error"),
            VMStatusView::DeserializationError => write!(f, "Deserialization Error"),
            VMStatusView::PublishingFailure => write!(f, "Publishing Failure"),
        }
    }
}

/// Below is a sample response from a failed JSON RPC call:
/// "{
///   "error": {
///     "code": -32000,
///     "data": null,
///     "message": "Server error: send failed because receiver is gone"
///   },
///   "id": 0,
///   "jsonrpc: "2.0"
/// }"
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct JSONRpcFailureResponse {
    id: u64,
    jsonrpc: String,
    error: JsonRpcError,
}

/// A custom error returned by the JSON RPC server for API calls that fail internally (e.g.,
/// an internal error during execution on the server side).
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct JsonRpcError {
    code: i16,
    message: String,
    data: Option<Value>,
}

/// Represents a vector of bytes using hex encoding.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct Bytes(pub String);

impl Bytes {
    pub fn into_bytes(self) -> Result<Vec<u8>, Error> {
        Ok(hex::decode(self.0)?)
    }
}

impl From<&[u8]> for Bytes {
    fn from(bytes: &[u8]) -> Self {
        Self(hex::encode(bytes))
    }
}

impl From<&Vec<u8>> for Bytes {
    fn from(bytes: &Vec<u8>) -> Self {
        Self(hex::encode(bytes))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use anyhow::Result;
    use futures::{channel::mpsc::channel, StreamExt};
    use libra_config::utils;
    use libra_crypto::HashValue;
    use libra_json_rpc::test_bootstrap;
    use libra_types::{
        account_address::AccountAddress,
        account_state_blob::{AccountStateBlob, AccountStateWithProof},
        block_info::BlockInfo,
        contract_event::ContractEvent,
        epoch_change::EpochChangeProof,
        event::EventKey,
        ledger_info::{LedgerInfo, LedgerInfoWithSignatures},
        mempool_status::{MempoolStatus, MempoolStatusCode},
        proof::{AccumulatorConsistencyProof, SparseMerkleProof},
        transaction::{TransactionListWithProof, TransactionWithProof, Version},
    };
    use libradb::errors::LibraDbError::NotFound;
    use std::{collections::BTreeMap, sync::Arc};
    use storage_interface::{DbReader, Order, StartupInfo, TreeState};
    use tokio::runtime::Runtime;
    use vm_validator::{
        mocks::mock_vm_validator::MockVMValidator, vm_validator::TransactionValidation,
    };

    #[test]
    fn test_submit_transaction() {
        let mock_db = create_empty_mock_db();
        let (client, _server) = create_client_and_server(mock_db, true);
        let signed_transaction = test_helpers::generate_signed_transaction();

        // Ensure transaction submitted and validated successfully
        let result = client.submit_transaction(signed_transaction);
        assert!(result.is_ok());
    }

    #[test]
    fn test_submit_transaction_failure() {
        let mock_db = create_empty_mock_db();
        // When creating the JSON RPC server, specify 'mock_validator=false' to prevent a vm validator
        // being passed to the server. This will cause any transaction submission requests to the JSON
        // RPC server to fail, thus causing the server to return an error.
        let (client, _server) = create_client_and_server(mock_db, false);
        let signed_transaction = test_helpers::generate_signed_transaction();

        // Ensure transaction submitted successfully
        let result = client.submit_transaction(signed_transaction);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_account_state() {
        // Create test account state data
        let account = AccountAddress::random();
        let account_state = test_helpers::create_test_account_state();
        let version_height = 0;
        let account_state_with_proof =
            test_helpers::create_test_state_with_proof(&account_state, version_height);

        // Create an account to account_state_with_proof mapping
        let mut map = BTreeMap::new();
        map.insert(account, account_state_with_proof);

        // Populate the test database with the test data and create the client/server
        let mock_db = MockLibraDB::new(map);
        let (client, _server) = create_client_and_server(mock_db, true);

        // Ensure the client returns the correct AccountState
        let result = client.get_account_state(account, Some(version_height));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), account_state);
    }

    #[test]
    fn test_get_account_state_version_not_specified() {
        // Create test account state data
        let account = AccountAddress::random();
        let account_state = test_helpers::create_test_account_state();
        let account_state_with_proof =
            test_helpers::create_test_state_with_proof(&account_state, 0);

        // Create an account to account_state_with_proof mapping
        let mut map = BTreeMap::new();
        map.insert(account, account_state_with_proof);

        // Populate the test database with the test data and create the client/server
        let mock_db = MockLibraDB::new(map);
        let (client, _server) = create_client_and_server(mock_db, true);

        // Ensure the client returns the latest AccountState (even though no version was specified)
        let result = client.get_account_state(account, None);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), account_state);
    }

    #[test]
    fn test_get_account_state_missing() {
        let mock_db = create_empty_mock_db();
        let (client, _server) = create_client_and_server(mock_db, true);

        // Ensure the client returns an error for a missing AccountState
        let account = AccountAddress::random();
        let result = client.get_account_state(account, Some(0));
        assert!(result.is_err());
    }

    #[test]
    fn test_get_account_state_missing_blob() {
        // Create test account state data
        let account = AccountAddress::random();
        let version_height = 0;
        let account_state_proof = test_helpers::create_test_state_proof();
        let account_state_with_proof =
            AccountStateWithProof::new(version_height, None, account_state_proof);

        // Create an account to account_state_with_proof mapping
        let mut map = BTreeMap::new();
        map.insert(account, account_state_with_proof);

        // Populate the test database with the test data and create the client/server
        let mock_db = MockLibraDB::new(map);
        let (client, _server) = create_client_and_server(mock_db, true);

        // Ensure the client returns an error for the missing AccountState
        let result = client.get_account_state(account, Some(version_height));
        assert!(result.is_err());
    }

    /// Generates and returns a (client, server) pair, where the client is a lightweight JSON client
    /// and the server is a JSON server that serves the JSON RPC requests. The server communicates
    /// with the given database to handle each JSON RPC request. If mock_validator is set to true,
    /// the server is also given a mock vm validator to validate any submitted transactions.
    fn create_client_and_server(db: MockLibraDB, mock_validator: bool) -> (JsonRpcClient, Runtime) {
        let address = "0.0.0.0";
        let port = utils::get_available_port();
        let host = format!("{}:{}", address, port);
        let (mp_sender, mut mp_events) = channel(1024);
        let server = test_bootstrap(host.parse().unwrap(), Arc::new(db), mp_sender);

        let url = format!("http://{}", host);
        let client = JsonRpcClient::new(url);

        if mock_validator {
            // Provide a VMValidator to the runtime.
            server.spawn(async move {
                while let Some((txn, cb)) = mp_events.next().await {
                    let vm_status = MockVMValidator.validate_transaction(txn).unwrap().status();
                    let result = if vm_status.is_some() {
                        (MempoolStatus::new(MempoolStatusCode::VmError), vm_status)
                    } else {
                        (MempoolStatus::new(MempoolStatusCode::Accepted), None)
                    };
                    cb.send(Ok(result)).unwrap();
                }
            });
        }

        (client, server)
    }

    /// Returns an empty mock database for testing.
    fn create_empty_mock_db() -> MockLibraDB {
        MockLibraDB::new(BTreeMap::new())
    }

    // This offers a simple mock of LibraDB for testing.
    #[derive(Clone)]
    pub struct MockLibraDB {
        account_states_with_proof: BTreeMap<AccountAddress, AccountStateWithProof>,
    }

    /// A mock libra database for test purposes.
    impl MockLibraDB {
        pub fn new(
            account_states_with_proof: BTreeMap<AccountAddress, AccountStateWithProof>,
        ) -> Self {
            Self {
                account_states_with_proof,
            }
        }
    }

    /// We only require implementing a subset of these API calls for testing purposes. To keep
    /// our code as minimal as possible, the unimplemented API calls simply return an error.
    impl DbReader for MockLibraDB {
        fn get_transactions(
            &self,
            _start_version: u64,
            _limit: u64,
            _ledger_version: u64,
            _fetch_events: bool,
        ) -> Result<TransactionListWithProof> {
            unimplemented!()
        }

        fn get_events(
            &self,
            _event_key: &EventKey,
            _start: u64,
            _order: Order,
            _limit: u64,
        ) -> Result<Vec<(u64, ContractEvent)>> {
            unimplemented!()
        }

        fn get_latest_account_state(
            &self,
            _address: AccountAddress,
        ) -> Result<Option<AccountStateBlob>> {
            unimplemented!()
        }

        fn get_latest_ledger_info(&self) -> Result<LedgerInfoWithSignatures> {
            Ok(LedgerInfoWithSignatures::new(
                LedgerInfo::new(
                    BlockInfo::new(0, 0, HashValue::zero(), HashValue::zero(), 0, 0, None),
                    HashValue::zero(),
                ),
                BTreeMap::new(),
            ))
        }

        fn get_startup_info(&self) -> Result<Option<StartupInfo>> {
            unimplemented!()
        }

        fn get_txn_by_account(
            &self,
            _address: AccountAddress,
            _seq_num: u64,
            _ledger_version: u64,
            _fetch_events: bool,
        ) -> Result<Option<TransactionWithProof>> {
            unimplemented!()
        }

        fn get_state_proof_with_ledger_info(
            &self,
            _known_version: u64,
            _ledger_info: LedgerInfoWithSignatures,
        ) -> Result<(EpochChangeProof, AccumulatorConsistencyProof)> {
            unimplemented!()
        }

        fn get_state_proof(
            &self,
            _known_version: u64,
        ) -> Result<(
            LedgerInfoWithSignatures,
            EpochChangeProof,
            AccumulatorConsistencyProof,
        )> {
            unimplemented!()
        }

        /// Return the associated AccountStateWithProof for the given account address. If the
        /// AccountStateWithProof doesn't exist, an error is returned.
        fn get_account_state_with_proof(
            &self,
            address: AccountAddress,
            _version: Version,
            _ledger_version: Version,
        ) -> Result<AccountStateWithProof> {
            if let Some(account_state_proof) = self.account_states_with_proof.get(&address) {
                Ok(account_state_proof.clone())
            } else {
                Err(NotFound("AccountStateWithProof".into()).into())
            }
        }

        fn get_account_state_with_proof_by_version(
            &self,
            _address: AccountAddress,
            _version: u64,
        ) -> Result<(Option<AccountStateBlob>, SparseMerkleProof)> {
            unimplemented!()
        }

        fn get_latest_state_root(&self) -> Result<(u64, HashValue)> {
            unimplemented!()
        }

        fn get_latest_tree_state(&self) -> Result<TreeState> {
            unimplemented!()
        }

        fn get_epoch_ending_ledger_infos(&self, _: u64, _: u64) -> Result<EpochChangeProof> {
            unimplemented!()
        }

        fn get_epoch_ending_ledger_info(&self, _: u64) -> Result<LedgerInfoWithSignatures> {
            unimplemented!()
        }

        fn get_block_timestamp(&self, _: u64) -> Result<u64> {
            unimplemented!()
        }
    }
}

#[cfg(any(test, feature = "fuzzing"))]
/// This module provides all the functionality necessary to test the secure JSON RPC client using
/// fuzzing.
pub mod fuzzing {
    use crate::{
        AccountStateResponse, AccountStateWithProofResponse, Bytes, SubmitTransactionResponse,
        TransactionView, TransactionViewResponse, VMStatusView,
    };
    use libra_proptest_helpers::Index;
    use libra_types::proptest_types::{arb_json_value, AccountInfoUniverse, AccountStateBlobGen};
    use proptest::prelude::*;
    use ureq::Response;

    // Note: these tests ensure that the various fuzzers are maintained (i.e., not broken
    // at some time in the future and only discovered when a fuzz test fails).
    proptest! {
        #![proptest_config(ProptestConfig::with_cases(10))]

        #[test]
        fn test_get_account_state_proptest(input in arb_account_state_response()) {
            let _ = crate::process_account_state_response(input);
        }

        #[test]
        fn test_submit_transaction_proptest(input in arb_submit_transaction_response()) {
            let _ = crate::process_submit_transaction_response(input);
        }

        #[test]
        fn test_transaction_status_proptest(input in arb_transaction_status_response()) {
            let _ = crate::process_transaction_status_response(input);
        }
    }

    // This generates an arbitrary response for the get_account_state_with_proof() JSON RPC API
    // call.
    prop_compose! {
        pub fn arb_account_state_response(
        )(
            status in any::<u16>(),
            status_text in any::<String>(),
            id in any::<u64>(),
            jsonrpc in any::<String>(),
            version_height in any::<u64>(),
            universe in any_with::<AccountInfoUniverse>(1).no_shrink(),
            index in any::<Index>(),
            account_state_blob_gen in any::<AccountStateBlobGen>(),
        ) -> Response {
            let account_state_blob = account_state_blob_gen.materialize(index, &universe);
            let encoded_blob = Bytes::from(&lcs::to_bytes(&account_state_blob).unwrap());

            let response_body = AccountStateWithProofResponse {
                id,
                jsonrpc,
                result: AccountStateResponse {
                    version: version_height,
                    blob: Some(encoded_blob),
                },
            };
            let response_body =
                serde_json::to_string::<AccountStateWithProofResponse>(&response_body).unwrap();
            Response::new(status, &status_text, &response_body)
        }
    }

    // This generates an arbitrary response for the submit_transaction() JSON RPC API call.
    prop_compose! {
        pub fn arb_submit_transaction_response(
        )(
            status in any::<u16>(),
            status_text in any::<String>(),
            id in any::<u64>(),
            jsonrpc in any::<String>(),
            result in arb_json_value(),
        ) -> Response {
            let response_body = SubmitTransactionResponse {
                id,
                jsonrpc,
                result: Some(result),
            };
            let response_body =
                serde_json::to_string::<SubmitTransactionResponse>(&response_body).unwrap();
            Response::new(status, &status_text, &response_body)
        }
    }

    // This generates an arbitrary response for the get_account_transaction() JSON RPC API call.
    prop_compose! {
        pub fn arb_transaction_status_response(
        )(
            status in any::<u16>(),
            status_text in any::<String>(),
            id in any::<u64>(),
            jsonrpc in any::<String>(),
            vm_status in arb_vm_status_view(),
        ) -> Response {
            let transaction_view = TransactionView {
                vm_status
            };
            let response_body = TransactionViewResponse {
                id,
                jsonrpc,
                result: Some(transaction_view),
            };
            let response_body =
                serde_json::to_string::<TransactionViewResponse>(&response_body).unwrap();
            Response::new(status, &status_text, &response_body)
        }
    }

    // This function generates an arbitrary VMStatusView.
    fn arb_vm_status_view() -> impl Strategy<Value = VMStatusView> {
        prop_oneof![
            Just(VMStatusView::Executed),
            Just(VMStatusView::OutOfGas),
            (any::<String>(), any::<u64>()).prop_map(|(location, abort_code)| {
                VMStatusView::MoveAbort {
                    location,
                    abort_code,
                }
            }),
            (any::<String>(), any::<u16>(), any::<u16>()).prop_map(
                |(location, function_index, code_offset)| {
                    VMStatusView::ExecutionFailure {
                        location,
                        function_index,
                        code_offset,
                    }
                }
            ),
            Just(VMStatusView::VerificationError),
            Just(VMStatusView::DeserializationError),
            Just(VMStatusView::PublishingFailure),
        ]
    }
}

#[cfg(test)]
mod test_helpers {
    use libra_crypto::{ed25519::Ed25519PrivateKey, HashValue, PrivateKey, Uniform};
    use libra_types::{
        account_address::AccountAddress,
        account_config::{AccountResource, BalanceResource},
        account_state::AccountState,
        account_state_blob::{AccountStateBlob, AccountStateWithProof},
        event::EventHandle,
        proof::{AccountStateProof, AccumulatorProof, SparseMerkleProof, TransactionInfoWithProof},
        test_helpers::transaction_test_helpers::get_test_signed_txn,
        transaction::{SignedTransaction, TransactionInfo},
        vm_status::KeptVMStatus,
    };
    use std::convert::TryFrom;

    /// Generates an AccountStateWithProof using the given AccountState and version height for
    /// testing.
    pub(crate) fn create_test_state_with_proof(
        account_state: &AccountState,
        version_height: u64,
    ) -> AccountStateWithProof {
        AccountStateWithProof::new(
            version_height,
            Some(AccountStateBlob::try_from(account_state).unwrap()),
            create_test_state_proof(),
        )
    }

    /// Generates an AccountState for testing.
    pub(crate) fn create_test_account_state() -> AccountState {
        let account_resource = create_test_account_resource();
        let balance_resource = create_test_balance_resource();
        AccountState::try_from((&account_resource, &balance_resource)).unwrap()
    }

    /// Generates an AccountStateProof for testing.
    pub(crate) fn create_test_state_proof() -> AccountStateProof {
        let transaction_info = TransactionInfo::new(
            HashValue::zero(),
            HashValue::zero(),
            HashValue::zero(),
            0,
            KeptVMStatus::MiscellaneousError,
        );

        AccountStateProof::new(
            TransactionInfoWithProof::new(AccumulatorProof::new(vec![]), transaction_info),
            SparseMerkleProof::new(None, vec![]),
        )
    }

    /// Generates and returns a (randomized) SignedTransaction for testing.
    pub(crate) fn generate_signed_transaction() -> SignedTransaction {
        let sender = AccountAddress::random();
        let private_key = Ed25519PrivateKey::generate_for_testing();
        get_test_signed_txn(sender, 0, &private_key, private_key.public_key(), None)
    }

    /// Generates an AccountResource for testing.
    fn create_test_account_resource() -> AccountResource {
        AccountResource::new(
            10,
            vec![],
            None,
            None,
            EventHandle::random_handle(100),
            EventHandle::random_handle(100),
        )
    }

    /// Generates a BalanceResource for testing.
    fn create_test_balance_resource() -> BalanceResource {
        BalanceResource::new(100)
    }
}