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
use call_contract::{CallContract, RegistryInfo};
use ethabi::FunctionOutputDecoder;
use ethereum_types::Address;
use parking_lot::RwLock;
use std::{collections::HashMap, mem, sync::Arc};
use types::{ids::BlockId, transaction::SignedTransaction};
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 + RegistryInfo>(
&self,
client: &C,
tx: &SignedTransaction,
) -> Result<bool, String> {
let sender = tx.sender();
if !tx.has_zero_gas_price() {
return Ok(false);
}
self.check_address(client, sender)
}
pub fn check_address<C: CallContract + RegistryInfo>(
&self,
client: &C,
sender: Address,
) -> Result<bool, String> {
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 = client
.registry_address(
SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME.to_owned(),
BlockId::Latest,
)
.ok_or_else(|| "Certifier contract is not configured")?;
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: CallContract + RegistryInfo>(
&self,
client: &C,
) -> Result<bool, String> {
trace!(target: "txqueue", "Refreshing certified addresses cache");
let cache = mem::replace(
&mut *self.certified_addresses_cache.write(),
HashMap::default(),
);
if let Some(contract_address) = client.registry_address(
SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME.to_owned(),
BlockId::Latest,
) {
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);
}
*self.certified_addresses_cache.write() = cache;
Ok(true)
} else {
Ok(false)
}
}
fn call_contract<C: CallContract + RegistryInfo>(
&self,
client: &C,
contract_address: Address,
sender: Address,
) -> Result<bool, String> {
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())
}
}