Deduplicate registrar contract & calling logic (#11110)

* Rename RegistryInfo -> RegistryInfoDeprecated

* Add BlockId parameter to Registrar::get_address and RegistrarClient::call_contract

* Remove RegistrarClient::Call (use async for now); add RegistrarClient::get_address

* Remove Registrar type in favour of naked trait

* Use CallContract trait bound instead of separate call_contract method

* Make RegistrarClient::get_address and URLHint::resolve synchronous

* RegistrarClient::get_address: return check if address is zero

* Modify RegistryInfo::registry_address to take &str

* return Result from RegistryInfo::registry_address

* Replace RegistryInfo with RegistrarClient

- Modifed RegistrarClient::registrar_address to return Option
- Removed BlockChainClient::registrar_address

* Fix other build configs

* Fix unit test builds

* Remove local RegistrarClient type from run::execute_impl

* Remove registrar.json from ethcore

* Formatting/line breaks

* Update RegistrarClient docs, remove explicit lifetime

* Weak ref to ethcore client from hash fetch client

* Fix unit tests
This commit is contained in:
Toby Dimmick
2019-10-03 14:15:25 +01:00
committed by David
parent 0c0f965354
commit 79a17dedd0
34 changed files with 266 additions and 227 deletions

View File

@@ -51,7 +51,7 @@ use blockchain::{
TransactionAddress,
TreeRoute
};
use call_contract::{CallContract, RegistryInfo};
use call_contract::CallContract;
use client::{
bad_blocks, BlockProducer, BroadcastProposalBlock, Call,
ClientConfig, EngineInfo, ImportSealedBlock, PrepareOpenBlock,
@@ -95,6 +95,7 @@ use machine::{
transaction_ext::Transaction,
};
use miner::{Miner, MinerService, PendingOrdering};
use registrar::RegistrarClient;
use snapshot::{self, SnapshotClient, SnapshotWriter};
use spec::Spec;
use state_db::StateDB;
@@ -138,8 +139,6 @@ use verification;
use verification::queue::kind::BlockLike;
use vm::{CreateContractAddress, EnvInfo, LastHashes};
use_contract!(registry, "res/contracts/registrar.json");
const MAX_ANCIENT_BLOCKS_QUEUE_SIZE: usize = 4096;
// Max number of blocks imported at once.
const MAX_ANCIENT_BLOCKS_TO_IMPORT: usize = 4;
@@ -1415,22 +1414,6 @@ impl TransactionInfo for Client {
impl BlockChainTrait for Client {}
impl RegistryInfo for Client {
fn registry_address(&self, name: String, block: BlockId) -> Option<Address> {
use ethabi::FunctionOutputDecoder;
let address = self.registrar_address?;
let (data, decoder) = registry::functions::get_address::call(keccak(name.as_bytes()), "A");
let value = decoder.decode(&self.call_contract(block, address, data).ok()?).ok()?;
if value.is_zero() {
None
} else {
Some(value)
}
}
}
impl CallContract for Client {
fn call_contract(&self, block_id: BlockId, address: Address, data: Bytes) -> Result<Bytes, String> {
let state_pruned = || CallError::StatePruned.to_string();
@@ -1445,6 +1428,12 @@ impl CallContract for Client {
}
}
impl RegistrarClient for Client {
fn registrar_address(&self) -> Option<Address> {
self.registrar_address
}
}
impl ImportBlock for Client {
fn import_block(&self, unverified: Unverified) -> EthcoreResult<H256> {
if self.chain.read().is_known(&unverified.hash()) {
@@ -2178,10 +2167,6 @@ impl BlockChainClient for Client {
let signed = SignedTransaction::new(transaction.with_signature(signature, chain_id))?;
self.importer.miner.import_own_transaction(self, signed.into())
}
fn registrar_address(&self) -> Option<Address> {
self.registrar_address.clone()
}
}
impl IoClient for Client {

View File

@@ -23,7 +23,6 @@ extern crate ansi_term;
extern crate client_traits;
extern crate common_types as types;
extern crate engine;
extern crate ethabi;
extern crate ethcore_blockchain as blockchain;
extern crate ethcore_call_contract as call_contract;
extern crate ethcore_db as db;
@@ -45,6 +44,7 @@ extern crate trie_db as trie;
extern crate patricia_trie_ethereum as ethtrie;
extern crate rand;
extern crate rayon;
extern crate registrar;
extern crate rlp;
extern crate rustc_hex;
extern crate serde;
@@ -97,8 +97,6 @@ extern crate serde_json;
#[cfg(any(test, feature = "tempdir"))]
extern crate tempdir;
#[macro_use]
extern crate ethabi_contract;
#[macro_use]
extern crate log;
#[macro_use]

View File

@@ -46,7 +46,8 @@ use types::{
};
use block::SealedBlock;
use call_contract::{CallContract, RegistryInfo};
use call_contract::CallContract;
use registrar::RegistrarClient;
use client::{BlockProducer, SealedBlockImporter};
use client_traits::{BlockChain, ChainInfo, AccountData, Nonce, ScheduleInfo};
use account_state::state::StateInfo;
@@ -54,7 +55,7 @@ use account_state::state::StateInfo;
/// Provides methods to verify incoming external transactions
pub trait TransactionVerifierClient: Send + Sync
// Required for ServiceTransactionChecker
+ CallContract + RegistryInfo
+ CallContract + RegistrarClient
// Required for verifiying transactions
+ BlockChain + ScheduleInfo + AccountData
{}

View File

@@ -63,7 +63,8 @@ use types::{
use vm::{Schedule, LastHashes};
use block::{OpenBlock, SealedBlock, ClosedBlock};
use call_contract::{CallContract, RegistryInfo};
use call_contract::CallContract;
use registrar::RegistrarClient;
use client::{
ReopenBlock, PrepareOpenBlock, ImportSealedBlock, BroadcastProposalBlock, Call,
EngineInfo, BlockProducer, SealedBlockImporter,
@@ -532,7 +533,20 @@ impl BlockInfo for TestBlockChainClient {
}
impl CallContract for TestBlockChainClient {
fn call_contract(&self, _id: BlockId, _address: Address, _data: Bytes) -> Result<Bytes, String> { Ok(vec![]) }
fn call_contract(
&self,
_block_id: BlockId,
_address: Address,
_data: Bytes
) -> Result<Bytes, String> {
Ok(vec![])
}
}
impl RegistrarClient for TestBlockChainClient {
fn registrar_address(&self) -> Option<Address> {
None
}
}
impl TransactionInfo for TestBlockChainClient {
@@ -543,10 +557,6 @@ impl TransactionInfo for TestBlockChainClient {
impl BlockChain for TestBlockChainClient {}
impl RegistryInfo for TestBlockChainClient {
fn registry_address(&self, _name: String, _block: BlockId) -> Option<Address> { None }
}
impl ImportBlock for TestBlockChainClient {
fn import_block(&self, unverified: Unverified) -> EthcoreResult<H256> {
let header = unverified.header;
@@ -916,8 +926,6 @@ impl BlockChainClient for TestBlockChainClient {
let signed = SignedTransaction::new(transaction.with_signature(sig, chain_id)).unwrap();
self.miner.import_own_transaction(self, signed.into())
}
fn registrar_address(&self) -> Option<Address> { None }
}
impl IoClient for TestBlockChainClient {

View File

@@ -48,6 +48,7 @@ use test_helpers::{
generate_dummy_client_with_data, get_good_dummy_block, get_bad_state_dummy_block
};
use rustc_hex::ToHex;
use registrar::RegistrarClient;
#[test]
fn imports_from_empty() {