Skip to content

Commit

Permalink
chore: Cherry pick #10348 (#10368)
Browse files Browse the repository at this point in the history
Signed-off-by: lukelee-sl <luke.lee@swirldslabs.com>
  • Loading branch information
lukelee-sl authored Dec 7, 2023
1 parent 618c723 commit 2ccffcf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void trackNewAction(final MessageFrame messageFrame, final Consumer<Soli
&& messageFrame.getWorldUpdater().getAccount(contractAddress) == null) {
action.setTargetedAddress(contractAddress.toArray());
} else {
final var recipient = EntityId.fromAddress(asMirrorAddress(contractAddress, messageFrame));
final var recipient = getEntityIdOrNullByAddressAndMessageFrame(contractAddress, messageFrame, action);
if (CodeV0.EMPTY_CODE.equals(messageFrame.getCode())) {
// code can be empty when calling precompiles too, but we handle
// that in tracePrecompileCall, after precompile execution is completed
Expand Down Expand Up @@ -203,8 +203,10 @@ private void finalizeActionFor(final SolidityAction action, final MessageFrame f
action.setOutput(frame.getOutputData().toArrayUnsafe());
if (action.getInvalidSolidityAddress() != null) {
// we had a successful lazy create, replace targeted address with its new Hedera id
final var recipientAsHederaId = EntityId.fromAddress(
asMirrorAddress(Address.wrap(Bytes.of(action.getInvalidSolidityAddress())), frame));
// or set it to null if it's noop for non existing account
Address recipientAddress = Address.wrap(Bytes.of(action.getInvalidSolidityAddress()));
final var recipientAsHederaId =
getEntityIdOrNullByAddressAndMessageFrame(recipientAddress, frame, action);
action.setTargetedAddress(null);
action.setRecipientAccount(recipientAsHederaId);
}
Expand Down Expand Up @@ -363,4 +365,14 @@ private static <E, I> String get(
final E subject, final Function<E, I> getter, final Function<I, String> processor) {
return null != subject ? processor.compose(getter).apply(subject) : "null";
}

private EntityId getEntityIdOrNullByAddressAndMessageFrame(
Address address, MessageFrame frame, SolidityAction action) {
try {
return EntityId.fromAddress(asMirrorAddress(address, frame));
} catch (IllegalArgumentException e) {
action.setTargetedAddress(address.toArray());
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class HederaTracerTest {
private static final Bytes output = Bytes.wrap("output".getBytes(StandardCharsets.UTF_8));
private static final Address originator = Address.fromHexString("0x1");
private static final Address contract = Address.fromHexString("0x2");
private static final Address badContractAddress = Address.fromHexString("0x12345678901234567890");
private static final Address accountReceiver = Address.fromHexString("0x3");
private static final Address sender = Address.fromHexString("0x4");

Expand Down Expand Up @@ -322,6 +323,33 @@ void initializesActionsAsExpectedOnNewFrames() {
assertEquals(1, subject.getActions().get(2).getCallDepth());
}

@Test
void initializesActionsWhenBadRecipientAddress() {
Operation mockOperation = mock(Operation.class);

// mock out top level frame
final var topLevelMessageFrame = mock(MessageFrame.class);
given(topLevelMessageFrame.getCode()).willReturn(code);
given(topLevelMessageFrame.getType()).willReturn(Type.MESSAGE_CALL);
given(topLevelMessageFrame.getOriginatorAddress()).willReturn(originator);
given(topLevelMessageFrame.getContractAddress()).willReturn(badContractAddress);
given(topLevelMessageFrame.getRemainingGas()).willReturn(initialGas);
given(topLevelMessageFrame.getInputData()).willReturn(input);
given(topLevelMessageFrame.getValue()).willReturn(value);
given(topLevelMessageFrame.getWorldUpdater()).willReturn(worldUpdater);
given(worldUpdater.aliases()).willReturn(contractAliases);
given(contractAliases.resolveForEvm(originator)).willReturn(originator);
given(contractAliases.resolveForEvm(badContractAddress)).willReturn(badContractAddress);
given(worldUpdater.getAccount(badContractAddress)).willReturn(mock(MutableAccount.class));

// trace top level frame
subject.init(topLevelMessageFrame);
final var actions = subject.getActions();
final var solidityAction = actions.get(0);
assertThat(solidityAction.getRecipientAccount()).isNull();
assertThat(solidityAction.getInvalidSolidityAddress()).isEqualTo(badContractAddress.toArray());
}

@Test
void finalizesCodeSuccessfulCallMessageFrameAsExpected() {
// given
Expand Down

0 comments on commit 2ccffcf

Please sign in to comment.