Forgotten testnet1 cherries (#475)
* Very quick peer banning endpoint, helps with #406 * Ping heights (#407) * add height to ping/ping * reformat output * fix p2p test * Fix orphan handling, not related to current head. Fixes #412 * Check before borrow, fixes #267 * Not finding an output commit in pos is an AlreadySpent * Fix race condition, sending before conn is ready * Explicit error for unknown pos of a forked block * Remove config outdated tests. Fix #333 * Check ref and try before borrow, fix #400 * We do not want to sync with old peers anyway * Hide cargo compiler warning for unused NoopAdapter and unused test code. Add TODOs
This commit is contained in:
committed by
Ignotus Peverell
parent
bffd955c26
commit
17d5898677
+16
-2
@@ -479,11 +479,14 @@ pub struct Ping {
|
||||
/// total difficulty accumulated by the sender, used to check whether sync
|
||||
/// may be needed
|
||||
pub total_difficulty: Difficulty,
|
||||
/// total height
|
||||
pub height: u64,
|
||||
}
|
||||
|
||||
impl Writeable for Ping {
|
||||
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
||||
self.total_difficulty.write(writer).unwrap();
|
||||
self.height.write(writer).unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -495,7 +498,11 @@ impl Readable for Ping {
|
||||
Ok(diff) => diff,
|
||||
Err(_) => Difficulty::zero(),
|
||||
};
|
||||
Ok(Ping { total_difficulty })
|
||||
let height = match reader.read_u64(){
|
||||
Ok(h) => h,
|
||||
Err(_) => 0,
|
||||
};
|
||||
Ok(Ping { total_difficulty, height })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -503,11 +510,14 @@ pub struct Pong {
|
||||
/// total difficulty accumulated by the sender, used to check whether sync
|
||||
/// may be needed
|
||||
pub total_difficulty: Difficulty,
|
||||
/// height accumulated by sender
|
||||
pub height: u64
|
||||
}
|
||||
|
||||
impl Writeable for Pong {
|
||||
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
||||
self.total_difficulty.write(writer).unwrap();
|
||||
self.height.write(writer).unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -519,6 +529,10 @@ impl Readable for Pong {
|
||||
Ok(diff) => diff,
|
||||
Err(_) => Difficulty::zero(),
|
||||
};
|
||||
Ok(Pong { total_difficulty })
|
||||
let height = match reader.read_u64() {
|
||||
Ok(h) => h,
|
||||
Err(_) => 0,
|
||||
};
|
||||
Ok(Pong { total_difficulty, height })
|
||||
}
|
||||
}
|
||||
|
||||
+9
-4
@@ -140,14 +140,15 @@ impl Peer {
|
||||
self.proto.transmitted_bytes()
|
||||
}
|
||||
|
||||
pub fn send_ping(&self, total_difficulty: Difficulty) -> Result<(), Error> {
|
||||
self.proto.send_ping(total_difficulty)
|
||||
pub fn send_ping(&self, total_difficulty: Difficulty, height: u64) -> Result<(), Error> {
|
||||
self.proto.send_ping(total_difficulty, height)
|
||||
}
|
||||
|
||||
/// Sends the provided block to the remote peer. The request may be dropped
|
||||
/// if the remote peer is known to already have the block.
|
||||
pub fn send_block(&self, b: &core::Block) -> Result<(), Error> {
|
||||
if !self.tracking_adapter.has(b.hash()) {
|
||||
debug!(LOGGER, "Send block {} to {}", b.hash(), self.info.addr);
|
||||
self.proto.send_block(b)
|
||||
} else {
|
||||
Ok(())
|
||||
@@ -225,6 +226,10 @@ impl NetAdapter for TrackingAdapter {
|
||||
self.adapter.total_difficulty()
|
||||
}
|
||||
|
||||
fn total_height(&self) -> u64 {
|
||||
self.adapter.total_height()
|
||||
}
|
||||
|
||||
fn transaction_received(&self, tx: core::Transaction) {
|
||||
self.push(tx.hash());
|
||||
self.adapter.transaction_received(tx)
|
||||
@@ -259,7 +264,7 @@ impl NetAdapter for TrackingAdapter {
|
||||
self.adapter.peer_connected(pi)
|
||||
}
|
||||
|
||||
fn peer_difficulty(&self, addr: SocketAddr, diff: Difficulty) {
|
||||
self.adapter.peer_difficulty(addr, diff)
|
||||
fn peer_difficulty(&self, addr: SocketAddr, diff: Difficulty, height:u64) {
|
||||
self.adapter.peer_difficulty(addr, diff, height)
|
||||
}
|
||||
}
|
||||
|
||||
+10
-6
@@ -67,11 +67,11 @@ impl Protocol for ProtocolV1 {
|
||||
|
||||
/// Sends a ping message to the remote peer. Will panic if handle has never
|
||||
/// been called on this protocol.
|
||||
fn send_ping(&self, total_difficulty: Difficulty) -> Result<(), Error> {
|
||||
fn send_ping(&self, total_difficulty: Difficulty, height: u64) -> Result<(), Error> {
|
||||
self.send_request(
|
||||
Type::Ping,
|
||||
Type::Pong,
|
||||
&Ping { total_difficulty },
|
||||
&Ping { total_difficulty, height },
|
||||
None,
|
||||
)
|
||||
}
|
||||
@@ -128,7 +128,11 @@ impl ProtocolV1 {
|
||||
body: &W,
|
||||
expect_resp: Option<Hash>,
|
||||
) -> Result<(), Error> {
|
||||
self.conn.borrow().send_request(t, rt, body, expect_resp)
|
||||
if self.conn.is_initialized() {
|
||||
self.conn.borrow().send_request(t, rt, body, expect_resp)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,8 +146,8 @@ fn handle_payload(
|
||||
match header.msg_type {
|
||||
Type::Ping => {
|
||||
let ping = ser::deserialize::<Ping>(&mut &buf[..])?;
|
||||
adapter.peer_difficulty(addr, ping.total_difficulty);
|
||||
let pong = Pong { total_difficulty: adapter.total_difficulty() };
|
||||
adapter.peer_difficulty(addr, ping.total_difficulty, ping.height);
|
||||
let pong = Pong { total_difficulty: adapter.total_difficulty(), height: adapter.total_height() };
|
||||
let mut body_data = vec![];
|
||||
try!(ser::serialize(&mut body_data, &pong));
|
||||
let mut data = vec![];
|
||||
@@ -157,7 +161,7 @@ fn handle_payload(
|
||||
}
|
||||
Type::Pong => {
|
||||
let pong = ser::deserialize::<Pong>(&mut &buf[..])?;
|
||||
adapter.peer_difficulty(addr, pong.total_difficulty);
|
||||
adapter.peer_difficulty(addr, pong.total_difficulty, pong.height);
|
||||
Ok(None)
|
||||
},
|
||||
Type::Transaction => {
|
||||
|
||||
+8
-3
@@ -44,6 +44,9 @@ impl NetAdapter for DummyAdapter {
|
||||
fn total_difficulty(&self) -> Difficulty {
|
||||
Difficulty::one()
|
||||
}
|
||||
fn total_height(&self) -> u64 {
|
||||
0
|
||||
}
|
||||
fn transaction_received(&self, _: core::Transaction) {}
|
||||
fn block_received(&self, _: core::Block, _: SocketAddr) {}
|
||||
fn headers_received(&self, _: Vec<core::BlockHeader>, _:SocketAddr) {}
|
||||
@@ -58,7 +61,7 @@ impl NetAdapter for DummyAdapter {
|
||||
}
|
||||
fn peer_addrs_received(&self, _: Vec<SocketAddr>) {}
|
||||
fn peer_connected(&self, _: &PeerInfo) {}
|
||||
fn peer_difficulty(&self, _: SocketAddr, _: Difficulty) {}
|
||||
fn peer_difficulty(&self, _: SocketAddr, _: Difficulty, _:u64) {}
|
||||
}
|
||||
|
||||
/// P2P server implementation, handling bootstrapping to find and connect to
|
||||
@@ -189,7 +192,8 @@ impl Server {
|
||||
.interval(Duration::new(20, 0))
|
||||
.fold((), move |_, _| {
|
||||
let total_diff = adapter.total_difficulty();
|
||||
check_peers(peers_inner.clone(), total_diff);
|
||||
let total_height = adapter.total_height();
|
||||
check_peers(peers_inner.clone(), total_diff, total_height);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
@@ -507,12 +511,13 @@ where
|
||||
fn check_peers(
|
||||
peers: Arc<RwLock<HashMap<SocketAddr, Arc<RwLock<Peer>>>>>,
|
||||
total_difficulty: Difficulty,
|
||||
height: u64
|
||||
) {
|
||||
let peers_map = peers.read().unwrap();
|
||||
for p in peers_map.values() {
|
||||
let p = p.read().unwrap();
|
||||
if p.is_connected() {
|
||||
let _ = p.send_ping(total_difficulty.clone());
|
||||
let _ = p.send_ping(total_difficulty.clone(), height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -135,7 +135,7 @@ pub trait Protocol {
|
||||
-> Box<Future<Item = (), Error = Error>>;
|
||||
|
||||
/// Sends a ping message to the remote peer.
|
||||
fn send_ping(&self, total_difficulty: Difficulty) -> Result<(), Error>;
|
||||
fn send_ping(&self, total_difficulty: Difficulty, height: u64) -> Result<(), Error>;
|
||||
|
||||
/// Relays a block to the remote peer.
|
||||
fn send_block(&self, b: &core::Block) -> Result<(), Error>;
|
||||
@@ -163,9 +163,12 @@ pub trait Protocol {
|
||||
/// forwarding or querying of blocks and transactions from the network among
|
||||
/// other things.
|
||||
pub trait NetAdapter: Sync + Send {
|
||||
/// Current height of our chain.
|
||||
/// Current total difficulty on our chain
|
||||
fn total_difficulty(&self) -> Difficulty;
|
||||
|
||||
/// Current total height
|
||||
fn total_height(&self) -> u64;
|
||||
|
||||
/// A valid transaction has been received from one of our peers
|
||||
fn transaction_received(&self, tx: core::Transaction);
|
||||
|
||||
@@ -196,5 +199,5 @@ pub trait NetAdapter: Sync + Send {
|
||||
fn peer_connected(&self, &PeerInfo);
|
||||
|
||||
/// Heard total_difficulty from a connected peer (via ping/pong).
|
||||
fn peer_difficulty(&self, SocketAddr, Difficulty);
|
||||
fn peer_difficulty(&self, SocketAddr, Difficulty, u64);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user