use of com.mobilecoin.lib.UnsignedLong in project Signal-Android by WhisperSystems.
the class Wallet method tryGetFullLedger.
@WorkerThread
@Nullable
public MobileCoinLedgerWrapper tryGetFullLedger(@Nullable Long minimumBlockIndex) throws IOException {
try {
MobileCoinLedger.Builder builder = MobileCoinLedger.newBuilder();
BigInteger totalUnspent = BigInteger.ZERO;
long highestBlockTimeStamp = 0;
UnsignedLong highestBlockIndex = UnsignedLong.ZERO;
final long asOfTimestamp = System.currentTimeMillis();
AccountSnapshot accountSnapshot = mobileCoinClient.getAccountSnapshot();
final BigInteger minimumTxFee = mobileCoinClient.getOrFetchMinimumTxFee();
if (minimumBlockIndex != null) {
long snapshotBlockIndex = accountSnapshot.getBlockIndex().longValue();
if (snapshotBlockIndex < minimumBlockIndex) {
Log.d(TAG, "Waiting for block index");
return null;
}
}
for (OwnedTxOut txOut : accountSnapshot.getAccountActivity().getAllTxOuts()) {
MobileCoinLedger.OwnedTXO.Builder txoBuilder = MobileCoinLedger.OwnedTXO.newBuilder().setAmount(Uint64Util.bigIntegerToUInt64(txOut.getValue())).setReceivedInBlock(getBlock(txOut.getReceivedBlockIndex(), txOut.getReceivedBlockTimestamp())).setKeyImage(ByteString.copyFrom(txOut.getKeyImage().getData())).setPublicKey(ByteString.copyFrom(txOut.getPublicKey().getKeyBytes()));
if (txOut.getSpentBlockIndex() != null && (minimumBlockIndex == null || txOut.isSpent(UnsignedLong.valueOf(minimumBlockIndex)))) {
txoBuilder.setSpentInBlock(getBlock(txOut.getSpentBlockIndex(), txOut.getSpentBlockTimestamp()));
builder.addSpentTxos(txoBuilder);
} else {
totalUnspent = totalUnspent.add(txOut.getValue());
builder.addUnspentTxos(txoBuilder);
}
if (txOut.getSpentBlockIndex() != null && txOut.getSpentBlockIndex().compareTo(highestBlockIndex) > 0) {
highestBlockIndex = txOut.getSpentBlockIndex();
}
if (txOut.getReceivedBlockIndex().compareTo(highestBlockIndex) > 0) {
highestBlockIndex = txOut.getReceivedBlockIndex();
}
if (txOut.getSpentBlockTimestamp() != null && txOut.getSpentBlockTimestamp().getTime() > highestBlockTimeStamp) {
highestBlockTimeStamp = txOut.getSpentBlockTimestamp().getTime();
}
if (txOut.getReceivedBlockTimestamp() != null && txOut.getReceivedBlockTimestamp().getTime() > highestBlockTimeStamp) {
highestBlockTimeStamp = txOut.getReceivedBlockTimestamp().getTime();
}
}
builder.setBalance(Uint64Util.bigIntegerToUInt64(totalUnspent)).setTransferableBalance(Uint64Util.bigIntegerToUInt64(accountSnapshot.getTransferableAmount(minimumTxFee))).setAsOfTimeStamp(asOfTimestamp).setHighestBlock(MobileCoinLedger.Block.newBuilder().setBlockNumber(highestBlockIndex.longValue()).setTimestamp(highestBlockTimeStamp));
return new MobileCoinLedgerWrapper(builder.build());
} catch (InvalidFogResponse e) {
Log.w(TAG, "Problem getting ledger", e);
throw new IOException(e);
} catch (NetworkException e) {
Log.w(TAG, "Network problem getting ledger", e);
if (e.statusCode == 401) {
Log.d(TAG, "Reauthorizing client");
reauthorizeClient();
}
throw new IOException(e);
} catch (AttestationException e) {
Log.w(TAG, "Attestation problem getting ledger", e);
throw new IOException(e);
} catch (Uint64RangeException e) {
throw new AssertionError(e);
}
}
Aggregations