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
use std::collections::HashMap;
use std::mem;
use std::sync::Arc;
use call_contract::CallContract;
use tetsy_registrar::RegistrarClient;
use types::ids::BlockId;
use types::transaction::SignedTransaction;
use vapabi::FunctionOutputDecoder;
use vapory_types::Address;
use parking_lot::RwLock;
use_contract!(service_transaction, "res/contracts/service_transaction.json");
const SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME: &'static str = "service_transaction_checker";
#[derive(Default, Clone)]
pub struct ServiceTransactionChecker {
certified_addresses_cache: Arc<RwLock<HashMap<Address, bool>>>
}
impl ServiceTransactionChecker {
pub fn check<C: CallContract + RegistrarClient>(
&self,
client: &C,
tx: &SignedTransaction
) -> Result<bool, String> {
if !tx.gas_price.is_zero() {
return Ok(false)
}
let sender = tx.sender();
self.check_address(client, sender)
}
pub fn check_address<C>(&self, client: &C, sender: Address) -> Result<bool, String>
where C: CallContract + RegistrarClient
{
trace!(target: "txqueue", "Checking service transaction checker contract from {}", sender);
if let Some(allowed) = self
.certified_addresses_cache
.try_read()
.as_ref()
.and_then(|c| c.get(&sender))
{
return Ok(*allowed);
}
let contract_address = match client.get_address(
SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME,
BlockId::Latest
) {
Ok(Some(addr)) => addr,
Ok(None) => return Err("contract is not configured".to_owned()),
Err(e) => return Err(e)
};
self.call_contract(client, contract_address, sender).and_then(|allowed| {
if let Some(mut cache) = self.certified_addresses_cache.try_write() {
cache.insert(sender, allowed);
};
Ok(allowed)
})
}
pub fn refresh_cache<C>(&self, client: &C) -> Result<bool, String>
where C: CallContract + RegistrarClient
{
trace!(target: "txqueue", "Refreshing certified addresses cache");
let cache = mem::replace(&mut *self.certified_addresses_cache.write(), HashMap::default());
if client.registrar_address().is_none() {
return Ok(false);
}
let contract_address_fetch = client.get_address(
SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME,
BlockId::Latest
)?;
if let Some(contract_address) = contract_address_fetch {
let addresses: Vec<_> = cache.keys().collect();
let mut cache: HashMap<Address, bool> = HashMap::default();
for address in addresses {
let allowed = self.call_contract(client, contract_address, *address)?;
cache.insert(*address, allowed);
}
mem::replace(&mut *self.certified_addresses_cache.write(), cache);
Ok(true)
} else {
Ok(false)
}
}
fn call_contract<C>(
&self,
client: &C,
contract_address: Address,
sender: Address
) -> Result<bool, String>
where C: CallContract + RegistrarClient
{
let (data, decoder) = service_transaction::functions::certified::call(sender);
let value = client.call_contract(BlockId::Latest, contract_address, data)?;
decoder.decode(&value).map_err(|e| e.to_string())
}
}