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

Add TransactionBuilder#TransactionBuilder(Transaction) constructor. #567

Merged
merged 2 commits into from
Jan 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ As this project is pre 1.0, breaking changes may happen for minor version bumps.
### Update
* Support resource leeway parameter when simulating Soroban transactions. ([#561](https://github.com/stellar/java-stellar-sdk/pull/561))
* Support for the new, optional `diagnosticEventsXdr` field on the `SorobanServer.sendTransaction` method. ([#564](https://github.com/stellar/java-stellar-sdk/pull/564))
* Add `TransactionBuilder#TransactionBuilder(Transaction)` constructor. ([#567](https://github.com/stellar/java-stellar-sdk/pull/567))

### Breaking changes
* The types of the following fields have changed. ([#560](https://github.com/stellar/java-stellar-sdk/pull/560))
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/stellar/sdk/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static org.stellar.sdk.TransactionPreconditions.TIMEOUT_INFINITE;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -57,6 +59,41 @@ public TransactionBuilder(TransactionBuilderAccount sourceAccount, Network netwo
this(AccountConverter.enableMuxed(), sourceAccount, network);
}

/**
* Construct a new transaction builder from a transaction.
*
* @param transaction the transaction to rebuild
*/
public TransactionBuilder(Transaction transaction) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice, this builder pattern is good way to maintain immutability and convenience!

AbstractTransaction abstractTransaction;
try {
// deep copy
abstractTransaction =
Transaction.fromEnvelopeXdr(
transaction.getAccountConverter(),
transaction.toEnvelopeXdrBase64(),
transaction.getNetwork());
} catch (IOException e) {
// This should never happen
throw new RuntimeException(e);
}

if (!(abstractTransaction instanceof Transaction)) {
// This should never happen
throw new RuntimeException("TransactionBuilder can only be used to rebuild a Transaction");
}
Transaction tx = (Transaction) abstractTransaction;

this.sourceAccount = new Account(tx.getSourceAccount(), tx.getSequenceNumber() - 1);
this.accountConverter = tx.accountConverter;
this.memo = tx.getMemo();
this.operations = Arrays.asList(tx.getOperations());
this.baseFee = tx.getFee() / tx.getOperations().length;
this.network = tx.getNetwork();
this.preconditions = tx.getPreconditions();
this.sorobanData = tx.getSorobanData();
}

public int getOperationsCount() {
return operations.size();
}
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/stellar/sdk/TransactionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
Expand Down Expand Up @@ -940,6 +941,59 @@ public void testNoNetworkSet() throws FormatException {
}
}

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

Account account = new Account(source.getAccountId(), 2908908335136768L);
BumpSequenceOperation operation0 = new BumpSequenceOperation.Builder(1L).build();
BumpSequenceOperation operation1 = new BumpSequenceOperation.Builder(2L).build();
LedgerKey ledgerKey =
new LedgerKey.Builder()
.discriminant(LedgerEntryType.ACCOUNT)
.account(
new LedgerKey.LedgerKeyAccount.Builder()
.accountID(
KeyPair.fromAccountId(
"GB7TAYRUZGE6TVT7NHP5SMIZRNQA6PLM423EYISAOAP3MKYIQMVYP2JO")
.getXdrAccountId())
.build())
.build();
SorobanTransactionData sorobanData =
new SorobanTransactionData.Builder()
.resources(
new SorobanResources.Builder()
.footprint(
new LedgerFootprint.Builder()
.readOnly(new LedgerKey[] {ledgerKey})
.readWrite(new LedgerKey[] {})
.build())
.readBytes(new Uint32(new XdrUnsignedInteger(699)))
.writeBytes(new Uint32(new XdrUnsignedInteger(0)))
.instructions(new Uint32(new XdrUnsignedInteger(34567)))
.build())
.resourceFee(new Int64(100L))
.ext(new ExtensionPoint.Builder().discriminant(0).build())
.build();

Transaction transaction =
new Transaction(
AccountConverter.enableMuxed(),
account.getAccountId(),
980,
account.getIncrementedSequenceNumber(),
new org.stellar.sdk.Operation[] {operation0, operation1},
new MemoText("hello"),
new TransactionPreconditions(
null, null, BigInteger.ZERO, 0, new ArrayList<>(), new TimeBounds(100, 200)),
sorobanData, // For testing purposes, it is impossible to occur in a real environment.
Network.PUBLIC);

TransactionBuilder builder = new TransactionBuilder(transaction);
assertEquals(transaction, builder.build());
}

@Test
public void voidBuilderSorobanDataXdrString() {
KeyPair source =
Expand Down
Loading