Skip to content

Commit

Permalink
Util #523
Browse files Browse the repository at this point in the history
  • Loading branch information
straumat committed Mar 15, 2024
1 parent 787641f commit cdf9cca
Show file tree
Hide file tree
Showing 24 changed files with 51 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.bucket4j.Bucket;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.royllo.explorer.core.util.base.BaseProviderService;
import org.royllo.explorer.core.util.base.BaseProvider;
import org.royllo.explorer.core.util.parameters.MempoolParameters;
import org.royllo.explorer.core.util.parameters.OutgoingRateLimitsParameters;
import org.springframework.stereotype.Service;
Expand All @@ -16,7 +16,7 @@
*/
@Service
@RequiredArgsConstructor
public class ProviderTransactionServiceImplementation extends BaseProviderService implements MempoolTransactionService {
public class ProviderTransactionServiceImplementation extends BaseProvider implements MempoolTransactionService {

/** Outgoing rate limits parameters. */
private final OutgoingRateLimitsParameters outgoingRateLimitsParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.NonNull;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.royllo.explorer.core.util.base.BaseProviderService;
import org.royllo.explorer.core.util.base.BaseProvider;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
Expand All @@ -31,7 +31,7 @@
@SuppressWarnings("SpellCheckingInspection")
@Service
@Profile("!s3-storage")
public class LocalFileServiceImplementation extends BaseProviderService implements ContentService {
public class LocalFileServiceImplementation extends BaseProvider implements ContentService {

/** Web server port. */
public static final int WEB_SERVER_PORT = 9093;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.royllo.explorer.core.util.base.BaseProviderService;
import org.royllo.explorer.core.util.base.BaseProvider;
import org.royllo.explorer.core.util.enums.ProofType;
import org.royllo.explorer.core.util.parameters.OutgoingRateLimitsParameters;
import org.royllo.explorer.core.util.parameters.TAPDParameters;
Expand All @@ -28,7 +28,7 @@
@SuppressWarnings("unused")
@Service
@RequiredArgsConstructor
public class TapdServiceImplementation extends BaseProviderService implements TapdService {
public class TapdServiceImplementation extends BaseProvider implements TapdService {

/** Webflux codec maximum size. */
public static final int CODEC_MAXIMUM_SIZE = 32 * 1024 * 1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.Optional;

import static java.util.stream.Collectors.joining;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.TWEAKED_GROUP_KEY_LENGTH;

Expand Down Expand Up @@ -97,7 +97,7 @@ public Page<AssetDTO> queryAssets(@NonNull final String query,

// =============================================================================================================
// ASSET_ID_ALIAS search.
if (cleanedQuery.length() == ASSET_ALIAS_LENGTH) {
if (cleanedQuery.length() == ASSET_ID_ALIAS_LENGTH) {
// If nothing found, we will search on asset id alias.
final Optional<Asset> assetIdAliasSearch = assetRepository.findByAssetIdAlias(cleanedQuery);
if (assetIdAliasSearch.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import org.slf4j.LoggerFactory;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Base.
*/
Expand Down Expand Up @@ -58,7 +59,7 @@ public abstract class Base {
protected static String sha256(final String value) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(value.getBytes(StandardCharsets.UTF_8));
byte[] digest = md.digest(value.getBytes(UTF_8));
return DatatypeConverter.printHexBinary(digest).toLowerCase();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-256 is not available: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
/**
* Base provider service.
*/
public abstract class BaseProviderService extends Base {
public abstract class BaseProvider extends Base {

/** Max attempts. */
private static final int RETRY_MAXIMUM_ATTEMPTS = 5;
private static final int RETRY_MAXIMUM_ATTEMPTS = 3;

/** Duration between retries. */
private static final Duration RETRY_DURATION_BETWEEN_ATTEMPTS = Duration.of(5, SECONDS);
private static final Duration RETRY_DURATION_BETWEEN_ATTEMPTS = Duration.of(10, SECONDS);

/** Default service call retry configuration. */
protected final Retry defaultRetryConfiguration = Retry.backoff(RETRY_MAXIMUM_ATTEMPTS, RETRY_DURATION_BETWEEN_ATTEMPTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public class BitcoinConstants {

/** A TXID is always 32 bytes (64 characters) and hexadecimal. */
public static final int TX_ID_SIZE = 64;
public static final int TX_ID_LENGTH = 64;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class TaprootAssetsConstants {
/** Tweaked group key size is always 33 bytes (66 characters) and hexadecimal. */
public static final int TWEAKED_GROUP_KEY_LENGTH = 66;

/** Asset id size is always 32 bytes (64 characters) and hexadecimal. */
/** Asset id length is always 32 bytes (64 characters) and hexadecimal. */
public static final int ASSET_ID_LENGTH = 64;

/** The length of the asset id alias. */
public static final int ASSET_ALIAS_LENGTH = 8;
public static final int ASSET_ID_ALIAS_LENGTH = 8;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import static java.util.Collections.emptyList;

/**
* String list converter.
*/
@Converter
public final class StringListConverter implements AttributeConverter<List<String>, String> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public enum RequestStatus {
FAILURE;

/**
* Returns the list of status that are finals - Updates with these status cannot be changed.
* Returns the list of opened status (Work to do).
*
* @return list of final status
* @return list of opened status
*/
public static List<RequestStatus> openedStatus() {
return List.of(OPENED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import java.util.Random;

import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_ALIAS_LENGTH;

/**
* Asset mapper decorator.
Expand Down Expand Up @@ -64,9 +64,9 @@ public final AssetDTO mapToAssetDTO(final DecodedProofResponse.DecodedProof sour
* @return random alias
*/
private String getAssetIdAlias() {
StringBuilder assetIdAlias = new StringBuilder(ASSET_ALIAS_LENGTH);
StringBuilder assetIdAlias = new StringBuilder(ASSET_ID_ALIAS_LENGTH);

for (int i = 0; i < ASSET_ALIAS_LENGTH; i++) {
for (int i = 0; i < ASSET_ID_ALIAS_LENGTH; i++) {
int index = random.nextInt(CHARACTERS.length());
assetIdAlias.append(CHARACTERS.charAt(index));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.mapstruct.NullValuePropertyMappingStrategy.IGNORE;

/**
* Bitcoin related mapper.
* Bitcoin mapper.
*/
@Mapper(nullValuePropertyMappingStrategy = IGNORE)
public interface BitcoinMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import static org.mapstruct.NullValuePropertyMappingStrategy.IGNORE;

/**
* Proof related mapper.
* Proof mapper.
*/
@SuppressWarnings("unused")
@Mapper(nullValuePropertyMappingStrategy = IGNORE, uses = {UserMapper.class, AssetMapper.class, BitcoinMapper.class})
@Mapper(nullValuePropertyMappingStrategy = IGNORE,
uses = {UserMapper.class, AssetMapper.class, BitcoinMapper.class})
public interface ProofMapper {

Proof mapToProof(ProofDTO source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.mapstruct.NullValuePropertyMappingStrategy.IGNORE;

/**
* Request related mapper.
* Request mapper.
*/
@SuppressWarnings("unused")
@Mapper(nullValuePropertyMappingStrategy = IGNORE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* Universe server mapper.
*/
@SuppressWarnings("unused")
@Mapper(nullValuePropertyMappingStrategy = IGNORE, uses = {UserMapper.class})
@Mapper(nullValuePropertyMappingStrategy = IGNORE,
uses = {UserMapper.class})
public interface UniverseServerMapper {

@Mapping(target = "createdOn", ignore = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import static org.mapstruct.NullValuePropertyMappingStrategy.IGNORE;

/**
* User related mapper.
* User mapper.
*/
@Mapper(nullValuePropertyMappingStrategy = IGNORE)
public interface UserMapper {
// TODO: specify fields.

User mapToUser(UserDTO source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class Api {
private String baseUrl;

/** TAPD macaroon. */
@NotEmpty(message = "Macaroon is required")
@NotEmpty(message = "TAPD Macaroon is required")
private String macaroon;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Applications.properties management.
* Application properties management.
*/
package org.royllo.explorer.core.util.parameters;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Validator for a transaction output.
* Validator for a server address.
*/
@Target({FIELD})
@Retention(RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.regex.Pattern;

import static org.royllo.explorer.core.util.constants.BitcoinConstants.TX_ID_SIZE;
import static org.royllo.explorer.core.util.constants.BitcoinConstants.TX_ID_LENGTH;

/**
* Transaction output validator.
Expand All @@ -19,7 +19,7 @@ public final boolean isValid(final String value,
// Format is txid:vout where ':' is mandatory, txid is 64 characters and vout is an int superior or equals to 0.
return value != null // Transaction output cannot be null.
&& value.contains(":") // Transaction output has a ':' to separate tx id and vout.
&& value.substring(0, value.indexOf(":")).length() == TX_ID_SIZE // Transaction id size is 64 characters.
&& value.substring(0, value.indexOf(":")).length() == TX_ID_LENGTH // Transaction id size is 64 characters.
&& !value.substring(value.indexOf(":") + 1).isEmpty() // Transaction vout is mandatory.
&& Pattern.matches("[0-9]+[.]?[0-9]*", (value.substring(value.indexOf(":") + 1))) // Transaction vout must be a number
&& Integer.parseInt(value.substring(value.indexOf(":") + 1)) >= 0; // Transaction vout must be superior to zero.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_PORT;
import static org.royllo.explorer.core.util.constants.AnonymousUserConstants.ANONYMOUS_ID;
import static org.royllo.explorer.core.util.constants.AnonymousUserConstants.ANONYMOUS_USER_DTO;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.enums.AssetType.NORMAL;
import static org.royllo.explorer.core.util.enums.FileType.IMAGE;
import static org.royllo.explorer.core.util.enums.FileType.JSON;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void addAsset() {
assertEquals(ANONYMOUS_USER_DTO.getId(), asset1.getCreator().getId());
assertEquals("my asset id", asset1.getAssetId());
assertNotNull(asset1.getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset1.getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset1.getAssetIdAlias().length());
// Genesis.
assertNotNull(asset1.getGenesisPoint());
assertNotNull(asset1.getGenesisPoint().getId());
Expand Down Expand Up @@ -172,7 +172,7 @@ public void addAsset() {
assertEquals(ANONYMOUS_USER_DTO.getId(), asset2.getCreator().getId());
assertEquals("assetId2", asset2.getAssetId());
assertNotNull(asset2.getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset2.getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset2.getAssetIdAlias().length());
// Genesis.
assertNotNull(asset2.getGenesisPoint());
assertNotNull(asset2.getGenesisPoint().getId());
Expand Down Expand Up @@ -218,7 +218,7 @@ public void addAsset() {
assertEquals(assetGroupCount + 1, assetGroupRepository.findAll().size());
assertEquals("assetId3", asset3.getAssetId());
assertNotNull(asset3.getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset3.getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset3.getAssetIdAlias().length());
assertEquals("tweakedGroupKey-1", asset3.getAssetGroup().getTweakedGroupKey());
assertEquals("assetIdSig-1", asset3.getAssetGroup().getAssetWitness());
assertEquals("rawGroupKey-1", asset3.getAssetGroup().getRawGroupKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import static org.royllo.explorer.core.dto.proof.ProofDTO.PROOF_FILE_NAME_EXTENSION;
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_HOST;
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_PORT;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.enums.RequestStatus.OPENED;
import static org.royllo.explorer.core.util.enums.RequestStatus.SUCCESS;
import static org.royllo.test.MempoolData.ROYLLO_NFT_ANCHOR_1_TXID;
Expand Down Expand Up @@ -143,7 +143,7 @@ public void processProof() {
final Optional<AssetDTO> asset = assetService.getAssetByAssetId(ROYLLO_NFT_ASSET_ID);
assertTrue(asset.isPresent());
assertNotNull(asset.get().getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset.get().getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset.get().getAssetIdAlias().length());
assertNotNull(asset.get().getAmount());
assertNotNull(asset.get().getIssuanceDate());
verifyTransaction(bitcoinService.getBitcoinTransactionOutput(ROYLLO_NFT_GENESIS_TXID, ROYLLO_NFT_GENESIS_VOUT).get(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.royllo.explorer.core.dto.proof.ProofDTO.PROOF_FILE_NAME_EXTENSION;
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_HOST;
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_PORT;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.enums.RequestStatus.OPENED;
import static org.royllo.explorer.core.util.enums.RequestStatus.SUCCESS;
import static org.royllo.test.MempoolData.SET_OF_ROYLLO_NFT_ANCHOR_1_TXID;
Expand Down Expand Up @@ -207,15 +207,15 @@ public void processProof() {
final Optional<AssetDTO> asset1 = assetService.getAssetByAssetId(SET_OF_ROYLLO_NFT_1_ASSET_ID);
assertTrue(asset1.isPresent());
assertNotNull(asset1.get().getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset1.get().getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset1.get().getAssetIdAlias().length());
final Optional<AssetDTO> asset2 = assetService.getAssetByAssetId(SET_OF_ROYLLO_NFT_2_ASSET_ID);
assertTrue(asset2.isPresent());
assertNotNull(asset2.get().getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset2.get().getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset2.get().getAssetIdAlias().length());
final Optional<AssetDTO> asset3 = assetService.getAssetByAssetId(SET_OF_ROYLLO_NFT_3_ASSET_ID);
assertTrue(asset3.isPresent());
assertNotNull(asset3.get().getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset3.get().getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset3.get().getAssetIdAlias().length());

verifyTransaction(bitcoinService.getBitcoinTransactionOutput(SET_OF_ROYLLO_NFT_GENESIS_TXID, SET_OF_ROYLLO_NFT_GENESIS_VOUT).get(),
SET_OF_ROYLLO_NFT_GENESIS_TXID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import static org.royllo.explorer.core.dto.proof.ProofDTO.PROOF_FILE_NAME_EXTENSION;
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_HOST;
import static org.royllo.explorer.core.provider.storage.LocalFileServiceImplementation.WEB_SERVER_PORT;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.constants.TaprootAssetsConstants.ASSET_ID_ALIAS_LENGTH;
import static org.royllo.explorer.core.util.enums.RequestStatus.OPENED;
import static org.royllo.explorer.core.util.enums.RequestStatus.SUCCESS;
import static org.royllo.test.MempoolData.UNLIMITED_ROYLLO_COIN_1_ANCHOR_1_GENESIS_VOUT;
Expand Down Expand Up @@ -190,7 +190,7 @@ public void processProof() {
final Optional<AssetDTO> asset1 = assetService.getAssetByAssetId(UNLIMITED_ROYLLO_COIN_1_ASSET_ID);
assertTrue(asset1.isPresent());
assertNotNull(asset1.get().getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset1.get().getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset1.get().getAssetIdAlias().length());
assertNotNull(asset1.get().getAssetGroup());
assertEquals(UNLIMITED_ROYLLO_COIN_TWEAKED_GROUP_KEY, asset1.get().getAssetGroup().getTweakedGroupKey());
assertNotNull(asset1.get().getAmount());
Expand All @@ -199,7 +199,7 @@ public void processProof() {
final Optional<AssetDTO> asset2 = assetService.getAssetByAssetId(UNLIMITED_ROYLLO_COIN_2_ASSET_ID);
assertTrue(asset2.isPresent());
assertNotNull(asset2.get().getAssetIdAlias());
assertEquals(ASSET_ALIAS_LENGTH, asset2.get().getAssetIdAlias().length());
assertEquals(ASSET_ID_ALIAS_LENGTH, asset2.get().getAssetIdAlias().length());
assertNotNull(asset2.get().getAssetGroup());
assertEquals(UNLIMITED_ROYLLO_COIN_TWEAKED_GROUP_KEY, asset2.get().getAssetGroup().getTweakedGroupKey());
assertNotNull(asset2.get().getAmount());
Expand Down

0 comments on commit cdf9cca

Please sign in to comment.