-
Notifications
You must be signed in to change notification settings - Fork 2
feat: support for getting blocks from Handshake peer #426
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
Conversation
📝 WalkthroughWalkthroughThis PR adds inventory and block message support to the handshake protocol. It introduces InvItem (Type uint32, Hash [32]byte) and inventory type constants, MsgGetData (Inventory []InvItem) and MsgBlock (Block *handshake.Block) with Encode/Decode implementations, and wires those message types into the protocol decoder. MsgGetData encodes a varint length and 36-byte items; MsgBlock decodes a block from a reader (Encode currently stubbed). Peer.GetBlock(hash) was added to send a getdata request and return a received MsgBlock. A unit test verifies MsgGetData encode/decode round-trip. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Signed-off-by: Aurora Gaffney <aurora@blinklabs.io>
1b1c330 to
3b220f3
Compare
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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
internal/handshake/protocol/peer.go (1)
242-265: GetBlock flow is consistent; consider future-proofing for interleaved messagesThis mirrors
GetHeaders/GetProofnicely: you build a singleInvTypeBlockentry, sendMessageGetData, then expect a*MsgBlockand return itsBlock. The implementation looks correct as long as the peer responds directly with ablockmessage.If, in future, peers can legally send other (supported) messages before the
blockresponse, you may want to loop onreceiveMessage()until you see a*MsgBlockor hit a timeout, instead of failing on the first non‑MsgBlockmessage.internal/handshake/protocol/messages.go (1)
460-476: MsgBlock.Decode looks fine; consider guarding the Encode stub
MsgBlock.Decodedelegating tohandshake.NewBlockFromReader(bytes.NewReader(data))is straightforward and matches the rest of the API.
MsgBlock.Encodecurrently returns an empty slice with a comment explaining that block encoding isn’t implemented. That’s okay as long asMsgBlockis only ever used on the receive side, but if a caller accidentally tries to send aMessageBlock, it will silently produce an invalid zero‑length payload.Optional: change
Encodeto fail fast (e.g.,panic("MsgBlock.Encode not implemented")or log/metrics) so misuse is obvious during development.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
internal/handshake/protocol/messages.go(5 hunks)internal/handshake/protocol/messages_test.go(1 hunks)internal/handshake/protocol/peer.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
internal/handshake/protocol/peer.go (2)
internal/handshake/block.go (1)
Block(19-22)internal/handshake/protocol/messages.go (5)
MsgGetData(353-355)InvItem(534-537)InvTypeBlock(527-527)MessageGetData(28-28)MsgBlock(460-462)
internal/handshake/protocol/messages.go (1)
internal/handshake/block.go (2)
Block(19-22)NewBlockFromReader(24-37)
internal/handshake/protocol/messages_test.go (1)
internal/handshake/protocol/messages.go (4)
Message(51-54)MsgGetData(353-355)InvItem(534-537)InvTypeBlock(527-527)
🔇 Additional comments (2)
internal/handshake/protocol/messages.go (2)
20-35: New GetData/Block message wiring looks consistent; verify IDs against Handshake specAdding
MessageGetDataandMessageBlockand wiring them intodecodeMessagevia&MsgGetData{}/&MsgBlock{}follows the existing pattern for other message types and looks correct from a code perspective.Because these numeric IDs are part of the on‑wire Handshake protocol, it’s worth double‑checking them against the upstream spec/hsd implementation to avoid subtle interoperability issues.
Also applies to: 73-103
525-553: Fix InvItem.Decode hash assignment; avoid invalid slice-to-array conversion
InvItem.Decodevalidates the 36‑byte length correctly, but the hash assignment uses the same invalid pattern as in the tests:i.Hash = [32]byte(data[4:])Converting a
[]byteto a[32]bytelike this is not allowed in Go and will fail to compile. Use an explicit copy into the fixed-size array:func (i *InvItem) Decode(data []byte) error { if len(data) != 36 { return errors.New("invalid payload length") } - i.Type = binary.LittleEndian.Uint32(data[0:4]) - i.Hash = [32]byte(data[4:]) + i.Type = binary.LittleEndian.Uint32(data[0:4]) + copy(i.Hash[:], data[4:]) return nil }Same pattern appears in other decode paths in this file; it may be worth standardizing on a helper for 32‑byte hash copies.
⛔ Skipped due to learnings
Learnt from: agaffney Repo: blinklabs-io/cdnsd PR: 424 File: internal/handshake/protocol/messages_test.go:132-136 Timestamp: 2025-11-12T22:02:04.720Z Learning: In Go 1.20 and later, slices can be converted directly to arrays using the syntax `[N]T(slice)` where N is the array size and T is the element type. For example, `[32]byte(mySlice)` is valid and will copy the slice elements into a new array. This conversion panics if the slice length is less than the array size.
Summary by CodeRabbit
New Features
Tests