Skip to content

Commit

Permalink
Fix the representation of UnsignedInteger and UnsignedHyperInteger in…
Browse files Browse the repository at this point in the history
… XDR.
  • Loading branch information
overcat committed Aug 3, 2023
1 parent 25dc8a8 commit 12da1c3
Show file tree
Hide file tree
Showing 30 changed files with 381 additions and 144 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/stellar/sdk/AllowTrustOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a
Expand All @@ -28,7 +29,7 @@ public class BumpFootprintExpirationOperation extends Operation {
* the number of ledgers past the LCL (last closed ledger) by which to extend the validity of the
* ledger keys in this transaction
*/
@NonNull Integer ledgersToExpire;
@NonNull Long ledgersToExpire;

/**
* Constructs a new BumpFootprintExpirationOperation object from the XDR representation of the
Expand All @@ -38,15 +39,15 @@ public class BumpFootprintExpirationOperation extends Operation {
*/
public static BumpFootprintExpirationOperation fromXdr(BumpFootprintExpirationOp op) {
return BumpFootprintExpirationOperation.builder()
.ledgersToExpire(op.getLedgersToExpire().getUint32())
.ledgersToExpire(op.getLedgersToExpire().getUint32().getNumber())
.build();
}

@Override
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody(AccountConverter accountConverter) {
BumpFootprintExpirationOp op = new BumpFootprintExpirationOp();
op.setExt(new ExtensionPoint.Builder().discriminant(0).build());
op.setLedgersToExpire(new Uint32(ledgersToExpire));
op.setLedgersToExpire(new Uint32(new XdrUnsignedInteger(ledgersToExpire)));

org.stellar.sdk.xdr.Operation.OperationBody body =
new org.stellar.sdk.xdr.Operation.OperationBody();
Expand All @@ -60,8 +61,8 @@ public abstract static class BumpFootprintExpirationOperationBuilder<
C extends BumpFootprintExpirationOperation,
B extends BumpFootprintExpirationOperationBuilder<C, B>>
extends OperationBuilder<C, B> {
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;
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/stellar/sdk/LedgerBounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Value;
import org.stellar.sdk.xdr.Uint32;
import org.stellar.sdk.xdr.XdrUnsignedInteger;

@Value
@lombok.Builder
Expand All @@ -10,20 +11,22 @@
* href="https://github.com/stellar/stellar-protocol/blob/master/core/cap-0021.md#specification">CAP-21<a/>
*/
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();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/Memo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/stellar/sdk/MemoId.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/stellar/sdk/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
Expand All @@ -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
Expand All @@ -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;
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/stellar/sdk/SetOptionsOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/stellar/sdk/SetTrustlineFlagsOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static Uint32 bitwiseOr(EnumSet<TrustLineFlags> set) {
v |= f.getValue();
}
Uint32 combined = new Uint32();
combined.setUint32(v);
combined.setUint32(new XdrUnsignedInteger(v));
return combined;
}

Expand Down Expand Up @@ -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<TrustLineFlags> flagSetFromInt(int x) {
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/stellar/sdk/StrKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/stellar/sdk/TimeBounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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.
*
* @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;

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 12da1c3

Please sign in to comment.