Skip to content

Commit

Permalink
chore: Refactor hapi tests to use hapiTest(...) instead of `default…
Browse files Browse the repository at this point in the history
…HapiSpec(...)` (Part 3) (#16698)

Signed-off-by: Matt Hess <matt.hess@swirldslabs.com>
Signed-off-by: Neeharika-Sompalli <neeharika.sompalli@swirldslabs.com>
Co-authored-by: Neeharika-Sompalli <neeharika.sompalli@swirldslabs.com>
  • Loading branch information
mhess-swl and Neeharika-Sompalli authored Dec 23, 2024
1 parent 7d83ba2 commit fe64ed3
Show file tree
Hide file tree
Showing 43 changed files with 2,263 additions and 2,725 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.hedera.services.bdd.spec.HapiPropertySource.asHexedSolidityAddress;
import static com.hedera.services.bdd.spec.HapiPropertySource.asSolidityAddress;
import static com.hedera.services.bdd.spec.HapiPropertySource.asToken;
import static com.hedera.services.bdd.spec.HapiSpec.defaultHapiSpec;
import static com.hedera.services.bdd.spec.HapiSpec.hapiTest;
import static com.hedera.services.bdd.spec.assertions.AccountInfoAsserts.accountWith;
import static com.hedera.services.bdd.spec.assertions.AccountInfoAsserts.changeFromSnapshot;
Expand Down Expand Up @@ -121,82 +120,78 @@ final Stream<DynamicTest> canCreateTokenWithCryptoAdminKeyOnlyIfHasTopLevelSig()
final AtomicReference<byte[]> adminKey = new AtomicReference<>();
final AtomicReference<AccountCreationDetails> creationDetails = new AtomicReference<>();

return defaultHapiSpec("canCreateTokenWithCryptoAdminKeyOnlyIfHasTopLevelSig")
.given(
// Deploy our test contract
uploadInitCode(contract),
contractCreate(contract).gas(5_000_000L),
return hapiTest(
// Deploy our test contract
uploadInitCode(contract),
contractCreate(contract).gas(5_000_000L),

// Create an ECDSA key
newKeyNamed(cryptoKey)
.shape(SECP256K1_ON)
.exposingKeyTo(
k -> adminKey.set(k.getECDSASecp256K1().toByteArray())),
// Create an account with an EVM address derived from this key
cryptoTransfer(tinyBarsFromToWithAlias(DEFAULT_PAYER, cryptoKey, 2 * ONE_HUNDRED_HBARS))
.via("creation"),
// Get its EVM address for later use in the contract call
getTxnRecord("creation")
.exposingCreationDetailsTo(allDetails -> creationDetails.set(allDetails.getFirst())),
// Update key to a threshold key authorizing our contract use this account as a token treasury
newKeyNamed(thresholdKey)
.shape(threshOf(1, PREDEFINED_SHAPE, CONTRACT).signedWith(sigs(cryptoKey, contract))),
sourcing(() -> cryptoUpdate(
asAccountString(creationDetails.get().createdId()))
// Create an ECDSA key
newKeyNamed(cryptoKey)
.shape(SECP256K1_ON)
.exposingKeyTo(k -> adminKey.set(k.getECDSASecp256K1().toByteArray())),
// Create an account with an EVM address derived from this key
cryptoTransfer(tinyBarsFromToWithAlias(DEFAULT_PAYER, cryptoKey, 2 * ONE_HUNDRED_HBARS))
.via("creation"),
// Get its EVM address for later use in the contract call
getTxnRecord("creation")
.exposingCreationDetailsTo(allDetails -> creationDetails.set(allDetails.getFirst())),
// Update key to a threshold key authorizing our contract use this account as a token treasury
newKeyNamed(thresholdKey)
.shape(threshOf(1, PREDEFINED_SHAPE, CONTRACT).signedWith(sigs(cryptoKey, contract))),
sourcing(
() -> cryptoUpdate(asAccountString(creationDetails.get().createdId()))
.key(thresholdKey)
.signedBy(DEFAULT_PAYER, cryptoKey)))
.when(
// First verify we fail to create without the admin key's top-level signature
sourcing(() -> contractCall(
contract,
"createFungibleTokenWithSECP256K1AdminKeyPublic",
// Treasury is the EVM address
creationDetails.get().evmAddress(),
// Admin key is the ECDSA key
adminKey.get())
.via("creationWithoutTopLevelSig")
.gas(5_000_000L)
.sending(100 * ONE_HBAR)
.hasKnownStatus(CONTRACT_REVERT_EXECUTED)),
// Next verify we succeed when using the top-level SignatureMap to
// sign with the admin key
sourcing(() -> contractCall(
contract,
"createFungibleTokenWithSECP256K1AdminKeyPublic",
creationDetails.get().evmAddress(),
adminKey.get())
.via("creationActivatingAdminKeyViaSigMap")
.gas(5_000_000L)
.sending(100 * ONE_HBAR)
// This is the important change, include a top-level signature with the admin key
.alsoSigningWithFullPrefix(cryptoKey)),
// Finally confirm we ALSO succeed when providing the admin key's
// signature via an EthereumTransaction signature
cryptoCreate(RELAYER).balance(10 * THOUSAND_HBAR),
sourcing(() -> ethereumCall(
contract,
"createFungibleTokenWithSECP256K1AdminKeyPublic",
creationDetails.get().evmAddress(),
adminKey.get())
.type(EthTxData.EthTransactionType.EIP1559)
.nonce(0)
.signingWith(cryptoKey)
.payingWith(RELAYER)
.sending(50 * ONE_HBAR)
.maxGasAllowance(ONE_HBAR * 10)
.gasLimit(5_000_000L)
.via("creationActivatingAdminKeyViaEthTxSig")))
.then(
childRecordsCheck(
"creationWithoutTopLevelSig",
CONTRACT_REVERT_EXECUTED,
recordWith().status(INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE)),
getTxnRecord("creationActivatingAdminKeyViaSigMap")
.exposingTokenCreationsTo(createdIds ->
assertFalse(createdIds.isEmpty(), "Top-level sig map creation failed")),
getTxnRecord("creationActivatingAdminKeyViaEthTxSig")
.exposingTokenCreationsTo(
createdIds -> assertFalse(createdIds.isEmpty(), "EthTx sig creation failed")));
.signedBy(DEFAULT_PAYER, cryptoKey)),
// First verify we fail to create without the admin key's top-level signature
sourcing(() -> contractCall(
contract,
"createFungibleTokenWithSECP256K1AdminKeyPublic",
// Treasury is the EVM address
creationDetails.get().evmAddress(),
// Admin key is the ECDSA key
adminKey.get())
.via("creationWithoutTopLevelSig")
.gas(5_000_000L)
.sending(100 * ONE_HBAR)
.hasKnownStatus(CONTRACT_REVERT_EXECUTED)),
// Next verify we succeed when using the top-level SignatureMap to
// sign with the admin key
sourcing(() -> contractCall(
contract,
"createFungibleTokenWithSECP256K1AdminKeyPublic",
creationDetails.get().evmAddress(),
adminKey.get())
.via("creationActivatingAdminKeyViaSigMap")
.gas(5_000_000L)
.sending(100 * ONE_HBAR)
// This is the important change, include a top-level signature with the admin key
.alsoSigningWithFullPrefix(cryptoKey)),
// Finally confirm we ALSO succeed when providing the admin key's
// signature via an EthereumTransaction signature
cryptoCreate(RELAYER).balance(10 * THOUSAND_HBAR),
sourcing(() -> ethereumCall(
contract,
"createFungibleTokenWithSECP256K1AdminKeyPublic",
creationDetails.get().evmAddress(),
adminKey.get())
.type(EthTxData.EthTransactionType.EIP1559)
.nonce(0)
.signingWith(cryptoKey)
.payingWith(RELAYER)
.sending(50 * ONE_HBAR)
.maxGasAllowance(ONE_HBAR * 10)
.gasLimit(5_000_000L)
.via("creationActivatingAdminKeyViaEthTxSig")),
childRecordsCheck(
"creationWithoutTopLevelSig",
CONTRACT_REVERT_EXECUTED,
recordWith().status(INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE)),
getTxnRecord("creationActivatingAdminKeyViaSigMap")
.exposingTokenCreationsTo(
createdIds -> assertFalse(createdIds.isEmpty(), "Top-level sig map creation failed")),
getTxnRecord("creationActivatingAdminKeyViaEthTxSig")
.exposingTokenCreationsTo(
createdIds -> assertFalse(createdIds.isEmpty(), "EthTx sig creation failed")));
}

@HapiTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,16 @@ final Stream<DynamicTest> nonceNotUpdatedWhenSenderDoesNotHaveEnoughBalanceHandl

@HapiTest
final Stream<DynamicTest> nonceNotUpdatedForNonEthereumTransaction() {
return defaultHapiSpec("nonceNotUpdatedForNonEthereumTransaction")
.given(
cryptoCreate(RELAYER).balance(ONE_HUNDRED_HBARS),
uploadInitCode(INTERNAL_CALLEE_CONTRACT),
contractCreate(INTERNAL_CALLEE_CONTRACT))
.when(contractCall(INTERNAL_CALLEE_CONTRACT, EXTERNAL_FUNCTION)
return hapiTest(
cryptoCreate(RELAYER).balance(ONE_HUNDRED_HBARS),
uploadInitCode(INTERNAL_CALLEE_CONTRACT),
contractCreate(INTERNAL_CALLEE_CONTRACT),
contractCall(INTERNAL_CALLEE_CONTRACT, EXTERNAL_FUNCTION)
.gas(ENOUGH_GAS_LIMIT)
.payingWith(RELAYER)
.signingWith(RELAYER)
.via(TX))
.then(getAccountInfo(RELAYER).has(accountWith().nonce(0L)));
.via(TX),
getAccountInfo(RELAYER).has(accountWith().nonce(0L)));
}

@HapiTest
Expand Down Expand Up @@ -832,28 +831,25 @@ final Stream<DynamicTest> nonceUpdatedAfterEvmReversionDueSendingValueToEthereum
// depends on https://github.com/hashgraph/hedera-services/pull/11359
@HapiTest
final Stream<DynamicTest> nonceUpdatedAfterEvmReversionDueSendingValueToHederaPrecompileEthContractCreate() {
return defaultHapiSpec("nonceUpdatedAfterEvmReversionDueSendingValueToHederaPrecompileEthContractCreate")
.given(
newKeyNamed(SECP_256K1_SOURCE_KEY).shape(SECP_256K1_SHAPE),
cryptoCreate(RELAYER).balance(ONE_MILLION_HBARS),
cryptoTransfer(tinyBarsFromAccountToAlias(GENESIS, SECP_256K1_SOURCE_KEY, ONE_MILLION_HBARS)),
uploadInitCode(REVERTER_CONSTRUCTOR_CALL_WITH_VALUE_TO_HEDERA_PRECOMPILE_CONTRACT))
.when(ethereumContractCreate(REVERTER_CONSTRUCTOR_CALL_WITH_VALUE_TO_HEDERA_PRECOMPILE_CONTRACT)
return hapiTest(
newKeyNamed(SECP_256K1_SOURCE_KEY).shape(SECP_256K1_SHAPE),
cryptoCreate(RELAYER).balance(ONE_MILLION_HBARS),
cryptoTransfer(tinyBarsFromAccountToAlias(GENESIS, SECP_256K1_SOURCE_KEY, ONE_MILLION_HBARS)),
uploadInitCode(REVERTER_CONSTRUCTOR_CALL_WITH_VALUE_TO_HEDERA_PRECOMPILE_CONTRACT),
ethereumContractCreate(REVERTER_CONSTRUCTOR_CALL_WITH_VALUE_TO_HEDERA_PRECOMPILE_CONTRACT)
.balance(1L)
.type(EthTransactionType.EIP1559)
.signingWith(SECP_256K1_SOURCE_KEY)
.payingWith(RELAYER)
.nonce(0)
.gasLimit(ENOUGH_GAS_LIMIT)
.hasKnownStatus(INVALID_CONTRACT_ID)
.via(TX))
.then(
getAliasedAccountInfo(SECP_256K1_SOURCE_KEY)
.has(accountWith().nonce(1L)),
getTxnRecord(TX)
.hasPriority(recordWith()
.status(INVALID_CONTRACT_ID)
.contractCreateResult(resultWith().signerNonce(1L))));
.via(TX),
getAliasedAccountInfo(SECP_256K1_SOURCE_KEY).has(accountWith().nonce(1L)),
getTxnRecord(TX)
.hasPriority(recordWith()
.status(INVALID_CONTRACT_ID)
.contractCreateResult(resultWith().signerNonce(1L))));
}

@HapiTest
Expand All @@ -879,14 +875,13 @@ final Stream<DynamicTest> nonceUpdatedAfterSuccessfulEthereumContractCreation()
@HapiTest
final Stream<DynamicTest> revertsWhenSenderDoesNotExist() {
AtomicReference<AccountID> receiverId = new AtomicReference<>();
return defaultHapiSpec("revertsWhenSenderDoesNotExist")
.given(
cryptoCreate(RECEIVER).balance(0L).exposingCreatedIdTo(receiverId::set),
newKeyNamed(SECP_256K1_SOURCE_KEY).shape(SECP_256K1_SHAPE),
cryptoCreate(RELAYER).balance(ONE_HUNDRED_HBARS),
uploadInitCode(INTERNAL_CALLER_CONTRACT),
contractCreate(INTERNAL_CALLER_CONTRACT).balance(ONE_HBAR))
.when(withOpContext((spec, op) -> allRunFor(
return hapiTest(
cryptoCreate(RECEIVER).balance(0L).exposingCreatedIdTo(receiverId::set),
newKeyNamed(SECP_256K1_SOURCE_KEY).shape(SECP_256K1_SHAPE),
cryptoCreate(RELAYER).balance(ONE_HUNDRED_HBARS),
uploadInitCode(INTERNAL_CALLER_CONTRACT),
contractCreate(INTERNAL_CALLER_CONTRACT).balance(ONE_HBAR),
withOpContext((spec, op) -> allRunFor(
spec,
ethereumCall(
INTERNAL_CALLER_CONTRACT,
Expand All @@ -898,8 +893,8 @@ final Stream<DynamicTest> revertsWhenSenderDoesNotExist() {
.nonce(0)
.gasLimit(ENOUGH_GAS_LIMIT)
.hasKnownStatus(INVALID_ACCOUNT_ID)
.via(TX))))
.then(getTxnRecord(TX)
.via(TX))),
getTxnRecord(TX)
.hasPriority(
recordWith().contractCallResult(resultWith().signerNonce(0L))));
}
Expand Down
Loading

0 comments on commit fe64ed3

Please sign in to comment.