Skip to content

Commit baa0850

Browse files
authored
Rename tbr vaa util function (#86)
* Rename tbr vaa util function * Add unchecked math
1 parent 198b107 commit baa0850

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/libraries/VaaLib.sol

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,23 +436,28 @@ library VaaLib {
436436
) = decodeVaaBodyMemUnchecked(encodedVaa, 0, encodedVaa.length);
437437
}
438438

439-
function decodeEmitterChainIdCdUnchecked(
439+
// Convinience decoding function for token bridge Vaas
440+
function decodeEmitterChainAndPayloadCdUnchecked(
440441
bytes calldata encodedVaa
441-
) internal pure returns (uint16 emitterChainId) {
442+
) internal pure returns (uint16 emitterChainId, bytes calldata payload) { unchecked {
442443
checkVaaVersionCd(encodedVaa);
443444
uint envelopeOffset = skipVaaHeaderCd(encodedVaa);
444445
uint offset = envelopeOffset + ENVELOPE_EMITTER_CHAIN_ID_OFFSET;
445-
(emitterChainId, ) = encodedVaa.asUint16CdUnchecked(offset);
446-
}
446+
(emitterChainId, offset) = encodedVaa.asUint16CdUnchecked(offset);
447+
offset += ENVELOPE_EMITTER_ADDRESS_SIZE + ENVELOPE_SEQUENCE_SIZE + ENVELOPE_CONSISTENCY_LEVEL_SIZE;
448+
payload = decodeVaaPayloadCd(encodedVaa, offset);
449+
}}
447450

448-
function decodeEmitterChainIdMemUnchecked(
451+
function decodeEmitterChainAndPayloadMemUnchecked(
449452
bytes memory encodedVaa
450-
) internal pure returns (uint16 emitterChainId) {
453+
) internal pure returns (uint16 emitterChainId, bytes memory payload) { unchecked {
451454
checkVaaVersionMemUnchecked(encodedVaa, 0);
452455
uint envelopeOffset = skipVaaHeaderMemUnchecked(encodedVaa, 0);
453456
uint offset = envelopeOffset + ENVELOPE_EMITTER_CHAIN_ID_OFFSET;
454-
(emitterChainId, ) = encodedVaa.asUint16MemUnchecked(offset);
455-
}
457+
(emitterChainId, offset) = encodedVaa.asUint16MemUnchecked(offset);
458+
offset += ENVELOPE_EMITTER_ADDRESS_SIZE + ENVELOPE_SEQUENCE_SIZE + ENVELOPE_CONSISTENCY_LEVEL_SIZE;
459+
(payload, ) = decodeVaaPayloadMemUnchecked(encodedVaa, offset, encodedVaa.length);
460+
}}
456461

457462
// ------------ Advanced Decoding Functions ------------
458463

@@ -868,11 +873,11 @@ library VaaLib {
868873
bytes memory encoded,
869874
uint payloadOffset,
870875
uint vaaLength
871-
) internal pure returns (bytes memory payload, uint newOffset) {
876+
) internal pure returns (bytes memory payload, uint newOffset) { unchecked {
872877
//check to avoid underflow in following subtraction
873878
payloadOffset.checkBound(vaaLength);
874879
(payload, newOffset) = encoded.sliceMemUnchecked(payloadOffset, vaaLength - payloadOffset);
875-
}
880+
}}
876881

877882
// ------------ Encoding ------------
878883

@@ -1013,11 +1018,11 @@ library VaaLib {
10131018
function _decodeRemainderCd(
10141019
bytes calldata encodedVaa,
10151020
uint offset
1016-
) private pure returns (bytes calldata remainder) {
1021+
) private pure returns (bytes calldata remainder) { unchecked {
10171022
//check to avoid underflow in following subtraction
10181023
offset.checkBound(encodedVaa.length);
10191024
(remainder, ) = encodedVaa.sliceCdUnchecked(offset, encodedVaa.length - offset);
1020-
}
1025+
}}
10211026
}
10221027

10231028
using VaaLib for VaaHeader global;

test/WormholeMessages.t.sol

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,16 @@ contract WormholeMessagesTest is Test {
593593
}
594594
function testDecodingAmPayload() public { runBoth(decodingAmPayload); }
595595

596-
function decodingEmitterChainId(bool cd) internal {
597-
uint16 emitterChainId = abi.decode(
598-
callWithBytes(vaaLibWrapper, "decodeEmitterChainId", cd, true, amVaa(), true),
599-
(uint16)
596+
function decodingEmitterChainAndPayload(bool cd) internal {
597+
uint16 emitterChainId;
598+
bytes memory payload;
599+
(emitterChainId, payload) = abi.decode(
600+
callWithBytes(vaaLibWrapper, "decodeEmitterChainAndPayload", cd, true, amVaa(), true),
601+
(uint16, bytes)
600602
);
601603

602604
assertEq(emitterChainId, amVaaVm().emitterChainId);
605+
assertEq(payload, amVaaVm().payload);
603606
}
604-
function testDecodingEmitterChainId() public { runBoth(decodingEmitterChainId); }
607+
function testDecodingEmitterChainAndPayload() public { runBoth(decodingEmitterChainAndPayload); }
605608
}

test/generated/VaaLibTestWrapper.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ contract VaaLibTestWrapper {
102102
return VaaLib.decodeVaaBodyStructMem(encodedVaa);
103103
}
104104

105-
function decodeEmitterChainIdCdUnchecked(bytes calldata encodedVaa) external pure returns (uint16 emitterChainId) {
106-
return VaaLib.decodeEmitterChainIdCdUnchecked(encodedVaa);
105+
function decodeEmitterChainAndPayloadCdUnchecked(bytes calldata encodedVaa) external pure returns (uint16 emitterChainId, bytes calldata payload) {
106+
return VaaLib.decodeEmitterChainAndPayloadCdUnchecked(encodedVaa);
107107
}
108108

109-
function decodeEmitterChainIdMemUnchecked(bytes calldata encodedVaa) external pure returns (uint16 emitterChainId) {
110-
return VaaLib.decodeEmitterChainIdMemUnchecked(encodedVaa);
109+
function decodeEmitterChainAndPayloadMemUnchecked(bytes calldata encodedVaa) external pure returns (uint16 emitterChainId, bytes memory payload) {
110+
return VaaLib.decodeEmitterChainAndPayloadMemUnchecked(encodedVaa);
111111
}
112112

113113
function asIWormholeSignatures(GuardianSignature[] calldata signatures) external pure returns (IWormhole.Signature[] memory vmSignatures) {

0 commit comments

Comments
 (0)