Skip to content

Commit

Permalink
fix: adds contract existence StaticHyVM (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou authored Aug 14, 2023
1 parent 143fdf9 commit 7791799
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/StaticHyVM/StaticHyVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ contract StaticHyVM {
/// @return data Return data from delegate call
function doDelegateCall(bytes calldata payload) public returns (bytes memory) {
if (msg.sender != address(this)) revert OnlySelf();
if (hyvm.code.length == 0) revert AddressNotContract(hyvm);

(bool success, bytes memory data) = hyvm.delegatecall(payload);
if (!success) _bubbleError(data, "StaticHyVM: delegatecall failed");
Expand Down
8 changes: 8 additions & 0 deletions test/StaticHyVM/StaticHyVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ contract TestStaticHyVM is Test {
staticHyvm.doDelegateCall("");
}

function testCannotStaticExecIfHyVMNotContract() public {
// Sets the bytecode of the hyvm to be empty
// It will emulate the case where the address is not a contract
vm.etch(hyvm, bytes(""));
vm.expectRevert(abi.encodeWithSelector(StaticHyVM.AddressNotContract.selector, hyvm));
staticHyvm.staticExec(bytes(""));
}

function testCannotStaticExecWriteFunction() public {
bytecode = Utils.replaceSelectorBypassCalldataSizeCheck(bytecode, hex"c350518d");

Expand Down

0 comments on commit 7791799

Please sign in to comment.