/
handler.rs
1121 lines (1028 loc) · 41 KB
/
handler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Rivtower Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::block_txn::{BlockTxnMessage, BlockTxnReq};
use crate::block_verify::BlockVerify;
use crate::dispatcher::Dispatcher;
use crate::history::HistoryHeights;
use crate::transaction_verify::Error;
use cita_types::traits::LowerHex;
use cita_types::{clean_0x, Address, H256, U256};
use crypto::{pubkey_to_address, PubKey, Sign, Signature, SIGNATURE_BYTES_LEN};
use error::ErrorCode;
use jsonrpc_types::rpc_types::TxResponse;
use libproto::auth::{Miscellaneous, MiscellaneousReq};
use libproto::blockchain::{AccountGasLimit, SignedTransaction, Transaction};
use libproto::router::{MsgType, RoutingKey, SubModules};
use libproto::snapshot::{Cmd, Resp, SnapshotReq, SnapshotResp};
use libproto::{
BlackList, BlockTxHashes, BlockTxHashesReq, BlockTxn, Crypto, GetBlockTxn, Message,
OperateType, Origin, Request, Response, UnverifiedTransaction, VerifyBlockReq, VerifyTxReq,
};
use libproto::{TryFrom, TryInto};
use lru::LruCache;
use pubsub::channel::{Receiver, Sender};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rayon::ThreadPoolBuilder;
use std::collections::{HashMap, HashSet};
use std::convert::Into;
use std::str::FromStr;
use std::time::Duration;
use util::BLOCKLIMIT;
const TX_OK: &str = "OK";
// Paid for every non-zero byte of data or code for a transaction
const G_TX_DATA_NON_ZERO: usize = 68;
// Paid for every transaction
const G_TRANSACTION: usize = 21000;
// Paid for contract create
const G_CREATE: usize = 32000;
// verify signature
pub fn verify_tx_sig(crypto: Crypto, hash: &H256, sig_bytes: &[u8]) -> Result<Vec<u8>, ()> {
if sig_bytes.len() != SIGNATURE_BYTES_LEN {
return Err(());
}
let sig = Signature::from(sig_bytes);
match crypto {
Crypto::DEFAULT => sig
.recover(&hash)
.map(|pubkey| pubkey.to_vec())
.map_err(|_| ()),
_ => {
warn!("Unexpected crypto");
Err(())
}
}
}
pub struct SysConfigInfo {
pub block_quota_limit: u64,
pub account_quota_limit: AccountGasLimit,
pub check_quota: bool,
pub admin_address: Option<Address>,
pub version: Option<u32>,
}
#[derive(Debug, PartialEq)]
enum ChainId {
V0(u32),
V1(U256),
}
pub struct MsgHandler {
rx_sub: Receiver<(String, Vec<u8>)>,
tx_pub: Sender<(String, Vec<u8>)>,
// only cache verify sig
cache: LruCache<H256, Option<Vec<u8>>>,
chain_id: Option<ChainId>,
history_heights: HistoryHeights,
history_hashes: HashMap<u64, HashSet<H256>>,
dispatcher: Dispatcher,
tx_request: Sender<Request>,
tx_pool_limit: usize,
is_snapshot: bool,
black_list_cache: HashMap<Address, i8>,
is_need_proposal_new_block: bool,
config_info: SysConfigInfo,
block_txn_req: Option<BlockTxnReq>,
verify_block_req: Option<VerifyBlockReq>,
}
impl MsgHandler {
pub fn new(
rx_sub: Receiver<(String, Vec<u8>)>,
tx_pub: Sender<(String, Vec<u8>)>,
dispatcher: Dispatcher,
tx_request: Sender<Request>,
tx_pool_limit: usize,
tx_verify_thread_num: usize,
tx_verify_cache_size: usize,
) -> Self {
ThreadPoolBuilder::new()
.num_threads(tx_verify_thread_num)
.build_global()
.unwrap();
MsgHandler {
rx_sub,
tx_pub,
cache: LruCache::new(tx_verify_cache_size),
chain_id: None,
history_heights: HistoryHeights::new(),
history_hashes: HashMap::with_capacity(BLOCKLIMIT as usize),
dispatcher,
tx_request,
tx_pool_limit,
is_snapshot: false,
black_list_cache: HashMap::new(),
is_need_proposal_new_block: false,
config_info: SysConfigInfo {
block_quota_limit: 0,
account_quota_limit: AccountGasLimit::new(),
check_quota: false,
admin_address: None,
version: None,
},
block_txn_req: None,
verify_block_req: None,
}
}
fn is_ready(&self) -> bool {
self.history_heights.is_init()
&& self.chain_id.is_some()
&& !self.is_snapshot
&& self.config_info.version.is_some()
}
fn is_flow_control(&self, tx_count: usize) -> bool {
self.tx_pool_limit != 0 && tx_count + self.dispatcher.tx_pool_len() > self.tx_pool_limit
}
#[allow(unknown_lints, clippy::option_option)] // TODO clippy
fn get_ret_from_cache(&self, tx_hash: &H256) -> Option<Option<Vec<u8>>> {
self.cache.peek(tx_hash).cloned()
}
fn save_ret_to_cache(&mut self, tx_hash: H256, option_pubkey: Option<Vec<u8>>) {
self.cache.put(tx_hash, option_pubkey);
}
pub fn verify_tx_quota(&self, quota: u64, signer: &[u8]) -> bool {
if quota > self.config_info.block_quota_limit {
return false;
}
if self.config_info.check_quota {
let addr = pubkey_to_address(&PubKey::from(signer));
let mut quota_limit = self
.config_info
.account_quota_limit
.get_common_quota_limit();
if let Some(value) = self
.config_info
.account_quota_limit
.get_specific_quota_limit()
.get(&addr.lower_hex())
{
quota_limit = *value;
}
if quota > quota_limit {
return false;
}
}
true
}
// verify to and version and min quota
fn verify_request(&self, req: &Request) -> Result<(), Error> {
let un_tx = req.get_un_tx();
let tx = un_tx.get_transaction();
let tx_version = tx.get_version();
if tx_version != self.config_info.version.unwrap() {
info!(
"invalid version: tx_verion-{:?}, chain_version-{:?}",
tx_version,
self.config_info.version.unwrap()
);
return Err(Error::InvalidVersion);
}
if tx_version == 0 {
// new to must be empty
if !tx.get_to_v1().is_empty() {
return Err(Error::InvalidValue);
}
let to = clean_0x(tx.get_to());
if !to.is_empty() && Address::from_str(to).is_err() {
return Err(Error::InvalidValue);
}
} else if tx_version < 3 {
// old to must be empty
if !tx.get_to().is_empty() {
return Err(Error::InvalidValue);
}
// check to_v1
let to = tx.get_to_v1();
if !to.is_empty() && to.len() != 20 {
return Err(Error::InvalidValue);
}
} else {
error!("unexpected version {}!", tx_version);
return Err(Error::InvalidValue);
}
if !verify_base_quota_required(tx) {
return Err(Error::QuotaNotEnough);
}
Ok(())
}
/// Verify black list
fn verify_black_list(&self, req: &VerifyTxReq) -> Result<(), Error> {
if let Some(credit) = self
.black_list_cache
.get(&pubkey_to_address(&PubKey::from_slice(req.get_signer())))
{
if *credit < 0 {
Err(Error::Forbidden)
} else {
Ok(())
}
} else {
Ok(())
}
}
fn verify_tx_req_chain_id(&self, req: &VerifyTxReq) -> Result<(), Error> {
let version = self.config_info.version.unwrap();
let chain_id = match version {
0 => {
// new chain id must be empty
if !req.get_chain_id_v1().is_empty() {
None
} else {
let chain_id = req.get_chain_id();
Some(ChainId::V0(chain_id))
}
}
version if version < 3 => {
// old chain id must be empty
if req.get_chain_id() != 0 || req.get_chain_id_v1().len() != 32 {
None
} else {
let chain_id = U256::from(req.get_chain_id_v1());
Some(ChainId::V1(chain_id))
}
}
_ => {
error!("unexpected version {}!", version);
None
}
};
if chain_id != self.chain_id {
trace!(
"tx chain_id {:?}, self.chain_id {:?}",
chain_id.unwrap(),
self.chain_id
);
return Err(Error::BadChainId);
}
Ok(())
}
// verify chain id, nonce, value, valid_until_block, dup, quota and black list
fn verify_tx_req(&self, req: &VerifyTxReq) -> Result<(), Error> {
let ret = self.verify_tx_req_chain_id(req);
if ret.is_err() {
return ret;
}
if req.get_nonce().len() > 128 {
return Err(Error::InvalidNonce);
}
if req.get_value().len() != 32 {
return Err(Error::InvalidValue);
}
if self
.config_info
.admin_address
.map(|admin| pubkey_to_address(&PubKey::from_slice(req.get_signer())) != admin)
.unwrap_or_else(|| false)
{
return Err(Error::Forbidden);
}
let valid_until_block = req.get_valid_until_block();
let next_height = self.history_heights.next_height();
if valid_until_block < next_height || valid_until_block >= (next_height + BLOCKLIMIT) {
return Err(Error::InvalidUntilBlock);
}
let tx_hash = H256::from_slice(req.get_tx_hash());
for (height, hashes) in &self.history_hashes {
if hashes.contains(&tx_hash) {
trace!(
"Tx with hash {:?} has already existed in height:{}",
tx_hash,
height
);
return Err(Error::Dup);
}
}
if !self.verify_tx_quota(req.get_quota(), req.get_signer()) {
return Err(Error::QuotaNotEnough);
}
Ok(())
}
fn publish_tx_failed_result(&self, request_id: Vec<u8>, ret: &Error) {
let result = format!("{:?}", ret);
let mut response = Response::new();
response.set_request_id(request_id);
response.set_code(ErrorCode::tx_auth_error());
response.set_error_msg(result);
trace!("response new tx {:?}", response);
let msg: Message = response.into();
self.tx_pub
.send((
routing_key!(Auth >> Response).into(),
msg.try_into().unwrap(),
))
.unwrap();
}
fn publish_tx_success_result(&self, request_id: Vec<u8>, tx_hash: H256) {
let mut response = Response::new();
response.set_request_id(request_id);
let tx_response = TxResponse::new(tx_hash, TX_OK.to_string());
let tx_state = serde_json::to_string(&tx_response).unwrap();
response.set_tx_state(tx_state);
let msg: Message = response.into();
self.tx_pub
.send((
routing_key!(Auth >> Response).into(),
msg.try_into().unwrap(),
))
.unwrap();
}
fn forward_request(&self, tx_req: Request) {
let _ = self.tx_request.send(tx_req);
}
fn send_single_block_tx_hashes_req(&mut self, height: u64) {
let mut req = BlockTxHashesReq::new();
req.set_height(height);
let msg: Message = req.into();
self.tx_pub
.send((
routing_key!(Auth >> BlockTxHashesReq).into(),
msg.try_into().unwrap(),
))
.unwrap();
}
fn send_block_tx_hashes_req(&mut self, check: bool) {
// we will send req for all height
// so don't too frequent
if check && self.history_heights.is_too_frequent() {
return;
}
trace!(
"send block tx hashes request {} to {}",
self.history_heights.min_height(),
self.history_heights.max_height()
);
for i in self.history_heights.min_height()..self.history_heights.max_height() {
if !self.history_hashes.contains_key(&i) {
self.send_single_block_tx_hashes_req(i);
}
}
self.history_heights.update_time_stamp();
}
fn daily_task(&mut self) {
if self.is_need_proposal_new_block && self.is_ready() {
self.dispatcher.proposal_tx_list(
(self.history_heights.next_height() - 1) as usize, // todo fix bft
&self.tx_pub,
&self.config_info,
);
// after proposal new block clear flag
self.is_need_proposal_new_block = false;
}
}
fn get_chain_id(&mut self) {
if self.chain_id.is_none() && self.config_info.version.is_some() {
trace!("chain id is not ready");
let msg: Message = MiscellaneousReq::new().into();
if let Ok(rabbit_mq_msg) = msg.try_into() {
if let Err(e) = self
.tx_pub
.send((routing_key!(Auth >> MiscellaneousReq).into(), rabbit_mq_msg))
{
error!("Send MiscellaneousReq message error {:?}", e);
}
} else {
error!("Can not get rabbit mq message from MiscellaneousReq.");
}
}
}
fn process_msg(&mut self) {
if let Ok((key, payload)) = self.rx_sub.recv_timeout(Duration::new(3, 0)) {
if Message::try_from(&payload).is_err() {
error!("Can not get message from payload {:?}", &payload);
return;
}
let mut msg = Message::try_from(&payload).unwrap();
let rounting_key = RoutingKey::from(&key);
trace!("process message key = {}", key);
match rounting_key {
// we got this message when chain reach new height or response the BlockTxHashesReq
routing_key!(Chain >> BlockTxHashes) => {
if let Some(block_tx_hashes) = msg.take_block_tx_hashes() {
self.deal_block_tx_hashes(&block_tx_hashes)
} else {
error!("Can not get block tx hashes from message {:?}.", msg);
}
}
routing_key!(Executor >> BlackList) => {
if let Some(black_list) = msg.take_black_list() {
self.deal_black_list(&black_list);
} else {
error!("Can not get black list from message {:?}.", msg);
}
}
routing_key!(Net >> Request) | routing_key!(Jsonrpc >> RequestNewTxBatch) => {
if let Some(newtx_req) = msg.take_request() {
let is_local = rounting_key.is_sub_module(SubModules::Jsonrpc);
self.deal_request(is_local, newtx_req);
} else {
error!("Can not get request from message {:?}.", msg);
}
}
routing_key!(Executor >> Miscellaneous) => {
if let Some(miscellaneous) = msg.take_miscellaneous() {
self.deal_miscellaneous(&miscellaneous);
} else {
error!("Can not get miscellaneous from message {:?}.", msg);
}
}
routing_key!(Snapshot >> SnapshotReq) => {
if let Some(snapshot_req) = msg.take_snapshot_req() {
self.deal_snapshot(&snapshot_req);
} else {
error!("Can not get snapshot from message {:?}.", msg);
}
}
routing_key!(Net >> GetBlockTxn) => {
if let Some(mut get_block_txn) = msg.take_get_block_txn() {
let origin = msg.get_origin();
self.deal_get_block_txn(&mut get_block_txn, origin);
} else {
error!("Can not get block txn from message {:?}.", msg);
}
}
// Compact proposal
routing_key!(Consensus >> VerifyBlockReq) => {
if !self.is_ready() {
info!("Net/Consensus >> CompactProposal: auth is not ready");
} else {
self.deal_signed_proposal(msg);
}
}
routing_key!(Net >> BlockTxn) => {
if !self.is_ready() || self.verify_block_req.is_none() {
info!("Net >> BlockTxn: auth is not ready");
} else {
let verify_block_req = self.verify_block_req.clone();
if let Some(verify_block_req) = verify_block_req {
self.deal_block_txn(msg, verify_block_req);
}
}
}
_ => {
error!("receive unexpected message key {}", key);
}
}
}
}
pub fn handle_remote_msg(&mut self) {
loop {
// send request to get chain id if we have not got it
// chain id need version
// so get chain id after get version
self.get_chain_id();
// block hashes of some height we not have
// we will send request for these height
if !self.history_heights.is_init() {
trace!("history block hashes is not ready");
self.send_block_tx_hashes_req(true);
}
// Daily tasks
self.daily_task();
// process message from MQ
self.process_msg();
}
}
fn deal_block_tx_hashes(&mut self, block_tx_hashes: &BlockTxHashes) {
let height = block_tx_hashes.get_height();
info!("get block tx hashes for height {:?}", height);
let tx_hashes = block_tx_hashes.get_tx_hashes();
// because next height init value is 1
// the empty chain first msg height is 0 with quota info
if height >= self.history_heights.next_height()
|| (self.history_heights.next_height() == 1 && height == 0)
{
// get latest quota info from chain
let block_quota_limit = block_tx_hashes.get_block_quota_limit();
let account_quota_limit = block_tx_hashes.get_account_quota_limit().clone();
let check_quota = block_tx_hashes.get_check_quota();
self.config_info.check_quota = check_quota;
self.config_info.block_quota_limit = block_quota_limit;
self.config_info.account_quota_limit = account_quota_limit;
self.config_info.admin_address = if block_tx_hashes.get_admin_address().is_empty() {
None
} else {
Some(Address::from(block_tx_hashes.get_admin_address()))
};
let block_tx_version = block_tx_hashes.get_version();
let check_version = block_tx_version > 0 && block_tx_version < 3;
// Get chain id according to version
if check_version && self.config_info.version == Some(0) {
trace!("Fetch new chain id");
let msg: Message = MiscellaneousReq::new().into();
self.tx_pub
.send((
routing_key!(Auth >> MiscellaneousReq).into(),
msg.try_into().unwrap(),
))
.unwrap();
}
self.config_info.version = Some(block_tx_hashes.get_version());
// need to proposal new block
self.is_need_proposal_new_block = true;
}
// update history_heights
let old_min_height = self.history_heights.min_height();
self.history_heights.update_height(height);
// update tx pool
let mut tx_hashes_h256 = HashSet::with_capacity(tx_hashes.len());
for data in tx_hashes.iter() {
let hash = H256::from_slice(data);
tx_hashes_h256.insert(hash);
}
self.dispatcher.del_txs_from_pool_with_hash(&tx_hashes_h256);
// update history_hashes
for i in old_min_height..self.history_heights.min_height() {
self.history_hashes.remove(&i);
}
self.history_hashes.entry(height).or_insert(tx_hashes_h256);
}
fn deal_black_list(&mut self, black_list: &BlackList) {
black_list
.get_clear_list()
.iter()
.for_each(|clear_list: &Vec<u8>| {
let address = Address::from_slice(clear_list.as_slice());
if address == Address::default() {
debug!("Clean blacklist when cita-executor restarts ");
self.black_list_cache.clear();
return;
}
self.black_list_cache.remove(&address);
});
black_list
.get_black_list()
.iter()
.for_each(|blacklist: &Vec<u8>| {
self.black_list_cache
.entry(Address::from_slice(blacklist.as_slice()))
.and_modify(|e| {
if *e >= 0 {
*e -= 1;
}
})
.or_insert(3);
debug!("Current black list is {:?}", self.black_list_cache);
});
}
#[allow(unknown_lints, clippy::cognitive_complexity)] // TODO clippy
fn deal_request(&mut self, is_local: bool, newtx_req: Request) {
if newtx_req.has_batch_req() {
let batch_new_tx = newtx_req.get_batch_req().get_new_tx_requests();
trace!(
"get batch new tx request has {} tx, is local? {}",
batch_new_tx.len(),
is_local
);
if !self.is_ready() {
if is_local {
for tx_req in batch_new_tx.iter() {
let request_id = tx_req.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &Error::NotReady);
}
}
return;
}
if self.is_flow_control(batch_new_tx.len()) {
trace!("flow control ...");
if is_local {
for tx_req in batch_new_tx.iter() {
let request_id = tx_req.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &Error::Busy);
}
}
return;
}
let mut requests = HashMap::new();
let mut requests_no_cached = HashMap::new();
for tx_req in batch_new_tx {
let req = tx_req.get_un_tx().tx_verify_req_msg();
let tx_hash = H256::from_slice(req.get_tx_hash());
if let Some(option_pubkey) = self.get_ret_from_cache(&tx_hash) {
if option_pubkey.is_none() {
if is_local {
let request_id = tx_req.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &Error::BadSig);
}
continue;
}
let mut new_req = req.clone();
new_req.set_signer(option_pubkey.unwrap());
requests.insert(tx_hash, (new_req, tx_req, true));
} else {
requests_no_cached.insert(tx_hash, req.clone());
requests.insert(tx_hash, (req, tx_req, true));
}
}
let results: Vec<(H256, Option<Vec<u8>>)> = requests_no_cached
.into_par_iter()
.map(|(tx_hash, ref req)| {
let result = verify_tx_sig(
req.get_crypto(),
&H256::from(req.get_hash()),
&req.get_signature(),
);
match result {
Ok(pubkey) => (tx_hash, Some(pubkey)),
Err(_) => (tx_hash, None),
}
})
.collect();
for (tx_hash, option_pubkey) in results {
self.save_ret_to_cache(tx_hash, option_pubkey.clone());
if let Some(pubkey) = option_pubkey {
if let Some(ref mut v) = requests.get_mut(&tx_hash) {
v.0.set_signer(pubkey);
}
} else if let Some(ref mut v) = requests.get_mut(&tx_hash) {
if is_local {
let request_id = v.1.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &Error::BadSig);
}
v.2 = false;
}
}
// other verify
requests
.into_iter()
.filter(|(_tx_hash, (_req, _tx_req, flag))| *flag)
.filter(|(_tx_hash, (ref req, ref tx_req, _flag))| {
if let Err(e) = self.verify_black_list(&req) {
if is_local {
let request_id = tx_req.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &e);
}
false
} else {
true
}
})
.filter(|(_tx_hash, (ref _req, ref tx_req, _flag))| {
if let Err(e) = self.verify_request(tx_req) {
if is_local {
let request_id = tx_req.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &e);
}
false
} else {
true
}
})
.filter(|(_tx_hash, (ref req, ref tx_req, _flag))| {
if let Err(e) = self.verify_tx_req(&req) {
if is_local {
let request_id = tx_req.get_request_id().to_vec();
self.publish_tx_failed_result(request_id, &e);
}
false
} else {
true
}
})
.for_each(|(tx_hash, (req, tx_req, _flag))| {
let mut signed_tx = SignedTransaction::new();
signed_tx.set_transaction_with_sig(tx_req.get_un_tx().clone());
signed_tx.set_signer(req.get_signer().to_vec());
signed_tx.set_tx_hash(tx_hash.to_vec());
let request_id = tx_req.get_request_id().to_vec();
if self.dispatcher.add_tx_to_pool(&signed_tx) {
if is_local {
self.publish_tx_success_result(request_id, tx_hash);
}
// new tx need forward to other nodes
self.forward_request(tx_req.clone());
} else if is_local {
// dup with transaction in tx pool
self.publish_tx_failed_result(request_id, &Error::Dup);
}
});
} else if newtx_req.has_un_tx() {
trace!("get single new tx request from Jsonrpc");
let request_id = newtx_req.get_request_id().to_vec();
if !self.is_ready() {
trace!("net || jsonrpc: auth is not ready");
if is_local {
self.publish_tx_failed_result(request_id, &Error::NotReady);
}
return;
}
if self.is_flow_control(1) {
trace!("flow control ...");
if is_local {
self.publish_tx_failed_result(request_id, &Error::Busy);
}
return;
}
let mut req = newtx_req.get_un_tx().tx_verify_req_msg();
// verify with cache
let tx_hash = H256::from_slice(req.get_tx_hash());
if let Some(option_pubkey) = self.get_ret_from_cache(&tx_hash) {
if option_pubkey.is_none() {
self.publish_tx_failed_result(request_id, &Error::BadSig);
return;
}
req.set_signer(option_pubkey.unwrap());
} else {
let result = verify_tx_sig(
req.get_crypto(),
&H256::from(req.get_hash()),
&req.get_signature(),
);
self.save_ret_to_cache(tx_hash, result.clone().ok());
match result {
Ok(pubkey) => {
req.set_signer(pubkey);
}
Err(_) => {
if is_local {
self.publish_tx_failed_result(request_id, &Error::BadSig);
}
return;
}
}
}
// black verify
if let Err(e) = self.verify_black_list(&req) {
if is_local {
self.publish_tx_failed_result(request_id, &e);
}
return;
}
if let Err(e) = self.verify_request(&newtx_req) {
if is_local {
self.publish_tx_failed_result(request_id, &e);
}
return;
}
// other verify
if let Err(e) = self.verify_tx_req(&req) {
if is_local {
self.publish_tx_failed_result(request_id, &e);
}
return;
}
// add tx pool
let mut signed_tx = SignedTransaction::new();
signed_tx.set_transaction_with_sig(newtx_req.get_un_tx().clone());
signed_tx.set_signer(req.get_signer().to_vec());
signed_tx.set_tx_hash(tx_hash.to_vec());
if self.dispatcher.add_tx_to_pool(&signed_tx) {
if is_local {
self.publish_tx_success_result(request_id, tx_hash);
}
// new tx need forward to other nodes
self.forward_request(newtx_req);
} else if is_local {
// dup with transaction in tx pool
self.publish_tx_failed_result(request_id, &Error::Dup);
}
}
}
fn deal_snapshot(&mut self, snapshot_req: &SnapshotReq) {
match snapshot_req.cmd {
Cmd::Snapshot => {
info!("receive Snapshot::Snapshot: {:?}", snapshot_req);
snapshot_response(&self.tx_pub, Resp::SnapshotAck, true);
}
Cmd::Begin => {
info!("receive Snapshot::Begin: {:?}", snapshot_req);
self.is_snapshot = true;
snapshot_response(&self.tx_pub, Resp::BeginAck, true);
}
Cmd::Restore => {
info!("receive Snapshot::Restore: {:?}", snapshot_req);
snapshot_response(&self.tx_pub, Resp::RestoreAck, true);
}
Cmd::Clear => {
info!("receive Snapshot::Clear: {:?}", snapshot_req);
self.dispatcher.clear_txs_pool(0);
self.cache.clear();
self.history_heights.reset();
self.history_hashes.clear();
self.black_list_cache.clear();
snapshot_response(&self.tx_pub, Resp::ClearAck, true);
}
Cmd::End => {
info!("receive Snapshot::End: {:?}", snapshot_req);
self.send_single_block_tx_hashes_req(snapshot_req.end_height);
self.is_snapshot = false;
snapshot_response(&self.tx_pub, Resp::EndAck, true);
}
}
}
fn deal_miscellaneous(&mut self, miscellaneous: &Miscellaneous) {
if let Some(version) = self.config_info.version {
self.chain_id = if version == 0 {
Some(ChainId::V0(miscellaneous.chain_id))
} else if version < 3 {
if miscellaneous.chain_id_v1.len() == 32 {
Some(ChainId::V1(U256::from(
miscellaneous.chain_id_v1.as_slice(),
)))
} else {
None
}
} else {
error!("unexpected version {}!", version);
None
};
info!("Get chain_id({:?}) from executor", self.chain_id);
}
}
fn deal_get_block_txn(&mut self, get_block_txn: &mut GetBlockTxn, origin: Origin) {
let short_ids: Vec<H256> = get_block_txn
.get_short_ids()
.iter()
.map(|id| H256::from(U256::from(id.as_slice())))
.collect();
let txs: Vec<UnverifiedTransaction> = self
.dispatcher
.get_txs(&short_ids)
.into_iter()
.map(|mut tx| tx.take_transaction_with_sig())
.collect();
info!("GetBlockTxn size: {}, origin: {}", txs.len(), origin);
let mut block_txn = BlockTxn::new();
block_txn.set_block_hash(get_block_txn.take_block_hash());
block_txn.set_transactions(txs.into());
let msg = Message::init(OperateType::Single, origin, block_txn.into());
self.tx_pub
.send((
routing_key!(Auth >> BlockTxn).into(),
(&msg).try_into().unwrap(),
))
.unwrap();
}
fn deal_signed_proposal(&mut self, mut msg: Message) {
let verify_block_req = msg.take_verify_block_req().unwrap();
let block_hash = verify_block_req.get_block().crypt_hash();
let tx_hashes = verify_block_req.get_block().get_body().transaction_hashes();
let origin = msg.get_origin();
{
if tx_hashes.is_empty() {
return;
};
// Check tx hash in cache
for tx_hash in tx_hashes.clone() {
if let Some(option_pubkey) = self.get_ret_from_cache(&tx_hash) {
// BadSig
if option_pubkey.is_none() {
let resp = verify_block_req.reply(Err(()));
let msg = Message::init(OperateType::Single, origin, resp.into());
self.tx_pub
.send((
routing_key!(Auth >> VerifyBlockResp).into(),
(&msg).try_into().unwrap(),
))
.unwrap();
return;
}
}
}
}
// Check missing hashes
let missing_hashes = self.dispatcher.check_missing(tx_hashes.clone());
if missing_hashes.is_empty() {
// TODO: Refactor
let transactions = self.dispatcher.get_txs(&tx_hashes);
if let Err(err) = verify_block_req.check_txs(&transactions[..]) {
error!("verify_block_req check txs failed {:?}", err);
}
let resp = verify_block_req.reply(Ok(transactions));
let msg = Message::init(OperateType::Single, 0, resp.into());
self.tx_pub
.send((
routing_key!(Auth >> VerifyBlockResp).into(),
(&msg).try_into().unwrap(),
))
.unwrap();
} else {
info!("missing_hashes len : {}", missing_hashes.len());
self.verify_block_req = Some(verify_block_req);
let mut get_block_txn = GetBlockTxn::new();
get_block_txn.set_block_hash(block_hash.to_vec());
let missing_hashes = missing_hashes
.into_iter()
.map(|hash| hash.to_vec())
.collect();
get_block_txn.set_short_ids(missing_hashes);
self.block_txn_req = Some((origin, get_block_txn.clone()));
let msg = Message::init(OperateType::Single, origin, get_block_txn.into());
self.tx_pub
.send((
routing_key!(Auth >> GetBlockTxn).into(),