-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `TxKind` * feat: `#[no_std]` support * test: fix `non_local_definitions` lint * chore: more lints
- Loading branch information
Showing
6 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use crate::Address; | ||
|
||
#[cfg(feature = "rlp")] | ||
use alloy_rlp::{Buf, BufMut, Decodable, Encodable, EMPTY_STRING_CODE}; | ||
|
||
/// The `to` field of a transaction. Either a target address, or empty for a | ||
/// contract creation. | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] | ||
pub enum TxKind { | ||
/// A transaction that creates a contract. | ||
#[default] | ||
Create, | ||
/// A transaction that calls a contract or transfer. | ||
Call(Address), | ||
} | ||
|
||
impl From<Option<Address>> for TxKind { | ||
/// Creates a `TxKind::Call` with the `Some` address, `None` otherwise. | ||
#[inline] | ||
fn from(value: Option<Address>) -> Self { | ||
match value { | ||
None => TxKind::Create, | ||
Some(addr) => TxKind::Call(addr), | ||
} | ||
} | ||
} | ||
|
||
impl From<Address> for TxKind { | ||
/// Creates a `TxKind::Call` with the given address. | ||
#[inline] | ||
fn from(value: Address) -> Self { | ||
TxKind::Call(value) | ||
} | ||
} | ||
|
||
impl TxKind { | ||
/// Returns the address of the contract that will be called or will receive the transfer. | ||
pub const fn to(self) -> Option<Address> { | ||
match self { | ||
TxKind::Create => None, | ||
TxKind::Call(to) => Some(to), | ||
} | ||
} | ||
|
||
/// Returns true if the transaction is a contract creation. | ||
#[inline] | ||
pub const fn is_create(self) -> bool { | ||
matches!(self, TxKind::Create) | ||
} | ||
|
||
/// Returns true if the transaction is a contract call. | ||
#[inline] | ||
pub const fn is_call(self) -> bool { | ||
matches!(self, TxKind::Call(_)) | ||
} | ||
|
||
/// Calculates a heuristic for the in-memory size of this object. | ||
#[inline] | ||
pub const fn size(self) -> usize { | ||
core::mem::size_of::<Self>() | ||
} | ||
} | ||
|
||
#[cfg(feature = "rlp")] | ||
impl Encodable for TxKind { | ||
fn encode(&self, out: &mut dyn BufMut) { | ||
match self { | ||
TxKind::Call(to) => to.encode(out), | ||
TxKind::Create => out.put_u8(EMPTY_STRING_CODE), | ||
} | ||
} | ||
|
||
fn length(&self) -> usize { | ||
match self { | ||
TxKind::Call(to) => to.length(), | ||
TxKind::Create => 1, // EMPTY_STRING_CODE is a single byte | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "rlp")] | ||
impl Decodable for TxKind { | ||
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> { | ||
if let Some(&first) = buf.first() { | ||
if first == EMPTY_STRING_CODE { | ||
buf.advance(1); | ||
Ok(TxKind::Create) | ||
} else { | ||
let addr = <Address as Decodable>::decode(buf)?; | ||
Ok(TxKind::Call(addr)) | ||
} | ||
} else { | ||
Err(alloy_rlp::Error::InputTooShort) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
// TODO: remove when https://github.com/proptest-rs/proptest/pull/427 is merged | ||
#![allow(unknown_lints, non_local_definitions)] | ||
|
||
mod sol; |