Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel-Trintinalia <[email protected]>
  • Loading branch information
Gabriel-Trintinalia committed Aug 25, 2023
1 parent a5c432e commit 17aa721
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,22 @@ protected JsonRpcError fromErrorResp(final JsonRpcResponse resp) {
protected BlockHeader createBlockHeader(
final Optional<List<Withdrawal>> maybeWithdrawals,
final Optional<List<Deposit>> maybeDeposits) {
return createBlockHeaderFixture(maybeWithdrawals, maybeDeposits).buildHeader();
}

protected BlockHeaderTestFixture createBlockHeaderFixture(
final Optional<List<Withdrawal>> maybeWithdrawals,
final Optional<List<Deposit>> maybeDeposits) {
BlockHeader parentBlockHeader =
new BlockHeaderTestFixture().baseFeePerGas(Wei.ONE).buildHeader();
BlockHeader mockHeader =
new BlockHeaderTestFixture()
.baseFeePerGas(Wei.ONE)
.parentHash(parentBlockHeader.getParentHash())
.number(parentBlockHeader.getNumber() + 1)
.timestamp(parentBlockHeader.getTimestamp() + 1)
.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null))
.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null))
.parentBeaconBlockRoot(maybeParentBeaconBlockRoot)
.buildHeader();
return mockHeader;
return new BlockHeaderTestFixture()
.baseFeePerGas(Wei.ONE)
.parentHash(parentBlockHeader.getParentHash())
.number(parentBlockHeader.getNumber() + 1)
.timestamp(parentBlockHeader.getTimestamp() + 1)
.withdrawalsRoot(maybeWithdrawals.map(BodyValidation::withdrawalsRoot).orElse(null))
.depositsRoot(maybeDeposits.map(BodyValidation::depositsRoot).orElse(null))
.parentBeaconBlockRoot(maybeParentBeaconBlockRoot);
}

protected void assertValidResponse(final BlockHeader mockHeader, final JsonRpcResponse resp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.datatypes.BlobGas;
import org.hyperledger.besu.ethereum.BlockProcessingOutputs;
import org.hyperledger.besu.ethereum.BlockProcessingResult;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.ExecutionEngineJsonRpcMethod;
Expand Down Expand Up @@ -127,6 +128,46 @@ public void shouldReturnInvalidIfWithdrawalsIsNotNull_WhenWithdrawalsProhibited(
verify(engineCallListener, times(1)).executionEngineCalled();
}

@Test
public void shouldReturnInvalidIf_BloBGasUsed_NotNull() {
final List<WithdrawalParameter> withdrawals = List.of();
lenient()
.when(protocolSpec.getWithdrawalsValidator())
.thenReturn(new WithdrawalsValidator.ProhibitedWithdrawals());

BlockHeader blockHeader =
createBlockHeaderFixture(Optional.of(Collections.emptyList()), Optional.empty())
.blobGasUsed(100L)
.buildHeader();

var resp = resp(mockEnginePayload(blockHeader, Collections.emptyList(), withdrawals, null));

final JsonRpcError jsonRpcError = fromErrorResp(resp);
assertThat(jsonRpcError.getCode()).isEqualTo(INVALID_PARAMS.getCode());
assertThat(jsonRpcError.getData()).isEqualTo("non-nil BlobGasUsed pre-cancun");
verify(engineCallListener, times(1)).executionEngineCalled();
}

@Test
public void shouldReturnInvalidIf_excessBlobGas_NotNull() {
final List<WithdrawalParameter> withdrawals = List.of();
lenient()
.when(protocolSpec.getWithdrawalsValidator())
.thenReturn(new WithdrawalsValidator.ProhibitedWithdrawals());

BlockHeader blockHeader =
createBlockHeaderFixture(Optional.of(Collections.emptyList()), Optional.empty())
.excessBlobGas(BlobGas.MAX_BLOB_GAS)
.buildHeader();

var resp = resp(mockEnginePayload(blockHeader, Collections.emptyList(), withdrawals, null));

final JsonRpcError jsonRpcError = fromErrorResp(resp);
assertThat(jsonRpcError.getCode()).isEqualTo(INVALID_PARAMS.getCode());
assertThat(jsonRpcError.getData()).isEqualTo("non-nil BlobGasUsed pre-cancun");
verify(engineCallListener, times(1)).executionEngineCalled();
}

@Test
public void shouldReturnInvalidIfWithdrawalsIsNull_WhenWithdrawalsAllowed() {
final List<WithdrawalParameter> withdrawals = null;
Expand Down

0 comments on commit 17aa721

Please sign in to comment.