Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimism RPC Types #2

Merged
merged 25 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0582d5c
chore: add serde and alloy_primitives to the dependencies
EmperorOrokuSaki Apr 10, 2024
dbdcda9
feat: add transaction receipt type without tests + several dependencies.
EmperorOrokuSaki Apr 10, 2024
1ebd0e0
feat: add log
EmperorOrokuSaki Apr 10, 2024
15eb436
feat: add txtype, deposit nonce, and receipt version.
EmperorOrokuSaki Apr 10, 2024
599c9a2
feat: add block.
EmperorOrokuSaki Apr 10, 2024
2def3b0
feat: add txType as a separate file under transactions and update rec…
EmperorOrokuSaki Apr 10, 2024
6b0c42a
refactor: update optimism specific fields and their (de)serialization…
EmperorOrokuSaki Apr 10, 2024
87742c3
docs: remove outdated documentation.
EmperorOrokuSaki Apr 10, 2024
516eb59
feat: add transaction, and request types. Adjust block to use the cra…
EmperorOrokuSaki Apr 10, 2024
fafe2aa
feat: add op-consensus and receiptEnvelope
EmperorOrokuSaki Apr 11, 2024
2599060
feat: add call.rs and update visibility of transaction requests, type…
EmperorOrokuSaki Apr 11, 2024
d5c9b78
lint: cargo fmt.
EmperorOrokuSaki Apr 11, 2024
b9169e9
feat: add pubsub.rs
EmperorOrokuSaki Apr 12, 2024
f1675b9
feat: fix imports, add TODO comments, organize the code.
EmperorOrokuSaki Apr 12, 2024
d939344
lint: cargo fmt
EmperorOrokuSaki Apr 12, 2024
5dbd2f5
fix: receipt.rs imports are fixed.
EmperorOrokuSaki Apr 12, 2024
2304918
feat: add filters.rs
EmperorOrokuSaki Apr 12, 2024
2ad3438
feat: re-export all eth types.
EmperorOrokuSaki Apr 12, 2024
6a72e80
feat: review changes.
EmperorOrokuSaki Apr 15, 2024
93141f0
refactor: re-import instead of redefining.
EmperorOrokuSaki Apr 18, 2024
b5005c9
chore: bump alloy version.
EmperorOrokuSaki Apr 20, 2024
577749e
feat: use generics, remove unnecessary types.
EmperorOrokuSaki Apr 20, 2024
0744884
lint: fmt
EmperorOrokuSaki Apr 20, 2024
12195e1
refactor: use native types
EmperorOrokuSaki Apr 25, 2024
cf248d4
chore: bump alloy version
EmperorOrokuSaki Apr 25, 2024
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
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ homepage = "https://github.com/alloy-rs/op-alloy"
repository = "https://github.com/alloy-rs/op-alloy"
exclude = ["benches/", "tests/"]

[workspace.dependencies]
# Alloy
alloy-primitives = { version = "0.7.0", default-features = false }
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "89f14f9", features = [
"serde",
"rpc-types-eth",
"rpc-types","rlp","consensus"
] }
op-consensus = { version = "0.1.0", default-features = false, path = "crates/op-consensus" }
op-rpc-types = { version = "0.1.0", default-features = false, path = "crates/op-rpc-types" }

# Serde
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

## misc-testing
arbitrary = { version = "1.3", features = ["derive"] }
rand = "0.8"
proptest = "1.4"
proptest-derive = "0.4"

[workspace.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
33 changes: 33 additions & 0 deletions crates/op-rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,36 @@ homepage.workspace = true
authors.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
op-consensus = { workspace = true, features = ["serde"]}
# alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] }
# alloy-serde.workspace = true
# alloy-genesis.workspace = true

# alloy-consensus = { workspace = true, features = ["std", "serde"] }
# alloy-eips = { workspace = true, features = ["std", "serde"] }
EmperorOrokuSaki marked this conversation as resolved.
Show resolved Hide resolved

serde = { workspace = true, features = ["derive"] }
alloy = { workspace = true , features = ["serde", "rpc-types", "rpc-types-eth", "rlp", "consensus"]}
serde_json.workspace = true

# arbitrary
arbitrary = { version = "1.3", features = ["derive"], optional = true }
proptest = { version = "1.4", optional = true }
proptest-derive = { version = "0.4", optional = true }

[features]
arbitrary = [
"dep:arbitrary",
"dep:proptest-derive",
"dep:proptest",
"alloy-primitives/arbitrary",
]

[dev-dependencies]
arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
proptest-derive.workspace = true
rand.workspace = true
2 changes: 1 addition & 1 deletion crates/op-rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@

mod op;
51 changes: 51 additions & 0 deletions crates/op-rpc-types/src/op/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Block RPC types.

#![allow(unknown_lints, non_local_definitions)]

use crate::op::transaction::Transaction;
use alloy::rpc::types::eth::{
BlockTransactions, Header, Rich, Withdrawal
};
use alloy_primitives::{B256, U256};
use serde::{Deserialize, Serialize};

/// Block representation
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Block{
/// Header of the block.
#[serde(flatten)]
pub header: Header,
/// Uncles' hashes.
#[serde(default)]
pub uncles: Vec<B256>,
/// Block Transactions. In the case of an uncle block, this field is not included in RPC
/// responses, and when deserialized, it will be set to [BlockTransactions::Uncle].
#[serde(
default = "BlockTransactions::uncle",
skip_serializing_if = "BlockTransactions::is_uncle"
)]
pub transactions: BlockTransactions<Transaction>,
/// Integer the size of this block in bytes.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub size: Option<U256>,
/// Withdrawals in the block.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub withdrawals: Option<Vec<Withdrawal>>,
}

impl Block {
/// Converts a block with Tx hashes into a full block.
pub fn into_full_block(self, txs: Vec<Transaction>) -> Self {
Self { transactions: BlockTransactions::Full(txs), ..self }
}
}

/// A Block representation that allows to include additional fields
pub type RichBlock = Rich<Block>;

impl From<Block> for RichBlock {
fn from(block: Block) -> Self {
Rich { inner: block, extra_info: Default::default() }
}
}
13 changes: 13 additions & 0 deletions crates/op-rpc-types/src/op/call.rs
EmperorOrokuSaki marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::op::transaction::request::TransactionRequest;
use alloy::rpc::types::eth::BlockOverrides;
use serde::{Deserialize, Serialize};

/// Bundle of transactions
#[derive(Debug, Clone, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Bundle {
/// All transactions to execute
pub transactions: Vec<TransactionRequest>,
/// Block overrides to apply
pub block_override: Option<BlockOverrides>,
}
69 changes: 69 additions & 0 deletions crates/op-rpc-types/src/op/filters.rs
EmperorOrokuSaki marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::op::Transaction;
use alloy::rpc::types::eth::Log as RpcLog;
use alloy_primitives::B256;
use serde::{Deserialize, Deserializer, Serialize};

/// Response of the `eth_getFilterChanges` RPC.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum FilterChanges {
/// Empty result.
#[serde(with = "empty_array")]
Empty,
/// New logs.
Logs(Vec<RpcLog>),
/// New hashes (block or transactions).
Hashes(Vec<B256>),
/// New transactions.
Transactions(Vec<Transaction>),
}
mod empty_array {
use serde::{Serialize, Serializer};

pub(super) fn serialize<S>(s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
(&[] as &[()]).serialize(s)
}
}
impl<'de> Deserialize<'de> for FilterChanges {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Changes {
Hashes(Vec<B256>),
Logs(Vec<RpcLog>),
Transactions(Vec<Transaction>),
}

let changes = Changes::deserialize(deserializer)?;
let changes = match changes {
Changes::Logs(vals) => {
if vals.is_empty() {
FilterChanges::Empty
} else {
FilterChanges::Logs(vals)
}
}
Changes::Hashes(vals) => {
if vals.is_empty() {
FilterChanges::Empty
} else {
FilterChanges::Hashes(vals)
}
}
Changes::Transactions(vals) => {
if vals.is_empty() {
FilterChanges::Empty
} else {
FilterChanges::Transactions(vals)
}
}
};
Ok(changes)
}
}
6 changes: 6 additions & 0 deletions crates/op-rpc-types/src/op/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod block;
mod call;
mod filters;
mod pubsub;
mod transaction;
pub use alloy::rpc::types::eth::*;
37 changes: 37 additions & 0 deletions crates/op-rpc-types/src/op/pubsub.rs
EmperorOrokuSaki marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Optimism types for pub-sub

use crate::op::transaction::Transaction;
use alloy::rpc::types::eth::{pubsub::PubSubSyncStatus, Log, RichHeader};
use alloy_primitives::B256;
use serde::{Deserialize, Serialize, Serializer};

/// Subscription result.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(untagged)]
EmperorOrokuSaki marked this conversation as resolved.
Show resolved Hide resolved
pub enum SubscriptionResult {
/// New block header.
Header(Box<RichHeader>),
/// Log
Log(Box<Log>),
/// Transaction hash
TransactionHash(B256),
/// Full Transaction
FullTransaction(Box<Transaction>),
/// SyncStatus
SyncState(PubSubSyncStatus),
}

impl Serialize for SubscriptionResult {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
SubscriptionResult::Header(ref header) => header.serialize(serializer),
SubscriptionResult::Log(ref log) => log.serialize(serializer),
SubscriptionResult::TransactionHash(ref hash) => hash.serialize(serializer),
SubscriptionResult::FullTransaction(ref tx) => tx.serialize(serializer),
SubscriptionResult::SyncState(ref sync) => sync.serialize(serializer),
}
}
}
Loading