Skip to content

Commit

Permalink
core/dto and core/provider review #523
Browse files Browse the repository at this point in the history
  • Loading branch information
straumat committed Mar 12, 2024
1 parent 6b5501d commit 497b8b2
Show file tree
Hide file tree
Showing 29 changed files with 106 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public class Proof extends BaseDomain {
/** Proof type. */
@Enumerated(STRING)
@Column(name = "PROOF_TYPE", updatable = false)
private ProofType proofType;
private ProofType type;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static jakarta.persistence.FetchType.EAGER;

/**
* Request to add proof to royllo database.
* Request to add a proof to royllo database.
*/
@Getter
@Setter
Expand All @@ -38,7 +38,7 @@ public class AddProofRequest extends Request {
/** Proof type. */
@Enumerated(STRING)
@Column(name = "PROOF_TYPE", updatable = false)
private ProofType proofType;
private ProofType type;

/** The asset created/updated by this request. */
@ManyToOne(fetch = EAGER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Entity
@Table(name = "REQUEST")
@Inheritance(strategy = JOINED)
@DiscriminatorColumn(name = "TYPE")
@DiscriminatorColumn(name = "REQUEST_TYPE")
public abstract class Request extends BaseDomain {

/** Unique identifier. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public class User extends BaseDomain {
@Column(name = "USER_ID", nullable = false, updatable = false)
private String userId;

/** Username. */
@Column(name = "USERNAME", nullable = false)
private String username;

/** User role. */
@Enumerated(STRING)
@Column(name = "ROLE", nullable = false)
private UserRole role;

/** Username. */
@Column(name = "USERNAME", nullable = false)
private String username;

/** Profile picture file name. */
@Column(name = "PROFILE_PICTURE_FILE_NAME")
private String profilePictureFileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AssetDTO {
public static final int ASSET_ID_ALIAS_MAX_SIZE = 30;

/** Maximum size of the readme. */
public static final int README_MAX_SIZE = 3000;
public static final int README_MAX_SIZE = 3_000;

/** Unique identifier. */
Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public class ProofDTO {
String proofId;

/** Proof type. */
// TODO Rename to TYPE
@NotNull(message = "{validation.proof.proofType.required}")
ProofType proofType;
ProofType type;

/**
* Returns the proof file name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.royllo.explorer.core.util.enums.ProofType;

/**
* Request to add proof to royllo database.
* Request to add a proof to royllo database.
*/
@Getter
@SuperBuilder
Expand All @@ -22,9 +22,8 @@ public class AddProofRequestDTO extends RequestDTO {
String proof;

/** Proof type. */
// TODO Change to TYPE
@NotNull(message = "{validation.request.type.required}")
ProofType proofType;
ProofType type;

/** The asset created/updated by this request. */
AssetDTO asset;
Expand All @@ -35,7 +34,9 @@ public class AddProofRequestDTO extends RequestDTO {
* @param newAsset new asset
*/
public void setAsset(final AssetDTO newAsset) {
assert asset == null : "You can't update the target asset, it's already set";
if (asset != null) {
throw new IllegalStateException("You can't update the target asset, it's already set");
}
asset = newAsset;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class AddUniverseServerRequestDTO extends RequestDTO {
* @param newUniverseServer new universe server
*/
public void setUniverseServer(final UniverseServerDTO newUniverseServer) {
assert universeServer == null : "You can't update the target universe server, it's already set";
if (universeServer != null) {
throw new IllegalStateException("You can't update the target universe server, it's already set");
}
universeServer = newUniverseServer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ClaimAssetOwnershipRequestDTO extends RequestDTO {
@Override
public final void success() {
super.success();
// For security reasons, we remove the proof with witness from the request once treated.
// For security reasons, we remove the proof from the request once it has been treated.
proofWithWitness = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static org.royllo.explorer.core.util.enums.RequestStatus.SUCCESS;

/**
* User request to update royllo database.
* User request to update royllo data.
*/
@Getter
@SuperBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class UserDTO {
/** Website maximum size. */
private static final int WEBSITE_MAXIMUM_SIZE = 50;

/** Username maximum size - If longer thant this, the username must be shortened. */
/** Username maximum size. */
public static final int USERNAME_MAXIMUM_SIZE = 20;

/** Username preview size - The size of the preview on both ends. */
Expand All @@ -46,16 +46,16 @@ public class UserDTO {
@NotNull(message = "{validation.user.userId.required}")
String userId;

/** User role. */
@NotNull(message = "{validation.user.role.required}")
UserRole role;

/** Username. */
@Setter
@NonFinal
@Pattern(regexp = "^[a-zA-Z0-9_]{3,20}$", message = "{validation.user.username.invalid}")
String username;

/** User role. */
@NotNull(message = "{validation.user.role.required}")
UserRole role;

/** Profile picture file name. */
@Setter
@NonFinal
Expand Down Expand Up @@ -86,7 +86,7 @@ public class UserDTO {
* @return the shortened username
*/
public String getShortenedUsername() {
// If username is too long, cut it.
// If username is too long, make a short version.
if (username != null && username.length() >= USERNAME_MAXIMUM_SIZE) {
return username.substring(0, USERNAME_PREVIEW_SIZE)
+ "..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void postConstruct() {
@Override
@SuppressWarnings("checkstyle:DesignForExtension")
public Mono<GetTransactionResponse> getTransaction(final String txid) {
logger.debug("Calling mempool for transaction {}", txid);
logger.info("Calling mempool for transaction {}", txid);

// Consume a token from the token bucket.
// If a token is not available this method will block until the refill adds one to the bucket.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +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.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
Expand All @@ -25,12 +26,12 @@

/**
* Content service - Local file implementation - Used for local development.
* It also includes a web server to serve content and simulate a s3 CDN.
* It also includes a web server to serve content and simulate a S3 CDN.
*/
@SuppressWarnings("SpellCheckingInspection")
@Service
@Profile("!s3-storage")
public class LocalFileServiceImplementation implements ContentService {
public class LocalFileServiceImplementation extends BaseProviderService implements ContentService {

/** Web server port. */
public static final int WEB_SERVER_PORT = 9093;
Expand Down Expand Up @@ -84,15 +85,15 @@ public LocalFileServiceImplementation() throws DecoderException, IOException {
final String unlimitedRoylloCoin2 = "544150460000000001fd04c55441505000040000000002241f7b079e5bc9aa73cdb388697bab10a79064e540e7507da77c214f589854cd890000000104500000202034a4f2a5fecc95d461212ad94ecb66d0c9ee7ac5fdb43d003100000000000000cf4a3cba30f469954f2fa882aeb881ae88294608a980abddd1595d48f9daf729a77539655f293b19402f697e06cd020000000001011f7b079e5bc9aa73cdb388697bab10a79064e540e7507da77c214f589854cd890100000000ffffffff02e8030000000000002251206f479cbd41e4ffce26c55a28268877fd40b0e64c283c817b875930bed23cea1e2b8216000000000022512078523cac7967c76daecc63e1fce58e7035fa400f8e5516299443dd4b2deebe210140864ccb6887b5dd92e0592b95aa5af1e4c0c82bbb20688a44c1a42a784aab5c5d2d22dbef014b51dfab133c4ea5d24826ddbd857ab8744cf7665492aa3ad45f0f0000000008a205c0f934598d194699c7c789782caabb2442ec4bdd0cba397c9bef4763aab4e3cb925852af91b28490c3f7678303f382a8cd71ede1e12b136779836f78a14e8ddcf5d067675f07d4c4692aa81d552f47e69faa56b63a01d27bc44cb08f8a2036feb5e4fe06516959842e30fa5d75c60a6f5caf8f82ae1d694837f2f587d352471f7b592c21cdb6cf968e27038ad439167cea8d4399a5adc4a6d90797ca27fe6a78190afd0164000100025e1f7b079e5bc9aa73cdb388697bab10a79064e540e7507da77c214f589854cd890000000114756e6c696d69746564526f796c6c6f436f696e3236a5ee53396cf2d45b9f7bfb306ec706bca2e5738d9bbf4cf52e3cd2cf45a31100000000000401000603fd01f50bad01ab01650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034201403c027fc40af5c14afedbbd9e08d61017ed36780f0b92d94ac97bb0f81fc8bf63a7c75c65bf37ee41be7a5287e1da049a8ccea15053ac2c11f0ad6a5000bc06590e020000102102ea041c360a308ee44466ca4e2b07c5692c02a8d01f0720982dcd0642366546ff11210349d60c6689bbbb2ffc9b30b45b96a3d6e5fd5fd01c867344c0054f61050482410c9f0004000000000221024b84c791ddceed288f4c832d047e67f8152ce1af352cc18930902d84bc6bf9130374014900010002203eb8c5d0a5cf2a1c3ffbf1ad097e22260c783d2a2d6222a23647ff6791a6a04004220000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff022700010002220000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0d30012e0004000000010221022300be5ca01e926c7bb7f8e59dc06111bf2d13627e8e28c3a581a632a8437e2e0503040101112d0001000228756e6c696d69746564526f796c6c6f436f696e20656d697373696f6e203220627920526f796c6c6f16040026af10175e1f7b079e5bc9aa73cdb388697bab10a79064e540e7507da77c214f589854cd890000000114756e6c696d69746564526f796c6c6f436f696e3236a5ee53396cf2d45b9f7bfb306ec706bca2e5738d9bbf4cf52e3cd2cf45a3110000000000e8d13eb083538735c4bb6a9354741d6187206bdfa196ff9f0534523182753408";
storeFile(unlimitedRoylloCoin2.getBytes(), sha256(unlimitedRoylloCoin2) + PROOF_FILE_NAME_EXTENSION);

// Profile images;
// Adding an existing profile images for straumat.
storeFile(new ClassPathResource("static/straumat_profil.jpeg").getContentAsByteArray(),
"22222222-2222-2222-2222-222222222222.jpeg");

// Starting a web server to serve content.
PathResourceManager resourceManager = new PathResourceManager(fileSystem.getPath("."));
webServer = Undertow.builder()
.addHttpListener(WEB_SERVER_PORT, WEB_SERVER_HOST) // Set port and host
.setHandler(new ResourceHandler(resourceManager)) // Set the handler to serve files
.addHttpListener(WEB_SERVER_PORT, WEB_SERVER_HOST) // Set port and host.
.setHandler(new ResourceHandler(resourceManager)) // Set the handler to serve files.
.build();
webServer.start();
}
Expand All @@ -109,24 +110,34 @@ public void onDestroy() throws Exception {
public void storeFile(final byte[] fileContent,
@NonNull final String fileName) {
try {
logger.info("Storing file {}", fileName);
Files.write(fileSystem.getPath(".").resolve(fileName), fileContent);
} catch (Exception e) {
logger.error("Error storing file {}: {}", fileName, e.getMessage());
throw new RuntimeException(e);
}
}

@Override
@SuppressWarnings("checkstyle:DesignForExtension")
public boolean fileExists(@NonNull final String fileName) {
return Files.exists(fileSystem.getPath(".").resolve(fileName));
boolean fileExists = Files.exists(fileSystem.getPath(".").resolve(fileName));
if (fileExists) {
logger.info("File {} exists", fileName);
} else {
logger.info("File {} does not exist", fileName);
}
return fileExists;
}

@Override
@SuppressWarnings("checkstyle:DesignForExtension")
public void deleteFile(@NonNull final String fileName) {
try {
logger.info("Deleting file {}", fileName);
Files.delete(fileSystem.getPath(".").resolve(fileName));
} catch (IOException e) {
logger.error("Error deleting file {}: {}", fileName, e.getMessage());
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public void storeFile(final byte[] fileContent, @NonNull final String fileName)
.object(fileName).stream(new ByteArrayInputStream(fileContent), fileContent.length, -1)
.contentType(new Tika().detect(fileContent))
.build());
logger.info("File {} stored in S3", fileName);
} catch (Exception e) {
logger.error("Error while storing file {} in S3: {}", fileName, e.getMessage());
throw new RuntimeException(e.getMessage());
}
}

Expand All @@ -76,8 +78,10 @@ public boolean fileExists(@NonNull final String fileName) {
.bucket(s3Parameters.getBucketName())
.object(fileName)
.build());
logger.info("File {} exists in S3", fileName);
return true;
} catch (ErrorResponseException e) {
logger.info("File {} does not exist in S3", fileName);
return false;
} catch (Exception e) {
logger.error("Error checking if file exists {} in S3: {}", fileName, e.getMessage());
Expand All @@ -93,6 +97,7 @@ public void deleteFile(@NonNull final String fileName) {
.bucket(s3Parameters.getBucketName())
.object(fileName)
.build());
logger.info("File {} deleted from S3", fileName);
} catch (Exception e) {
logger.error("Error deleting file {} in S3: {}", fileName, e.getMessage());
throw new RuntimeException(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.royllo.explorer.core.util.enums.ProofType;
import org.royllo.explorer.core.util.parameters.OutgoingRateLimitsParameters;
import org.royllo.explorer.core.util.parameters.TAPDParameters;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;
Expand All @@ -21,6 +20,8 @@

import javax.net.ssl.SSLException;

import static org.springframework.http.MediaType.APPLICATION_JSON;

/**
* TAPD service implementation.
*/
Expand Down Expand Up @@ -100,7 +101,7 @@ public final Mono<DecodedProofResponse> decode(final String proof, final long pr
.post()
.uri("/v1/taproot-assets/proofs/decode")
.header("Grpc-Metadata-macaroon", tapdParameters.getApi().getMacaroon())
.contentType(MediaType.APPLICATION_JSON)
.contentType(APPLICATION_JSON)
.body(BodyInserters.fromValue(DecodedProofRequest.builder()
.rawProof(proof)
.proofAtDepth(proofAtDepth)
Expand All @@ -109,6 +110,9 @@ public final Mono<DecodedProofResponse> decode(final String proof, final long pr
.build()
))
.exchangeToFlux(response -> response.bodyToFlux(DecodedProofResponse.class))
.doOnError(throwable -> logger.error("Error calling decode for proof from tapd at {} depth: {}",
proofAtDepth,
throwable.getMessage()))
.next();
}

Expand All @@ -135,6 +139,9 @@ public final Mono<UniverseRootsResponse> getUniverseRoots(final String serverAdd
.get()
.uri("/v1/taproot-assets/universe/roots?offset=" + offset + "&limit=" + limit)
.exchangeToFlux(response -> response.bodyToFlux(UniverseRootsResponse.class))
.doOnError(throwable -> logger.error("Error getting universe roots from tapd server {}: {}",
serverAddress,
throwable.getMessage()))
.next();
}

Expand All @@ -161,6 +168,9 @@ public final Mono<UniverseLeavesResponse> getUniverseLeaves(final String serverA
.get()
.uri("/v1/taproot-assets/universe/leaves/asset-id/" + assetId + "?proof_type=" + proofType)
.exchangeToFlux(response -> response.bodyToFlux(UniverseLeavesResponse.class))
.doOnError(throwable -> logger.error("Error getting universe leaves from tapd for asset {}: {}",
assetId,
throwable.getMessage()))
.next();
}

Expand All @@ -185,12 +195,15 @@ public final Mono<OwnershipVerifyResponse> verifyOwnership(final String proofWit
.post()
.uri("/v1/taproot-assets/wallet/ownership/verify")
.header("Grpc-Metadata-macaroon", tapdParameters.getApi().getMacaroon())
.contentType(MediaType.APPLICATION_JSON)
.contentType(APPLICATION_JSON)
.body(BodyInserters.fromValue(OwnershipVerifyRequest.builder()
.proofWithWitness(proofWithWitness)
.build()
))
.exchangeToFlux(response -> response.bodyToFlux(OwnershipVerifyResponse.class))
.doOnError(throwable -> logger.error("Error verifying asset ownership with the proof {}: {}",
proofWithWitness,
throwable.getMessage()))
.next();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ProofDTO addProof(@NonNull final String proof,
.proofId(sha256(proof))
.creator(ANONYMOUS_USER)
.asset(asset.get())
.proofType(proofType)
.type(proofType)
.build());
final ProofDTO proofDTO = PROOF_MAPPER.mapToProofDTO(proofToCreate);
logger.info("Proof '{}' with id {}", getProofAbstract(proof), proofDTO.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public AddProofRequestDTO createAddProofRequest(final String proof,
.creator(ANONYMOUS_USER)
.status(OPENED)
.proof(proof)
.proofType(proofType)
.type(proofType)
.build();

AddProofRequestDTO savedRequest = REQUEST_MAPPER.mapToAddAssetRequestDTO(requestRepository.save(request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
@Mapper(nullValuePropertyMappingStrategy = IGNORE)
public interface UserMapper {
// TODO: specify fields.

User mapToUser(UserDTO source);

Expand Down
Loading

0 comments on commit 497b8b2

Please sign in to comment.