Skip to content

Commit

Permalink
replace .intValue() to .intValueExact() and Math.toIntValueExact() fo…
Browse files Browse the repository at this point in the history
…r all BigIntegers and Longs
  • Loading branch information
Grigorov-Georgi committed Jan 15, 2025
1 parent 25c7b93 commit 9661198
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/limechain/babe/Authorship.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static Integer getSecondarySlotAuthor(byte[] randomness,
var authorityIndex = rand.mod(authoritiesCount);

if (authorityIndex.compareTo(authoritiesCount) < 0) {
return authorityIndex.intValue();
return authorityIndex.intValueExact();
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void write(ScaleCodecWriter writer, BlockHeader blockHeader, boolean sea
writer.writeUint256(blockHeader.getParentHash().getBytes());
// NOTE: Usage of BlockNumberWriter is intentionally omitted here,
// since we want this to be a compact int, not a var size int
writer.writeCompact(blockHeader.getBlockNumber().intValue());
writer.writeCompact(blockHeader.getBlockNumber().intValueExact());
writer.writeUint256(blockHeader.getStateRoot().getBytes());
writer.writeUint256(blockHeader.getExtrinsicsRoot().getBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public void write(ScaleCodecWriter wrt, BigInteger value) throws IOException {
throw new IllegalArgumentException("Negative values are not supported: " + value);
}
if (blockNumSize > 0) {
wrt.directWrite(value.and(BigInteger.valueOf(255L)).intValue());
wrt.directWrite(value.and(BigInteger.valueOf(255L)).intValueExact());
for (int i = 1; i < blockNumSize; i++) {
wrt.directWrite(value.shiftRight(8 * i).and(BigInteger.valueOf(255L)).intValue());
wrt.directWrite(value.shiftRight(8 * i).and(BigInteger.valueOf(255L)).intValueExact());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/limechain/storage/block/BlockState.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ public List<Hash256> retrieveRangeFromDatabase(final Hash256 startHash, final Bl

BigInteger blocksInRange = endHeader.getBlockNumber()
.subtract(startHeader.getBlockNumber()).add(BigInteger.ONE);
List<Hash256> hashes = new ArrayList<>(blocksInRange.intValue());
List<Hash256> hashes = new ArrayList<>(blocksInRange.intValueExact());

int lastPosition = blocksInRange.intValue() - 1;
int lastPosition = blocksInRange.intValueExact() - 1;

hashes.add(0, startHash);
hashes.add(lastPosition, endHeader.getHash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void start() {

int startNumber = syncState.getLastFinalizedBlockNumber()
.add(BigInteger.ONE)
.intValue();
.intValueExact();

int blocksToFetch = 100;
List<Block> receivedBlocks = requester.requestBlocks(BlockRequestField.ALL, startNumber, blocksToFetch).join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void updateSetData(BigInteger setChangeBlock) {

List<BlockData> response = requester.requestBlockData(
BlockRequestField.ALL,
setChangeBlock.intValue(),
setChangeBlock.intValueExact(),
1
).join();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private static TransactionValidationRequest createScaleValidationRequest(BigInte
Extrinsic transaction) {
TransactionValidationRequest request = new TransactionValidationRequest();

switch (txQueueVersion.intValue()) {
switch (txQueueVersion.intValueExact()) {
case 1 -> request.setTransaction(transaction.getData());
case 2 -> {
request.setSource(source);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/limechain/sync/fullsync/InMemoryDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public List<byte[]> findKeysByPrefix(String prefixSeek, int limit) {

@Override
public DeleteByPrefixResult deleteByPrefix(String prefix, Long limit) {
var keys = this.findKeysByPrefix(prefix, limit.intValue());
var keys = this.findKeysByPrefix(prefix, Math.toIntExact(limit));

int deleted = 0;
for (var key : keys) {
storage.remove(new String(key));
deleted++;
}

boolean allDeleted = this.findKeysByPrefix(prefix, limit.intValue()).isEmpty();
boolean allDeleted = this.findKeysByPrefix(prefix, Math.toIntExact(limit)).isEmpty();

return new DeleteByPrefixResult(deleted, allDeleted);
}
Expand Down

0 comments on commit 9661198

Please sign in to comment.