add reject message

This commit is contained in:
Tamas Blummer 2019-08-27 13:01:21 +02:00
parent 24361dd2f1
commit 3266c192b6
2 changed files with 71 additions and 1 deletions

View File

@ -131,7 +131,9 @@ pub enum NetworkMessage {
/// BIP157 cfcheckpt
CFCheckpt(message_filter::CFCheckpt),
/// `alert`
Alert(Vec<u8>)
Alert(Vec<u8>),
/// `reject`
Reject(message_network::Reject)
}
impl RawNetworkMessage {
@ -161,6 +163,7 @@ impl RawNetworkMessage {
NetworkMessage::GetCFCheckpt(_) => "getcfckpt",
NetworkMessage::CFCheckpt(_) => "cfcheckpt",
NetworkMessage::Alert(_) => "alert",
NetworkMessage::Reject(_) => "reject",
}.to_owned()
}
}
@ -211,6 +214,7 @@ impl Encodable for RawNetworkMessage {
NetworkMessage::GetCFCheckpt(ref dat) => serialize(dat),
NetworkMessage::CFCheckpt(ref dat) => serialize(dat),
NetworkMessage::Alert(ref dat) => serialize(dat),
NetworkMessage::Reject(ref dat) => serialize(dat),
NetworkMessage::Verack
| NetworkMessage::SendHeaders
| NetworkMessage::MemPool

View File

@ -20,6 +20,12 @@
use network::address::Address;
use network::constants;
use consensus::{Encodable, Decodable, ReadExt};
use consensus::encode;
use std::io;
use byteorder::WriteBytesExt;
use network::message_network::RejectReason::{MALFORMED, INVALID, OBSOLETE, DUPLICATE, NONSTANDARD, DUST, CHECKPOINT, FEE};
use hashes::sha256d;
/// Some simple messages
@ -78,6 +84,66 @@ impl_consensus_encoding!(VersionMessage, version, services, timestamp,
receiver, sender, nonce,
user_agent, start_height, relay);
#[repr(u8)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
/// message rejection reason as a code
pub enum RejectReason {
/// malformed message
MALFORMED = 0x01,
/// invalid message
INVALID = 0x10,
/// obsolete message
OBSOLETE = 0x11,
/// duplicate message
DUPLICATE = 0x12,
/// nonstandard transaction
NONSTANDARD = 0x40,
/// an output is belw dust limit
DUST = 0x41,
/// insufficient fee
FEE = 0x42,
/// checkpoint
CHECKPOINT = 0x43
}
impl Encodable for RejectReason {
fn consensus_encode<W: io::Write>(&self, mut e: W) -> Result<usize, encode::Error> {
e.write_u8(*self as u8)?;
Ok(1)
}
}
impl Decodable for RejectReason {
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
Ok(match d.read_u8()? {
0x01 => MALFORMED,
0x10 => INVALID,
0x11 => OBSOLETE,
0x12 => DUPLICATE,
0x40 => NONSTANDARD,
0x41 => DUST,
0x42 => FEE,
0x43 => CHECKPOINT,
_ => return Err(encode::Error::ParseFailed("unknonw reject code"))
})
}
}
/// Reject message might be sent by peers rejecting one of our messages
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Reject {
/// message type rejected
pub message: String,
/// reason of rejection as code
pub ccode: RejectReason,
/// reason of rejectection
pub reason: String,
/// reference to rejected item
pub hash: sha256d::Hash
}
impl_consensus_encoding!(Reject, message, ccode, reason, hash);
#[cfg(test)]
mod tests {
use super::VersionMessage;