Skip to content

Commit

Permalink
fix: Prevent assertion skip on 0 expected value
Browse files Browse the repository at this point in the history
Expecting 0 value would make the conditional fail with a falsey value.
Adding additional else case to ensure the return value is undefined as
an extra check in case the assertions are not being run.

This correctly validates msg.value does indeed have the value passed to
callcode(), just not the same one as msg.value of the parent caller.
  • Loading branch information
drklee3 committed Oct 17, 2024
1 parent a346d11 commit c437a06
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions tests/e2e-evm/test/callcode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ describe("CallCode", () => {
}),
// msg.sender is the caller contract
wantSender: (ctx) => ctx.lowLevelCaller.address,
// msg.value is not preserved via callcode
wantValue: 0n,
// msg.value is still 100 since the value is passed to callcode()
wantValue: 100n,
},
{
name: "callcode with storage",
Expand Down Expand Up @@ -195,7 +195,7 @@ describe("CallCode", () => {
const res = await publicClient.call(txData);

// Check the return value for the msg.sender and msg.value if applicable
if (tc.wantSender && tc.wantValue) {
if (tc.wantSender) {
if (!res.data) {
// Fail this way as a type guard to ensure res.data is not undefined
expect.fail("no data returned");
Expand All @@ -213,6 +213,8 @@ describe("CallCode", () => {
// getAddress to ensure both are checksum encoded
expect(getAddress(returnAddress)).to.equal(getAddress(expectedSender), "unexpected msg.sender");
expect(returnValue).to.equal(tc.wantValue, "unexpected msg.value");
} else {
expect(res.data).to.be.undefined;
}

const txHash = await walletClient.sendTransaction(txData);
Expand All @@ -235,6 +237,7 @@ describe("CallCode", () => {
expect(storageValue).to.equal(expectedStorage, "unexpected storage value");
}

// Verify balance with the added value
const balance = await publicClient.getBalance({ address: txData.to });
const expectedBalance = startingBalance + (txData.value ?? 0n);
expect(balance).to.equal(expectedBalance, "unexpected balance");
Expand Down

0 comments on commit c437a06

Please sign in to comment.