SHA256
39 lines
1.8 KiB
Java
39 lines
1.8 KiB
Java
package server.archive;
|
|
|
|
import blockchain.Ans104DataItem;
|
|
import blockchain.BchBlockEntry;
|
|
import blockchain.CoreContentContainer;
|
|
import blockchain.CoreContentTransport;
|
|
import java.time.Instant;
|
|
import java.util.Arrays;
|
|
|
|
/** Thin adapter for the existing archive queues. No signing, state or independent retry. */
|
|
final class ArchiveBlockPair {
|
|
private ArchiveBlockPair() {}
|
|
|
|
@FunctionalInterface interface Upload<R> {
|
|
R upload(byte[] raw, byte[] id) throws Exception;
|
|
}
|
|
@FunctionalInterface interface Download {
|
|
byte[] download(byte[] id) throws Exception;
|
|
}
|
|
|
|
/** Returns CORE acknowledgement only after both uploads succeed. */
|
|
static <R> R publish(byte[] container, byte[] expectedCoreId, Upload<R> uploader) throws Exception {
|
|
BchBlockEntry block = BchBlockEntry.fromCoreContent(container);
|
|
Ans104DataItem core = block.getCoreDataItem(), content = block.getContentDataItem();
|
|
if (!Arrays.equals(core.id32(), expectedCoreId)) throw new IllegalArgumentException("CORE id mismatch");
|
|
uploader.upload(content.rawBytes(), content.id32());
|
|
return uploader.upload(core.rawBytes(), core.id32());
|
|
}
|
|
|
|
/** Download by signed reference, not by search tag or the CORE bundle location. */
|
|
static byte[] download(byte[] coreRaw, byte[] expectedCoreId, Download downloader) throws Exception {
|
|
Ans104DataItem core = new Ans104DataItem(coreRaw);
|
|
var record = CoreContentTransport.verifyCore(core, core.owner32(), expectedCoreId, Instant.now().getEpochSecond());
|
|
Ans104DataItem content = new Ans104DataItem(downloader.download(record.contentDataItemId32()));
|
|
CoreContentTransport.verifyContent(record, content, core.owner32());
|
|
return CoreContentContainer.pack(core, content);
|
|
}
|
|
}
|