Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 157 additions & 127 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ proc-macro-crate = "3"
serde = { version = "1.0", features = [
"derive",
"alloc",
"rc"
"rc",
], default-features = false }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde_derive = { version = "1.0", default-features = false }
Expand Down Expand Up @@ -325,7 +325,7 @@ reqwest = "0.12"

# crypto & hash
#fixed-hash = "0.5"
keccak-hash = "0.5"
keccak-hash = "0.11.0"
tiny-keccak = "2.0.2"
bls-signatures = { git = "https://github.com/Conflux-Chain/bls-signatures.git", rev = "fb52187df92d27c365642cb7e7b2aaf60437cf9c", default-features = false, features = [
"multicore",
Expand Down Expand Up @@ -381,7 +381,7 @@ vergen = "8.3.2"
target_info = "0.1"
bit-set = "0.4"
typenum = "1.17.0"
typemap = { package = "typemap-ors", version = "1.0"}
typemap = { package = "typemap-ors", version = "1.0" }
impl-trait-for-tuples = "^0.2"
impl-tools = "^0.10"
derive_more = "0.99"
Expand Down Expand Up @@ -424,12 +424,15 @@ chrono = "0.4"
duration-str = "0.17"

# parity crates
rlp = "0.4.0"
# The fork of the rlp crate adds a legacy v0.4.x boolean encoding to the v0.6.x
rlp = { git = "https://github.com/Conflux-Chain/parity-common-rocksdb.git", rev = "b28832745c952c8bcac88348276b283c26de776a", package = "rlp", features = [
"legacy_bool_encoding",
] }
rlp_derive = { package = "rlp-derive", version = "0.2.0" }
panic_hook = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "09da4dfeecd754df2034d4e71a260277aaaf9783" }
dir = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "09da4dfeecd754df2034d4e71a260277aaaf9783" }
unexpected = { git = "https://github.com/Conflux-Chain/conflux-parity-deps.git", rev = "09da4dfeecd754df2034d4e71a260277aaaf9783" }
ethereum-types = "0.9"
ethereum-types = "0.15.1"
parity-wordlist = "1.3.0"
parity-crypto = "0.9.0"
parity-path = "0.1"
Expand All @@ -455,3 +458,4 @@ rocksdb = { git = "https://github.com/Conflux-Chain/rust-rocksdb.git", rev = "7d

[patch.crates-io]
sqlite3-sys = { git = "https://github.com/Conflux-Chain/sqlite3-sys.git", rev = "1de8e5998f7c2d919336660b8ef4e8f52ac43844" }
rlp = { git = "https://github.com/Conflux-Chain/parity-common-rocksdb.git", rev = "b28832745c952c8bcac88348276b283c26de776a", package = "rlp" }
2 changes: 1 addition & 1 deletion crates/cfx_key/src/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ mod derivation {

// 0x00 (padding) -- private_key -- index
// 0 -- 1..33 -- 33..end
private.to_big_endian(&mut data[1..33]);
private.write_as_big_endian(&mut data[1..33]);
index.store(&mut data[33..(33 + T::len())]);

hmac_pair(&data, private_key, chain_code)
Expand Down
2 changes: 1 addition & 1 deletion crates/cfx_types/src/contract_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn cal_contract_address(
// future.
buffer[0] = 0x0;
buffer[1..(1 + 20)].copy_from_slice(&sender[..]);
nonce.to_little_endian(&mut buffer[(1 + 20)..(1 + 20 + 32)]);
nonce.write_as_little_endian(&mut buffer[(1 + 20)..(1 + 20 + 32)]);
buffer[(1 + 20 + 32)..].copy_from_slice(&code_hash[..]);
// In Conflux, we use the first four bits to indicate the type of
// the address. For contract address, the bits will be
Expand Down
22 changes: 21 additions & 1 deletion crates/cfx_types/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,30 @@ pub fn parse_hex_string<F: FromStr>(hex_str: &str) -> Result<F, F::Err> {

pub fn u256_to_h256_be(value: U256) -> H256 {
let mut buf = [0u8; 32];
value.to_big_endian(&mut buf);
value.write_as_big_endian(&mut buf);
H256::from(buf)
}

pub fn h256_to_u256_be(value: H256) -> U256 {
U256::from_big_endian(value.as_bytes())
}

#[cfg(test)]
mod tests {
use hex;
#[test]
fn test_legacy_bool_encode() {
// by now we need use the legacy bool encoding
// which is 0x00 for false and 0x01 for true
// the newer encoding is 0x80 for false and 0x01 for true

let false_bytes = hex::decode("00").unwrap();
let true_bytes = hex::decode("01").unwrap();

assert_eq!(rlp::encode(&false).as_ref(), false_bytes);
assert_eq!(rlp::encode(&true).as_ref(), true_bytes);

assert_eq!(rlp::decode::<bool>(&false_bytes), Ok(false));
assert_eq!(rlp::decode::<bool>(&true_bytes), Ok(true));
}
}
3 changes: 2 additions & 1 deletion crates/cfxcore/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ rangetools = { workspace = true }
rayon = { workspace = true }
rlp = { workspace = true }
rlp_derive = { workspace = true }
bytes = { workspace = true }
rustc-hex = { workspace = true }
secret-store = { workspace = true }
serde = { workspace = true, features = ["rc"] }
Expand Down Expand Up @@ -134,4 +135,4 @@ consensus_bench = []
fuzzing = ["proptest", "proptest-derive"]

# [lints.rust]
# unexpected_cfgs = { level = "warn", check-cfg = ['cfg(mirai)'] }
# unexpected_cfgs = { level = "warn", check-cfg = ['cfg(mirai)'] }
2 changes: 1 addition & 1 deletion crates/cfxcore/core/examples/snapshot_merge_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn main() -> Result<(), Error> {
assert!(value.is_some(), "Address {:?} does not exist", addr);
let account_bytes = rlp::encode(account);
let get_bytes = value.unwrap();
assert_eq!(account_bytes.as_slice(), get_bytes.as_ref());
assert_eq!(account_bytes.as_ref(), get_bytes.as_ref());
}
// TODO Make snapshot3 to compare the snapshot merkle_root
let state_root_3 = StateRootWithAuxInfo::genesis(&MERKLE_NULL_NODE);
Expand Down
15 changes: 8 additions & 7 deletions crates/cfxcore/core/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type RequestId = u64;
pub type MsgId = u16;
const MSG_ID_MAX: u16 = 1 << 14;

use bytes::{BufMut, BytesMut};
pub use cfx_bytes::Bytes;
pub use priority_send_queue::SendQueuePriority;
use rlp::{Decodable, Rlp};
Expand Down Expand Up @@ -60,16 +61,16 @@ pub trait Message:
// If true, message may be throttled when sent to remote peer.
fn is_size_sensitive(&self) -> bool { false }
fn msg_id(&self) -> MsgId;
fn push_msg_id_leb128_encoding(&self, buffer: &mut Vec<u8>) {
fn push_msg_id_leb128_encoding(&self, buffer: &mut BytesMut) {
let msg_id = self.msg_id();
assert!(msg_id < MSG_ID_MAX);
let msg_id_msb = (msg_id >> 7) as u8;
let mut msg_id_lsb = (msg_id as u8) & 0x7f;
if msg_id_msb != 0 {
buffer.push(msg_id_msb);
buffer.put_u8(msg_id_msb);
msg_id_lsb |= 0x80;
}
buffer.push(msg_id_lsb);
buffer.put_u8(msg_id_lsb);
}
fn msg_name(&self) -> &'static str;
fn priority(&self) -> SendQueuePriority { SendQueuePriority::High }
Expand Down Expand Up @@ -187,7 +188,7 @@ macro_rules! build_msg_basic {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}
};
Expand Down Expand Up @@ -278,10 +279,10 @@ mod test {
#[test]
fn test_message_id_encode_decode() {
for msg_id in 0..MSG_ID_MAX {
let mut buf = vec![];
let message = TestMessage { msg_id };
buf.extend_from_slice(&message.rlp_bytes());
message.push_msg_id_leb128_encoding(&mut buf);
let mut encoded = message.rlp_bytes();
message.push_msg_id_leb128_encoding(&mut encoded);
let buf = encoded.as_ref();
match decode_msg(&buf) {
None => assert!(false, "Can not decode message"),
Some((decoded_msg_id, rlp)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl VrfProposer {
proposal_threshold_u256: U256, epoch_state: EpochState,
) -> Self {
let mut proposal_threshold = [0 as u8; HashValue::LENGTH];
proposal_threshold_u256.to_big_endian(&mut proposal_threshold);
proposal_threshold_u256.write_as_big_endian(&mut proposal_threshold);
Self {
author,
vrf_private_key,
Expand Down
6 changes: 3 additions & 3 deletions crates/cfxcore/core/src/pow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ impl ProofOfWorkConfig {
// withholding attack among mining pools.
pub fn nonce_to_lower_bound(nonce: &U256) -> U256 {
let mut buf = [0u8; 32];
nonce.to_big_endian(&mut buf[..]);
nonce.write_as_big_endian(&mut buf[..]);
for i in 16..32 {
buf[i] = 0;
}
buf[0] = buf[0] & 0x7f;
// Note that U256::from assumes big_endian of the bytes
let lower_bound = U256::from(buf);
let lower_bound = U256::from_big_endian(buf.as_ref());
lower_bound
}

Expand Down Expand Up @@ -291,7 +291,7 @@ impl PowComputer {
for i in 0..32 {
buf[i] = block_hash[i];
}
nonce.to_little_endian(&mut buf[32..64]);
nonce.write_as_little_endian(&mut buf[32..64]);
let intermediate = keccak_hash(&buf[..]);
let mut tmp = [0u8; 32];
for i in 0..32 {
Expand Down
16 changes: 8 additions & 8 deletions crates/cfxcore/core/src/sync/message/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Message for GetBlockHashesResponse {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand All @@ -114,7 +114,7 @@ impl Message for Transactions {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand All @@ -130,7 +130,7 @@ impl Message for GetBlocksResponse {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand All @@ -150,7 +150,7 @@ impl Message for GetBlocksWithPublicResponse {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand All @@ -166,7 +166,7 @@ impl Message for GetBlockTxnResponse {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand All @@ -184,7 +184,7 @@ impl Message for TransactionDigests {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand All @@ -202,7 +202,7 @@ impl Message for GetTransactionsResponse {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}
impl GetMaybeRequestId for GetTransactionsFromTxHashesResponse {}
Expand All @@ -225,7 +225,7 @@ impl Message for GetTransactionsFromTxHashesResponse {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}
/// handle the RLP encoded message with given context `ctx`.
Expand Down
4 changes: 2 additions & 2 deletions crates/cfxcore/core/src/sync/message/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Message for GetTransactions {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand Down Expand Up @@ -474,7 +474,7 @@ impl Message for GetTransactionsFromTxHashes {
fn encode(&self) -> Vec<u8> {
let mut encoded = self.rlp_bytes();
self.push_msg_id_leb128_encoding(&mut encoded);
encoded
encoded.into()
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/cfxcore/core/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ fn block_receipts_trie(block_receipts: &Vec<Receipt>) -> SimpleMpt {
make_simple_mpt(
block_receipts
.iter()
.map(|receipt| receipt.rlp_bytes().into_boxed_slice())
.map(|receipt| {
let vec_u8: Vec<u8> = receipt.rlp_bytes().into();
vec_u8.into_boxed_slice()
})
.collect(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cfxcore/internal_common/src/block_data_db_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait DatabaseDecodable: Sized {
macro_rules! impl_db_encoding_as_rlp {
($type:ty) => {
impl $crate::DatabaseEncodable for $type {
fn db_encode(&self) -> Vec<u8> { rlp::encode(self) }
fn db_encode(&self) -> Vec<u8> { rlp::encode(self).to_vec() }
}

impl $crate::DatabaseDecodable for $type {
Expand All @@ -39,7 +39,7 @@ impl DatabaseEncodable for BlockHeader {
fn db_encode(&self) -> Bytes {
let mut rlp_stream = RlpStream::new();
self.stream_rlp_with_pow_hash(&mut rlp_stream);
rlp_stream.drain()
rlp_stream.as_raw().into()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl<Version: Decodable, T: Decodable> Decodable
impl<Version: Encodable, T: Encodable> DatabaseEncodable
for DataVersionTuple<Version, T>
{
fn db_encode(&self) -> Vec<u8> { rlp::encode(self) }
fn db_encode(&self) -> Vec<u8> { rlp::encode(self).into() }
}

impl<Version: Decodable, T: Decodable> DatabaseDecodable
Expand Down Expand Up @@ -370,7 +370,7 @@ where T: DatabaseEncodable {
for e in list {
rlp_stream.append_raw(&e.db_encode(), 1);
}
rlp_stream.drain()
rlp_stream.as_raw().into()
}

pub fn db_decode_list<T>(bytes: &[u8]) -> Result<Vec<T>, DecoderError>
Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/rpc/impls/cfx/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ impl RpcImpl {
.expect("failed to convert scaled risk to bigInt");
let (sign, big_endian_bytes) = scaled_risk.to_bytes_be();
assert_ne!(sign, num_bigint::Sign::Minus);
let rpc_result = U256::from(big_endian_bytes.as_slice());
let rpc_result = U256::from_big_endian(big_endian_bytes.as_slice());
Ok(Some(rpc_result.into()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/rpc/impls/cfx/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl RpcImpl {
accounts,
)?;

Self::send_tx_helper(light, Bytes::new(tx.rlp_bytes()))
Self::send_tx_helper(light, Bytes::new(tx.rlp_bytes().into()))
};

fut.boxed()
Expand Down
7 changes: 2 additions & 5 deletions crates/dbs/statedb/src/statedb_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ impl StateDbExt for StateDbGeneric {
if value.is_default() {
self.delete(key, debug_record)
} else {
self.set_raw(
key,
::rlp::encode(value).into_boxed_slice(),
debug_record,
)
let value_vec = Vec::from(::rlp::encode(value));
self.set_raw(key, value_vec.into_boxed_slice(), debug_record)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/dbs/storage/src/impls/delta_mpt/cow_node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ impl CowNodeRef {
.value
.try_into()
.expect("not exceed i64::MAX"),
trie_node.rlp_bytes().as_slice(),
trie_node.rlp_bytes().as_ref(),
)?;
commit_transaction.info.row_number =
commit_transaction.info.row_number.get_next()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn test_no_alloc_in_empty_children_table() {

let rlp_bytes = empty_children_table.to_ref().rlp_bytes();
let rlp_parsed =
ChildrenTableManagedDeltaMpt::decode(&Rlp::new(rlp_bytes.as_slice()))
ChildrenTableManagedDeltaMpt::decode(&Rlp::new(rlp_bytes.as_ref()))
.unwrap();
let decoded_table = ChildrenTableDeltaMpt::from(rlp_parsed);
decoded_table.assert_no_alloc_in_empty_children_table();
Expand Down
Loading
Loading