Skip to content

Commit 7de1721

Browse files
authored
Commit to storage based on actual change instead of dirty flag. (#1721)
* fix missing from_key_bytes impl for DepositList and VoteList. * fix is_builtin check, do not include null address. * Commit to storage based on actual change instead of dirty flag. - Refactored storage_layout, StateDb. - Commit staking state in genesis block. * optimize lock for get * sample chain
1 parent e1782a4 commit 7de1721

File tree

25 files changed

+90698
-57383
lines changed

25 files changed

+90698
-57383
lines changed

accounts/cfxstore/src/account/safe_account.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl SafeAccount {
141141
let meta_plain = json::VaultKeyMeta::load(&meta_plain)
142142
.map_err(|e| Error::Custom(format!("{:?}", e)))?;
143143

144-
if !meta_plain.address.is_user_account_address() {
144+
if !Address::from_slice(&meta_plain.address).is_user_account_address() {
145145
warn!("Trying to import a non-user type account address. Are you trying to import an Ethkey for Conflux? Note that the address scheme between Ethereum and Conflux are different.");
146146
return Err(Error::Custom(format!(
147147
"Import non-user type address. Address: {:?}",

accounts/cfxstore/src/json/hash.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use super::Error;
18-
use cfx_types::address_util::AddressUtil;
1918
use rustc_hex::{FromHex, ToHex};
2019
use serde::{
2120
de::{Error as SerdeError, Visitor},
@@ -146,11 +145,3 @@ impl Eq for H160 {}
146145
impl Ord for H160 {
147146
fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) }
148147
}
149-
150-
impl AddressUtil for H160 {
151-
#[inline]
152-
fn type_byte(&self) -> &u8 { &self.0[0] }
153-
154-
#[inline]
155-
fn type_byte_mut(&mut self) -> &mut u8 { &mut self.0[0] }
156-
}

cfx_types/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub mod address_util {
3939

4040
fn type_byte_mut(&mut self) -> &mut u8;
4141

42+
fn is_null_address(&self) -> bool;
43+
4244
#[inline]
4345
fn address_type_bits(&self) -> u8 { self.type_byte() & 0xf0 }
4446

@@ -54,6 +56,7 @@ pub mod address_util {
5456
self.is_contract_address()
5557
|| self.is_user_account_address()
5658
|| self.is_builtin_address()
59+
|| self.is_null_address()
5760
}
5861

5962
#[inline]
@@ -69,6 +72,7 @@ pub mod address_util {
6972
#[inline]
7073
fn is_builtin_address(&self) -> bool {
7174
self.address_type_bits() == TYPE_BITS_BUILTIN
75+
&& !self.is_null_address()
7276
}
7377

7478
#[inline]
@@ -90,6 +94,22 @@ pub mod address_util {
9094
fn type_byte_mut(&mut self) -> &mut u8 {
9195
&mut self.as_fixed_bytes_mut()[0]
9296
}
97+
98+
#[inline]
99+
fn is_null_address(&self) -> bool { self.is_zero() }
100+
}
101+
102+
impl AddressUtil for &[u8] {
103+
#[inline]
104+
fn type_byte(&self) -> &u8 { &self[0] }
105+
106+
#[inline]
107+
fn type_byte_mut(&mut self) -> &mut u8 { unreachable!() }
108+
109+
#[inline]
110+
fn is_null_address(&self) -> bool {
111+
self.iter().all(|&byte| byte == 0u8)
112+
}
93113
}
94114

95115
#[cfg(test)]

changelogs/CHANGELOG-1.0.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Fix code() return value for uninitialized contract.
77
- Fix bug in kill_account after which the contract account is revived by simple transaction.
88
- Fix the place of collateral refund for suicide contracts.
9+
- Fix missing StorageKey conversion from bytes of DepositList and VoteList.
910

1011
## Incompatible changes
1112

core/benchmark/storage/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,7 @@ fn main() -> errors::Result<()> {
21552155
use cfx_types::hexstr_to_h256;
21562156
use cfxcore::{
21572157
block_data_manager::StateAvailabilityBoundary,
2158-
statedb::StateDb,
2158+
statedb::{StateDb, StateDbExt},
21592159
storage::{
21602160
state::StateTrait,
21612161
storage_db::key_value_db::{KeyValueDbTrait, KeyValueDbTraitRead},

core/examples/snapshot_merge_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use cfx_types::{Address, H256};
66
use cfxcore::{
7-
statedb::StateDb,
7+
statedb::{StateDb, StateDbExt},
88
storage::{
99
state::StateTrait,
1010
state_manager::{StateManager, StateManagerTrait},
@@ -426,7 +426,7 @@ where
426426
.unwrap();
427427
}
428428
let epoch = H256::random();
429-
state.commit(epoch).unwrap();
429+
state.commit(epoch, /* debug_record = */ None).unwrap();
430430
epoch
431431
}
432432

core/src/consensus/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
pow::{PowComputer, ProofOfWorkConfig},
3232
rpc_errors::Result as RpcResult,
3333
state::State,
34-
statedb::StateDb,
34+
statedb::{StateDb, StateDbExt, StateDbGetOriginalMethods},
3535
statistics::SharedStatistics,
3636
storage::state_manager::StateManagerTrait,
3737
transaction_pool::SharedTransactionPool,
@@ -552,7 +552,7 @@ impl ConsensusGraph {
552552
) -> Result<Option<StorageRoot>, String> {
553553
let state_db = self.get_state_db_by_epoch_number(epoch_number)?;
554554

555-
match state_db.get_storage_root(&address) {
555+
match state_db.get_original_storage_root(&address) {
556556
Ok(maybe_storage_root) => Ok(maybe_storage_root),
557557
Err(e) => {
558558
error!("db error occurred: {:?}", e);

core/src/executive/context.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
},
1818
};
1919
use cfx_types::{address_util::AddressUtil, Address, H256, U256};
20-
use primitives::{transaction::UNSIGNED_SENDER, StorageLayout};
20+
use primitives::transaction::UNSIGNED_SENDER;
2121
use std::sync::Arc;
2222

2323
/// Policy for handling output data on `RETURN` opcode.
@@ -327,11 +327,6 @@ impl<'a> ContextTrait for Context<'a> {
327327
self.origin.storage_owner,
328328
)?;
329329

330-
self.state.set_storage_layout(
331-
&self.origin.address,
332-
StorageLayout::Regular(0),
333-
)?;
334-
335330
Ok(*gas - return_cost)
336331
}
337332
OutputPolicy::InitContract => Ok(*gas),

core/src/executive/executive.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use crate::{
2727
};
2828
use cfx_types::{address_util::AddressUtil, Address, H256, U256, U512};
2929
use primitives::{
30-
receipt::StorageChange, transaction::Action, SignedTransaction,
30+
receipt::StorageChange, storage::STORAGE_LAYOUT_REGULAR_V0,
31+
transaction::Action, SignedTransaction, StorageLayout,
3132
};
3233
use std::{
3334
cell::RefCell,
@@ -336,7 +337,7 @@ impl<'a> CallCreateExecutive<'a> {
336337

337338
fn transfer_exec_balance_and_init_contract(
338339
params: &ActionParams, spec: &Spec, state: &mut State,
339-
substate: &mut Substate,
340+
substate: &mut Substate, storage_layout: Option<StorageLayout>,
340341
) -> vm::Result<()>
341342
{
342343
if let ActionValue::Transfer(val) = params.value {
@@ -353,6 +354,7 @@ impl<'a> CallCreateExecutive<'a> {
353354
&params.original_sender,
354355
val.saturating_add(prev_balance),
355356
state.contract_start_nonce(),
357+
storage_layout,
356358
)?;
357359
} else {
358360
// In contract creation, the `params.value` should never be
@@ -704,7 +706,11 @@ impl<'a> CallCreateExecutive<'a> {
704706
)?;
705707
state.checkpoint();
706708
Self::transfer_exec_balance_and_init_contract(
707-
&params, spec, state, substate,
709+
&params,
710+
spec,
711+
state,
712+
substate,
713+
Some(STORAGE_LAYOUT_REGULAR_V0),
708714
)?;
709715
Ok(())
710716
};

core/src/executive/executive_tests.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use cfx_types::{
2121
address_util::AddressUtil, Address, BigEndianHash, U256, U512,
2222
};
2323
use keylib::{Generator, Random};
24-
use primitives::{transaction::Action, Transaction};
24+
use primitives::{
25+
storage::STORAGE_LAYOUT_REGULAR_V0, transaction::Action, Transaction,
26+
};
2527
use rustc_hex::FromHex;
2628
use std::{
2729
cmp::{self, min},
@@ -611,7 +613,6 @@ fn test_deposit_withdraw_lock() {
611613
*state.total_issued_tokens(),
612614
U256::from(2_000_000_000_000_000_000u64)
613615
);
614-
assert_eq!(state.block_number(), 0);
615616

616617
let mut params = ActionParams::default();
617618
params.code_address = STORAGE_INTEREST_STAKING_CONTRACT_ADDRESS.clone();
@@ -646,7 +647,6 @@ fn test_deposit_withdraw_lock() {
646647
U256::from(2_000_000_000_000_000_000u64)
647648
);
648649
assert_eq!(*state.total_staking_tokens(), U256::zero());
649-
assert_eq!(state.block_number(), 0);
650650

651651
// deposit 10^18 - 1, not enough
652652
params.call_type = CallType::Call;
@@ -692,7 +692,6 @@ fn test_deposit_withdraw_lock() {
692692
*state.total_staking_tokens(),
693693
U256::from(1_000_000_000_000_000_000u64)
694694
);
695-
assert_eq!(state.block_number(), 0);
696695

697696
// empty data
698697
params.data = None;
@@ -725,7 +724,6 @@ fn test_deposit_withdraw_lock() {
725724
*state.total_staking_tokens(),
726725
U256::from(1_000_000_000_000_000_000u64)
727726
);
728-
assert_eq!(state.block_number(), 0);
729727

730728
// less data
731729
params.data = Some("b6b55f25000000000000000000000000000000000000000000000000000000174876e8".from_hex().unwrap());
@@ -758,7 +756,6 @@ fn test_deposit_withdraw_lock() {
758756
*state.total_staking_tokens(),
759757
U256::from(1_000_000_000_000_000_000u64)
760758
);
761-
assert_eq!(state.block_number(), 0);
762759

763760
// more data
764761
params.data = Some("b6b55f25000000000000000000000000000000000000000000000000000000174876e80000".from_hex().unwrap());
@@ -791,7 +788,6 @@ fn test_deposit_withdraw_lock() {
791788
*state.total_staking_tokens(),
792789
U256::from(1_000_000_000_000_000_000u64)
793790
);
794-
assert_eq!(state.block_number(), 0);
795791

796792
// withdraw
797793
params.data = Some("2e1a7d4d0000000000000000000000000000000000000000000000000000000ba43b7400".from_hex().unwrap());
@@ -820,7 +816,6 @@ fn test_deposit_withdraw_lock() {
820816
*state.total_staking_tokens(),
821817
U256::from(999_999_950_000_000_000u64)
822818
);
823-
assert_eq!(state.block_number(), 0);
824819
// withdraw more than staking balance
825820
params.data = Some("2e1a7d4d0000000000000000000000000000000000000000000000000de0b6a803288c01".from_hex().unwrap());
826821
let result = Executive::new(
@@ -854,7 +849,6 @@ fn test_deposit_withdraw_lock() {
854849
*state.total_staking_tokens(),
855850
U256::from(999_999_950_000_000_000u64)
856851
);
857-
assert_eq!(state.block_number(), 0);
858852

859853
// lock until block_number = 0
860854
params.data = Some("5547dedb00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap());
@@ -1062,6 +1056,7 @@ fn test_commission_privilege() {
10621056
&sender.address(),
10631057
U256::zero(),
10641058
U256::one(),
1059+
Some(STORAGE_LAYOUT_REGULAR_V0),
10651060
)
10661061
.expect(&concat!(file!(), ":", line!(), ":", column!()));
10671062
state.init_code(&address, code, sender.address()).unwrap();
@@ -1429,6 +1424,7 @@ fn test_storage_commission_privilege() {
14291424
&sender.address(),
14301425
U256::zero(),
14311426
U256::one(),
1427+
Some(STORAGE_LAYOUT_REGULAR_V0),
14321428
)
14331429
.expect(&concat!(file!(), ":", line!(), ":", column!()));
14341430
state.init_code(&address, code, sender.address()).unwrap();

0 commit comments

Comments
 (0)