From 4103a92052917dc4eafbe8e0a62a310c6ff2b07c Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Wed, 2 Aug 2023 16:29:33 +0800 Subject: [PATCH] Correct the data type of certain fields to store the expected design values. --- .../org/stellar/sdk/AllowTrustOperation.java | 10 ++- .../sdk/BumpFootprintExpirationOperation.java | 11 +-- .../java/org/stellar/sdk/LedgerBounds.java | 13 +-- src/main/java/org/stellar/sdk/Memo.java | 15 +++- src/main/java/org/stellar/sdk/MemoId.java | 24 ++++-- src/main/java/org/stellar/sdk/Predicate.java | 15 ++-- .../java/org/stellar/sdk/Sep10Challenge.java | 14 ++-- .../org/stellar/sdk/SetOptionsOperation.java | 28 +++---- .../sdk/SetTrustlineFlagsOperation.java | 6 +- src/main/java/org/stellar/sdk/StrKey.java | 21 +++-- src/main/java/org/stellar/sdk/TimeBounds.java | 46 +++++++---- .../java/org/stellar/sdk/Transaction.java | 11 +-- .../org/stellar/sdk/TransactionBuilder.java | 41 +++++++--- .../stellar/sdk/TransactionPreconditions.java | 47 +++++++---- .../sdk/responses/PredicateDeserializer.java | 13 ++- .../responses/TransactionDeserializer.java | 3 +- .../stellar/sdk/xdr/AuthenticatedMessage.java | 7 +- src/main/java/org/stellar/sdk/xdr/Uint32.java | 12 +-- src/main/java/org/stellar/sdk/xdr/Uint64.java | 12 +-- .../sdk/xdr/XdrUnsignedHyperInteger.java | 77 ++++++++++++++++++ .../stellar/sdk/xdr/XdrUnsignedInteger.java | 65 +++++++++++++++ .../BumpFootprintExpirationOperationTest.java | 36 ++++++--- .../stellar/sdk/ClaimableBalanceIdTest.java | 3 +- .../sdk/InvokeHostFunctionOperationTest.java | 3 +- src/test/java/org/stellar/sdk/MemoTest.java | 13 +-- .../java/org/stellar/sdk/OperationTest.java | 4 +- .../org/stellar/sdk/Sep10ChallengeTest.java | 4 +- .../org/stellar/sdk/SorobanServerTest.java | 11 +-- src/test/java/org/stellar/sdk/StrKeyTest.java | 4 +- .../java/org/stellar/sdk/TimeBoundsTest.java | 27 ++++--- .../stellar/sdk/TransactionBuilderTest.java | 80 +++++++++++++------ .../sdk/TransactionPreconditionsTest.java | 42 +++++----- .../java/org/stellar/sdk/TransactionTest.java | 24 +++--- src/test/java/org/stellar/sdk/UtilTest.java | 8 +- .../sdk/xdr/AccountEntryDecodeTest.java | 20 +++-- 35 files changed, 534 insertions(+), 236 deletions(-) create mode 100644 src/main/java/org/stellar/sdk/xdr/XdrUnsignedHyperInteger.java create mode 100644 src/main/java/org/stellar/sdk/xdr/XdrUnsignedInteger.java diff --git a/src/main/java/org/stellar/sdk/AllowTrustOperation.java b/src/main/java/org/stellar/sdk/AllowTrustOperation.java index 53312fe9e..2ef0e823c 100644 --- a/src/main/java/org/stellar/sdk/AllowTrustOperation.java +++ b/src/main/java/org/stellar/sdk/AllowTrustOperation.java @@ -69,11 +69,13 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc Uint32 flag = new Uint32(); // authorize if (authorize) { - flag.setUint32(TrustLineFlags.AUTHORIZED_FLAG.getValue()); + flag.setUint32(new XdrUnsignedInteger(TrustLineFlags.AUTHORIZED_FLAG.getValue())); } else if (authorizeToMaintainLiabilities) { - flag.setUint32(TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue()); + flag.setUint32( + new XdrUnsignedInteger( + TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue())); } else { - flag.setUint32(0); + flag.setUint32(new XdrUnsignedInteger(0)); } op.setAuthorize(flag); @@ -110,7 +112,7 @@ public static class Builder { throw new RuntimeException("Unknown asset code"); } - int flag = op.getAuthorize().getUint32().intValue(); + int flag = op.getAuthorize().getUint32().getNumber().intValue(); if (flag == TrustLineFlags.AUTHORIZED_FLAG.getValue()) { authorize = true; authorizeToMaintainLiabilities = false; diff --git a/src/main/java/org/stellar/sdk/BumpFootprintExpirationOperation.java b/src/main/java/org/stellar/sdk/BumpFootprintExpirationOperation.java index 4949a5ea5..489f9a85c 100644 --- a/src/main/java/org/stellar/sdk/BumpFootprintExpirationOperation.java +++ b/src/main/java/org/stellar/sdk/BumpFootprintExpirationOperation.java @@ -8,6 +8,7 @@ import org.stellar.sdk.xdr.ExtensionPoint; import org.stellar.sdk.xdr.OperationType; import org.stellar.sdk.xdr.Uint32; +import org.stellar.sdk.xdr.XdrUnsignedInteger; /** * Represents > extends OperationBuilder { - public B ledgersToExpire(Integer ledgersToExpire) { - if (ledgersToExpire <= 0) { + public B ledgersToExpire(Long ledgersToExpire) { + if (ledgersToExpire <= 0 || ledgersToExpire > 0xFFFFFFFFL) { throw new IllegalArgumentException("ledgersToExpire isn't a ledger quantity (uint32)"); } this.ledgersToExpire = ledgersToExpire; diff --git a/src/main/java/org/stellar/sdk/LedgerBounds.java b/src/main/java/org/stellar/sdk/LedgerBounds.java index f9bd6d277..37c75ac15 100644 --- a/src/main/java/org/stellar/sdk/LedgerBounds.java +++ b/src/main/java/org/stellar/sdk/LedgerBounds.java @@ -2,6 +2,7 @@ import lombok.Value; import org.stellar.sdk.xdr.Uint32; +import org.stellar.sdk.xdr.XdrUnsignedInteger; @Value @lombok.Builder @@ -10,20 +11,20 @@ * href="https://github.com/stellar/stellar-protocol/blob/master/core/cap-0021.md#specification">CAP-21 */ public class LedgerBounds { - int minLedger; - int maxLedger; + long minLedger; + long maxLedger; public static LedgerBounds fromXdr(org.stellar.sdk.xdr.LedgerBounds xdrLedgerBounds) { return new LedgerBoundsBuilder() - .minLedger(xdrLedgerBounds.getMinLedger().getUint32()) - .maxLedger(xdrLedgerBounds.getMaxLedger().getUint32()) + .minLedger(xdrLedgerBounds.getMinLedger().getUint32().getNumber()) + .maxLedger(xdrLedgerBounds.getMaxLedger().getUint32().getNumber()) .build(); } public org.stellar.sdk.xdr.LedgerBounds toXdr() { return new org.stellar.sdk.xdr.LedgerBounds.Builder() - .maxLedger(new Uint32(maxLedger)) - .minLedger(new Uint32(minLedger)) + .maxLedger(new Uint32(new XdrUnsignedInteger(maxLedger))) + .minLedger(new Uint32(new XdrUnsignedInteger(minLedger))) .build(); } } diff --git a/src/main/java/org/stellar/sdk/Memo.java b/src/main/java/org/stellar/sdk/Memo.java index 697347995..a5ab82afa 100644 --- a/src/main/java/org/stellar/sdk/Memo.java +++ b/src/main/java/org/stellar/sdk/Memo.java @@ -1,5 +1,7 @@ package org.stellar.sdk; +import java.math.BigInteger; + /** * The memo contains optional extra information. It is the responsibility of the client to interpret * this value. Memos can be one of the following types: @@ -46,7 +48,16 @@ public static MemoText text(byte[] text) { * * @param id */ - public static MemoId id(long id) { + public static MemoId id(BigInteger id) { + return new MemoId(id); + } + + /** + * Creates new {@link MemoId} instance. + * + * @param id + */ + public static MemoId id(Long id) { return new MemoId(id); } @@ -91,7 +102,7 @@ public static Memo fromXdr(org.stellar.sdk.xdr.Memo memo) { case MEMO_NONE: return none(); case MEMO_ID: - return id(memo.getId().getUint64().longValue()); + return id(memo.getId().getUint64().getNumber()); case MEMO_TEXT: return text(memo.getText().getBytes()); case MEMO_HASH: diff --git a/src/main/java/org/stellar/sdk/MemoId.java b/src/main/java/org/stellar/sdk/MemoId.java index f7b0de8ca..f702f2029 100644 --- a/src/main/java/org/stellar/sdk/MemoId.java +++ b/src/main/java/org/stellar/sdk/MemoId.java @@ -1,19 +1,28 @@ package org.stellar.sdk; import com.google.common.base.Objects; -import com.google.common.primitives.UnsignedLongs; +import java.math.BigInteger; import org.stellar.sdk.xdr.MemoType; import org.stellar.sdk.xdr.Uint64; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; /** Represents MEMO_ID. */ public class MemoId extends Memo { - private long id; + private final BigInteger id; - public MemoId(long id) { + public MemoId(BigInteger id) { + if (id.compareTo(XdrUnsignedHyperInteger.MIN_VALUE) < 0 + || id.compareTo(XdrUnsignedHyperInteger.MAX_VALUE) > 0) { + throw new IllegalArgumentException("MEMO_ID must be between 0 and 2^64-1"); + } this.id = id; } - public long getId() { + public MemoId(Long id) { + this(BigInteger.valueOf(id)); + } + + public BigInteger getId() { return id; } @@ -21,8 +30,7 @@ public long getId() { org.stellar.sdk.xdr.Memo toXdr() { org.stellar.sdk.xdr.Memo memo = new org.stellar.sdk.xdr.Memo(); memo.setDiscriminant(MemoType.MEMO_ID); - Uint64 idXdr = new Uint64(); - idXdr.setUint64(id); + Uint64 idXdr = new Uint64(new XdrUnsignedHyperInteger(id)); memo.setId(idXdr); return memo; } @@ -37,11 +45,11 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MemoId memoId = (MemoId) o; - return id == memoId.id; + return Objects.equal(id, memoId.id); } @Override public String toString() { - return UnsignedLongs.toString(this.id); + return id.toString(); } } diff --git a/src/main/java/org/stellar/sdk/Predicate.java b/src/main/java/org/stellar/sdk/Predicate.java index 4928061c2..460388159 100644 --- a/src/main/java/org/stellar/sdk/Predicate.java +++ b/src/main/java/org/stellar/sdk/Predicate.java @@ -9,6 +9,7 @@ import org.stellar.sdk.xdr.Int64; import org.stellar.sdk.xdr.TimePoint; import org.stellar.sdk.xdr.Uint64; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; import org.threeten.bp.Instant; public abstract class Predicate { @@ -186,15 +187,15 @@ public AbsBefore(TimePoint timePoint) { } public AbsBefore(long epochSeconds) { - this(new TimePoint(new Uint64(epochSeconds))); + this(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(epochSeconds)))); } public long getTimestampSeconds() { - return timePoint.getTimePoint().getUint64(); + return timePoint.getTimePoint().getUint64().getNumber().longValue(); } public Instant getDate() { - return Instant.ofEpochSecond(timePoint.getTimePoint().getUint64()); + return Instant.ofEpochSecond(timePoint.getTimePoint().getUint64().getNumber().longValue()); } @Override @@ -214,7 +215,7 @@ public int hashCode() { public ClaimPredicate toXdr() { org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate(); xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_BEFORE_ABSOLUTE_TIME); - xdr.setAbsBefore(new Int64(timePoint.getTimePoint().getUint64())); + xdr.setAbsBefore(new Int64(timePoint.getTimePoint().getUint64().getNumber().longValue())); return xdr; } } @@ -228,11 +229,11 @@ public RelBefore(Duration secondsSinceClose) { } public RelBefore(long secondsSinceClose) { - this(new Duration(new Uint64(secondsSinceClose))); + this(new Duration(new Uint64(new XdrUnsignedHyperInteger(secondsSinceClose)))); } public long getSecondsSinceClose() { - return duration.getDuration().getUint64(); + return duration.getDuration().getUint64().getNumber().longValue(); } @Override @@ -252,7 +253,7 @@ public int hashCode() { public ClaimPredicate toXdr() { org.stellar.sdk.xdr.ClaimPredicate xdr = new org.stellar.sdk.xdr.ClaimPredicate(); xdr.setDiscriminant(ClaimPredicateType.CLAIM_PREDICATE_BEFORE_RELATIVE_TIME); - xdr.setRelBefore(new Int64(duration.getDuration().getUint64())); + xdr.setRelBefore(new Int64(duration.getDuration().getUint64().getNumber().longValue())); return xdr; } } diff --git a/src/main/java/org/stellar/sdk/Sep10Challenge.java b/src/main/java/org/stellar/sdk/Sep10Challenge.java index 567df3de3..be86cd019 100644 --- a/src/main/java/org/stellar/sdk/Sep10Challenge.java +++ b/src/main/java/org/stellar/sdk/Sep10Challenge.java @@ -6,6 +6,7 @@ import com.google.common.collect.Multimap; import com.google.common.io.BaseEncoding; import java.io.IOException; +import java.math.BigInteger; import java.security.SecureRandom; import java.util.Arrays; import java.util.Collections; @@ -18,7 +19,7 @@ import org.stellar.sdk.xdr.SignatureHint; public class Sep10Challenge { - static final int GRACE_PERIOD_SECONDS = 5 * 60; + static final BigInteger GRACE_PERIOD_SECONDS = BigInteger.valueOf(5 * 60); static final String CLIENT_DOMAIN_DATA_NAME = "client_domain"; private static final String HOME_DOMAIN_MANAGER_DATA_NAME_FLAG = "auth"; private static final String WEB_AUTH_DOMAIN_MANAGER_DATA_NAME = "web_auth_domain"; @@ -247,14 +248,15 @@ public static ChallengeTransaction readChallengeTransaction( throw new InvalidSep10ChallengeException("only memo type `id` is supported"); } - long maxTime = transaction.getTimeBounds().getMaxTime(); - long minTime = transaction.getTimeBounds().getMinTime(); - if (maxTime == 0L) { + BigInteger maxTime = transaction.getTimeBounds().getMaxTime(); + BigInteger minTime = transaction.getTimeBounds().getMinTime(); + if (maxTime.equals(BigInteger.ZERO)) { throw new InvalidSep10ChallengeException("Transaction requires non-infinite timebounds."); } - long currentTime = System.currentTimeMillis() / 1000L; - if ((currentTime + GRACE_PERIOD_SECONDS) < minTime || currentTime > maxTime) { + BigInteger currentTime = BigInteger.valueOf(System.currentTimeMillis() / 1000L); + if (currentTime.add(GRACE_PERIOD_SECONDS).compareTo(minTime) < 0 + || currentTime.compareTo(maxTime) > 0) { throw new InvalidSep10ChallengeException( "Transaction is not within range of the specified timebounds."); } diff --git a/src/main/java/org/stellar/sdk/SetOptionsOperation.java b/src/main/java/org/stellar/sdk/SetOptionsOperation.java index b57960d84..0a3e94927 100644 --- a/src/main/java/org/stellar/sdk/SetOptionsOperation.java +++ b/src/main/java/org/stellar/sdk/SetOptionsOperation.java @@ -131,32 +131,32 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc } if (clearFlags != null) { Uint32 clearFlags = new Uint32(); - clearFlags.setUint32(this.clearFlags); + clearFlags.setUint32(new XdrUnsignedInteger(this.clearFlags)); op.setClearFlags(clearFlags); } if (setFlags != null) { Uint32 setFlags = new Uint32(); - setFlags.setUint32(this.setFlags); + setFlags.setUint32(new XdrUnsignedInteger(this.setFlags)); op.setSetFlags(setFlags); } if (masterKeyWeight != null) { Uint32 uint32 = new Uint32(); - uint32.setUint32(masterKeyWeight); + uint32.setUint32(new XdrUnsignedInteger(masterKeyWeight)); op.setMasterWeight(uint32); } if (lowThreshold != null) { Uint32 uint32 = new Uint32(); - uint32.setUint32(lowThreshold); + uint32.setUint32(new XdrUnsignedInteger(lowThreshold)); op.setLowThreshold(uint32); } if (mediumThreshold != null) { Uint32 uint32 = new Uint32(); - uint32.setUint32(mediumThreshold); + uint32.setUint32(new XdrUnsignedInteger(mediumThreshold)); op.setMedThreshold(uint32); } if (highThreshold != null) { Uint32 uint32 = new Uint32(); - uint32.setUint32(highThreshold); + uint32.setUint32(new XdrUnsignedInteger(highThreshold)); op.setHighThreshold(uint32); } if (homeDomain != null) { @@ -167,7 +167,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter acc if (signer != null) { org.stellar.sdk.xdr.Signer signer = new org.stellar.sdk.xdr.Signer(); Uint32 weight = new Uint32(); - weight.setUint32(signerWeight & 0xFF); + weight.setUint32(new XdrUnsignedInteger(signerWeight & 0xFF)); signer.setKey(this.signer); signer.setWeight(weight); op.setSigner(signer); @@ -203,29 +203,29 @@ public static class Builder { inflationDestination = StrKey.encodeStellarAccountId(op.getInflationDest()); } if (op.getClearFlags() != null) { - clearFlags = op.getClearFlags().getUint32(); + clearFlags = op.getClearFlags().getUint32().getNumber().intValue(); } if (op.getSetFlags() != null) { - setFlags = op.getSetFlags().getUint32(); + setFlags = op.getSetFlags().getUint32().getNumber().intValue(); } if (op.getMasterWeight() != null) { - masterKeyWeight = op.getMasterWeight().getUint32(); + masterKeyWeight = op.getMasterWeight().getUint32().getNumber().intValue(); } if (op.getLowThreshold() != null) { - lowThreshold = op.getLowThreshold().getUint32(); + lowThreshold = op.getLowThreshold().getUint32().getNumber().intValue(); } if (op.getMedThreshold() != null) { - mediumThreshold = op.getMedThreshold().getUint32(); + mediumThreshold = op.getMedThreshold().getUint32().getNumber().intValue(); } if (op.getHighThreshold() != null) { - highThreshold = op.getHighThreshold().getUint32(); + highThreshold = op.getHighThreshold().getUint32().getNumber().intValue(); } if (op.getHomeDomain() != null) { homeDomain = op.getHomeDomain().getString32().toString(); } if (op.getSigner() != null) { signer = op.getSigner().getKey(); - signerWeight = op.getSigner().getWeight().getUint32() & 0xFF; + signerWeight = op.getSigner().getWeight().getUint32().getNumber().intValue() & 0xFF; } } diff --git a/src/main/java/org/stellar/sdk/SetTrustlineFlagsOperation.java b/src/main/java/org/stellar/sdk/SetTrustlineFlagsOperation.java index baad5b619..ba959f18e 100644 --- a/src/main/java/org/stellar/sdk/SetTrustlineFlagsOperation.java +++ b/src/main/java/org/stellar/sdk/SetTrustlineFlagsOperation.java @@ -53,7 +53,7 @@ private static Uint32 bitwiseOr(EnumSet set) { v |= f.getValue(); } Uint32 combined = new Uint32(); - combined.setUint32(v); + combined.setUint32(new XdrUnsignedInteger(v)); return combined; } @@ -89,8 +89,8 @@ public static class Builder { Builder(SetTrustLineFlagsOp op) { trustor = StrKey.encodeStellarAccountId(op.getTrustor()); asset = Util.assertNonNativeAsset(Asset.fromXdr(op.getAsset())); - clearFlags = flagSetFromInt(op.getClearFlags().getUint32()); - setFlags = flagSetFromInt(op.getSetFlags().getUint32()); + clearFlags = flagSetFromInt(op.getClearFlags().getUint32().getNumber().intValue()); + setFlags = flagSetFromInt(op.getSetFlags().getUint32().getNumber().intValue()); } private static EnumSet flagSetFromInt(int x) { diff --git a/src/main/java/org/stellar/sdk/StrKey.java b/src/main/java/org/stellar/sdk/StrKey.java index 74041aef2..c6ab224f6 100644 --- a/src/main/java/org/stellar/sdk/StrKey.java +++ b/src/main/java/org/stellar/sdk/StrKey.java @@ -3,7 +3,6 @@ import com.google.common.base.Optional; import com.google.common.io.BaseEncoding; import com.google.common.primitives.Bytes; -import com.google.common.primitives.Longs; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.CharArrayWriter; @@ -20,6 +19,7 @@ import org.stellar.sdk.xdr.Uint64; import org.stellar.sdk.xdr.XdrDataInputStream; import org.stellar.sdk.xdr.XdrDataOutputStream; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; class StrKey { @@ -66,11 +66,7 @@ public static String encodeStellarMuxedAccount(MuxedAccount muxedAccount) { switch (muxedAccount.getDiscriminant()) { case KEY_TYPE_MUXED_ED25519: return String.valueOf( - encodeCheck( - VersionByte.MUXED, - Bytes.concat( - muxedAccount.getMed25519().getEd25519().getUint256(), - Longs.toByteArray(muxedAccount.getMed25519().getId().getUint64())))); + encodeCheck(VersionByte.MUXED, getMuxedEd25519AccountBytes(muxedAccount))); case KEY_TYPE_ED25519: return String.valueOf( encodeCheck(VersionByte.ACCOUNT_ID, muxedAccount.getEd25519().getUint256())); @@ -137,7 +133,7 @@ public static MuxedAccount encodeToXDRMuxedAccount(String data) { MuxedAccount.MuxedAccountMed25519 med = new MuxedAccount.MuxedAccountMed25519(); try { med.setEd25519(Uint256.decode(input)); - med.setId(Uint64.decode(input)); + med.setId(new Uint64(XdrUnsignedHyperInteger.decode(input))); } catch (IOException e) { throw new IllegalArgumentException("invalid address: " + data, e); } @@ -398,4 +394,15 @@ public int getValue() { return value; } } + + private static byte[] getMuxedEd25519AccountBytes(MuxedAccount muxedAccount) { + byte[] accountBytes = muxedAccount.getMed25519().getEd25519().getUint256(); + byte[] idBytes = muxedAccount.getMed25519().getId().getUint64().getNumber().toByteArray(); + byte[] idPaddedBytes = new byte[8]; + int idNumBytesToCopy = Math.min(idBytes.length, 8); + int idCopyStartIndex = idBytes.length - idNumBytesToCopy; + System.arraycopy( + idBytes, idCopyStartIndex, idPaddedBytes, 8 - idNumBytesToCopy, idNumBytesToCopy); + return Bytes.concat(accountBytes, idPaddedBytes); + } } diff --git a/src/main/java/org/stellar/sdk/TimeBounds.java b/src/main/java/org/stellar/sdk/TimeBounds.java index 7dbd9383f..b797c7df6 100644 --- a/src/main/java/org/stellar/sdk/TimeBounds.java +++ b/src/main/java/org/stellar/sdk/TimeBounds.java @@ -1,8 +1,10 @@ package org.stellar.sdk; import com.google.common.base.Objects; +import java.math.BigInteger; import org.stellar.sdk.xdr.TimePoint; import org.stellar.sdk.xdr.Uint64; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; /** * TimeBounds represents the time interval that a transaction is valid. @@ -10,30 +12,41 @@ * @see Transaction */ public final class TimeBounds { - private final long mMinTime; - private final long mMaxTime; + private final BigInteger mMinTime; + private final BigInteger mMaxTime; /** * @param minTime 64bit Unix timestamp * @param maxTime 64bit Unix timestamp */ - public TimeBounds(long minTime, long maxTime) { - if (minTime < 0) { - throw new IllegalArgumentException("minTime cannot be negative"); + public TimeBounds(BigInteger minTime, BigInteger maxTime) { + if (minTime.compareTo(XdrUnsignedHyperInteger.MIN_VALUE) < 0 + || minTime.compareTo(XdrUnsignedHyperInteger.MAX_VALUE) > 0) { + throw new IllegalArgumentException("minTime must be between 0 and 2^64-1"); } - if (maxTime < 0) { - throw new IllegalArgumentException("maxTime cannot be negative"); + if (maxTime.compareTo(XdrUnsignedHyperInteger.MIN_VALUE) < 0 + || maxTime.compareTo(XdrUnsignedHyperInteger.MAX_VALUE) > 0) { + throw new IllegalArgumentException("maxTime must be between 0 and 2^64-1"); } - if (maxTime != TransactionPreconditions.TIMEOUT_INFINITE && minTime > maxTime) { - throw new IllegalArgumentException("minTime must be >= maxTime"); + if (!TransactionPreconditions.TIMEOUT_INFINITE.equals(maxTime) + && minTime.compareTo(maxTime) > 0) { + throw new IllegalArgumentException("minTime must be <= maxTime"); } mMinTime = minTime; mMaxTime = maxTime; } + /** + * @param minTime 64bit Unix timestamp + * @param maxTime 64bit Unix timestamp + */ + public TimeBounds(long minTime, long maxTime) { + this(BigInteger.valueOf(minTime), BigInteger.valueOf(maxTime)); + } + /** * A factory method that sets maxTime to the specified second from now. * @@ -46,11 +59,11 @@ public static TimeBounds expiresAfter(long timeout) { return new TimeBounds(0, endTime); } - public long getMinTime() { + public BigInteger getMinTime() { return mMinTime; } - public long getMaxTime() { + public BigInteger getMaxTime() { return mMaxTime; } @@ -60,8 +73,8 @@ public static TimeBounds fromXdr(org.stellar.sdk.xdr.TimeBounds timeBounds) { } return new TimeBounds( - timeBounds.getMinTime().getTimePoint().getUint64(), - timeBounds.getMaxTime().getTimePoint().getUint64()); + timeBounds.getMinTime().getTimePoint().getUint64().getNumber(), + timeBounds.getMaxTime().getTimePoint().getUint64().getNumber()); } public org.stellar.sdk.xdr.TimeBounds toXdr() { @@ -70,8 +83,8 @@ public org.stellar.sdk.xdr.TimeBounds toXdr() { TimePoint maxTime = new TimePoint(); Uint64 minTimeTemp = new Uint64(); Uint64 maxTimeTemp = new Uint64(); - minTimeTemp.setUint64(mMinTime); - maxTimeTemp.setUint64(mMaxTime); + minTimeTemp.setUint64(new XdrUnsignedHyperInteger(mMinTime)); + maxTimeTemp.setUint64(new XdrUnsignedHyperInteger(mMaxTime)); minTime.setTimePoint(minTimeTemp); maxTime.setTimePoint(maxTimeTemp); timeBounds.setMinTime(minTime); @@ -88,7 +101,8 @@ public boolean equals(Object o) { return false; } TimeBounds that = (TimeBounds) o; - return mMinTime == that.mMinTime && mMaxTime == that.mMaxTime; + return Objects.equal(this.mMaxTime, that.mMaxTime) + && Objects.equal(this.mMinTime, that.mMinTime); } @Override diff --git a/src/main/java/org/stellar/sdk/Transaction.java b/src/main/java/org/stellar/sdk/Transaction.java index 430bef521..2bc6df2ad 100644 --- a/src/main/java/org/stellar/sdk/Transaction.java +++ b/src/main/java/org/stellar/sdk/Transaction.java @@ -24,6 +24,7 @@ import org.stellar.sdk.xdr.TransactionV1Envelope; import org.stellar.sdk.xdr.Uint32; import org.stellar.sdk.xdr.XdrDataOutputStream; +import org.stellar.sdk.xdr.XdrUnsignedInteger; /** * Represents mOperations; - private Integer mBaseFee; + private Long mBaseFee; private Network mNetwork; private TransactionPreconditions mPreconditions; private SorobanTransactionData mSorobanData; @@ -155,25 +157,25 @@ public TransactionBuilder addTimeBounds(TimeBounds timeBounds) { * @deprecated this method will be removed in upcoming releases, use addPreconditions() * with TimeBound set instead for more control over preconditions. */ - public TransactionBuilder setTimeout(long timeout) { + public TransactionBuilder setTimeout(BigInteger timeout) { if (mPreconditions.getTimeBounds() != null - && mPreconditions.getTimeBounds().getMaxTime() != TIMEOUT_INFINITE) { + && !TIMEOUT_INFINITE.equals(mPreconditions.getTimeBounds().getMaxTime())) { throw new RuntimeException( "TimeBounds.max_time has been already set - setting timeout would overwrite it."); } - if (timeout < 0) { + if (timeout.compareTo(BigInteger.ZERO) < 0) { throw new RuntimeException("timeout cannot be negative"); } - long timeoutTimestamp = - timeout != TIMEOUT_INFINITE - ? System.currentTimeMillis() / 1000L + timeout + BigInteger timeoutTimestamp = + !TIMEOUT_INFINITE.equals(timeout) + ? timeout.add(BigInteger.valueOf(System.currentTimeMillis() / 1000L)) : TIMEOUT_INFINITE; TransactionPreconditionsBuilder preconditionsBuilder = mPreconditions.toBuilder(); if (mPreconditions.getTimeBounds() == null) { - preconditionsBuilder.timeBounds(new TimeBounds(0, timeoutTimestamp)); + preconditionsBuilder.timeBounds(new TimeBounds(BigInteger.ZERO, timeoutTimestamp)); } else { preconditionsBuilder.timeBounds( new TimeBounds(mPreconditions.getTimeBounds().getMinTime(), timeoutTimestamp)); @@ -182,7 +184,19 @@ public TransactionBuilder setTimeout(long timeout) { return this; } - public TransactionBuilder setBaseFee(int baseFee) { + /** + * An alias for {@link #setTimeout(BigInteger)} with timeout in seconds. + * + * @param timeout Timeout in seconds. + * @return updated Builder + * @deprecated this method will be removed in upcoming releases, use addPreconditions() + * with TimeBound set instead for more control over preconditions. + */ + public TransactionBuilder setTimeout(long timeout) { + return setTimeout(BigInteger.valueOf(timeout)); + } + + public TransactionBuilder setBaseFee(long baseFee) { if (baseFee < AbstractTransaction.MIN_BASE_FEE) { throw new IllegalArgumentException( "baseFee cannot be smaller than the BASE_FEE (" @@ -195,6 +209,10 @@ public TransactionBuilder setBaseFee(int baseFee) { return this; } + public TransactionBuilder setBaseFee(int baseFee) { + return setBaseFee((long) baseFee); + } + /** * Builds a transaction and increments the sequence number on the source account after transaction * is constructed. @@ -240,10 +258,11 @@ public Long apply(TransactionBuilderAccount sourceAccount) { } }; + @Deprecated public static org.stellar.sdk.xdr.TimeBounds buildTimeBounds(long minTime, long maxTime) { return new org.stellar.sdk.xdr.TimeBounds.Builder() - .minTime(new TimePoint(new Uint64(minTime))) - .maxTime(new TimePoint(new Uint64(maxTime))) + .minTime(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(minTime)))) + .maxTime(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(maxTime)))) .build(); } diff --git a/src/main/java/org/stellar/sdk/TransactionPreconditions.java b/src/main/java/org/stellar/sdk/TransactionPreconditions.java index 6d8b37b8c..e25b7ab4e 100644 --- a/src/main/java/org/stellar/sdk/TransactionPreconditions.java +++ b/src/main/java/org/stellar/sdk/TransactionPreconditions.java @@ -1,5 +1,6 @@ package org.stellar.sdk; +import java.math.BigInteger; import java.util.Arrays; import java.util.List; import lombok.Builder; @@ -15,6 +16,8 @@ import org.stellar.sdk.xdr.SignerKey; import org.stellar.sdk.xdr.Uint32; import org.stellar.sdk.xdr.Uint64; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; +import org.stellar.sdk.xdr.XdrUnsignedInteger; @Value @Builder(toBuilder = true) @@ -24,12 +27,12 @@ */ public class TransactionPreconditions { public static final long MAX_EXTRA_SIGNERS_COUNT = 2; - public static final long TIMEOUT_INFINITE = 0; + public static final BigInteger TIMEOUT_INFINITE = BigInteger.ZERO; LedgerBounds ledgerBounds; - Long minSeqNumber; - long minSeqAge; - int minSeqLedgerGap; + Long minSeqNumber; // int64 + @Builder.Default BigInteger minSeqAge = BigInteger.ZERO; // uint64 + long minSeqLedgerGap; // uint32 @Singular @NonNull List extraSigners; TimeBounds timeBounds; @@ -48,7 +51,7 @@ public void isValid() { public boolean hasV2() { return (ledgerBounds != null || (minSeqLedgerGap > 0) - || (minSeqAge > 0) + || (minSeqAge.compareTo(BigInteger.ZERO) > 0) || minSeqNumber != null || !extraSigners.isEmpty()); } @@ -60,15 +63,28 @@ public static TransactionPreconditions fromXdr(Preconditions preconditions) { if (preconditions.getV2().getTimeBounds() != null) { builder.timeBounds( new TimeBounds( - preconditions.getV2().getTimeBounds().getMinTime().getTimePoint().getUint64(), - preconditions.getV2().getTimeBounds().getMaxTime().getTimePoint().getUint64())); + preconditions + .getV2() + .getTimeBounds() + .getMinTime() + .getTimePoint() + .getUint64() + .getNumber(), + preconditions + .getV2() + .getTimeBounds() + .getMaxTime() + .getTimePoint() + .getUint64() + .getNumber())); } if (preconditions.getV2().getExtraSigners() != null && preconditions.getV2().getExtraSigners().length > 0) { builder.extraSigners(Arrays.asList(preconditions.getV2().getExtraSigners())); } if (preconditions.getV2().getMinSeqAge() != null) { - builder.minSeqAge(preconditions.getV2().getMinSeqAge().getDuration().getUint64()); + builder.minSeqAge( + preconditions.getV2().getMinSeqAge().getDuration().getUint64().getNumber()); } if (preconditions.getV2().getLedgerBounds() != null) { builder.ledgerBounds(LedgerBounds.fromXdr(preconditions.getV2().getLedgerBounds())); @@ -77,14 +93,15 @@ public static TransactionPreconditions fromXdr(Preconditions preconditions) { builder.minSeqNumber(preconditions.getV2().getMinSeqNum().getSequenceNumber().getInt64()); } if (preconditions.getV2().getMinSeqLedgerGap() != null) { - builder.minSeqLedgerGap(preconditions.getV2().getMinSeqLedgerGap().getUint32()); + builder.minSeqLedgerGap( + preconditions.getV2().getMinSeqLedgerGap().getUint32().getNumber().intValue()); } } else { if (preconditions.getTimeBounds() != null) { builder.timeBounds( new TimeBounds( - preconditions.getTimeBounds().getMinTime().getTimePoint().getUint64(), - preconditions.getTimeBounds().getMaxTime().getTimePoint().getUint64())); + preconditions.getTimeBounds().getMinTime().getTimePoint().getUint64().getNumber(), + preconditions.getTimeBounds().getMaxTime().getTimePoint().getUint64().getNumber())); } } @@ -99,20 +116,20 @@ public Preconditions toXdr() { PreconditionsV2.Builder v2Builder = new PreconditionsV2.Builder(); v2Builder.extraSigners(extraSigners.toArray(new SignerKey[] {})); - v2Builder.minSeqAge(new Duration(new Uint64(minSeqAge))); + v2Builder.minSeqAge(new Duration(new Uint64(new XdrUnsignedHyperInteger(minSeqAge)))); if (ledgerBounds != null) { v2Builder.ledgerBounds( new org.stellar.sdk.xdr.LedgerBounds.Builder() - .minLedger(new Uint32(ledgerBounds.getMinLedger())) - .maxLedger(new Uint32(ledgerBounds.getMaxLedger())) + .minLedger(new Uint32(new XdrUnsignedInteger(ledgerBounds.getMinLedger()))) + .maxLedger(new Uint32(new XdrUnsignedInteger(ledgerBounds.getMaxLedger()))) .build()); } if (minSeqNumber != null) { v2Builder.minSeqNum(new SequenceNumber(new Int64(minSeqNumber))); } - v2Builder.minSeqLedgerGap(new Uint32(minSeqLedgerGap)); + v2Builder.minSeqLedgerGap(new Uint32(new XdrUnsignedInteger(minSeqLedgerGap))); if (timeBounds != null) { v2Builder.timeBounds(timeBounds.toXdr()); diff --git a/src/main/java/org/stellar/sdk/responses/PredicateDeserializer.java b/src/main/java/org/stellar/sdk/responses/PredicateDeserializer.java index 103bb395f..27fe89086 100644 --- a/src/main/java/org/stellar/sdk/responses/PredicateDeserializer.java +++ b/src/main/java/org/stellar/sdk/responses/PredicateDeserializer.java @@ -8,6 +8,7 @@ import org.stellar.sdk.xdr.Duration; import org.stellar.sdk.xdr.TimePoint; import org.stellar.sdk.xdr.Uint64; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; import org.threeten.bp.Instant; public class PredicateDeserializer implements JsonDeserializer { @@ -44,15 +45,21 @@ public Predicate deserialize(JsonElement json, Type typeOfT, JsonDeserialization // abs_before date string if (obj.has("abs_before_epoch")) { return new Predicate.AbsBefore( - new TimePoint(new Uint64(obj.get("abs_before_epoch").getAsLong()))); + new TimePoint( + new Uint64( + new XdrUnsignedHyperInteger(obj.get("abs_before_epoch").getAsBigInteger())))); } else if (obj.has("abs_before")) { String formattedDate = obj.get("abs_before").getAsString(); return new Predicate.AbsBefore( - new TimePoint(new Uint64(Instant.parse(formattedDate).getEpochSecond()))); + new TimePoint( + new Uint64( + new XdrUnsignedHyperInteger(Instant.parse(formattedDate).getEpochSecond())))); } if (obj.has("rel_before")) { - return new Predicate.RelBefore(new Duration(new Uint64(obj.get("rel_before").getAsLong()))); + return new Predicate.RelBefore( + new Duration( + new Uint64(new XdrUnsignedHyperInteger(obj.get("rel_before").getAsBigInteger())))); } throw new IllegalArgumentException("Unsupported predicate: " + json.toString()); diff --git a/src/main/java/org/stellar/sdk/responses/TransactionDeserializer.java b/src/main/java/org/stellar/sdk/responses/TransactionDeserializer.java index 67e8e104a..01ceae628 100644 --- a/src/main/java/org/stellar/sdk/responses/TransactionDeserializer.java +++ b/src/main/java/org/stellar/sdk/responses/TransactionDeserializer.java @@ -5,6 +5,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.lang.reflect.Type; +import java.math.BigInteger; import org.stellar.sdk.Memo; import org.stellar.sdk.xdr.TransactionEnvelope; import org.stellar.sdk.xdr.XdrDataInputStream; @@ -81,7 +82,7 @@ public TransactionResponse deserialize( String memoValue = json.getAsJsonObject().get("memo").getAsString(); BaseEncoding base64Encoding = BaseEncoding.base64(); if (memoType.equals("id")) { - memo = Memo.id(Long.parseUnsignedLong(memoValue)); + memo = Memo.id(new BigInteger(memoValue)); } else if (memoType.equals("hash")) { memo = Memo.hash(base64Encoding.decode(memoValue)); } else if (memoType.equals("return")) { diff --git a/src/main/java/org/stellar/sdk/xdr/AuthenticatedMessage.java b/src/main/java/org/stellar/sdk/xdr/AuthenticatedMessage.java index f37fab5f8..7289d31e0 100644 --- a/src/main/java/org/stellar/sdk/xdr/AuthenticatedMessage.java +++ b/src/main/java/org/stellar/sdk/xdr/AuthenticatedMessage.java @@ -72,8 +72,9 @@ public static void encode( throws IOException { // Xdrgen::AST::Identifier // Uint32 - stream.writeInt(encodedAuthenticatedMessage.getDiscriminant().getUint32()); - switch (encodedAuthenticatedMessage.getDiscriminant().getUint32()) { + stream.writeInt( + encodedAuthenticatedMessage.getDiscriminant().getUint32().getNumber().intValue()); + switch (encodedAuthenticatedMessage.getDiscriminant().getUint32().getNumber().intValue()) { case 0: AuthenticatedMessageV0.encode(stream, encodedAuthenticatedMessage.v0); break; @@ -88,7 +89,7 @@ public static AuthenticatedMessage decode(XdrDataInputStream stream) throws IOEx AuthenticatedMessage decodedAuthenticatedMessage = new AuthenticatedMessage(); Uint32 discriminant = Uint32.decode(stream); decodedAuthenticatedMessage.setDiscriminant(discriminant); - switch (decodedAuthenticatedMessage.getDiscriminant().getUint32()) { + switch (decodedAuthenticatedMessage.getDiscriminant().getUint32().getNumber().intValue()) { case 0: decodedAuthenticatedMessage.v0 = AuthenticatedMessageV0.decode(stream); break; diff --git a/src/main/java/org/stellar/sdk/xdr/Uint32.java b/src/main/java/org/stellar/sdk/xdr/Uint32.java index 57f564457..8c66f883f 100644 --- a/src/main/java/org/stellar/sdk/xdr/Uint32.java +++ b/src/main/java/org/stellar/sdk/xdr/Uint32.java @@ -14,24 +14,24 @@ // =========================================================================== public class Uint32 implements XdrElement { - private Integer uint32; + private XdrUnsignedInteger uint32; public Uint32() {} - public Uint32(Integer uint32) { + public Uint32(XdrUnsignedInteger uint32) { this.uint32 = uint32; } - public Integer getUint32() { + public XdrUnsignedInteger getUint32() { return this.uint32; } - public void setUint32(Integer value) { + public void setUint32(XdrUnsignedInteger value) { this.uint32 = value; } public static void encode(XdrDataOutputStream stream, Uint32 encodedUint32) throws IOException { - stream.writeInt(encodedUint32.uint32); + encodedUint32.uint32.encode(stream); } public void encode(XdrDataOutputStream stream) throws IOException { @@ -40,7 +40,7 @@ public void encode(XdrDataOutputStream stream) throws IOException { public static Uint32 decode(XdrDataInputStream stream) throws IOException { Uint32 decodedUint32 = new Uint32(); - decodedUint32.uint32 = stream.readInt(); + decodedUint32.uint32 = XdrUnsignedInteger.decode(stream); return decodedUint32; } diff --git a/src/main/java/org/stellar/sdk/xdr/Uint64.java b/src/main/java/org/stellar/sdk/xdr/Uint64.java index b0ee52e74..819c554d6 100644 --- a/src/main/java/org/stellar/sdk/xdr/Uint64.java +++ b/src/main/java/org/stellar/sdk/xdr/Uint64.java @@ -14,24 +14,24 @@ // =========================================================================== public class Uint64 implements XdrElement { - private Long uint64; + private XdrUnsignedHyperInteger uint64; public Uint64() {} - public Uint64(Long uint64) { + public Uint64(XdrUnsignedHyperInteger uint64) { this.uint64 = uint64; } - public Long getUint64() { + public XdrUnsignedHyperInteger getUint64() { return this.uint64; } - public void setUint64(Long value) { + public void setUint64(XdrUnsignedHyperInteger value) { this.uint64 = value; } public static void encode(XdrDataOutputStream stream, Uint64 encodedUint64) throws IOException { - stream.writeLong(encodedUint64.uint64); + encodedUint64.uint64.encode(stream); } public void encode(XdrDataOutputStream stream) throws IOException { @@ -40,7 +40,7 @@ public void encode(XdrDataOutputStream stream) throws IOException { public static Uint64 decode(XdrDataInputStream stream) throws IOException { Uint64 decodedUint64 = new Uint64(); - decodedUint64.uint64 = stream.readLong(); + decodedUint64.uint64 = XdrUnsignedHyperInteger.decode(stream); return decodedUint64; } diff --git a/src/main/java/org/stellar/sdk/xdr/XdrUnsignedHyperInteger.java b/src/main/java/org/stellar/sdk/xdr/XdrUnsignedHyperInteger.java new file mode 100644 index 000000000..8ef477129 --- /dev/null +++ b/src/main/java/org/stellar/sdk/xdr/XdrUnsignedHyperInteger.java @@ -0,0 +1,77 @@ +package org.stellar.sdk.xdr; + +import com.google.common.base.Objects; +import java.io.IOException; +import java.math.BigInteger; + +/** + * Represents XDR Unsigned Hyper Integer. + * + * @see XDR: External Data + * Representation Standard + */ +public class XdrUnsignedHyperInteger implements XdrElement { + public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); + public static final BigInteger MIN_VALUE = BigInteger.ZERO; + private final BigInteger number; + + public XdrUnsignedHyperInteger(BigInteger number) { + if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { + throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); + } + this.number = number; + } + + public XdrUnsignedHyperInteger(Long number) { + if (number < 0) { + throw new IllegalArgumentException( + "number must be greater than or equal to 0 if you want to construct it from Long"); + } + this.number = BigInteger.valueOf(number); + } + + @Override + public void encode(XdrDataOutputStream stream) throws IOException { + stream.write(getBytes()); + } + + public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { + byte[] bytes = new byte[8]; + stream.readFully(bytes); + BigInteger uint64 = new BigInteger(1, bytes); + return new XdrUnsignedHyperInteger(uint64); + } + + private byte[] getBytes() { + byte[] bytes = number.toByteArray(); + byte[] paddedBytes = new byte[8]; + + int numBytesToCopy = Math.min(bytes.length, 8); + int copyStartIndex = bytes.length - numBytesToCopy; + System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); + return paddedBytes; + } + + public BigInteger getNumber() { + return number; + } + + @Override + public int hashCode() { + return Objects.hashCode(this.number); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof XdrUnsignedHyperInteger)) { + return false; + } + + XdrUnsignedHyperInteger other = (XdrUnsignedHyperInteger) object; + return Objects.equal(this.number, other.number); + } + + public String toString() { + return "XdrUnsignedInteger(number=" + this.getNumber() + ")"; + } +} diff --git a/src/main/java/org/stellar/sdk/xdr/XdrUnsignedInteger.java b/src/main/java/org/stellar/sdk/xdr/XdrUnsignedInteger.java new file mode 100644 index 000000000..b717ccd6e --- /dev/null +++ b/src/main/java/org/stellar/sdk/xdr/XdrUnsignedInteger.java @@ -0,0 +1,65 @@ +package org.stellar.sdk.xdr; + +import com.google.common.base.Objects; +import java.io.IOException; + +/** + * Represents XDR Unsigned Integer. + * + * @see XDR: External Data + * Representation Standard + */ +public class XdrUnsignedInteger implements XdrElement { + public static final long MAX_VALUE = (1L << 32) - 1; + public static final long MIN_VALUE = 0; + private final Long number; + + public XdrUnsignedInteger(Long number) { + if (number < MIN_VALUE || number > MAX_VALUE) { + throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); + } + this.number = number; + } + + public XdrUnsignedInteger(Integer number) { + if (number < 0) { + throw new IllegalArgumentException( + "number must be greater than or equal to 0 if you want to construct it from Integer"); + } + this.number = number.longValue(); + } + + public Long getNumber() { + return number; + } + + public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { + int intValue = stream.readInt(); + long uint32Value = Integer.toUnsignedLong(intValue); + return new XdrUnsignedInteger(uint32Value); + } + + @Override + public void encode(XdrDataOutputStream stream) throws IOException { + stream.writeInt(number.intValue()); + } + + @Override + public int hashCode() { + return Objects.hashCode(this.number); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof XdrUnsignedInteger)) { + return false; + } + + XdrUnsignedInteger other = (XdrUnsignedInteger) object; + return Objects.equal(this.number, other.number); + } + + public String toString() { + return "XdrUnsignedInteger(number=" + this.getNumber() + ")"; + } +} diff --git a/src/test/java/org/stellar/sdk/BumpFootprintExpirationOperationTest.java b/src/test/java/org/stellar/sdk/BumpFootprintExpirationOperationTest.java index 21411c5df..1319103f9 100644 --- a/src/test/java/org/stellar/sdk/BumpFootprintExpirationOperationTest.java +++ b/src/test/java/org/stellar/sdk/BumpFootprintExpirationOperationTest.java @@ -12,10 +12,10 @@ public void testBuilderWithSource() { String source = "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW"; BumpFootprintExpirationOperation op = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(123) + .ledgersToExpire(123L) .sourceAccount(source) .build(); - assertEquals(Integer.valueOf(123), op.getLedgersToExpire()); + assertEquals(Long.valueOf(123), op.getLedgersToExpire()); assertEquals(source, op.getSourceAccount()); String expectXdr = "AAAAAQAAAAAk4TTtavBWsGnEN3KxHw4Ohwi22ZJHWi8hlamN5pm0TgAAABkAAAAAAAAAew=="; assertEquals(expectXdr, op.toXdrBase64()); @@ -24,8 +24,8 @@ public void testBuilderWithSource() { @Test public void testBuilderWithoutSource() { BumpFootprintExpirationOperation op = - BumpFootprintExpirationOperation.builder().ledgersToExpire(123).build(); - assertEquals(Integer.valueOf(123), op.getLedgersToExpire()); + BumpFootprintExpirationOperation.builder().ledgersToExpire(123L).build(); + assertEquals(Long.valueOf(123), op.getLedgersToExpire()); assertNull(op.getSourceAccount()); String expectXdr = "AAAAAAAAABkAAAAAAAAAew=="; assertEquals(expectXdr, op.toXdrBase64()); @@ -36,7 +36,7 @@ public void testFromXdr() { String source = "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW"; BumpFootprintExpirationOperation originOp = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(123) + .ledgersToExpire(123L) .sourceAccount(source) .build(); org.stellar.sdk.xdr.Operation xdrObject = originOp.toXdr(); @@ -49,12 +49,12 @@ public void testEquals() { String source = "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW"; BumpFootprintExpirationOperation operation1 = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(123) + .ledgersToExpire(123L) .sourceAccount(source) .build(); BumpFootprintExpirationOperation operation2 = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(123) + .ledgersToExpire(123L) .sourceAccount(source) .build(); assertEquals(operation1, operation2); @@ -65,12 +65,12 @@ public void testNotEquals() { String source = "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW"; BumpFootprintExpirationOperation operation1 = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(123) + .ledgersToExpire(123L) .sourceAccount(source) .build(); BumpFootprintExpirationOperation operation2 = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(124) + .ledgersToExpire(124L) .sourceAccount(source) .build(); Assert.assertNotEquals(operation1, operation2); @@ -81,18 +81,28 @@ public void testNotEqualsSource() { String source = "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW"; BumpFootprintExpirationOperation operation1 = BumpFootprintExpirationOperation.builder() - .ledgersToExpire(123) + .ledgersToExpire(123L) .sourceAccount(source) .build(); BumpFootprintExpirationOperation operation2 = - BumpFootprintExpirationOperation.builder().ledgersToExpire(123).build(); + BumpFootprintExpirationOperation.builder().ledgersToExpire(123L).build(); Assert.assertNotEquals(operation1, operation2); } @Test - public void testLedgersToExpireIsInvalidThrows() { + public void testLedgersToExpireIsInvalidThrowsLessThanZero() { try { - BumpFootprintExpirationOperation.builder().ledgersToExpire(-1).build(); + BumpFootprintExpirationOperation.builder().ledgersToExpire(-1L).build(); + Assert.fail(); + } catch (IllegalArgumentException e) { + Assert.assertEquals("ledgersToExpire isn't a ledger quantity (uint32)", e.getMessage()); + } + } + + @Test + public void testLedgersToExpireIsInvalidThrowsGreatThanMaxUint32() { + try { + BumpFootprintExpirationOperation.builder().ledgersToExpire(4294967296L).build(); Assert.fail(); } catch (IllegalArgumentException e) { Assert.assertEquals("ledgersToExpire isn't a ledger quantity (uint32)", e.getMessage()); diff --git a/src/test/java/org/stellar/sdk/ClaimableBalanceIdTest.java b/src/test/java/org/stellar/sdk/ClaimableBalanceIdTest.java index 361195308..d9484965c 100644 --- a/src/test/java/org/stellar/sdk/ClaimableBalanceIdTest.java +++ b/src/test/java/org/stellar/sdk/ClaimableBalanceIdTest.java @@ -9,6 +9,7 @@ import org.stellar.sdk.xdr.CryptoKeyType; import org.stellar.sdk.xdr.MuxedAccount; import org.stellar.sdk.xdr.Uint64; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; public class ClaimableBalanceIdTest { @@ -84,7 +85,7 @@ public void testClaimableBalanceIds() throws IOException { muxedAccount.setDiscriminant(CryptoKeyType.KEY_TYPE_MUXED_ED25519); MuxedAccount.MuxedAccountMed25519 med = new MuxedAccount.MuxedAccountMed25519(); med.setEd25519(StrKey.encodeToXDRMuxedAccount(sourceAccount).getEd25519()); - med.setId(new Uint64(41l)); + med.setId(new Uint64(new XdrUnsignedHyperInteger(41l))); muxedAccount.setMed25519(med); transaction = diff --git a/src/test/java/org/stellar/sdk/InvokeHostFunctionOperationTest.java b/src/test/java/org/stellar/sdk/InvokeHostFunctionOperationTest.java index 4e7099e49..c241e7147 100644 --- a/src/test/java/org/stellar/sdk/InvokeHostFunctionOperationTest.java +++ b/src/test/java/org/stellar/sdk/InvokeHostFunctionOperationTest.java @@ -32,6 +32,7 @@ import org.stellar.sdk.xdr.Uint256; import org.stellar.sdk.xdr.Uint32; import org.stellar.sdk.xdr.XdrString; +import org.stellar.sdk.xdr.XdrUnsignedInteger; public class InvokeHostFunctionOperationTest { CreateContractArgs createContractArgs = @@ -215,7 +216,7 @@ public void testNotEqualsAuth() { "GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW") .toSCAddress()) .nonce(new Int64(123123432L)) - .signatureExpirationLedger(new Uint32(10)) + .signatureExpirationLedger(new Uint32(new XdrUnsignedInteger(10))) .signatureArgs(new SCVec(new SCVal[] {})) .build()) .build()) diff --git a/src/test/java/org/stellar/sdk/MemoTest.java b/src/test/java/org/stellar/sdk/MemoTest.java index f67702033..1e5b6ecf9 100644 --- a/src/test/java/org/stellar/sdk/MemoTest.java +++ b/src/test/java/org/stellar/sdk/MemoTest.java @@ -9,6 +9,7 @@ import com.google.common.io.BaseEncoding; import com.google.gson.JsonElement; import com.google.gson.JsonParser; +import java.math.BigInteger; import java.util.Arrays; import org.junit.Test; import org.stellar.sdk.responses.TransactionDeserializer; @@ -62,22 +63,22 @@ public void testMemoTextTooLongUtf8() { @Test public void testMemoId() { MemoId memo = Memo.id(9223372036854775807L); - assertEquals(9223372036854775807L, memo.getId()); + assertEquals(BigInteger.valueOf(9223372036854775807L), memo.getId()); assertEquals(MemoType.MEMO_ID, memo.toXdr().getDiscriminant()); - assertEquals(new Long(9223372036854775807L), memo.toXdr().getId().getUint64()); + assertEquals( + BigInteger.valueOf(9223372036854775807L), memo.toXdr().getId().getUint64().getNumber()); assertEquals("9223372036854775807", memo.toString()); } @Test public void testParseMemoId() { - String longId = "10048071741004807174"; + String maxId = "18446744073709551615"; JsonElement element = - new JsonParser() - .parse(String.format("{ \"memo_type\": \"id\", \"memo\": \"%s\" }", longId)); + new JsonParser().parse(String.format("{ \"memo_type\": \"id\", \"memo\": \"%s\" }", maxId)); TransactionResponse transactionResponse = new TransactionDeserializer().deserialize(element, null, null); MemoId memoId = (MemoId) transactionResponse.getMemo(); - assertEquals(longId, Long.toUnsignedString(memoId.getId())); + assertEquals(new BigInteger(maxId), memoId.getId()); } @Test diff --git a/src/test/java/org/stellar/sdk/OperationTest.java b/src/test/java/org/stellar/sdk/OperationTest.java index 6d6f905b0..d34b684fb 100644 --- a/src/test/java/org/stellar/sdk/OperationTest.java +++ b/src/test/java/org/stellar/sdk/OperationTest.java @@ -1329,11 +1329,11 @@ public void testSetTrustlineFlagsOperation() { org.stellar.sdk.xdr.Operation xdr = operation.toXdr(AccountConverter.enableMuxed()); assertEquals( TrustLineFlags.AUTHORIZED_FLAG.getValue(), - xdr.getBody().getSetTrustLineFlagsOp().getClearFlags().getUint32().intValue()); + xdr.getBody().getSetTrustLineFlagsOp().getClearFlags().getUint32().getNumber().intValue()); assertEquals( TrustLineFlags.AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG.getValue() | TrustLineFlags.TRUSTLINE_CLAWBACK_ENABLED_FLAG.getValue(), - xdr.getBody().getSetTrustLineFlagsOp().getSetFlags().getUint32().intValue()); + xdr.getBody().getSetTrustLineFlagsOp().getSetFlags().getUint32().getNumber().intValue()); SetTrustlineFlagsOperation parsedOperation = (SetTrustlineFlagsOperation) Operation.fromXdr(AccountConverter.enableMuxed(), xdr); assertEquals(accountId, parsedOperation.getTrustor()); diff --git a/src/test/java/org/stellar/sdk/Sep10ChallengeTest.java b/src/test/java/org/stellar/sdk/Sep10ChallengeTest.java index 1f928fd3f..3e61df922 100644 --- a/src/test/java/org/stellar/sdk/Sep10ChallengeTest.java +++ b/src/test/java/org/stellar/sdk/Sep10ChallengeTest.java @@ -480,7 +480,7 @@ public void testReadChallengeTransactionInvalidTimeBoundsTooEarly() KeyPair client = KeyPair.random(); long current = System.currentTimeMillis() / 1000L; - long start = current + Sep10Challenge.GRACE_PERIOD_SECONDS + 30; + long start = current + Sep10Challenge.GRACE_PERIOD_SECONDS.longValue() + 30; long end = current + 600; TimeBounds timeBounds = new TimeBounds(start, end); String domainName = "example.com"; @@ -510,7 +510,7 @@ public void testReadChallengeTransactionGracePeriod() KeyPair client = KeyPair.random(); long current = System.currentTimeMillis() / 1000L; - long start = current + Sep10Challenge.GRACE_PERIOD_SECONDS - 30; + long start = current + Sep10Challenge.GRACE_PERIOD_SECONDS.longValue() - 30; long end = current + 600; TimeBounds timeBounds = new TimeBounds(start, end); String domainName = "example.com"; diff --git a/src/test/java/org/stellar/sdk/SorobanServerTest.java b/src/test/java/org/stellar/sdk/SorobanServerTest.java index cd576f103..7aae8ae34 100644 --- a/src/test/java/org/stellar/sdk/SorobanServerTest.java +++ b/src/test/java/org/stellar/sdk/SorobanServerTest.java @@ -70,6 +70,7 @@ import org.stellar.sdk.xdr.XdrDataInputStream; import org.stellar.sdk.xdr.XdrDataOutputStream; import org.stellar.sdk.xdr.XdrString; +import org.stellar.sdk.xdr.XdrUnsignedInteger; public class SorobanServerTest { private final Gson gson = new Gson(); @@ -921,10 +922,10 @@ public void testPrepareTransactionWithSorobanData() .readOnly(new LedgerKey[] {ledgerKey}) .readWrite(new LedgerKey[] {}) .build()) - .extendedMetaDataSizeBytes(new Uint32(216)) - .readBytes(new Uint32(699)) - .writeBytes(new Uint32(0)) - .instructions(new Uint32(34567)) + .extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(216))) + .readBytes(new Uint32(new XdrUnsignedInteger(699))) + .writeBytes(new Uint32(new XdrUnsignedInteger(0))) + .instructions(new Uint32(new XdrUnsignedInteger(34567))) .build()) .refundableFee(new Int64(100L)) .ext(new ExtensionPoint.Builder().discriminant(0).build()) @@ -1382,7 +1383,7 @@ private Transaction buildSorobanTransaction( new Address(opInvokerKp.getAccountId()).toSCVal(), new SCVal.Builder() .discriminant(SCValType.SCV_U32) - .u32(new Uint32(10)) + .u32(new Uint32(new XdrUnsignedInteger(10))) .build() })) .build()) diff --git a/src/test/java/org/stellar/sdk/StrKeyTest.java b/src/test/java/org/stellar/sdk/StrKeyTest.java index 0526241ce..d82b8cf1a 100644 --- a/src/test/java/org/stellar/sdk/StrKeyTest.java +++ b/src/test/java/org/stellar/sdk/StrKeyTest.java @@ -308,7 +308,9 @@ public void testEncodeToXDRMuxedAccountMAddress() { MuxedAccount muxedAccount = StrKey.encodeToXDRMuxedAccount(muxedAddress); assertEquals(CryptoKeyType.KEY_TYPE_MUXED_ED25519, muxedAccount.getDiscriminant()); assertEquals(account.getAccountID().getEd25519(), muxedAccount.getMed25519().getEd25519()); - assertEquals(new Long(-9223372036854775808L), muxedAccount.getMed25519().getId().getUint64()); + assertEquals( + -9223372036854775808L, + muxedAccount.getMed25519().getId().getUint64().getNumber().longValue()); assertEquals(muxedAddress, StrKey.encodeStellarMuxedAccount(muxedAccount)); diff --git a/src/test/java/org/stellar/sdk/TimeBoundsTest.java b/src/test/java/org/stellar/sdk/TimeBoundsTest.java index eaded0cb7..4bbdd40c8 100644 --- a/src/test/java/org/stellar/sdk/TimeBoundsTest.java +++ b/src/test/java/org/stellar/sdk/TimeBoundsTest.java @@ -11,7 +11,7 @@ public void testSetTimeBoundsNegativeMinTime() { new TimeBounds(-1, 300); fail(); } catch (IllegalArgumentException e) { - assertEquals("minTime cannot be negative", e.getMessage()); + assertEquals("minTime must be between 0 and 2^64-1", e.getMessage()); } } @@ -21,7 +21,7 @@ public void testSetTimeBoundsNegativeMaxTime() { new TimeBounds(1, -300); fail(); } catch (IllegalArgumentException e) { - assertEquals("maxTime cannot be negative", e.getMessage()); + assertEquals("maxTime must be between 0 and 2^64-1", e.getMessage()); } } @@ -31,36 +31,36 @@ public void testSetTimeBoundsMinTimeGreatThanMaxTime() { new TimeBounds(300, 1); fail(); } catch (IllegalArgumentException e) { - assertEquals("minTime must be >= maxTime", e.getMessage()); + assertEquals("minTime must be <= maxTime", e.getMessage()); } } @Test public void TestSetTimeoutInfinite() { TimeBounds timebounds = new TimeBounds(300, 0); - assertEquals(300, timebounds.getMinTime()); - assertEquals(0, timebounds.getMaxTime()); + assertEquals(300, timebounds.getMinTime().longValue()); + assertEquals(0, timebounds.getMaxTime().longValue()); } @Test public void TestSetTimeoutInfiniteBothZero() { TimeBounds timebounds = new TimeBounds(0, 0); - assertEquals(0, timebounds.getMinTime()); - assertEquals(0, timebounds.getMaxTime()); + assertEquals(0, timebounds.getMinTime().longValue()); + assertEquals(0, timebounds.getMaxTime().longValue()); } @Test public void TestSetTimeout() { TimeBounds timebounds = new TimeBounds(1, 300); - assertEquals(1, timebounds.getMinTime()); - assertEquals(300, timebounds.getMaxTime()); + assertEquals(1, timebounds.getMinTime().longValue()); + assertEquals(300, timebounds.getMaxTime().longValue()); } @Test public void TestSetTimeoutMinEqualMax() { TimeBounds timebounds = new TimeBounds(300, 300); - assertEquals(300, timebounds.getMinTime()); - assertEquals(300, timebounds.getMaxTime()); + assertEquals(300, timebounds.getMinTime().longValue()); + assertEquals(300, timebounds.getMaxTime().longValue()); } @Test @@ -68,8 +68,9 @@ public void TestTimeoutWithTimeout() { long timeout = 300; TimeBounds timebounds = TimeBounds.expiresAfter(timeout); long now = System.currentTimeMillis() / 1000L; - assertEquals(0, timebounds.getMinTime()); + assertEquals(0, timebounds.getMinTime().longValue()); assertTrue( - timebounds.getMaxTime() - timeout <= now && timebounds.getMaxTime() - timeout >= now - 1); + timebounds.getMaxTime().longValue() - timeout <= now + && timebounds.getMaxTime().longValue() - timeout >= now - 1); } } diff --git a/src/test/java/org/stellar/sdk/TransactionBuilderTest.java b/src/test/java/org/stellar/sdk/TransactionBuilderTest.java index fb1e713d0..2e6ca7f84 100644 --- a/src/test/java/org/stellar/sdk/TransactionBuilderTest.java +++ b/src/test/java/org/stellar/sdk/TransactionBuilderTest.java @@ -6,6 +6,7 @@ import com.google.common.io.BaseEncoding; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.math.BigInteger; import org.junit.Test; import org.stellar.sdk.xdr.*; @@ -155,6 +156,7 @@ public void testBuilderTimeBounds() throws FormatException, IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 42); assertEquals( @@ -166,6 +168,7 @@ public void testBuilderTimeBounds() throws FormatException, IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 1337); @@ -243,8 +246,8 @@ public void testBuilderTimebounds() throws IOException { .setBaseFee(Transaction.MIN_BASE_FEE) .build(); - assertEquals(42, transaction.getTimeBounds().getMinTime()); - assertEquals(1337, transaction.getTimeBounds().getMaxTime()); + assertEquals(42, transaction.getTimeBounds().getMinTime().intValue()); + assertEquals(1337, transaction.getTimeBounds().getMaxTime().intValue()); // Convert transaction to binary XDR and back again to make sure correctly xdr de/serialized. XdrDataInputStream is = @@ -305,8 +308,8 @@ public void testBuilderTimeout() throws IOException { .setBaseFee(Transaction.MIN_BASE_FEE) .build(); - assertEquals(0, transaction.getTimeBounds().getMinTime()); - assertTrue(currentUnix + 10 <= transaction.getTimeBounds().getMaxTime()); + assertEquals(0, transaction.getTimeBounds().getMinTime().longValue()); + assertTrue(currentUnix + 10 <= transaction.getTimeBounds().getMaxTime().longValue()); // Convert transaction to binary XDR and back again to make sure timebounds are correctly // de/serialized. @@ -338,7 +341,8 @@ public void testBuilderSetsLedgerBounds() throws IOException { new CreateAccountOperation.Builder(newAccount.getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) .ledgerBounds(LedgerBounds.builder().minLedger(1).maxLedger(2).build()) .build()) .setBaseFee(Transaction.MIN_BASE_FEE) @@ -377,7 +381,7 @@ public void testBuilderSetsMinSeqNum() throws IOException { SequenceNumber seqNum = new SequenceNumber(); seqNum.setSequenceNumber(new Int64(5L)); preconditionsV2.timeBounds( - TransactionBuilder.buildTimeBounds(0, TransactionPreconditions.TIMEOUT_INFINITE)); + buildTimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)); preconditionsV2.minSeqNum(seqNum); Transaction transaction = @@ -386,7 +390,8 @@ public void testBuilderSetsMinSeqNum() throws IOException { new CreateAccountOperation.Builder(newAccount.getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) .minSeqNumber(5L) .build()) .setBaseFee(Transaction.MIN_BASE_FEE) @@ -427,13 +432,14 @@ public void testBuilderSetsMinSeqAge() throws IOException { new CreateAccountOperation.Builder(newAccount.getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) - .minSeqAge(5L) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) + .minSeqAge(BigInteger.valueOf(5L)) .build()) .setBaseFee(Transaction.MIN_BASE_FEE) .build(); - assertEquals(5, transaction.getPreconditions().getMinSeqAge()); + assertEquals(5, transaction.getPreconditions().getMinSeqAge().intValue()); // Convert transaction to binary XDR and back again to make sure correctly xdr de/serialized. XdrDataInputStream is = @@ -468,7 +474,8 @@ public void testBuilderSetsMinSeqLedgerGap() throws IOException { new CreateAccountOperation.Builder(newAccount.getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) .minSeqLedgerGap(5) .build()) .setBaseFee(Transaction.MIN_BASE_FEE) @@ -525,7 +532,8 @@ public void testBuilderExtraSigners() throws IOException { new CreateAccountOperation.Builder(newAccount.getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) .extraSigners(newArrayList(signerKey)) .minSeqLedgerGap(5) .build()) @@ -571,7 +579,8 @@ public void testBuilderFailsWhenTooManyExtraSigners() throws IOException { new CreateAccountOperation.Builder(KeyPair.random().getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) .extraSigners( newArrayList( new SignerKey.Builder().build(), @@ -599,14 +608,14 @@ public void testBuilderFailsWhenTimeoutLessThanTimeBoundsMinimum() throws Except // set min time to 120 seconds from now .timeBounds( new TimeBounds( - (System.currentTimeMillis() / 1000) + 120, + BigInteger.valueOf((System.currentTimeMillis() / 1000) + 120), TransactionPreconditions.TIMEOUT_INFINITE)) .build()) .setBaseFee(Transaction.MIN_BASE_FEE) .setTimeout(1); // sat max time to 1 second from now fail(); } catch (IllegalArgumentException exception) { - assertTrue(exception.getMessage().contains("minTime must be >= maxTime")); + assertTrue(exception.getMessage().contains("minTime must be <= maxTime")); } } @@ -619,7 +628,8 @@ public void testBuilderUsesAccountSequence() throws IOException { new CreateAccountOperation.Builder(KeyPair.random().getAccountId(), "2000").build()) .addPreconditions( TransactionPreconditions.builder() - .timeBounds(new TimeBounds(0L, TransactionPreconditions.TIMEOUT_INFINITE)) + .timeBounds( + new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE)) .build()) .setBaseFee(Transaction.MIN_BASE_FEE) .build(); @@ -721,6 +731,7 @@ public void testBuilderInfinteTimeoutOnly() throws IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); assertEquals( @@ -732,6 +743,7 @@ public void testBuilderInfinteTimeoutOnly() throws IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); } @@ -773,6 +785,7 @@ public void testBuilderTimeoutOnly() throws IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); assertTrue( @@ -785,6 +798,7 @@ public void testBuilderTimeoutOnly() throws IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue()); } @@ -801,7 +815,8 @@ public void testBuilderTimeoutAndMaxTimeNotSet() throws IOException { new TransactionBuilder(AccountConverter.enableMuxed(), account, Network.TESTNET) .addOperation( new CreateAccountOperation.Builder(destination.getAccountId(), "2000").build()) - .addTimeBounds(new TimeBounds(42, TransactionPreconditions.TIMEOUT_INFINITE)) + .addTimeBounds( + new TimeBounds(BigInteger.valueOf(42), TransactionPreconditions.TIMEOUT_INFINITE)) .setTimeout(10) .setBaseFee(Transaction.MIN_BASE_FEE) .build(); @@ -826,6 +841,7 @@ public void testBuilderTimeoutAndMaxTimeNotSet() throws IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 42); assertTrue( @@ -838,6 +854,7 @@ public void testBuilderTimeoutAndMaxTimeNotSet() throws IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue()); } @@ -854,7 +871,8 @@ public void testBuilderInfinteTimeoutAndMaxTimeNotSet() throws FormatException, new TransactionBuilder(AccountConverter.enableMuxed(), account, Network.TESTNET) .addOperation( new CreateAccountOperation.Builder(destination.getAccountId(), "2000").build()) - .addTimeBounds(new TimeBounds(42, TransactionPreconditions.TIMEOUT_INFINITE)) + .addTimeBounds( + new TimeBounds(BigInteger.valueOf(42), TransactionPreconditions.TIMEOUT_INFINITE)) .setTimeout(TransactionPreconditions.TIMEOUT_INFINITE) .addMemo(Memo.hash(Util.hash("abcdef".getBytes()))) .setBaseFee(100) @@ -880,6 +898,7 @@ public void testBuilderInfinteTimeoutAndMaxTimeNotSet() throws FormatException, .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 42); assertEquals( @@ -891,6 +910,7 @@ public void testBuilderInfinteTimeoutAndMaxTimeNotSet() throws FormatException, .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); } @@ -1018,10 +1038,10 @@ public void voidBuilderSorobanDataXdrString() { .readOnly(new LedgerKey[] {ledgerKey}) .readWrite(new LedgerKey[] {}) .build()) - .extendedMetaDataSizeBytes(new Uint32(216)) - .readBytes(new Uint32(699)) - .writeBytes(new Uint32(0)) - .instructions(new Uint32(34567)) + .extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(216))) + .readBytes(new Uint32(new XdrUnsignedInteger(699))) + .writeBytes(new Uint32(new XdrUnsignedInteger(0))) + .instructions(new Uint32(new XdrUnsignedInteger(34567))) .build()) .refundableFee(new Int64(100L)) .ext(new ExtensionPoint.Builder().discriminant(0).build()) @@ -1074,10 +1094,10 @@ public void voidBuilderSorobanDataXdrObject() { .readOnly(new LedgerKey[] {ledgerKey}) .readWrite(new LedgerKey[] {}) .build()) - .extendedMetaDataSizeBytes(new Uint32(216)) - .readBytes(new Uint32(699)) - .writeBytes(new Uint32(0)) - .instructions(new Uint32(34567)) + .extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(216))) + .readBytes(new Uint32(new XdrUnsignedInteger(699))) + .writeBytes(new Uint32(new XdrUnsignedInteger(0))) + .instructions(new Uint32(new XdrUnsignedInteger(34567))) .build()) .refundableFee(new Int64(100L)) .ext(new ExtensionPoint.Builder().discriminant(0).build()) @@ -1129,4 +1149,12 @@ public void voidBuilderSorobanDataXdrObject() { .build(); assertEquals(expectedExt, transaction.toEnvelopeXdr().getV1().getTx().getExt()); } + + private static org.stellar.sdk.xdr.TimeBounds buildTimeBounds( + BigInteger minTime, BigInteger maxTime) { + return new org.stellar.sdk.xdr.TimeBounds.Builder() + .minTime(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(minTime)))) + .maxTime(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(maxTime)))) + .build(); + } } diff --git a/src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java b/src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java index d66b0b539..81940e1dc 100644 --- a/src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java +++ b/src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java @@ -25,6 +25,8 @@ import org.stellar.sdk.xdr.Uint64; import org.stellar.sdk.xdr.XdrDataInputStream; import org.stellar.sdk.xdr.XdrDataOutputStream; +import org.stellar.sdk.xdr.XdrUnsignedHyperInteger; +import org.stellar.sdk.xdr.XdrUnsignedInteger; public class TransactionPreconditionsTest { @@ -36,14 +38,14 @@ public void itConvertsFromXdr() throws IOException { PreconditionsV2.Builder v2Builder = new PreconditionsV2.Builder(); v2Builder.extraSigners(new SignerKey[] {}); - v2Builder.minSeqAge(new Duration(new Uint64(2L))); + v2Builder.minSeqAge(new Duration(new Uint64(new XdrUnsignedHyperInteger(2L)))); v2Builder.ledgerBounds( new org.stellar.sdk.xdr.LedgerBounds.Builder() - .minLedger(new Uint32(1)) - .maxLedger(new Uint32(2)) + .minLedger(new Uint32(new XdrUnsignedInteger(1))) + .maxLedger(new Uint32(new XdrUnsignedInteger(2))) .build()); v2Builder.minSeqNum(new SequenceNumber(new Int64(4L))); - v2Builder.minSeqLedgerGap(new Uint32(0)); + v2Builder.minSeqLedgerGap(new Uint32(new XdrUnsignedInteger(0))); preconditionsBuilder.v2(v2Builder.build()); Preconditions xdr = preconditionsBuilder.build(); @@ -54,7 +56,7 @@ public void itConvertsFromXdr() throws IOException { TransactionPreconditions transactionPreconditions = TransactionPreconditions.fromXdr(xdr); - assertEquals(transactionPreconditions.getMinSeqAge(), 2); + assertEquals(transactionPreconditions.getMinSeqAge().longValue(), 2); assertEquals(transactionPreconditions.getLedgerBounds().getMinLedger(), 1); assertEquals(transactionPreconditions.getLedgerBounds().getMaxLedger(), 2); assertEquals(transactionPreconditions.getMinSeqNumber(), Long.valueOf(4)); @@ -69,12 +71,12 @@ public void itRoundTripsFromV2ToV1IfOnlyTimeboundsPresent() throws IOException { PreconditionsV2.Builder v2Builder = new PreconditionsV2.Builder(); org.stellar.sdk.xdr.TimeBounds xdrTimeBounds = new org.stellar.sdk.xdr.TimeBounds.Builder() - .minTime(new TimePoint(new Uint64(1L))) - .maxTime(new TimePoint(new Uint64(2L))) + .minTime(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(1L)))) + .maxTime(new TimePoint(new Uint64(new XdrUnsignedHyperInteger(2L)))) .build(); v2Builder.timeBounds(xdrTimeBounds); - v2Builder.minSeqLedgerGap(new Uint32(0)); - v2Builder.minSeqAge(new Duration(new Uint64(0L))); + v2Builder.minSeqLedgerGap(new Uint32(new XdrUnsignedInteger(0))); + v2Builder.minSeqAge(new Duration(new Uint64(new XdrUnsignedHyperInteger(0L)))); v2Builder.extraSigners(new SignerKey[] {}); preconditionsBuilder.v2(v2Builder.build()); // create V2 Precond with just timebounds @@ -135,14 +137,16 @@ public void itConvertsToV2Xdr() throws IOException { assertEquals(xdr.getDiscriminant(), PreconditionType.PRECOND_V2); assertEquals( - xdr.getV2().getTimeBounds().getMinTime().getTimePoint().getUint64(), Long.valueOf(1)); + xdr.getV2().getTimeBounds().getMinTime().getTimePoint().getUint64().getNumber().longValue(), + 1L); assertEquals( - xdr.getV2().getTimeBounds().getMaxTime().getTimePoint().getUint64(), Long.valueOf(2)); + xdr.getV2().getTimeBounds().getMaxTime().getTimePoint().getUint64().getNumber().longValue(), + 2L); assertEquals(xdr.getV2().getMinSeqNum().getSequenceNumber().getInt64(), Long.valueOf(3)); // xdr encoding requires non-null for min ledger gap - assertEquals(xdr.getV2().getMinSeqLedgerGap().getUint32().intValue(), 0); + assertEquals(xdr.getV2().getMinSeqLedgerGap().getUint32().getNumber().intValue(), 0); // xdr encoding requires non-null for min seq age - assertEquals(xdr.getV2().getMinSeqAge().getDuration().getUint64().longValue(), 0); + assertEquals(xdr.getV2().getMinSeqAge().getDuration().getUint64().getNumber().longValue(), 0); assertEquals(xdr.getV2().getExtraSigners().length, 3); } @@ -160,8 +164,10 @@ public void itConvertsOnlyTimeBoundsXdr() throws IOException { Preconditions.decode(new XdrDataInputStream(new ByteArrayInputStream(baos.toByteArray()))); assertEquals(xdr.getDiscriminant(), PreconditionType.PRECOND_TIME); - assertEquals(xdr.getTimeBounds().getMinTime().getTimePoint().getUint64(), Long.valueOf(1)); - assertEquals(xdr.getTimeBounds().getMaxTime().getTimePoint().getUint64(), Long.valueOf(2)); + assertEquals( + xdr.getTimeBounds().getMinTime().getTimePoint().getUint64().getNumber().longValue(), 1L); + assertEquals( + xdr.getTimeBounds().getMaxTime().getTimePoint().getUint64().getNumber().longValue(), 2L); assertNull(xdr.getV2()); } @@ -239,11 +245,11 @@ public void itChecksV2Status() { PreconditionsV2.Builder v2Builder = new PreconditionsV2.Builder(); v2Builder.extraSigners(new SignerKey[] {}); - v2Builder.minSeqAge(new Duration(new Uint64(2L))); + v2Builder.minSeqAge(new Duration(new Uint64(new XdrUnsignedHyperInteger(2L)))); v2Builder.ledgerBounds( new org.stellar.sdk.xdr.LedgerBounds.Builder() - .minLedger(new Uint32(1)) - .maxLedger(new Uint32(2)) + .minLedger(new Uint32(new XdrUnsignedInteger(1))) + .maxLedger(new Uint32(new XdrUnsignedInteger(2))) .build()); v2Builder.minSeqNum(new SequenceNumber(new Int64(4L))); preconditionsBuilder.v2(v2Builder.build()); diff --git a/src/test/java/org/stellar/sdk/TransactionTest.java b/src/test/java/org/stellar/sdk/TransactionTest.java index e8cd7e432..a121ea898 100644 --- a/src/test/java/org/stellar/sdk/TransactionTest.java +++ b/src/test/java/org/stellar/sdk/TransactionTest.java @@ -8,6 +8,7 @@ import com.google.common.io.BaseEncoding; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.math.BigInteger; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Arrays; @@ -37,7 +38,8 @@ public void testParseV0Transaction() throws FormatException, IOException { new CreateAccountOperation.Builder(destination.getAccountId(), "2000").build() }, null, - new TransactionPreconditions(null, null, 0, 0, new ArrayList(), null), + new TransactionPreconditions( + null, null, BigInteger.ZERO, 0, new ArrayList(), null), null, Network.PUBLIC); @@ -84,7 +86,8 @@ public void testAddingSignaturesDirectly() { new CreateAccountOperation.Builder(destination.getAccountId(), "2000").build() }, null, - new TransactionPreconditions(null, null, 0, 0, new ArrayList(), null), + new TransactionPreconditions( + null, null, BigInteger.ZERO, 0, new ArrayList(), null), null, Network.PUBLIC); @@ -122,7 +125,8 @@ public void testSha256HashSigning() throws FormatException { .build() }, null, - new TransactionPreconditions(null, null, 0, 0, new ArrayList(), null), + new TransactionPreconditions( + null, null, BigInteger.ZERO, 0, new ArrayList(), null), null, Network.PUBLIC); @@ -158,7 +162,8 @@ public void testToBase64EnvelopeXdrBuilderNoSignatures() throws FormatException, new CreateAccountOperation.Builder(destination.getAccountId(), "2000").build() }, null, - new TransactionPreconditions(null, null, 0, 0, new ArrayList(), null), + new TransactionPreconditions( + null, null, BigInteger.ZERO, 0, new ArrayList(), null), null, Network.TESTNET); @@ -198,10 +203,10 @@ public void testConstructorWithSorobanData() throws IOException { .readOnly(new LedgerKey[] {ledgerKey}) .readWrite(new LedgerKey[] {}) .build()) - .extendedMetaDataSizeBytes(new Uint32(216)) - .readBytes(new Uint32(699)) - .writeBytes(new Uint32(0)) - .instructions(new Uint32(34567)) + .extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(216))) + .readBytes(new Uint32(new XdrUnsignedInteger(699))) + .writeBytes(new Uint32(new XdrUnsignedInteger(0))) + .instructions(new Uint32(new XdrUnsignedInteger(34567))) .build()) .refundableFee(new Int64(100L)) .ext(new ExtensionPoint.Builder().discriminant(0).build()) @@ -241,7 +246,8 @@ public void testConstructorWithSorobanData() throws IOException { account.getIncrementedSequenceNumber(), new org.stellar.sdk.Operation[] {invokeHostFunctionOperation}, null, - new TransactionPreconditions(null, null, 0, 0, new ArrayList(), null), + new TransactionPreconditions( + null, null, BigInteger.ZERO, 0, new ArrayList(), null), sorobanData, Network.TESTNET); diff --git a/src/test/java/org/stellar/sdk/UtilTest.java b/src/test/java/org/stellar/sdk/UtilTest.java index 1c46dcca1..b362c2e63 100644 --- a/src/test/java/org/stellar/sdk/UtilTest.java +++ b/src/test/java/org/stellar/sdk/UtilTest.java @@ -29,10 +29,10 @@ public void testXdrToSorobanTransactionData() { .readOnly(new LedgerKey[] {ledgerKey}) .readWrite(new LedgerKey[] {}) .build()) - .extendedMetaDataSizeBytes(new Uint32(216)) - .readBytes(new Uint32(699)) - .writeBytes(new Uint32(0)) - .instructions(new Uint32(34567)) + .extendedMetaDataSizeBytes(new Uint32(new XdrUnsignedInteger(216))) + .readBytes(new Uint32(new XdrUnsignedInteger(699))) + .writeBytes(new Uint32(new XdrUnsignedInteger(0))) + .instructions(new Uint32(new XdrUnsignedInteger(34567))) .build()) .refundableFee(new Int64(100L)) .ext(new ExtensionPoint.Builder().discriminant(0).build()) diff --git a/src/test/java/org/stellar/sdk/xdr/AccountEntryDecodeTest.java b/src/test/java/org/stellar/sdk/xdr/AccountEntryDecodeTest.java index 6a55aca23..f4004aef8 100644 --- a/src/test/java/org/stellar/sdk/xdr/AccountEntryDecodeTest.java +++ b/src/test/java/org/stellar/sdk/xdr/AccountEntryDecodeTest.java @@ -23,7 +23,7 @@ public void testDecodeSignerPayload() throws IOException { signerKey.getEd25519SignedPayload().setPayload(new byte[] {1, 2, 3, 4}); signerKey.getEd25519SignedPayload().setEd25519(new Uint256(new byte[32])); signer.setKey(signerKey); - signer.setWeight(new Uint32(1)); + signer.setWeight(new Uint32(new XdrUnsignedInteger(1))); bldr.signers(new Signer[] {signer}); bldr.accountID( new AccountID( @@ -33,8 +33,8 @@ public void testDecodeSignerPayload() throws IOException { .build())); bldr.seqNum(new SequenceNumber(new Int64(1L))); bldr.balance(new Int64(0L)); - bldr.numSubEntries(new Uint32(0)); - bldr.flags(new Uint32(0)); + bldr.numSubEntries(new Uint32(new XdrUnsignedInteger(0))); + bldr.flags(new Uint32(new XdrUnsignedInteger(0))); bldr.homeDomain(new String32(new XdrString(""))); bldr.thresholds(new Thresholds(new byte[3])); bldr.ext( @@ -52,8 +52,8 @@ public void testDecodeSignerPayload() throws IOException { .discriminant(2) .v2( new AccountEntryExtensionV2.Builder() - .numSponsored(new Uint32(0)) - .numSponsoring(new Uint32(0)) + .numSponsored(new Uint32(new XdrUnsignedInteger(0))) + .numSponsoring(new Uint32(new XdrUnsignedInteger(0))) .signerSponsoringIDs(new SponsorshipDescriptor[] {}) .ext( new AccountEntryExtensionV2.AccountEntryExtensionV2Ext @@ -61,8 +61,12 @@ public void testDecodeSignerPayload() throws IOException { .discriminant(3) .v3( new AccountEntryExtensionV3.Builder() - .seqLedger(new Uint32(1)) - .seqTime(new TimePoint(new Uint64(2L))) + .seqLedger( + new Uint32(new XdrUnsignedInteger(1))) + .seqTime( + new TimePoint( + new Uint64( + new XdrUnsignedHyperInteger(2L)))) .ext( new ExtensionPoint.Builder() .discriminant(0) @@ -102,6 +106,7 @@ public void testDecodeSignerPayload() throws IOException { .getV3() .getSeqLedger() .getUint32() + .getNumber() .longValue()); assertEquals( 2L, @@ -115,6 +120,7 @@ public void testDecodeSignerPayload() throws IOException { .getSeqTime() .getTimePoint() .getUint64() + .getNumber() .longValue()); } }