From cf1c19981028f5a4fb09764f1acd5737f3c671d0 Mon Sep 17 00:00:00 2001 From: overcat <4catcode@gmail.com> Date: Wed, 2 Aug 2023 16:29:33 +0800 Subject: [PATCH] Fix the representation of UnsignedInteger and UnsignedHyperInteger in XDR. --- .../org/stellar/sdk/AllowTrustOperation.java | 10 ++- .../sdk/BumpFootprintExpirationOperation.java | 11 +-- .../java/org/stellar/sdk/LedgerBounds.java | 11 ++- src/main/java/org/stellar/sdk/Memo.java | 2 +- src/main/java/org/stellar/sdk/MemoId.java | 3 +- src/main/java/org/stellar/sdk/Predicate.java | 15 ++-- .../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 | 11 ++- .../java/org/stellar/sdk/Transaction.java | 11 +-- .../org/stellar/sdk/TransactionBuilder.java | 5 +- .../stellar/sdk/TransactionPreconditions.java | 52 ++++++++++--- .../sdk/responses/PredicateDeserializer.java | 13 +++- .../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 | 2 +- src/test/java/org/stellar/sdk/MemoTest.java | 2 +- .../java/org/stellar/sdk/OperationTest.java | 4 +- src/test/java/org/stellar/sdk/StrKeyTest.java | 4 +- .../stellar/sdk/TransactionBuilderTest.java | 26 +++++-- .../sdk/TransactionPreconditionsTest.java | 40 ++++++---- .../java/org/stellar/sdk/TransactionTest.java | 8 +- src/test/java/org/stellar/sdk/UtilTest.java | 8 +- .../sdk/xdr/AccountEntryDecodeTest.java | 20 +++-- 30 files changed, 381 insertions(+), 144 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..109d20ab8 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,22 @@ * href="https://github.com/stellar/stellar-protocol/blob/master/core/cap-0021.md#specification">CAP-21 */ public class LedgerBounds { + // TODO: minLedger and maxLedger are defined as uint32 in XDR, so we need to modify it here to + // long. int minLedger; int 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().intValue()) + .maxLedger(xdrLedgerBounds.getMaxLedger().getUint32().getNumber().intValue()) .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..560beda35 100644 --- a/src/main/java/org/stellar/sdk/Memo.java +++ b/src/main/java/org/stellar/sdk/Memo.java @@ -91,7 +91,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().longValue()); 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..5268fa806 100644 --- a/src/main/java/org/stellar/sdk/MemoId.java +++ b/src/main/java/org/stellar/sdk/MemoId.java @@ -4,6 +4,7 @@ import com.google.common.primitives.UnsignedLongs; 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 { @@ -22,7 +23,7 @@ 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); + idXdr.setUint64(new XdrUnsignedHyperInteger(id)); memo.setId(idXdr); return memo; } 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/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..9c416d648 100644 --- a/src/main/java/org/stellar/sdk/TimeBounds.java +++ b/src/main/java/org/stellar/sdk/TimeBounds.java @@ -3,6 +3,7 @@ import com.google.common.base.Objects; 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,6 +11,8 @@ * @see Transaction */ public final class TimeBounds { + // TODO: In XDR, maxTime and minTime are defined as uint64 types, so we need to modify them here + // to BigInteger. private final long mMinTime; private final long mMaxTime; @@ -60,8 +63,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().longValue(), + timeBounds.getMaxTime().getTimePoint().getUint64().getNumber().longValue()); } public org.stellar.sdk.xdr.TimeBounds toXdr() { @@ -70,8 +73,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); diff --git a/src/main/java/org/stellar/sdk/Transaction.java b/src/main/java/org/stellar/sdk/Transaction.java index fe3409013..e8b199f55 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 extraSigners; TimeBounds timeBounds; @@ -60,15 +64,30 @@ 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() + .longValue(), + preconditions + .getV2() + .getTimeBounds() + .getMaxTime() + .getTimePoint() + .getUint64() + .getNumber() + .longValue())); } 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().longValue()); } if (preconditions.getV2().getLedgerBounds() != null) { builder.ledgerBounds(LedgerBounds.fromXdr(preconditions.getV2().getLedgerBounds())); @@ -77,14 +96,27 @@ 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() + .longValue(), + preconditions + .getTimeBounds() + .getMaxTime() + .getTimePoint() + .getUint64() + .getNumber() + .longValue())); } } @@ -99,20 +131,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..13983e9cc 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").getAsLong())))); } 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").getAsLong())))); } throw new IllegalArgumentException("Unsupported predicate: " + json.toString()); 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 7b08fe9dd..84c1e376e 100644 --- a/src/test/java/org/stellar/sdk/InvokeHostFunctionOperationTest.java +++ b/src/test/java/org/stellar/sdk/InvokeHostFunctionOperationTest.java @@ -188,7 +188,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..d5b373da9 100644 --- a/src/test/java/org/stellar/sdk/MemoTest.java +++ b/src/test/java/org/stellar/sdk/MemoTest.java @@ -64,7 +64,7 @@ public void testMemoId() { MemoId memo = Memo.id(9223372036854775807L); assertEquals(9223372036854775807L, memo.getId()); assertEquals(MemoType.MEMO_ID, memo.toXdr().getDiscriminant()); - assertEquals(new Long(9223372036854775807L), memo.toXdr().getId().getUint64()); + assertEquals(9223372036854775807L, memo.toXdr().getId().getUint64().getNumber().longValue()); assertEquals("9223372036854775807", memo.toString()); } 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/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/TransactionBuilderTest.java b/src/test/java/org/stellar/sdk/TransactionBuilderTest.java index fb1e713d0..5486d4004 100644 --- a/src/test/java/org/stellar/sdk/TransactionBuilderTest.java +++ b/src/test/java/org/stellar/sdk/TransactionBuilderTest.java @@ -155,6 +155,7 @@ public void testBuilderTimeBounds() throws FormatException, IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 42); assertEquals( @@ -166,6 +167,7 @@ public void testBuilderTimeBounds() throws FormatException, IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 1337); @@ -721,6 +723,7 @@ public void testBuilderInfinteTimeoutOnly() throws IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); assertEquals( @@ -732,6 +735,7 @@ public void testBuilderInfinteTimeoutOnly() throws IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); } @@ -773,6 +777,7 @@ public void testBuilderTimeoutOnly() throws IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); assertTrue( @@ -785,6 +790,7 @@ public void testBuilderTimeoutOnly() throws IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue()); } @@ -826,6 +832,7 @@ public void testBuilderTimeoutAndMaxTimeNotSet() throws IOException { .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 42); assertTrue( @@ -838,6 +845,7 @@ public void testBuilderTimeoutAndMaxTimeNotSet() throws IOException { .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue()); } @@ -880,6 +888,7 @@ public void testBuilderInfinteTimeoutAndMaxTimeNotSet() throws FormatException, .getMinTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 42); assertEquals( @@ -891,6 +900,7 @@ public void testBuilderInfinteTimeoutAndMaxTimeNotSet() throws FormatException, .getMaxTime() .getTimePoint() .getUint64() + .getNumber() .longValue(), 0); } @@ -1018,10 +1028,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 +1084,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()) diff --git a/src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java b/src/test/java/org/stellar/sdk/TransactionPreconditionsTest.java index d66b0b539..144ca11e5 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(); @@ -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..28b56cc4c 100644 --- a/src/test/java/org/stellar/sdk/TransactionTest.java +++ b/src/test/java/org/stellar/sdk/TransactionTest.java @@ -198,10 +198,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()) 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()); } }