-
Notifications
You must be signed in to change notification settings - Fork 19
bolt: implement tx_add_output message codec #26
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
Open
harsh04044
wants to merge
1
commit into
morehouse:master
Choose a base branch
from
harsh04044:bolt/tx-add-output
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| //! BOLT 2 `tx_add_output` message. | ||
|
|
||
| use super::BoltError; | ||
| use super::types::ChannelId; | ||
| use super::wire::WireFormat; | ||
|
|
||
| /// BOLT 2 `tx_add_output` message (type 67). | ||
| /// | ||
| /// Sent during interactive transaction construction to propose adding an | ||
| /// output to the shared transaction. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct TxAddOutput { | ||
| /// The channel this message pertains to | ||
| pub channel_id: ChannelId, | ||
| /// Serial ID for this output, must be even if sent by the initiator, | ||
| /// odd if sent by the non-initiator (BOLT 2 parity rule) | ||
| pub serial_id: u64, | ||
| /// The value of this output in satoshis | ||
| pub sats: u64, | ||
| /// The scriptPubKey for this output | ||
| pub script: Vec<u8>, | ||
| } | ||
|
|
||
| impl TxAddOutput { | ||
| /// Encodes to wire format (without message type prefix). | ||
| #[must_use] | ||
| pub fn encode(&self) -> Vec<u8> { | ||
| let mut out = Vec::new(); | ||
| self.channel_id.write(&mut out); | ||
| self.serial_id.write(&mut out); | ||
| self.sats.write(&mut out); | ||
| self.script.write(&mut out); | ||
| out | ||
| } | ||
|
|
||
| /// Decodes from wire format (without message type prefix). | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns `Truncated` if the payload is too short. | ||
| pub fn decode(payload: &[u8]) -> Result<Self, BoltError> { | ||
| let mut cursor = payload; | ||
| let channel_id = WireFormat::read(&mut cursor)?; | ||
| let serial_id = WireFormat::read(&mut cursor)?; | ||
| let sats = WireFormat::read(&mut cursor)?; | ||
| let script: Vec<u8> = WireFormat::read(&mut cursor)?; | ||
| Ok(Self { | ||
| channel_id, | ||
| serial_id, | ||
| sats, | ||
| script, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::super::CHANNEL_ID_SIZE; | ||
| use super::*; | ||
|
|
||
| fn sample_msg() -> TxAddOutput { | ||
| TxAddOutput { | ||
| channel_id: ChannelId::new([0xab; CHANNEL_ID_SIZE]), | ||
| serial_id: 42, | ||
| sats: 100_000, | ||
| script: vec![0x76, 0xa9, 0x14, 0xab, 0xcd], | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn roundtrip() { | ||
| let original = sample_msg(); | ||
| let encoded = original.encode(); | ||
| let decoded = TxAddOutput::decode(&encoded).unwrap(); | ||
| assert_eq!(original, decoded); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_ignores_trailing_bytes() { | ||
| let original = sample_msg(); | ||
| let mut encoded = original.encode(); | ||
| encoded.extend_from_slice(&[0xaa, 0xbb, 0xcc]); | ||
| let decoded = TxAddOutput::decode(&encoded).unwrap(); | ||
| assert_eq!(decoded, original); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_truncated_channel_id() { | ||
| assert_eq!( | ||
| TxAddOutput::decode(&[0x00; 20]), | ||
| Err(BoltError::Truncated { | ||
| expected: CHANNEL_ID_SIZE, | ||
| actual: 20 | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_truncated_serial_id() { | ||
| // channel_id (32 bytes) + 4 bytes of serial_id | ||
| assert_eq!( | ||
| TxAddOutput::decode(&[0x00; CHANNEL_ID_SIZE + 4]), | ||
| Err(BoltError::Truncated { | ||
| expected: 8, | ||
| actual: 4 | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_truncated_sats() { | ||
| // channel_id (32) + serial_id (8) + 4 bytes of sats | ||
| assert_eq!( | ||
| TxAddOutput::decode(&[0x00; CHANNEL_ID_SIZE + 8 + 4]), | ||
| Err(BoltError::Truncated { | ||
| expected: 8, | ||
| actual: 4 | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
|
morehouse marked this conversation as resolved.
|
||
| #[test] | ||
| fn decode_truncated_script_len() { | ||
|
morehouse marked this conversation as resolved.
|
||
| // channel_id (32) + serial_id (8) + sats (8) + only 1 byte of the 2-byte script length | ||
| let mut payload = vec![0x00u8; CHANNEL_ID_SIZE + 8 + 8]; | ||
| payload.push(0x00); // only 1 byte of the 2-byte script length field | ||
| assert_eq!( | ||
| TxAddOutput::decode(&payload), | ||
| Err(BoltError::Truncated { | ||
| expected: 2, | ||
| actual: 1 | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_truncated_script_data() { | ||
| // channel_id (32) + serial_id (8) + sats (8) + script_len=10 (2 bytes) + only 3 bytes of data | ||
| let mut payload = vec![0x00u8; CHANNEL_ID_SIZE + 8 + 8]; | ||
| payload.push(0x00); // script_len high byte | ||
| payload.push(0x0a); // script_len low byte = 10 | ||
| payload.extend_from_slice(&[0xde, 0xad, 0xbe]); // only 3 bytes instead of 10 | ||
| assert_eq!( | ||
| TxAddOutput::decode(&payload), | ||
| Err(BoltError::Truncated { | ||
| expected: 10, | ||
| actual: 3 | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_empty() { | ||
| assert_eq!( | ||
| TxAddOutput::decode(&[]), | ||
| Err(BoltError::Truncated { | ||
| expected: CHANNEL_ID_SIZE, | ||
| actual: 0 | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn roundtrip_empty_script() { | ||
| let msg = TxAddOutput { | ||
| script: vec![], | ||
| ..sample_msg() | ||
| }; | ||
| let encoded = msg.encode(); | ||
| let decoded = TxAddOutput::decode(&encoded).unwrap(); | ||
| assert_eq!(decoded, msg); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.