Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Feb 29, 2024

U256 uses a compact implementation when encoding/decoding to/from database, so there's no difference between explicit u128 and U256 types. We can remove the Ethereum/Optimism branching logic from the TxValue wrapper alongside the wrapper itself.

/// As ethereum circulation on mainnet is around 120mil eth as of 2022 that is around
/// 120000000000000000000000000 wei we are safe to use u128 for TxValue's encoding
/// as its max number is 340282366920938463463374607431768211455.
/// This optimization should be disabled for chains such as Optimism, where
/// some tx values may require more than 128-bit precision.
impl Compact for TxValue {
#[inline]
fn to_compact<B>(self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
#[cfg(feature = "optimism")]
{
self.0.to_compact(buf)
}
#[cfg(not(feature = "optimism"))]
{
self.0.to::<u128>().to_compact(buf)
}
}
#[inline]
fn from_compact(buf: &[u8], identifier: usize) -> (Self, &[u8]) {
#[cfg(feature = "optimism")]
{
let (i, buf) = U256::from_compact(buf, identifier);
(TxValue(i), buf)
}
#[cfg(not(feature = "optimism"))]
{
let (i, buf) = u128::from_compact(buf, identifier);
(TxValue::from(i), buf)
}
}
}

@shekhirin shekhirin added A-db Related to the database S-breaking This PR includes a breaking change labels Feb 29, 2024
@shekhirin shekhirin marked this pull request as ready for review February 29, 2024 11:15
@shekhirin shekhirin force-pushed the alexey/remove-tx-value branch 2 times, most recently from 11cc2a0 to 01f9ccd Compare February 29, 2024 11:22
@shekhirin shekhirin requested a review from onbjerg as a code owner February 29, 2024 11:25
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@shekhirin shekhirin merged commit e0fc334 into breaking-changes Feb 29, 2024
@shekhirin shekhirin deleted the alexey/remove-tx-value branch February 29, 2024 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-db Related to the database S-breaking This PR includes a breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants