diff --git a/src/main/java/com/staketab/minanames/client/ZkCloudWorkerClient.java b/src/main/java/com/staketab/minanames/client/ZkCloudWorkerClient.java index 9bd0c74..2656ca4 100644 --- a/src/main/java/com/staketab/minanames/client/ZkCloudWorkerClient.java +++ b/src/main/java/com/staketab/minanames/client/ZkCloudWorkerClient.java @@ -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 sendToZkCloudWorker(ZkCloudWorkerRequestDTO requestDTO) { @@ -29,5 +32,6 @@ public ResponseEntity sendToZkCloudWorker(ZkCloudWorkerRequestDTO reques private void setDefaultValues(ZkCloudWorkerRequestDTO requestDTO) { requestDTO.setAuth(auth); requestDTO.setJwtToken(jwtToken); + requestDTO.setChain(chain); } } diff --git a/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerDataDTO.java b/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerDataDTO.java index 8ed9261..9513a03 100644 --- a/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerDataDTO.java +++ b/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerDataDTO.java @@ -14,7 +14,7 @@ public class ZkCloudWorkerDataDTO { private List transactions; private String metadata; @Builder.Default - private String args = "{\"contractAddress\":\"B62qmyBYvHL5g7os2HFcGJC1QASTkFC8ydUBZRKGrxDqhV853YoNAME\"}"; + private String args = "{\"contractAddress\":\"B62qrR3kE3S9xsQy2Jq8tp3TceWDeAmiXhU4KCXh19HzAVPj7BiNAME\"}"; @Builder.Default private String repo = "nameservice"; @Builder.Default diff --git a/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerRequestDTO.java b/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerRequestDTO.java index 88cc089..70b226f 100644 --- a/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerRequestDTO.java +++ b/src/main/java/com/staketab/minanames/dto/ZkCloudWorkerRequestDTO.java @@ -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; } diff --git a/src/main/java/com/staketab/minanames/entity/LogInfoStatus.java b/src/main/java/com/staketab/minanames/entity/LogInfoStatus.java index 2b9a5a6..c9d200b 100644 --- a/src/main/java/com/staketab/minanames/entity/LogInfoStatus.java +++ b/src/main/java/com/staketab/minanames/entity/LogInfoStatus.java @@ -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"), diff --git a/src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTask.java b/src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTask.java new file mode 100644 index 0000000..a69a1a0 --- /dev/null +++ b/src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTask.java @@ -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; + } +} diff --git a/src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTxOperation.java b/src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTxOperation.java new file mode 100644 index 0000000..b743ca4 --- /dev/null +++ b/src/main/java/com/staketab/minanames/entity/ZkCloudWorkerTxOperation.java @@ -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; + } +} diff --git a/src/main/java/com/staketab/minanames/service/LogInfoService.java b/src/main/java/com/staketab/minanames/service/LogInfoService.java index 0d1cfd2..2fc3d37 100644 --- a/src/main/java/com/staketab/minanames/service/LogInfoService.java +++ b/src/main/java/com/staketab/minanames/service/LogInfoService.java @@ -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 logInfoEntities); + void saveAllLogInfos(List domainEntities, LogInfoStatus status); } diff --git a/src/main/java/com/staketab/minanames/service/ZkCloudWorkerService.java b/src/main/java/com/staketab/minanames/service/ZkCloudWorkerService.java new file mode 100644 index 0000000..840b85b --- /dev/null +++ b/src/main/java/com/staketab/minanames/service/ZkCloudWorkerService.java @@ -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 appliedTxs); +} diff --git a/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java index cd268d1..f3ed02a 100644 --- a/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java +++ b/src/main/java/com/staketab/minanames/service/impl/DomainServiceImpl.java @@ -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; @@ -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); } @@ -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); } @@ -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); }); } @@ -149,11 +147,7 @@ public void removeReservedDomains() { LocalDateTime localDateTime = LocalDateTime.now().minusDays(1); long currentTimestamp = Timestamp.valueOf(localDateTime).getTime(); List domainEntities = domainRepository.findAllByReservationTimestampLessThan(currentTimestamp, PENDING.name()); - List logInfoEntities = domainEntities - .stream() - .map(domainEntity -> buildLogInfoEntity(domainEntity, REMOVE_RESERVATION)) - .toList(); - logInfoService.saveAllLogInfos(logInfoEntities); + logInfoService.saveAllLogInfos(domainEntities, REMOVE_RESERVATION); domainRepository.deleteAll(domainEntities); } @@ -162,11 +156,7 @@ public void removeCartReservedDomains() { LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(10); long currentTimestamp = Timestamp.valueOf(localDateTime).getTime(); List domainEntities = domainRepository.findAllByReservationTimestampLessThan(currentTimestamp, RESERVED.name()); - List logInfoEntities = domainEntities - .stream() - .map(domainEntity -> buildLogInfoEntity(domainEntity, REMOVE_CART_RESERVATION)) - .toList(); - logInfoService.saveAllLogInfos(logInfoEntities); + logInfoService.saveAllLogInfos(domainEntities, REMOVE_CART_RESERVATION); domainRepository.deleteAll(domainEntities); } @@ -179,10 +169,7 @@ private void saveUpdatedDomains(List domains, Map 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); } @@ -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(); - } } diff --git a/src/main/java/com/staketab/minanames/service/impl/LogInfoEntityImpl.java b/src/main/java/com/staketab/minanames/service/impl/LogInfoEntityImpl.java deleted file mode 100644 index de56557..0000000 --- a/src/main/java/com/staketab/minanames/service/impl/LogInfoEntityImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.staketab.minanames.service.impl; - -import com.staketab.minanames.entity.LogInfoEntity; -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 LogInfoEntityImpl implements LogInfoService { - - private final LogInfoRepository logInfoRepository; - - @Override - public void saveLogInfo(LogInfoEntity logInfoEntity) { - logInfoRepository.save(logInfoEntity); - } - - @Override - public void saveAllLogInfos(List logInfoEntities) { - logInfoRepository.saveAll(logInfoEntities); - } -} diff --git a/src/main/java/com/staketab/minanames/service/impl/LogInfoServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/LogInfoServiceImpl.java new file mode 100644 index 0000000..b365146 --- /dev/null +++ b/src/main/java/com/staketab/minanames/service/impl/LogInfoServiceImpl.java @@ -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 domainEntities, LogInfoStatus status) { + List 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(); + } +} diff --git a/src/main/java/com/staketab/minanames/service/impl/TxServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/TxServiceImpl.java index da74129..7076538 100644 --- a/src/main/java/com/staketab/minanames/service/impl/TxServiceImpl.java +++ b/src/main/java/com/staketab/minanames/service/impl/TxServiceImpl.java @@ -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; @@ -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; @@ -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) { @@ -51,8 +52,8 @@ public void checkTransactions() { List correctAppliedTxs = txsWithoutIncorrectAmount(appliedTxs); removeFailedTxs(failedTxs); - sendTxsToZkCloudWorker(correctAppliedTxs); applyReservedTxs(correctAppliedTxs); + zkCloudWorkerService.sendTxs(correctAppliedTxs); } @Override @@ -70,10 +71,6 @@ public void deleteTxs(List txHashes) { payableTransactionRepository.deleteAllByTxHashIn(txHashes); } - private void sendTxsToZkCloudWorker(List appliedTxs) { - //todo add client to zkCloudWorker - } - private void applyReservedTxs(List pendingTxs) { List appliedTxs = pendingTxs.stream() .peek(payableTransaction -> payableTransaction.setTxStatus(TxStatus.APPLIED)) @@ -148,11 +145,8 @@ public static Map calculateTotalAmountByTransaction(List txs, LogInfoStatus status) { - List logInfoEntities = domainRepository.findAllByTransactionIn(txs) - .stream() - .map(domainEntity -> buildLogInfoEntity(domainEntity, status)) - .toList(); - logInfoService.saveAllLogInfos(logInfoEntities); + List domainEntities = domainRepository.findAllByTransactionIn(txs); + logInfoService.saveAllLogInfos(domainEntities, status); } private Map> generateMapOfFailedAndAppliedTxs(List payableTransactions) { @@ -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(); - } } diff --git a/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java b/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java new file mode 100644 index 0000000..e53a6f5 --- /dev/null +++ b/src/main/java/com/staketab/minanames/service/impl/ZkCloudWorkerServiceImpl.java @@ -0,0 +1,71 @@ +package com.staketab.minanames.service.impl; + +import com.staketab.minanames.client.ZkCloudWorkerClient; +import com.staketab.minanames.dto.ZkCloudWorkerDataDTO; +import com.staketab.minanames.dto.ZkCloudWorkerRequestDTO; +import com.staketab.minanames.dto.ZkCloudWorkerTransaction; +import com.staketab.minanames.entity.DomainEntity; +import com.staketab.minanames.entity.PayableTransactionEntity; +import com.staketab.minanames.entity.ZkCloudWorkerTask; +import com.staketab.minanames.repository.DomainRepository; +import com.staketab.minanames.service.LogInfoService; +import com.staketab.minanames.service.ZkCloudWorkerService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.List; + +import static com.staketab.minanames.entity.LogInfoStatus.SEND_DOMAIN_TO_ZK_CLOUD_WORKER; +import static com.staketab.minanames.entity.ZkCloudWorkerTask.SEND_TRANSACTION; +import static com.staketab.minanames.entity.ZkCloudWorkerTxOperation.ADD; + +@Slf4j +@Service +@RequiredArgsConstructor +public class ZkCloudWorkerServiceImpl implements ZkCloudWorkerService { + + private final LogInfoService logInfoService; + private final DomainRepository domainRepository; + private final ZkCloudWorkerClient zkCloudWorkerClient; + + @Override + public void sendTxs(List appliedTxs) { + List domainEntities = domainRepository.findAllByTransactionIn(appliedTxs); + List zkCloudWorkerTransactions = domainEntities + .stream() + .map(this::mapToZkCloudWorkerTransaction) + .toList(); + List entities = domainEntities.stream() + .peek(domainEntity -> domainEntity.setIsSendToCloudWorker(true)).toList(); + ZkCloudWorkerDataDTO zkCloudWorkerDataDTO = mapToZkCloudWorkerDataDTO(zkCloudWorkerTransactions, SEND_TRANSACTION); + ZkCloudWorkerRequestDTO zkCloudWorkerRequestDTO = mapToZkCloudWorkerRequestDTO(zkCloudWorkerDataDTO); + zkCloudWorkerClient.sendToZkCloudWorker(zkCloudWorkerRequestDTO); + logInfoService.saveAllLogInfos(domainEntities, SEND_DOMAIN_TO_ZK_CLOUD_WORKER); + domainRepository.saveAll(entities); + } + + private ZkCloudWorkerRequestDTO mapToZkCloudWorkerRequestDTO(ZkCloudWorkerDataDTO zkCloudWorkerDataDTO) { + return ZkCloudWorkerRequestDTO.builder() + .data(zkCloudWorkerDataDTO) + .build(); + } + + private ZkCloudWorkerDataDTO mapToZkCloudWorkerDataDTO(List tx, ZkCloudWorkerTask zkCloudWorkerTask) { + return ZkCloudWorkerDataDTO.builder() + .transactions(tx) + .task(zkCloudWorkerTask.getName()) + .metadata(zkCloudWorkerTask.getMetadata()) + .build(); + } + + private ZkCloudWorkerTransaction mapToZkCloudWorkerTransaction(DomainEntity domainEntity) { + return ZkCloudWorkerTransaction.builder() + .operation(ADD.getName()) + .address(domainEntity.getOwnerAddress()) + .expiry(domainEntity.getEndTimestamp()) + .name(domainEntity.getDomainName()) + // .signature("Test") + .build(); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 83dc298..c79d833 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -9,6 +9,7 @@ server: enabled: false zk-cloud-worker: + chain: devnet url: ${ZK_CLOUD_WORKER_URL} auth: ${ZK_CLOUD_WORKER_AUTH} jwt-token: ${ZK_CLOUD_WORKER_JWT_TOKEN}