Skip to content

Commit

Permalink
Merge pull request #40 from Staketab/dev
Browse files Browse the repository at this point in the history
send Domains to zkCloudWorker
  • Loading branch information
MrFoxogen authored Apr 25, 2024
2 parents 1c4be71 + 9d33bab commit 508d969
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class ZkCloudWorkerClient {
@Value("${zk-cloud-worker.jwt-token}")
private String jwtToken;

@Value("${zk-cloud-worker.chain}")
private String chain;

private static final String PATH_TO_ZK_CLOUD_WORKER = "/zkcloudworker";

public ResponseEntity<String> sendToZkCloudWorker(ZkCloudWorkerRequestDTO requestDTO) {
Expand All @@ -29,5 +32,6 @@ public ResponseEntity<String> sendToZkCloudWorker(ZkCloudWorkerRequestDTO reques
private void setDefaultValues(ZkCloudWorkerRequestDTO requestDTO) {
requestDTO.setAuth(auth);
requestDTO.setJwtToken(jwtToken);
requestDTO.setChain(chain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ZkCloudWorkerDataDTO {
private List<ZkCloudWorkerTransaction> transactions;
private String metadata;
@Builder.Default
private String args = "{\"contractAddress\":\"B62qmyBYvHL5g7os2HFcGJC1QASTkFC8ydUBZRKGrxDqhV853YoNAME\"}";
private String args = "{\"contractAddress\":\"B62qrR3kE3S9xsQy2Jq8tp3TceWDeAmiXhU4KCXh19HzAVPj7BiNAME\"}";
@Builder.Default
private String repo = "nameservice";
@Builder.Default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.staketab.minanames.dto;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class ZkCloudWorkerRequestDTO {
private String auth;
private String jwtToken;
private String command;
@Builder.Default
private String command = "execute";
private ZkCloudWorkerDataDTO data;
private String chain;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum LogInfoStatus {
CART_RESERVE("Reserve new domain for cart"),
DELETE_CART_RESERVE("Remove reserved domain for cart"),
APPLY_CART_RESERVED_DOMAINS("Apply cart reserved domains"),
SEND_DOMAIN_TO_ZK_CLOUD_WORKER("Send domain to zk cloud worker"),
FAILED("Failed tx"),
INCORRECT_AMOUNT("Incorrect amount"),
APPLIED("Applied tx"),
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.staketab.minanames.entity;

import lombok.Getter;

@Getter
public enum ZkCloudWorkerTask {
SEND_TRANSACTION("createTxTask", "backend txs"),
GET_BLOCK_INFO("getBlocksInfo", "commands info");

private final String name;
private final String metadata;

ZkCloudWorkerTask(String name, String metadata) {
this.name = name;
this.metadata = metadata;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.staketab.minanames.entity;

import lombok.Getter;

@Getter
public enum ZkCloudWorkerTxOperation {
ADD("add"),
EXTEND("extend"),
UPDATE("update"),
REMOVE("remove");

private final String name;

ZkCloudWorkerTxOperation(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.staketab.minanames.service;

import com.staketab.minanames.entity.LogInfoEntity;
import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.LogInfoStatus;

import java.util.List;

public interface LogInfoService {

void saveLogInfo(LogInfoEntity logInfoEntity);
void saveLogInfo(DomainEntity domainEntity, LogInfoStatus status);

void saveAllLogInfos(List<LogInfoEntity> logInfoEntities);
void saveAllLogInfos(List<DomainEntity> domainEntities, LogInfoStatus status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.staketab.minanames.service;

import com.staketab.minanames.entity.PayableTransactionEntity;

import java.util.List;

public interface ZkCloudWorkerService {

void sendTxs(List<PayableTransactionEntity> appliedTxs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.DomainStatus;
import com.staketab.minanames.entity.LogInfoEntity;
import com.staketab.minanames.entity.LogInfoStatus;
import com.staketab.minanames.entity.PayableTransactionEntity;
import com.staketab.minanames.entity.TxStatus;
import com.staketab.minanames.exception.DuplicateKeyException;
Expand Down Expand Up @@ -74,7 +72,7 @@ public DomainEntity create(DomainReservationDTO request) {
throw new DuplicateKeyException(String.format("Domain already exist with name: %s", domainName));
});
DomainEntity domain = buildDomainEntity(request);
logInfoService.saveLogInfo(buildLogInfoEntity(domain, CREATE));
logInfoService.saveLogInfo(domain, CREATE);
return domainRepository.save(domain);
}

Expand All @@ -86,7 +84,7 @@ public DomainEntity reserve(DomainCartReservationDTO request) {
throw new DuplicateKeyException(String.format("Domain already exist with name: %s", domainName));
});
DomainEntity domain = buildDomainEntityReserve(request);
logInfoService.saveLogInfo(buildLogInfoEntity(domain, CART_RESERVE));
logInfoService.saveLogInfo(domain, CART_RESERVE);
return domainRepository.save(domain);
}

Expand All @@ -109,7 +107,7 @@ public void applyReservedDomain(ApplyReservedDomainDTO domainRequest) {
@Override
public void removeReservedDomain(String id) {
domainRepository.findById(id).ifPresent(domainEntity -> {
logInfoService.saveLogInfo(buildLogInfoEntity(domainEntity, DELETE_CART_RESERVE));
logInfoService.saveLogInfo(domainEntity, DELETE_CART_RESERVE);
domainRepository.delete(domainEntity);
});
}
Expand Down Expand Up @@ -149,11 +147,7 @@ public void removeReservedDomains() {
LocalDateTime localDateTime = LocalDateTime.now().minusDays(1);
long currentTimestamp = Timestamp.valueOf(localDateTime).getTime();
List<DomainEntity> domainEntities = domainRepository.findAllByReservationTimestampLessThan(currentTimestamp, PENDING.name());
List<LogInfoEntity> logInfoEntities = domainEntities
.stream()
.map(domainEntity -> buildLogInfoEntity(domainEntity, REMOVE_RESERVATION))
.toList();
logInfoService.saveAllLogInfos(logInfoEntities);
logInfoService.saveAllLogInfos(domainEntities, REMOVE_RESERVATION);
domainRepository.deleteAll(domainEntities);
}

Expand All @@ -162,11 +156,7 @@ public void removeCartReservedDomains() {
LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(10);
long currentTimestamp = Timestamp.valueOf(localDateTime).getTime();
List<DomainEntity> domainEntities = domainRepository.findAllByReservationTimestampLessThan(currentTimestamp, RESERVED.name());
List<LogInfoEntity> logInfoEntities = domainEntities
.stream()
.map(domainEntity -> buildLogInfoEntity(domainEntity, REMOVE_CART_RESERVATION))
.toList();
logInfoService.saveAllLogInfos(logInfoEntities);
logInfoService.saveAllLogInfos(domainEntities, REMOVE_CART_RESERVATION);
domainRepository.deleteAll(domainEntities);
}

Expand All @@ -179,10 +169,7 @@ private void saveUpdatedDomains(List<DomainEntity> domains, Map<String, CartRese
domainEntity.setTransaction(payableTransaction);
domainEntity.setDomainStatus(PENDING.name());
}).toList();
List<LogInfoEntity> logInfoEntities = domainEntities.stream()
.map(domainEntity -> buildLogInfoEntity(domainEntity, APPLY_CART_RESERVED_DOMAINS))
.toList();
logInfoService.saveAllLogInfos(logInfoEntities);
logInfoService.saveAllLogInfos(domainEntities, APPLY_CART_RESERVED_DOMAINS);
domainRepository.saveAll(domainEntities);
}

Expand Down Expand Up @@ -241,14 +228,4 @@ private DomainDTO buildDomainDTO(DomainEntity domainEntity) {
.transaction(domainEntity.getTransaction().getTxHash())
.build();
}

private LogInfoEntity buildLogInfoEntity(DomainEntity domainEntity, LogInfoStatus status) {
return LogInfoEntity.builder()
.logInfoStatus(status.name())
.txHash(domainEntity.getTransaction().getTxHash())
.domainName(domainEntity.getDomainName())
.amount(domainEntity.getAmount())
.ownerAddress(domainEntity.getOwnerAddress())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.staketab.minanames.service.impl;

import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.LogInfoEntity;
import com.staketab.minanames.entity.LogInfoStatus;
import com.staketab.minanames.repository.LogInfoRepository;
import com.staketab.minanames.service.LogInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class LogInfoServiceImpl implements LogInfoService {

private final LogInfoRepository logInfoRepository;

@Override
public void saveLogInfo(DomainEntity domainEntity, LogInfoStatus status) {
logInfoRepository.save(buildLogInfoEntity(domainEntity, status));
}

@Override
public void saveAllLogInfos(List<DomainEntity> domainEntities, LogInfoStatus status) {
List<LogInfoEntity> logInfoEntities = domainEntities
.stream()
.map(domainEntity -> buildLogInfoEntity(domainEntity, status))
.toList();
logInfoRepository.saveAll(logInfoEntities);
}

private LogInfoEntity buildLogInfoEntity(DomainEntity domainEntity, LogInfoStatus status) {
return LogInfoEntity.builder()
.logInfoStatus(status.name())
.txHash(domainEntity.getTransaction().getTxHash())
.domainName(domainEntity.getDomainName())
.amount(domainEntity.getAmount())
.ownerAddress(domainEntity.getOwnerAddress())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.staketab.minanames.service.impl;

import com.staketab.minanames.entity.DomainEntity;
import com.staketab.minanames.entity.LogInfoEntity;
import com.staketab.minanames.entity.LogInfoStatus;
import com.staketab.minanames.entity.PayableTransactionEntity;
import com.staketab.minanames.entity.TxStatus;
Expand All @@ -11,6 +10,7 @@
import com.staketab.minanames.repository.PayableTransactionRepository;
import com.staketab.minanames.service.LogInfoService;
import com.staketab.minanames.service.TxService;
import com.staketab.minanames.service.ZkCloudWorkerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -32,9 +32,10 @@
public class TxServiceImpl implements TxService {

private final LogInfoService logInfoService;
private final PayableTransactionRepository payableTransactionRepository;
private final TransactionRepository transactionRepository;
private final DomainRepository domainRepository;
private final ZkCloudWorkerService zkCloudWorkerService;
private final TransactionRepository transactionRepository;
private final PayableTransactionRepository payableTransactionRepository;

@Override
public PayableTransactionEntity getOrCreate(String txHash, int countDomains, TxStatus status) {
Expand All @@ -51,8 +52,8 @@ public void checkTransactions() {
List<PayableTransactionEntity> correctAppliedTxs = txsWithoutIncorrectAmount(appliedTxs);

removeFailedTxs(failedTxs);
sendTxsToZkCloudWorker(correctAppliedTxs);
applyReservedTxs(correctAppliedTxs);
zkCloudWorkerService.sendTxs(correctAppliedTxs);
}

@Override
Expand All @@ -70,10 +71,6 @@ public void deleteTxs(List<String> txHashes) {
payableTransactionRepository.deleteAllByTxHashIn(txHashes);
}

private void sendTxsToZkCloudWorker(List<PayableTransactionEntity> appliedTxs) {
//todo add client to zkCloudWorker
}

private void applyReservedTxs(List<PayableTransactionEntity> pendingTxs) {
List<PayableTransactionEntity> appliedTxs = pendingTxs.stream()
.peek(payableTransaction -> payableTransaction.setTxStatus(TxStatus.APPLIED))
Expand Down Expand Up @@ -148,11 +145,8 @@ public static Map<String, Long> calculateTotalAmountByTransaction(List<DomainEnt
}

private void saveLogInfo(List<PayableTransactionEntity> txs, LogInfoStatus status) {
List<LogInfoEntity> logInfoEntities = domainRepository.findAllByTransactionIn(txs)
.stream()
.map(domainEntity -> buildLogInfoEntity(domainEntity, status))
.toList();
logInfoService.saveAllLogInfos(logInfoEntities);
List<DomainEntity> domainEntities = domainRepository.findAllByTransactionIn(txs);
logInfoService.saveAllLogInfos(domainEntities, status);
}

private Map<TxStatus, List<PayableTransactionEntity>> generateMapOfFailedAndAppliedTxs(List<PayableTransactionEntity> payableTransactions) {
Expand Down Expand Up @@ -185,14 +179,4 @@ private PayableTransactionEntity createTx(String txHash, Integer countDomains, T
.countDomains(countDomains)
.build());
}

private LogInfoEntity buildLogInfoEntity(DomainEntity domainEntity, LogInfoStatus status) {
return LogInfoEntity.builder()
.logInfoStatus(status.name())
.txHash(domainEntity.getTransaction().getTxHash())
.domainName(domainEntity.getDomainName())
.amount(domainEntity.getAmount())
.ownerAddress(domainEntity.getOwnerAddress())
.build();
}
}
Loading

0 comments on commit 508d969

Please sign in to comment.