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

evm: Remove dead transceiver code #18

Open
wants to merge 1 commit into
base: ntt-with-mm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions evm/echidna/FuzzNttManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -405,19 +405,6 @@ contract FuzzNttManager is FuzzingHelpers {
errorSelector == selectorToUint(IManagerBase.PeerNotRegistered.selector),
"NttManager: transfer expected to fail if sending to an unset peer"
);
} else if (
errorSelector == selectorToUint(TransceiverStructs.InvalidInstructionIndex.selector)
) {
TransceiverStructs.TransceiverInstruction[] memory instructions = TransceiverStructs
.parseTransceiverInstructions(encodedInstructions, numRegisteredTransceivers);
for (uint256 i = 0; i < instructions.length; ++i) {
if (instructions[i].index < numRegisteredTransceivers) {
assertWithMsg(
false,
"NttManager: transfer should not fail if instruction index is in bounds"
);
}
}
} else {
assertWithMsg(false, "NttManager: transfer unexpected revert");
}
Expand Down Expand Up @@ -481,21 +468,6 @@ contract FuzzNttManager is FuzzingHelpers {
== selectorToUint(IRateLimiter.OutboundQueuedTransferStillQueued.selector),
"NttManager: completeOutboundQueuedTransfer expected to fail if not queued for long enough"
);
} else if (
errorSelector == selectorToUint(TransceiverStructs.InvalidInstructionIndex.selector)
) {
TransceiverStructs.TransceiverInstruction[] memory instructions = TransceiverStructs
.parseTransceiverInstructions(
queuedTransfer.transceiverInstructions, numRegisteredTransceivers
);
for (uint256 i = 0; i < instructions.length; ++i) {
if (instructions[i].index < numRegisteredTransceivers) {
assertWithMsg(
false,
"NttManager: transfer should not fail if instruction index is in bounds"
);
}
}
} else if (numEnabledTransceivers == 0) {
// In this case the sender should be able to cancel their outbound queued transfer
try nttManager.cancelOutboundQueuedTransfer(messageSequence) {
Expand Down
294 changes: 0 additions & 294 deletions evm/src/libraries/TransceiverStructs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ library TransceiverStructs {
/// @param prefix The prefix that was found in the encoded message.
error IncorrectPrefix(bytes4 prefix);

/// @notice Error thrown when the transceiver instructions aren't
/// encoded with strictly increasing indices
/// @dev Selector 0x0555a4b9.
/// @param lastIndex Last parsed instruction index
/// @param instructionIndex The instruction index that was unordered
error UnorderedInstructions(uint256 lastIndex, uint256 instructionIndex);

/// @notice Error thrown when a transceiver instruction index
/// is greater than the number of registered transceivers
/// @dev We index from 0 so if providedIndex == numTransceivers then we're out-of-bounds too
/// @dev Selector 0x689f5016.
/// @param providedIndex The index specified in the instruction
/// @param numTransceivers The number of registered transceivers
error InvalidInstructionIndex(uint256 providedIndex, uint256 numTransceivers);

/// @dev Prefix for all NativeTokenTransfer payloads
/// This is 0x99'N''T''T'
bytes4 constant NTT_PREFIX = 0x994E5454;
Expand Down Expand Up @@ -184,283 +169,4 @@ library TransceiverStructs {
}
encoded.checkLength(offset);
}

/// @dev Message emitted by Transceiver implementations.
/// Each message includes an Transceiver-specified 4-byte prefix.
/// The wire format is as follows:
/// - prefix - 4 bytes
/// - sourceNttManagerAddress - 32 bytes
/// - recipientNttManagerAddress - 32 bytes
/// - nttManagerPayloadLength - 2 bytes
/// - nttManagerPayload - `nttManagerPayloadLength` bytes
/// - transceiverPayloadLength - 2 bytes
/// - transceiverPayload - `transceiverPayloadLength` bytes
struct TransceiverMessage {
/// @notice Address of the NttManager contract that emitted this message.
bytes32 sourceNttManagerAddress;
/// @notice Address of the NttManager contract that receives this message.
bytes32 recipientNttManagerAddress;
/// @notice Payload provided to the Transceiver contract by the NttManager contract.
bytes nttManagerPayload;
/// @notice Optional payload that the transceiver can encode and use for its own message passing purposes.
bytes transceiverPayload;
}

// @notice Encodes an Transceiver message for communication between the
// NttManager and the Transceiver.
// @param m The TransceiverMessage struct containing the message details.
// @return encoded The byte array corresponding to the encoded message.
// @custom:throw PayloadTooLong if the length of transceiverId, nttManagerPayload,
// or transceiverPayload exceeds the allowed maximum.
function encodeTransceiverMessage(
bytes4 prefix,
TransceiverMessage memory m
) public pure returns (bytes memory encoded) {
if (m.nttManagerPayload.length > type(uint16).max) {
revert PayloadTooLong(m.nttManagerPayload.length);
}
uint16 nttManagerPayloadLength = uint16(m.nttManagerPayload.length);

if (m.transceiverPayload.length > type(uint16).max) {
revert PayloadTooLong(m.transceiverPayload.length);
}
uint16 transceiverPayloadLength = uint16(m.transceiverPayload.length);

return abi.encodePacked(
prefix,
m.sourceNttManagerAddress,
m.recipientNttManagerAddress,
nttManagerPayloadLength,
m.nttManagerPayload,
transceiverPayloadLength,
m.transceiverPayload
);
}

function buildAndEncodeTransceiverMessage(
bytes4 prefix,
bytes32 sourceNttManagerAddress,
bytes32 recipientNttManagerAddress,
bytes memory nttManagerMessage,
bytes memory transceiverPayload
) public pure returns (TransceiverMessage memory, bytes memory) {
TransceiverMessage memory transceiverMessage = TransceiverMessage({
sourceNttManagerAddress: sourceNttManagerAddress,
recipientNttManagerAddress: recipientNttManagerAddress,
nttManagerPayload: nttManagerMessage,
transceiverPayload: transceiverPayload
});
bytes memory encoded = encodeTransceiverMessage(prefix, transceiverMessage);
return (transceiverMessage, encoded);
}

/// @dev Parses an encoded message and extracts information into an TransceiverMessage struct.
/// @param encoded The encoded bytes containing information about the TransceiverMessage.
/// @return transceiverMessage The parsed TransceiverMessage struct.
/// @custom:throw IncorrectPrefix if the prefix of the encoded message does not
/// match the expected prefix.
function parseTransceiverMessage(
bytes4 expectedPrefix,
bytes memory encoded
) internal pure returns (TransceiverMessage memory transceiverMessage) {
uint256 offset = 0;
bytes4 prefix;

(prefix, offset) = encoded.asBytes4Unchecked(offset);

if (prefix != expectedPrefix) {
revert IncorrectPrefix(prefix);
}

(transceiverMessage.sourceNttManagerAddress, offset) = encoded.asBytes32Unchecked(offset);
(transceiverMessage.recipientNttManagerAddress, offset) = encoded.asBytes32Unchecked(offset);
uint16 nttManagerPayloadLength;
(nttManagerPayloadLength, offset) = encoded.asUint16Unchecked(offset);
(transceiverMessage.nttManagerPayload, offset) =
encoded.sliceUnchecked(offset, nttManagerPayloadLength);
uint16 transceiverPayloadLength;
(transceiverPayloadLength, offset) = encoded.asUint16Unchecked(offset);
(transceiverMessage.transceiverPayload, offset) =
encoded.sliceUnchecked(offset, transceiverPayloadLength);

// Check if the entire byte array has been processed
encoded.checkLength(offset);
}

/// @dev Parses the payload of an Transceiver message and returns
/// the parsed NttManagerMessage struct.
/// @param expectedPrefix The prefix that should be encoded in the nttManager message.
/// @param payload The payload sent across the wire.
function parseTransceiverAndNttManagerMessage(
bytes4 expectedPrefix,
bytes memory payload
) public pure returns (TransceiverMessage memory, NttManagerMessage memory) {
// parse the encoded message payload from the Transceiver
TransceiverMessage memory parsedTransceiverMessage =
parseTransceiverMessage(expectedPrefix, payload);

// parse the encoded message payload from the NttManager
NttManagerMessage memory parsedNttManagerMessage =
parseNttManagerMessage(parsedTransceiverMessage.nttManagerPayload);

return (parsedTransceiverMessage, parsedNttManagerMessage);
}

/// @dev Variable-length transceiver-specific instruction that can be passed by the caller to the nttManager.
/// The index field refers to the index of the registeredTransceiver that this instruction should be passed to.
/// The serialization format is:
/// - index - 1 byte
/// - payloadLength - 1 byte
/// - payload - `payloadLength` bytes
struct TransceiverInstruction {
uint8 index;
bytes payload;
}

function encodeTransceiverInstruction(
TransceiverInstruction memory instruction
) public pure returns (bytes memory) {
if (instruction.payload.length > type(uint8).max) {
revert PayloadTooLong(instruction.payload.length);
}
uint8 payloadLength = uint8(instruction.payload.length);
return abi.encodePacked(instruction.index, payloadLength, instruction.payload);
}

function parseTransceiverInstructionUnchecked(
bytes memory encoded,
uint256 offset
) public pure returns (TransceiverInstruction memory instruction, uint256 nextOffset) {
(instruction.index, nextOffset) = encoded.asUint8Unchecked(offset);
uint8 instructionLength;
(instructionLength, nextOffset) = encoded.asUint8Unchecked(nextOffset);
(instruction.payload, nextOffset) = encoded.sliceUnchecked(nextOffset, instructionLength);
}

function parseTransceiverInstructionChecked(
bytes memory encoded
) public pure returns (TransceiverInstruction memory instruction) {
uint256 offset = 0;
(instruction, offset) = parseTransceiverInstructionUnchecked(encoded, offset);
encoded.checkLength(offset);
}

/// @dev Encode an array of multiple variable-length transceiver-specific instructions.
/// The serialization format is:
/// - instructionsLength - 1 byte
/// - `instructionsLength` number of serialized `TransceiverInstruction` types.
function encodeTransceiverInstructions(
TransceiverInstruction[] memory instructions
) public pure returns (bytes memory) {
if (instructions.length > type(uint8).max) {
revert PayloadTooLong(instructions.length);
}
uint256 instructionsLength = instructions.length;

bytes memory encoded;
for (uint256 i = 0; i < instructionsLength; i++) {
bytes memory innerEncoded = encodeTransceiverInstruction(instructions[i]);
encoded = bytes.concat(encoded, innerEncoded);
}
return abi.encodePacked(uint8(instructionsLength), encoded);
}

function parseTransceiverInstructions(
bytes memory encoded,
uint256 numRegisteredTransceivers
) public pure returns (TransceiverInstruction[] memory) {
uint256 offset = 0;
uint256 instructionsLength;
(instructionsLength, offset) = encoded.asUint8Unchecked(offset);

// We allocate an array with the length of the number of registered transceivers
// This gives us the flexibility to not have to pass instructions for transceivers that
// don't need them
TransceiverInstruction[] memory instructions =
new TransceiverInstruction[](numRegisteredTransceivers);

uint256 lastIndex = 0;
for (uint256 i = 0; i < instructionsLength; i++) {
TransceiverInstruction memory instruction;
(instruction, offset) = parseTransceiverInstructionUnchecked(encoded, offset);

uint8 instructionIndex = instruction.index;

// The instructions passed in have to be strictly increasing in terms of transceiver index
if (i != 0 && instructionIndex <= lastIndex) {
revert UnorderedInstructions(lastIndex, instructionIndex);
}

// Instruction index is out of bounds
if (instructionIndex >= numRegisteredTransceivers) {
revert InvalidInstructionIndex(instructionIndex, numRegisteredTransceivers);
}

lastIndex = instructionIndex;

instructions[instructionIndex] = instruction;
}

encoded.checkLength(offset);

return instructions;
}

struct TransceiverInit {
bytes4 transceiverIdentifier;
bytes32 nttManagerAddress;
uint8 nttManagerMode;
bytes32 tokenAddress;
uint8 tokenDecimals;
}

function encodeTransceiverInit(
TransceiverInit memory init
) public pure returns (bytes memory) {
return abi.encodePacked(
init.transceiverIdentifier,
init.nttManagerAddress,
init.nttManagerMode,
init.tokenAddress,
init.tokenDecimals
);
}

function decodeTransceiverInit(
bytes memory encoded
) public pure returns (TransceiverInit memory init) {
uint256 offset = 0;
(init.transceiverIdentifier, offset) = encoded.asBytes4Unchecked(offset);
(init.nttManagerAddress, offset) = encoded.asBytes32Unchecked(offset);
(init.nttManagerMode, offset) = encoded.asUint8Unchecked(offset);
(init.tokenAddress, offset) = encoded.asBytes32Unchecked(offset);
(init.tokenDecimals, offset) = encoded.asUint8Unchecked(offset);
encoded.checkLength(offset);
}

struct TransceiverRegistration {
bytes4 transceiverIdentifier;
uint16 transceiverChainId;
bytes32 transceiverAddress;
}

function encodeTransceiverRegistration(
TransceiverRegistration memory registration
) public pure returns (bytes memory) {
return abi.encodePacked(
registration.transceiverIdentifier,
registration.transceiverChainId,
registration.transceiverAddress
);
}

function decodeTransceiverRegistration(
bytes memory encoded
) public pure returns (TransceiverRegistration memory registration) {
uint256 offset = 0;
(registration.transceiverIdentifier, offset) = encoded.asBytes4Unchecked(offset);
(registration.transceiverChainId, offset) = encoded.asUint16Unchecked(offset);
(registration.transceiverAddress, offset) = encoded.asBytes32Unchecked(offset);
encoded.checkLength(offset);
}
}
16 changes: 0 additions & 16 deletions evm/test/RateLimit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1106,22 +1106,6 @@ contract TestRateLimit is Test, IRateLimiterEvents {
// bytes memory encodedEm;
uint256 inboundLimit = inboundLimitAmt;
TrimmedAmount trimmedAmount = packTrimmedAmount(uint64(amount), 8);
// {
// TransceiverStructs.TransceiverMessage memory em;
// (m, em) = TransceiverHelpersLib.attestTransceiversHelper(
// user_B,
// 0,
// chainId,
// nttManager,
// nttManager,
// trimmedAmount,
// inboundLimit.trim(token.decimals(), token.decimals()),
// transceivers
// );
// encodedEm = TransceiverStructs.encodeTransceiverMessage(
// TransceiverHelpersLib.TEST_TRANSCEIVER_PAYLOAD_PREFIX, em
// );
// }

(
TransceiverStructs.NttManagerMessage memory m,
Expand Down
Loading
Loading