-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse inv, addr, getaddr messages. #31
Conversation
013268c might be useful for cherry-picking |
(tried changing base to refresh included commits, then realised it required rebasing). |
|
||
/// An address with metadata on its advertised services and last-seen time. | ||
/// | ||
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#Network_address) | ||
// XXX determine whether we will use this struct in *our* networking handling |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suspicion is that we will want to move it into protocol
but I'd rather wait a bit to see how things settle, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me, more metadata thingies related to the protocol.
1250799
to
e42da36
Compare
Did some cleanup and also closes #25. |
@@ -126,7 +126,7 @@ impl Encoder for Codec { | |||
GetBlocks { .. } => b"getblocks\0\0\0", | |||
Headers { .. } => b"headers\0\0\0\0\0", | |||
GetHeaders { .. } => b"getheaders\0\0", | |||
Inventory { .. } => b"inv\0\0\0\0\0\0\0\0\0", // XXX Inventory -> Inv ? | |||
Inv { .. } => b"inv\0\0\0\0\0\0\0\0\0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No strong opinions on this -- I'm happy to drop this commit from the PR but aligning names with bitcoin like this makes the table line up nicer.
addr.zcash_serialize(&mut writer)?; | ||
} | ||
} | ||
Inv(ref hashes) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be ref hashes
so that hashes: &Vec<InventoryHash>
, not hashes: Vec<InventoryHash>
, which would cause the vector to be moved out of the borrowed Message
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Perhaps the Version
fields should also use ref
match patterns (I think they're all Copy
, but taking borrows might be slightly more efficient).
@@ -274,7 +287,8 @@ impl Decoder for Codec { | |||
} | |||
|
|||
// Now that we know we have the full body, split off the body, | |||
// and reset the decoder state for the next message. | |||
// and reset the decoder state for the next message. Otherwise | |||
// we will attempt to read the next header as the current body. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixup from #22 (comment)
pub struct TxHash(pub [u8; 32]); | ||
/// Stub-- delete later. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub struct BlockHash(pub [u8; 32]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to be replaced with types from #21 before merging; these are just placeholders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These types are used for protocol messages, so it makes more sense to keep them scoped with the protocol handling, rather than other networking logic.
These are starting to stack up but I think until generic arrays arrive the cure is worse than the disease :S
This removes the inventory vector structs from `zebra-chain` (as they are really part of the network protocol) and refactors them into a single `InventoryHash` type. This corresponds to Bitcoin's "inventory vector" but with a different, better name (it's not a vector, it's just a typed hash of some other item).
I don't feel super strongly about this change, so I'm happy to drop it, but it makes the parsing match statements line up nicely and aligns naming with the naming used in Bitcoin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
// case. Although the size of the recieved data buffer is bounded by the | ||
// codec's max_len field, it's still possible for someone to send a | ||
// short addr message with a large count field, so if we naively trust | ||
// the count field we could be tricked into preallocating a large |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
// addrs are encoded as: timestamp + services + ipv6 + port | ||
const ENCODED_ADDR_SIZE: usize = 4 + 8 + 16 + 2; | ||
let max_count = self.builder.max_len / ENCODED_ADDR_SIZE; | ||
let mut addrs = Vec::with_capacity(std::cmp::min(count, max_count)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
// codec's max_len field, it's still possible for someone to send a | ||
// short message with a large count field, so if we naively trust | ||
// the count field we could be tricked into preallocating a large | ||
// buffer. Instead, calculate the maximum count for a valid message from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
||
/// An inventory hash which refers to some advertised or requested data. | ||
/// | ||
/// Bitcoin calls this an "inventory vector" but it is just a typed hash, not a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize, | ||
}; | ||
|
||
/// Stub-- delete later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
|
||
/// Stub-- delete later. | ||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub struct TxHash(pub [u8; 32]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the replacement for this would live in zebra-chain
, yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, and the BlockHash
should turn into a BlockHeaderHash
from your PR, I just didn't want to change them until both were available.
/// Inventory vectors. | ||
inventory: Vec<zebra_chain::types::InventoryVector>, | ||
}, | ||
Inv(Vec<InventoryHash>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
/// Inventory vectors. | ||
inventory: Vec<zebra_chain::types::InventoryVector>, | ||
}, | ||
GetData(Vec<InventoryHash>), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
(Note: this is supposed to apply on top of #20 or later).
Blocked on #21, in the sense that this PR defines some stub types which are actually part of #21 and should be edited once it lands.
This doesn't include tests but I would rather do the testing via #28 or #27; this could either be merged before or after that.