Skip to content
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

Merged
merged 8 commits into from
Sep 28, 2019
Merged

Parse inv, addr, getaddr messages. #31

merged 8 commits into from
Sep 28, 2019

Conversation

hdevalence
Copy link
Contributor

(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.

@hdevalence
Copy link
Contributor Author

hdevalence commented Sep 25, 2019

013268c might be useful for cherry-picking

@hdevalence hdevalence changed the base branch from main to block September 25, 2019 22:00
@hdevalence hdevalence changed the base branch from block to main September 25, 2019 22:00
@hdevalence
Copy link
Contributor Author

(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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

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?

Copy link
Contributor

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.

@hdevalence hdevalence force-pushed the inv branch 2 times, most recently from 1250799 to e42da36 Compare September 25, 2019 23:23
@hdevalence
Copy link
Contributor Author

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",
Copy link
Contributor Author

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) => {
Copy link
Contributor Author

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.

Copy link
Contributor Author

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.
Copy link
Contributor Author

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]);
Copy link
Contributor Author

@hdevalence hdevalence Sep 25, 2019

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#21 doesn't introduce a transaction hash type (which is part of #35); I would prefer to merge this changeset with placeholders intact until we have all of the types required in place (since it unblocks some other work).

@hdevalence hdevalence changed the title Parse inv messages. Parse inv, addr, getaddr messages. Sep 25, 2019
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.
@hdevalence hdevalence marked this pull request as ready for review September 27, 2019 16:40
Copy link
Contributor

@dconnolly dconnolly left a 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
Copy link
Contributor

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));
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

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]);
Copy link
Contributor

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?

Copy link
Contributor Author

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>),
Copy link
Contributor

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>),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dconnolly dconnolly merged commit 0a85be2 into main Sep 28, 2019
@dconnolly dconnolly deleted the inv branch September 28, 2019 00:41
This was referenced Sep 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants