Skip to content

Commit

Permalink
Telos EVM Runtime Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirpashaa committed Aug 14, 2024
1 parent 603e39a commit 15f4412
Show file tree
Hide file tree
Showing 33 changed files with 204 additions and 36 deletions.
19 changes: 7 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,13 @@ reth-trie-db = { path = "crates/trie/db" }
reth-trie-parallel = { path = "crates/trie/parallel" }

# revm
revm = { version = "13.0.0", features = [
revm = { git = "https://github.com/telosnetwork/telos-revm", branch = "telos-main", features = [
"std",
"secp256k1",
"blst",
], default-features = false }
revm-inspectors = "0.5"
revm-primitives = { version = "8.0.0", features = [
revm-inspectors = { git = "https://github.com/telosnetwork/telos-evm-inspectors", branch = "telos-main" }
revm-primitives = { git = "https://github.com/telosnetwork/telos-revm", branch = "telos-main", features = [
"std",
], default-features = false }

Expand Down
20 changes: 20 additions & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ optimism = [

# no-op feature flag for switching between the `optimism` and default functionality in CI matrices
ethereum = []
telos = [
"reth-basic-payload-builder/telos",
"reth-ethereum-payload-builder/telos",
"reth-evm/telos",
"reth-execution-types/telos",
"reth-payload-builder/telos",
"reth-provider/telos",
"reth-revm/telos",
"reth-rpc/telos",
"reth-rpc-eth-types/telos",
"reth-transaction-pool/telos",
"reth-trie/telos",
"reth-trie-db/telos",
"reth-engine-util/telos",
"reth-primitives/telos",
"reth-blockchain-tree/telos",
"reth-rpc-types-compat/telos",
"reth-consensus-common/telos",
"reth-stages/telos",
]

[[bin]]
name = "reth"
Expand Down
3 changes: 3 additions & 0 deletions crates/blockchain-tree-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ reth-storage-errors.workspace = true

# misc
thiserror.workspace = true

[features]
telos = []
3 changes: 3 additions & 0 deletions crates/blockchain-tree-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ pub enum BlockValidationKind {
impl BlockValidationKind {
/// Returns true if the state root should be validated if possible.
pub const fn is_exhaustive(&self) -> bool {
#[cfg(feature = "telos")]
return false;
#[cfg(not(feature = "telos"))]
matches!(self, Self::Exhaustive)
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/blockchain-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ alloy-genesis.workspace = true
[features]
test-utils = []
optimism = ["reth-primitives/optimism", "reth-provider/optimism"]
telos = [
"reth-blockchain-tree-api/telos",
]
26 changes: 23 additions & 3 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ use reth_blockchain_tree_api::{
use reth_consensus::{Consensus, ConsensusError};
use reth_db_api::database::Database;
use reth_evm::execute::BlockExecutorProvider;
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
#[cfg(not(feature = "telos"))]
use reth_execution_errors::BlockExecutionError;
use reth_execution_errors::BlockValidationError;
use reth_execution_types::{Chain, ExecutionOutcome};
#[cfg(not(feature = "telos"))]
use reth_primitives::GotExpected;
use reth_primitives::{
BlockHash, BlockNumHash, BlockNumber, EthereumHardfork, ForkBlock, GotExpected, Receipt,
BlockHash, BlockNumHash, BlockNumber, EthereumHardfork, ForkBlock, Receipt,
SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
};
use reth_provider::{
Expand All @@ -25,7 +29,9 @@ use reth_provider::{
};
use reth_prune_types::PruneModes;
use reth_stages_api::{MetricEvent, MetricEventsSender};
use reth_storage_errors::provider::{ProviderResult, RootMismatch};
#[cfg(not(feature = "telos"))]
use reth_storage_errors::provider::RootMismatch;
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{hashed_cursor::HashedPostStateCursorFactory, StateRoot};
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseStateRoot};
use std::{
Expand Down Expand Up @@ -396,6 +402,7 @@ where

let provider = self.externals.provider_factory.provider()?;

#[cfg(not(feature = "telos"))] {
// Validate that the block is post merge
let parent_td = provider
.header_td(&block.parent_hash)?
Expand All @@ -414,6 +421,7 @@ where
})
.into())
}
}

let parent_header = provider
.header(&block.parent_hash)?
Expand Down Expand Up @@ -1239,6 +1247,16 @@ where
// State root calculation can take a while, and we're sure no write transaction
// will be open in parallel. See https://github.com/paradigmxyz/reth/issues/6168.
.disable_long_read_transaction_safety();
#[cfg(feature = "telos")]
let (_, trie_updates) = StateRoot::from_tx(provider.tx_ref())
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::new(provider.tx_ref()),
&hashed_state_sorted,
))
.with_prefix_sets(prefix_sets)
.root_with_updates()
.map_err(Into::<BlockValidationError>::into)?;
#[cfg(not(feature = "telos"))]
let (state_root, trie_updates) = StateRoot::from_tx(provider.tx_ref())
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::new(provider.tx_ref()),
Expand All @@ -1247,7 +1265,9 @@ where
.with_prefix_sets(prefix_sets)
.root_with_updates()
.map_err(Into::<BlockValidationError>::into)?;
#[cfg(not(feature = "telos"))]
let tip = blocks.tip();
#[cfg(not(feature = "telos"))]
if state_root != tip.state_root {
return Err(ProviderError::StateRootMismatch(Box::new(RootMismatch {
root: GotExpected { got: state_root, expected: tip.state_root },
Expand Down
3 changes: 3 additions & 0 deletions crates/consensus/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ reth-consensus.workspace = true
reth-storage-api.workspace = true
rand.workspace = true
mockall = "0.12"

[features]
telos = []
6 changes: 6 additions & 0 deletions crates/consensus/common/src/calc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "telos")]
use reth_chainspec::Chain;
use reth_chainspec::{ChainSpec, EthereumHardfork};
use reth_primitives::{constants::ETH_TO_WEI, BlockNumber, U256};

Expand Down Expand Up @@ -26,6 +28,10 @@ pub fn base_block_reward(
block_difficulty: U256,
total_difficulty: U256,
) -> Option<u128> {
#[cfg(feature = "telos")]
if chain_spec.chain == Chain::from_id(40) || chain_spec.chain == Chain::from_id(41) {
return None
}
if chain_spec.fork(EthereumHardfork::Paris).active_at_ttd(total_difficulty, block_difficulty) {
None
} else {
Expand Down
3 changes: 3 additions & 0 deletions crates/consensus/common/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ pub fn validate_against_parent_timestamp(
parent: &SealedHeader,
) -> Result<(), ConsensusError> {
if header.is_timestamp_in_past(parent.timestamp) {
#[cfg(feature = "telos")]
return Ok(());
#[cfg(not(feature = "telos"))]
return Err(ConsensusError::TimestampIsInPast {
parent_timestamp: parent.timestamp,
timestamp: header.timestamp,
Expand Down
3 changes: 3 additions & 0 deletions crates/engine/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ optimism = [
"reth-beacon-consensus/optimism",
"reth-ethereum-forks/optimism"
]
telos = [
"revm-primitives/telos",
]
5 changes: 5 additions & 0 deletions crates/ethereum/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ revm.workspace = true

# misc
tracing.workspace = true

[features]
telos = [
"revm/telos",
]
3 changes: 3 additions & 0 deletions crates/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ parking_lot.workspace = true
default = ["std"]
std = []
test-utils = ["dep:parking_lot"]
telos = [
"revm/telos",
]
3 changes: 3 additions & 0 deletions crates/evm/execution-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ default = ["std"]
optimism = ["dep:reth-chainspec"]
serde = ["dep:serde", "reth-trie/serde", "revm/serde"]
std = []
telos = [
"revm/telos",
]
1 change: 1 addition & 0 deletions crates/optimism/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ optimism = [
"reth-optimism-consensus/optimism",
"reth-revm/optimism",
]
telos = []
5 changes: 5 additions & 0 deletions crates/payload/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ metrics.workspace = true

# misc
tracing.workspace = true

[features]
telos = [
"revm/telos",
]
5 changes: 4 additions & 1 deletion crates/payload/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ tracing.workspace = true
revm.workspace = true

[features]
test-utils = []
test-utils = []
telos = [
"revm/telos",
]
3 changes: 3 additions & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ optimism = [
]
alloy-compat = ["reth-primitives-traits/alloy-compat", "dep:alloy-rpc-types"]
test-utils = ["reth-primitives-traits/test-utils"]
telos = [
"revm-primitives/telos",
]

[[bench]]
name = "recover_ecdsa_crit"
Expand Down
13 changes: 10 additions & 3 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ impl TransactionSignedNoHash {
///
/// For optimism this will return [`Address::ZERO`] if the Signature is empty, this is because pre bedrock (on OP mainnet), relay messages to the L2 Cross Domain Messenger were sent as legacy transactions from the zero address with an empty signature, e.g.: <https://optimistic.etherscan.io/tx/0x1bb352ff9215efe5a4c102f45d730bae323c3288d2636672eb61543ddd47abad>
/// This makes it possible to import pre bedrock transactions via the sender recovery stage.
pub fn encode_and_recover_unchecked(&self, buffer: &mut Vec<u8>) -> Option<Address> {
pub fn encode_and_recover_unchecked(&self, buffer: &mut Vec<u8>, #[cfg(feature = "telos")] chain_id: Option<u64>) -> Option<Address> {
buffer.clear();
self.transaction.encode_without_signature(buffer);

Expand All @@ -880,7 +880,7 @@ impl TransactionSignedNoHash {
}
}

self.signature.recover_signer_unchecked(keccak256(buffer))
self.signature.recover_signer_unchecked(keccak256(buffer), #[cfg(feature = "telos")] chain_id)
}

/// Converts into a transaction type with its hash: [`TransactionSigned`].
Expand Down Expand Up @@ -1038,6 +1038,13 @@ impl TransactionSigned {
if let Transaction::Deposit(TxDeposit { from, .. }) = self.transaction {
return Some(from)
}
#[cfg(feature = "telos")]
if self.transaction.chain_id() == Some(3) {
let mut s_bytes: [u8; 20] = [0; 20];
s_bytes.copy_from_slice(&self.signature.s.to_be_bytes::<32>()[0..20]);
let address = Address::new(s_bytes);
return Some(address);
}
let signature_hash = self.signature_hash();
self.signature.recover_signer(signature_hash)
}
Expand All @@ -1055,7 +1062,7 @@ impl TransactionSigned {
return Some(from)
}
let signature_hash = self.signature_hash();
self.signature.recover_signer_unchecked(signature_hash)
self.signature.recover_signer_unchecked(signature_hash, #[cfg(feature = "telos")] self.chain_id())
}

/// Recovers a list of signers from a transaction list iterator.
Expand Down
14 changes: 11 additions & 3 deletions crates/primitives/src/transaction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,15 @@ impl Signature {
/// Using this for signature validation will succeed, even if the signature is malleable or not
/// compliant with EIP-2. This is provided for compatibility with old signatures which have
/// large `s` values.
pub fn recover_signer_unchecked(&self, hash: B256) -> Option<Address> {
pub fn recover_signer_unchecked(&self, hash: B256, #[cfg(feature = "telos")] chain_id: Option<u64>) -> Option<Address> {
#[cfg(feature = "telos")]
if chain_id == Some(3) {
let mut s_bytes: [u8; 20] = [0; 20];
s_bytes.copy_from_slice(&self.s.to_be_bytes::<32>()[0..20]);
let address = Address::new(s_bytes);
return Some(address);
}

let mut sig: [u8; 65] = [0; 65];

sig[0..32].copy_from_slice(&self.r.to_be_bytes::<32>());
Expand All @@ -177,7 +185,7 @@ impl Signature {
return None
}

self.recover_signer_unchecked(hash)
self.recover_signer_unchecked(hash, #[cfg(feature = "telos")] None)
}

/// Turn this signature into its byte
Expand Down Expand Up @@ -361,6 +369,6 @@ mod tests {
assert!(signature.recover_signer(hash).is_none());

// use unchecked, ensure it succeeds (the signature is valid if not for EIP-2)
assert!(signature.recover_signer_unchecked(hash).is_some());
assert!(signature.recover_signer_unchecked(hash, #[cfg(feature = "telos")] tx.chain_id()).is_some());
}
}
3 changes: 3 additions & 0 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ std = []
c-kzg = ["revm/c-kzg"]
test-utils = ["dep:reth-trie"]
optimism = ["revm/optimism"]
telos = [
"revm/telos",
]
5 changes: 4 additions & 1 deletion crates/rpc/rpc-eth-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ serde_json.workspace = true

[features]
default = ["js-tracer"]
js-tracer = ["revm-inspectors/js-tracer"]
js-tracer = ["revm-inspectors/js-tracer"]
telos = [
"revm/telos",
]
1 change: 1 addition & 0 deletions crates/rpc/rpc-types-compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ serde_json.workspace = true

[features]
optimism = ["reth-primitives/optimism"]
telos = []
Loading

0 comments on commit 15f4412

Please sign in to comment.