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
use std::sync::{Arc, RwLock};

use sp_core::{sr25519, Pair};
use sp_runtime::{AccountId32, MultiSignature};

use dashmap::DashMap;

use rhai::{Dynamic, Engine, EvalAltResult, INT};

use crate::client::{Client, ExtrinsicCallResult};
use crate::metadata::EncodedCall;

pub type AccountId = AccountId32;

#[derive(Clone)]
pub struct User {
  pub pair: sr25519::Pair,
  pub nonce: u32,
  pub name: String,
  account: AccountId,
  client: Client,
}

impl User {
  fn new(client: Client, name: &str) -> Result<Self, Box<EvalAltResult>> {
    log::info!("New user: {}", name);
    let seed = format!("//{}", name);
    let pair = sr25519::Pair::from_string(&seed, None).map_err(|e| format!("{:?}", e))?;
    let account = AccountId::new(pair.public().into());
    Ok(Self {
      name: name.into(),
      pair,
      account,
      nonce: 0u32,
      client,
    })
  }

  pub fn public(&self) -> sr25519::Public {
    self.pair.public()
  }

  pub fn acc(&self) -> AccountId {
    self.account.clone()
  }

  fn nonce(&self) -> INT {
    self.nonce as INT
  }

  pub fn sign_data(&self, data: Vec<u8>) -> MultiSignature {
    MultiSignature::Sr25519(self.pair.sign(&data[..]))
  }

  pub fn submit_call(
    &mut self,
    call: EncodedCall,
  ) -> Result<ExtrinsicCallResult, Box<EvalAltResult>> {
    // Check if we need to load the `nonce` for this user.
    if self.nonce == 0u32 {
      self.nonce = self.client.get_nonce(self.acc())?.unwrap_or(0);
    }
    let res = self.client.submit_call(self, call)?;

    // Only update the nonce if the extrinsic executed.
    self.nonce += 1;

    Ok(res)
  }

  fn to_string(&self) -> String {
    self.name.clone()
  }
}

#[derive(Clone)]
pub struct SharedUser(Arc<RwLock<User>>);

impl SharedUser {
  pub fn public(&self) -> sr25519::Public {
    self.0.read().unwrap().public()
  }

  pub fn acc(&mut self) -> AccountId {
    self.0.read().unwrap().acc()
  }

  fn nonce(&mut self) -> INT {
    self.0.read().unwrap().nonce()
  }

  pub fn sign_data(&mut self, data: Vec<u8>) -> MultiSignature {
    self.0.read().unwrap().sign_data(data)
  }

  pub fn submit_call(
    &mut self,
    call: EncodedCall,
  ) -> Result<ExtrinsicCallResult, Box<EvalAltResult>> {
    self.0.write().unwrap().submit_call(call)
  }

  fn to_string(&mut self) -> String {
    self.0.read().unwrap().to_string()
  }
}

pub struct InnerUsers {
  users: DashMap<String, Dynamic>,
  account_map: DashMap<AccountId, Dynamic>,
  client: Client,
}

impl InnerUsers {
  pub fn new(client: Client) -> Self {
    Self {
      users: DashMap::new(),
      account_map: DashMap::new(),
      client,
    }
  }

  pub fn find_by_account(&self, acc: AccountId) -> Dynamic {
    self.account_map.get(&acc).as_deref().cloned().unwrap_or(Dynamic::UNIT)
  }

  fn get_user(&self, name: String) -> Result<Dynamic, Box<EvalAltResult>> {
    // Try save user.  If another thread generated the user first, then use that user.
    use dashmap::mapref::entry::Entry;
    Ok(match self.users.entry(name) {
      Entry::Occupied(entry) => entry.get().clone(),
      Entry::Vacant(entry) => {
        // Generate new user.
        let user = User::new(self.client.clone(), entry.key())?;
        let acc = user.acc();
        // Create a shared wrapper for the user.
        let shared = Dynamic::from(SharedUser(Arc::new(RwLock::new(user))));

        self.account_map.insert(acc, shared.clone());
        entry.insert(shared.clone());
        shared
      }
    })
  }
}

#[derive(Clone)]
pub struct Users(Arc<InnerUsers>);

impl Users {
  pub fn new(client: Client) -> Self {
    Self(Arc::new(InnerUsers::new(client)))
  }

  pub fn find_by_account(&mut self, acc: AccountId) -> Dynamic {
    self.0.find_by_account(acc)
  }

  fn get_user(&mut self, name: String) -> Result<Dynamic, Box<EvalAltResult>> {
    self.0.get_user(name)
  }
}

pub fn init_engine(engine: &mut Engine, client: &Client) -> Users {
  engine
    .register_type_with_name::<SharedUser>("User")
    .register_get("acc", SharedUser::acc)
    .register_get("nonce", SharedUser::nonce)
    .register_fn("to_string", SharedUser::to_string)
    .register_fn("sign", SharedUser::sign_data)
    .register_result_fn("submit", SharedUser::submit_call)
    .register_type_with_name::<AccountId>("AccountId")
    .register_fn("to_string", |acc: &mut AccountId| acc.to_string())
    .register_fn("==", |acc1: AccountId, acc2: AccountId| acc1 == acc2)
    .register_type_with_name::<Users>("Users")
    .register_fn("new_users", Users::new)
    .register_fn("find_by_account", Users::find_by_account)
    .register_indexer_get_result(Users::get_user);
  Users::new(client.clone())
}