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

refactor: refactor the parts in Preconditions that are inconsistent with the specifications. #626

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ As this project is pre 1.0, breaking changes may happen for minor version bumps.
- feat: optimize `RequestTimeoutException`, when a timeout occurs, if the server returns some information, you can read them.
- refactor!: mark `Asset#create(String, String, String)` as deprecated, use `Asset.createNonNativeAsset(String, String)` or `Asset.createNativeAsset()` instead.
- refactor!: remove `LiquidityPoolID`. Use `String` to represent the liquidity pool ID.
- refactor!: `TransactionPreconditions#TransactionPreconditions(LedgerBounds, Long, BigInteger, long, List, TimeBounds)` has been removed, use `TransactionPreconditions#TransactionPreconditions(TimeBounds, LedgerBounds, Long, BigInteger, long, List)` instead.
- refactor: Set the default value of `TransactionPreconditions.extraSigners` to `new ArrayList<>()`, it is not nullable.

## 0.44.0
### Update
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/stellar/sdk/TransactionPreconditions.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.stellar.sdk;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Builder;
import lombok.NonNull;
import lombok.Singular;
import lombok.Value;
import org.jetbrains.annotations.Nullable;
import org.stellar.sdk.xdr.Duration;
import org.stellar.sdk.xdr.Int64;
import org.stellar.sdk.xdr.PreconditionType;
Expand All @@ -29,21 +30,26 @@ public class TransactionPreconditions {
public static final long MAX_EXTRA_SIGNERS_COUNT = 2;
public static final BigInteger TIMEOUT_INFINITE = BigInteger.ZERO;

// We strongly recommend setting it, but in fact, a transaction without timebounds will also be
// accepted by stellar-core.
/** The time bounds for the transaction. */
TimeBounds timeBounds;

/** The ledger bounds for the transaction. */
LedgerBounds ledgerBounds;
@Nullable LedgerBounds ledgerBounds;

/**
* The minimum source account sequence number this transaction is valid for. if <code>null</code>,
* the transaction is valid when **source account's sequence number == tx.sequence - 1**.
*/
Long minSeqNumber; // int64
@Nullable Long minSeqNumber; // int64

/**
* The minimum amount of time between source account sequence time and the ledger time when this
* transaction will become valid. If the value is <code>0</code>, the transaction is unrestricted
* by the account sequence age. Cannot be negative.
*/
@Builder.Default BigInteger minSeqAge = BigInteger.ZERO; // uint64
@Builder.Default @NonNull BigInteger minSeqAge = BigInteger.ZERO; // uint64

/**
* The minimum number of ledgers between source account sequence and the ledger number when this
Expand All @@ -53,10 +59,7 @@ public class TransactionPreconditions {
long minSeqLedgerGap; // uint32

/** Required extra signers. */
@Singular @NonNull List<SignerKey> extraSigners;

/** The time bounds for the transaction. */
TimeBounds timeBounds;
@Builder.Default @NonNull List<SignerKey> extraSigners = new ArrayList<>();

/**
* Validates the preconditions.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/stellar/sdk/FeeBumpTransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void testCreateWithBaseFeeWithSorobanOp() {
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.operations.Operation[] {invokeHostFunctionOperation},
null,
new TransactionPreconditions(null, null, BigInteger.ZERO, 0, new ArrayList<>(), null),
new TransactionPreconditions(null, null, null, BigInteger.ZERO, 0, new ArrayList<>()),
sorobanData,
Network.TESTNET);

Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package org.stellar.sdk;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.LinkedList;
import org.junit.Test;
import org.stellar.sdk.xdr.Duration;
import org.stellar.sdk.xdr.Int64;
Expand Down Expand Up @@ -60,6 +63,7 @@ public void itConvertsFromXdr() throws IOException {
assertEquals(transactionPreconditions.getLedgerBounds().getMaxLedger(), 2);
assertEquals(transactionPreconditions.getMinSeqNumber(), Long.valueOf(4));
assertEquals(transactionPreconditions.getMinSeqLedgerGap(), 0);
assertEquals(transactionPreconditions.getExtraSigners().size(), 0);
}

@Test
Expand Down Expand Up @@ -256,4 +260,32 @@ public void itChecksV2Status() {
TransactionPreconditions transactionPreconditions = TransactionPreconditions.fromXdr(xdr);
assertTrue(transactionPreconditions.hasV2());
}

@Test
public void testSetTimeBoundsOnly() {
TimeBounds timeBounds = new TimeBounds(1, 2);
TransactionPreconditions preconditions =
TransactionPreconditions.builder().timeBounds(timeBounds).build();
assertEquals(timeBounds, preconditions.getTimeBounds());
assertEquals(0, preconditions.getExtraSigners().size());
assertEquals(BigInteger.ZERO, preconditions.getMinSeqAge());
assertNull(preconditions.getLedgerBounds());
assertNull(preconditions.getMinSeqNumber());
assertEquals(0, preconditions.getMinSeqLedgerGap());
assertFalse(preconditions.hasV2());
}

@Test
public void testEquals() {
TimeBounds timeBounds = new TimeBounds(1, 2);
TransactionPreconditions preconditions0 =
TransactionPreconditions.builder().timeBounds(timeBounds).build();
TransactionPreconditions preconditions1 =
TransactionPreconditions.fromXdr(preconditions0.toXdr());
assertEquals(preconditions0, preconditions1);
TransactionPreconditions preconditions2 =
new TransactionPreconditions(
timeBounds, null, null, BigInteger.ZERO, 0, new LinkedList<>());
assertEquals(preconditions0, preconditions2);
}
}
28 changes: 14 additions & 14 deletions src/test/java/org/stellar/sdk/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testParseV0Transaction() throws FormatException, IOException {
},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.PUBLIC);

Expand Down Expand Up @@ -117,7 +117,7 @@ public void testAddingSignaturesDirectly() {
},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.PUBLIC);

Expand Down Expand Up @@ -157,7 +157,7 @@ public void testSha256HashSigning() throws FormatException {
},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.PUBLIC);

Expand Down Expand Up @@ -196,7 +196,7 @@ public void testToBase64EnvelopeXdrBuilderNoSignatures() throws FormatException,
},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);

Expand Down Expand Up @@ -277,7 +277,7 @@ public void testConstructorWithSorobanData() throws IOException {
new org.stellar.sdk.operations.Operation[] {invokeHostFunctionOperation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
sorobanData,
Network.TESTNET);

Expand Down Expand Up @@ -313,7 +313,7 @@ public void testIsSorobanTransactionInvokeHostFunctionOperation() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);
assertTrue(transaction.isSorobanTransaction());
Expand Down Expand Up @@ -350,7 +350,7 @@ public void testIsSorobanTransactionExtendFootprintTTLOperation() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
sorobanData,
Network.TESTNET);
assertTrue(transaction.isSorobanTransaction());
Expand Down Expand Up @@ -386,7 +386,7 @@ public void testIsSorobanTransactionRestoreFootprintOperation() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
sorobanData,
Network.TESTNET);
assertTrue(transaction.isSorobanTransaction());
Expand Down Expand Up @@ -415,7 +415,7 @@ public void testIsSorobanTransactionMultiOperations() {
new org.stellar.sdk.operations.Operation[] {operation, operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);
assertFalse(transaction.isSorobanTransaction());
Expand All @@ -437,7 +437,7 @@ public void testIsSorobanTransactionBumpSequenceOperation() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);
assertFalse(transaction.isSorobanTransaction());
Expand All @@ -459,7 +459,7 @@ public void testHashCodeAndEquals() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);

Expand All @@ -472,7 +472,7 @@ public void testHashCodeAndEquals() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);
assertEquals(transaction1.hashCode(), transaction2.hashCode());
Expand All @@ -487,7 +487,7 @@ public void testHashCodeAndEquals() {
new org.stellar.sdk.operations.Operation[] {operation},
Memo.text("not equal tx"),
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.TESTNET);
assertNotEquals(transaction1, transaction3);
Expand All @@ -501,7 +501,7 @@ public void testHashCodeAndEquals() {
new org.stellar.sdk.operations.Operation[] {operation},
null,
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>(), null),
null, null, null, BigInteger.ZERO, 0, new ArrayList<SignerKey>()),
null,
Network.PUBLIC);
assertNotEquals(transaction1, transaction4);
Expand Down
Loading