chore(reth-codec): use constant (#10533)

This commit is contained in:
nk_ysg
2024-08-26 16:17:52 +08:00
committed by GitHub
parent cb6e5ed831
commit 96a8c23d97
2 changed files with 20 additions and 8 deletions

View File

@ -8,6 +8,12 @@ use bytes::{Buf, BufMut};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
/// Identifier for [`HashBuilderValue::Hash`]
const HASH_BUILDER_TYPE_HASH: u8 = 0;
/// Identifier for [`HashBuilderValue::Bytes`]
const HASH_BUILDER_TYPE_BYTES: u8 = 1;
impl Compact for HashBuilderValue {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
@ -15,11 +21,11 @@ impl Compact for HashBuilderValue {
{
match self {
Self::Hash(hash) => {
buf.put_u8(0);
buf.put_u8(HASH_BUILDER_TYPE_HASH);
1 + hash.to_compact(buf)
}
Self::Bytes(bytes) => {
buf.put_u8(1);
buf.put_u8(HASH_BUILDER_TYPE_BYTES);
1 + bytes.to_compact(buf)
}
}
@ -31,11 +37,11 @@ impl Compact for HashBuilderValue {
// database.
fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {
match buf.get_u8() {
0 => {
HASH_BUILDER_TYPE_HASH => {
let (hash, buf) = B256::from_compact(buf, 32);
(Self::Hash(hash), buf)
}
1 => {
HASH_BUILDER_TYPE_BYTES => {
let (bytes, buf) = Vec::from_compact(buf, 0);
(Self::Bytes(bytes), buf)
}

View File

@ -3,23 +3,29 @@
use crate::Compact;
use alloy_primitives::{Address, TxKind};
/// Identifier for [`TxKind::Create`]
const TX_KIND_TYPE_CREATE: usize = 0;
/// Identifier for [`TxKind::Call`]
const TX_KIND_TYPE_CALL: usize = 1;
impl Compact for TxKind {
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
match self {
Self::Create => 0,
Self::Create => TX_KIND_TYPE_CREATE,
Self::Call(address) => {
address.to_compact(buf);
1
TX_KIND_TYPE_CALL
}
}
}
fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
match identifier {
0 => (Self::Create, buf),
1 => {
TX_KIND_TYPE_CREATE => (Self::Create, buf),
TX_KIND_TYPE_CALL => {
let (addr, buf) = Address::from_compact(buf, buf.len());
(addr.into(), buf)
}