few more message types

This commit is contained in:
debris 2016-10-03 23:36:22 +02:00
parent 362bfc6539
commit e8de78bc0b
10 changed files with 137 additions and 19 deletions

1
Cargo.lock generated
View File

@ -160,6 +160,7 @@ version = "0.1.0"
dependencies = [
"bitcrypto 0.1.0",
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"chain 0.1.0",
"primitives 0.1.0",
"serialization 0.1.0",
]

View File

@ -8,6 +8,7 @@ use ser::{
use merkle_root::merkle_root;
use {BlockHeader, Transaction};
#[derive(Debug, PartialEq)]
pub struct Block {
block_header: BlockHeader,
transactions: Vec<Transaction>,

View File

@ -30,7 +30,7 @@ pub const SEQUENCE_LOCKTIME_TYPE_FLAG: u32 = (1 << 22);
pub const SEQUENCE_LOCKTIME_MASK: u32 = 0x0000ffff;
#[derive(Debug, Clone)]
#[derive(Debug, PartialEq, Clone)]
pub struct OutPoint {
pub hash: H256,
pub index: u32,
@ -65,7 +65,7 @@ impl OutPoint {
}
}
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct TransactionInput {
pub previous_output: OutPoint,
pub script_sig: Bytes,
@ -107,7 +107,7 @@ impl TransactionInput {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, PartialEq, Clone)]
pub struct TransactionOutput {
pub value: u64,
pub script_pubkey: Bytes,
@ -151,7 +151,7 @@ impl TransactionOutput {
}
}
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Transaction {
pub version: i32,
pub inputs: Vec<TransactionInput>,

View File

@ -7,6 +7,7 @@ authors = ["debris <marek.kotewicz@gmail.com>"]
byteorder = "0.5"
bitcrypto = { path = "../crypto" }
chain = { path = "../chain" }
primitives = { path = "../primitives" }
serialization = { path = "../serialization" }

View File

@ -1,5 +1,6 @@
extern crate byteorder;
extern crate bitcrypto as crypto;
extern crate chain;
extern crate primitives;
extern crate serialization as ser;

View File

@ -2,8 +2,12 @@ use ser::{
Serializable, Stream,
Reader, Error as ReaderError
};
use chain::{Transaction, Block};
use common::Command;
use types::{Version, Addr, AddrBelow31402};
use types::{
Version, Addr, AddrBelow31402, Inv,
GetData, NotFound, GetBlocks, GetHeaders
};
pub fn deserialize_payload(data: &[u8], version: u32, command: &Command) -> Result<Payload, ReaderError> {
let mut reader = Reader::new(data);
@ -14,6 +18,13 @@ pub fn deserialize_payload(data: &[u8], version: u32, command: &Command) -> Resu
true => reader.read().map(Payload::Addr),
false => reader.read().map(Payload::AddrBelow31402),
},
"inv" => reader.read().map(Payload::Inv),
"getdata" => reader.read().map(Payload::GetData),
"notfound" => reader.read().map(Payload::NotFound),
"getblocks" => reader.read().map(Payload::GetBlocks),
"getheaders" => reader.read().map(Payload::GetHeaders),
"tx" => reader.read().map(Payload::Tx),
"block" => reader.read().map(Payload::Block),
_ => Err(ReaderError::MalformedData),
}
}
@ -24,15 +35,31 @@ pub enum Payload {
Verack,
Addr(Addr),
AddrBelow31402(AddrBelow31402),
Inv(Inv),
GetData(GetData),
NotFound(NotFound),
GetBlocks(GetBlocks),
GetHeaders(GetHeaders),
Tx(Transaction),
Block(Block),
}
impl Payload {
pub fn command(&self) -> Command {
match *self {
Payload::Version(_) => "version".into(),
Payload::Verack => "verack".into(),
Payload::Addr(_) | Payload::AddrBelow31402(_) => "addr".into(),
}
let cmd = match *self {
Payload::Version(_) => "version",
Payload::Verack => "verack",
Payload::Addr(_) | Payload::AddrBelow31402(_) => "addr",
Payload::Inv(_) => "inv",
Payload::GetData(_) => "getdata",
Payload::NotFound(_) => "notfound",
Payload::GetBlocks(_) => "getblocks",
Payload::GetHeaders(_) => "getheaders",
Payload::Tx(_) => "tx",
Payload::Block(_) => "block",
};
cmd.into()
}
}
@ -40,16 +67,17 @@ impl Payload {
impl Serializable for Payload {
fn serialize(&self, stream: &mut Stream) {
match *self {
Payload::Version(ref version) => {
stream.append(version);
},
Payload::Version(ref p) => { stream.append(p); },
Payload::Verack => {},
Payload::Addr(ref addr) => {
stream.append(addr);
},
Payload::AddrBelow31402(ref addr) => {
stream.append(addr);
},
Payload::Addr(ref p) => { stream.append(p); },
Payload::AddrBelow31402(ref p) => { stream.append(p); },
Payload::Inv(ref p) => { stream.append(p); },
Payload::GetData(ref p) => { stream.append(p); },
Payload::NotFound(ref p) => { stream.append(p); },
Payload::GetBlocks(ref p) => { stream.append(p); },
Payload::GetHeaders(ref p) => { stream.append(p); },
Payload::Tx(ref p) => { stream.append(p); },
Payload::Block(ref p) => { stream.append(p); },
}
}
}

View File

@ -0,0 +1,31 @@
use hash::H256;
use ser::{Serializable, Stream, Deserializable, Reader, Error as ReaderError};
#[derive(Debug, PartialEq)]
pub struct GetBlocks {
version: u32,
block_locator_hashes: Vec<H256>,
hash_stop: H256,
}
impl Serializable for GetBlocks {
fn serialize(&self, stream: &mut Stream) {
stream
.append(&self.version)
.append_list(&self.block_locator_hashes)
.append(&self.hash_stop);
}
}
impl Deserializable for GetBlocks {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
let get_blocks = GetBlocks {
version: try!(reader.read()),
block_locator_hashes: try!(reader.read_list()),
hash_stop: try!(reader.read()),
};
Ok(get_blocks)
}
}

View File

@ -0,0 +1,22 @@
use chain::BlockHeader;
use ser::{Serializable, Stream, Deserializable, Reader, Error as ReaderError};
pub struct Headers {
headers: Vec<BlockHeader>,
}
impl Serializable for Headers {
fn serialize(&self, stream: &mut Stream) {
stream.append_list(&self.headers);
}
}
impl Deserializable for Headers {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
let headers = Headers {
headers: try!(reader.read_list()),
};
Ok(headers)
}
}

23
message/src/types/inv.rs Normal file
View File

@ -0,0 +1,23 @@
use common::InventoryVector;
use ser::{Serializable, Stream, Deserializable, Reader, Error as ReaderError};
#[derive(Debug, PartialEq)]
pub struct Inv {
inventory: Vec<InventoryVector>,
}
impl Serializable for Inv {
fn serialize(&self, stream: &mut Stream) {
stream.append_list(&self.inventory);
}
}
impl Deserializable for Inv {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
let inv = Inv {
inventory: try!(reader.read_list()),
};
Ok(inv)
}
}

View File

@ -1,5 +1,15 @@
mod addr;
mod getblocks;
mod headers;
mod inv;
pub mod version;
pub use self::addr::{Addr, AddrBelow31402};
pub use self::getblocks::GetBlocks;
pub use self::headers::Headers;
pub use self::inv::Inv;
pub use self::version::Version;
pub type GetData = Inv;
pub type NotFound = Inv;
pub type GetHeaders = GetBlocks;