Basic Authority (#991)

* Firt commit.

* First non-functional but correct implementation of BasicAuthority.

Still needs:
- Sealing infrastructure.

* Punch a hole to give miner access to key store.

* Fix test built.

* Basic version of synchronous mining.

This will seal a block whenever a new transaction comes through.
To be made better we need a timer which will wait for one second after the
last block before sealing a new one - better still would be to cooperatively
interleave blocks with other sealing nodes.

* Add tests.

* Fix minor issues from repotting.

* Address grumbles.
This commit is contained in:
Gav Wood
2016-05-03 17:23:53 +02:00
parent 1583f7d434
commit ac73b2628a
34 changed files with 506 additions and 88 deletions

View File

@@ -20,13 +20,14 @@ use std::sync::{Arc, RwLock};
use jsonrpc_core::IoHandler;
use util::hash::{Address, H256, FixedHash};
use util::numbers::{Uint, U256};
use util::keys::{TestAccount, TestAccountProvider};
use ethcore::client::{TestBlockChainClient, EachBlockWith, Executed, TransactionId};
use ethcore::log_entry::{LocalizedLogEntry, LogEntry};
use ethcore::receipt::LocalizedReceipt;
use ethcore::transaction::{Transaction, Action};
use ethminer::ExternalMiner;
use v1::{Eth, EthClient};
use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config, TestMinerService};
use v1::tests::helpers::{TestSyncProvider, Config, TestMinerService};
fn blockchain_client() -> Arc<TestBlockChainClient> {
let client = TestBlockChainClient::new();

View File

@@ -1,107 +0,0 @@
// Copyright 2015, 2016 Ethcore (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/>.
//! Test implementation of account provider.
use std::sync::RwLock;
use std::collections::HashMap;
use std::io;
use util::hash::{Address, H256, FixedHash};
use util::crypto::{Secret, Signature, KeyPair};
use util::keys::store::{AccountProvider, SigningError, EncryptedHashMapError};
/// Account mock.
#[derive(Clone)]
pub struct TestAccount {
/// True if account is unlocked.
pub unlocked: bool,
/// Account's password.
pub password: String,
/// Account's secret.
pub secret: Secret,
}
impl TestAccount {
/// Creates new test account.
pub fn new(password: &str) -> Self {
let pair = KeyPair::create().unwrap();
TestAccount {
unlocked: false,
password: password.to_owned(),
secret: pair.secret().clone()
}
}
/// Returns account address.
pub fn address(&self) -> Address {
KeyPair::from_secret(self.secret.clone()).unwrap().address()
}
}
/// Test account provider.
pub struct TestAccountProvider {
/// Test provider accounts.
pub accounts: RwLock<HashMap<Address, TestAccount>>,
}
impl TestAccountProvider {
/// Basic constructor.
pub fn new(accounts: HashMap<Address, TestAccount>) -> Self {
TestAccountProvider {
accounts: RwLock::new(accounts),
}
}
}
impl AccountProvider for TestAccountProvider {
fn accounts(&self) -> Result<Vec<Address>, io::Error> {
Ok(self.accounts.read().unwrap().keys().cloned().collect())
}
fn unlock_account(&self, account: &Address, pass: &str) -> Result<(), EncryptedHashMapError> {
match self.accounts.write().unwrap().get_mut(account) {
Some(ref mut acc) if acc.password == pass => {
acc.unlocked = true;
Ok(())
},
Some(_) => Err(EncryptedHashMapError::InvalidPassword),
None => Err(EncryptedHashMapError::UnknownIdentifier),
}
}
fn new_account(&self, pass: &str) -> Result<Address, io::Error> {
let account = TestAccount::new(pass);
let address = KeyPair::from_secret(account.secret.clone()).unwrap().address();
self.accounts.write().unwrap().insert(address.clone(), account);
Ok(address)
}
fn account_secret(&self, address: &Address) -> Result<Secret, SigningError> {
// todo: consider checking if account is unlock. some test may need alteration then.
self.accounts
.read()
.unwrap()
.get(address)
.ok_or(SigningError::NoAccount)
.map(|acc| acc.secret.clone())
}
fn sign(&self, _account: &Address, _message: &H256) -> Result<Signature, SigningError> {
unimplemented!()
}
}

View File

@@ -16,10 +16,8 @@
//! Test rpc services.
mod account_provider;
mod sync_provider;
mod miner_service;
pub use self::account_provider::{TestAccount, TestAccountProvider};
pub use self::sync_provider::{Config, TestSyncProvider};
pub use self::miner_service::{TestMinerService};

View File

@@ -16,9 +16,9 @@
use std::sync::Arc;
use jsonrpc_core::IoHandler;
use v1::tests::helpers::{TestAccount, TestAccountProvider};
use v1::{PersonalClient, Personal};
use util::numbers::*;
use util::keys::{TestAccount, TestAccountProvider};
use v1::{PersonalClient, Personal};
use std::collections::*;
fn accounts_provider() -> Arc<TestAccountProvider> {
@@ -49,7 +49,6 @@ fn accounts() {
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn new_account() {
let (test_provider, io) = setup();