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

feat: add primitive and RPC types #51

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
56ff52d
feat: add more serde utils and impls
DaniPopes May 18, 2023
e18c39d
rm proptest feature
DaniPopes May 18, 2023
1a37710
docs
DaniPopes May 19, 2023
f1eac22
feat: improve primitives
DaniPopes May 19, 2023
5909689
feat: implement Arbitrary for Bytes and wrapped FixedBytes
DaniPopes May 20, 2023
d00d282
feat: improve primitives utils
DaniPopes May 20, 2023
21eca2b
feat: ethers-types mvp
DaniPopes May 20, 2023
b464e35
feat: improve primitives
DaniPopes May 19, 2023
c154b3b
feat: implement Arbitrary for Bytes and wrapped FixedBytes
DaniPopes May 20, 2023
dd6269a
feat: improve primitives utils
DaniPopes May 20, 2023
54e7648
fix: hex breaking change
DaniPopes May 19, 2023
169c34e
fixes
DaniPopes May 20, 2023
5936868
chore: clippy
DaniPopes May 20, 2023
a9a9995
serde
DaniPopes May 20, 2023
d169b07
feat: add more serde utils and impls
DaniPopes May 18, 2023
215f741
ignore test
DaniPopes May 20, 2023
71ad34a
chore: minify json
DaniPopes May 22, 2023
5aa846d
remove dependency on jsonrpsee-types
DaniPopes May 22, 2023
3f69ffe
Merge branch 'dani/changes' into dani/rpc-types
DaniPopes May 22, 2023
ca65035
fix: RLP
DaniPopes May 22, 2023
cce2b5a
fix: ethers-types rlp TODOs
DaniPopes May 22, 2023
19baa63
feat: add proof generation under feature, fix remaining TODOs
DaniPopes May 22, 2023
73b3106
fix: RLP `Encode` impl for `FixedBytes` and `bool`
DaniPopes May 22, 2023
39e127f
proptest
DaniPopes May 22, 2023
a096c94
chore: clippy
DaniPopes May 22, 2023
b0406e3
are you ok proptest?
DaniPopes May 22, 2023
828399d
improve serde impls
DaniPopes May 23, 2023
5f85e52
update chain impl
DaniPopes May 29, 2023
080d8c5
fixings
DaniPopes May 29, 2023
2acd1a7
Merge branch 'main' into dani/changes
DaniPopes May 31, 2023
3aa999e
Merge branch 'dani/changes' into dani/rpc-types
DaniPopes May 31, 2023
cd683b2
Merge branch 'main' into dani/rpc-types
DaniPopes Jul 7, 2023
83ce672
updates
DaniPopes Jul 7, 2023
246c667
sync reth_rpc_types
DaniPopes Jul 7, 2023
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ alloy-json-abi = { version = "0.2.0", path = "crates/json-abi", default-features
alloy-primitives = { version = "0.2.0", path = "crates/primitives", default-features = false }
alloy-rlp = { version = "0.2.0", path = "crates/rlp", default-features = false }
alloy-rlp-derive = { version = "0.2.0", path = "crates/rlp-derive", default-features = false }
alloy-rpc-types = { version = "0.2.0", path = "crates/rpc-types", default-features = false }
alloy-sol-macro = { version = "0.2.0", path = "crates/sol-macro", default-features = false }
alloy-sol-types = { version = "0.2.0", path = "crates/sol-types", default-features = false }
syn-solidity = { version = "0.2.0", path = "crates/syn-solidity", default-features = false }
alloy-types = { version = "0.2.0", path = "crates/types", default-features = false }

ruint = { version = "1.9.0", package = "ruint2", default-features = false }
ruint-macro = { version = "1.0.3", package = "ruint2-macro", default-features = false }
Expand Down Expand Up @@ -62,3 +64,5 @@ tiny-keccak = "2.0"
# make ruint2 depend on the local alloy-rlp
[patch.crates-io]
alloy-rlp = { path = "crates/rlp" }
alloy-rlp-derive = { path = "crates/rlp-derive" }
ruint2 = { path = "../uint/crates/ruint" }
42 changes: 42 additions & 0 deletions crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "alloy-rpc-types"
description = "Ethereum RPC types"
readme = "README.md"
keywords = ["ethereum", "abi", "encoding", "evm", "rpc"]
categories = ["encoding", "cryptography::cryptocurrencies"]
homepage = "https://github.com/alloy-rs/core/tree/main/crates/rpc-types"

version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["std", "rlp"] }
alloy-rlp = { workspace = true, features = ["std", "derive"] }

# misc
serde = { workspace = true, features = ["std", "derive"] }
serde_json = { workspace = true, features = ["std"] }
bytes = { workspace = true, features = ["std"] }
hex = { workspace = true, features = ["std"] }
hex-literal.workspace = true
once_cell = { workspace = true, features = ["std"] }

# arbitrary
arbitrary = { workspace = true, features = ["derive"], optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["std", "arbitrary"] }
arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
proptest-derive.workspace = true

[features]
arbitrary = ["alloy-primitives/arbitrary", "dep:arbitrary", "dep:proptest", "dep:proptest-derive"]
test-utils = []
5 changes: 5 additions & 0 deletions crates/rpc-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ethers-rpc-types

Ethereum RPC type definitions.

Contains all relevant types for the various RPC endpoints, grouped by namespace.
12 changes: 12 additions & 0 deletions crates/rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![doc = include_str!("../README.md")]
#![warn(
missing_docs,
unreachable_pub,
// unused_crate_dependencies, // TODO
clippy::missing_const_for_fn
)]
#![deny(unused_must_use, rust_2018_idioms)]

// TODO: no_std ?

// TODO: See `ethers-types`
72 changes: 72 additions & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[package]
name = "alloy-types"
description = "Ethereum types"
keywords = ["ethereum", "abi", "encoding", "evm", "eth"]
categories = ["encoding", "cryptography::cryptocurrencies"]
homepage = "https://github.com/alloy-rs/core/tree/main/crates/types"

version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["std", "serde", "rlp"] }
alloy-rlp = { workspace = true, features = ["std", "derive"] }

# crypto
secp256k1 = { version = "0.27.0", features = ["recovery"] }

# used for forkid
crc = "3"

# only "net" modules
url = "2"

# misc
serde = { workspace = true, features = ["std", "derive"] }
serde_json = { workspace = true, features = ["std"] }
bytes = { workspace = true, features = ["std"] }
hex = { workspace = true, features = ["std"] }
once_cell = { workspace = true, features = ["std"] }

# macros
derive_more.workspace = true
num_enum.workspace = true
strum.workspace = true
thiserror.workspace = true

# proof related
triehash = { version = "0.8", optional = true }
plain_hasher = { version = "0.2", optional = true }
hash-db = { version = "0.15", optional = true }
tracing = { version = "0.1", optional = true }

# arbitrary
arbitrary = { workspace = true, optional = true }
derive_arbitrary = { workspace = true, optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

[dev-dependencies]
assert_matches = "1"
rand = "0.8"

alloy-primitives = { workspace = true, features = ["std", "arbitrary", "getrandom"] }
arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
proptest-derive.workspace = true

[features]
proof = ["dep:triehash", "dep:plain_hasher", "dep:hash-db", "dep:tracing"]
arbitrary = [
"alloy-primitives/arbitrary",
"dep:arbitrary",
"dep:derive_arbitrary",
"dep:proptest",
"dep:proptest-derive",
]
test-utils = []
14 changes: 14 additions & 0 deletions crates/types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ethers-types

Ethereum type definitions.

# TODO

1. [x] Implement RLP for `ruint::Uint` (upstream)
- remove all `TODO_UINT_RLP`
- fix encoding tests
2. [x] Fix remaining `TODO`s and inline `cfg(TODO.*)`
- ~~see also Cargo.toml deps~~
3. [ ] Dedup _all_ primitive and rpc types
- Tracked by `unreachable_pub` or `ambiguous_glob_reexports` rustc lints, #107880 >=1.70 (currently beta or nightly)
4. [ ] Split rpc types into `ethers-rpc-types`. We may be able to make it completely independent of `ethers-types`
1 change: 1 addition & 0 deletions crates/types/res/genesis/goerli.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/types/res/genesis/mainnet.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/types/res/genesis/sepolia.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nonce":"0x00","timestamp":"0x6159af19","extraData":"0x5365706f6c69612c20417468656e732c204174746963612c2047726565636521","gasLimit":"0x1c9c380","difficulty":"0x20000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","stateRoot":"0x5eb6e371a698b8d68f665192350ffcecbbbf322916f4b51bd79bb6887da3f494","alloc":{"0xa2A6d93439144FFE4D27c9E088dCD8b783946263":{"balance":"0xD3C21BCECCEDA1000000"},"0xBc11295936Aa79d594139de1B2e12629414F3BDB":{"balance":"0xD3C21BCECCEDA1000000"},"0x7cF5b79bfe291A67AB02b393E456cCc4c266F753":{"balance":"0xD3C21BCECCEDA1000000"},"0xaaec86394441f915bce3e6ab399977e9906f3b69":{"balance":"0xD3C21BCECCEDA1000000"},"0xF47CaE1CF79ca6758Bfc787dbD21E6bdBe7112B8":{"balance":"0xD3C21BCECCEDA1000000"},"0xd7eDDB78ED295B3C9629240E8924fb8D8874ddD8":{"balance":"0xD3C21BCECCEDA1000000"},"0x8b7F0977Bb4f0fBE7076FA22bC24acA043583F5e":{"balance":"0xD3C21BCECCEDA1000000"},"0xe2e2659028143784d557bcec6ff3a0721048880a":{"balance":"0xD3C21BCECCEDA1000000"},"0xd9a5179f091d85051d3c982785efd1455cec8699":{"balance":"0xD3C21BCECCEDA1000000"},"0xbeef32ca5b9a198d27B4e02F4c70439fE60356Cf":{"balance":"0xD3C21BCECCEDA1000000"},"0x0000006916a87b82333f4245046623b23794c65c":{"balance":"0x84595161401484A000000"},"0xb21c33de1fab3fa15499c62b59fe0cc3250020d1":{"balance":"0x52B7D2DCC80CD2E4000000"},"0x10F5d45854e038071485AC9e402308cF80D2d2fE":{"balance":"0x52B7D2DCC80CD2E4000000"},"0xd7d76c58b3a519e9fA6Cc4D22dC017259BC49F1E":{"balance":"0x52B7D2DCC80CD2E4000000"},"0x799D329e5f583419167cD722962485926E338F4a":{"balance":"0xDE0B6B3A7640000"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}
110 changes: 110 additions & 0 deletions crates/types/src/admin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use crate::{NodeRecord, PeerId};
use alloy_primitives::{B256, U256};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
net::{IpAddr, SocketAddr},
};

/// Represents the `admin_nodeInfo` response, which can be queried for all the
/// information known about the running node at the networking granularity.
///
/// Note: this format is not standardized. Reth follows Geth's format,
/// see: <https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin>
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NodeInfo {
/// Enode of the node in URL format.
pub enode: NodeRecord,
/// ID of the local node.
pub id: PeerId,
/// IP of the local node.
pub ip: IpAddr,
/// Address exposed for listening for the local node.
#[serde(rename = "listenAddr")]
pub listen_addr: SocketAddr,
/// Ports exposed by the node for discovery and listening.
pub ports: Ports,
/// Name of the network
pub name: String,
/// Networking protocols being run by the local node.
pub protocols: Protocols,
}

impl NodeInfo {
/// Creates a new instance of `NodeInfo`.
pub fn new(enr: NodeRecord, status: NetworkStatus) -> NodeInfo {
NodeInfo {
enode: enr,
id: enr.id,
ip: enr.address,
listen_addr: enr.tcp_addr(),
ports: Ports {
discovery: enr.udp_port,
listener: enr.tcp_port,
},
name: status.client_version,
protocols: Protocols {
eth: status.eth_protocol_info,
other: Default::default(),
},
}
}
}

/// All supported protocols
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Protocols {
/// Info about `eth` sub-protocol
pub eth: EthProtocolInfo,
/// Placeholder for any other protocols
#[serde(flatten, default)]
pub other: BTreeMap<String, serde_json::Value>,
}

/// Ports exposed by the node for discovery and listening.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ports {
/// Port exposed for node discovery.
pub discovery: u16,
/// Port exposed for listening.
pub listener: u16,
}

/// The status of the network being ran by the local node.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkStatus {
/// The local node client version.
pub client_version: String,
/// The current ethereum protocol version
pub protocol_version: u64,
/// Information about the Ethereum Wire Protocol.
pub eth_protocol_info: EthProtocolInfo,
}

/// Information about the Ethereum Wire Protocol (ETH)
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EthProtocolInfo {
/// The current difficulty at the head of the chain.
pub difficulty: U256,
/// The block hash of the head of the chain.
pub head: B256,
/// Network ID in base 10.
pub network: u64,
/// Genesis block of the current chain.
pub genesis: B256,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_node_info_roundtrip() {
let sample = r#"{"enode":"enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@[::]:30303","id":"44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d","ip":"::","listenAddr":"[::]:30303","name":"reth","ports":{"discovery":30303,"listener":30303},"protocols":{"eth":{"difficulty":17334254859343145000,"genesis":"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3","head":"0xb83f73fbe6220c111136aefd27b160bf4a34085c65ba89f24246b3162257c36a","network":1}}}"#;

let info: NodeInfo = serde_json::from_str(sample).unwrap();
let serialized = serde_json::to_string(&info).unwrap();
let de_serialized: NodeInfo = serde_json::from_str(&serialized).unwrap();
assert_eq!(info, de_serialized)
}
}
10 changes: 10 additions & 0 deletions crates/types/src/chain/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use alloy_primitives::{BlockNumber, B256};

/// Current status of the blockchain's head.
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct ChainInfo {
/// The block hash of the highest fully synced block.
pub best_hash: B256,
/// The block number of the highest fully synced block.
pub best_number: BlockNumber,
}
Loading