Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug in Transaction.isSorobanTransaction to accommodate BumpFootprintExpirationOperation. #518

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jobs:
fail-fast: false
matrix:
# minSdk and targetSdk, see ${project_root}/android_test/app/build.gradle.kts
# TODO: Investigate why it is unstable on API 33.
api-level: [ 26, 33 ]
steps:
- name: checkout
Expand All @@ -98,7 +99,7 @@ jobs:
profile: 'pixel_2'

deploy:
needs: [ javadoc, android-test ]
needs: [ javadoc, shadow_jar ] # TODO: add android-test when it is stable.
permissions:
contents: write
if: github.event_name == 'release' && github.event.action == 'created'
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ As this project is pre 1.0, breaking changes may happen for minor version bumps.

## Pending

## 0.41.0-beta.3
* Fix the bug in Transaction.isSorobanTransaction to accommodate BumpFootprintExpirationOperation. ([#518](https://github.com/stellar/java-stellar-sdk/pull/518))

## 0.41.0-beta.2
* Migrate from CircleCI to GitHub Actions. ([#512](https://github.com/stellar/java-stellar-sdk/pull/512))

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spotless {


sourceCompatibility = JavaVersion.VERSION_1_8.toString()
version = '0.41.0-beta.2'
version = '0.41.0-beta.3'
group = 'stellar'
jar.enabled = false

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ public boolean isSorobanTransaction() {

Operation op = mOperations[0];
return op instanceof InvokeHostFunctionOperation
|| op instanceof BumpSequenceOperation
|| op instanceof BumpFootprintExpirationOperation
|| op instanceof RestoreFootprintOperation;
}
}
191 changes: 189 additions & 2 deletions src/test/java/org/stellar/sdk/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -12,8 +13,34 @@
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.stellar.sdk.xdr.*;
import org.stellar.sdk.scval.Scv;
import org.stellar.sdk.xdr.ContractDataDurability;
import org.stellar.sdk.xdr.ContractEntryBodyType;
import org.stellar.sdk.xdr.ContractExecutable;
import org.stellar.sdk.xdr.ContractExecutableType;
import org.stellar.sdk.xdr.ContractIDPreimage;
import org.stellar.sdk.xdr.ContractIDPreimageType;
import org.stellar.sdk.xdr.CreateContractArgs;
import org.stellar.sdk.xdr.DecoratedSignature;
import org.stellar.sdk.xdr.EnvelopeType;
import org.stellar.sdk.xdr.ExtensionPoint;
import org.stellar.sdk.xdr.HostFunction;
import org.stellar.sdk.xdr.HostFunctionType;
import org.stellar.sdk.xdr.Int64;
import org.stellar.sdk.xdr.LedgerEntryType;
import org.stellar.sdk.xdr.LedgerFootprint;
import org.stellar.sdk.xdr.LedgerKey;
import org.stellar.sdk.xdr.SCVal;
import org.stellar.sdk.xdr.SignerKey;
import org.stellar.sdk.xdr.SorobanResources;
import org.stellar.sdk.xdr.SorobanTransactionData;
import org.stellar.sdk.xdr.Uint256;
import org.stellar.sdk.xdr.Uint32;
import org.stellar.sdk.xdr.XdrDataInputStream;
import org.stellar.sdk.xdr.XdrUnsignedInteger;

public class TransactionTest {

Expand Down Expand Up @@ -58,7 +85,7 @@ public void testParseV0Transaction() throws FormatException, IOException {
(Transaction)
Transaction.fromEnvelopeXdr(
AccountConverter.enableMuxed(), transaction.toEnvelopeXdrBase64(), Network.PUBLIC);
assertTrue(parsed.equals(transaction));
assertEquals(parsed, transaction);
assertEquals(EnvelopeType.ENVELOPE_TYPE_TX_V0, parsed.toEnvelopeXdr().getDiscriminant());
assertEquals(transaction.toEnvelopeXdrBase64(), parsed.toEnvelopeXdrBase64());

Expand Down Expand Up @@ -260,4 +287,164 @@ public void testConstructorWithSorobanData() throws IOException {
"AAAAAgAAAABexSIg06FtXzmFBQQtHZsrnyWxUzmthkBEhs/ktoeVYgAAAGQAClWjAAAAAQAAAAAAAAAAAAAAAQAAAAAAAAAYAAAAAQAAAAAAAAAAAAAAAH8wYjTJienWf2nf2TEZi2APPWzmtkwiQHAftisIgyuHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAAAfzBiNMmJ6dZ/ad/ZMRmLYA89bOa2TCJAcB+2KwiDK4cAAAAAAACHBwAAArsAAAAAAAAA2AAAAAAAAABkAAAAAA==";
assertEquals(expectedXdr, transaction.toEnvelopeXdrBase64());
}

@Test
public void testIsSorobanTransactionInvokeHostFunctionOperation() {
KeyPair source =
KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");

Account account = new Account(source.getAccountId(), 2908908335136768L);

String contractId = "CDYUOBUVMZPWIU4GB4XNBAYL6NIHIMYLZFNEXOCDIO33MBJMNPSFBYKU";
String functionName = "hello";
List<SCVal> params = Collections.singletonList(Scv.toSymbol("Soroban"));
InvokeHostFunctionOperation operation =
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
contractId, functionName, params)
.build();

Transaction transaction =
new Transaction(
AccountConverter.enableMuxed(),
account.getAccountId(),
Transaction.MIN_BASE_FEE,
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null,
Network.TESTNET);
assertTrue(transaction.isSorobanTransaction());
}

@Test
public void testIsSorobanTransactionBumpFootprintExpirationOperation() {
KeyPair source =
KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");

Account account = new Account(source.getAccountId(), 2908908335136768L);
String contractId = "CDYUOBUVMZPWIU4GB4XNBAYL6NIHIMYLZFNEXOCDIO33MBJMNPSFBYKU";
SorobanTransactionData sorobanData =
new SorobanDataBuilder()
.setReadOnly(
Collections.singletonList(
new LedgerKey.Builder()
.discriminant(LedgerEntryType.CONTRACT_DATA)
.contractData(
new LedgerKey.LedgerKeyContractData.Builder()
.contract(new Address(contractId).toSCAddress())
.key(Scv.toLedgerKeyContractInstance())
.durability(ContractDataDurability.PERSISTENT)
.bodyType(ContractEntryBodyType.DATA_ENTRY)
.build())
.build()))
.build();
BumpFootprintExpirationOperation operation =
BumpFootprintExpirationOperation.builder().ledgersToExpire(4096L).build();
Transaction transaction =
new Transaction(
AccountConverter.enableMuxed(),
account.getAccountId(),
Transaction.MIN_BASE_FEE,
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
sorobanData,
Network.TESTNET);
assertTrue(transaction.isSorobanTransaction());
}

@Test
public void testIsSorobanTransactionRestoreFootprintOperation() {
KeyPair source =
KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");

Account account = new Account(source.getAccountId(), 2908908335136768L);
String contractId = "CDYUOBUVMZPWIU4GB4XNBAYL6NIHIMYLZFNEXOCDIO33MBJMNPSFBYKU";
SorobanTransactionData sorobanData =
new SorobanDataBuilder()
.setReadOnly(
Collections.singletonList(
new LedgerKey.Builder()
.discriminant(LedgerEntryType.CONTRACT_DATA)
.contractData(
new LedgerKey.LedgerKeyContractData.Builder()
.contract(new Address(contractId).toSCAddress())
.key(Scv.toLedgerKeyContractInstance())
.durability(ContractDataDurability.PERSISTENT)
.bodyType(ContractEntryBodyType.DATA_ENTRY)
.build())
.build()))
.build();
RestoreFootprintOperation operation = RestoreFootprintOperation.builder().build();
Transaction transaction =
new Transaction(
AccountConverter.enableMuxed(),
account.getAccountId(),
Transaction.MIN_BASE_FEE,
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
sorobanData,
Network.TESTNET);
assertTrue(transaction.isSorobanTransaction());
}

@Test
public void testIsSorobanTransactionMultiOperations() {
KeyPair source =
KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");

Account account = new Account(source.getAccountId(), 2908908335136768L);

String contractId = "CDYUOBUVMZPWIU4GB4XNBAYL6NIHIMYLZFNEXOCDIO33MBJMNPSFBYKU";
String functionName = "hello";
List<SCVal> params = Collections.singletonList(Scv.toSymbol("Soroban"));
InvokeHostFunctionOperation operation =
InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
contractId, functionName, params)
.build();

Transaction transaction =
new Transaction(
AccountConverter.enableMuxed(),
account.getAccountId(),
Transaction.MIN_BASE_FEE,
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.Operation[] {operation, operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null,
Network.TESTNET);
assertFalse(transaction.isSorobanTransaction());
}

@Test
public void testIsSorobanTransactionBumpSequenceOperation() {
KeyPair source =
KeyPair.fromSecretSeed("SCH27VUZZ6UAKB67BDNF6FA42YMBMQCBKXWGMFD5TZ6S5ZZCZFLRXKHS");

Account account = new Account(source.getAccountId(), 2908908335136768L);
BumpSequenceOperation operation = new BumpSequenceOperation.Builder(0L).build();

Transaction transaction =
new Transaction(
AccountConverter.enableMuxed(),
account.getAccountId(),
Transaction.MIN_BASE_FEE,
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.Operation[] {operation, operation},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think only single op needed in tx correct? otherwise isSorobanTransaction=false, but for non-tested reason, i.e. it's more than one op, it won't validate op instance mismatch yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is to verify that a transaction containing multiple operations is not a soroban transaction. You can take a look at https://github.com/stellar/java-stellar-sdk/pull/518/files#diff-0114ab460186855a151f487001dae40013372c8430be55eeebdb6e24bfc12705R448

assertFalse(transaction.isSorobanTransaction());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is already another test for asserting multi-op - testIsSorobanTransactionMultiOperations outcome, I think this test was intending to exercise a non-multi op code path but an incorrect op type, which seemed to be the intent, just pass one operation and it should verify same false outcome.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I made a mistake. I have already corrected it. 🤦

null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null,
Network.TESTNET);
assertFalse(transaction.isSorobanTransaction());
}
}