use of com.hedera.services.bdd.suites.HapiApiSuite.ONE_HUNDRED_HBARS in project hedera-services by hashgraph.
the class UtilVerbs method updateSpecialFile.
public static HapiSpecOperation updateSpecialFile(final String payer, final String fileName, final ByteString contents, final int bytesPerOp, final int appendsPerBurst) {
return withOpContext((spec, opLog) -> {
final var bytesToUpload = contents.size();
final var bytesToAppend = bytesToUpload - bytesPerOp;
final var appendsRequired = bytesToAppend / bytesPerOp + Math.min(1, bytesToAppend % bytesPerOp);
COMMON_MESSAGES.info("Beginning update for " + fileName + " (" + appendsRequired + " appends required)");
final var numBursts = appendsRequired / appendsPerBurst + Math.min(1, appendsRequired % appendsPerBurst);
int position = Math.min(bytesPerOp, bytesToUpload);
final var updateSubOp = fileUpdate(fileName).fee(ONE_HUNDRED_HBARS).contents(contents.substring(0, position)).alertingPre(fid -> COMMON_MESSAGES.info("Submitting initial update for file 0.0." + fid.getFileNum())).alertingPost(code -> COMMON_MESSAGES.info("Finished initial update with " + code)).noLogging().payingWith(payer).signedBy(payer);
allRunFor(spec, updateSubOp);
final AtomicInteger burstNo = new AtomicInteger(1);
while (position < bytesToUpload) {
final var totalBytesLeft = bytesToUpload - position;
final var appendsLeft = totalBytesLeft / bytesPerOp + Math.min(1, totalBytesLeft % bytesPerOp);
final var appendsHere = new AtomicInteger(Math.min(appendsPerBurst, appendsLeft));
boolean isFirstAppend = true;
final List<HapiSpecOperation> theBurst = new ArrayList<>();
final CountDownLatch burstLatch = new CountDownLatch(1);
final AtomicReference<Instant> burstStart = new AtomicReference<>();
while (appendsHere.getAndDecrement() >= 0) {
final var bytesLeft = bytesToUpload - position;
final var bytesThisAppend = Math.min(bytesLeft, bytesPerOp);
final var newPosition = position + bytesThisAppend;
final var appendSubOp = fileAppend(fileName).content(contents.substring(position, newPosition).toByteArray()).fee(ONE_HUNDRED_HBARS).noLogging().payingWith(payer).signedBy(payer).deferStatusResolution();
if (isFirstAppend) {
final var fixedBurstNo = burstNo.get();
final var fixedAppendsHere = appendsHere.get() + 1;
appendSubOp.alertingPre(fid -> {
burstStart.set(Instant.now());
COMMON_MESSAGES.info("Starting burst " + fixedBurstNo + "/" + numBursts + " (" + fixedAppendsHere + " ops)");
});
isFirstAppend = false;
}
if (appendsHere.get() < 0) {
final var fixedBurstNo = burstNo.get();
appendSubOp.alertingPost(code -> {
final var burstSecs = Duration.between(burstStart.get(), Instant.now()).getSeconds();
COMMON_MESSAGES.info("Completed burst #" + fixedBurstNo + "/" + numBursts + " in " + burstSecs + "s with " + code);
burstLatch.countDown();
});
}
theBurst.add(appendSubOp);
position = newPosition;
}
allRunFor(spec, theBurst);
burstLatch.await();
burstNo.getAndIncrement();
}
});
}
Aggregations