Coverage Report

Created: 2022-11-10 19:56

/home/runner/work/creditcoin/creditcoin/node/rpc/src/task.rs
Line
Count
Source (jump to first uncovered line)
1
use super::Error;
2
use core::marker::PhantomData;
3
use core::str::FromStr;
4
use creditcoin_node_runtime as runtime;
5
use creditcoin_runtime_api::TaskApi;
6
use jsonrpc_core::Result as RpcResult;
7
use jsonrpc_core::{Error as RpcError, ErrorCode};
8
use jsonrpc_derive::rpc;
9
use sc_rpc::DenyUnsafe;
10
use sp_blockchain::HeaderBackend;
11
use sp_runtime::{generic::BlockId, traits};
12
use std::sync::Arc;
13
14
type AccountId = <runtime::Runtime as frame_system::Config>::AccountId;
15
16
0
#[rpc]
17
pub trait TaskRpc<AccountId> {
18
  #[rpc(name = "task_getOffchainNonceKey")]
19
  fn offchain_nonce_key(&self, account_id: String) -> RpcResult<Vec<u8>>;
20
}
21
22
pub struct Task<C, B> {
23
  client: Arc<C>,
24
  deny_unsafe: DenyUnsafe,
25
  _p: PhantomData<B>,
26
}
27
28
impl<C, B> Task<C, B> {
29
2
  pub fn new(client: Arc<C>, deny_unsafe: DenyUnsafe) -> Self {
30
2
    Self { deny_unsafe, client, _p: Default::default() }
31
2
  }
32
}
33
34
impl<C, B> TaskRpc<AccountId> for Task<C, B>
35
where
36
  C: sp_api::ProvideRuntimeApi<B>,
37
  C: HeaderBackend<B>,
38
  C: Send + Sync + 'static,
39
  C::Api: TaskApi<B, AccountId>,
40
  B: traits::Block,
41
{
42
  fn offchain_nonce_key(&self, account_id: String) -> RpcResult<Vec<u8>> {
43
3
    self.deny_unsafe.check_if_safe()
?0
;
44
3
    let api = self.client.runtime_api();
45
3
    let at = {
46
3
      let best = self.client.info().best_hash;
47
3
      BlockId::hash(best)
48
    };
49
50
3
    let 
account_id2
= AccountId::from_str(&account_id).map_err(|e| RpcError {
51
1
      code: ErrorCode::InvalidParams,
52
1
      message: "Not a valid hex-string or SS58 address".into(),
53
1
      data: Some(format!("{:?}", e).into()),
54
3
    })
?1
;
55
56
2
    api.offchain_nonce_key(&at, &account_id).map_err(|e| RpcError {
57
0
      code: ErrorCode::ServerError(Error::RuntimeError.into()),
58
0
      message: "Unable to query offchain nonce key.".into(),
59
0
      data: Some(format!("{:?}", e).into()),
60
2
    })
61
3
  }
62
}
63
64
#[cfg(test)]
65
pub mod test {
66
  use super::*;
67
  use creditcoin_node_runtime::Block;
68
69
1
  #[test]
70
1
  fn offchain_nonce_key_works() {
71
1
    let client = Arc::new(test_client::new());
72
1
    let t = Task::<_, Block>::new(client, DenyUnsafe::No);
73
1
    //$ ./node key inspect //Alice
74
1
    t.offchain_nonce_key("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".into())
75
1
      .unwrap();
76
1
    t.offchain_nonce_key(
77
1
      "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d".into(),
78
1
    )
79
1
    .unwrap();
80
1
  }
81
82
1
  #[test]
83
1
  fn offchain_nonce_key_should_error_when_input_is_not_a_valid_hex_string() {
84
1
    let client = Arc::new(test_client::new());
85
1
    let t = Task::<_, Block>::new(client, DenyUnsafe::No);
86
1
87
1
    match t.offchain_nonce_key("0xThisIsNotValid".into()) {
88
1
      Err(e) => {
89
1
        assert_eq!(e.to_string(), "Invalid params: Not a valid hex-string or SS58 address");
90
      },
91
0
      Ok(_) => panic!("This is not expected"),
92
    }
93
1
  }
94
}