Skip to content

Commit

Permalink
Provide more Delete X tests for Non-payer key verification (#10103)
Browse files Browse the repository at this point in the history
Signed-off-by: dikel <[email protected]>
  • Loading branch information
dikel authored Nov 27, 2023
1 parent 528a340 commit eccf8fd
Showing 1 changed file with 106 additions and 1 deletion.
107 changes: 106 additions & 1 deletion hedera-node/hedera-app/src/xtest/java/contract/DeleteXTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@

package contract;

import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE;
import static com.hedera.hapi.node.base.ResponseCodeEnum.INVALID_TOKEN_ID;
import static com.hedera.hapi.node.base.ResponseCodeEnum.TOKEN_IS_IMMUTABLE;
import static com.hedera.hapi.node.base.ResponseCodeEnum.TOKEN_WAS_DELETED;
import static com.hedera.node.app.service.contract.impl.utils.ConversionUtils.asHeadlongAddress;
import static contract.AssociationsXTestConstants.A_TOKEN_ADDRESS;
import static contract.AssociationsXTestConstants.A_TOKEN_ID;
import static contract.AssociationsXTestConstants.B_TOKEN_ADDRESS;
import static contract.AssociationsXTestConstants.B_TOKEN_ID;
import static contract.AssociationsXTestConstants.C_TOKEN_ADDRESS;
import static contract.AssociationsXTestConstants.C_TOKEN_ID;
import static contract.AssociationsXTestConstants.D_TOKEN_ADDRESS;
import static contract.AssociationsXTestConstants.D_TOKEN_ID;
import static contract.HtsErc721TransferXTestConstants.UNAUTHORIZED_SPENDER_ID;
import static contract.XTestConstants.AN_ED25519_KEY;
import static contract.XTestConstants.ERC20_TOKEN_ADDRESS;
import static contract.XTestConstants.ERC20_TOKEN_ID;
import static contract.XTestConstants.ERC721_TOKEN_ADDRESS;
Expand Down Expand Up @@ -50,6 +61,20 @@
import java.util.Map;
import org.apache.tuweni.bytes.Bytes;

/**
* Exercises delete on a fungible and non-fungible token via the following steps relative to an {@code OWNER} account:
* <ol>
* <li>Deletes {@code ERC20_TOKEN} via DELETE operation</li>
* <li>Deletes {@code ERC721_TOKEN} via DELETE operation</li>
* <li>Freezes a deleted {@code ERC20_TOKEN}. This should fail with TOKEN_WAS_DELETED</li>
* <li>Freezes a deleted {@code ERC721_TOKEN}. This should fail with TOKEN_WAS_DELETED</li>
* <li>Deletes {@code ERC20_TOKEN} without admin key. This should fail with TOKEN_IS_IMMUTABLE</li>
* <li>Deletes {@code ERC721_TOKEN} without admin key. This should fail with TOKEN_IS_IMMUTABLE</li>
* <li>Deletes {@code ERC20_TOKEN} with wrong admin key. This should fail with INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE</li>
* <li>Deletes {@code ERC721_TOKEN} with wrong admin key. This should fail with INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE</li>
* <li>Deletes token with invalid token address via DELETE operation. This should fail with INVALID_TOKEN_ID</li>
* </ol>
*/
public class DeleteXTest extends AbstractContractXTest {
@Override
protected void doScenarioOperations() {
Expand All @@ -61,6 +86,14 @@ protected void doScenarioOperations() {
.array()),
assertSuccess());

// Successfully delete token
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Bytes.wrap(DeleteTranslator.DELETE_TOKEN
.encodeCallWithArgs(ERC721_TOKEN_ADDRESS)
.array()),
assertSuccess());

// Try to freeze deleted token
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Expand All @@ -70,15 +103,55 @@ protected void doScenarioOperations() {
output -> assertEquals(
Bytes.wrap(ReturnTypes.encodedRc(TOKEN_WAS_DELETED).array()), output));

// Try to freeze deleted token
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Bytes.wrap(FreezeUnfreezeTranslator.FREEZE
.encodeCallWithArgs(ERC721_TOKEN_ADDRESS, asHeadlongAddress(SENDER_ADDRESS.toByteArray()))
.array()),
output -> assertEquals(
Bytes.wrap(ReturnTypes.encodedRc(TOKEN_WAS_DELETED).array()), output));

// Fail if token has no admin key
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Bytes.wrap(DeleteTranslator.DELETE_TOKEN
.encodeCallWithArgs(ERC721_TOKEN_ADDRESS)
.encodeCallWithArgs(A_TOKEN_ADDRESS)
.array()),
output -> assertEquals(
Bytes.wrap(ReturnTypes.encodedRc(TOKEN_IS_IMMUTABLE).array()), output));

// Fail if token has no admin key
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Bytes.wrap(DeleteTranslator.DELETE_TOKEN
.encodeCallWithArgs(B_TOKEN_ADDRESS)
.array()),
output -> assertEquals(
Bytes.wrap(ReturnTypes.encodedRc(TOKEN_IS_IMMUTABLE).array()), output));

// Fail if token has wrong admin key
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Bytes.wrap(DeleteTranslator.DELETE_TOKEN
.encodeCallWithArgs(C_TOKEN_ADDRESS)
.array()),
output -> assertEquals(
Bytes.wrap(ReturnTypes.encodedRc(INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE)
.array()),
output));

// Fail if token has wrong admin key
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Bytes.wrap(DeleteTranslator.DELETE_TOKEN
.encodeCallWithArgs(D_TOKEN_ADDRESS)
.array()),
output -> assertEquals(
Bytes.wrap(ReturnTypes.encodedRc(INVALID_FULL_PREFIX_SIGNATURE_FOR_PRECOMPILE)
.array()),
output));

// should fail when token has invalid address
runHtsCallAndExpectOnSuccess(
SENDER_BESU_ADDRESS,
Expand Down Expand Up @@ -122,8 +195,40 @@ protected Map<TokenID, Token> initialTokens() {
Token.newBuilder()
.tokenId(ERC721_TOKEN_ID)
.treasuryAccountId(SENDER_ID)
.tokenType(TokenType.NON_FUNGIBLE_UNIQUE)
.adminKey(SENDER_CONTRACT_ID_KEY)
.freezeKey(SENDER_CONTRACT_ID_KEY)
.build());
tokens.put(
A_TOKEN_ID,
Token.newBuilder()
.tokenId(A_TOKEN_ID)
.treasuryAccountId(SENDER_ID)
.tokenType(TokenType.FUNGIBLE_COMMON)
.build());
tokens.put(
B_TOKEN_ID,
Token.newBuilder()
.tokenId(B_TOKEN_ID)
.treasuryAccountId(SENDER_ID)
.tokenType(TokenType.NON_FUNGIBLE_UNIQUE)
.build());
tokens.put(
C_TOKEN_ID,
Token.newBuilder()
.tokenId(C_TOKEN_ID)
.treasuryAccountId(UNAUTHORIZED_SPENDER_ID)
.tokenType(TokenType.FUNGIBLE_COMMON)
.adminKey(AN_ED25519_KEY)
.build());
tokens.put(
D_TOKEN_ID,
Token.newBuilder()
.tokenId(D_TOKEN_ID)
.treasuryAccountId(UNAUTHORIZED_SPENDER_ID)
.tokenType(TokenType.NON_FUNGIBLE_UNIQUE)
.adminKey(AN_ED25519_KEY)
.build());
return tokens;
}

Expand Down

0 comments on commit eccf8fd

Please sign in to comment.