Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.

Commit 637e803

Browse files
bors[bot]yihuang
andauthored
Merge #666
666: Problem (CRO-578 WIP): Wallet keep loading and decrypt global state r=tomtau a=yihuang Solution: - No need to encrypt global state - Store `lite::TrustedState` in global state - Cache global state in memory when syncing. Make some less controversial changes first. Co-authored-by: yihuang <[email protected]>
2 parents 1e3485c + 54746e8 commit 637e803

File tree

7 files changed

+144
-184
lines changed

7 files changed

+144
-184
lines changed

client-common/src/tendermint/lite.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Lite tendermint client
2+
use parity_scale_codec::{Decode, Encode, Error, Input, Output};
23
use serde::{Deserialize, Serialize};
4+
use serde_json;
35
use tendermint::{block::Header, validator};
46

57
use crate::tendermint::client::Client;
@@ -14,6 +16,29 @@ pub struct TrustedState {
1416
pub validators: validator::Set,
1517
}
1618

19+
impl TrustedState {
20+
/// construct genesis trusted state
21+
pub fn genesis(genesis_validators: Vec<validator::Info>) -> TrustedState {
22+
TrustedState {
23+
header: None,
24+
validators: validator::Set::new(genesis_validators),
25+
}
26+
}
27+
}
28+
29+
impl Encode for TrustedState {
30+
fn encode_to<T: Output>(&self, dest: &mut T) {
31+
serde_json::to_string(self).unwrap().encode_to(dest)
32+
}
33+
}
34+
35+
impl Decode for TrustedState {
36+
fn decode<I: Input>(value: &mut I) -> Result<Self, Error> {
37+
serde_json::from_str(&String::decode(value)?)
38+
.map_err(|_| "fail to encode trusted_state to json ".into())
39+
}
40+
}
41+
1742
/// get genesis validator set
1843
pub fn get_genesis_validators<C>(client: &C) -> CommonResult<validator::Set>
1944
where

client-core/src/handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub trait TransactionHandler: Send + Sync {
1717
&self,
1818
name: &str,
1919
passphrase: &SecUtf8,
20-
transaction: Transaction,
20+
transaction: &Transaction,
2121
block_height: u64,
2222
block_time: Time,
2323
) -> Result<()>;
@@ -26,5 +26,5 @@ pub trait TransactionHandler: Send + Sync {
2626
/// Interface for handling stream of block headers in Crypto.com Chain
2727
pub trait BlockHandler: Send + Sync {
2828
/// Handles a block header in Crypto.com Chain
29-
fn on_next(&self, name: &str, passphrase: &SecUtf8, block_header: BlockHeader) -> Result<()>;
29+
fn on_next(&self, name: &str, passphrase: &SecUtf8, block_header: &BlockHeader) -> Result<()>;
3030
}

client-core/src/handler/default_block_handler.rs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use secstr::SecUtf8;
33
use chain_core::tx::TransactionId;
44
use client_common::{BlockHeader, ErrorKind, Result, ResultExt, Storage};
55

6-
use crate::service::{GlobalStateService, KeyService, WalletService};
6+
use crate::service::{KeyService, WalletService};
77
use crate::{BlockHandler, TransactionHandler, TransactionObfuscation};
88

99
/// Default implementation of `BlockHandler`
@@ -19,7 +19,6 @@ where
1919

2020
key_service: KeyService<S>,
2121
wallet_service: WalletService<S>,
22-
global_state_service: GlobalStateService<S>,
2322
}
2423

2524
impl<O, H, S> DefaultBlockHandler<O, H, S>
@@ -36,7 +35,6 @@ where
3635
transaction_handler,
3736
key_service: KeyService::new(storage.clone()),
3837
wallet_service: WalletService::new(storage.clone()),
39-
global_state_service: GlobalStateService::new(storage),
4038
}
4139
}
4240
}
@@ -47,8 +45,8 @@ where
4745
H: TransactionHandler,
4846
S: Storage,
4947
{
50-
fn on_next(&self, name: &str, passphrase: &SecUtf8, block_header: BlockHeader) -> Result<()> {
51-
for transaction in block_header.unencrypted_transactions {
48+
fn on_next(&self, name: &str, passphrase: &SecUtf8, block_header: &BlockHeader) -> Result<()> {
49+
for transaction in block_header.unencrypted_transactions.iter() {
5250
if block_header.transaction_ids.contains(&transaction.id()) {
5351
self.transaction_handler.on_next(
5452
name,
@@ -83,7 +81,7 @@ where
8381
.transaction_obfuscation
8482
.decrypt(&block_header.enclave_transaction_ids, &private_key)?;
8583

86-
for transaction in transactions {
84+
for transaction in transactions.iter() {
8785
self.transaction_handler.on_next(
8886
name,
8987
passphrase,
@@ -93,13 +91,7 @@ where
9391
)?;
9492
}
9593
}
96-
97-
self.global_state_service.set_global_state(
98-
name,
99-
passphrase,
100-
block_header.block_height,
101-
block_header.app_hash,
102-
)
94+
Ok(())
10395
}
10496
}
10597

@@ -150,11 +142,11 @@ mod tests {
150142
&self,
151143
_name: &str,
152144
_passphrase: &SecUtf8,
153-
transaction: Transaction,
145+
transaction: &Transaction,
154146
block_height: u64,
155147
block_time: Time,
156148
) -> Result<()> {
157-
if transaction != transfer_transaction() && transaction != unbond_transaction() {
149+
if transaction != &transfer_transaction() && transaction != &unbond_transaction() {
158150
panic!("Invalid transaction")
159151
}
160152
assert_eq!(1, block_height);
@@ -228,24 +220,8 @@ mod tests {
228220
storage.clone(),
229221
);
230222

231-
let global_state_service = GlobalStateService::new(storage);
232-
233-
assert_eq!(
234-
0,
235-
global_state_service
236-
.last_block_height(name, passphrase)
237-
.unwrap()
238-
);
239-
240223
block_handler
241-
.on_next(name, passphrase, block_header)
224+
.on_next(name, passphrase, &block_header)
242225
.unwrap();
243-
244-
assert_eq!(
245-
1,
246-
global_state_service
247-
.last_block_height(name, passphrase)
248-
.unwrap()
249-
);
250226
}
251227
}

client-core/src/handler/default_transaction_handler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ where
4747
&self,
4848
name: &str,
4949
passphrase: &SecUtf8,
50-
transaction: Transaction,
50+
transaction: &Transaction,
5151
block_height: u64,
5252
block_time: Time,
5353
) -> Result<()> {
5454
let transaction_id = transaction.id();
5555
let inputs = self.decorate_inputs(name, passphrase, transaction.inputs().to_vec())?;
5656
let outputs = transaction.outputs().to_vec();
57-
let transaction_type = TransactionType::from(&transaction);
57+
let transaction_type = TransactionType::from(transaction);
5858
let balance_change =
5959
self.calculate_balance_change(name, passphrase, &transaction_id, &inputs, &outputs)?;
6060

@@ -333,7 +333,7 @@ mod tests {
333333
.on_next(
334334
name1,
335335
passphrase1,
336-
transactions[0].clone(),
336+
&transactions[0],
337337
0,
338338
Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
339339
)
@@ -358,7 +358,7 @@ mod tests {
358358
.on_next(
359359
name1,
360360
passphrase1,
361-
transactions[1].clone(),
361+
&transactions[1],
362362
1,
363363
Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
364364
)
@@ -382,7 +382,7 @@ mod tests {
382382
.on_next(
383383
name2,
384384
passphrase2,
385-
transactions[0].clone(),
385+
&transactions[0],
386386
0,
387387
Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
388388
)
@@ -407,7 +407,7 @@ mod tests {
407407
.on_next(
408408
name2,
409409
passphrase2,
410-
transactions[1].clone(),
410+
&transactions[1],
411411
1,
412412
Time::from_str("2019-04-09T09:38:41.735577Z").unwrap(),
413413
)

client-core/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod wallet_state_service;
1111
#[doc(hidden)]
1212
pub use self::wallet_state_service::WalletStateMemento;
1313

14-
pub use self::global_state_service::GlobalStateService;
14+
pub use self::global_state_service::{GlobalState, GlobalStateService};
1515
pub use self::hd_key_service::HdKeyService;
1616
pub use self::key_service::KeyService;
1717
pub use self::multi_sig_session_service::MultiSigSessionService;

0 commit comments

Comments
 (0)