Skip to content

Commit 6666499

Browse files
Seulgi Kimsgkim126
authored andcommitted
Replace Deref for hash to AsRef and AsMut
The new hashes will not implement `Deref`.
1 parent 3fa3035 commit 6666499

File tree

33 files changed

+100
-112
lines changed

33 files changed

+100
-112
lines changed

basic_module/account/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn check(signed_tx: &SignedTransaction) -> bool {
3737
let signature = signed_tx.signature;
3838
let network_id = signed_tx.tx.network_id;
3939

40-
check_network_id(network_id) && verify(&signature, &signed_tx.tx.hash(), &signed_tx.signer_public)
40+
check_network_id(network_id) && verify(&signature, signed_tx.tx.hash().as_ref(), &signed_tx.signer_public)
4141
}
4242

4343
fn check_network_id(network_id: NetworkId) -> bool {

basic_module/staking/src/transactions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct SignedTransaction {
3838
impl SignedTransaction {
3939
pub fn verify(&self) -> bool {
4040
let message = self.tx.hash();
41-
verify(&self.signature, &message, &self.signer_public)
41+
verify(&self.signature, message.as_ref(), &self.signer_public)
4242
}
4343
}
4444

core/src/blockchain/blockchain.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn get_or_insert_with<F: FnOnce() -> BlockHash>(db: &dyn KeyValueDB, key: &[u8],
6262
let hash = default();
6363

6464
let mut batch = DBTransaction::new();
65-
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, &hash);
65+
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, hash.as_ref());
6666
db.write(batch).expect("Low level database error. Some issue with disk?");
6767
hash
6868
}
@@ -136,9 +136,9 @@ impl BlockChain {
136136
best_block: block.into_inner(),
137137
});
138138

139-
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, hash);
139+
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, hash.as_ref());
140140
*self.pending_best_block_hash.write() = Some(*hash);
141-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_BLOCK_KEY, hash);
141+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_BLOCK_KEY, hash.as_ref());
142142
*self.pending_best_proposal_block_hash.write() = Some(*hash);
143143
}
144144

@@ -178,11 +178,11 @@ impl BlockChain {
178178

179179
if let Some(best_block_hash) = best_block_changed.new_best_hash() {
180180
let mut pending_best_block_hash = self.pending_best_block_hash.write();
181-
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, &best_block_hash);
181+
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, best_block_hash.as_ref());
182182
*pending_best_block_hash = Some(best_block_hash);
183183

184184
let mut pending_best_proposal_block_hash = self.pending_best_proposal_block_hash.write();
185-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_BLOCK_KEY, &*new_block_hash);
185+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_BLOCK_KEY, new_block_hash.as_ref());
186186
*pending_best_proposal_block_hash = Some(new_block_hash);
187187
}
188188

@@ -316,11 +316,11 @@ impl BlockChain {
316316
self.body_db.update_best_block(batch, &best_block_changed);
317317

318318
let mut pending_best_block_hash = self.pending_best_block_hash.write();
319-
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, &block_hash);
319+
batch.put(db::COL_EXTRA, BEST_BLOCK_KEY, block_hash.as_ref());
320320
*pending_best_block_hash = Some(block_hash);
321321

322322
let mut pending_best_proposal_block_hash = self.pending_best_proposal_block_hash.write();
323-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_BLOCK_KEY, &*block_hash);
323+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_BLOCK_KEY, block_hash.as_ref());
324324
*pending_best_proposal_block_hash = Some(block_hash);
325325

326326
ChainUpdateResult::new(&best_block_changed)

core/src/blockchain/body_db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl BodyDB {
5555
let genesis_hash = genesis.hash();
5656
if bdb.block_body(&genesis_hash).is_none() {
5757
let mut batch = DBTransaction::new();
58-
batch.put(db::COL_BODIES, &genesis_hash, &Self::block_to_body(genesis));
58+
batch.put(db::COL_BODIES, genesis_hash.as_ref(), &Self::block_to_body(genesis));
5959

6060
bdb.db.write(batch).expect("Low level database error. Some issue with disk?");
6161
}
@@ -76,7 +76,7 @@ impl BodyDB {
7676
let compressed_body = compress(&Self::block_to_body(block), blocks_swapper());
7777

7878
// store block in db
79-
batch.put(db::COL_BODIES, &hash, &compressed_body);
79+
batch.put(db::COL_BODIES, hash.as_ref(), &compressed_body);
8080
}
8181

8282
pub fn update_best_block(&self, batch: &mut DBTransaction, best_block_changed: &BestBlockChanged) {
@@ -164,7 +164,7 @@ impl BodyProvider for BodyDB {
164164

165165
// Read from DB and populate cache
166166
let compressed_body =
167-
self.db.get(db::COL_BODIES, hash).expect("Low level database error. Some issue with disk?")?;
167+
self.db.get(db::COL_BODIES, hash.as_ref()).expect("Low level database error. Some issue with disk?")?;
168168

169169
let raw_body = decompress(&compressed_body, blocks_swapper());
170170
let mut lock = self.body_cache.lock();

core/src/blockchain/extras.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::db::Key;
1818
use crate::types::TransactionId;
1919
use ctypes::{BlockHash, BlockNumber, TransactionIndex, TxHash};
2020
use primitives::{H256, H264};
21-
use std::ops::Deref;
2221

2322
/// Represents index of extra data in database
2423
#[derive(Copy, Debug, Hash, Eq, PartialEq, Clone)]
@@ -33,18 +32,16 @@ enum ExtrasIndex {
3332

3433
fn with_index(hash: &H256, i: ExtrasIndex) -> H264 {
3534
let mut result = H264::default();
36-
result[0] = i as u8;
37-
(*result)[1..].copy_from_slice(hash);
35+
result.as_mut()[0] = i as u8;
36+
result.as_mut()[1..].copy_from_slice(hash.as_ref());
3837
result
3938
}
4039

4140
pub struct BlockNumberKey([u8; 5]);
4241

43-
impl Deref for BlockNumberKey {
44-
type Target = [u8];
45-
46-
fn deref(&self) -> &Self::Target {
47-
&self.0
42+
impl AsRef<[u8]> for BlockNumberKey {
43+
fn as_ref(&self) -> &[u8] {
44+
self.0.as_ref()
4845
}
4946
}
5047

core/src/blockchain/headerchain.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ impl HeaderChain {
7777
};
7878

7979
let mut batch = DBTransaction::new();
80-
batch.put(db::COL_HEADERS, &hash, genesis.rlp().as_raw());
80+
batch.put(db::COL_HEADERS, hash.as_ref(), genesis.rlp().as_raw());
8181

8282
batch.write(db::COL_EXTRA, &hash, &details);
8383
batch.write(db::COL_EXTRA, &genesis.number(), &hash);
8484

85-
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, &hash);
86-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, &hash);
85+
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, hash.as_ref());
86+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, hash.as_ref());
8787
db.write(batch).expect("Low level database error. Some issue with disk?");
8888
hash
8989
}
@@ -127,7 +127,7 @@ impl HeaderChain {
127127
}
128128

129129
let compressed_header = compress(header.rlp().as_raw(), blocks_swapper());
130-
batch.put(db::COL_HEADERS, &hash, &compressed_header);
130+
batch.put(db::COL_HEADERS, hash.as_ref(), &compressed_header);
131131

132132
let mut new_hashes = HashMap::new();
133133
new_hashes.insert(header.number(), hash);
@@ -151,9 +151,9 @@ impl HeaderChain {
151151
assert!(self.pending_best_header_hash.read().is_none());
152152
assert!(self.pending_best_proposal_block_hash.read().is_none());
153153

154-
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, hash);
154+
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, hash.as_ref());
155155
*self.pending_best_header_hash.write() = Some(*hash);
156-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, hash);
156+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, hash.as_ref());
157157
*self.pending_best_proposal_block_hash.write() = Some(*hash);
158158
}
159159

@@ -181,7 +181,7 @@ impl HeaderChain {
181181

182182
// store block in db
183183
let compressed_header = compress(header.rlp().as_raw(), blocks_swapper());
184-
batch.put(db::COL_HEADERS, &hash, &compressed_header);
184+
batch.put(db::COL_HEADERS, hash.as_ref(), &compressed_header);
185185

186186
let best_header_changed = self.best_header_changed(header, engine);
187187

@@ -191,10 +191,10 @@ impl HeaderChain {
191191
let mut pending_best_header_hash = self.pending_best_header_hash.write();
192192
let mut pending_best_proposal_header_hash = self.pending_best_proposal_block_hash.write();
193193
if let Some(best_block_hash) = best_header_changed.new_best_hash() {
194-
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, &best_block_hash);
194+
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, best_block_hash.as_ref());
195195
*pending_best_header_hash = Some(best_block_hash);
196196

197-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, &hash);
197+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, hash.as_ref());
198198
*pending_best_proposal_header_hash = Some(hash);
199199
}
200200

@@ -344,11 +344,11 @@ impl HeaderChain {
344344
new_hashes.insert(block_detail.number, block_hash);
345345

346346
let mut pending_best_header_hash = self.pending_best_header_hash.write();
347-
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, &block_hash);
347+
batch.put(db::COL_EXTRA, BEST_HEADER_KEY, block_hash.as_ref());
348348
*pending_best_header_hash = Some(block_hash);
349349

350350
let mut pending_best_proposal_block_hash = self.pending_best_proposal_block_hash.write();
351-
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, &block_hash);
351+
batch.put(db::COL_EXTRA, BEST_PROPOSAL_HEADER_KEY, block_hash.as_ref());
352352
*pending_best_proposal_block_hash = Some(block_hash);
353353

354354
let mut pending_hashes = self.pending_hashes.write();
@@ -454,7 +454,7 @@ fn block_header_data(
454454
}
455455
}
456456
// Read from DB and populate cache
457-
let b = db.get(db::COL_HEADERS, hash).expect("Low level database error. Some issue with disk?")?;
457+
let b = db.get(db::COL_HEADERS, hash.as_ref()).expect("Low level database error. Some issue with disk?")?;
458458

459459
let bytes = decompress(&b, blocks_swapper());
460460

core/src/consensus/tendermint/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl ConsensusEngine for Tendermint {
138138
}
139139
let public = validator_set[bitset_index].public_key;
140140
let delegation = validator_set[bitset_index].delegation;
141-
if !verify(&signature, &precommit_vote_on.hash(), &public) {
141+
if !verify(&signature, precommit_vote_on.hash().as_ref(), &public) {
142142
return Err(EngineError::BlockNotAuthorized(public).into())
143143
}
144144
signed_delegation += delegation;

core/src/consensus/tendermint/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl ConsensusMessage {
373373
}
374374

375375
pub fn verify(&self, signer_public: &Public) -> bool {
376-
verify(&self.signature, &self.on.hash(), signer_public)
376+
verify(&self.signature, self.on.hash().as_ref(), signer_public)
377377
}
378378
}
379379

core/src/consensus/tendermint/worker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ impl Worker {
12231223
let parent_hash = header.parent_hash();
12241224
for (bitset_index, signature) in seal_view.signatures()? {
12251225
let public = self.validators.get_current(header.parent_hash(), bitset_index);
1226-
if !verify(&signature, &precommit_vote_on.hash(), &public) {
1226+
if !verify(&signature, precommit_vote_on.hash().as_ref(), &public) {
12271227
return Err(EngineError::BlockNotAuthorized(public).into())
12281228
}
12291229
assert!(!voted_validators.is_set(bitset_index), "Double vote");

core/src/db.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use kvdb::{DBTransaction, KeyValueDB};
1818
use parking_lot::RwLock;
1919
use std::collections::HashMap;
2020
use std::hash::Hash;
21-
use std::ops::Deref;
2221

2322
// database columns
2423
/// Column for State
@@ -77,7 +76,7 @@ where
7776
/// Should be used to get database key associated with given value.
7877
pub trait Key<T> {
7978
/// The db key associated with this value.
80-
type Target: Deref<Target = [u8]>;
79+
type Target: AsRef<[u8]>;
8180

8281
/// Returns db key.
8382
fn key(&self) -> Self::Target;
@@ -89,13 +88,13 @@ pub trait Writable {
8988
fn write<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>, value: &T)
9089
where
9190
T: rlp::Encodable,
92-
R: Deref<Target = [u8]>;
91+
R: AsRef<[u8]>;
9392

9493
/// Deletes key from the databse.
9594
fn delete<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>)
9695
where
9796
T: rlp::Encodable,
98-
R: Deref<Target = [u8]>;
97+
R: AsRef<[u8]>;
9998

10099
/// Writes the value into the database and updates the cache.
101100
fn write_with_cache<K, T, R>(
@@ -108,7 +107,7 @@ pub trait Writable {
108107
) where
109108
K: Key<T, Target = R> + Hash + Eq,
110109
T: rlp::Encodable,
111-
R: Deref<Target = [u8]>, {
110+
R: AsRef<[u8]>, {
112111
self.write(col, &key, &value);
113112
match policy {
114113
CacheUpdatePolicy::Overwrite => {
@@ -130,7 +129,7 @@ pub trait Writable {
130129
) where
131130
K: Key<T, Target = R> + Hash + Eq,
132131
T: rlp::Encodable,
133-
R: Deref<Target = [u8]>, {
132+
R: AsRef<[u8]>, {
134133
match policy {
135134
CacheUpdatePolicy::Overwrite => {
136135
for (key, value) in values {
@@ -154,7 +153,7 @@ pub trait Readable {
154153
fn read<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> Option<T>
155154
where
156155
T: rlp::Decodable,
157-
R: Deref<Target = [u8]>;
156+
R: AsRef<[u8]>;
158157

159158
/// Returns value for given key either in cache or in database.
160159
fn read_with_cache<K, T, C>(&self, col: Option<u32>, cache: &mut C, key: &K) -> Option<T>
@@ -177,13 +176,13 @@ pub trait Readable {
177176
/// Returns true if given value exists.
178177
fn exists<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> bool
179178
where
180-
R: Deref<Target = [u8]>;
179+
R: AsRef<[u8]>;
181180

182181
/// Returns true if given value exists either in cache or in database.
183182
fn exists_with_cache<K, T, R, C>(&self, col: Option<u32>, cache: &RwLock<C>, key: &K) -> bool
184183
where
185184
K: Eq + Hash + Key<T, Target = R>,
186-
R: Deref<Target = [u8]>,
185+
R: AsRef<[u8]>,
187186
C: Cache<K, T>, {
188187
{
189188
let read = cache.read();
@@ -200,42 +199,42 @@ impl Writable for DBTransaction {
200199
fn write<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>, value: &T)
201200
where
202201
T: rlp::Encodable,
203-
R: Deref<Target = [u8]>, {
204-
self.put(col, &key.key(), &rlp::encode(value));
202+
R: AsRef<[u8]>, {
203+
self.put(col, key.key().as_ref(), &rlp::encode(value));
205204
}
206205

207206
fn delete<T, R>(&mut self, col: Option<u32>, key: &dyn Key<T, Target = R>)
208207
where
209208
T: rlp::Encodable,
210-
R: Deref<Target = [u8]>, {
211-
self.delete(col, &key.key());
209+
R: AsRef<[u8]>, {
210+
self.delete(col, key.key().as_ref());
212211
}
213212
}
214213

215214
impl<KVDB: KeyValueDB + ?Sized> Readable for KVDB {
216215
fn read<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> Option<T>
217216
where
218217
T: rlp::Decodable,
219-
R: Deref<Target = [u8]>, {
220-
let result = self.get(col, &key.key());
218+
R: AsRef<[u8]>, {
219+
let result = self.get(col, key.key().as_ref());
221220

222221
match result {
223222
Ok(option) => option.map(|v| rlp::decode(&v).unwrap()),
224223
Err(err) => {
225-
panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
224+
panic!("db get failed, key: {:?}, err: {:?}", key.key().as_ref(), err);
226225
}
227226
}
228227
}
229228

230229
fn exists<T, R>(&self, col: Option<u32>, key: &dyn Key<T, Target = R>) -> bool
231230
where
232-
R: Deref<Target = [u8]>, {
233-
let result = self.get(col, &key.key());
231+
R: AsRef<[u8]>, {
232+
let result = self.get(col, key.key().as_ref());
234233

235234
match result {
236235
Ok(v) => v.is_some(),
237236
Err(err) => {
238-
panic!("db get failed, key: {:?}, err: {:?}", &key.key() as &[u8], err);
237+
panic!("db get failed, key: {:?}, err: {:?}", key.key().as_ref(), err);
239238
}
240239
}
241240
}

0 commit comments

Comments
 (0)