|
2 | 2 | package com.hedera.node.app.service.contract.impl.test.handlers; |
3 | 3 |
|
4 | 4 | import static com.hedera.hapi.node.base.HederaFunctionality.ETHEREUM_TRANSACTION; |
5 | | -import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_ETHEREUM_TRANSACTION; |
6 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.CALLED_CONTRACT_ID; |
7 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.DEFAULT_CONFIG; |
8 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.ETH_DATA_WITHOUT_TO_ADDRESS; |
9 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.ETH_DATA_WITH_TO_ADDRESS; |
10 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.HEVM_CREATION; |
11 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.SENDER_ID; |
12 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.SIGNER_NONCE; |
13 | | -import static com.hedera.node.app.service.contract.impl.test.TestHelpers.SUCCESS_RESULT_WITH_SIGNER_NONCE; |
| 5 | +import static com.hedera.hapi.node.base.ResponseCodeEnum.*; |
| 6 | +import static com.hedera.node.app.service.contract.impl.test.TestHelpers.*; |
14 | 7 | import static com.hedera.node.app.service.contract.impl.test.handlers.ContractCallHandlerTest.INTRINSIC_GAS_FOR_0_ARG_METHOD; |
15 | 8 | import static com.hedera.node.app.spi.fixtures.Assertions.assertThrowsPreCheck; |
16 | 9 | import static com.hedera.node.app.spi.workflows.HandleContext.DispatchMetadata.EMPTY_METADATA; |
17 | 10 | import static com.hedera.node.app.spi.workflows.HandleContext.DispatchMetadata.Type.ETHEREUM_NONCE_INCREMENT_CALLBACK; |
18 | | -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
19 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | +import static org.junit.jupiter.api.Assertions.*; |
20 | 12 | import static org.mockito.ArgumentMatchers.any; |
21 | 13 | import static org.mockito.ArgumentMatchers.notNull; |
22 | 14 | import static org.mockito.BDDMockito.given; |
@@ -407,33 +399,94 @@ void testCalculateFeesWithZeroHapiFeesConfigDisabled() { |
407 | 399 | } |
408 | 400 |
|
409 | 401 | @Test |
410 | | - void validatePureChecks() { |
| 402 | + void validatePureChecksHappyPath() { |
| 403 | + // check bad to evm address |
| 404 | + try (MockedStatic<EthTxData> ethTxData = Mockito.mockStatic(EthTxData.class)) { |
| 405 | + ethTxData.when(() -> EthTxData.populateEthTxData(any())).thenReturn(ethTxDataReturned); |
| 406 | + given(pureChecksContext.body()).willReturn(ethTxWithTx()); |
| 407 | + given(ethTxDataReturned.value()).willReturn(BigInteger.ONE); |
| 408 | + given(ethTxDataReturned.hasToAddress()).willReturn(true); |
| 409 | + final var toAddress = RECEIVER_ADDRESS.toByteArray(); |
| 410 | + given(ethTxDataReturned.to()).willReturn(toAddress); |
| 411 | + given(gasCalculator.transactionIntrinsicGasCost(org.apache.tuweni.bytes.Bytes.wrap(new byte[0]), false, 0L)) |
| 412 | + .willReturn(INTRINSIC_GAS_FOR_0_ARG_METHOD); |
| 413 | + given(ethTxDataReturned.gasLimit()).willReturn(INTRINSIC_GAS_FOR_0_ARG_METHOD); |
| 414 | + assertDoesNotThrow(() -> subject.pureChecks(pureChecksContext)); |
| 415 | + } |
| 416 | + } |
| 417 | + |
| 418 | + @Test |
| 419 | + void validatePureChecksCheckBadEthTxnBody() { |
411 | 420 | // check bad eth txn body |
412 | 421 | final var txn1 = ethTxWithNoTx(); |
413 | 422 | given(pureChecksContext.body()).willReturn(txn1); |
414 | 423 | assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 424 | + PreCheckException exception = |
| 425 | + assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 426 | + assertEquals(INVALID_ETHEREUM_TRANSACTION, exception.responseCode()); |
| 427 | + } |
| 428 | + |
| 429 | + @Test |
| 430 | + void validatePureChecksHbarsToBurnAddress() { |
| 431 | + // check bad to evm address |
| 432 | + try (MockedStatic<EthTxData> ethTxData = Mockito.mockStatic(EthTxData.class)) { |
| 433 | + ethTxData.when(() -> EthTxData.populateEthTxData(any())).thenReturn(ethTxDataReturned); |
| 434 | + given(pureChecksContext.body()).willReturn(ethTxWithTx()); |
| 435 | + given(ethTxDataReturned.value()).willReturn(BigInteger.ONE); |
| 436 | + given(ethTxDataReturned.to()).willReturn(new byte[20]); |
| 437 | + PreCheckException exception = |
| 438 | + assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 439 | + assertEquals(INVALID_SOLIDITY_ADDRESS, exception.responseCode()); |
| 440 | + } |
| 441 | + } |
415 | 442 |
|
| 443 | + @Test |
| 444 | + void validatePureChecksBadToEvmAddress() { |
416 | 445 | // check bad to evm address |
417 | 446 | try (MockedStatic<EthTxData> ethTxData = Mockito.mockStatic(EthTxData.class)) { |
418 | 447 | final var toAddress = new byte[] {1, 0, 1, 0}; |
419 | 448 | ethTxData.when(() -> EthTxData.populateEthTxData(any())).thenReturn(ethTxDataReturned); |
420 | | - given(ethTxDataReturned.gasLimit()).willReturn(INTRINSIC_GAS_FOR_0_ARG_METHOD + 1); |
| 449 | + given(pureChecksContext.body()).willReturn(ethTxWithTx()); |
421 | 450 | given(ethTxDataReturned.value()).willReturn(BigInteger.ZERO); |
422 | 451 | given(ethTxDataReturned.hasToAddress()).willReturn(true); |
423 | | - given(gasCalculator.transactionIntrinsicGasCost(org.apache.tuweni.bytes.Bytes.wrap(new byte[0]), false, 0L)) |
424 | | - .willReturn(INTRINSIC_GAS_FOR_0_ARG_METHOD); |
425 | 452 | given(ethTxDataReturned.to()).willReturn(toAddress); |
426 | | - given(pureChecksContext.body()).willReturn(ethTxWithTx()); |
427 | | - assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 453 | + PreCheckException exception = |
| 454 | + assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 455 | + assertEquals(INVALID_CONTRACT_ID, exception.responseCode()); |
428 | 456 | } |
| 457 | + } |
429 | 458 |
|
| 459 | + @Test |
| 460 | + void validatePureChecksAtLeastIntrinsicGas() { |
430 | 461 | // check at least intrinsic gas |
431 | 462 | try (MockedStatic<EthTxData> ethTxData = Mockito.mockStatic(EthTxData.class)) { |
432 | 463 | ethTxData.when(() -> EthTxData.populateEthTxData(any())).thenReturn(ethTxDataReturned); |
| 464 | + given(pureChecksContext.body()).willReturn(ethTxWithTx()); |
| 465 | + given(ethTxDataReturned.value()).willReturn(BigInteger.ZERO); |
| 466 | + given(ethTxDataReturned.hasToAddress()).willReturn(true); |
| 467 | + final var toAddress = RECEIVER_ADDRESS.toByteArray(); |
| 468 | + given(ethTxDataReturned.to()).willReturn(toAddress); |
433 | 469 | given(gasCalculator.transactionIntrinsicGasCost(org.apache.tuweni.bytes.Bytes.wrap(new byte[0]), false, 0L)) |
434 | 470 | .willReturn(INTRINSIC_GAS_FOR_0_ARG_METHOD); |
| 471 | + PreCheckException exception = |
| 472 | + assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 473 | + assertEquals(INSUFFICIENT_GAS, exception.responseCode()); |
| 474 | + } |
| 475 | + } |
| 476 | + |
| 477 | + @Test |
| 478 | + void validatePureChecksAtLeastIntrinsicGasForContractCreate() { |
| 479 | + // check at least intrinsic gas for contract create (hasToAddress() == false) |
| 480 | + try (MockedStatic<EthTxData> ethTxData = Mockito.mockStatic(EthTxData.class)) { |
| 481 | + ethTxData.when(() -> EthTxData.populateEthTxData(any())).thenReturn(ethTxDataReturned); |
435 | 482 | given(pureChecksContext.body()).willReturn(ethTxWithTx()); |
436 | | - assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 483 | + given(ethTxDataReturned.value()).willReturn(BigInteger.ZERO); |
| 484 | + given(ethTxDataReturned.hasToAddress()).willReturn(false); |
| 485 | + given(gasCalculator.transactionIntrinsicGasCost(org.apache.tuweni.bytes.Bytes.wrap(new byte[0]), true, 0L)) |
| 486 | + .willReturn(INTRINSIC_GAS_FOR_0_ARG_METHOD); |
| 487 | + PreCheckException exception = |
| 488 | + assertThrows(PreCheckException.class, () -> subject.pureChecks(pureChecksContext)); |
| 489 | + assertEquals(INSUFFICIENT_GAS, exception.responseCode()); |
437 | 490 | } |
438 | 491 | } |
439 | 492 |
|
|
0 commit comments