Skip to content

Commit

Permalink
Fix long overflow during missing entities migration (#385)
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Sheehy <steven.sheehy@hedera.com>
  • Loading branch information
steven-sheehy authored and mike-burrage-hedera committed Nov 18, 2019
1 parent 18303a0 commit f7403b2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ private void updateAccount(String line) throws Exception {
Entities entity = entityExists.orElseGet(() -> toEntity(accountID));

if (entity.getExpiryTimeNs() == null && accountInfo.hasExpirationTime()) {
entity.setExpiryTimeNs(Utility.timeStampInNanos(accountInfo.getExpirationTime()));
try {
entity.setExpiryTimeNs(Utility.timeStampInNanos(accountInfo.getExpirationTime()));
} catch (ArithmeticException e) {
log.warn("Invalid expiration time for account {}: {}", accountID.getAccountNum(), StringUtils.trim(e.getMessage()));
}
}

if (entity.getAutoRenewPeriod() == null && accountInfo.hasAutoRenewPeriod()) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/hedera/mirror/util/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,7 @@ public static Long timeStampInNanos(Timestamp timestamp) {
try {
return Math.addExact(Math.multiplyExact(timestamp.getSeconds(), SCALAR), timestamp.getNanos());
} catch (ArithmeticException e) {
log.error("Long overflow when converting Timestamp to nanos timestamp : {}", timestamp, e);
throw e;
throw new ArithmeticException("Long overflow when converting Timestamp to nanos timestamp: " + timestamp);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.annotation.Resource;
import javax.sql.DataSource;

import com.hederahashgraph.api.proto.java.Timestamp;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.flywaydb.core.api.configuration.Configuration;
Expand Down Expand Up @@ -215,6 +216,17 @@ void corruptFile() throws Exception {
assertThat(entityRepository.count()).isEqualTo(0L);
}

@Test
void longOverflow() throws Exception {
AccountInfo.Builder accountInfo = accountInfo().setExpirationTime(Timestamp.newBuilder().setSeconds(31556889864403199L).build());
write(accountInfo.build());
migration.migrate(new FlywayContext());
assertThat(entityRepository.findAll())
.hasSize(2)
.extracting(Entities::getExpiryTimeNs)
.containsOnlyNulls();
}

private AccountInfo.Builder accountInfo() throws Exception {
return AccountInfo.newBuilder()
.setAccountID(accountId)
Expand Down

0 comments on commit f7403b2

Please sign in to comment.