Skip to content

Commit

Permalink
:feat status code in combination
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrofpk committed Jul 9, 2022
1 parent 628bc21 commit 29a221e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ModelAndView home() {

mv.addObject("uri", appUri.getUri());

Long maxId = combinationRepository.findMaxId();
Long maxId = combinationRepository.findMaxPulseIndex();
CombinationEntity previous = null;
if (maxId!=null) {
CombinationEntity byPulseIndex = combinationRepository.findByPulseIndex(maxId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public ResponseEntity previous(@PathVariable String timeStamp){
public ResponseEntity last(){
try {

Long maxId = combinationRepository.findMaxId();
Long maxId = combinationRepository.findMaxPulseIndex();

CombinationEntity byPulseIndex = combinationRepository.findByPulseIndex(maxId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.beacon.vdf.scheduling.CombinationResultDto;
import com.example.beacon.vdf.sources.SeedBuilder;
import com.example.beacon.vdf.sources.SeedCombinationResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,27 +28,21 @@
import java.security.PrivateKey;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class CombinationServiceCalcAndPersist {

private final CombinationRepository combinationRepository;

private final String certificateId = "04c5dc3b40d25294c55f9bc2496fd4fe9340c1308cd073900014e6c214933c7f7737227fc5e4527298b9e95a67ad92e0310b37a77557a10518ced0ce1743e132";

private final ICipherSuite cipherSuite;

private final SeedBuilder seedBuilder;

private final VdfUnicornService vdfUnicornService;

private final Environment env;

private final SeedCombinationResult seedCombinationResult;

private static final Logger logger = LoggerFactory.getLogger(CombinationServiceCalcAndPersist.class);

@Autowired
public CombinationServiceCalcAndPersist(CombinationRepository combinationRepository,
SeedBuilder seedBuilder, VdfUnicornService vdfUnicornService,
Expand All @@ -60,21 +55,21 @@ public CombinationServiceCalcAndPersist(CombinationRepository combinationReposit
this.seedCombinationResult = seedCombinationResult;
}


@Async("threadPoolTaskExecutor")
public void run(String timeStamp, List<SeedUnicordCombinationVo> seedUnicordCombinationVos, BigInteger x) throws Exception {
int iterations = Integer.parseInt(env.getProperty("beacon.combination.iterations"));

logger.warn("Start combination sloth");
log.warn("Start combination sloth");
BigInteger y = VdfSloth.mod_op(x, iterations);
logger.warn("End combination sloth");
log.warn("End combination sloth");

persist(timeStamp, seedUnicordCombinationVos, iterations, x, y);
}

@Transactional
protected void persist(String timeStamp, List<SeedUnicordCombinationVo> seedUnicordCombinationVos, int iterations, BigInteger x, BigInteger y) throws Exception {
Long maxPulseIndex = combinationRepository.findMaxId();
Long maxPulseIndex = combinationRepository.findMaxPulseIndex();
PrivateKey privateKey = CriptoUtilService.loadPrivateKeyPkcs1(env.getProperty("beacon.x509.privatekey"));

if (maxPulseIndex==null){
maxPulseIndex = 1L;
Expand Down Expand Up @@ -105,11 +100,12 @@ protected void persist(String timeStamp, List<SeedUnicordCombinationVo> seedUnic
combinationEntity.setY(y.toString());
combinationEntity.setIterations(iterations);

combinationEntity.setStatusCode(getStatusCode(combinationEntity));

//sign
ByteSerializationFields serialization = new ByteSerializationFields(combinationEntity);
ByteArrayOutputStream baos = serialization.getBaos();

PrivateKey privateKey = CriptoUtilService.loadPrivateKeyPkcs1(env.getProperty("beacon.x509.privatekey"));
String signature = cipherSuite.signPkcs15(privateKey, serialization.getBaos().toByteArray());
combinationEntity.setSignatureValue(signature);

Expand All @@ -123,17 +119,39 @@ protected void persist(String timeStamp, List<SeedUnicordCombinationVo> seedUnic
CombinationResultDto combinationResultDto = new CombinationResultDto(combinationEntity.getTimeStamp().toString(), combinationEntity.getOutputValue(), combinationEntity.getUri());
sendToUnicorn(combinationResultDto);

logger.warn("Stop run:");
log.warn("Stop run:");
}

protected void sendToUnicorn(CombinationResultDto combinationResultDto) throws Exception {
if (!vdfUnicornService.isOpen()){
return;
}

logger.warn(combinationResultDto.toString());
log.warn(combinationResultDto.toString());
seedCombinationResult.setCombinationResultDto(combinationResultDto);
vdfUnicornService.endTimeSlot();
}

private int getStatusCode(CombinationEntity currentEntity){
int statusCode;

Long maxId = combinationRepository.findMaxId();
Optional<CombinationEntity> previewsPulse = combinationRepository.findById(maxId);

if (previewsPulse.isPresent()){
ZonedDateTime previewPulseTimeStamp = previewsPulse.get().getTimeStamp();
ZonedDateTime currentPulseTimesptamp = currentEntity.getTimeStamp();

long betweenInMinutes = ChronoUnit.MINUTES.between(previewPulseTimeStamp, currentPulseTimesptamp);
if (betweenInMinutes != 10L){
statusCode = 2;
} else {
statusCode = 0;
}
} else {
statusCode = 1;
}
return statusCode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class CombinationEntity {

private int iterations;

private int statusCode;

private String outputValue;

private ZonedDateTime createdAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public interface CombinationRepository extends JpaRepository<CombinationEntity,
@Query(value = "SELECT distinct p from CombinationEntity p left join fetch p.seedList l where p.timeStamp >= ?1 and p.timeStamp <=?2 order by p.timeStamp")
List<CombinationEntity> findSequence(ZonedDateTime timeStamp1, ZonedDateTime timeStamp2);

@Deprecated // may have performance issues - use findMaxId
@Query(value = "select distinct max(p.pulseIndex) from CombinationEntity p")
Long findMaxPulseIndex();

@Query(value = "select distinct max(p.id) from CombinationEntity p")
Long findMaxId();

@Query(value = "select p from CombinationEntity p left join fetch p.seedList where p.pulseIndex = ?1")
Expand Down

0 comments on commit 29a221e

Please sign in to comment.