Skip to content

Commit

Permalink
moar unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Danno Ferrin <danno@numisight.com>
  • Loading branch information
shemnon committed Aug 30, 2024
1 parent 689dfc9 commit f19e242
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
/** The Call operation. */
public class ExtCallOperation extends AbstractExtCallOperation {

/** The constant OPCODE. */
public static final int OPCODE = 0xF8;

static final int STACK_VALUE = 3;

/**
Expand All @@ -32,7 +35,7 @@ public class ExtCallOperation extends AbstractExtCallOperation {
* @param gasCalculator the gas calculator
*/
public ExtCallOperation(final GasCalculator gasCalculator) {
super(0xF8, "EXTCALL", 4, 1, gasCalculator);
super(OPCODE, "EXTCALL", 4, 1, gasCalculator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
/** The Delegate call operation. */
public class ExtDelegateCallOperation extends AbstractExtCallOperation {

/** The constant OPCODE. */
public static final int OPCODE = 0xF9;

/**
* Instantiates a new Delegate call operation.
*
* @param gasCalculator the gas calculator
*/
public ExtDelegateCallOperation(final GasCalculator gasCalculator) {
super(0xF9, "EXTDELEGATECALL", 3, 1, gasCalculator);
super(OPCODE, "EXTDELEGATECALL", 3, 1, gasCalculator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
/** The Static call operation. */
public class ExtStaticCallOperation extends AbstractExtCallOperation {

/** The constant OPCODE. */
public static final int OPCODE = 0xFB;

/**
* Instantiates a new Static call operation.
*
* @param gasCalculator the gas calculator
*/
public ExtStaticCallOperation(final GasCalculator gasCalculator) {
super(0xFB, "EXTSTATICCALL", 3, 1, gasCalculator);
super(OPCODE, "EXTSTATICCALL", 3, 1, gasCalculator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ExtCallOperationTest {
private final WorldUpdater worldUpdater = mock(WorldUpdater.class);
private final MutableAccount account = mock(MutableAccount.class);
private static final EVM EOF_EVM = MainnetEVMs.pragueEOF(EvmConfiguration.DEFAULT);
public static final Code LEGACY_CODE =
EOF_EVM.getCodeUncached(Bytes.of(ExtCallOperation.OPCODE, 1));
public static final Code SIMPLE_EOF =
EOF_EVM.getCodeUncached(Bytes.fromHexString("0xEF00010100040200010001040000000080000000"));
public static final Code INVALID_EOF =
Expand Down Expand Up @@ -276,4 +278,33 @@ void overflowTest() {
assertThat(parentFrame.getStackItem(0))
.isEqualTo(AbstractExtCallOperation.EOF1_EXCEPTION_STACK_ITEM);
}

@Test
void legacyTest() {
final ExtCallOperation operation = new ExtCallOperation(new PragueEOFGasCalculator());

final var messageFrame =
new TestMessageFrameBuilder()
.initialGas(400000)
.code(LEGACY_CODE)
.pushStackItem(CONTRACT_ADDRESS) // canary for non-returning
.pushStackItem(Bytes.EMPTY)
.pushStackItem(Bytes.EMPTY)
.pushStackItem(Bytes.EMPTY)
.pushStackItem(CONTRACT_ADDRESS)
.worldUpdater(worldUpdater)
.build();
messageFrame.warmUpAddress(CONTRACT_ADDRESS);
when(account.getBalance()).thenReturn(Wei.ZERO);
when(account.getCodeHash()).thenReturn(SIMPLE_EOF.getCodeHash());
when(account.getCode()).thenReturn(SIMPLE_EOF.getBytes());
when(worldUpdater.get(any())).thenReturn(account);
when(worldUpdater.getAccount(any())).thenReturn(account);
when(worldUpdater.updater()).thenReturn(worldUpdater);

var result = operation.execute(messageFrame, EOF_EVM);

assertThat(result.getGasCost()).isZero();
assertThat(result.getHaltReason()).isEqualTo(ExceptionalHaltReason.INVALID_OPERATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ExtDelegateCallOperationTest {
private final MutableAccount account = mock(MutableAccount.class);
// private final MutableAccount targetAccount = mock(MutableAccount.class);
private static final EVM EOF_EVM = MainnetEVMs.pragueEOF(EvmConfiguration.DEFAULT);
public static final Code LEGACY_CODE =
EOF_EVM.getCodeUncached(Bytes.of(ExtDelegateCallOperation.OPCODE, 1));
public static final Code SIMPLE_EOF =
EOF_EVM.getCodeUncached(Bytes.fromHexString("0xEF00010100040200010001040000000080000000"));
public static final Code SIMPLE_LEGACY = EOF_EVM.getCodeUncached(Bytes.fromHexString("0x00"));
Expand Down Expand Up @@ -259,4 +261,34 @@ void overflowTest() {
assertThat(parentFrame.getStackItem(0))
.isEqualTo(AbstractExtCallOperation.EOF1_EXCEPTION_STACK_ITEM);
}

@Test
void legacyTest() {
final ExtDelegateCallOperation operation =
new ExtDelegateCallOperation(new PragueEOFGasCalculator());

final var messageFrame =
new TestMessageFrameBuilder()
.initialGas(400000)
.code(LEGACY_CODE)
.pushStackItem(CONTRACT_ADDRESS) // canary for non-returning
.pushStackItem(Bytes.EMPTY)
.pushStackItem(Bytes.EMPTY)
.pushStackItem(Bytes.EMPTY)
.pushStackItem(CONTRACT_ADDRESS)
.worldUpdater(worldUpdater)
.build();
messageFrame.warmUpAddress(CONTRACT_ADDRESS);
when(account.getBalance()).thenReturn(Wei.ZERO);
when(account.getCodeHash()).thenReturn(SIMPLE_EOF.getCodeHash());
when(account.getCode()).thenReturn(SIMPLE_EOF.getBytes());
when(worldUpdater.get(any())).thenReturn(account);
when(worldUpdater.getAccount(any())).thenReturn(account);
when(worldUpdater.updater()).thenReturn(worldUpdater);

var result = operation.execute(messageFrame, EOF_EVM);

assertThat(result.getGasCost()).isZero();
assertThat(result.getHaltReason()).isEqualTo(ExceptionalHaltReason.INVALID_OPERATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ExtStaticCallOperationTest {
private final WorldUpdater worldUpdater = mock(WorldUpdater.class);
private final MutableAccount account = mock(MutableAccount.class);
private static final EVM EOF_EVM = MainnetEVMs.pragueEOF(EvmConfiguration.DEFAULT);
public static final Code LEGACY_CODE =
EOF_EVM.getCodeUncached(Bytes.of(ExtStaticCallOperation.OPCODE, 1));
public static final Code SIMPLE_EOF =
EOF_EVM.getCodeUncached(Bytes.fromHexString("0xEF00010100040200010001040000000080000000"));
public static final Code INVALID_EOF =
Expand Down Expand Up @@ -185,4 +187,34 @@ void overflowTest() {
assertThat(parentFrame.getStackItem(0))
.isEqualTo(AbstractExtCallOperation.EOF1_EXCEPTION_STACK_ITEM);
}

@Test
void legacyTest() {
final ExtStaticCallOperation operation =
new ExtStaticCallOperation(new PragueEOFGasCalculator());

final var messageFrame =
new TestMessageFrameBuilder()
.initialGas(400000)
.code(LEGACY_CODE)
.pushStackItem(CONTRACT_ADDRESS) // canary for non-returning
.pushStackItem(Bytes.EMPTY)
.pushStackItem(Bytes.EMPTY)
.pushStackItem(Bytes.EMPTY)
.pushStackItem(CONTRACT_ADDRESS)
.worldUpdater(worldUpdater)
.build();
messageFrame.warmUpAddress(CONTRACT_ADDRESS);
when(account.getBalance()).thenReturn(Wei.ZERO);
when(account.getCodeHash()).thenReturn(SIMPLE_EOF.getCodeHash());
when(account.getCode()).thenReturn(SIMPLE_EOF.getBytes());
when(worldUpdater.get(any())).thenReturn(account);
when(worldUpdater.getAccount(any())).thenReturn(account);
when(worldUpdater.updater()).thenReturn(worldUpdater);

var result = operation.execute(messageFrame, EOF_EVM);

assertThat(result.getGasCost()).isZero();
assertThat(result.getHaltReason()).isEqualTo(ExceptionalHaltReason.INVALID_OPERATION);
}
}

0 comments on commit f19e242

Please sign in to comment.