Merge branch 'master' into transactions-propagate
This commit is contained in:
commit
cd686b5d68
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1250,7 +1250,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "parity-ui-precompiled"
|
||||
version = "1.4.0"
|
||||
source = "git+https://github.com/ethcore/js-precompiled.git#ce7e830d36483dab10419d9105ac39dc520e7a61"
|
||||
source = "git+https://github.com/ethcore/js-precompiled.git#5e3b9629692c550811b228d68ca99d1461a4f6cb"
|
||||
dependencies = [
|
||||
"parity-dapps-glue 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
42
ethcore/res/authority_round.json
Normal file
42
ethcore/res/authority_round.json
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "TestAuthorityRound",
|
||||
"engine": {
|
||||
"AuthorityRound": {
|
||||
"params": {
|
||||
"gasLimitBoundDivisor": "0x0400",
|
||||
"stepDuration": "1",
|
||||
"authorities" : [
|
||||
"0x7d577a597b2742b498cb5cf0c26cdcd726d39e6e",
|
||||
"0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"params": {
|
||||
"accountStartNonce": "0x0",
|
||||
"maximumExtraDataSize": "0x20",
|
||||
"minGasLimit": "0x1388",
|
||||
"networkID" : "0x69"
|
||||
},
|
||||
"genesis": {
|
||||
"seal": {
|
||||
"generic": {
|
||||
"fields": 1,
|
||||
"rlp": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"
|
||||
}
|
||||
},
|
||||
"difficulty": "0x20000",
|
||||
"author": "0x0000000000000000000000000000000000000000",
|
||||
"timestamp": "0x00",
|
||||
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
|
||||
"extraData": "0x",
|
||||
"gasLimit": "0x2fefd8"
|
||||
},
|
||||
"accounts": {
|
||||
"0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
|
||||
"0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
|
||||
"0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
|
||||
"0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
|
||||
"9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" }
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "TestAuthority",
|
||||
"name": "TestBasicAuthority",
|
||||
"engine": {
|
||||
"BasicAuthority": {
|
||||
"params": {
|
@ -131,10 +131,11 @@
|
||||
"0x807640a13483f8ac783c557fcdf27be11ea4ac7a"
|
||||
],
|
||||
"eip150Transition": "0x259518",
|
||||
"eip155Transition": "0x7fffffffffffffff",
|
||||
"eip160Transition": "0x7fffffffffffffff",
|
||||
"eip161abcTransition": "0x7fffffffffffffff",
|
||||
"eip161dTransition": "0x7fffffffffffffff"
|
||||
"eip155Transition": 2675000,
|
||||
"eip160Transition": 2675000,
|
||||
"eip161abcTransition": 2675000,
|
||||
"eip161dTransition": 2675000,
|
||||
"maxCodeSize": 24576
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -11,10 +11,10 @@
|
||||
"registrar": "0x52dff57a8a1532e6afb3dc07e2af58bb9eb05b3d",
|
||||
"homesteadTransition": "0x789b0",
|
||||
"eip150Transition": "0x1b34d8",
|
||||
"eip155Transition": "0x7fffffffffffffff",
|
||||
"eip160Transition": "0x7fffffffffffffff",
|
||||
"eip161abcTransition": "0x7fffffffffffffff",
|
||||
"eip161dTransition": "0x7fffffffffffffff"
|
||||
"eip155Transition": 1885000,
|
||||
"eip160Transition": 1885000,
|
||||
"eip161abcTransition": 1885000,
|
||||
"eip161dTransition": 1885000
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 9028c4801fd39fbb71a9796979182549a24e81c8
|
||||
Subproject commit 97066e40ccd061f727deb5cd860e4d9135aa2551
|
@ -554,6 +554,7 @@ impl Client {
|
||||
|
||||
/// Import transactions from the IO queue
|
||||
pub fn import_queued_transactions(&self, transactions: &[Bytes]) -> usize {
|
||||
trace!(target: "external_tx", "Importing queued");
|
||||
let _timer = PerfTimer::new("import_queued_transactions");
|
||||
self.queue_transactions.fetch_sub(transactions.len(), AtomicOrdering::SeqCst);
|
||||
let txs = transactions.iter().filter_map(|bytes| UntrustedRlp::new(bytes).as_val().ok()).collect();
|
||||
@ -561,6 +562,11 @@ impl Client {
|
||||
results.len()
|
||||
}
|
||||
|
||||
/// Used by PoA to try sealing on period change.
|
||||
pub fn update_sealing(&self) {
|
||||
self.miner.update_sealing(self)
|
||||
}
|
||||
|
||||
/// Attempt to get a copy of a specific block's final state.
|
||||
///
|
||||
/// This will not fail if given BlockID::Latest.
|
||||
@ -1193,7 +1199,9 @@ impl BlockChainClient for Client {
|
||||
}
|
||||
|
||||
fn queue_transactions(&self, transactions: Vec<Bytes>) {
|
||||
if self.queue_transactions.load(AtomicOrdering::Relaxed) > MAX_TX_QUEUE_SIZE {
|
||||
let queue_size = self.queue_transactions.load(AtomicOrdering::Relaxed);
|
||||
trace!(target: "external_tx", "Queue size: {}", queue_size);
|
||||
if queue_size > MAX_TX_QUEUE_SIZE {
|
||||
debug!("Ignoring {} transactions: queue is full", transactions.len());
|
||||
} else {
|
||||
let len = transactions.len();
|
||||
|
@ -119,6 +119,16 @@ impl TestBlockChainClient {
|
||||
/// Creates new test client with specified extra data for each block
|
||||
pub fn new_with_extra_data(extra_data: Bytes) -> Self {
|
||||
let spec = Spec::new_test();
|
||||
TestBlockChainClient::new_with_spec_and_extra(spec, extra_data)
|
||||
}
|
||||
|
||||
/// Create test client with custom spec.
|
||||
pub fn new_with_spec(spec: Spec) -> Self {
|
||||
TestBlockChainClient::new_with_spec_and_extra(spec, Bytes::new())
|
||||
}
|
||||
|
||||
/// Create test client with custom spec and extra data.
|
||||
pub fn new_with_spec_and_extra(spec: Spec, extra_data: Bytes) -> Self {
|
||||
let mut client = TestBlockChainClient {
|
||||
blocks: RwLock::new(HashMap::new()),
|
||||
numbers: RwLock::new(HashMap::new()),
|
||||
@ -315,7 +325,7 @@ pub fn get_temp_state_db() -> GuardedTempResult<StateDB> {
|
||||
|
||||
impl MiningBlockChainClient for TestBlockChainClient {
|
||||
fn latest_schedule(&self) -> Schedule {
|
||||
Schedule::new_post_eip150(true, true, true)
|
||||
Schedule::new_post_eip150(24576, true, true, true)
|
||||
}
|
||||
|
||||
fn prepare_open_block(&self, author: Address, gas_range_target: (U256, U256), extra_data: Bytes) -> OpenBlock {
|
||||
|
429
ethcore/src/engines/authority_round.rs
Normal file
429
ethcore/src/engines/authority_round.rs
Normal file
@ -0,0 +1,429 @@
|
||||
// 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/>.
|
||||
|
||||
//! A blockchain engine that supports a non-instant BFT proof-of-authority.
|
||||
|
||||
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering as AtomicOrdering};
|
||||
use std::sync::Weak;
|
||||
use std::time::{UNIX_EPOCH, Duration};
|
||||
use util::*;
|
||||
use ethkey::{verify_address, Signature};
|
||||
use rlp::{UntrustedRlp, View, encode};
|
||||
use account_provider::AccountProvider;
|
||||
use block::*;
|
||||
use spec::CommonParams;
|
||||
use engines::Engine;
|
||||
use header::Header;
|
||||
use error::{Error, BlockError};
|
||||
use evm::Schedule;
|
||||
use ethjson;
|
||||
use io::{IoContext, IoHandler, TimerToken, IoService, IoChannel};
|
||||
use service::ClientIoMessage;
|
||||
use transaction::SignedTransaction;
|
||||
use env_info::EnvInfo;
|
||||
use builtin::Builtin;
|
||||
|
||||
/// `AuthorityRound` params.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct AuthorityRoundParams {
|
||||
/// Gas limit divisor.
|
||||
pub gas_limit_bound_divisor: U256,
|
||||
/// Time to wait before next block or authority switching.
|
||||
pub step_duration: Duration,
|
||||
/// Valid authorities.
|
||||
pub authorities: Vec<Address>,
|
||||
/// Number of authorities.
|
||||
pub authority_n: usize,
|
||||
}
|
||||
|
||||
impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
|
||||
fn from(p: ethjson::spec::AuthorityRoundParams) -> Self {
|
||||
AuthorityRoundParams {
|
||||
gas_limit_bound_divisor: p.gas_limit_bound_divisor.into(),
|
||||
step_duration: Duration::from_secs(p.step_duration.into()),
|
||||
authority_n: p.authorities.len(),
|
||||
authorities: p.authorities.into_iter().map(Into::into).collect::<Vec<_>>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Engine using `AuthorityRound` proof-of-work consensus algorithm, suitable for Ethereum
|
||||
/// mainnet chains in the Olympic, Frontier and Homestead eras.
|
||||
pub struct AuthorityRound {
|
||||
params: CommonParams,
|
||||
our_params: AuthorityRoundParams,
|
||||
builtins: BTreeMap<Address, Builtin>,
|
||||
transition_service: IoService<BlockArrived>,
|
||||
message_channel: Mutex<Option<IoChannel<ClientIoMessage>>>,
|
||||
step: AtomicUsize,
|
||||
proposed: AtomicBool,
|
||||
}
|
||||
|
||||
fn header_step(header: &Header) -> Result<usize, ::rlp::DecoderError> {
|
||||
UntrustedRlp::new(&header.seal()[0]).as_val()
|
||||
}
|
||||
|
||||
fn header_signature(header: &Header) -> Result<Signature, ::rlp::DecoderError> {
|
||||
UntrustedRlp::new(&header.seal()[1]).as_val::<H520>().map(Into::into)
|
||||
}
|
||||
|
||||
trait AsMillis {
|
||||
fn as_millis(&self) -> u64;
|
||||
}
|
||||
|
||||
impl AsMillis for Duration {
|
||||
fn as_millis(&self) -> u64 {
|
||||
self.as_secs()*1_000 + (self.subsec_nanos()/1_000_000) as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthorityRound {
|
||||
/// Create a new instance of AuthorityRound engine.
|
||||
pub fn new(params: CommonParams, our_params: AuthorityRoundParams, builtins: BTreeMap<Address, Builtin>) -> Result<Arc<Self>, Error> {
|
||||
let initial_step = (unix_now().as_secs() / our_params.step_duration.as_secs()) as usize;
|
||||
let engine = Arc::new(
|
||||
AuthorityRound {
|
||||
params: params,
|
||||
our_params: our_params,
|
||||
builtins: builtins,
|
||||
transition_service: try!(IoService::<BlockArrived>::start()),
|
||||
message_channel: Mutex::new(None),
|
||||
step: AtomicUsize::new(initial_step),
|
||||
proposed: AtomicBool::new(false)
|
||||
});
|
||||
let handler = TransitionHandler { engine: Arc::downgrade(&engine) };
|
||||
try!(engine.transition_service.register_handler(Arc::new(handler)));
|
||||
Ok(engine)
|
||||
}
|
||||
|
||||
fn step(&self) -> usize {
|
||||
self.step.load(AtomicOrdering::SeqCst)
|
||||
}
|
||||
|
||||
fn remaining_step_duration(&self) -> Duration {
|
||||
let now = unix_now();
|
||||
let step_end = self.our_params.step_duration * (self.step() as u32 + 1);
|
||||
if step_end > now {
|
||||
step_end - now
|
||||
} else {
|
||||
Duration::from_secs(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn step_proposer(&self, step: usize) -> &Address {
|
||||
let ref p = self.our_params;
|
||||
p.authorities.get(step % p.authority_n).expect("There are authority_n authorities; taking number modulo authority_n gives number in authority_n range; qed")
|
||||
}
|
||||
|
||||
fn is_step_proposer(&self, step: usize, address: &Address) -> bool {
|
||||
self.step_proposer(step) == address
|
||||
}
|
||||
}
|
||||
|
||||
fn unix_now() -> Duration {
|
||||
UNIX_EPOCH.elapsed().expect("Valid time has to be set in your system.")
|
||||
}
|
||||
|
||||
struct TransitionHandler {
|
||||
engine: Weak<AuthorityRound>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct BlockArrived;
|
||||
|
||||
const ENGINE_TIMEOUT_TOKEN: TimerToken = 23;
|
||||
|
||||
impl IoHandler<BlockArrived> for TransitionHandler {
|
||||
fn initialize(&self, io: &IoContext<BlockArrived>) {
|
||||
if let Some(engine) = self.engine.upgrade() {
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.remaining_step_duration().as_millis())
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to start consensus step timer: {}.", e))
|
||||
}
|
||||
}
|
||||
|
||||
fn timeout(&self, io: &IoContext<BlockArrived>, timer: TimerToken) {
|
||||
if timer == ENGINE_TIMEOUT_TOKEN {
|
||||
if let Some(engine) = self.engine.upgrade() {
|
||||
engine.step.fetch_add(1, AtomicOrdering::SeqCst);
|
||||
engine.proposed.store(false, AtomicOrdering::SeqCst);
|
||||
if let Some(ref channel) = *engine.message_channel.lock() {
|
||||
match channel.send(ClientIoMessage::UpdateSealing) {
|
||||
Ok(_) => trace!(target: "poa", "timeout: UpdateSealing message sent for step {}.", engine.step.load(AtomicOrdering::Relaxed)),
|
||||
Err(err) => trace!(target: "poa", "timeout: Could not send a sealing message {} for step {}.", err, engine.step.load(AtomicOrdering::Relaxed)),
|
||||
}
|
||||
}
|
||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.remaining_step_duration().as_millis())
|
||||
.unwrap_or_else(|e| warn!(target: "poa", "Failed to restart consensus step timer: {}.", e))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Engine for AuthorityRound {
|
||||
fn name(&self) -> &str { "AuthorityRound" }
|
||||
fn version(&self) -> SemanticVersion { SemanticVersion::new(1, 0, 0) }
|
||||
/// Two fields - consensus step and the corresponding proposer signature.
|
||||
fn seal_fields(&self) -> usize { 2 }
|
||||
|
||||
fn params(&self) -> &CommonParams { &self.params }
|
||||
fn builtins(&self) -> &BTreeMap<Address, Builtin> { &self.builtins }
|
||||
|
||||
/// Additional engine-specific information for the user/developer concerning `header`.
|
||||
fn extra_info(&self, header: &Header) -> BTreeMap<String, String> {
|
||||
map![
|
||||
"step".into() => header_step(header).as_ref().map(ToString::to_string).unwrap_or("".into()),
|
||||
"signature".into() => header_signature(header).as_ref().map(ToString::to_string).unwrap_or("".into())
|
||||
]
|
||||
}
|
||||
|
||||
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
|
||||
Schedule::new_post_eip150(usize::max_value(), true, true, true)
|
||||
}
|
||||
|
||||
fn populate_from_parent(&self, header: &mut Header, parent: &Header, gas_floor_target: U256, _gas_ceil_target: U256) {
|
||||
header.set_difficulty(parent.difficulty().clone());
|
||||
header.set_gas_limit({
|
||||
let gas_limit = parent.gas_limit().clone();
|
||||
let bound_divisor = self.our_params.gas_limit_bound_divisor;
|
||||
if gas_limit < gas_floor_target {
|
||||
min(gas_floor_target, gas_limit + gas_limit / bound_divisor - 1.into())
|
||||
} else {
|
||||
max(gas_floor_target, gas_limit - gas_limit / bound_divisor + 1.into())
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Apply the block reward on finalisation of the block.
|
||||
/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
|
||||
fn on_close_block(&self, _block: &mut ExecutedBlock) {}
|
||||
|
||||
fn is_sealer(&self, author: &Address) -> Option<bool> {
|
||||
let ref p = self.our_params;
|
||||
Some(p.authorities.contains(author))
|
||||
}
|
||||
|
||||
/// Attempt to seal the block internally.
|
||||
///
|
||||
/// This operation is synchronous and may (quite reasonably) not be available, in which `false` will
|
||||
/// be returned.
|
||||
fn generate_seal(&self, block: &ExecutedBlock, accounts: Option<&AccountProvider>) -> Option<Vec<Bytes>> {
|
||||
if self.proposed.load(AtomicOrdering::SeqCst) { return None; }
|
||||
let header = block.header();
|
||||
let step = self.step();
|
||||
if self.is_step_proposer(step, header.author()) {
|
||||
if let Some(ap) = accounts {
|
||||
// Account should be permanently unlocked, otherwise sealing will fail.
|
||||
if let Ok(signature) = ap.sign(*header.author(), None, header.bare_hash()) {
|
||||
trace!(target: "poa", "generate_seal: Issuing a block for step {}.", step);
|
||||
self.proposed.store(true, AtomicOrdering::SeqCst);
|
||||
return Some(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: Accounts secret key unavailable.");
|
||||
}
|
||||
} else {
|
||||
warn!(target: "poa", "generate_seal: FAIL: Accounts not provided.");
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Check the number of seal fields.
|
||||
fn verify_block_basic(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
if header.seal().len() != self.seal_fields() {
|
||||
trace!(target: "poa", "verify_block_basic: wrong number of seal fields");
|
||||
Err(From::from(BlockError::InvalidSealArity(
|
||||
Mismatch { expected: self.seal_fields(), found: header.seal().len() }
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the signature belongs to the correct proposer.
|
||||
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
let header_step = try!(header_step(header));
|
||||
// Give one step slack if step is lagging, double vote is still not possible.
|
||||
if header_step <= self.step() + 1 {
|
||||
let proposer_signature = try!(header_signature(header));
|
||||
let ok_sig = try!(verify_address(self.step_proposer(header_step), &proposer_signature, &header.bare_hash()));
|
||||
if ok_sig {
|
||||
Ok(())
|
||||
} else {
|
||||
trace!(target: "poa", "verify_block_unordered: invalid seal signature");
|
||||
try!(Err(BlockError::InvalidSeal))
|
||||
}
|
||||
} else {
|
||||
trace!(target: "poa", "verify_block_unordered: block from the future");
|
||||
try!(Err(BlockError::InvalidSeal))
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_block_family(&self, header: &Header, parent: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
|
||||
// Don't calculate difficulty for genesis blocks.
|
||||
if header.number() == 0 {
|
||||
return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { min: Some(1), max: None, found: header.number() })));
|
||||
}
|
||||
|
||||
let step = try!(header_step(header));
|
||||
// Check if parent is from a previous step.
|
||||
if step == try!(header_step(parent)) {
|
||||
trace!(target: "poa", "Multiple blocks proposed for step {}.", step);
|
||||
try!(Err(BlockError::DoubleVote(header.author().clone())));
|
||||
}
|
||||
|
||||
// Check difficulty is correct given the two timestamps.
|
||||
if header.difficulty() != parent.difficulty() {
|
||||
return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: *parent.difficulty(), found: *header.difficulty() })))
|
||||
}
|
||||
let gas_limit_divisor = self.our_params.gas_limit_bound_divisor;
|
||||
let min_gas = parent.gas_limit().clone() - parent.gas_limit().clone() / gas_limit_divisor;
|
||||
let max_gas = parent.gas_limit().clone() + parent.gas_limit().clone() / gas_limit_divisor;
|
||||
if header.gas_limit() <= &min_gas || header.gas_limit() >= &max_gas {
|
||||
return Err(From::from(BlockError::InvalidGasLimit(OutOfBounds { min: Some(min_gas), max: Some(max_gas), found: header.gas_limit().clone() })));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_transaction_basic(&self, t: &SignedTransaction, _header: &Header) -> Result<(), Error> {
|
||||
try!(t.check_low_s());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn verify_transaction(&self, t: &SignedTransaction, _header: &Header) -> Result<(), Error> {
|
||||
t.sender().map(|_|()) // Perform EC recovery and cache sender
|
||||
}
|
||||
|
||||
fn register_message_channel(&self, message_channel: IoChannel<ClientIoMessage>) {
|
||||
let mut guard = self.message_channel.lock();
|
||||
*guard = Some(message_channel);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use util::*;
|
||||
use env_info::EnvInfo;
|
||||
use header::Header;
|
||||
use error::{Error, BlockError};
|
||||
use rlp::encode;
|
||||
use block::*;
|
||||
use tests::helpers::*;
|
||||
use account_provider::AccountProvider;
|
||||
use spec::Spec;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
#[test]
|
||||
fn has_valid_metadata() {
|
||||
let engine = Spec::new_test_round().engine;
|
||||
assert!(!engine.name().is_empty());
|
||||
assert!(engine.version().major >= 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_return_schedule() {
|
||||
let engine = Spec::new_test_round().engine;
|
||||
let schedule = engine.schedule(&EnvInfo {
|
||||
number: 10000000,
|
||||
author: 0.into(),
|
||||
timestamp: 0,
|
||||
difficulty: 0.into(),
|
||||
last_hashes: Arc::new(vec![]),
|
||||
gas_used: 0.into(),
|
||||
gas_limit: 0.into(),
|
||||
});
|
||||
|
||||
assert!(schedule.stack_limit > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verification_fails_on_short_seal() {
|
||||
let engine = Spec::new_test_round().engine;
|
||||
let header: Header = Header::default();
|
||||
|
||||
let verify_result = engine.verify_block_basic(&header, None);
|
||||
|
||||
match verify_result {
|
||||
Err(Error::Block(BlockError::InvalidSealArity(_))) => {},
|
||||
Err(_) => { panic!("should be block seal-arity mismatch error (got {:?})", verify_result); },
|
||||
_ => { panic!("Should be error, got Ok"); },
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_do_signature_verification_fail() {
|
||||
let engine = Spec::new_test_round().engine;
|
||||
let mut header: Header = Header::default();
|
||||
header.set_seal(vec![encode(&H520::default()).to_vec()]);
|
||||
|
||||
let verify_result = engine.verify_block_unordered(&header, None);
|
||||
assert!(verify_result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generates_seal_and_does_not_double_propose() {
|
||||
let tap = AccountProvider::transient_provider();
|
||||
let addr1 = tap.insert_account("1".sha3(), "1").unwrap();
|
||||
tap.unlock_account_permanently(addr1, "1".into()).unwrap();
|
||||
let addr2 = tap.insert_account("2".sha3(), "2").unwrap();
|
||||
tap.unlock_account_permanently(addr2, "2".into()).unwrap();
|
||||
|
||||
let spec = Spec::new_test_round();
|
||||
let engine = &*spec.engine;
|
||||
let genesis_header = spec.genesis_header();
|
||||
let mut db1 = get_temp_state_db().take();
|
||||
spec.ensure_db_good(&mut db1).unwrap();
|
||||
let mut db2 = get_temp_state_db().take();
|
||||
spec.ensure_db_good(&mut db2).unwrap();
|
||||
let last_hashes = Arc::new(vec![genesis_header.hash()]);
|
||||
let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
||||
let b1 = b1.close_and_lock();
|
||||
let b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes, addr2, (3141562.into(), 31415620.into()), vec![]).unwrap();
|
||||
let b2 = b2.close_and_lock();
|
||||
|
||||
if let Some(seal) = engine.generate_seal(b1.block(), Some(&tap)) {
|
||||
assert!(b1.clone().try_seal(engine, seal).is_ok());
|
||||
// Second proposal is forbidden.
|
||||
assert!(engine.generate_seal(b1.block(), Some(&tap)).is_none());
|
||||
}
|
||||
|
||||
if let Some(seal) = engine.generate_seal(b2.block(), Some(&tap)) {
|
||||
assert!(b2.clone().try_seal(engine, seal).is_ok());
|
||||
// Second proposal is forbidden.
|
||||
assert!(engine.generate_seal(b2.block(), Some(&tap)).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn proposer_switching() {
|
||||
let mut header: Header = Header::default();
|
||||
let tap = AccountProvider::transient_provider();
|
||||
let addr = tap.insert_account("0".sha3(), "0").unwrap();
|
||||
|
||||
header.set_author(addr);
|
||||
|
||||
let engine = Spec::new_test_round().engine;
|
||||
|
||||
let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
|
||||
let mut step = UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
header.set_seal(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
||||
let first_ok = engine.verify_block_seal(&header).is_ok();
|
||||
step = step + 1;
|
||||
header.set_seal(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
||||
let second_ok = engine.verify_block_seal(&header).is_ok();
|
||||
|
||||
assert!(first_ok ^ second_ok);
|
||||
}
|
||||
}
|
@ -181,13 +181,6 @@ impl Engine for BasicAuthority {
|
||||
}
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Get the none field of the header.
|
||||
pub fn signature(&self) -> H520 {
|
||||
::rlp::decode(&self.seal()[0])
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use util::*;
|
||||
@ -201,7 +194,7 @@ mod tests {
|
||||
|
||||
/// Create a new test chain spec with `BasicAuthority` consensus engine.
|
||||
fn new_test_authority() -> Spec {
|
||||
let bytes: &[u8] = include_bytes!("../../res/test_authority.json");
|
||||
let bytes: &[u8] = include_bytes!("../../res/basic_authority.json");
|
||||
Spec::load(bytes).expect("invalid chain spec")
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,7 @@ impl Engine for InstantSeal {
|
||||
}
|
||||
|
||||
fn schedule(&self, _env_info: &EnvInfo) -> Schedule {
|
||||
Schedule::new_post_eip150(false, false, false)
|
||||
Schedule::new_post_eip150(usize::max_value(), false, false, false)
|
||||
}
|
||||
|
||||
fn is_sealer(&self, _author: &Address) -> Option<bool> { Some(true) }
|
||||
|
@ -19,10 +19,12 @@
|
||||
mod null_engine;
|
||||
mod instant_seal;
|
||||
mod basic_authority;
|
||||
mod authority_round;
|
||||
|
||||
pub use self::null_engine::NullEngine;
|
||||
pub use self::instant_seal::InstantSeal;
|
||||
pub use self::basic_authority::BasicAuthority;
|
||||
pub use self::authority_round::AuthorityRound;
|
||||
|
||||
use util::*;
|
||||
use account_provider::AccountProvider;
|
||||
@ -32,6 +34,8 @@ use env_info::EnvInfo;
|
||||
use error::Error;
|
||||
use spec::CommonParams;
|
||||
use evm::Schedule;
|
||||
use io::IoChannel;
|
||||
use service::ClientIoMessage;
|
||||
use header::Header;
|
||||
use transaction::SignedTransaction;
|
||||
|
||||
@ -140,5 +144,7 @@ pub trait Engine : Sync + Send {
|
||||
self.builtins().get(a).expect("attempted to execute nonexistent builtin").execute(input, output);
|
||||
}
|
||||
|
||||
/// Add a channel for communication with Client which can be used for sealing.
|
||||
fn register_message_channel(&self, _message_channel: IoChannel<ClientIoMessage>) {}
|
||||
// TODO: sealing stuff - though might want to leave this for later.
|
||||
}
|
||||
|
@ -167,6 +167,8 @@ pub enum BlockError {
|
||||
UnknownParent(H256),
|
||||
/// Uncle parent given is unknown.
|
||||
UnknownUncleParent(H256),
|
||||
/// The same author issued different votes at the same step.
|
||||
DoubleVote(H160),
|
||||
}
|
||||
|
||||
impl fmt::Display for BlockError {
|
||||
@ -200,6 +202,7 @@ impl fmt::Display for BlockError {
|
||||
RidiculousNumber(ref oob) => format!("Implausible block number. {}", oob),
|
||||
UnknownParent(ref hash) => format!("Unknown parent: {}", hash),
|
||||
UnknownUncleParent(ref hash) => format!("Unknown uncle parent: {}", hash),
|
||||
DoubleVote(ref address) => format!("Author {} issued too many blocks.", address),
|
||||
};
|
||||
|
||||
f.write_fmt(format_args!("Block error ({})", msg))
|
||||
|
@ -73,7 +73,9 @@ pub struct EthashParams {
|
||||
/// Number of first block where ECIP-1010 begins.
|
||||
pub ecip1010_pause_transition: u64,
|
||||
/// Number of first block where ECIP-1010 ends.
|
||||
pub ecip1010_continue_transition: u64
|
||||
pub ecip1010_continue_transition: u64,
|
||||
/// Maximum amount of code that can be deploying into a contract.
|
||||
pub max_code_size: u64,
|
||||
}
|
||||
|
||||
impl From<ethjson::spec::EthashParams> for EthashParams {
|
||||
@ -87,19 +89,20 @@ impl From<ethjson::spec::EthashParams> for EthashParams {
|
||||
block_reward: p.block_reward.into(),
|
||||
registrar: p.registrar.map_or_else(Address::new, Into::into),
|
||||
homestead_transition: p.homestead_transition.map_or(0, Into::into),
|
||||
dao_hardfork_transition: p.dao_hardfork_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
dao_hardfork_transition: p.dao_hardfork_transition.map_or(u64::max_value(), Into::into),
|
||||
dao_hardfork_beneficiary: p.dao_hardfork_beneficiary.map_or_else(Address::new, Into::into),
|
||||
dao_hardfork_accounts: p.dao_hardfork_accounts.unwrap_or_else(Vec::new).into_iter().map(Into::into).collect(),
|
||||
difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(u64::max_value(), Into::into),
|
||||
difficulty_hardfork_bound_divisor: p.difficulty_hardfork_bound_divisor.map_or(p.difficulty_bound_divisor.into(), Into::into),
|
||||
bomb_defuse_transition: p.bomb_defuse_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
bomb_defuse_transition: p.bomb_defuse_transition.map_or(u64::max_value(), Into::into),
|
||||
eip150_transition: p.eip150_transition.map_or(0, Into::into),
|
||||
eip155_transition: p.eip155_transition.map_or(0, Into::into),
|
||||
eip160_transition: p.eip160_transition.map_or(0, Into::into),
|
||||
eip161abc_transition: p.eip161abc_transition.map_or(0, Into::into),
|
||||
eip161d_transition: p.eip161d_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
ecip1010_pause_transition: p.ecip1010_pause_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
ecip1010_continue_transition: p.ecip1010_continue_transition.map_or(0x7fffffffffffffff, Into::into),
|
||||
eip161d_transition: p.eip161d_transition.map_or(u64::max_value(), Into::into),
|
||||
ecip1010_pause_transition: p.ecip1010_pause_transition.map_or(u64::max_value(), Into::into),
|
||||
ecip1010_continue_transition: p.ecip1010_continue_transition.map_or(u64::max_value(), Into::into),
|
||||
max_code_size: p.max_code_size.map_or(u64::max_value(), Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -152,6 +155,7 @@ impl Engine for Ethash {
|
||||
Schedule::new_homestead()
|
||||
} else {
|
||||
Schedule::new_post_eip150(
|
||||
self.ethash_params.max_code_size as usize,
|
||||
env_info.number >= self.ethash_params.eip160_transition,
|
||||
env_info.number >= self.ethash_params.eip161abc_transition,
|
||||
env_info.number >= self.ethash_params.eip161d_transition
|
||||
|
@ -70,6 +70,8 @@ pub struct Schedule {
|
||||
pub quad_coeff_div: usize,
|
||||
/// Cost for contract length when executing `CREATE`
|
||||
pub create_data_gas: usize,
|
||||
/// Maximum code size when creating a contract.
|
||||
pub create_data_limit: usize,
|
||||
/// Transaction cost
|
||||
pub tx_gas: usize,
|
||||
/// `CREATE` transaction cost
|
||||
@ -111,7 +113,7 @@ impl Schedule {
|
||||
}
|
||||
|
||||
/// Schedule for the post-EIP-150-era of the Ethereum main net.
|
||||
pub fn new_post_eip150(fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule {
|
||||
pub fn new_post_eip150(max_code_size: usize, fix_exp: bool, no_empty: bool, kill_empty: bool) -> Schedule {
|
||||
Schedule {
|
||||
exceptional_failed_code_deposit: true,
|
||||
have_delegate_call: true,
|
||||
@ -139,6 +141,7 @@ impl Schedule {
|
||||
memory_gas: 3,
|
||||
quad_coeff_div: 512,
|
||||
create_data_gas: 200,
|
||||
create_data_limit: max_code_size,
|
||||
tx_gas: 21000,
|
||||
tx_create_gas: 53000,
|
||||
tx_data_zero_gas: 4,
|
||||
@ -183,6 +186,7 @@ impl Schedule {
|
||||
memory_gas: 3,
|
||||
quad_coeff_div: 512,
|
||||
create_data_gas: 200,
|
||||
create_data_limit: usize::max_value(),
|
||||
tx_gas: 21000,
|
||||
tx_create_gas: tcg,
|
||||
tx_data_zero_gas: 4,
|
||||
|
@ -242,7 +242,7 @@ impl<'a, T, V> Ext for Externalities<'a, T, V> where T: 'a + Tracer, V: 'a + VMT
|
||||
},
|
||||
OutputPolicy::InitContract(ref mut copy) => {
|
||||
let return_cost = U256::from(data.len()) * U256::from(self.schedule.create_data_gas);
|
||||
if return_cost > *gas {
|
||||
if return_cost > *gas || data.len() > self.schedule.create_data_limit {
|
||||
return match self.schedule.exceptional_failed_code_deposit {
|
||||
true => Err(evm::Error::OutOfGas),
|
||||
false => Ok(*gas)
|
||||
|
@ -214,7 +214,8 @@ pub struct Miner {
|
||||
sealing_block_last_request: Mutex<u64>,
|
||||
// for sealing...
|
||||
options: MinerOptions,
|
||||
seals_internally: bool,
|
||||
/// Does the node perform internal (without work) sealing.
|
||||
pub seals_internally: bool,
|
||||
|
||||
gas_range_target: RwLock<(U256, U256)>,
|
||||
author: RwLock<Address>,
|
||||
@ -269,6 +270,11 @@ impl Miner {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new instance of miner with accounts and with given spec.
|
||||
pub fn with_spec_and_accounts(spec: &Spec, accounts: Option<Arc<AccountProvider>>) -> Miner {
|
||||
Miner::new_raw(Default::default(), GasPricer::new_fixed(20_000_000_000u64.into()), spec, accounts)
|
||||
}
|
||||
|
||||
/// Creates new instance of miner without accounts, but with given spec.
|
||||
pub fn with_spec(spec: &Spec) -> Miner {
|
||||
Miner::new_raw(Default::default(), GasPricer::new_fixed(20_000_000_000u64.into()), spec, None)
|
||||
@ -431,6 +437,7 @@ impl Miner {
|
||||
let last_request = *self.sealing_block_last_request.lock();
|
||||
let should_disable_sealing = !self.forced_sealing()
|
||||
&& !has_local_transactions
|
||||
&& !self.seals_internally
|
||||
&& best_block > last_request
|
||||
&& best_block - last_request > SEALING_TIMEOUT_IN_BLOCKS;
|
||||
|
||||
@ -474,9 +481,10 @@ impl Miner {
|
||||
|
||||
/// Uses Engine to seal the block internally and then imports it to chain.
|
||||
fn seal_and_import_block_internally(&self, chain: &MiningBlockChainClient, block: ClosedBlock) -> bool {
|
||||
if !block.transactions().is_empty() {
|
||||
if !block.transactions().is_empty() || self.forced_sealing() {
|
||||
if let Ok(sealed) = self.seal_block_internally(block) {
|
||||
if chain.import_block(sealed.rlp_bytes()).is_ok() {
|
||||
trace!(target: "miner", "import_block_internally: imported internally sealed block");
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -775,7 +783,7 @@ impl MinerService for Miner {
|
||||
chain: &MiningBlockChainClient,
|
||||
transactions: Vec<SignedTransaction>
|
||||
) -> Vec<Result<TransactionImportResult, Error>> {
|
||||
|
||||
trace!(target: "external_tx", "Importing external transactions");
|
||||
let results = {
|
||||
let mut transaction_queue = self.transaction_queue.lock();
|
||||
self.add_transactions_to_queue(
|
||||
|
@ -48,6 +48,8 @@ pub enum ClientIoMessage {
|
||||
FeedBlockChunk(H256, Bytes),
|
||||
/// Take a snapshot for the block with given number.
|
||||
TakeSnapshot(u64),
|
||||
/// Trigger sealing update (useful for internal sealing).
|
||||
UpdateSealing,
|
||||
}
|
||||
|
||||
/// Client service setup. Creates and registers client and network services with the IO subsystem.
|
||||
@ -111,6 +113,8 @@ impl ClientService {
|
||||
});
|
||||
try!(io_service.register_handler(client_io));
|
||||
|
||||
spec.engine.register_message_channel(io_service.channel());
|
||||
|
||||
let stop_guard = ::devtools::StopGuard::new();
|
||||
run_ipc(ipc_path, client.clone(), snapshot.clone(), stop_guard.share());
|
||||
|
||||
@ -213,8 +217,11 @@ impl IoHandler<ClientIoMessage> for ClientIoHandler {
|
||||
if let Err(e) = res {
|
||||
debug!(target: "snapshot", "Failed to initialize periodic snapshot thread: {:?}", e);
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
ClientIoMessage::UpdateSealing => {
|
||||
trace!(target: "authorityround", "message: UpdateSealing");
|
||||
self.client.update_sealing()
|
||||
},
|
||||
_ => {} // ignore other messages
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@
|
||||
|
||||
use util::*;
|
||||
use builtin::Builtin;
|
||||
use engines::{Engine, NullEngine, InstantSeal, BasicAuthority};
|
||||
use engines::{Engine, NullEngine, InstantSeal, BasicAuthority, AuthorityRound};
|
||||
use pod_state::*;
|
||||
use account_db::*;
|
||||
use header::{BlockNumber, Header};
|
||||
@ -150,6 +150,7 @@ impl Spec {
|
||||
ethjson::spec::Engine::InstantSeal => Arc::new(InstantSeal::new(params, builtins)),
|
||||
ethjson::spec::Engine::Ethash(ethash) => Arc::new(ethereum::Ethash::new(params, From::from(ethash.params), builtins)),
|
||||
ethjson::spec::Engine::BasicAuthority(basic_authority) => Arc::new(BasicAuthority::new(params, From::from(basic_authority.params), builtins)),
|
||||
ethjson::spec::Engine::AuthorityRound(authority_round) => AuthorityRound::new(params, From::from(authority_round.params), builtins).expect("Consensus engine could not be started."),
|
||||
}
|
||||
}
|
||||
|
||||
@ -280,6 +281,10 @@ impl Spec {
|
||||
|
||||
/// Create a new Spec with InstantSeal consensus which does internal sealing (not requiring work).
|
||||
pub fn new_instant() -> Spec { load_bundled!("instant_seal") }
|
||||
|
||||
/// Create a new Spec with AuthorityRound consensus which does internal sealing (not requiring work).
|
||||
/// Accounts with secrets "1".sha3() and "2".sha3() are the authorities.
|
||||
pub fn new_test_round() -> Self { load_bundled!("authority_round") }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -433,18 +433,19 @@ pub fn get_default_ethash_params() -> EthashParams{
|
||||
block_reward: U256::from(0),
|
||||
registrar: "0000000000000000000000000000000000000001".into(),
|
||||
homestead_transition: 1150000,
|
||||
dao_hardfork_transition: 0x7fffffffffffffff,
|
||||
dao_hardfork_transition: u64::max_value(),
|
||||
dao_hardfork_beneficiary: "0000000000000000000000000000000000000001".into(),
|
||||
dao_hardfork_accounts: vec![],
|
||||
difficulty_hardfork_transition: 0x7fffffffffffffff,
|
||||
difficulty_hardfork_transition: u64::max_value(),
|
||||
difficulty_hardfork_bound_divisor: U256::from(0),
|
||||
bomb_defuse_transition: 0x7fffffffffffffff,
|
||||
eip150_transition: 0x7fffffffffffffff,
|
||||
eip155_transition: 0x7fffffffffffffff,
|
||||
eip160_transition: 0x7fffffffffffffff,
|
||||
eip161abc_transition: 0x7fffffffffffffff,
|
||||
eip161d_transition: 0x7fffffffffffffff,
|
||||
ecip1010_pause_transition: 0x7fffffffffffffff,
|
||||
ecip1010_continue_transition: 0x7fffffffffffffff
|
||||
bomb_defuse_transition: u64::max_value(),
|
||||
eip150_transition: u64::max_value(),
|
||||
eip155_transition: u64::max_value(),
|
||||
eip160_transition: u64::max_value(),
|
||||
eip161abc_transition: u64::max_value(),
|
||||
eip161d_transition: u64::max_value(),
|
||||
ecip1010_pause_transition: u64::max_value(),
|
||||
ecip1010_continue_transition: u64::max_value(),
|
||||
max_code_size: u64::max_value(),
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "parity.js",
|
||||
"version": "0.2.43",
|
||||
"version": "0.2.49",
|
||||
"main": "release/index.js",
|
||||
"jsnext:main": "src/index.js",
|
||||
"author": "Parity Team <admin@parity.io>",
|
||||
|
@ -46,7 +46,8 @@ function stringToBytes (input) {
|
||||
if (isArray(input)) {
|
||||
return input;
|
||||
} else if (input.substr(0, 2) === '0x') {
|
||||
return input.substr(2).toLowerCase().match(/.{1,2}/g).map((value) => parseInt(value, 16));
|
||||
const matches = input.substr(2).toLowerCase().match(/.{1,2}/g) || [];
|
||||
return matches.map((value) => parseInt(value, 16));
|
||||
} else {
|
||||
return input.split('').map((char) => char.charCodeAt(0));
|
||||
}
|
||||
|
@ -40,9 +40,12 @@ export default class Contract {
|
||||
|
||||
this._events.forEach((evt) => {
|
||||
this._instance[evt.name] = evt;
|
||||
this._instance[evt.signature] = evt;
|
||||
});
|
||||
|
||||
this._functions.forEach((fn) => {
|
||||
this._instance[fn.name] = fn;
|
||||
this._instance[fn.signature] = fn;
|
||||
});
|
||||
|
||||
this._sendSubscriptionChanges();
|
||||
|
@ -20,6 +20,7 @@ import sinon from 'sinon';
|
||||
import { TEST_HTTP_URL, mockHttp } from '../../../test/mockRpc';
|
||||
|
||||
import Abi from '../../abi';
|
||||
import { sha3 } from '../util/sha3';
|
||||
|
||||
import Api from '../api';
|
||||
import Contract from './contract';
|
||||
@ -113,7 +114,13 @@ describe('api/contract/Contract', () => {
|
||||
]);
|
||||
contract.at('6789');
|
||||
|
||||
expect(Object.keys(contract.instance)).to.deep.equal(['Drained', 'balanceOf', 'address']);
|
||||
expect(Object.keys(contract.instance)).to.deep.equal([
|
||||
'Drained',
|
||||
/^(?:0x)(.+)$/.exec(sha3('Drained(uint256)'))[1],
|
||||
'balanceOf',
|
||||
/^(?:0x)(.+)$/.exec(sha3('balanceOf(address)'))[1].substr(0, 8),
|
||||
'address'
|
||||
]);
|
||||
expect(contract.address).to.equal('6789');
|
||||
});
|
||||
});
|
||||
|
@ -17,3 +17,15 @@
|
||||
export function bytesToHex (bytes) {
|
||||
return '0x' + bytes.map((b) => ('0' + b.toString(16)).slice(-2)).join('');
|
||||
}
|
||||
|
||||
export function hex2Ascii (_hex) {
|
||||
const hex = /^(?:0x)?(.*)$/.exec(_hex.toString())[1];
|
||||
|
||||
let str = '';
|
||||
|
||||
for (let i = 0; i < hex.length; i += 2) {
|
||||
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import { isAddress as isAddressValid, toChecksumAddress } from '../../abi/util/address';
|
||||
import { decodeCallData, decodeMethodInput, methodToAbi } from './decode';
|
||||
import { bytesToHex } from './format';
|
||||
import { bytesToHex, hex2Ascii } from './format';
|
||||
import { fromWei, toWei } from './wei';
|
||||
import { sha3 } from './sha3';
|
||||
import { isArray, isFunction, isHex, isInstanceOf, isString } from './types';
|
||||
@ -30,6 +30,7 @@ export default {
|
||||
isInstanceOf,
|
||||
isString,
|
||||
bytesToHex,
|
||||
hex2Ascii,
|
||||
createIdentityImg,
|
||||
decodeCallData,
|
||||
decodeMethodInput,
|
||||
|
@ -18,6 +18,7 @@ import DappReg from './dappreg';
|
||||
import Registry from './registry';
|
||||
import SignatureReg from './signaturereg';
|
||||
import TokenReg from './tokenreg';
|
||||
import GithubHint from './githubhint';
|
||||
|
||||
let instance = null;
|
||||
|
||||
@ -30,6 +31,7 @@ export default class Contracts {
|
||||
this._dappreg = new DappReg(api, this._registry);
|
||||
this._signaturereg = new SignatureReg(api, this._registry);
|
||||
this._tokenreg = new TokenReg(api, this._registry);
|
||||
this._githubhint = new GithubHint(api, this._registry);
|
||||
}
|
||||
|
||||
get registry () {
|
||||
@ -48,6 +50,10 @@ export default class Contracts {
|
||||
return this._tokenreg;
|
||||
}
|
||||
|
||||
get githubHint () {
|
||||
return this._githubhint;
|
||||
}
|
||||
|
||||
static create (api) {
|
||||
return new Contracts(api);
|
||||
}
|
||||
|
32
js/src/contracts/githubhint.js
Normal file
32
js/src/contracts/githubhint.js
Normal file
@ -0,0 +1,32 @@
|
||||
// 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/>.
|
||||
|
||||
export default class GithubHint {
|
||||
constructor (api, registry) {
|
||||
this._api = api;
|
||||
this._registry = registry;
|
||||
|
||||
this.getInstance();
|
||||
}
|
||||
|
||||
getContract () {
|
||||
return this._registry.getContract('githubhint');
|
||||
}
|
||||
|
||||
getInstance () {
|
||||
return this.getContract().instance;
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ export default class Registry {
|
||||
});
|
||||
}
|
||||
|
||||
getContractInstance (_name) {
|
||||
getContract (_name) {
|
||||
const name = _name.toLowerCase();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -54,13 +54,19 @@ export default class Registry {
|
||||
this
|
||||
.lookupAddress(name)
|
||||
.then((address) => {
|
||||
this._contracts[name] = this._api.newContract(abis[name], address).instance;
|
||||
this._contracts[name] = this._api.newContract(abis[name], address);
|
||||
resolve(this._contracts[name]);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
getContractInstance (_name) {
|
||||
return this
|
||||
.getContract(_name)
|
||||
.then((contract) => contract.instance);
|
||||
}
|
||||
|
||||
lookupAddress (_name) {
|
||||
const name = _name.toLowerCase();
|
||||
const sha3 = this._api.util.sha3(name);
|
||||
|
@ -22,8 +22,12 @@ export default class TokenReg {
|
||||
this.getInstance();
|
||||
}
|
||||
|
||||
getContract () {
|
||||
return this._registry.getContract('tokenreg');
|
||||
}
|
||||
|
||||
getInstance () {
|
||||
return this._registry.getContractInstance('tokenreg');
|
||||
return this.getContract().instance;
|
||||
}
|
||||
|
||||
tokenCount () {
|
||||
|
@ -46,11 +46,22 @@ a.link, a.link:hover, a.link:visited {
|
||||
}
|
||||
|
||||
.address {
|
||||
max-width: 250px;
|
||||
text-align: left;
|
||||
|
||||
div {
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
}
|
||||
|
||||
.description {
|
||||
text-align: left;
|
||||
|
||||
div {
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
.center {
|
||||
|
@ -58,6 +58,7 @@ export default class Event extends Component {
|
||||
<td className={ styles.description }>
|
||||
<div>{ isPending ? '' : coin.tla }</div>
|
||||
<div>{ isPending ? '' : coin.name }</div>
|
||||
<div>{ this.renderAddress(event.params.coin) }</div>
|
||||
</td>
|
||||
<td className={ styles.address }>
|
||||
{ this.renderAddress(event.params.owner) }
|
||||
|
@ -46,3 +46,9 @@
|
||||
.icon {
|
||||
margin: 0 0 -4px 1em;
|
||||
}
|
||||
|
||||
.byline {
|
||||
opacity: 0.75;
|
||||
font-size: 0.75em;
|
||||
padding-top: 0.25em;
|
||||
}
|
||||
|
@ -68,6 +68,9 @@ export default class Owner extends Component {
|
||||
<Token
|
||||
address={ token.address }
|
||||
tokenreg={ token.tokenreg } />
|
||||
<div className={ styles.byline }>
|
||||
{ token.address }
|
||||
</div>
|
||||
</div>
|
||||
));
|
||||
}
|
||||
|
@ -46,8 +46,6 @@ export const loadAccounts = () => (dispatch) => {
|
||||
address
|
||||
}));
|
||||
|
||||
console.log('accounts', accountsList);
|
||||
|
||||
dispatch(setAccounts(accountsList));
|
||||
dispatch(setAccountsInfo(accountsInfo));
|
||||
dispatch(setSelectedAccount(accountsList[0].address));
|
||||
|
@ -42,12 +42,9 @@ export default class QueryAction extends Component {
|
||||
|
||||
onClose: PropTypes.func.isRequired,
|
||||
handleQueryToken: PropTypes.func.isRequired,
|
||||
handleQueryMetaLookup: PropTypes.func.isRequired,
|
||||
|
||||
data: PropTypes.object,
|
||||
notFound: PropTypes.bool,
|
||||
metaLoading: PropTypes.bool,
|
||||
metaData: PropTypes.object
|
||||
notFound: PropTypes.bool
|
||||
}
|
||||
|
||||
state = initState;
|
||||
@ -131,10 +128,7 @@ export default class QueryAction extends Component {
|
||||
return (
|
||||
<Token
|
||||
fullWidth
|
||||
handleMetaLookup={ this.props.handleQueryMetaLookup }
|
||||
isMetaLoading={ this.props.metaLoading }
|
||||
meta={ this.props.metaData }
|
||||
{ ...data }
|
||||
tla={ data.tla }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
import { getTokenTotalSupply } from '../utils';
|
||||
|
||||
const { sha3, bytesToHex } = window.parity.api.util;
|
||||
|
||||
export const SET_REGISTER_SENDING = 'SET_REGISTER_SENDING';
|
||||
export const setRegisterSending = (isSending) => ({
|
||||
type: SET_REGISTER_SENDING,
|
||||
@ -41,8 +39,6 @@ export const registerCompleted = () => ({
|
||||
});
|
||||
|
||||
export const registerToken = (tokenData) => (dispatch, getState) => {
|
||||
console.log('registering token', tokenData);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
const fee = state.status.contract.fee;
|
||||
@ -83,8 +79,6 @@ export const registerToken = (tokenData) => (dispatch, getState) => {
|
||||
})
|
||||
.then((gasEstimate) => {
|
||||
options.gas = gasEstimate.mul(1.2).toFixed(0);
|
||||
console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
|
||||
|
||||
return contractInstance.register.postTransaction(options, values);
|
||||
})
|
||||
.then((result) => {
|
||||
@ -183,34 +177,3 @@ export const queryToken = (key, query) => (dispatch, getState) => {
|
||||
dispatch(setQueryLoading(false));
|
||||
});
|
||||
};
|
||||
|
||||
export const queryTokenMeta = (id, query) => (dispatch, getState) => {
|
||||
console.log('loading token meta', query);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
|
||||
const key = sha3(query);
|
||||
|
||||
const startDate = Date.now();
|
||||
dispatch(setQueryMetaLoading(true));
|
||||
|
||||
contractInstance
|
||||
.meta
|
||||
.call({}, [ id, key ])
|
||||
.then((value) => {
|
||||
const meta = {
|
||||
key, query,
|
||||
value: value.find(v => v !== 0) ? bytesToHex(value) : null
|
||||
};
|
||||
|
||||
dispatch(setQueryMeta(meta));
|
||||
|
||||
setTimeout(() => {
|
||||
dispatch(setQueryMetaLoading(false));
|
||||
}, 500 - (Date.now() - startDate));
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error('load meta query error', e);
|
||||
});
|
||||
};
|
||||
|
@ -37,7 +37,6 @@ export default class Actions extends Component {
|
||||
|
||||
handleQueryToken: PropTypes.func.isRequired,
|
||||
handleQueryClose: PropTypes.func.isRequired,
|
||||
handleQueryMetaLookup: PropTypes.func.isRequired,
|
||||
query: PropTypes.object.isRequired
|
||||
};
|
||||
|
||||
@ -82,7 +81,6 @@ export default class Actions extends Component {
|
||||
show={ this.state.show[ QUERY_ACTION ] }
|
||||
onClose={ this.onQueryClose }
|
||||
handleQueryToken={ this.props.handleQueryToken }
|
||||
handleQueryMetaLookup={ this.props.handleQueryMetaLookup }
|
||||
{ ...this.props.query } />
|
||||
</div>
|
||||
);
|
||||
|
@ -19,7 +19,7 @@ import { connect } from 'react-redux';
|
||||
|
||||
import Actions from './component';
|
||||
|
||||
import { registerToken, registerReset, queryToken, queryReset, queryTokenMeta } from './actions';
|
||||
import { registerToken, registerReset, queryToken, queryReset } from './actions';
|
||||
|
||||
class TokensContainer extends Component {
|
||||
|
||||
@ -49,9 +49,6 @@ const mapDispatchToProps = (dispatch) => {
|
||||
},
|
||||
handleQueryClose: () => {
|
||||
dispatch(queryReset());
|
||||
},
|
||||
handleQueryMetaLookup: (id, query) => {
|
||||
dispatch(queryTokenMeta(id, query));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -19,6 +19,7 @@
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding-bottom: 10em;
|
||||
}
|
||||
|
||||
.warning {
|
||||
|
@ -14,11 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import {
|
||||
registry as registryAbi,
|
||||
tokenreg as tokenregAbi,
|
||||
githubhint as githubhintAbi
|
||||
} from '../../../contracts/abi';
|
||||
import Contracts from '../../../contracts';
|
||||
|
||||
import { loadToken, setTokenPending, deleteToken, setTokenData } from '../Tokens/actions';
|
||||
|
||||
@ -34,43 +30,31 @@ export const FIND_CONTRACT = 'FIND_CONTRACT';
|
||||
export const loadContract = () => (dispatch) => {
|
||||
dispatch(setLoading(true));
|
||||
|
||||
api.parity
|
||||
.registryAddress()
|
||||
.then((registryAddress) => {
|
||||
console.log(`registry found at ${registryAddress}`);
|
||||
const registry = api.newContract(registryAbi, registryAddress).instance;
|
||||
|
||||
return Promise.all([
|
||||
registry.getAddress.call({}, [api.util.sha3('tokenreg'), 'A']),
|
||||
registry.getAddress.call({}, [api.util.sha3('githubhint'), 'A'])
|
||||
]);
|
||||
})
|
||||
.then(([ tokenregAddress, githubhintAddress ]) => {
|
||||
console.log(`tokenreg was found at ${tokenregAddress}`);
|
||||
|
||||
const tokenregContract = api
|
||||
.newContract(tokenregAbi, tokenregAddress);
|
||||
|
||||
const githubhintContract = api
|
||||
.newContract(githubhintAbi, githubhintAddress);
|
||||
const { tokenReg, githubHint } = new Contracts(api);
|
||||
|
||||
return Promise
|
||||
.all([
|
||||
tokenReg.getContract(),
|
||||
githubHint.getContract()
|
||||
])
|
||||
.then(([ tokenRegContract, githubHintContract ]) => {
|
||||
dispatch(setContractDetails({
|
||||
address: tokenregAddress,
|
||||
instance: tokenregContract.instance,
|
||||
raw: tokenregContract
|
||||
address: tokenRegContract.address,
|
||||
instance: tokenRegContract.instance,
|
||||
raw: tokenRegContract
|
||||
}));
|
||||
|
||||
dispatch(setGithubhintDetails({
|
||||
address: githubhintAddress,
|
||||
instance: githubhintContract.instance,
|
||||
raw: githubhintContract
|
||||
address: githubHintContract.address,
|
||||
instance: githubHintContract.instance,
|
||||
raw: githubHintContract
|
||||
}));
|
||||
|
||||
dispatch(loadContractDetails());
|
||||
dispatch(subscribeEvents());
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('loadContract error', error);
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
|
||||
@ -78,7 +62,7 @@ export const LOAD_CONTRACT_DETAILS = 'LOAD_CONTRACT_DETAILS';
|
||||
export const loadContractDetails = () => (dispatch, getState) => {
|
||||
const state = getState();
|
||||
|
||||
const instance = state.status.contract.instance;
|
||||
const { instance } = state.status.contract;
|
||||
|
||||
Promise
|
||||
.all([
|
||||
@ -87,8 +71,6 @@ export const loadContractDetails = () => (dispatch, getState) => {
|
||||
instance.fee.call()
|
||||
])
|
||||
.then(([accounts, owner, fee]) => {
|
||||
console.log(`owner as ${owner}, fee set at ${fee.toFormat()}`);
|
||||
|
||||
const isOwner = accounts.filter(a => a === owner).length > 0;
|
||||
|
||||
dispatch(setContractDetails({
|
||||
@ -119,14 +101,14 @@ export const setGithubhintDetails = (details) => ({
|
||||
export const subscribeEvents = () => (dispatch, getState) => {
|
||||
const state = getState();
|
||||
|
||||
const contract = state.status.contract.raw;
|
||||
const { raw } = state.status.contract;
|
||||
const previousSubscriptionId = state.status.subscriptionId;
|
||||
|
||||
if (previousSubscriptionId) {
|
||||
contract.unsubscribe(previousSubscriptionId);
|
||||
raw.unsubscribe(previousSubscriptionId);
|
||||
}
|
||||
|
||||
contract
|
||||
raw
|
||||
.subscribe(null, {
|
||||
fromBlock: 'latest',
|
||||
toBlock: 'pending',
|
||||
@ -187,7 +169,7 @@ export const subscribeEvents = () => (dispatch, getState) => {
|
||||
));
|
||||
}
|
||||
|
||||
console.log('new log event', log);
|
||||
console.warn('unknown log event', log);
|
||||
});
|
||||
})
|
||||
.then((subscriptionId) => {
|
||||
|
@ -25,17 +25,15 @@ const initialState = {
|
||||
isLoading: true,
|
||||
subscriptionId: null,
|
||||
contract: {
|
||||
addres: null,
|
||||
address: null,
|
||||
instance: null,
|
||||
raw: null,
|
||||
owner: null,
|
||||
isOwner: false,
|
||||
fee: null
|
||||
},
|
||||
githubhint: {
|
||||
address: null,
|
||||
instance: null,
|
||||
raw: null
|
||||
instance: null
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -14,4 +14,4 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
export default from './token';
|
||||
export default from './tokenContainer';
|
||||
|
@ -57,15 +57,28 @@ export default class Token extends Component {
|
||||
isLoading: PropTypes.bool,
|
||||
isPending: PropTypes.bool,
|
||||
isTokenOwner: PropTypes.bool.isRequired,
|
||||
isContractOwner: PropTypes.bool.isRequired,
|
||||
isContractOwner: PropTypes.bool,
|
||||
|
||||
fullWidth: PropTypes.bool
|
||||
};
|
||||
|
||||
state = {
|
||||
metaKeyIndex: 0
|
||||
static defaultProps = {
|
||||
isContractOwner: false
|
||||
};
|
||||
|
||||
state = {
|
||||
metaKeyIndex: 0,
|
||||
showMeta: false
|
||||
};
|
||||
|
||||
shouldComponentUpdate (nextProps) {
|
||||
if (nextProps.isLoading && this.props.isLoading) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
render () {
|
||||
const { isLoading, fullWidth } = this.props;
|
||||
|
||||
@ -237,7 +250,12 @@ export default class Token extends Component {
|
||||
}
|
||||
|
||||
renderMeta (meta) {
|
||||
const isMetaLoading = this.props.isMetaLoading;
|
||||
const { isMetaLoading } = this.props;
|
||||
const { showMeta } = this.state;
|
||||
|
||||
if (!showMeta) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isMetaLoading) {
|
||||
return (<div>
|
||||
@ -331,6 +349,7 @@ export default class Token extends Component {
|
||||
const key = metaDataKeys[keyIndex].value;
|
||||
const index = this.props.index;
|
||||
|
||||
this.setState({ showMeta: true });
|
||||
this.props.handleMetaLookup(index, key);
|
||||
}
|
||||
|
||||
|
73
js/src/dapps/tokenreg/Tokens/Token/tokenContainer.js
Normal file
73
js/src/dapps/tokenreg/Tokens/Token/tokenContainer.js
Normal file
@ -0,0 +1,73 @@
|
||||
// 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/>.
|
||||
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
import Token from './token';
|
||||
|
||||
import { queryTokenMeta, unregisterToken, addTokenMeta } from '../actions';
|
||||
|
||||
class TokenContainer extends Component {
|
||||
static propTypes = {
|
||||
handleMetaLookup: PropTypes.func.isRequired,
|
||||
handleUnregister: PropTypes.func.isRequired,
|
||||
handleAddMeta: PropTypes.func.isRequired,
|
||||
|
||||
tla: PropTypes.string.isRequired
|
||||
};
|
||||
|
||||
render () {
|
||||
return (
|
||||
<Token
|
||||
{ ...this.props }
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const mapStateToProps = (_, initProps) => {
|
||||
const { tla } = initProps;
|
||||
|
||||
return (state) => {
|
||||
const { isOwner } = state.status.contract;
|
||||
const { tokens } = state.tokens;
|
||||
const token = tokens.find((t) => t.tla === tla);
|
||||
|
||||
return { ...token, isContractOwner: isOwner };
|
||||
};
|
||||
};
|
||||
|
||||
const mapDispatchToProps = (dispatch) => {
|
||||
return {
|
||||
handleMetaLookup: (index, query) => {
|
||||
dispatch(queryTokenMeta(index, query));
|
||||
},
|
||||
|
||||
handleUnregister: (index) => {
|
||||
dispatch(unregisterToken(index));
|
||||
},
|
||||
|
||||
handleAddMeta: (index, key, value) => {
|
||||
dispatch(addTokenMeta(index, key, value));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(TokenContainer);
|
@ -67,8 +67,6 @@ export const deleteToken = (index) => ({
|
||||
});
|
||||
|
||||
export const loadTokens = () => (dispatch, getState) => {
|
||||
console.log('loading tokens...');
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
|
||||
@ -79,7 +77,6 @@ export const loadTokens = () => (dispatch, getState) => {
|
||||
.call()
|
||||
.then((count) => {
|
||||
const tokenCount = parseInt(count);
|
||||
console.log(`token count: ${tokenCount}`);
|
||||
dispatch(setTokenCount(tokenCount));
|
||||
|
||||
for (let i = 0; i < tokenCount; i++) {
|
||||
@ -94,8 +91,6 @@ export const loadTokens = () => (dispatch, getState) => {
|
||||
};
|
||||
|
||||
export const loadToken = (index) => (dispatch, getState) => {
|
||||
console.log('loading token', index);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
|
||||
@ -144,7 +139,7 @@ export const loadToken = (index) => (dispatch, getState) => {
|
||||
}
|
||||
|
||||
data.totalSupply = data.totalSupply.toNumber();
|
||||
console.log(`token loaded: #${index}`, data);
|
||||
|
||||
dispatch(setTokenData(index, data));
|
||||
dispatch(setTokenLoading(index, false));
|
||||
})
|
||||
@ -159,8 +154,6 @@ export const loadToken = (index) => (dispatch, getState) => {
|
||||
};
|
||||
|
||||
export const queryTokenMeta = (index, query) => (dispatch, getState) => {
|
||||
console.log('loading token meta', index, query);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
|
||||
@ -176,7 +169,6 @@ export const queryTokenMeta = (index, query) => (dispatch, getState) => {
|
||||
value: value.find(v => v !== 0) ? bytesToHex(value) : null
|
||||
};
|
||||
|
||||
console.log(`token meta loaded: #${index}`, value);
|
||||
dispatch(setTokenMeta(index, meta));
|
||||
|
||||
setTimeout(() => {
|
||||
@ -189,8 +181,6 @@ export const queryTokenMeta = (index, query) => (dispatch, getState) => {
|
||||
};
|
||||
|
||||
export const addTokenMeta = (index, key, value) => (dispatch, getState) => {
|
||||
console.log('add token meta', index, key, value);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
const token = state.tokens.tokens.find(t => t.index === index);
|
||||
@ -203,8 +193,6 @@ export const addTokenMeta = (index, key, value) => (dispatch, getState) => {
|
||||
.estimateGas(options, values)
|
||||
.then((gasEstimate) => {
|
||||
options.gas = gasEstimate.mul(1.2).toFixed(0);
|
||||
console.log(`addTokenMeta: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
|
||||
|
||||
return contractInstance.setMeta.postTransaction(options, values);
|
||||
})
|
||||
.catch((e) => {
|
||||
@ -213,8 +201,6 @@ export const addTokenMeta = (index, key, value) => (dispatch, getState) => {
|
||||
};
|
||||
|
||||
export const addGithubhintURL = (from, key, url) => (dispatch, getState) => {
|
||||
console.log('add githubhint url', key, url);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.githubhint.instance;
|
||||
|
||||
@ -227,8 +213,6 @@ export const addGithubhintURL = (from, key, url) => (dispatch, getState) => {
|
||||
.estimateGas(options, values)
|
||||
.then((gasEstimate) => {
|
||||
options.gas = gasEstimate.mul(1.2).toFixed(0);
|
||||
console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
|
||||
|
||||
return contractInstance.hintURL.postTransaction(options, values);
|
||||
})
|
||||
.catch((e) => {
|
||||
@ -237,24 +221,20 @@ export const addGithubhintURL = (from, key, url) => (dispatch, getState) => {
|
||||
};
|
||||
|
||||
export const unregisterToken = (index) => (dispatch, getState) => {
|
||||
console.log('unregistering token', index);
|
||||
|
||||
const state = getState();
|
||||
const contractInstance = state.status.contract.instance;
|
||||
const { contract } = getState().status;
|
||||
const { instance, owner } = contract;
|
||||
|
||||
const values = [ index ];
|
||||
const options = {
|
||||
from: state.accounts.selected.address
|
||||
from: owner
|
||||
};
|
||||
|
||||
contractInstance
|
||||
instance
|
||||
.unregister
|
||||
.estimateGas(options, values)
|
||||
.then((gasEstimate) => {
|
||||
options.gas = gasEstimate.mul(1.2).toFixed(0);
|
||||
console.log(`transfer: gas estimated as ${gasEstimate.toFixed(0)} setting to ${options.gas}`);
|
||||
|
||||
return contractInstance.unregister.postTransaction(options, values);
|
||||
return instance.unregister.postTransaction(options, values);
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(`unregisterToken #${index} error`, e);
|
||||
|
@ -19,16 +19,13 @@ import { connect } from 'react-redux';
|
||||
|
||||
import Tokens from './tokens';
|
||||
|
||||
import { loadTokens, queryTokenMeta, unregisterToken, addTokenMeta } from './actions';
|
||||
import { loadTokens } from './actions';
|
||||
|
||||
class TokensContainer extends Component {
|
||||
static propTypes = {
|
||||
isOwner: PropTypes.bool,
|
||||
isLoading: PropTypes.bool,
|
||||
tokens: PropTypes.array,
|
||||
tokenCount: PropTypes.number,
|
||||
onLoadTokens: PropTypes.func,
|
||||
accounts: PropTypes.array
|
||||
onLoadTokens: PropTypes.func
|
||||
};
|
||||
|
||||
componentDidMount () {
|
||||
@ -36,7 +33,6 @@ class TokensContainer extends Component {
|
||||
}
|
||||
|
||||
render () {
|
||||
console.log(this.props);
|
||||
return (
|
||||
<Tokens
|
||||
{ ...this.props }
|
||||
@ -46,30 +42,19 @@ class TokensContainer extends Component {
|
||||
}
|
||||
|
||||
const mapStateToProps = (state) => {
|
||||
const { list } = state.accounts;
|
||||
const { isLoading, tokens, tokenCount } = state.tokens;
|
||||
const { isLoading, tokens } = state.tokens;
|
||||
|
||||
const { isOwner } = state.status.contract;
|
||||
const filteredTokens = tokens
|
||||
.filter((token) => token && token.tla)
|
||||
.map((token) => ({ tla: token.tla, owner: token.owner }));
|
||||
|
||||
return { isLoading, tokens, tokenCount, isOwner, accounts: list };
|
||||
return { isLoading, tokens: filteredTokens };
|
||||
};
|
||||
|
||||
const mapDispatchToProps = (dispatch) => {
|
||||
return {
|
||||
onLoadTokens: () => {
|
||||
dispatch(loadTokens());
|
||||
},
|
||||
|
||||
handleMetaLookup: (index, query) => {
|
||||
dispatch(queryTokenMeta(index, query));
|
||||
},
|
||||
|
||||
handleUnregister: (index) => {
|
||||
dispatch(unregisterToken(index));
|
||||
},
|
||||
|
||||
handleAddMeta: (index, key, value) => {
|
||||
dispatch(addTokenMeta(index, key, value));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
@ -23,13 +23,8 @@ import styles from './tokens.css';
|
||||
|
||||
export default class Tokens extends Component {
|
||||
static propTypes = {
|
||||
handleAddMeta: PropTypes.func.isRequired,
|
||||
handleUnregister: PropTypes.func.isRequired,
|
||||
handleMetaLookup: PropTypes.func.isRequired,
|
||||
isOwner: PropTypes.bool.isRequired,
|
||||
isLoading: PropTypes.bool.isRequired,
|
||||
tokens: PropTypes.array,
|
||||
accounts: PropTypes.array
|
||||
tokens: PropTypes.array
|
||||
};
|
||||
|
||||
render () {
|
||||
@ -45,24 +40,12 @@ export default class Tokens extends Component {
|
||||
}
|
||||
|
||||
renderTokens (tokens) {
|
||||
const { accounts, isOwner } = this.props;
|
||||
|
||||
return tokens.map((token, index) => {
|
||||
if (!token || !token.tla) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const isTokenOwner = !!accounts.find((account) => account.address === token.owner);
|
||||
|
||||
return tokens.map((token) => {
|
||||
return (
|
||||
<Token
|
||||
{ ...token }
|
||||
handleUnregister={ this.props.handleUnregister }
|
||||
handleMetaLookup={ this.props.handleMetaLookup }
|
||||
handleAddMeta={ this.props.handleAddMeta }
|
||||
key={ index }
|
||||
isTokenOwner={ isTokenOwner }
|
||||
isContractOwner={ isOwner } />
|
||||
key={ token.tla }
|
||||
tla={ token.tla }
|
||||
/>
|
||||
);
|
||||
});
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import ReactDOM from 'react-dom';
|
||||
import injectTapEventPlugin from 'react-tap-event-plugin';
|
||||
import { createHashHistory } from 'history';
|
||||
import { Redirect, Router, Route, useRouterHistory } from 'react-router';
|
||||
import qs from 'querystring';
|
||||
|
||||
import SecureApi from './secureApi';
|
||||
import ContractInstances from './contracts';
|
||||
@ -45,6 +46,7 @@ import './index.html';
|
||||
|
||||
injectTapEventPlugin();
|
||||
|
||||
const AUTH_HASH = '#/auth?';
|
||||
const parityUrl = process.env.PARITY_URL ||
|
||||
(
|
||||
process.env.NODE_ENV === 'production'
|
||||
@ -52,7 +54,12 @@ const parityUrl = process.env.PARITY_URL ||
|
||||
: '127.0.0.1:8180'
|
||||
);
|
||||
|
||||
const api = new SecureApi(`ws://${parityUrl}`);
|
||||
let token = null;
|
||||
if (window.location.hash && window.location.hash.indexOf(AUTH_HASH) === 0) {
|
||||
token = qs.parse(window.location.hash.substr(AUTH_HASH.length)).token;
|
||||
}
|
||||
|
||||
const api = new SecureApi(`ws://${parityUrl}`, token);
|
||||
ContractInstances.create(api);
|
||||
|
||||
const store = initStore(api);
|
||||
@ -67,6 +74,7 @@ ReactDOM.render(
|
||||
<ContextProvider api={ api } muiTheme={ muiTheme } store={ store }>
|
||||
<Router className={ styles.reset } history={ routerHistory }>
|
||||
<Redirect from='/' to='/accounts' />
|
||||
<Redirect from='/auth' to='/accounts' query={ {} } />
|
||||
<Redirect from='/settings' to='/settings/views' />
|
||||
<Route path='/' component={ Application }>
|
||||
<Route path='accounts' component={ Accounts } />
|
||||
|
@ -227,6 +227,7 @@ export default class AddContract extends Component {
|
||||
onEditAbi = (abiIn) => {
|
||||
const { api } = this.context;
|
||||
const { abi, abiError, abiParsed } = validateAbi(abiIn, api);
|
||||
|
||||
this.setState({ abi, abiError, abiParsed });
|
||||
}
|
||||
|
||||
|
@ -15,10 +15,10 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
import { MenuItem } from 'material-ui';
|
||||
|
||||
import { AddressSelect, Form, Input, InputAddressSelect, Select } from '../../../ui';
|
||||
import { AddressSelect, Form, Input, TypedInput } from '../../../ui';
|
||||
import { validateAbi } from '../../../util/validation';
|
||||
import { parseAbiType } from '../../../util/abi';
|
||||
|
||||
import styles from '../deployContract.css';
|
||||
|
||||
@ -103,6 +103,7 @@ export default class DetailsStep extends Component {
|
||||
value={ code }
|
||||
onSubmit={ this.onCodeChange }
|
||||
readOnly={ readOnly } />
|
||||
|
||||
{ this.renderConstructorInputs() }
|
||||
</Form>
|
||||
);
|
||||
@ -117,59 +118,23 @@ export default class DetailsStep extends Component {
|
||||
}
|
||||
|
||||
return inputs.map((input, index) => {
|
||||
const onChange = (event, value) => this.onParamChange(index, value);
|
||||
const onChangeBool = (event, _index, value) => this.onParamChange(index, value === 'true');
|
||||
const onSubmit = (value) => this.onParamChange(index, value);
|
||||
const label = `${input.name}: ${input.type}`;
|
||||
let inputBox = null;
|
||||
const onChange = (value) => this.onParamChange(index, value);
|
||||
|
||||
switch (input.type) {
|
||||
case 'address':
|
||||
inputBox = (
|
||||
<InputAddressSelect
|
||||
accounts={ accounts }
|
||||
editing
|
||||
label={ label }
|
||||
value={ params[index] }
|
||||
error={ paramsError[index] }
|
||||
onChange={ onChange } />
|
||||
);
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
const boolitems = ['false', 'true'].map((bool) => {
|
||||
return (
|
||||
<MenuItem
|
||||
key={ bool }
|
||||
value={ bool }
|
||||
label={ bool }>{ bool }</MenuItem>
|
||||
);
|
||||
});
|
||||
inputBox = (
|
||||
<Select
|
||||
label={ label }
|
||||
value={ params[index] ? 'true' : 'false' }
|
||||
error={ paramsError[index] }
|
||||
onChange={ onChangeBool }>
|
||||
{ boolitems }
|
||||
</Select>
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
inputBox = (
|
||||
<Input
|
||||
label={ label }
|
||||
value={ params[index] }
|
||||
error={ paramsError[index] }
|
||||
onSubmit={ onSubmit } />
|
||||
);
|
||||
break;
|
||||
}
|
||||
const label = `${input.name ? `${input.name}: ` : ''}${input.type}`;
|
||||
const value = params[index];
|
||||
const error = paramsError[index];
|
||||
const param = parseAbiType(input.type);
|
||||
|
||||
return (
|
||||
<div key={ index } className={ styles.funcparams }>
|
||||
{ inputBox }
|
||||
<TypedInput
|
||||
label={ label }
|
||||
value={ value }
|
||||
error={ error }
|
||||
accounts={ accounts }
|
||||
onChange={ onChange }
|
||||
param={ param }
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@ -200,35 +165,14 @@ export default class DetailsStep extends Component {
|
||||
const { abiError, abiParsed } = validateAbi(abi, api);
|
||||
|
||||
if (!abiError) {
|
||||
const { inputs } = abiParsed.find((method) => method.type === 'constructor') || { inputs: [] };
|
||||
const { inputs } = abiParsed
|
||||
.find((method) => method.type === 'constructor') || { inputs: [] };
|
||||
|
||||
const params = [];
|
||||
|
||||
inputs.forEach((input) => {
|
||||
switch (input.type) {
|
||||
case 'address':
|
||||
params.push('0x');
|
||||
break;
|
||||
|
||||
case 'bool':
|
||||
params.push(false);
|
||||
break;
|
||||
|
||||
case 'bytes':
|
||||
params.push('0x');
|
||||
break;
|
||||
|
||||
case 'uint':
|
||||
params.push('0');
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
params.push('');
|
||||
break;
|
||||
|
||||
default:
|
||||
params.push('0');
|
||||
break;
|
||||
}
|
||||
const param = parseAbiType(input.type);
|
||||
params.push(param.default);
|
||||
});
|
||||
|
||||
onParamsChange(params);
|
||||
|
@ -101,7 +101,8 @@ export default class DeployContract extends Component {
|
||||
steps={ deployError ? null : steps }
|
||||
title={ deployError ? 'deployment failed' : null }
|
||||
waiting={ [1] }
|
||||
visible>
|
||||
visible
|
||||
scroll>
|
||||
{ this.renderStep() }
|
||||
</Modal>
|
||||
);
|
||||
@ -118,8 +119,22 @@ export default class DeployContract extends Component {
|
||||
onClick={ this.onClose } />
|
||||
);
|
||||
|
||||
const closeBtn = (
|
||||
<Button
|
||||
icon={ <ContentClear /> }
|
||||
label='Close'
|
||||
onClick={ this.onClose } />
|
||||
);
|
||||
|
||||
const closeBtnOk = (
|
||||
<Button
|
||||
icon={ <ActionDoneAll /> }
|
||||
label='Close'
|
||||
onClick={ this.onClose } />
|
||||
);
|
||||
|
||||
if (deployError) {
|
||||
return cancelBtn;
|
||||
return closeBtn;
|
||||
}
|
||||
|
||||
switch (step) {
|
||||
@ -134,17 +149,10 @@ export default class DeployContract extends Component {
|
||||
];
|
||||
|
||||
case 1:
|
||||
return [
|
||||
cancelBtn
|
||||
];
|
||||
return [ closeBtn ];
|
||||
|
||||
case 2:
|
||||
return [
|
||||
<Button
|
||||
icon={ <ActionDoneAll /> }
|
||||
label='Close'
|
||||
onClick={ this.onClose } />
|
||||
];
|
||||
return [ closeBtnOk ];
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,8 +285,6 @@ export default class DeployContract extends Component {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('onDeploymentState', data);
|
||||
|
||||
switch (data.state) {
|
||||
case 'estimateGas':
|
||||
case 'postTransaction':
|
||||
|
@ -314,7 +314,7 @@ export default class Transfer extends Component {
|
||||
}
|
||||
|
||||
const token = balance.tokens.find((balance) => balance.token.tag === tag).token;
|
||||
const s = new BigNumber(num).mul(token.format || 1).toString();
|
||||
const s = new BigNumber(num).mul(token.format || 1).toFixed();
|
||||
|
||||
if (s.indexOf('.') !== -1) {
|
||||
return ERRORS.invalidDecimals;
|
||||
@ -516,6 +516,13 @@ export default class Transfer extends Component {
|
||||
}
|
||||
|
||||
recalculateGas = () => {
|
||||
if (!this.isValid()) {
|
||||
this.setState({
|
||||
gas: '0'
|
||||
}, this.recalculate);
|
||||
return;
|
||||
}
|
||||
|
||||
(this.state.isEth
|
||||
? this._estimateGasEth()
|
||||
: this._estimateGasToken()
|
||||
|
@ -19,12 +19,13 @@ import Api from './api';
|
||||
const sysuiToken = window.localStorage.getItem('sysuiToken');
|
||||
|
||||
export default class SecureApi extends Api {
|
||||
constructor (url) {
|
||||
constructor (url, nextToken) {
|
||||
super(new Api.Transport.Ws(url, sysuiToken));
|
||||
|
||||
this._isConnecting = true;
|
||||
this._connectState = sysuiToken === 'initial' ? 1 : 0;
|
||||
this._needsToken = false;
|
||||
this._nextToken = nextToken;
|
||||
this._dappsPort = 8080;
|
||||
this._dappsInterface = null;
|
||||
this._signerPort = 8180;
|
||||
@ -57,7 +58,11 @@ export default class SecureApi extends Api {
|
||||
if (isConnected) {
|
||||
return this.connectSuccess();
|
||||
} else if (lastError) {
|
||||
this.updateToken('initial', 1);
|
||||
const nextToken = this._nextToken || 'initial';
|
||||
const nextState = this._nextToken ? 0 : 1;
|
||||
|
||||
this._nextToken = null;
|
||||
this.updateToken(nextToken, nextState);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
|
||||
import FileSaver from 'file-saver';
|
||||
import FileDownloadIcon from 'material-ui/svg-icons/file/file-download';
|
||||
|
||||
@ -38,19 +39,18 @@ class ActionbarExport extends Component {
|
||||
className={ className }
|
||||
icon={ <FileDownloadIcon /> }
|
||||
label='export'
|
||||
onClick={ this.onDownloadBackup } />
|
||||
onClick={ this.handleExport }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
onDownloadBackup = () => {
|
||||
handleExport = () => {
|
||||
const { filename, content } = this.props;
|
||||
|
||||
const text = (typeof content === 'string')
|
||||
? content
|
||||
: JSON.stringify(content, null, 4);
|
||||
const text = JSON.stringify(content, null, 4);
|
||||
|
||||
const blob = new Blob([ text ], { type: 'text/plain;charset=utf-8' });
|
||||
FileSaver.saveAs(blob, filename);
|
||||
const blob = new Blob([ text ], { type: 'application/json' });
|
||||
FileSaver.saveAs(blob, `${filename}.json`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,8 @@
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
import { observer } from 'mobx-react';
|
||||
|
||||
import IconMenu from 'material-ui/IconMenu';
|
||||
import MenuItem from 'material-ui/MenuItem';
|
||||
|
||||
@ -22,11 +24,15 @@ import SortIcon from 'material-ui/svg-icons/content/sort';
|
||||
|
||||
import { Button } from '../../';
|
||||
|
||||
import SortStore from './sortStore';
|
||||
import styles from './sort.css';
|
||||
|
||||
@observer
|
||||
export default class ActionbarSort extends Component {
|
||||
static propTypes = {
|
||||
id: PropTypes.string.isRequired,
|
||||
onChange: PropTypes.func.isRequired,
|
||||
|
||||
order: PropTypes.string,
|
||||
showDefault: PropTypes.bool,
|
||||
metas: PropTypes.array
|
||||
@ -37,8 +43,10 @@ export default class ActionbarSort extends Component {
|
||||
showDefault: true
|
||||
}
|
||||
|
||||
state = {
|
||||
menuOpen: false
|
||||
store = new SortStore(this.props);
|
||||
|
||||
componentDidMount () {
|
||||
this.store.restoreSavedOrder();
|
||||
}
|
||||
|
||||
render () {
|
||||
@ -51,12 +59,12 @@ export default class ActionbarSort extends Component {
|
||||
className={ styles.sortButton }
|
||||
label=''
|
||||
icon={ <SortIcon /> }
|
||||
onClick={ this.handleMenuOpen }
|
||||
onClick={ this.store.handleMenuOpen }
|
||||
/>
|
||||
}
|
||||
open={ this.state.menuOpen }
|
||||
onRequestChange={ this.handleMenuChange }
|
||||
onItemTouchTap={ this.handleSortChange }
|
||||
open={ this.store.menuOpen }
|
||||
onRequestChange={ this.store.handleMenuChange }
|
||||
onItemTouchTap={ this.store.handleSortChange }
|
||||
targetOrigin={ { horizontal: 'right', vertical: 'top' } }
|
||||
anchorOrigin={ { horizontal: 'right', vertical: 'top' } }
|
||||
touchTapCloseDelay={ 0 }
|
||||
@ -109,16 +117,4 @@ export default class ActionbarSort extends Component {
|
||||
);
|
||||
}
|
||||
|
||||
handleSortChange = (event, child) => {
|
||||
const order = child.props.value;
|
||||
this.props.onChange(order);
|
||||
}
|
||||
|
||||
handleMenuOpen = () => {
|
||||
this.setState({ menuOpen: true });
|
||||
}
|
||||
|
||||
handleMenuChange = (open) => {
|
||||
this.setState({ menuOpen: open });
|
||||
}
|
||||
}
|
||||
|
71
js/src/ui/Actionbar/Sort/sortStore.js
Normal file
71
js/src/ui/Actionbar/Sort/sortStore.js
Normal file
@ -0,0 +1,71 @@
|
||||
// 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/>.
|
||||
|
||||
import { action, observable } from 'mobx';
|
||||
import store from 'store';
|
||||
|
||||
const LS_STORE_KEY = '_parity::sortStore';
|
||||
|
||||
export default class SortStore {
|
||||
@observable menuOpen = false;
|
||||
|
||||
constructor (props) {
|
||||
const { id, onChange } = props;
|
||||
|
||||
this.onChange = onChange;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@action handleMenuOpen = () => {
|
||||
this.menuOpen = true;
|
||||
}
|
||||
|
||||
@action handleMenuChange = (open) => {
|
||||
this.menuOpen = open;
|
||||
}
|
||||
|
||||
@action handleSortChange = (event, child) => {
|
||||
const order = child.props.value;
|
||||
this.onChange(order);
|
||||
this.saveOrder(order);
|
||||
}
|
||||
|
||||
@action restoreSavedOrder = () => {
|
||||
const order = this.getSavedOrder();
|
||||
this.onChange(order);
|
||||
}
|
||||
|
||||
getSavedOrder = () => {
|
||||
return (this.getSavedOrders())[this.id];
|
||||
}
|
||||
|
||||
getSavedOrders = () => {
|
||||
return store.get(LS_STORE_KEY) || {};
|
||||
}
|
||||
|
||||
setSavedOrders = (orders) => {
|
||||
store.set(LS_STORE_KEY, orders);
|
||||
}
|
||||
|
||||
saveOrder = (order) => {
|
||||
const orders = {
|
||||
...this.getSavedOrders(),
|
||||
[ this.id ]: order
|
||||
};
|
||||
|
||||
this.setSavedOrders(orders);
|
||||
}
|
||||
}
|
@ -39,6 +39,10 @@
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 35px;
|
||||
|
||||
&.noLabel {
|
||||
top: 11px;
|
||||
}
|
||||
}
|
||||
|
||||
.paddedInput input {
|
||||
|
@ -106,15 +106,21 @@ export default class AddressSelect extends Component {
|
||||
}
|
||||
|
||||
renderIdentityIcon (inputValue) {
|
||||
const { error, value } = this.props;
|
||||
const { error, value, label } = this.props;
|
||||
|
||||
if (error || !inputValue || value.length !== 42) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const classes = [ styles.icon ];
|
||||
|
||||
if (!label) {
|
||||
classes.push(styles.noLabel);
|
||||
}
|
||||
|
||||
return (
|
||||
<IdentityIcon
|
||||
className={ styles.icon }
|
||||
className={ classes.join(' ') }
|
||||
inline center
|
||||
address={ value } />
|
||||
);
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
import { TextField } from 'material-ui';
|
||||
import { noop } from 'lodash';
|
||||
|
||||
import CopyToClipboard from '../../CopyToClipboard';
|
||||
|
||||
@ -63,7 +64,9 @@ export default class Input extends Component {
|
||||
hideUnderline: PropTypes.bool,
|
||||
value: PropTypes.oneOfType([
|
||||
PropTypes.number, PropTypes.string
|
||||
])
|
||||
]),
|
||||
min: PropTypes.any,
|
||||
max: PropTypes.any
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
@ -79,14 +82,14 @@ export default class Input extends Component {
|
||||
}
|
||||
|
||||
componentWillReceiveProps (newProps) {
|
||||
if (newProps.value !== this.props.value) {
|
||||
if ((newProps.value !== this.props.value) && (newProps.value !== this.state.value)) {
|
||||
this.setValue(newProps.value);
|
||||
}
|
||||
}
|
||||
|
||||
render () {
|
||||
const { value } = this.state;
|
||||
const { children, className, hideUnderline, disabled, error, label, hint, multiLine, rows, type } = this.props;
|
||||
const { children, className, hideUnderline, disabled, error, label, hint, multiLine, rows, type, min, max } = this.props;
|
||||
|
||||
const readOnly = this.props.readOnly || disabled;
|
||||
|
||||
@ -129,7 +132,10 @@ export default class Input extends Component {
|
||||
onBlur={ this.onBlur }
|
||||
onChange={ this.onChange }
|
||||
onKeyDown={ this.onKeyDown }
|
||||
onPaste={ this.onPaste }
|
||||
inputStyle={ inputStyle }
|
||||
min={ min }
|
||||
max={ max }
|
||||
>
|
||||
{ children }
|
||||
</TextField>
|
||||
@ -138,7 +144,7 @@ export default class Input extends Component {
|
||||
}
|
||||
|
||||
renderCopyButton () {
|
||||
const { allowCopy, hideUnderline, label, hint, floatCopy } = this.props;
|
||||
const { allowCopy, label, hint, floatCopy } = this.props;
|
||||
const { value } = this.state;
|
||||
|
||||
if (!allowCopy) {
|
||||
@ -153,7 +159,7 @@ export default class Input extends Component {
|
||||
? allowCopy
|
||||
: value;
|
||||
|
||||
if (hideUnderline && !label) {
|
||||
if (!label) {
|
||||
style.marginBottom = 2;
|
||||
} else if (label && !hint) {
|
||||
style.marginBottom = 4;
|
||||
@ -176,9 +182,10 @@ export default class Input extends Component {
|
||||
}
|
||||
|
||||
onChange = (event, value) => {
|
||||
this.setValue(value);
|
||||
|
||||
event.persist();
|
||||
this.setValue(value, () => {
|
||||
this.props.onChange && this.props.onChange(event, value);
|
||||
});
|
||||
}
|
||||
|
||||
onBlur = (event) => {
|
||||
@ -192,6 +199,14 @@ export default class Input extends Component {
|
||||
this.props.onBlur && this.props.onBlur(event);
|
||||
}
|
||||
|
||||
onPaste = (event) => {
|
||||
const value = event.clipboardData.getData('Text');
|
||||
|
||||
window.setTimeout(() => {
|
||||
this.onSubmit(value);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
onKeyDown = (event) => {
|
||||
const { value } = event.target;
|
||||
|
||||
@ -205,12 +220,12 @@ export default class Input extends Component {
|
||||
}
|
||||
|
||||
onSubmit = (value) => {
|
||||
this.setValue(value);
|
||||
|
||||
this.setValue(value, () => {
|
||||
this.props.onSubmit && this.props.onSubmit(value);
|
||||
});
|
||||
}
|
||||
|
||||
setValue (value) {
|
||||
this.setState({ value });
|
||||
setValue (value, cb = noop) {
|
||||
this.setState({ value }, cb);
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
.input input {
|
||||
padding-left: 48px !important;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.inputEmpty input {
|
||||
@ -30,6 +31,10 @@
|
||||
.iconDisabled {
|
||||
position: absolute;
|
||||
top: 35px;
|
||||
|
||||
&.noLabel {
|
||||
top: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
.icon {
|
||||
|
@ -69,14 +69,20 @@ class InputAddress extends Component {
|
||||
}
|
||||
|
||||
renderIcon () {
|
||||
const { value, disabled } = this.props;
|
||||
const { value, disabled, label } = this.props;
|
||||
|
||||
if (!value || !value.length || !util.isAddressValid(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const classes = [disabled ? styles.iconDisabled : styles.icon];
|
||||
|
||||
if (!label) {
|
||||
classes.push(styles.noLabel);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={ disabled ? styles.iconDisabled : styles.icon }>
|
||||
<div className={ classes.join(' ') }>
|
||||
<IdentityIcon
|
||||
inline center
|
||||
address={ value } />
|
||||
|
17
js/src/ui/Form/TypedInput/index.js
Normal file
17
js/src/ui/Form/TypedInput/index.js
Normal file
@ -0,0 +1,17 @@
|
||||
// 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/>.
|
||||
|
||||
export default from './typedInput';
|
31
js/src/ui/Form/TypedInput/typedInput.css
Normal file
31
js/src/ui/Form/TypedInput/typedInput.css
Normal file
@ -0,0 +1,31 @@
|
||||
/* 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/>.
|
||||
*/
|
||||
|
||||
.inputs {
|
||||
padding-top: 2px;
|
||||
overflow-x: hidden;
|
||||
|
||||
label {
|
||||
line-height: 22px;
|
||||
pointer-events: none;
|
||||
color: rgba(255, 255, 255, 0.498039);
|
||||
-webkit-user-select: none;
|
||||
font-size: 12px;
|
||||
top: 11px;
|
||||
position: relative;
|
||||
}
|
||||
}
|
239
js/src/ui/Form/TypedInput/typedInput.js
Normal file
239
js/src/ui/Form/TypedInput/typedInput.js
Normal file
@ -0,0 +1,239 @@
|
||||
// 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/>.
|
||||
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
import { MenuItem } from 'material-ui';
|
||||
import { range } from 'lodash';
|
||||
|
||||
import IconButton from 'material-ui/IconButton';
|
||||
import AddIcon from 'material-ui/svg-icons/content/add';
|
||||
import RemoveIcon from 'material-ui/svg-icons/content/remove';
|
||||
|
||||
import { Input, InputAddressSelect, Select } from '../../../ui';
|
||||
import { ABI_TYPES } from '../../../util/abi';
|
||||
|
||||
import styles from './typedInput.css';
|
||||
|
||||
export default class TypedInput extends Component {
|
||||
|
||||
static propTypes = {
|
||||
onChange: PropTypes.func.isRequired,
|
||||
accounts: PropTypes.object.isRequired,
|
||||
param: PropTypes.object.isRequired,
|
||||
|
||||
error: PropTypes.any,
|
||||
value: PropTypes.any,
|
||||
label: PropTypes.string
|
||||
};
|
||||
|
||||
render () {
|
||||
const { param } = this.props;
|
||||
const { type } = param;
|
||||
|
||||
if (type === ABI_TYPES.ARRAY) {
|
||||
const { accounts, label, value = param.default } = this.props;
|
||||
const { subtype, length } = param;
|
||||
|
||||
const fixedLength = !!length;
|
||||
|
||||
const inputs = range(length || value.length).map((_, index) => {
|
||||
const onChange = (inputValue) => {
|
||||
const newValues = [].concat(this.props.value);
|
||||
newValues[index] = inputValue;
|
||||
this.props.onChange(newValues);
|
||||
};
|
||||
|
||||
return (
|
||||
<TypedInput
|
||||
key={ `${subtype.type}_${index}` }
|
||||
onChange={ onChange }
|
||||
accounts={ accounts }
|
||||
param={ subtype }
|
||||
value={ value[index] }
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
<div className={ styles.inputs }>
|
||||
<label>{ label }</label>
|
||||
{ fixedLength ? null : this.renderLength() }
|
||||
{ inputs }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return this.renderType(type);
|
||||
}
|
||||
|
||||
renderLength () {
|
||||
const iconStyle = {
|
||||
width: 16,
|
||||
height: 16
|
||||
};
|
||||
|
||||
const style = {
|
||||
width: 32,
|
||||
height: 32,
|
||||
padding: 0
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<IconButton
|
||||
iconStyle={ iconStyle }
|
||||
style={ style }
|
||||
onClick={ this.onAddField }
|
||||
>
|
||||
<AddIcon />
|
||||
</IconButton>
|
||||
|
||||
<IconButton
|
||||
iconStyle={ iconStyle }
|
||||
style={ style }
|
||||
onClick={ this.onRemoveField }
|
||||
>
|
||||
<RemoveIcon />
|
||||
</IconButton>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
renderType (type) {
|
||||
if (type === ABI_TYPES.ADDRESS) {
|
||||
return this.renderAddress();
|
||||
}
|
||||
|
||||
if (type === ABI_TYPES.BOOL) {
|
||||
return this.renderBoolean();
|
||||
}
|
||||
|
||||
if (type === ABI_TYPES.STRING) {
|
||||
return this.renderDefault();
|
||||
}
|
||||
|
||||
if (type === ABI_TYPES.BYTES) {
|
||||
return this.renderDefault();
|
||||
}
|
||||
|
||||
if (type === ABI_TYPES.INT) {
|
||||
return this.renderNumber();
|
||||
}
|
||||
|
||||
if (type === ABI_TYPES.FIXED) {
|
||||
return this.renderNumber();
|
||||
}
|
||||
|
||||
return this.renderDefault();
|
||||
}
|
||||
|
||||
renderNumber () {
|
||||
const { label, value, error, param } = this.props;
|
||||
|
||||
return (
|
||||
<Input
|
||||
label={ label }
|
||||
value={ value }
|
||||
error={ error }
|
||||
onSubmit={ this.onSubmit }
|
||||
type='number'
|
||||
min={ param.signed ? null : 0 }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
renderDefault () {
|
||||
const { label, value, error } = this.props;
|
||||
|
||||
return (
|
||||
<Input
|
||||
label={ label }
|
||||
value={ value }
|
||||
error={ error }
|
||||
onSubmit={ this.onSubmit }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
renderAddress () {
|
||||
const { accounts, label, value, error } = this.props;
|
||||
|
||||
return (
|
||||
<InputAddressSelect
|
||||
accounts={ accounts }
|
||||
label={ label }
|
||||
value={ value }
|
||||
error={ error }
|
||||
onChange={ this.onChange }
|
||||
editing
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
renderBoolean () {
|
||||
const { label, value, error } = this.props;
|
||||
|
||||
const boolitems = ['false', 'true'].map((bool) => {
|
||||
return (
|
||||
<MenuItem
|
||||
key={ bool }
|
||||
value={ bool }
|
||||
label={ bool }
|
||||
>
|
||||
{ bool }
|
||||
</MenuItem>
|
||||
);
|
||||
});
|
||||
|
||||
return (
|
||||
<Select
|
||||
label={ label }
|
||||
value={ value ? 'true' : 'false' }
|
||||
error={ error }
|
||||
onChange={ this.onChangeBool }
|
||||
>
|
||||
{ boolitems }
|
||||
</Select>
|
||||
);
|
||||
}
|
||||
|
||||
onChangeBool = (event, _index, value) => {
|
||||
this.props.onChange(value === 'true');
|
||||
}
|
||||
|
||||
onChange = (event, value) => {
|
||||
this.props.onChange(value);
|
||||
}
|
||||
|
||||
onSubmit = (value) => {
|
||||
this.props.onChange(value);
|
||||
}
|
||||
|
||||
onAddField = () => {
|
||||
const { value, onChange, param } = this.props;
|
||||
const newValues = [].concat(value, param.subtype.default);
|
||||
|
||||
onChange(newValues);
|
||||
}
|
||||
|
||||
onRemoveField = () => {
|
||||
const { value, onChange } = this.props;
|
||||
const newValues = value.slice(0, -1);
|
||||
|
||||
onChange(newValues);
|
||||
}
|
||||
|
||||
}
|
@ -16,6 +16,7 @@
|
||||
|
||||
import AddressSelect from './AddressSelect';
|
||||
import FormWrap from './FormWrap';
|
||||
import TypedInput from './TypedInput';
|
||||
import Input from './Input';
|
||||
import InputAddress from './InputAddress';
|
||||
import InputAddressSelect from './InputAddressSelect';
|
||||
@ -27,6 +28,7 @@ export default from './form';
|
||||
export {
|
||||
AddressSelect,
|
||||
FormWrap,
|
||||
TypedInput,
|
||||
Input,
|
||||
InputAddress,
|
||||
InputAddressSelect,
|
||||
|
@ -18,6 +18,12 @@
|
||||
.container {
|
||||
}
|
||||
|
||||
.loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.details,
|
||||
.gasDetails {
|
||||
color: #aaa;
|
||||
@ -46,7 +52,7 @@
|
||||
.highlight {
|
||||
}
|
||||
|
||||
.inputs {
|
||||
.inputs, .addressContainer {
|
||||
padding-left: 2em;
|
||||
}
|
||||
|
||||
@ -73,3 +79,7 @@
|
||||
margin-bottom: -10px;
|
||||
margin-right: 0.5em;
|
||||
}
|
||||
|
||||
.inputData {
|
||||
word-break: break-all;
|
||||
}
|
||||
|
@ -18,14 +18,14 @@ import BigNumber from 'bignumber.js';
|
||||
import React, { Component, PropTypes } from 'react';
|
||||
import { connect } from 'react-redux';
|
||||
import { bindActionCreators } from 'redux';
|
||||
import CircularProgress from 'material-ui/CircularProgress';
|
||||
|
||||
import Contracts from '../../contracts';
|
||||
import IdentityIcon from '../IdentityIcon';
|
||||
import IdentityName from '../IdentityName';
|
||||
import { Input, InputAddress } from '../Form';
|
||||
|
||||
import styles from './methodDecoding.css';
|
||||
|
||||
const ASCII_INPUT = /^[a-z0-9\s,?;.:/!()-_@'"#]+$/i;
|
||||
const CONTRACT_CREATE = '0x60606040';
|
||||
const TOKEN_METHODS = {
|
||||
'0xa9059cbb': 'transfer(to,value)'
|
||||
@ -53,20 +53,36 @@ class MethodDecoding extends Component {
|
||||
token: null,
|
||||
isContract: false,
|
||||
isDeploy: false,
|
||||
isReceived: false
|
||||
isReceived: false,
|
||||
isLoading: true
|
||||
}
|
||||
|
||||
componentWillMount () {
|
||||
this.lookup();
|
||||
const lookupResult = this.lookup();
|
||||
|
||||
if (typeof lookupResult === 'object' && typeof lookupResult.then === 'function') {
|
||||
lookupResult.then(() => this.setState({ isLoading: false }));
|
||||
} else {
|
||||
this.setState({ isLoading: false });
|
||||
}
|
||||
}
|
||||
|
||||
render () {
|
||||
const { transaction } = this.props;
|
||||
const { isLoading } = this.state;
|
||||
|
||||
if (!transaction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className={ styles.loading }>
|
||||
<CircularProgress size={ 60 } thickness={ 2 } />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={ styles.container }>
|
||||
{ this.renderAction() }
|
||||
@ -82,26 +98,33 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<div className={ styles.gasDetails }>
|
||||
{ historic ? 'Provided' : 'Provides' } <span className={ styles.highlight }>{ gas.toFormat(0) } gas ({ gasPrice.div(1000000).toFormat(0) }M/<small>ETH</small>)</span> for a total transaction value of <span className={ styles.highlight }>{ this.renderEtherValue(gasValue) }</span>
|
||||
<span>{ historic ? 'Provided' : 'Provides' } </span>
|
||||
<span className={ styles.highlight }>
|
||||
{ gas.toFormat(0) } gas ({ gasPrice.div(1000000).toFormat(0) }M/<small>ETH</small>)
|
||||
</span>
|
||||
<span> for a total transaction value of </span>
|
||||
<span className={ styles.highlight }>{ this.renderEtherValue(gasValue) }</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
renderAction () {
|
||||
const { methodName, methodInputs, methodSignature, token, isDeploy, isReceived } = this.state;
|
||||
const { methodName, methodInputs, methodSignature, token, isDeploy, isReceived, isContract } = this.state;
|
||||
|
||||
if (isDeploy) {
|
||||
return this.renderDeploy();
|
||||
}
|
||||
|
||||
if (methodSignature) {
|
||||
if (isContract && methodSignature) {
|
||||
if (token && TOKEN_METHODS[methodSignature] && methodInputs) {
|
||||
return this.renderTokenAction();
|
||||
}
|
||||
|
||||
return methodName
|
||||
? this.renderSignatureMethod()
|
||||
: this.renderUnknownMethod();
|
||||
if (methodName) {
|
||||
return this.renderSignatureMethod();
|
||||
}
|
||||
|
||||
return this.renderUnknownMethod();
|
||||
}
|
||||
|
||||
return isReceived
|
||||
@ -109,6 +132,28 @@ class MethodDecoding extends Component {
|
||||
: this.renderValueTransfer();
|
||||
}
|
||||
|
||||
renderInputValue () {
|
||||
const { api } = this.context;
|
||||
const { transaction } = this.props;
|
||||
|
||||
if (!/^(0x)?([0]*[1-9a-f]+[0]*)+$/.test(transaction.input)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const ascii = api.util.hex2Ascii(transaction.input);
|
||||
|
||||
const text = ASCII_INPUT.test(ascii)
|
||||
? ascii
|
||||
: transaction.input;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<span>with the input </span>
|
||||
<code className={ styles.inputData }>{ text }</code>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
renderTokenAction () {
|
||||
const { historic } = this.props;
|
||||
const { methodSignature, methodInputs } = this.state;
|
||||
@ -120,7 +165,15 @@ class MethodDecoding extends Component {
|
||||
default:
|
||||
return (
|
||||
<div className={ styles.details }>
|
||||
{ historic ? 'Transferred' : 'Will transfer' } <span className={ styles.highlight }>{ this.renderTokenValue(value.value) }</span> to <span className={ styles.highlight }>{ this.renderAddressName(address) }</span>.
|
||||
<div>
|
||||
<span>{ historic ? 'Transferred' : 'Will transfer' } </span>
|
||||
<span className={ styles.highlight }>
|
||||
{ this.renderTokenValue(value.value) }
|
||||
</span>
|
||||
<span> to </span>
|
||||
</div>
|
||||
|
||||
{ this.renderAddressName(address) }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -139,7 +192,11 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<div className={ styles.details }>
|
||||
Deployed a contract at address <span className={ styles.highlight }>{ this.renderAddressName(transaction.creates, false) }</span>
|
||||
<div>
|
||||
<span>Deployed a contract at address </span>
|
||||
</div>
|
||||
|
||||
{ this.renderAddressName(transaction.creates, false) }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -150,7 +207,16 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<div className={ styles.details }>
|
||||
{ historic ? 'Received' : 'Will receive' } <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span> from { isContract ? 'the contract' : '' } <span className={ styles.highlight }>{ this.renderAddressName(transaction.from) }</span>
|
||||
<div>
|
||||
<span>{ historic ? 'Received' : 'Will receive' } </span>
|
||||
<span className={ styles.highlight }>
|
||||
{ this.renderEtherValue(transaction.value) }
|
||||
</span>
|
||||
<span> from { isContract ? 'the contract' : '' } </span>
|
||||
</div>
|
||||
|
||||
{ this.renderAddressName(transaction.from) }
|
||||
{ this.renderInputValue() }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -161,19 +227,44 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<div className={ styles.details }>
|
||||
{ historic ? 'Transferred' : 'Will transfer' } <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span> to { isContract ? 'the contract' : '' } <span className={ styles.highlight }>{ this.renderAddressName(transaction.to) }</span>
|
||||
<div>
|
||||
<span>{ historic ? 'Transferred' : 'Will transfer' } </span>
|
||||
<span className={ styles.highlight }>
|
||||
{ this.renderEtherValue(transaction.value) }
|
||||
</span>
|
||||
<span> to { isContract ? 'the contract' : '' } </span>
|
||||
</div>
|
||||
|
||||
{ this.renderAddressName(transaction.to) }
|
||||
{ this.renderInputValue() }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
renderSignatureMethod () {
|
||||
const { historic, transaction } = this.props;
|
||||
const { methodName } = this.state;
|
||||
const { methodName, methodInputs } = this.state;
|
||||
|
||||
return (
|
||||
<div className={ styles.details }>
|
||||
<div className={ styles.description }>
|
||||
{ historic ? 'Executed' : 'Will execute' } the <span className={ styles.name }>{ methodName }</span> function on the contract <span className={ styles.highlight }>{ this.renderAddressName(transaction.to) }</span>, transferring <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span>, passing the following parameters:
|
||||
<div>
|
||||
<span>{ historic ? 'Executed' : 'Will execute' } the </span>
|
||||
<span className={ styles.name }>{ methodName }</span>
|
||||
<span> function on the contract </span>
|
||||
</div>
|
||||
|
||||
{ this.renderAddressName(transaction.to) }
|
||||
|
||||
<div>
|
||||
<span>transferring </span>
|
||||
<span className={ styles.highlight }>
|
||||
{ this.renderEtherValue(transaction.value) }
|
||||
</span>
|
||||
<span>
|
||||
{ methodInputs.length ? ', passing the following parameters:' : '.' }
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className={ styles.inputs }>
|
||||
{ this.renderInputs() }
|
||||
@ -187,7 +278,21 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<div className={ styles.details }>
|
||||
{ historic ? 'Executed' : 'Will execute' } <span className={ styles.name }>an unknown/unregistered</span> method on the contract <span className={ styles.highlight }>{ this.renderAddressName(transaction.to) }</span>, transferring <span className={ styles.highlight }>{ this.renderEtherValue(transaction.value) }</span>.
|
||||
<div>
|
||||
<span>{ historic ? 'Executed' : 'Will execute' } </span>
|
||||
<span className={ styles.name }>an unknown/unregistered</span>
|
||||
<span> method on the contract </span>
|
||||
</div>
|
||||
|
||||
{ this.renderAddressName(transaction.to) }
|
||||
|
||||
<div>
|
||||
<span>transferring </span>
|
||||
<span className={ styles.highlight }>
|
||||
{ this.renderEtherValue(transaction.value) }
|
||||
</span>
|
||||
<span>.</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -239,7 +344,7 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<span className={ styles.tokenValue }>
|
||||
{ value.div(token.format).toFormat(5) }<small>{ token.tag }</small>
|
||||
{ value.div(token.format).toFormat(5) }<small> { token.tag }</small>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@ -250,17 +355,21 @@ class MethodDecoding extends Component {
|
||||
|
||||
return (
|
||||
<span className={ styles.etherValue }>
|
||||
{ ether.toFormat(5) }<small>ETH</small>
|
||||
{ ether.toFormat(5) }<small> ETH</small>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
renderAddressName (address, withName = true) {
|
||||
return (
|
||||
<span className={ styles.address }>
|
||||
<IdentityIcon center inline address={ address } className={ styles.identityicon } />
|
||||
{ withName ? <IdentityName address={ address } /> : address }
|
||||
</span>
|
||||
<div className={ styles.addressContainer }>
|
||||
<InputAddress
|
||||
disabled
|
||||
className={ styles.address }
|
||||
value={ address }
|
||||
text={ withName }
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@ -284,6 +393,21 @@ class MethodDecoding extends Component {
|
||||
return;
|
||||
}
|
||||
|
||||
if (contractAddress === '0x') {
|
||||
return;
|
||||
}
|
||||
|
||||
return api.eth
|
||||
.getCode(contractAddress || transaction.creates)
|
||||
.then((bytecode) => {
|
||||
const isContract = bytecode && /^(0x)?([0]*[1-9a-f]+[0]*)+$/.test(bytecode);
|
||||
|
||||
this.setState({ isContract });
|
||||
|
||||
if (!isContract) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { signature, paramdata } = api.util.decodeCallData(transaction.input);
|
||||
this.setState({ methodSignature: signature, methodParams: paramdata });
|
||||
|
||||
@ -292,12 +416,10 @@ class MethodDecoding extends Component {
|
||||
return;
|
||||
}
|
||||
|
||||
Promise
|
||||
.all([
|
||||
api.eth.getCode(contractAddress),
|
||||
Contracts.get().signatureReg.lookup(signature)
|
||||
])
|
||||
.then(([bytecode, method]) => {
|
||||
return Contracts.get()
|
||||
.signatureReg
|
||||
.lookup(signature)
|
||||
.then((method) => {
|
||||
let methodInputs = null;
|
||||
let methodName = null;
|
||||
|
||||
@ -319,8 +441,8 @@ class MethodDecoding extends Component {
|
||||
method,
|
||||
methodName,
|
||||
methodInputs,
|
||||
bytecode,
|
||||
isContract: bytecode && bytecode !== '0x'
|
||||
bytecode
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
|
@ -29,7 +29,7 @@ import ContextProvider from './ContextProvider';
|
||||
import CopyToClipboard from './CopyToClipboard';
|
||||
import Editor from './Editor';
|
||||
import Errors from './Errors';
|
||||
import Form, { AddressSelect, FormWrap, Input, InputAddress, InputAddressSelect, InputChip, InputInline, Select } from './Form';
|
||||
import Form, { AddressSelect, FormWrap, TypedInput, Input, InputAddress, InputAddressSelect, InputChip, InputInline, Select } from './Form';
|
||||
import IdentityIcon from './IdentityIcon';
|
||||
import IdentityName from './IdentityName';
|
||||
import MethodDecoding from './MethodDecoding';
|
||||
@ -62,6 +62,7 @@ export {
|
||||
Errors,
|
||||
Form,
|
||||
FormWrap,
|
||||
TypedInput,
|
||||
Input,
|
||||
InputAddress,
|
||||
InputAddressSelect,
|
||||
|
146
js/src/util/abi.js
Normal file
146
js/src/util/abi.js
Normal file
@ -0,0 +1,146 @@
|
||||
// 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/>.
|
||||
|
||||
import { range } from 'lodash';
|
||||
|
||||
const ARRAY_TYPE = 'ARRAY_TYPE';
|
||||
const ADDRESS_TYPE = 'ADDRESS_TYPE';
|
||||
const STRING_TYPE = 'STRING_TYPE';
|
||||
const BOOL_TYPE = 'BOOL_TYPE';
|
||||
const BYTES_TYPE = 'BYTES_TYPE';
|
||||
const INT_TYPE = 'INT_TYPE';
|
||||
const FIXED_TYPE = 'FIXED_TYPE';
|
||||
|
||||
export const ABI_TYPES = {
|
||||
ARRAY: ARRAY_TYPE, ADDRESS: ADDRESS_TYPE,
|
||||
STRING: STRING_TYPE, BOOL: BOOL_TYPE,
|
||||
BYTES: BYTES_TYPE, INT: INT_TYPE,
|
||||
FIXED: FIXED_TYPE
|
||||
};
|
||||
|
||||
export function parseAbiType (type) {
|
||||
const arrayRegex = /^(.+)\[(\d*)]$/;
|
||||
|
||||
if (arrayRegex.test(type)) {
|
||||
const matches = arrayRegex.exec(type);
|
||||
|
||||
const subtype = parseAbiType(matches[1]);
|
||||
const M = parseInt(matches[2]) || null;
|
||||
const defaultValue = !M
|
||||
? []
|
||||
: range(M).map(() => subtype.default);
|
||||
|
||||
return {
|
||||
type: ARRAY_TYPE,
|
||||
subtype: subtype,
|
||||
length: M,
|
||||
default: defaultValue
|
||||
};
|
||||
}
|
||||
|
||||
const lengthRegex = /^(u?int|bytes)(\d{1,3})$/;
|
||||
|
||||
if (lengthRegex.test(type)) {
|
||||
const matches = lengthRegex.exec(type);
|
||||
|
||||
const subtype = parseAbiType(matches[1]);
|
||||
const length = parseInt(matches[2]);
|
||||
|
||||
return {
|
||||
...subtype,
|
||||
length
|
||||
};
|
||||
}
|
||||
|
||||
const fixedLengthRegex = /^(u?fixed)(\d{1,3})x(\d{1,3})$/;
|
||||
|
||||
if (fixedLengthRegex.test(type)) {
|
||||
const matches = fixedLengthRegex.exec(type);
|
||||
|
||||
const subtype = parseAbiType(matches[1]);
|
||||
const M = parseInt(matches[2]);
|
||||
const N = parseInt(matches[3]);
|
||||
|
||||
return {
|
||||
...subtype,
|
||||
M, N
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'string') {
|
||||
return {
|
||||
type: STRING_TYPE,
|
||||
default: ''
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'bool') {
|
||||
return {
|
||||
type: BOOL_TYPE,
|
||||
default: false
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'address') {
|
||||
return {
|
||||
type: ADDRESS_TYPE,
|
||||
default: ''
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'bytes') {
|
||||
return {
|
||||
type: BYTES_TYPE,
|
||||
default: '0x'
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'uint') {
|
||||
return {
|
||||
type: INT_TYPE,
|
||||
default: 0,
|
||||
length: 256,
|
||||
signed: false
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'int') {
|
||||
return {
|
||||
type: INT_TYPE,
|
||||
default: 0,
|
||||
length: 256,
|
||||
signed: true
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'ufixed') {
|
||||
return {
|
||||
type: FIXED_TYPE,
|
||||
default: 0,
|
||||
length: 256,
|
||||
signed: false
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'fixed') {
|
||||
return {
|
||||
type: FIXED_TYPE,
|
||||
default: 0,
|
||||
length: 256,
|
||||
signed: true
|
||||
};
|
||||
}
|
||||
}
|
@ -44,7 +44,7 @@ export function validateAbi (abi, api) {
|
||||
|
||||
// Validate each elements of the Array
|
||||
const invalidIndex = abiParsed
|
||||
.map((o) => isValidAbiEvent(o, api) || isValidAbiFunction(o, api))
|
||||
.map((o) => isValidAbiEvent(o, api) || isValidAbiFunction(o, api) || isAbiFallback(o))
|
||||
.findIndex((valid) => !valid);
|
||||
|
||||
if (invalidIndex !== -1) {
|
||||
@ -74,6 +74,14 @@ function isValidAbiFunction (object, api) {
|
||||
(object.inputs && api.util.isArray(object.inputs));
|
||||
}
|
||||
|
||||
function isAbiFallback (object) {
|
||||
if (!object) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return object.type === 'fallback';
|
||||
}
|
||||
|
||||
function isValidAbiEvent (object, api) {
|
||||
if (!object) {
|
||||
return false;
|
||||
|
@ -22,7 +22,7 @@ import { uniq } from 'lodash';
|
||||
|
||||
import List from './List';
|
||||
import { CreateAccount } from '../../modals';
|
||||
import { Actionbar, ActionbarSearch, ActionbarSort, Button, Page, Tooltip } from '../../ui';
|
||||
import { Actionbar, ActionbarExport, ActionbarSearch, ActionbarSort, Button, Page, Tooltip } from '../../ui';
|
||||
|
||||
import styles from './accounts.css';
|
||||
|
||||
@ -90,12 +90,15 @@ class Accounts extends Component {
|
||||
return (
|
||||
<ActionbarSort
|
||||
key='sortAccounts'
|
||||
id='sortAccounts'
|
||||
order={ this.state.sortOrder }
|
||||
onChange={ onChange } />
|
||||
);
|
||||
}
|
||||
|
||||
renderActionbar () {
|
||||
const { accounts } = this.props;
|
||||
|
||||
const buttons = [
|
||||
<Button
|
||||
key='newAccount'
|
||||
@ -103,6 +106,11 @@ class Accounts extends Component {
|
||||
label='new account'
|
||||
onClick={ this.onNewAccountClick } />,
|
||||
|
||||
<ActionbarExport
|
||||
key='exportAccounts'
|
||||
content={ accounts }
|
||||
filename='accounts' />,
|
||||
|
||||
this.renderSearchButton(),
|
||||
this.renderSortButton()
|
||||
];
|
||||
|
@ -75,6 +75,7 @@ class Addresses extends Component {
|
||||
return (
|
||||
<ActionbarSort
|
||||
key='sortAccounts'
|
||||
id='sortAddresses'
|
||||
order={ this.state.sortOrder }
|
||||
onChange={ onChange } />
|
||||
);
|
||||
@ -106,7 +107,7 @@ class Addresses extends Component {
|
||||
<ActionbarExport
|
||||
key='exportAddressbook'
|
||||
content={ contacts }
|
||||
filename='addressbook.json' />,
|
||||
filename='addressbook' />,
|
||||
|
||||
<ActionbarImport
|
||||
key='importAddressbook'
|
||||
|
@ -19,7 +19,7 @@ import React, { Component, PropTypes } from 'react';
|
||||
import LinearProgress from 'material-ui/LinearProgress';
|
||||
import { Card, CardActions, CardTitle, CardText } from 'material-ui/Card';
|
||||
|
||||
import { Button, Input, InputAddressSelect } from '../../../ui';
|
||||
import { Button, Input, InputAddress, InputAddressSelect } from '../../../ui';
|
||||
|
||||
import styles from './queries.css';
|
||||
|
||||
@ -33,6 +33,7 @@ export default class InputQuery extends Component {
|
||||
inputs: PropTypes.array.isRequired,
|
||||
outputs: PropTypes.array.isRequired,
|
||||
name: PropTypes.string.isRequired,
|
||||
signature: PropTypes.string.isRequired,
|
||||
className: PropTypes.string
|
||||
}
|
||||
|
||||
@ -95,32 +96,48 @@ export default class InputQuery extends Component {
|
||||
}
|
||||
|
||||
if (!results || results.length < 1) return null;
|
||||
|
||||
return outputs
|
||||
.map((out, index) => ({
|
||||
name: out.name,
|
||||
type: out.type,
|
||||
value: results[index],
|
||||
display: this.renderValue(results[index])
|
||||
}))
|
||||
.sort((outA, outB) => outA.display.length - outB.display.length)
|
||||
.map((out, index) => (
|
||||
<div key={ index }>
|
||||
<div className={ styles.queryResultName }>
|
||||
{ out.name }
|
||||
</div>
|
||||
|
||||
.map((out, index) => {
|
||||
let input = null;
|
||||
if (out.type === 'address') {
|
||||
input = (
|
||||
<InputAddress
|
||||
className={ styles.queryValue }
|
||||
disabled
|
||||
value={ out.display }
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
input = (
|
||||
<Input
|
||||
className={ styles.queryValue }
|
||||
readOnly
|
||||
allowCopy
|
||||
value={ out.display }
|
||||
/>
|
||||
<br />
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div key={ index }>
|
||||
<div className={ styles.queryResultName }>
|
||||
{ out.name }
|
||||
</div>
|
||||
));
|
||||
{ input }
|
||||
</div>
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
renderInput (input) {
|
||||
const { values } = this.state;
|
||||
const { name, type } = input;
|
||||
const label = `${name ? `${name}: ` : ''}${type}`;
|
||||
|
||||
@ -142,6 +159,7 @@ export default class InputQuery extends Component {
|
||||
<InputAddressSelect
|
||||
hint={ type }
|
||||
label={ label }
|
||||
value={ values[name] }
|
||||
required
|
||||
onChange={ onChange }
|
||||
/>
|
||||
@ -154,6 +172,7 @@ export default class InputQuery extends Component {
|
||||
<Input
|
||||
hint={ type }
|
||||
label={ label }
|
||||
value={ values[name] }
|
||||
required
|
||||
onChange={ onChange }
|
||||
/>
|
||||
@ -177,7 +196,7 @@ export default class InputQuery extends Component {
|
||||
|
||||
onClick = () => {
|
||||
const { values } = this.state;
|
||||
const { inputs, contract, name, outputs } = this.props;
|
||||
const { inputs, contract, name, outputs, signature } = this.props;
|
||||
|
||||
this.setState({
|
||||
isLoading: true,
|
||||
@ -187,7 +206,7 @@ export default class InputQuery extends Component {
|
||||
const inputValues = inputs.map(input => values[input.name]);
|
||||
|
||||
contract
|
||||
.instance[name]
|
||||
.instance[signature]
|
||||
.call({}, inputValues)
|
||||
.then(results => {
|
||||
if (outputs.length === 1) {
|
||||
|
@ -56,18 +56,10 @@
|
||||
}
|
||||
|
||||
.methodResults {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
max-width: 24rem;
|
||||
justify-content: space-around;
|
||||
}
|
||||
|
||||
.methodResults > div {
|
||||
padding: 0.25rem 0.5rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
flex: 1 1 50%;
|
||||
box-sizing: border-box;
|
||||
|
||||
& > div {
|
||||
|
@ -19,7 +19,7 @@ import React, { Component, PropTypes } from 'react';
|
||||
import { Card, CardTitle, CardText } from 'material-ui/Card';
|
||||
|
||||
import InputQuery from './inputQuery';
|
||||
import { Container, ContainerTitle, Input } from '../../../ui';
|
||||
import { Container, ContainerTitle, Input, InputAddress } from '../../../ui';
|
||||
|
||||
import styles from './queries.css';
|
||||
|
||||
@ -70,7 +70,7 @@ export default class Queries extends Component {
|
||||
}
|
||||
|
||||
renderInputQuery (fn) {
|
||||
const { abi, name } = fn;
|
||||
const { abi, name, signature } = fn;
|
||||
const { contract } = this.props;
|
||||
|
||||
return (
|
||||
@ -80,6 +80,7 @@ export default class Queries extends Component {
|
||||
inputs={ abi.inputs }
|
||||
outputs={ abi.outputs }
|
||||
name={ name }
|
||||
signature={ signature }
|
||||
contract={ contract }
|
||||
/>
|
||||
</div>
|
||||
@ -99,14 +100,14 @@ export default class Queries extends Component {
|
||||
<CardText
|
||||
className={ styles.methodContent }
|
||||
>
|
||||
{ this.renderValue(values[fn.name]) }
|
||||
{ this.renderValue(values[fn.name], fn.outputs[0].kind.type) }
|
||||
</CardText>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
renderValue (value) {
|
||||
renderValue (value, type) {
|
||||
if (typeof value === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
@ -124,6 +125,16 @@ export default class Queries extends Component {
|
||||
valueToDisplay = value.toString();
|
||||
}
|
||||
|
||||
if (type === 'address') {
|
||||
return (
|
||||
<InputAddress
|
||||
className={ styles.queryValue }
|
||||
value={ valueToDisplay }
|
||||
disabled
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Input
|
||||
className={ styles.queryValue }
|
||||
|
@ -82,6 +82,7 @@ class Contracts extends Component {
|
||||
return (
|
||||
<ActionbarSort
|
||||
key='sortAccounts'
|
||||
id='sortContracts'
|
||||
order={ this.state.sortOrder }
|
||||
metas={ [
|
||||
{ key: 'timestamp', label: 'date' }
|
||||
|
@ -17,6 +17,9 @@
|
||||
.acc {
|
||||
text-align: center;
|
||||
display: inline-block;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.acc > * {
|
||||
@ -35,10 +38,13 @@
|
||||
}
|
||||
|
||||
.name {
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
white-space: nowrap;
|
||||
display: lock;
|
||||
display: block;
|
||||
vertical-align: middle;
|
||||
text-transform: uppercase;
|
||||
|
||||
span {
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,9 @@
|
||||
}
|
||||
|
||||
.transactionDetails {
|
||||
margin-right: 321px;
|
||||
padding-right: 321px;
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.isConfirmed {
|
||||
|
@ -19,7 +19,9 @@
|
||||
}
|
||||
|
||||
.transactionDetails {
|
||||
margin-right: 321px;
|
||||
padding-right: 321px;
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.mainContainer {
|
||||
|
@ -15,6 +15,7 @@
|
||||
/* along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
.signer {
|
||||
width: 916px;
|
||||
}
|
||||
|
||||
.pending {
|
||||
|
@ -14,6 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
const HappyPack = require('happypack');
|
||||
const webpack = require('webpack');
|
||||
|
||||
const ENV = process.env.NODE_ENV || 'development';
|
||||
@ -22,10 +23,26 @@ const DEST = process.env.BUILD_DEST || '.build';
|
||||
|
||||
let modules = [
|
||||
'babel-polyfill',
|
||||
'browserify-aes', 'ethereumjs-tx', 'scryptsy',
|
||||
'react', 'react-dom', 'react-redux', 'react-router',
|
||||
'redux', 'redux-thunk', 'react-router-redux',
|
||||
'lodash', 'material-ui', 'moment', 'blockies'
|
||||
'bignumber.js',
|
||||
'blockies',
|
||||
'brace',
|
||||
'browserify-aes',
|
||||
'chart.js',
|
||||
'ethereumjs-tx',
|
||||
'lodash',
|
||||
'material-ui',
|
||||
'mobx',
|
||||
'mobx-react',
|
||||
'moment',
|
||||
'react',
|
||||
'react-dom',
|
||||
'react-redux',
|
||||
'react-router',
|
||||
'react-router-redux',
|
||||
'recharts',
|
||||
'redux',
|
||||
'redux-thunk',
|
||||
'scryptsy'
|
||||
];
|
||||
|
||||
if (!isProd) {
|
||||
@ -44,6 +61,11 @@ module.exports = {
|
||||
{
|
||||
test: /\.json$/,
|
||||
loaders: ['json']
|
||||
},
|
||||
{
|
||||
test: /\.js$/,
|
||||
include: /(ethereumjs-tx)/,
|
||||
loaders: [ 'happypack/loader?id=js' ]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -63,6 +85,12 @@ module.exports = {
|
||||
'process.env': {
|
||||
NODE_ENV: JSON.stringify(ENV)
|
||||
}
|
||||
}),
|
||||
|
||||
new HappyPack({
|
||||
id: 'js',
|
||||
threads: 4,
|
||||
loaders: ['babel']
|
||||
})
|
||||
];
|
||||
|
||||
|
59
json/src/spec/authority_round.rs
Normal file
59
json/src/spec/authority_round.rs
Normal file
@ -0,0 +1,59 @@
|
||||
// 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/>.
|
||||
|
||||
//! Authority params deserialization.
|
||||
|
||||
use uint::Uint;
|
||||
use hash::Address;
|
||||
|
||||
/// Authority params deserialization.
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
pub struct AuthorityRoundParams {
|
||||
/// Gas limit divisor.
|
||||
#[serde(rename="gasLimitBoundDivisor")]
|
||||
pub gas_limit_bound_divisor: Uint,
|
||||
/// Block duration.
|
||||
#[serde(rename="stepDuration")]
|
||||
pub step_duration: Uint,
|
||||
/// Valid authorities
|
||||
pub authorities: Vec<Address>,
|
||||
}
|
||||
|
||||
/// Authority engine deserialization.
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
pub struct AuthorityRound {
|
||||
/// Ethash params.
|
||||
pub params: AuthorityRoundParams,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json;
|
||||
use spec::authority_round::AuthorityRound;
|
||||
|
||||
#[test]
|
||||
fn basic_authority_deserialization() {
|
||||
let s = r#"{
|
||||
"params": {
|
||||
"gasLimitBoundDivisor": "0x0400",
|
||||
"stepDuration": "0x02",
|
||||
"authorities" : ["0xc6d9d2cd449a754c494264e1809c50e34d64562b"]
|
||||
}
|
||||
}"#;
|
||||
|
||||
let _deserialized: AuthorityRound = serde_json::from_str(s).unwrap();
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
|
||||
use spec::Ethash;
|
||||
use spec::BasicAuthority;
|
||||
use spec::AuthorityRound;
|
||||
|
||||
/// Engine deserialization.
|
||||
#[derive(Debug, PartialEq, Deserialize)]
|
||||
@ -30,6 +31,8 @@ pub enum Engine {
|
||||
Ethash(Ethash),
|
||||
/// BasicAuthority engine.
|
||||
BasicAuthority(BasicAuthority),
|
||||
/// AuthorityRound engine.
|
||||
AuthorityRound(AuthorityRound),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -92,6 +92,11 @@ pub struct EthashParams {
|
||||
/// See main EthashParams docs.
|
||||
#[serde(rename="ecip1010ContinueTransition")]
|
||||
pub ecip1010_continue_transition: Option<Uint>,
|
||||
|
||||
/// See main EthashParams docs.
|
||||
#[serde(rename="maxCodeSize")]
|
||||
pub max_code_size: Option<Uint>,
|
||||
|
||||
}
|
||||
|
||||
/// Ethash engine deserialization.
|
||||
|
@ -26,6 +26,7 @@ pub mod engine;
|
||||
pub mod state;
|
||||
pub mod ethash;
|
||||
pub mod basic_authority;
|
||||
pub mod authority_round;
|
||||
|
||||
pub use self::account::Account;
|
||||
pub use self::builtin::{Builtin, Pricing, Linear};
|
||||
@ -37,3 +38,4 @@ pub use self::engine::Engine;
|
||||
pub use self::state::State;
|
||||
pub use self::ethash::{Ethash, EthashParams};
|
||||
pub use self::basic_authority::{BasicAuthority, BasicAuthorityParams};
|
||||
pub use self::authority_round::{AuthorityRound, AuthorityRoundParams};
|
||||
|
@ -117,7 +117,7 @@ fn execute_import(cmd: ImportBlockchain) -> Result<String, String> {
|
||||
let panic_handler = PanicHandler::new_in_arc();
|
||||
|
||||
// create dirs used by parity
|
||||
try!(cmd.dirs.create_dirs());
|
||||
try!(cmd.dirs.create_dirs(false, false));
|
||||
|
||||
// load spec file
|
||||
let spec = try!(cmd.spec.spec());
|
||||
@ -165,6 +165,9 @@ fn execute_import(cmd: ImportBlockchain) -> Result<String, String> {
|
||||
Arc::new(Miner::with_spec(&spec)),
|
||||
).map_err(|e| format!("Client service error: {:?}", e)));
|
||||
|
||||
// free up the spec in memory.
|
||||
drop(spec);
|
||||
|
||||
panic_handler.forward_from(&service);
|
||||
let client = service.client();
|
||||
|
||||
@ -263,7 +266,7 @@ fn execute_export(cmd: ExportBlockchain) -> Result<String, String> {
|
||||
let panic_handler = PanicHandler::new_in_arc();
|
||||
|
||||
// create dirs used by parity
|
||||
try!(cmd.dirs.create_dirs());
|
||||
try!(cmd.dirs.create_dirs(false, false));
|
||||
|
||||
let format = cmd.format.unwrap_or_default();
|
||||
|
||||
@ -312,6 +315,8 @@ fn execute_export(cmd: ExportBlockchain) -> Result<String, String> {
|
||||
Arc::new(Miner::with_spec(&spec)),
|
||||
).map_err(|e| format!("Client service error: {:?}", e)));
|
||||
|
||||
drop(spec);
|
||||
|
||||
panic_handler.forward_from(&service);
|
||||
let client = service.client();
|
||||
|
||||
|
@ -35,7 +35,7 @@ use params::{ResealPolicy, AccountsConfig, GasPricerConfig, MinerExtras};
|
||||
use ethcore_logger::Config as LogConfig;
|
||||
use dir::Directories;
|
||||
use dapps::Configuration as DappsConfiguration;
|
||||
use signer::{Configuration as SignerConfiguration, SignerCommand};
|
||||
use signer::{Configuration as SignerConfiguration};
|
||||
use run::RunCmd;
|
||||
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat};
|
||||
use presale::ImportWallet;
|
||||
@ -49,7 +49,7 @@ pub enum Cmd {
|
||||
Account(AccountCmd),
|
||||
ImportPresaleWallet(ImportWallet),
|
||||
Blockchain(BlockchainCmd),
|
||||
SignerToken(SignerCommand),
|
||||
SignerToken(SignerConfiguration),
|
||||
Snapshot(SnapshotCommand),
|
||||
Hash(Option<String>),
|
||||
}
|
||||
@ -103,9 +103,7 @@ impl Configuration {
|
||||
let cmd = if self.args.flag_version {
|
||||
Cmd::Version
|
||||
} else if self.args.cmd_signer && self.args.cmd_new_token {
|
||||
Cmd::SignerToken(SignerCommand {
|
||||
path: dirs.signer
|
||||
})
|
||||
Cmd::SignerToken(signer_conf)
|
||||
} else if self.args.cmd_tools && self.args.cmd_hash {
|
||||
Cmd::Hash(self.args.arg_file)
|
||||
} else if self.args.cmd_account {
|
||||
@ -690,7 +688,7 @@ mod tests {
|
||||
use ethcore::miner::{MinerOptions, PrioritizationStrategy};
|
||||
use helpers::{replace_home, default_network_config};
|
||||
use run::RunCmd;
|
||||
use signer::{Configuration as SignerConfiguration, SignerCommand};
|
||||
use signer::{Configuration as SignerConfiguration};
|
||||
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat};
|
||||
use presale::ImportWallet;
|
||||
use account::{AccountCmd, NewAccount, ImportAccounts};
|
||||
@ -827,8 +825,12 @@ mod tests {
|
||||
let args = vec!["parity", "signer", "new-token"];
|
||||
let conf = parse(&args);
|
||||
let expected = replace_home("$HOME/.parity/signer");
|
||||
assert_eq!(conf.into_command().unwrap().cmd, Cmd::SignerToken(SignerCommand {
|
||||
path: expected,
|
||||
assert_eq!(conf.into_command().unwrap().cmd, Cmd::SignerToken(SignerConfiguration {
|
||||
enabled: true,
|
||||
signer_path: expected,
|
||||
interface: "127.0.0.1".into(),
|
||||
port: 8180,
|
||||
skip_origin_validation: false,
|
||||
}));
|
||||
}
|
||||
|
||||
|
@ -44,11 +44,15 @@ impl Default for Directories {
|
||||
}
|
||||
|
||||
impl Directories {
|
||||
pub fn create_dirs(&self) -> Result<(), String> {
|
||||
pub fn create_dirs(&self, dapps_enabled: bool, signer_enabled: bool) -> Result<(), String> {
|
||||
try!(fs::create_dir_all(&self.db).map_err(|e| e.to_string()));
|
||||
try!(fs::create_dir_all(&self.keys).map_err(|e| e.to_string()));
|
||||
if signer_enabled {
|
||||
try!(fs::create_dir_all(&self.signer).map_err(|e| e.to_string()));
|
||||
}
|
||||
if dapps_enabled {
|
||||
try!(fs::create_dir_all(&self.dapps).map_err(|e| e.to_string()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -93,13 +93,29 @@ pub struct RunCmd {
|
||||
pub check_seal: bool,
|
||||
}
|
||||
|
||||
pub fn open_ui(dapps_conf: &dapps::Configuration, signer_conf: &signer::Configuration) -> Result<(), String> {
|
||||
if !dapps_conf.enabled {
|
||||
return Err("Cannot use UI command with Dapps turned off.".into())
|
||||
}
|
||||
|
||||
if !signer_conf.enabled {
|
||||
return Err("Cannot use UI command with UI turned off.".into())
|
||||
}
|
||||
|
||||
let token = try!(signer::generate_token_and_url(signer_conf));
|
||||
// Open a browser
|
||||
url::open(&token.url);
|
||||
// Print a message
|
||||
println!("{}", token.message);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
|
||||
if cmd.ui && cmd.dapps_conf.enabled {
|
||||
// Check if Parity is already running
|
||||
let addr = format!("{}:{}", cmd.dapps_conf.interface, cmd.dapps_conf.port);
|
||||
if !TcpListener::bind(&addr as &str).is_ok() {
|
||||
url::open(&format!("http://{}:{}/", cmd.dapps_conf.interface, cmd.dapps_conf.port));
|
||||
return Ok(());
|
||||
return open_ui(&cmd.dapps_conf, &cmd.signer_conf);
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,7 +126,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
|
||||
raise_fd_limit();
|
||||
|
||||
// create dirs used by parity
|
||||
try!(cmd.dirs.create_dirs());
|
||||
try!(cmd.dirs.create_dirs(cmd.dapps_conf.enabled, cmd.signer_conf.enabled));
|
||||
|
||||
// load spec
|
||||
let spec = try!(cmd.spec.spec());
|
||||
@ -235,6 +251,9 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
|
||||
miner.clone(),
|
||||
).map_err(|e| format!("Client service error: {:?}", e)));
|
||||
|
||||
// drop the spec to free up genesis state.
|
||||
drop(spec);
|
||||
|
||||
// forward panics from service
|
||||
panic_handler.forward_from(&service);
|
||||
|
||||
@ -309,7 +328,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
|
||||
};
|
||||
|
||||
// start signer server
|
||||
let signer_server = try!(signer::start(cmd.signer_conf, signer_deps));
|
||||
let signer_server = try!(signer::start(cmd.signer_conf.clone(), signer_deps));
|
||||
|
||||
let informant = Arc::new(Informant::new(
|
||||
service.client(),
|
||||
@ -363,10 +382,7 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
|
||||
|
||||
// start ui
|
||||
if cmd.ui {
|
||||
if !cmd.dapps_conf.enabled {
|
||||
return Err("Cannot use UI command with Dapps turned off.".into())
|
||||
}
|
||||
url::open(&format!("http://{}:{}/", cmd.dapps_conf.interface, cmd.dapps_conf.port));
|
||||
try!(open_ui(&cmd.dapps_conf, &cmd.signer_conf));
|
||||
}
|
||||
|
||||
// Handle exit
|
||||
|
@ -27,7 +27,7 @@ pub use ethcore_signer::Server as SignerServer;
|
||||
|
||||
const CODES_FILENAME: &'static str = "authcodes";
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Configuration {
|
||||
pub enabled: bool,
|
||||
pub port: u16,
|
||||
@ -53,6 +53,12 @@ pub struct Dependencies {
|
||||
pub apis: Arc<rpc_apis::Dependencies>,
|
||||
}
|
||||
|
||||
pub struct NewToken {
|
||||
pub token: String,
|
||||
pub url: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub fn start(conf: Configuration, deps: Dependencies) -> Result<Option<SignerServer>, String> {
|
||||
if !conf.enabled {
|
||||
Ok(None)
|
||||
@ -68,20 +74,33 @@ fn codes_path(path: String) -> PathBuf {
|
||||
p
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct SignerCommand {
|
||||
pub path: String,
|
||||
pub fn execute(cmd: Configuration) -> Result<String, String> {
|
||||
Ok(try!(generate_token_and_url(&cmd)).message)
|
||||
}
|
||||
|
||||
pub fn execute(cmd: SignerCommand) -> Result<String, String> {
|
||||
generate_new_token(cmd.path)
|
||||
.map(|code| format!("This key code will authorise your System Signer UI: {}", Colour::White.bold().paint(code)))
|
||||
.map_err(|err| format!("Error generating token: {:?}", err))
|
||||
pub fn generate_token_and_url(conf: &Configuration) -> Result<NewToken, String> {
|
||||
let code = try!(generate_new_token(conf.signer_path.clone()).map_err(|err| format!("Error generating token: {:?}", err)));
|
||||
let auth_url = format!("http://{}:{}/#/auth?token={}", conf.interface, conf.port, code);
|
||||
// And print in to the console
|
||||
Ok(NewToken {
|
||||
token: code.clone(),
|
||||
url: auth_url.clone(),
|
||||
message: format!(
|
||||
r#"
|
||||
Open: {}
|
||||
to authorize your browser.
|
||||
Or use the generated token:
|
||||
{}"#,
|
||||
Colour::White.bold().paint(auth_url),
|
||||
code
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn generate_new_token(path: String) -> io::Result<String> {
|
||||
let path = codes_path(path);
|
||||
let mut codes = try!(signer::AuthCodes::from_file(&path));
|
||||
codes.clear_garbage();
|
||||
let code = try!(codes.generate_new());
|
||||
try!(codes.to_file(&path));
|
||||
trace!("New key code created: {}", Colour::White.bold().paint(&code[..]));
|
||||
|
@ -183,7 +183,6 @@ impl SnapshotCommand {
|
||||
|
||||
Ok((service, panic_handler))
|
||||
}
|
||||
|
||||
/// restore from a snapshot
|
||||
pub fn restore(self) -> Result<(), String> {
|
||||
let file = self.file_path.clone();
|
||||
|
@ -16,12 +16,10 @@
|
||||
|
||||
use rand::Rng;
|
||||
use rand::os::OsRng;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::fs;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::path::Path;
|
||||
use std::time;
|
||||
use util::{H256, Hashable};
|
||||
use std::{fs, time, mem};
|
||||
use util::{H256, Hashable, Itertools};
|
||||
|
||||
/// Providing current time in seconds
|
||||
pub trait TimeProvider {
|
||||
@ -47,12 +45,35 @@ impl TimeProvider for DefaultTimeProvider {
|
||||
|
||||
/// No of seconds the hash is valid
|
||||
const TIME_THRESHOLD: u64 = 7;
|
||||
/// minimal length of hash
|
||||
const TOKEN_LENGTH: usize = 16;
|
||||
/// special "initial" token used for authorization when there are no tokens yet.
|
||||
const INITIAL_TOKEN: &'static str = "initial";
|
||||
/// Separator between fields in serialized tokens file.
|
||||
const SEPARATOR: &'static str = ";";
|
||||
/// Number of seconds to keep unused tokens.
|
||||
const UNUSED_TOKEN_TIMEOUT: u64 = 3600 * 24; // a day
|
||||
|
||||
struct Code {
|
||||
code: String,
|
||||
/// Duration since unix_epoch
|
||||
created_at: time::Duration,
|
||||
/// Duration since unix_epoch
|
||||
last_used_at: Option<time::Duration>,
|
||||
}
|
||||
|
||||
fn decode_time(val: &str) -> Option<time::Duration> {
|
||||
let time = val.parse::<u64>().ok();
|
||||
time.map(time::Duration::from_secs)
|
||||
}
|
||||
|
||||
fn encode_time(time: time::Duration) -> String {
|
||||
format!("{}", time.as_secs())
|
||||
}
|
||||
|
||||
/// Manages authorization codes for `SignerUIs`
|
||||
pub struct AuthCodes<T: TimeProvider = DefaultTimeProvider> {
|
||||
codes: Vec<String>,
|
||||
codes: Vec<Code>,
|
||||
now: T,
|
||||
}
|
||||
|
||||
@ -69,13 +90,32 @@ impl AuthCodes<DefaultTimeProvider> {
|
||||
"".into()
|
||||
}
|
||||
};
|
||||
let time_provider = DefaultTimeProvider::default();
|
||||
|
||||
let codes = content.lines()
|
||||
.filter(|f| f.len() >= TOKEN_LENGTH)
|
||||
.map(String::from)
|
||||
.filter_map(|line| {
|
||||
let mut parts = line.split(SEPARATOR);
|
||||
let token = parts.next();
|
||||
let created = parts.next();
|
||||
let used = parts.next();
|
||||
|
||||
match token {
|
||||
None => None,
|
||||
Some(token) if token.len() < TOKEN_LENGTH => None,
|
||||
Some(token) => {
|
||||
Some(Code {
|
||||
code: token.into(),
|
||||
last_used_at: used.and_then(decode_time),
|
||||
created_at: created.and_then(decode_time)
|
||||
.unwrap_or_else(|| time::Duration::from_secs(time_provider.now())),
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Ok(AuthCodes {
|
||||
codes: codes,
|
||||
now: DefaultTimeProvider::default(),
|
||||
now: time_provider,
|
||||
})
|
||||
}
|
||||
|
||||
@ -86,19 +126,30 @@ impl<T: TimeProvider> AuthCodes<T> {
|
||||
/// Writes all `AuthCodes` to a disk.
|
||||
pub fn to_file(&self, file: &Path) -> io::Result<()> {
|
||||
let mut file = try!(fs::File::create(file));
|
||||
let content = self.codes.join("\n");
|
||||
let content = self.codes.iter().map(|code| {
|
||||
let mut data = vec![code.code.clone(), encode_time(code.created_at.clone())];
|
||||
if let Some(used_at) = code.last_used_at.clone() {
|
||||
data.push(encode_time(used_at));
|
||||
}
|
||||
data.join(SEPARATOR)
|
||||
}).join("\n");
|
||||
file.write_all(content.as_bytes())
|
||||
}
|
||||
|
||||
/// Creates a new `AuthCodes` store with given `TimeProvider`.
|
||||
pub fn new(codes: Vec<String>, now: T) -> Self {
|
||||
AuthCodes {
|
||||
codes: codes,
|
||||
codes: codes.into_iter().map(|code| Code {
|
||||
code: code,
|
||||
created_at: time::Duration::from_secs(now.now()),
|
||||
last_used_at: None,
|
||||
}).collect(),
|
||||
now: now,
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if given hash is correct identifier of `SignerUI`
|
||||
/// Checks if given hash is correct authcode of `SignerUI`
|
||||
/// Updates this hash last used field in case it's valid.
|
||||
#[cfg_attr(feature="dev", allow(wrong_self_convention))]
|
||||
pub fn is_valid(&mut self, hash: &H256, time: u64) -> bool {
|
||||
let now = self.now.now();
|
||||
@ -121,8 +172,14 @@ impl<T: TimeProvider> AuthCodes<T> {
|
||||
}
|
||||
|
||||
// look for code
|
||||
self.codes.iter()
|
||||
.any(|code| &as_token(code) == hash)
|
||||
for mut code in &mut self.codes {
|
||||
if &as_token(&code.code) == hash {
|
||||
code.last_used_at = Some(time::Duration::from_secs(now));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Generates and returns a new code that can be used by `SignerUIs`
|
||||
@ -135,7 +192,11 @@ impl<T: TimeProvider> AuthCodes<T> {
|
||||
.collect::<Vec<String>>()
|
||||
.join("-");
|
||||
trace!(target: "signer", "New authentication token generated.");
|
||||
self.codes.push(code);
|
||||
self.codes.push(Code {
|
||||
code: code,
|
||||
created_at: time::Duration::from_secs(self.now.now()),
|
||||
last_used_at: None,
|
||||
});
|
||||
Ok(readable_code)
|
||||
}
|
||||
|
||||
@ -143,12 +204,31 @@ impl<T: TimeProvider> AuthCodes<T> {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.codes.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes old tokens that have not been used since creation.
|
||||
pub fn clear_garbage(&mut self) {
|
||||
let now = self.now.now();
|
||||
let threshold = time::Duration::from_secs(now.saturating_sub(UNUSED_TOKEN_TIMEOUT));
|
||||
|
||||
let codes = mem::replace(&mut self.codes, Vec::new());
|
||||
for code in codes {
|
||||
// Skip codes that are old and were never used.
|
||||
if code.last_used_at.is_none() && code.created_at <= threshold {
|
||||
continue;
|
||||
}
|
||||
self.codes.push(code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use devtools;
|
||||
use std::io::{Read, Write};
|
||||
use std::{time, fs};
|
||||
use std::cell::Cell;
|
||||
|
||||
use util::{H256, Hashable};
|
||||
use super::*;
|
||||
|
||||
@ -217,6 +297,54 @@ mod tests {
|
||||
assert_eq!(res2, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_read_old_format_from_file() {
|
||||
// given
|
||||
let path = devtools::RandomTempPath::new();
|
||||
let code = "23521352asdfasdfadf";
|
||||
{
|
||||
let mut file = fs::File::create(&path).unwrap();
|
||||
file.write_all(b"a\n23521352asdfasdfadf\nb\n").unwrap();
|
||||
}
|
||||
|
||||
// when
|
||||
let mut authcodes = AuthCodes::from_file(&path).unwrap();
|
||||
let time = time::UNIX_EPOCH.elapsed().unwrap().as_secs();
|
||||
|
||||
// then
|
||||
assert!(authcodes.is_valid(&generate_hash(code, time), time), "Code should be read from file");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_remove_old_unused_tokens() {
|
||||
// given
|
||||
let path = devtools::RandomTempPath::new();
|
||||
let code1 = "11111111asdfasdf111";
|
||||
let code2 = "22222222asdfasdf222";
|
||||
let code3 = "33333333asdfasdf333";
|
||||
|
||||
let time = Cell::new(100);
|
||||
let mut codes = AuthCodes::new(vec![code1.into(), code2.into(), code3.into()], || time.get());
|
||||
// `code2` should not be removed (we never remove tokens that were used)
|
||||
codes.is_valid(&generate_hash(code2, time.get()), time.get());
|
||||
|
||||
// when
|
||||
time.set(100 + 10_000_000);
|
||||
// mark `code1` as used now
|
||||
codes.is_valid(&generate_hash(code1, time.get()), time.get());
|
||||
|
||||
let new_code = codes.generate_new().unwrap().replace('-', "");
|
||||
codes.clear_garbage();
|
||||
codes.to_file(&path).unwrap();
|
||||
|
||||
// then
|
||||
let mut content = String::new();
|
||||
let mut file = fs::File::open(&path).unwrap();
|
||||
file.read_to_string(&mut content).unwrap();
|
||||
|
||||
assert_eq!(content, format!("{};100;10000100\n{};100;100\n{};10000100", code1, code2, new_code));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -94,6 +94,9 @@ fn auth_is_valid(codes_path: &Path, protocols: ws::Result<Vec<&str>>) -> bool {
|
||||
// Check if the code is valid
|
||||
AuthCodes::from_file(codes_path)
|
||||
.map(|mut codes| {
|
||||
// remove old tokens
|
||||
codes.clear_garbage();
|
||||
|
||||
let res = codes.is_valid(&auth, time);
|
||||
// make sure to save back authcodes - it might have been modified
|
||||
if let Err(_) = codes.to_file(codes_path) {
|
||||
|
@ -56,6 +56,7 @@ pub enum IoMessage<Message> where Message: Send + Clone + Sized {
|
||||
handler_id: HandlerId,
|
||||
token: TimerToken,
|
||||
delay: u64,
|
||||
once: bool,
|
||||
},
|
||||
RemoveTimer {
|
||||
handler_id: HandlerId,
|
||||
@ -92,12 +93,24 @@ impl<Message> IoContext<Message> where Message: Send + Clone + Sync + 'static {
|
||||
}
|
||||
}
|
||||
|
||||
/// Register a new IO timer. 'IoHandler::timeout' will be called with the token.
|
||||
/// Register a new recurring IO timer. 'IoHandler::timeout' will be called with the token.
|
||||
pub fn register_timer(&self, token: TimerToken, ms: u64) -> Result<(), IoError> {
|
||||
try!(self.channel.send_io(IoMessage::AddTimer {
|
||||
token: token,
|
||||
delay: ms,
|
||||
handler_id: self.handler,
|
||||
once: false,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register a new IO timer once. 'IoHandler::timeout' will be called with the token.
|
||||
pub fn register_timer_once(&self, token: TimerToken, ms: u64) -> Result<(), IoError> {
|
||||
try!(self.channel.send_io(IoMessage::AddTimer {
|
||||
token: token,
|
||||
delay: ms,
|
||||
handler_id: self.handler,
|
||||
once: true,
|
||||
}));
|
||||
Ok(())
|
||||
}
|
||||
@ -163,6 +176,7 @@ impl<Message> IoContext<Message> where Message: Send + Clone + Sync + 'static {
|
||||
struct UserTimer {
|
||||
delay: u64,
|
||||
timeout: Timeout,
|
||||
once: bool,
|
||||
}
|
||||
|
||||
/// Root IO handler. Manages user handlers, messages and IO timers.
|
||||
@ -235,8 +249,14 @@ impl<Message> Handler for IoManager<Message> where Message: Send + Clone + Sync
|
||||
let handler_index = token.0 / TOKENS_PER_HANDLER;
|
||||
let token_id = token.0 % TOKENS_PER_HANDLER;
|
||||
if let Some(handler) = self.handlers.read().get(handler_index) {
|
||||
if let Some(timer) = self.timers.read().get(&token.0) {
|
||||
let maybe_timer = self.timers.read().get(&token.0).cloned();
|
||||
if let Some(timer) = maybe_timer {
|
||||
if timer.once {
|
||||
self.timers.write().remove(&token_id);
|
||||
event_loop.clear_timeout(&timer.timeout);
|
||||
} else {
|
||||
event_loop.timeout(token, Duration::from_millis(timer.delay)).expect("Error re-registering user timer");
|
||||
}
|
||||
self.worker_channel.push(Work { work_type: WorkType::Timeout, token: token_id, handler: handler.clone(), handler_id: handler_index });
|
||||
self.work_ready.notify_all();
|
||||
}
|
||||
@ -264,10 +284,10 @@ impl<Message> Handler for IoManager<Message> where Message: Send + Clone + Sync
|
||||
event_loop.clear_timeout(&timer.timeout);
|
||||
}
|
||||
},
|
||||
IoMessage::AddTimer { handler_id, token, delay } => {
|
||||
IoMessage::AddTimer { handler_id, token, delay, once } => {
|
||||
let timer_id = token + handler_id * TOKENS_PER_HANDLER;
|
||||
let timeout = event_loop.timeout(Token(timer_id), Duration::from_millis(delay)).expect("Error registering user timer");
|
||||
self.timers.write().insert(timer_id, UserTimer { delay: delay, timeout: timeout });
|
||||
self.timers.write().insert(timer_id, UserTimer { delay: delay, timeout: timeout, once: once });
|
||||
},
|
||||
IoMessage::RemoveTimer { handler_id, token } => {
|
||||
let timer_id = token + handler_id * TOKENS_PER_HANDLER;
|
||||
|
Loading…
Reference in New Issue
Block a user