Skip to content

Commit da42488

Browse files
committed
use BytesMut for encoding
1 parent 44bb218 commit da42488

File tree

7 files changed

+34
-29
lines changed

7 files changed

+34
-29
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.

crates/cfxcore/core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ rangetools = { workspace = true }
5454
rayon = { workspace = true }
5555
rlp = { workspace = true }
5656
rlp_derive = { workspace = true }
57+
bytes = { workspace = true }
5758
rustc-hex = { workspace = true }
5859
secret-store = { workspace = true }
5960
serde = { workspace = true, features = ["rc"] }
@@ -133,4 +134,4 @@ consensus_bench = []
133134
fuzzing = ["proptest", "proptest-derive"]
134135

135136
# [lints.rust]
136-
# unexpected_cfgs = { level = "warn", check-cfg = ['cfg(mirai)'] }
137+
# unexpected_cfgs = { level = "warn", check-cfg = ['cfg(mirai)'] }

crates/cfxcore/core/src/message.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub type RequestId = u64;
66
pub type MsgId = u16;
77
const MSG_ID_MAX: u16 = 1 << 14;
88

9+
use bytes::{BufMut, BytesMut};
910
pub use cfx_bytes::Bytes;
1011
pub use priority_send_queue::SendQueuePriority;
1112
use rlp::{Decodable, Rlp};
@@ -60,16 +61,16 @@ pub trait Message:
6061
// If true, message may be throttled when sent to remote peer.
6162
fn is_size_sensitive(&self) -> bool { false }
6263
fn msg_id(&self) -> MsgId;
63-
fn push_msg_id_leb128_encoding(&self, buffer: &mut Vec<u8>) {
64+
fn push_msg_id_leb128_encoding(&self, buffer: &mut BytesMut) {
6465
let msg_id = self.msg_id();
6566
assert!(msg_id < MSG_ID_MAX);
6667
let msg_id_msb = (msg_id >> 7) as u8;
6768
let mut msg_id_lsb = (msg_id as u8) & 0x7f;
6869
if msg_id_msb != 0 {
69-
buffer.push(msg_id_msb);
70+
buffer.put_u8(msg_id_msb);
7071
msg_id_lsb |= 0x80;
7172
}
72-
buffer.push(msg_id_lsb);
73+
buffer.put_u8(msg_id_lsb);
7374
}
7475
fn msg_name(&self) -> &'static str;
7576
fn priority(&self) -> SendQueuePriority { SendQueuePriority::High }
@@ -185,9 +186,9 @@ macro_rules! build_msg_basic {
185186
fn msg_name(&self) -> &'static str { $name_str }
186187

187188
fn encode(&self) -> Vec<u8> {
188-
let mut encoded = self.rlp_bytes().into();
189+
let mut encoded = self.rlp_bytes();
189190
self.push_msg_id_leb128_encoding(&mut encoded);
190-
encoded
191+
encoded.into()
191192
}
192193
}
193194
};
@@ -278,10 +279,10 @@ mod test {
278279
#[test]
279280
fn test_message_id_encode_decode() {
280281
for msg_id in 0..MSG_ID_MAX {
281-
let mut buf = vec![];
282282
let message = TestMessage { msg_id };
283-
buf.extend_from_slice(&message.rlp_bytes());
284-
message.push_msg_id_leb128_encoding(&mut buf);
283+
let mut encoded = message.rlp_bytes();
284+
message.push_msg_id_leb128_encoding(&mut encoded);
285+
let buf = encoded.as_ref();
285286
match decode_msg(&buf) {
286287
None => assert!(false, "Can not decode message"),
287288
Some((decoded_msg_id, rlp)) => {

crates/cfxcore/core/src/sync/message/message.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ impl Message for GetBlockHashesResponse {
9595
fn priority(&self) -> SendQueuePriority { SendQueuePriority::Low }
9696

9797
fn encode(&self) -> Vec<u8> {
98-
let mut encoded = self.rlp_bytes().into();
98+
let mut encoded = self.rlp_bytes();
9999
self.push_msg_id_leb128_encoding(&mut encoded);
100-
encoded
100+
encoded.into()
101101
}
102102
}
103103

@@ -112,9 +112,9 @@ impl Message for Transactions {
112112
fn msg_name(&self) -> &'static str { "Transactions" }
113113

114114
fn encode(&self) -> Vec<u8> {
115-
let mut encoded = self.rlp_bytes().into();
115+
let mut encoded = self.rlp_bytes();
116116
self.push_msg_id_leb128_encoding(&mut encoded);
117-
encoded
117+
encoded.into()
118118
}
119119
}
120120

@@ -128,9 +128,9 @@ impl Message for GetBlocksResponse {
128128
fn msg_name(&self) -> &'static str { "GetBlocksResponse" }
129129

130130
fn encode(&self) -> Vec<u8> {
131-
let mut encoded = self.rlp_bytes().into();
131+
let mut encoded = self.rlp_bytes();
132132
self.push_msg_id_leb128_encoding(&mut encoded);
133-
encoded
133+
encoded.into()
134134
}
135135
}
136136

@@ -148,9 +148,9 @@ impl Message for GetBlocksWithPublicResponse {
148148
fn msg_name(&self) -> &'static str { "GetBlocksWithPublicResponse" }
149149

150150
fn encode(&self) -> Vec<u8> {
151-
let mut encoded = self.rlp_bytes().into();
151+
let mut encoded = self.rlp_bytes();
152152
self.push_msg_id_leb128_encoding(&mut encoded);
153-
encoded
153+
encoded.into()
154154
}
155155
}
156156

@@ -164,9 +164,9 @@ impl Message for GetBlockTxnResponse {
164164
fn msg_name(&self) -> &'static str { "GetBlockTxnResponse" }
165165

166166
fn encode(&self) -> Vec<u8> {
167-
let mut encoded = self.rlp_bytes().into();
167+
let mut encoded = self.rlp_bytes();
168168
self.push_msg_id_leb128_encoding(&mut encoded);
169-
encoded
169+
encoded.into()
170170
}
171171
}
172172

@@ -182,9 +182,9 @@ impl Message for TransactionDigests {
182182
fn priority(&self) -> SendQueuePriority { SendQueuePriority::Normal }
183183

184184
fn encode(&self) -> Vec<u8> {
185-
let mut encoded = self.rlp_bytes().into();
185+
let mut encoded = self.rlp_bytes();
186186
self.push_msg_id_leb128_encoding(&mut encoded);
187-
encoded
187+
encoded.into()
188188
}
189189
}
190190

@@ -200,9 +200,9 @@ impl Message for GetTransactionsResponse {
200200
fn priority(&self) -> SendQueuePriority { SendQueuePriority::Normal }
201201

202202
fn encode(&self) -> Vec<u8> {
203-
let mut encoded = self.rlp_bytes().into();
203+
let mut encoded = self.rlp_bytes();
204204
self.push_msg_id_leb128_encoding(&mut encoded);
205-
encoded
205+
encoded.into()
206206
}
207207
}
208208
impl GetMaybeRequestId for GetTransactionsFromTxHashesResponse {}
@@ -223,9 +223,9 @@ impl Message for GetTransactionsFromTxHashesResponse {
223223
fn priority(&self) -> SendQueuePriority { SendQueuePriority::Normal }
224224

225225
fn encode(&self) -> Vec<u8> {
226-
let mut encoded = self.rlp_bytes().into();
226+
let mut encoded = self.rlp_bytes();
227227
self.push_msg_id_leb128_encoding(&mut encoded);
228-
encoded
228+
encoded.into()
229229
}
230230
}
231231
/// handle the RLP encoded message with given context `ctx`.

crates/cfxcore/core/src/sync/message/transactions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ impl Message for GetTransactions {
313313
fn priority(&self) -> SendQueuePriority { SendQueuePriority::Normal }
314314

315315
fn encode(&self) -> Vec<u8> {
316-
let mut encoded = self.rlp_bytes().into();
316+
let mut encoded = self.rlp_bytes();
317317
self.push_msg_id_leb128_encoding(&mut encoded);
318-
encoded
318+
encoded.into()
319319
}
320320
}
321321

@@ -472,9 +472,9 @@ impl Message for GetTransactionsFromTxHashes {
472472
fn priority(&self) -> SendQueuePriority { SendQueuePriority::Normal }
473473

474474
fn encode(&self) -> Vec<u8> {
475-
let mut encoded = self.rlp_bytes().into();
475+
let mut encoded = self.rlp_bytes();
476476
self.push_msg_id_leb128_encoding(&mut encoded);
477-
encoded
477+
encoded.into()
478478
}
479479
}
480480

tools/consensus_bench/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.

tools/evm-spec-tester/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.

0 commit comments

Comments
 (0)