Skip to content

Commit 7ce5400

Browse files
committed
Add comments to clarify the order of struct parsing
The 'wire format' of the Transceiver messages encodes/decodes the fields of the struct in reverse order to how they are declared in the Solidity and Rust structs. This is done safely, and we have unit tests to verify that the encoding/decoding process is consistent on both sides. However, it's not clear that this is intentional when reading through the code. This comment should help with readability and hopefully will stop reviewers from falling down rabbit holes.
1 parent 1470fc6 commit 7ce5400

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

evm/src/libraries/TransceiverStructs.sol

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ library TransceiverStructs {
101101
pure
102102
returns (bytes memory encoded)
103103
{
104+
// The `amount` and `decimals` fields are encoded in reverse order compared to how they are declared in the
105+
// `TrimmedAmount` struct. This is consistent with the Rust NTT implementation.
104106
TrimmedAmount memory transferAmount = m.amount;
105107
return abi.encodePacked(
106108
NTT_PREFIX,
@@ -127,6 +129,8 @@ library TransceiverStructs {
127129
revert IncorrectPrefix(prefix);
128130
}
129131

132+
// The `amount` and `decimals` fields are parsed in reverse order compared to how they are declared in the
133+
// `TrimmedAmount` struct. This is consistent with the Rust NTT implementation.
130134
uint8 numDecimals;
131135
(numDecimals, offset) = encoded.asUint8Unchecked(offset);
132136
uint64 amount;

solana/modules/ntt-messages/src/trimmed_amount.rs

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ impl Readable for TrimmedAmount {
9696
Self: Sized,
9797
R: io::Read,
9898
{
99+
// The fields of this struct are intentionally read in reverse order compared to how they are declared in the
100+
// `TrimmedAmount` struct. This is consistent with the equivalent code in the EVM NTT implementation.
99101
let decimals = Readable::read(reader)?;
100102
let amount = Readable::read(reader)?;
101103
Ok(Self { amount, decimals })
@@ -108,6 +110,8 @@ impl Writeable for TrimmedAmount {
108110
W: io::Write,
109111
{
110112
let TrimmedAmount { amount, decimals } = self;
113+
// The fields of this struct are intentionally written in reverse order compared to how they are declared in the
114+
// `TrimmedAmount` struct. This is consistent with the equivalent code in the EVM NTT implementation.
111115
decimals.write(writer)?;
112116
amount.write(writer)?;
113117

0 commit comments

Comments
 (0)