SHA256
105 lines
5.7 KiB
Java
105 lines
5.7 KiB
Java
package server.archive;
|
|
|
|
import blockchain.Ans104DataItem;
|
|
import blockchain.ShineProtocolTags;
|
|
import utils.config.AppConfig;
|
|
|
|
import java.nio.file.Path;
|
|
import java.util.Locale;
|
|
|
|
/** Configuration of per-user-block ANS-104 Arweave/Turbo transport. */
|
|
public record ArweaveBlocksConfig(
|
|
PublishMode publishMode,
|
|
int publishIntervalMinutes,
|
|
int publishMaxItems,
|
|
long publishMaxBundleBytes,
|
|
String publishGateway,
|
|
Path walletJwkPath,
|
|
int minConfirmations,
|
|
int confirmPollSeconds,
|
|
int confirmTimeoutMinutes,
|
|
String turboUploadUrl,
|
|
String turboPaidByAddress,
|
|
Path turboWalletJwkPath,
|
|
boolean syncEnabled,
|
|
int syncIntervalMinutes,
|
|
int syncPageSize,
|
|
int syncQueueBatchSize,
|
|
long syncStartBlockHeight,
|
|
long syncMaxDataItemBytes,
|
|
String syncGateway,
|
|
String syncTurboApiBase,
|
|
int syncLocationRetrySeconds
|
|
) {
|
|
public enum PublishMode {
|
|
TURBO,
|
|
ARWEAVE,
|
|
NONE;
|
|
|
|
static PublishMode parse(String value) {
|
|
String normalized = value == null ? "none" : value.trim().toLowerCase(Locale.ROOT);
|
|
return switch (normalized) {
|
|
case "turbo" -> TURBO;
|
|
case "arweave" -> ARWEAVE;
|
|
case "none", "" -> NONE;
|
|
default -> throw new IllegalArgumentException(
|
|
"arweave.blocks.publish.mode must be turbo, arweave or none; got: " + value);
|
|
};
|
|
}
|
|
}
|
|
|
|
public static final String TEST_TAG_NAME = ShineProtocolTags.APP_TAG_NAME;
|
|
public static final String TEST_TAG_VALUE = ShineProtocolTags.CORE_NAMESPACE;
|
|
public static final String CHANNEL_TAG_NAME = ShineProtocolTags.CHANNEL_TAG_NAME;
|
|
|
|
public static ArweaveBlocksConfig load() {
|
|
AppConfig c = AppConfig.getInstance();
|
|
long maxDataItemBytes = parseLong(c.getParam("arweave.blocks.sync.maxDataItemBytes"), Ans104DataItem.MAX_DATA_ITEM_BYTES);
|
|
if (maxDataItemBytes > Ans104DataItem.MAX_DATA_ITEM_BYTES) maxDataItemBytes = Ans104DataItem.MAX_DATA_ITEM_BYTES;
|
|
return new ArweaveBlocksConfig(
|
|
PublishMode.parse(c.getParam("arweave.blocks.publish.mode")),
|
|
positive(c.getInt("arweave.blocks.publish.intervalMinutes", 15), "publish.intervalMinutes"),
|
|
positive(c.getInt("arweave.blocks.publish.maxItems", 10_000), "publish.maxItems"),
|
|
positiveLong(parseLong(c.getParam("arweave.blocks.publish.maxBundleBytes"), 128L * 1024 * 1024), "publish.maxBundleBytes"),
|
|
orDefault(c.getParam("arweave.blocks.publish.gateway"), "https://arweave.net"),
|
|
optionalPath(c.getParam("arweave.blocks.publish.walletJwkPath")),
|
|
nonNegative(c.getInt("arweave.blocks.publish.minConfirmations", 0), "publish.minConfirmations"),
|
|
positive(c.getInt("arweave.blocks.publish.confirmPollSeconds", 30), "publish.confirmPollSeconds"),
|
|
positive(c.getInt("arweave.blocks.publish.confirmTimeoutMinutes", 180), "publish.confirmTimeoutMinutes"),
|
|
orDefault(c.getParam("arweave.blocks.publish.turbo.uploadUrl"), "https://turbo.ardrive.io/tx"),
|
|
blankToNull(c.getParam("arweave.blocks.publish.turbo.paidByAddress")),
|
|
optionalPath(c.getParam("arweave.blocks.publish.turbo.walletJwkPath")),
|
|
c.getBoolean("arweave.blocks.sync.enabled", false),
|
|
positive(c.getInt("arweave.blocks.sync.intervalMinutes", 15), "sync.intervalMinutes"),
|
|
clamp(c.getInt("arweave.blocks.sync.pageSize", 100), 1, 100),
|
|
positive(c.getInt("arweave.blocks.sync.queueBatchSize", 10_000), "sync.queueBatchSize"),
|
|
nonNegativeLong(parseLong(c.getParam("arweave.blocks.sync.startBlockHeight"), 0L), "sync.startBlockHeight"),
|
|
positiveLong(maxDataItemBytes, "sync.maxDataItemBytes"),
|
|
orDefault(c.getParam("arweave.blocks.sync.gateway"), "https://turbo-gateway.com"),
|
|
orDefault(c.getParam("arweave.blocks.sync.turboApiBase"), "https://turbo.ardrive.io"),
|
|
positive(c.getInt("arweave.blocks.sync.locationRetrySeconds", 30), "sync.locationRetrySeconds")
|
|
);
|
|
}
|
|
|
|
public boolean publishEnabled() { return publishMode != PublishMode.NONE; }
|
|
|
|
public void validatePublisher() {
|
|
if (publishMode == PublishMode.ARWEAVE) {
|
|
throw new IllegalArgumentException("CORE/CONTENT user-block publication requires mode=turbo (or none)");
|
|
}
|
|
if (publishMode == PublishMode.TURBO && (turboUploadUrl == null || turboUploadUrl.isBlank())) {
|
|
throw new IllegalArgumentException("arweave.blocks.publish.turbo.uploadUrl is required for mode=turbo");
|
|
}
|
|
}
|
|
|
|
private static String orDefault(String v, String d) { return v == null || v.isBlank() ? d : v.trim(); }
|
|
private static String blankToNull(String v) { return v == null || v.isBlank() ? null : v.trim(); }
|
|
private static Path optionalPath(String v) { return v == null || v.isBlank() ? null : Path.of(v.trim()); }
|
|
private static long parseLong(String v, long d) { return v == null || v.isBlank() ? d : Long.parseLong(v.trim()); }
|
|
private static int positive(int v,String n){if(v<=0)throw new IllegalArgumentException(n+" must be >0");return v;}
|
|
private static int nonNegative(int v,String n){if(v<0)throw new IllegalArgumentException(n+" must be >=0");return v;}
|
|
private static long positiveLong(long v,String n){if(v<=0)throw new IllegalArgumentException(n+" must be >0");return v;}
|
|
private static long nonNegativeLong(long v,String n){if(v<0)throw new IllegalArgumentException(n+" must be >=0");return v;}
|
|
private static int clamp(int v,int min,int max){return Math.max(min,Math.min(max,v));}
|
|
}
|