2017-01-22 16:15:22 +01:00
|
|
|
// Copyright 2017 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-01-11 17:49:10 +01:00
|
|
|
//! A service transactions contract checker.
|
2017-04-03 09:40:18 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
use client::{RegistryInfo, CallContract, BlockId};
|
2018-01-11 17:49:10 +01:00
|
|
|
use transaction::SignedTransaction;
|
2017-01-22 16:15:22 +01:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
use_contract!(service_transaction, "ServiceTransaction", "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)]
|
2017-01-22 16:15:22 +01:00
|
|
|
pub struct ServiceTransactionChecker {
|
2018-02-09 09:32:06 +01:00
|
|
|
contract: service_transaction::ServiceTransaction,
|
2017-01-22 16:15:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ServiceTransactionChecker {
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Checks if given address 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();
|
|
|
|
let hash = tx.hash();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
let address = client.registry_address(SERVICE_TRANSACTION_CONTRACT_REGISTRY_NAME.to_owned(), BlockId::Latest)
|
2018-02-09 09:32:06 +01:00
|
|
|
.ok_or_else(|| "contract is not configured")?;
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
trace!(target: "txqueue", "[{:?}] Checking service transaction checker contract from {}", hash, sender);
|
2017-01-22 16:15:22 +01:00
|
|
|
|
2018-02-09 09:32:06 +01:00
|
|
|
self.contract.functions()
|
|
|
|
.certified()
|
2018-04-13 17:34:27 +02:00
|
|
|
.call(sender, &|data| client.call_contract(BlockId::Latest, address, data))
|
2018-02-09 09:32:06 +01:00
|
|
|
.map_err(|e| e.to_string())
|
2017-01-22 16:15:22 +01:00
|
|
|
}
|
|
|
|
}
|