Skip to content

Commit 9b9ad84

Browse files
committed
refactor: improve error handling and variable scope in _runTx function
1 parent 84839f0 commit 9b9ad84

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/core/TxAtomicSimple.sol

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,43 @@ abstract contract TxAtomicSimple is
126126
CoordinatorState.CoordinatorDecision.COORDINATOR_DECISION_UNKNOWN;
127127
bool prepareOK = false;
128128

129-
// slither-disable-next-line reentrancy-no-eth
130-
try module.onContractPrepare(
131-
CrossContext({txID: abi.encodePacked(txID), txIndex: TX_INDEX_COORDINATOR, signers: tx0.signers}),
132-
tx0.call_info
133-
) returns (bytes memory callResult) {
134-
// Verify return value if set
135-
if (tx0.return_value.value.length > 0) {
136-
if (keccak256(tx0.return_value.value) != keccak256(callResult)) {
137-
revert UnexpectedReturnValue();
129+
// Use a block scope to avoid "Stack Too Deep" error by limiting the lifetime of temporary variables
130+
{
131+
Height.Data memory emptyHeight = Height.Data(0, 0);
132+
Packet memory dummyPacket = Packet({
133+
sequence: 0,
134+
sourcePort: "",
135+
sourceChannel: "",
136+
destinationPort: "",
137+
destinationChannel: "",
138+
data: bytes(""),
139+
timeoutHeight: emptyHeight,
140+
timeoutTimestamp: 0
141+
});
142+
143+
IContractModule module = getModule(dummyPacket);
144+
if (address(module) == address(0)) {
145+
revert ModuleNotInitialized();
146+
}
147+
148+
// slither-disable-next-line reentrancy-no-eth
149+
try module.onContractPrepare(
150+
CrossContext({txID: abi.encodePacked(txID), txIndex: TX_INDEX_COORDINATOR, signers: tx0.signers}),
151+
tx0.call_info
152+
) returns (bytes memory callResult) {
153+
if (tx0.return_value.value.length > 0) {
154+
if (keccak256(tx0.return_value.value) != keccak256(callResult)) {
155+
revert UnexpectedReturnValue();
156+
}
138157
}
158+
prepareOK = true;
159+
phase = CoordinatorState.CoordinatorPhase.COORDINATOR_PHASE_PREPARE;
160+
decision = CoordinatorState.CoordinatorDecision.COORDINATOR_DECISION_UNKNOWN;
161+
} catch {
162+
prepareOK = false;
163+
phase = CoordinatorState.CoordinatorPhase.COORDINATOR_PHASE_COMMIT;
164+
decision = CoordinatorState.CoordinatorDecision.COORDINATOR_DECISION_ABORT;
139165
}
140-
prepareOK = true;
141-
phase = CoordinatorState.CoordinatorPhase.COORDINATOR_PHASE_PREPARE;
142-
decision = CoordinatorState.CoordinatorDecision.COORDINATOR_DECISION_UNKNOWN;
143-
} catch {
144-
prepareOK = false;
145-
phase = CoordinatorState.CoordinatorPhase.COORDINATOR_PHASE_COMMIT;
146-
decision = CoordinatorState.CoordinatorDecision.COORDINATOR_DECISION_ABORT;
147166
}
148167

149168
// --- 4. Send IBC Packet (only if prepareOK) ---

0 commit comments

Comments
 (0)