Skip to content

Commit d86333a

Browse files
grbIzl5chdn
authored andcommitted
Private transactions processing error handling (openethereum#8431)
* Integration test for private transaction returned * Do not interrupt verification in case of errors * Helpers use specified * Review comments fixed
1 parent 28b5906 commit d86333a

File tree

9 files changed

+193
-48
lines changed

9 files changed

+193
-48
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ethcore/private-tx/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ impl Provider where {
306306
/// Retrieve and verify the first available private transaction for every sender
307307
///
308308
/// TODO [ToDr] It seems that:
309-
/// 1. This method will fail on any error without removing invalid transaction.
310-
/// 2. It means that the transaction will be stuck there forever and we will never be able to make any progress.
311-
///
312-
/// It might be more sensible to `drain()` transactions from the queue instead and process all of them,
313-
/// possibly printing some errors in case of failures.
314309
/// The 3 methods `ready_transaction,get_descriptor,remove` are always used in conjuction so most likely
315310
/// can be replaced with a single `drain()` method instead.
316311
/// Thanks to this we also don't really need to lock the entire verification for the time of execution.
@@ -339,13 +334,11 @@ impl Provider where {
339334
trace!("Sending signature for private transaction: {:?}", signed_private_transaction);
340335
self.broadcast_signed_private_transaction(signed_private_transaction.rlp_bytes().into_vec());
341336
} else {
342-
trace!("Incorrect type of action for the transaction");
343-
bail!(ErrorKind::BadTransactonType);
337+
warn!("Incorrect type of action for the transaction");
344338
}
345339
},
346340
Err(e) => {
347-
trace!("Cannot retrieve descriptor for transaction with error {:?}", e);
348-
bail!(e);
341+
warn!("Cannot retrieve descriptor for transaction with error {:?}", e);
349342
}
350343
}
351344
verification_queue.remove_private_transaction(&transaction_hash);

ethcore/sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ ipnetwork = "0.12.6"
3535
[dev-dependencies]
3636
ethkey = { path = "../../ethkey" }
3737
kvdb-memorydb = { path = "../../util/kvdb-memorydb" }
38+
ethcore-private-tx = { path = "../private-tx" }

ethcore/sync/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern crate ethcore_light as light;
4646
#[cfg(test)] extern crate ethkey;
4747
#[cfg(test)] extern crate kvdb_memorydb;
4848
#[cfg(test)] extern crate rustc_hex;
49+
#[cfg(test)] extern crate ethcore_private_tx;
4950

5051
#[macro_use]
5152
extern crate macros;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "PrivateTransactions",
3+
"engine": {
4+
"instantSeal": null
5+
},
6+
"params": {
7+
"gasLimitBoundDivisor": "0x0400",
8+
"accountStartNonce": "0x0",
9+
"maximumExtraDataSize": "0x20",
10+
"minGasLimit": "0x1388",
11+
"networkID" : "0x11"
12+
},
13+
"genesis": {
14+
"seal": {
15+
"generic": "0x0"
16+
},
17+
"difficulty": "0x20000",
18+
"author": "0x0000000000000000000000000000000000000000",
19+
"timestamp": "0x00",
20+
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
21+
"extraData": "0x",
22+
"gasLimit": "0x989680"
23+
},
24+
"accounts": {
25+
"0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
26+
"0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
27+
"0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
28+
"0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }
29+
}
30+
}

ethcore/sync/src/tests/consensus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn authority_round() {
4848
ap.insert_account(s1.secret().clone(), "").unwrap();
4949

5050
let chain_id = Spec::new_test_round().chain_id();
51-
let mut net = TestNet::with_spec_and_accounts(2, SyncConfig::default(), Spec::new_test_round, Some(ap), false);
51+
let mut net = TestNet::with_spec_and_accounts(2, SyncConfig::default(), Spec::new_test_round, Some(ap));
5252
let io_handler0: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(0).chain.clone()));
5353
let io_handler1: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(1).chain.clone()));
5454
// Push transaction to both clients. Only one of them gets lucky to produce a block.
@@ -135,7 +135,7 @@ fn tendermint() {
135135
ap.insert_account(s1.secret().clone(), "").unwrap();
136136

137137
let chain_id = Spec::new_test_tendermint().chain_id();
138-
let mut net = TestNet::with_spec_and_accounts(2, SyncConfig::default(), Spec::new_test_tendermint, Some(ap), false);
138+
let mut net = TestNet::with_spec_and_accounts(2, SyncConfig::default(), Spec::new_test_tendermint, Some(ap));
139139
let io_handler0: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(0).chain.clone()));
140140
let io_handler1: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(1).chain.clone()));
141141
// Push transaction to both clients. Only one of them issues a proposal.

ethcore/sync/src/tests/helpers.rs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use io::{IoChannel, IoContext, IoHandler};
3333
use api::WARP_SYNC_PROTOCOL_ID;
3434
use chain::{ChainSync, ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_3};
3535
use SyncConfig;
36-
use private_tx::{NoopPrivateTxHandler, PrivateTxHandler, SimplePrivateTxHandler};
36+
use private_tx::SimplePrivateTxHandler;
3737

3838
pub trait FlushingBlockChainClient: BlockChainClient {
3939
fn flush(&self) {}
@@ -210,7 +210,7 @@ pub struct EthPeer<C> where C: FlushingBlockChainClient {
210210
pub snapshot_service: Arc<TestSnapshotService>,
211211
pub sync: RwLock<ChainSync>,
212212
pub queue: RwLock<VecDeque<TestPacket>>,
213-
pub private_tx_handler: Arc<PrivateTxHandler>,
213+
pub private_tx_handler: Arc<SimplePrivateTxHandler>,
214214
pub io_queue: RwLock<VecDeque<ChainMessageType>>,
215215
new_blocks_queue: RwLock<VecDeque<NewBlockMessage>>,
216216
}
@@ -335,7 +335,7 @@ impl TestNet<EthPeer<TestBlockChainClient>> {
335335
for _ in 0..n {
336336
let chain = TestBlockChainClient::new();
337337
let ss = Arc::new(TestSnapshotService::new());
338-
let private_tx_handler = Arc::new(NoopPrivateTxHandler);
338+
let private_tx_handler = Arc::new(SimplePrivateTxHandler::default());
339339
let sync = ChainSync::new(config.clone(), &chain, private_tx_handler.clone());
340340
net.peers.push(Arc::new(EthPeer {
341341
sync: RwLock::new(sync),
@@ -362,8 +362,7 @@ impl TestNet<EthPeer<EthcoreClient>> {
362362
n: usize,
363363
config: SyncConfig,
364364
spec_factory: F,
365-
accounts: Option<Arc<AccountProvider>>,
366-
private_tx_handler: bool,
365+
accounts: Option<Arc<AccountProvider>>
367366
) -> Self
368367
where F: Fn() -> Spec
369368
{
@@ -373,11 +372,7 @@ impl TestNet<EthPeer<EthcoreClient>> {
373372
disconnect_events: Vec::new(),
374373
};
375374
for _ in 0..n {
376-
if private_tx_handler {
377-
net.add_peer_with_private_config(config.clone(), spec_factory(), accounts.clone());
378-
} else {
379-
net.add_peer(config.clone(), spec_factory(), accounts.clone());
380-
}
375+
net.add_peer_with_private_config(config.clone(), spec_factory(), accounts.clone());
381376
}
382377
net
383378
}
@@ -410,33 +405,6 @@ impl TestNet<EthPeer<EthcoreClient>> {
410405
//private_provider.add_notify(peer.clone());
411406
self.peers.push(peer);
412407
}
413-
414-
pub fn add_peer(&mut self, config: SyncConfig, spec: Spec, accounts: Option<Arc<AccountProvider>>) {
415-
let miner = Arc::new(Miner::new_for_tests(&spec, accounts));
416-
let client = EthcoreClient::new(
417-
ClientConfig::default(),
418-
&spec,
419-
Arc::new(::kvdb_memorydb::create(::ethcore::db::NUM_COLUMNS.unwrap_or(0))),
420-
miner.clone(),
421-
IoChannel::disconnected(),
422-
).unwrap();
423-
424-
let ss = Arc::new(TestSnapshotService::new());
425-
let private_tx_handler = Arc::new(NoopPrivateTxHandler);
426-
let sync = ChainSync::new(config, &*client, private_tx_handler.clone());
427-
let peer = Arc::new(EthPeer {
428-
sync: RwLock::new(sync),
429-
snapshot_service: ss,
430-
queue: RwLock::new(VecDeque::new()),
431-
chain: client,
432-
miner,
433-
private_tx_handler,
434-
io_queue: RwLock::new(VecDeque::new()),
435-
new_blocks_queue: RwLock::new(VecDeque::new()),
436-
});
437-
peer.chain.add_notify(peer.clone());
438-
self.peers.push(peer);
439-
}
440408
}
441409

442410
impl<P> TestNet<P> where P: Peer {

ethcore/sync/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod helpers;
1818
pub mod snapshot;
1919
mod chain;
2020
mod consensus;
21+
mod private;
2122

2223
#[cfg(feature = "ipc")]
2324
mod rpc;

ethcore/sync/src/tests/private.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2+
// This file is part of Parity.
3+
4+
// Parity is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Parity is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use std::sync::Arc;
18+
use hash::keccak;
19+
use io::{IoHandler, IoChannel};
20+
use ethcore::client::{BlockChainClient, BlockId, ClientIoMessage};
21+
use ethcore::spec::Spec;
22+
use ethcore::miner::MinerService;
23+
use ethcore::CreateContractAddress;
24+
use transaction::{Transaction, Action};
25+
use ethcore::executive::{contract_address};
26+
use ethcore::test_helpers::{push_block_with_transactions};
27+
use ethcore_private_tx::{Provider, ProviderConfig, NoopEncryptor};
28+
use ethcore::account_provider::AccountProvider;
29+
use ethkey::{KeyPair};
30+
use tests::helpers::{TestNet, TestIoHandler};
31+
use rustc_hex::FromHex;
32+
use SyncConfig;
33+
34+
fn seal_spec() -> Spec {
35+
let spec_data = include_str!("../res/private_spec.json");
36+
Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap()
37+
}
38+
39+
#[test]
40+
fn send_private_transaction() {
41+
// Setup two clients
42+
let s0 = KeyPair::from_secret_slice(&keccak("1")).unwrap();
43+
let s1 = KeyPair::from_secret_slice(&keccak("0")).unwrap();
44+
let ap = Arc::new(AccountProvider::transient_provider());
45+
ap.insert_account(s0.secret().clone(), "").unwrap();
46+
ap.insert_account(s1.secret().clone(), "").unwrap();
47+
48+
let mut net = TestNet::with_spec_and_accounts(2, SyncConfig::default(), seal_spec, Some(ap.clone()));
49+
let client0 = net.peer(0).chain.clone();
50+
let client1 = net.peer(1).chain.clone();
51+
let io_handler0: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(0).chain.clone()));
52+
let io_handler1: Arc<IoHandler<ClientIoMessage>> = Arc::new(TestIoHandler::new(net.peer(1).chain.clone()));
53+
54+
net.peer(0).miner.set_author(s0.address(), Some("".into())).unwrap();
55+
net.peer(1).miner.set_author(s1.address(), Some("".to_owned())).unwrap();
56+
net.peer(0).chain.engine().register_client(Arc::downgrade(&net.peer(0).chain) as _);
57+
net.peer(1).chain.engine().register_client(Arc::downgrade(&net.peer(1).chain) as _);
58+
net.peer(0).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler0)));
59+
net.peer(1).chain.set_io_channel(IoChannel::to_handler(Arc::downgrade(&io_handler1)));
60+
61+
let (address, _) = contract_address(CreateContractAddress::FromSenderAndNonce, &s0.address(), &0.into(), &[]);
62+
let chain_id = client0.signing_chain_id();
63+
64+
// Exhange statuses
65+
net.sync();
66+
67+
// Setup private providers
68+
let validator_config = ProviderConfig{
69+
validator_accounts: vec![s1.address()],
70+
signer_account: None,
71+
passwords: vec!["".into()],
72+
};
73+
74+
let signer_config = ProviderConfig{
75+
validator_accounts: Vec::new(),
76+
signer_account: Some(s0.address()),
77+
passwords: vec!["".into()],
78+
};
79+
80+
let pm0 = Arc::new(Provider::new(
81+
client0.clone(),
82+
net.peer(0).miner.clone(),
83+
ap.clone(),
84+
Box::new(NoopEncryptor::default()),
85+
signer_config,
86+
IoChannel::to_handler(Arc::downgrade(&io_handler0)),
87+
).unwrap());
88+
pm0.add_notify(net.peers[0].clone());
89+
90+
let pm1 = Arc::new(Provider::new(
91+
client1.clone(),
92+
net.peer(1).miner.clone(),
93+
ap.clone(),
94+
Box::new(NoopEncryptor::default()),
95+
validator_config,
96+
IoChannel::to_handler(Arc::downgrade(&io_handler1)),
97+
).unwrap());
98+
pm1.add_notify(net.peers[1].clone());
99+
100+
// Create and deploy contract
101+
let private_contract_test = "6060604052341561000f57600080fd5b60d88061001d6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630c55699c146046578063bc64b76d14607457600080fd5b3415605057600080fd5b60566098565b60405180826000191660001916815260200191505060405180910390f35b3415607e57600080fd5b6096600480803560001916906020019091905050609e565b005b60005481565b8060008160001916905550505600a165627a7a723058206acbdf4b15ca4c2d43e1b1879b830451a34f1e9d02ff1f2f394d8d857e79d2080029".from_hex().unwrap();
102+
let mut private_create_tx = Transaction::default();
103+
private_create_tx.action = Action::Create;
104+
private_create_tx.data = private_contract_test;
105+
private_create_tx.gas = 200000.into();
106+
let private_create_tx_signed = private_create_tx.sign(&s0.secret(), None);
107+
let validators = vec![s1.address()];
108+
let (public_tx, _) = pm0.public_creation_transaction(BlockId::Latest, &private_create_tx_signed, &validators, 0.into()).unwrap();
109+
let public_tx = public_tx.sign(&s0.secret(), chain_id);
110+
111+
let public_tx_copy = public_tx.clone();
112+
push_block_with_transactions(&client0, &[public_tx]);
113+
push_block_with_transactions(&client1, &[public_tx_copy]);
114+
115+
net.sync();
116+
117+
//Create private transaction for modifying state
118+
let mut private_tx = Transaction::default();
119+
private_tx.action = Action::Call(address.clone());
120+
private_tx.data = "bc64b76d2a00000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap(); //setX(42)
121+
private_tx.gas = 120000.into();
122+
private_tx.nonce = 1.into();
123+
let private_tx = private_tx.sign(&s0.secret(), None);
124+
assert!(pm0.create_private_transaction(private_tx).is_ok());
125+
126+
//send private transaction message to validator
127+
net.sync();
128+
129+
let validator_handler = net.peer(1).private_tx_handler.clone();
130+
let received_private_transactions = validator_handler.txs.lock().clone();
131+
assert_eq!(received_private_transactions.len(), 1);
132+
133+
//process received private transaction message
134+
let private_transaction = received_private_transactions[0].clone();
135+
assert!(pm1.import_private_transaction(&private_transaction).is_ok());
136+
assert!(pm1.on_private_transaction_queued().is_ok());
137+
138+
//send signed response
139+
net.sync();
140+
141+
let sender_handler = net.peer(0).private_tx_handler.clone();
142+
let received_signed_private_transactions = sender_handler.signed_txs.lock().clone();
143+
assert_eq!(received_signed_private_transactions.len(), 1);
144+
145+
//process signed response
146+
let signed_private_transaction = received_signed_private_transactions[0].clone();
147+
assert!(pm0.import_signed_private_transaction(&signed_private_transaction).is_ok());
148+
let local_transactions = net.peer(0).miner.local_transactions();
149+
assert_eq!(local_transactions.len(), 1);
150+
}

0 commit comments

Comments
 (0)