Skip to content

Commit

Permalink
Revert HapiTest expectations for inline charging
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Tinker <[email protected]>
  • Loading branch information
tinker-michaelj committed Jul 18, 2024
1 parent 9896020 commit 66d8919
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.CHILD;
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.PRECEDING;
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.SCHEDULED;
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.USER;

import com.hedera.hapi.node.base.ResponseCodeEnum;
import com.hedera.hapi.node.base.Transaction;
Expand Down Expand Up @@ -65,14 +67,23 @@ public interface SingleTransactionRecordBuilder {
HandleContext.TransactionCategory category();

/**
* Returns true if this transaction originated from inside another handler or workflow; and not
* Returns true if this builder's transaction originated from inside another handler or workflow; and not
* a user transaction (or scheduled user transaction).
* @return true if this transaction is internal
*/
default boolean isInternalDispatch() {
return category() == CHILD || category() == PRECEDING;
}

/**
* Returns true if this builder's transaction originated from a user transaction or scheduled user transaction; and
* not from inside another handler or workflow.
* @return true if this transaction is internal
*/
default boolean isUserDispatch() {
return category() == USER || category() == SCHEDULED;
}

/**
* Convenience method to package as {@link TransactionBody} as a {@link Transaction} .
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ private TokenAssociation validateAndBuildAutoAssociation(
validateFalse(token.hasKycKey(), ACCOUNT_KYC_NOT_GRANTED_FOR_TOKEN);
validateFalse(token.accountsFrozenByDefault(), ACCOUNT_FROZEN_FOR_TOKEN);

// We only charge auto-association fees inline if this is NOT an internal dispatch; in that case the
// We only charge auto-association fees inline if this is a user disaptch; in that case the
// contract service will take the auto-association costs from the remaining EVM gas
if (!context.recordBuilders()
if (context.recordBuilders()
.getOrCreate(SingleTransactionRecordBuilder.class)
.isInternalDispatch()) {
.isUserDispatch()) {
final var unlimitedAssociationsEnabled =
config.getConfigData(EntitiesConfig.class).unlimitedAutoAssociationsEnabled();
// And the "sender pays" fee model only applies when using unlimited auto-associations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ void happyPathWorksWithAutoCreation() {
writableAccountStore.get(feeCollectorId).tinybarBalance();
given(handleContext.expiryValidator()).willReturn(expiryValidator);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(transferRecordBuilder);

subject.handle(handleContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@
import com.hedera.node.app.service.token.impl.handlers.transfer.EnsureAliasesStep;
import com.hedera.node.app.service.token.impl.handlers.transfer.ReplaceAliasesWithIDsInOp;
import com.hedera.node.app.service.token.impl.handlers.transfer.TransferContextImpl;
import com.hedera.node.app.service.token.records.CryptoTransferRecordBuilder;
import com.hedera.node.app.spi.workflows.HandleException;
import com.hedera.node.app.spi.workflows.record.SingleTransactionRecordBuilder;
import com.hedera.node.app.workflows.handle.record.SingleTransactionRecordBuilderImpl;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class AdjustFungibleTokenChangesStepTest extends StepsBase {
@Mock
private CryptoTransferRecordBuilder builder;

@Override
@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -81,6 +86,8 @@ void doesTokenBalanceChangesWithoutAllowances() {
final var receiver = asAccount(tokenReceiver);
given(handleContext.payer()).willReturn(spenderId);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);
final var replacedOp = getReplacedOp();
adjustFungibleTokenChangesStep = new AdjustFungibleTokenChangesStep(replacedOp.tokenTransfers(), payerId);

Expand Down Expand Up @@ -122,6 +129,8 @@ void doesTokenBalanceChangesWithAllowances() {
replaceAliasesWithIDsInOp = new ReplaceAliasesWithIDsInOp();
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);
given(handleContext.body()).willReturn(txn);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

final var receiver = asAccount(tokenReceiver);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
Expand Down Expand Up @@ -185,6 +194,8 @@ void failsWhenExpectedDecimalsDiffer() {
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);
given(handleContext.body()).willReturn(txn);
given(handleContext.payer()).willReturn(spenderId);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
final var replacedOp = getReplacedOp();
Expand Down Expand Up @@ -213,6 +224,8 @@ void allowanceWithGreaterThanAllowedAllowanceFails() {
replaceAliasesWithIDsInOp = new ReplaceAliasesWithIDsInOp();
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);
given(handleContext.body()).willReturn(txn);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
final var replacedOp = getReplacedOp();
Expand Down Expand Up @@ -246,6 +259,8 @@ void transferGreaterThanTokenRelBalanceFails() {
given(handleContext.body()).willReturn(txn);
given(handleContext.payer()).willReturn(spenderId);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

final var replacedOp = getReplacedOp();
adjustFungibleTokenChangesStep = new AdjustFungibleTokenChangesStep(replacedOp.tokenTransfers(), spenderId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@
import com.hedera.node.app.service.token.impl.handlers.transfer.EnsureAliasesStep;
import com.hedera.node.app.service.token.impl.handlers.transfer.ReplaceAliasesWithIDsInOp;
import com.hedera.node.app.service.token.impl.handlers.transfer.TransferContextImpl;
import com.hedera.node.app.service.token.records.CryptoTransferRecordBuilder;
import com.hedera.node.app.spi.workflows.HandleException;
import com.hedera.node.app.spi.workflows.record.SingleTransactionRecordBuilder;
import com.hedera.node.app.workflows.handle.record.SingleTransactionRecordBuilderImpl;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class AdjustHbarChangesStepTest extends StepsBase {
@Mock
private CryptoTransferRecordBuilder builder;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -86,6 +90,8 @@ void doesHbarBalanceChangesWithoutAllowances() {
final var receiver = asAccount(hbarReceiver);
given(handleContext.payer()).willReturn(spenderId);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);
final var replacedOp = getReplacedOp();
adjustHbarChangesStep = new AdjustHbarChangesStep(replacedOp, payerId);

Expand All @@ -112,6 +118,8 @@ void doesHbarBalanceChangesWithAllowances() {
replaceAliasesWithIDsInOp = new ReplaceAliasesWithIDsInOp();
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);
given(handleContext.body()).willReturn(txn);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

final var receiver = asAccount(hbarReceiver);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.hedera.hapi.node.token.CryptoTransferTransactionBody;
import com.hedera.node.app.service.token.impl.handlers.transfer.AssociateTokenRecipientsStep;
import com.hedera.node.app.service.token.impl.handlers.transfer.TransferContextImpl;
import com.hedera.node.app.service.token.records.CryptoTransferRecordBuilder;
import com.hedera.node.app.spi.records.RecordBuilders;
import com.hedera.node.app.spi.validation.ExpiryValidator;
import com.hedera.node.app.spi.workflows.HandleContext;
Expand All @@ -56,6 +57,9 @@ public class AssociateTokenRecipientsStepTest extends StepsBase {
@Mock
private RecordBuilders recordBuilders;

@Mock
private CryptoTransferRecordBuilder builder;

private AssociateTokenRecipientsStep subject;
private CryptoTransferTransactionBody txn;
private TransferContextImpl transferContext;
Expand Down Expand Up @@ -83,6 +87,8 @@ void associatesTokenRecepients() {
.withValue("entities.unlimitedAutoAssociationsEnabled", false)
.getOrCreateConfig();
given(handleContext.configuration()).willReturn(modifiedConfiguration);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

subject.doIn(transferContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class CustomFeeAssessmentStepTest extends StepsBase {
private TransferContextImpl transferContext;
private CustomFeeAssessmentStep subject;
private Token fungibleWithNoKyc;
private Token nonFungibleWithNoKyc;

@Mock
private CryptoTransferRecordBuilder builder;

@BeforeEach
public void setUp() {
super.setUp();
Expand All @@ -80,7 +84,6 @@ public void setUp() {
givenStoresAndConfig(handleContext);
givenTxn();
given(handleContext.body()).willReturn(txn);
given(recordBuilders.getOrCreate(CryptoTransferRecordBuilder.class)).willReturn(xferRecordBuilder);
givenAutoCreationDispatchEffects(payerId);

transferContext = new TransferContextImpl(handleContext);
Expand All @@ -89,6 +92,7 @@ public void setUp() {
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);

given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
given(recordBuilders.getOrCreate(any())).willReturn(builder);
final var replacedOp = getReplacedOp();
subject = new CustomFeeAssessmentStep(replacedOp);
}
Expand Down Expand Up @@ -174,8 +178,6 @@ void htsFixedFeeAndRoyaltyFeeWithFallbackSelfDenomination() {
assertThatTransferListContains(level1Op.tokenTransfers(), expectedLevel1TokenTransfers);
assertThatTransferListContains(givenOp.tokenTransfers(), expectedGivenOpTokenTransfers);
assertThatTransfersContains(givenOp.transfers().accountAmounts(), expectedGivenOpHbarTransfers);

// verify(xferRecordBuilder).assessedCustomFees(anyList());
}

@Test
Expand Down Expand Up @@ -254,8 +256,6 @@ void htsFixedFeeSelfDenominationAndRoyaltyFeeNoFallback() {

assertThatTransferListContains(givenOp.tokenTransfers(), expectedGivenOpTokenTransfers);
assertThatTransfersContains(givenOp.transfers().accountAmounts(), expectedGivenOpHbarTransfers);

// verify(xferRecordBuilder).assessedCustomFees(anyList());
}

@Test
Expand Down Expand Up @@ -367,8 +367,6 @@ void htsFixedFeeAndRoyaltyFeeWithFallbackNetOfTransfersSelfDenomination() {
assertThatTransferListContains(level1Op.tokenTransfers(), expectedLevel1TokenTransfers);
assertThatTransferListContains(givenOp.tokenTransfers(), expectedGivenOpTokenTransfers);
assertThatTransfersContains(givenOp.transfers().accountAmounts(), expectedGivenOpHbarTransfers);

// verify(xferRecordBuilder).assessedCustomFees(anyList());
}

@Test
Expand Down Expand Up @@ -446,6 +444,8 @@ private void givenDifferentTxn(final CryptoTransferTransactionBody body, final A
givenStoresAndConfig(handleContext);
givenTxn(body, payerId);
givenAutoCreationDispatchEffects(payerId);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);

transferContext = new TransferContextImpl(handleContext);
ensureAliasesStep = new EnsureAliasesStep(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@
import com.hedera.node.app.service.token.impl.handlers.transfer.NFTOwnersChangeStep;
import com.hedera.node.app.service.token.impl.handlers.transfer.ReplaceAliasesWithIDsInOp;
import com.hedera.node.app.service.token.impl.handlers.transfer.TransferContextImpl;
import com.hedera.node.app.service.token.records.CryptoTransferRecordBuilder;
import com.hedera.node.app.spi.workflows.HandleException;
import com.hedera.node.app.spi.workflows.record.SingleTransactionRecordBuilder;
import com.hedera.node.app.workflows.handle.record.SingleTransactionRecordBuilderImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

class NFTOwnersChangeStepTest extends StepsBase {
@Mock
private CryptoTransferRecordBuilder builder;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -98,6 +102,8 @@ void changesNftOwners() {
final var receiver = asAccount(tokenReceiver);
given(handleContext.payer()).willReturn(spenderId);
given(expiryValidator.expirationStatus(any(), anyBoolean(), anyLong())).willReturn(OK);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);
final var replacedOp = getReplacedOp();
changeNFTOwnersStep = new NFTOwnersChangeStep(replacedOp.tokenTransfers(), payerId);
final var nft = writableNftStore.get(nftIdSl1);
Expand Down Expand Up @@ -182,6 +188,8 @@ void changesNftOwnersWithAllowance() {
.build())
.build();
givenTxn(body, spenderId);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);
ensureAliasesStep = new EnsureAliasesStep(body);
replaceAliasesWithIDsInOp = new ReplaceAliasesWithIDsInOp();
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);
Expand Down Expand Up @@ -253,6 +261,8 @@ void failsWhenSpenderNotSameAsPayer() {
.build())
.build();
givenTxn(body, spenderId);
given(handleContext.recordBuilders()).willReturn(recordBuilders);
given(recordBuilders.getOrCreate(any())).willReturn(builder);
ensureAliasesStep = new EnsureAliasesStep(body);
replaceAliasesWithIDsInOp = new ReplaceAliasesWithIDsInOp();
associateTokenRecepientsStep = new AssociateTokenRecipientsStep(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ protected void givenAutoCreationDispatchEffects(AccountID syntheticPayer) {
return cryptoCreateRecordBuilder.accountID(asAccount(tokenReceiver));
});
given(storeFactory.writableStore(WritableAccountStore.class)).willReturn(writableAccountStore);
given(recordBuilders.getOrCreate(CryptoCreateRecordBuilder.class)).willReturn(cryptoCreateRecordBuilder);
given(recordBuilders.getOrCreate(CryptoTransferRecordBuilder.class)).willReturn(xferRecordBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,6 @@ final Stream<DynamicTest> cryptoTransferAllowanceToContractFT() {
childRecordsCheck(
successfulTransferFromTxn,
SUCCESS,
recordWith().status(SUCCESS),
recordWith()
.status(SUCCESS)
.contractCallResult(resultWith()
Expand Down Expand Up @@ -1282,7 +1281,6 @@ final Stream<DynamicTest> cryptoTransferAllowanceToContractNFT() {
childRecordsCheck(
successfulTransferFromTxn,
SUCCESS,
recordWith().status(SUCCESS),
recordWith()
.status(SUCCESS)
.contractCallResult(resultWith()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ final Stream<DynamicTest> transferNftAfterNestedMint() {
childRecordsCheck(
nestedTransferTxn,
SUCCESS,
recordWith().status(SUCCESS),
recordWith()
.status(SUCCESS)
.contractCallResult(resultWith()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,6 @@ final Stream<DynamicTest> fungibleTokenCreateThenQueryAndTransfer() {
TransactionRecordAsserts.recordWith().status(SUCCESS),
TransactionRecordAsserts.recordWith().status(SUCCESS),
TransactionRecordAsserts.recordWith().status(SUCCESS),
TransactionRecordAsserts.recordWith().status(SUCCESS),
TransactionRecordAsserts.recordWith().status(SUCCESS)),
sourcing(() -> getAccountBalance(ACCOUNT)
.hasTokenBalance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ final Stream<DynamicTest> hapiTransferFromForFungibleToken() {
childRecordsCheck(
successfulTransferFromTxn,
SUCCESS,
recordWith().status(SUCCESS),
recordWith()
.status(SUCCESS)
.contractCallResult(resultWith()
Expand Down Expand Up @@ -805,7 +804,6 @@ final Stream<DynamicTest> hapiTransferFromForNFT() {
childRecordsCheck(
successfulTransferFromTxn,
SUCCESS,
recordWith().status(SUCCESS),
recordWith()
.status(SUCCESS)
.contractCallResult(resultWith()
Expand Down
Loading

0 comments on commit 66d8919

Please sign in to comment.