mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
move HasRemoteAddr trait to reth-net-common (#775)
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -3631,6 +3631,7 @@ dependencies = [
|
||||
"hmac",
|
||||
"pin-project",
|
||||
"rand 0.8.5",
|
||||
"reth-net-common",
|
||||
"reth-primitives",
|
||||
"reth-rlp",
|
||||
"secp256k1 0.24.2",
|
||||
@ -3786,7 +3787,6 @@ name = "reth-net-common"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pin-project",
|
||||
"reth-ecies",
|
||||
"reth-primitives",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
@ -11,7 +11,6 @@ Types shared across network code
|
||||
[dependencies]
|
||||
# reth
|
||||
reth-primitives = { path = "../../primitives" }
|
||||
reth-ecies = { path = "../ecies" }
|
||||
|
||||
# async
|
||||
pin-project = "1.0"
|
||||
|
||||
@ -20,7 +20,6 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use reth_ecies::stream::HasRemoteAddr;
|
||||
use std::{
|
||||
convert::TryFrom as _,
|
||||
io,
|
||||
@ -37,6 +36,8 @@ use tokio::{
|
||||
net::TcpStream,
|
||||
};
|
||||
|
||||
use crate::stream::HasRemoteAddr;
|
||||
|
||||
/// Meters bandwidth usage of streams
|
||||
#[derive(Debug)]
|
||||
struct BandwidthMeterInner {
|
||||
|
||||
@ -9,3 +9,5 @@
|
||||
|
||||
pub mod ban_list;
|
||||
pub mod bandwidth_meter;
|
||||
/// Traits related to tokio streams
|
||||
pub mod stream;
|
||||
|
||||
13
crates/net/common/src/stream.rs
Normal file
13
crates/net/common/src/stream.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use std::net::SocketAddr;
|
||||
use tokio::net::TcpStream;
|
||||
/// This trait is for instrumenting a TCPStream with a socket addr
|
||||
pub trait HasRemoteAddr {
|
||||
/// Maybe returns a [`SocketAddr`]
|
||||
fn remote_addr(&self) -> Option<SocketAddr>;
|
||||
}
|
||||
|
||||
impl HasRemoteAddr for TcpStream {
|
||||
fn remote_addr(&self) -> Option<SocketAddr> {
|
||||
self.peer_addr().ok()
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ readme = "README.md"
|
||||
[dependencies]
|
||||
reth-rlp = { path = "../../common/rlp", features = ["derive", "ethereum-types", "std"] }
|
||||
reth-primitives = { path = "../../primitives" }
|
||||
reth-net-common = { path = "../common" }
|
||||
|
||||
futures = "0.3.24"
|
||||
thiserror = "1.0.37"
|
||||
|
||||
@ -4,19 +4,16 @@ use crate::{
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use futures::{ready, Sink, SinkExt};
|
||||
use reth_net_common::stream::HasRemoteAddr;
|
||||
use reth_primitives::H512 as PeerId;
|
||||
use secp256k1::SecretKey;
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
io,
|
||||
net::SocketAddr,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
use tokio::{
|
||||
io::{AsyncRead, AsyncWrite},
|
||||
net::TcpStream,
|
||||
};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tokio_stream::{Stream, StreamExt};
|
||||
use tokio_util::codec::{Decoder, Framed};
|
||||
use tracing::{debug, instrument, trace};
|
||||
@ -30,18 +27,6 @@ pub struct ECIESStream<Io> {
|
||||
remote_id: PeerId,
|
||||
}
|
||||
|
||||
/// This trait is just for instrumenting the stream with a socket addr
|
||||
pub trait HasRemoteAddr {
|
||||
/// Maybe returns a [`SocketAddr`]
|
||||
fn remote_addr(&self) -> Option<SocketAddr>;
|
||||
}
|
||||
|
||||
impl HasRemoteAddr for TcpStream {
|
||||
fn remote_addr(&self) -> Option<SocketAddr> {
|
||||
self.peer_addr().ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Io> ECIESStream<Io>
|
||||
where
|
||||
Io: AsyncRead + AsyncWrite + Unpin + HasRemoteAddr,
|
||||
@ -162,7 +147,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::util::pk2id;
|
||||
use secp256k1::{rand, SECP256K1};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
#[tokio::test]
|
||||
async fn can_write_and_read() {
|
||||
|
||||
Reference in New Issue
Block a user