use of com.hedera.mirror.common.domain.balance.AccountBalance in project hedera-mirror-node by hashgraph.
the class AccountBalanceLineParserV1Test method parse.
@DisplayName("Parse account balance line")
@ParameterizedTest(name = "from \"{0}\"")
@CsvSource(value = { "'0,0,123,700';false;0;123;700", "' 0,0,123,700';false;0;123;700", "'0, 0,123,700';false;0;123;700", "'0,0, 123,700';false;0;123;700", "'0,0,123, 700';false;0;123;700", "'0,0,123,700 ';false;0;123;700", "'1,0,123,700';true;;;", "'x,0,123,700';true;;;", "'0,x,123,700';true;;;", "'0,0,x,700';true;;;", "'0,0,123,a00';true;;;", "'1000000000000000000000000000,0,123,700';true;;;", "'0,1000000000000000000000000000,123,700';true;;;", "'0,0,1000000000000000000000000000,700';true;;;", "'0,0,123,1000000000000000000000000000';true;;;", "'-1,0,123,700';true;;;", "'0,-1,123,700';true;;;", "'0,0,-1,700';true;;;", "'0,0,123,-1';true;;;", "'foobar';true;;;", "'';true;;;", ";true;;;" }, delimiter = ';')
void parse(String line, boolean expectThrow, Long expectedRealm, Long expectedAccount, Long expectedBalance) {
if (!expectThrow) {
AccountBalance accountBalance = parser.parse(line, timestamp);
var id = accountBalance.getId();
assertThat(accountBalance.getBalance()).isEqualTo(expectedBalance);
assertThat(id).isNotNull();
assertThat(id.getAccountId().getShardNum()).isEqualTo(mirrorProperties.getShard());
assertThat(id.getAccountId().getRealmNum()).isEqualTo(expectedRealm);
assertThat(id.getAccountId().getEntityNum()).isEqualTo(expectedAccount);
assertThat(id.getConsensusTimestamp()).isEqualTo(timestamp);
} else {
assertThrows(InvalidDatasetException.class, () -> {
parser.parse(line, timestamp);
});
}
}
use of com.hedera.mirror.common.domain.balance.AccountBalance in project hedera-mirror-node by hashgraph.
the class AccountBalanceRepositoryTest method create.
private AccountBalance create(long consensusTimestamp, int accountNum, long balance, int numberOfTokenBalances) {
AccountBalance.Id id = new AccountBalance.Id();
id.setConsensusTimestamp(consensusTimestamp);
id.setAccountId(EntityId.of(0, 0, accountNum, EntityType.ACCOUNT));
AccountBalance accountBalance = new AccountBalance();
accountBalance.setBalance(balance);
accountBalance.setId(id);
accountBalance.setTokenBalances(createTokenBalances(consensusTimestamp, accountNum, balance, numberOfTokenBalances));
return accountBalanceRepository.save(accountBalance);
}
use of com.hedera.mirror.common.domain.balance.AccountBalance in project hedera-mirror-node by hashgraph.
the class AccountBalanceRepositoryTest method findByConsensusTimestamp.
@Test
void findByConsensusTimestamp() {
AccountBalance accountBalance1 = create(1L, 1, 100, 0);
AccountBalance accountBalance2 = create(1L, 2, 200, 3);
create(2L, 1, 50, 1);
List<AccountBalance> result = accountBalanceRepository.findByIdConsensusTimestamp(1);
result.forEach(ab -> ab.getTokenBalances().sort(Comparator.comparing(o -> o.getId().getTokenId())));
assertThat(result).usingRecursiveFieldByFieldElementComparator().containsExactlyInAnyOrder(accountBalance1, accountBalance2);
}
use of com.hedera.mirror.common.domain.balance.AccountBalance in project hedera-mirror-node by hashgraph.
the class ProtoBalanceFileReader method toFlux.
private Flux<AccountBalance> toFlux(StreamFileData streamFileData) {
return Flux.defer(() -> {
InputStream inputStream = streamFileData.getInputStream();
ExtensionRegistryLite extensionRegistry = ExtensionRegistryLite.getEmptyRegistry();
CodedInputStream input = CodedInputStream.newInstance(inputStream);
AtomicLong consensusTimestamp = new AtomicLong(0L);
UnknownFieldSet.Builder unknownFieldSet = UnknownFieldSet.newBuilder();
return Flux.<AccountBalance>generate(sink -> {
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch(tag) {
case TAG_EOF:
done = true;
break;
case TAG_TIMESTAMP:
Timestamp timestamp = input.readMessage(Timestamp.parser(), extensionRegistry);
consensusTimestamp.set(DomainUtils.timestampInNanosMax(timestamp));
break;
case TAG_BALANCE:
Assert.state(consensusTimestamp.get() > 0, "Missing consensus timestamp)");
var ab = input.readMessage(SingleAccountBalances.parser(), extensionRegistry);
sink.next(toAccountBalance(consensusTimestamp.get(), ab));
return;
default:
log.warn("Unsupported tag: {}", tag);
done = !unknownFieldSet.mergeFieldFrom(tag, input);
}
}
Assert.state(consensusTimestamp.get() > 0, "Missing consensus timestamp)");
sink.complete();
} catch (IOException e) {
sink.error(new StreamFileReaderException(e));
} catch (IllegalStateException e) {
sink.error(new InvalidStreamFileException(e));
}
}).doFinally(s -> IOUtils.closeQuietly(inputStream));
});
}
use of com.hedera.mirror.common.domain.balance.AccountBalance in project hedera-mirror-node by hashgraph.
the class ProtoBalanceFileReader method read.
@Override
public AccountBalanceFile read(StreamFileData streamFileData) {
Instant loadStart = Instant.now();
Flux<AccountBalance> items = toFlux(streamFileData);
long consensusTimestamp = items.map(AccountBalance::getId).map(AccountBalance.Id::getConsensusTimestamp).blockFirst();
try (InputStream inputStream = streamFileData.getInputStream()) {
AccountBalanceFile accountBalanceFile = new AccountBalanceFile();
accountBalanceFile.setBytes(streamFileData.getBytes());
accountBalanceFile.setConsensusTimestamp(consensusTimestamp);
accountBalanceFile.setFileHash(DigestUtils.sha384Hex(inputStream));
accountBalanceFile.setItems(items);
accountBalanceFile.setLoadStart(loadStart.getEpochSecond());
accountBalanceFile.setName(streamFileData.getFilename());
return accountBalanceFile;
} catch (IOException e) {
throw new StreamFileReaderException(e);
}
}
Aggregations