2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-01-22 16:15:22 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-01-22 16:15:22 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-01-22 16:15:22 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-22 16:15:22 +01:00
|
|
|
|
2018-01-11 17:49:10 +01:00
|
|
|
//! A service transactions contract checker.
|
2017-04-03 09:40:18 +02:00
|
|
|
|
2019-03-31 10:39:38 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::mem;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use call_contract::{RegistryInfo, CallContract};
|
2019-01-16 19:52:21 +01:00
|
|
|
use types::ids::BlockId;
|
2019-01-04 14:05:46 +01:00
|
|
|
use types::transaction::SignedTransaction;
|
2018-09-13 11:04:39 +02:00
|
|
|
use ethabi::FunctionOutputDecoder;
|
2019-01-28 10:58:34 +01:00
|
|
|
use ethereum_types::Address;
|
2019-03-31 10:39:38 +02:00
|
|
|
use parking_lot::RwLock;
|
2017-01-22 16:15:22 +01:00
|
|
|
|
2018-09-13 11:04:39 +02:00
|
|
|
use_contract!(service_transaction, "res/contracts/service_transaction.json");
|
2018-02-09 09:32:06 +01:00
|
|
|
|
2017-01-22 16:15:22 +01:00
|
|
|
const SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME: &'static str = "service_transaction_checker";
|
|
|
|
|
|
|
|
/// Service transactions checker.
|
2018-04-13 17:34:27 +02:00
|
|
|
#[derive(Default, Clone)]
|
2019-03-31 10:39:38 +02:00
|
|
|
pub struct ServiceTransactionChecker {
|
|
|
|
certified_addresses_cache: Arc<RwLock<HashMap<Address, bool>>>
|
|
|
|
}
|
2017-01-22 16:15:22 +01:00
|
|
|
|
|
|
|
impl ServiceTransactionChecker {
|
2019-03-31 10:39:38 +02:00
|
|
|
|
2019-01-28 10:58:34 +01:00
|
|
|
/// Checks if given address in tx is whitelisted to send service transactions.
|
2018-03-03 18:42:13 +01:00
|
|
|
pub fn check<C: CallContract + RegistryInfo>(&self, client: &C, tx: &SignedTransaction) -> Result<bool, String> {
|
2018-04-13 17:34:27 +02:00
|
|
|
let sender = tx.sender();
|
|
|
|
// Skip checking the contract if the transaction does not have zero gas price
|
|
|
|
if !tx.gas_price.is_zero() {
|
|
|
|
return Ok(false)
|
|
|
|
}
|
2018-02-09 09:32:06 +01:00
|
|
|
|
2019-01-28 10:58:34 +01:00
|
|
|
self.check_address(client, sender)
|
|
|
|
}
|
2017-01-22 16:15:22 +01:00
|
|
|
|
2019-01-28 10:58:34 +01:00
|
|
|
/// Checks if given address is whitelisted to send service transactions.
|
|
|
|
pub fn check_address<C: CallContract + RegistryInfo>(&self, client: &C, sender: Address) -> Result<bool, String> {
|
2019-03-31 10:39:38 +02:00
|
|
|
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);
|
|
|
|
}
|
2019-01-28 10:58:34 +01:00
|
|
|
let contract_address = client.registry_address(SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME.to_owned(), BlockId::Latest)
|
|
|
|
.ok_or_else(|| "contract is not configured")?;
|
2019-03-31 10:39:38 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Refresh certified addresses cache
|
|
|
|
pub fn refresh_cache<C: CallContract + RegistryInfo>(&self, client: &C) -> Result<bool, String> {
|
|
|
|
trace!(target: "txqueue", "Refreshing certified addresses cache");
|
|
|
|
// replace the cache with an empty list,
|
|
|
|
// since it's not recent it won't be used anyway.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
mem::replace(&mut *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> {
|
2018-09-13 11:04:39 +02:00
|
|
|
let (data, decoder) = service_transaction::functions::certified::call(sender);
|
2019-01-28 10:58:34 +01:00
|
|
|
let value = client.call_contract(BlockId::Latest, contract_address, data)?;
|
2018-09-13 11:04:39 +02:00
|
|
|
decoder.decode(&value).map_err(|e| e.to_string())
|
2017-01-22 16:15:22 +01:00
|
|
|
}
|
|
|
|
}
|