use hashes::sha256d;
use io::{Read, Write};
use crate::consensus::{encode, Decodable, Encodable, ReadExt};
use crate::internal_macros::impl_consensus_encoding;
use crate::p2p;
use crate::p2p::address::Address;
use crate::p2p::ServiceFlags;
use crate::prelude::*;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct VersionMessage {
pub version: u32,
pub services: ServiceFlags,
pub timestamp: i64,
pub receiver: Address,
pub sender: Address,
pub nonce: u64,
pub user_agent: String,
pub start_height: i32,
pub relay: bool,
}
impl VersionMessage {
pub fn new(
services: ServiceFlags,
timestamp: i64,
receiver: Address,
sender: Address,
nonce: u64,
user_agent: String,
start_height: i32,
) -> VersionMessage {
VersionMessage {
version: p2p::PROTOCOL_VERSION,
services,
timestamp,
receiver,
sender,
nonce,
user_agent,
start_height,
relay: false,
}
}
}
impl_consensus_encoding!(
VersionMessage,
version,
services,
timestamp,
receiver,
sender,
nonce,
user_agent,
start_height,
relay
);
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum RejectReason {
Malformed = 0x01,
Invalid = 0x10,
Obsolete = 0x11,
Duplicate = 0x12,
NonStandard = 0x40,
Dust = 0x41,
Fee = 0x42,
Checkpoint = 0x43,
}
impl Encodable for RejectReason {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
w.write_all(&[*self as u8])?;
Ok(1)
}
}
impl Decodable for RejectReason {
fn consensus_decode<R: Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
Ok(match r.read_u8()? {
0x01 => RejectReason::Malformed,
0x10 => RejectReason::Invalid,
0x11 => RejectReason::Obsolete,
0x12 => RejectReason::Duplicate,
0x40 => RejectReason::NonStandard,
0x41 => RejectReason::Dust,
0x42 => RejectReason::Fee,
0x43 => RejectReason::Checkpoint,
_ => return Err(encode::Error::ParseFailed("unknown reject code")),
})
}
}
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Reject {
pub message: Cow<'static, str>,
pub ccode: RejectReason,
pub reason: Cow<'static, str>,
pub hash: sha256d::Hash,
}
impl_consensus_encoding!(Reject, message, ccode, reason, hash);
#[cfg(test)]
mod tests {
use hashes::sha256d;
use hex::test_hex_unwrap as hex;
use super::{Reject, RejectReason, VersionMessage};
use crate::consensus::encode::{deserialize, serialize};
use crate::p2p::ServiceFlags;
#[test]
fn version_message_test() {
let from_sat = hex!("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001");
let decode: Result<VersionMessage, _> = deserialize(&from_sat);
assert!(decode.is_ok());
let real_decode = decode.unwrap();
assert_eq!(real_decode.version, 70002);
assert_eq!(real_decode.services, ServiceFlags::NETWORK);
assert_eq!(real_decode.timestamp, 1401217254);
assert_eq!(real_decode.nonce, 16735069437859780935);
assert_eq!(real_decode.user_agent, "/Satoshi:0.9.99/".to_string());
assert_eq!(real_decode.start_height, 302892);
assert!(real_decode.relay);
assert_eq!(serialize(&real_decode), from_sat);
}
#[test]
fn reject_message_test() {
let reject_tx_conflict = hex!("027478121474786e2d6d656d706f6f6c2d636f6e666c69637405df54d3860b3c41806a3546ab48279300affacf4b88591b229141dcf2f47004");
let reject_tx_nonfinal = hex!("02747840096e6f6e2d66696e616c259bbe6c83db8bbdfca7ca303b19413dc245d9f2371b344ede5f8b1339a5460b");
let decode_result_conflict: Result<Reject, _> = deserialize(&reject_tx_conflict);
let decode_result_nonfinal: Result<Reject, _> = deserialize(&reject_tx_nonfinal);
assert!(decode_result_conflict.is_ok());
assert!(decode_result_nonfinal.is_ok());
let conflict = decode_result_conflict.unwrap();
assert_eq!("tx", conflict.message);
assert_eq!(RejectReason::Duplicate, conflict.ccode);
assert_eq!("txn-mempool-conflict", conflict.reason);
assert_eq!(
"0470f4f2dc4191221b59884bcffaaf00932748ab46356a80413c0b86d354df05"
.parse::<sha256d::Hash>()
.unwrap(),
conflict.hash
);
let nonfinal = decode_result_nonfinal.unwrap();
assert_eq!("tx", nonfinal.message);
assert_eq!(RejectReason::NonStandard, nonfinal.ccode);
assert_eq!("non-final", nonfinal.reason);
assert_eq!(
"0b46a539138b5fde4e341b37f2d945c23d41193b30caa7fcbd8bdb836cbe9b25"
.parse::<sha256d::Hash>()
.unwrap(),
nonfinal.hash
);
assert_eq!(serialize(&conflict), reject_tx_conflict);
assert_eq!(serialize(&nonfinal), reject_tx_nonfinal);
}
}