Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Czeladka committed Sep 14, 2023
2 parents c3c99bc + a121628 commit aa73132
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
@Service
public class MetadataSerialiser {

public MetadataMap serialise(CreateEventCommand createEventCommand, long slot) {
public MetadataMap serialise(CreateEventCommand createEventCommand,
long slot) {
var map = MetadataBuilder.createMap();

map.put("type", EVENT_REGISTRATION.name());
Expand All @@ -28,7 +29,7 @@ public MetadataMap serialise(CreateEventCommand createEventCommand, long slot) {
map.put("votingEventType", createEventCommand.getVotingEventType().name());
map.put("schemaVersion", createEventCommand.getSchemaVersion().getSemVer());
map.put("creationSlot", BigInteger.valueOf(slot));
map.put("publicKey", "3op4jfpo3j434i39845");
map.put("votesHashAlgo", "BLAKE2b-256");

if (List.of(STAKE_BASED, BALANCE_BASED).contains(createEventCommand.getVotingEventType())) {
map.put("startEpoch", BigInteger.valueOf(createEventCommand.getStartEpoch().orElseThrow()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

Expand All @@ -37,6 +38,7 @@
@EnableScheduling
@Slf4j
@ImportRuntimeHints(VotingApp.Hints.class)
@EnableAsync
public class VotingApp {

public static void main(String[] args) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.cardano.foundation.voting.domain;

import org.cardano.foundation.voting.domain.entity.Vote;
import org.cardano.foundation.voting.repository.VoteRepository;
import org.cardanofoundation.merkle.MerkleElement;

import java.util.List;

// L1 Merkle commitment for an event
public record L1MerkleCommitment(List<Vote> votes, MerkleElement<Vote> root, String eventId) { }
public record L1MerkleCommitment(List<VoteRepository.CompactVote> signedVotes,
MerkleElement<VoteRepository.CompactVote> root,
String eventId) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.cardano.foundation.voting.domain;

import org.cardano.foundation.voting.repository.VoteRepository;
import org.cardanofoundation.cip30.CIP30Verifier;

import java.util.Optional;
import java.util.function.Function;

import static com.bloxbean.cardano.client.crypto.Blake2bUtil.blake2bHash256;

public final class VoteSerialisations {

public static final Function<VoteRepository.CompactVote, byte[]> VOTE_SERIALISER = createSerialiserFunction();

public static Function<VoteRepository.CompactVote, byte[]> createSerialiserFunction() {
return vote -> {
var cip30Verifier = new CIP30Verifier(vote.getCoseSignature(), vote.getCosePublicKey());
var verificationResult = cip30Verifier.verify();

var bytes = Optional.ofNullable(verificationResult.getMessage()).orElse(new byte[0]);

return blake2bHash256(bytes);
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.cardanofoundation.cip30.CIP30Verifier;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.Function;

@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -24,8 +22,6 @@
@ToString(exclude = { "coseSignature", "cosePublicKey"} )
public class Vote extends AbstractTimestampEntity {

public static final Function<Vote, byte[]> VOTE_SERIALISER = createSerialiserFunction();

@Id
@Column(name = "id", nullable = false)
private String id;
Expand Down Expand Up @@ -130,19 +126,4 @@ public void setVotedAtSlot(long votedAtSlot) {
this.votedAtSlot = votedAtSlot;
}

private static Function<Vote, byte[]> createSerialiserFunction() {
return vote -> {
var cip30Verifier = new CIP30Verifier(vote.getCoseSignature(), vote.getCosePublicKey());
var verificationResult = cip30Verifier.verify();

if (!verificationResult.isValid()) {
log.info("Verifying vote failed: {}", verificationResult.getMessage());

return new byte[0];
}

return Optional.ofNullable(verificationResult.getMessage()).orElse(new byte[0]);
};
}

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package org.cardano.foundation.voting.jobs;

import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.service.merkle_tree.VoteCommitmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "vote.commitment", value = "enabled", havingValue = "true")
public class VoteCommitmentJob implements Runnable {

@Autowired
private VoteCommitmentService voteCommitmentService;

@Value("${vote.commitment.enabled}")
private boolean isVoteCommitmentEnabled;
private final VoteCommitmentService voteCommitmentService;

@Override
@Scheduled(cron = "${vote.commitment.cron.expression}")
@Timed(value = "vote.commitment.cron.job", histogram = true)
public void run() {
if (!isVoteCommitmentEnabled) {
log.info("L1 votes commitment disabled on this instance.");
return;
}

log.info("Starting VoteCommitmentJob...");

var startStop = new StopWatch();
startStop.start();

voteCommitmentService.processVotesForAllEvents();

startStop.stop();

log.info("VoteCommitmentJob completed, running time:{} secs", startStop.getTotalTimeSeconds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface VoteRepository extends JpaRepository<Vote, String> {

@Query("SELECT v FROM Vote v WHERE v.eventId = :eventId ORDER BY v.votedAtSlot, v.createdAt DESC")
List<Vote> findAllByEventId(@Param("eventId") String eventId);
List<CompactVote> findAllCompactVotesByEventId(@Param("eventId") String eventId);

Optional<Vote> findByEventIdAndCategoryIdAndVoterStakingAddress(String eventId, String categoryId, String voterStakeAddress);

Expand Down Expand Up @@ -63,4 +63,12 @@ interface CategoryLevelStats {

}

interface CompactVote {

String getCoseSignature();

Optional<String> getCosePublicKey();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,15 @@ public Either<Problem, Leaderboard.ByProposalsInCategoryStats> getCategoryLeader

Map<String, Leaderboard.Votes> proposalResultsMap = votes.stream()
.collect(toMap(VoteRepository.CategoryLevelStats::getProposalId, v -> {
var totalVotesCount = Optional.ofNullable(v.getTotalVoteCount()).orElse(0L);
var totalVotingPower = Optional.ofNullable(v.getTotalVotingPower()).map(String::valueOf).orElse("0");
var totalVotesCount = Optional.ofNullable(v.getTotalVoteCount()).orElse(0L);
var totalVotingPower = Optional.ofNullable(v.getTotalVotingPower()).map(String::valueOf).orElse("0");

var b = Leaderboard.Votes.builder();
b.votes(totalVotesCount);
var b = Leaderboard.Votes.builder();
b.votes(totalVotesCount);

switch (eventDetails.votingEventType()) {
case BALANCE_BASED, STAKE_BASED -> b.votingPower(totalVotingPower);
}
switch (eventDetails.votingEventType()) {
case BALANCE_BASED, STAKE_BASED -> b.votingPower(totalVotingPower);
}

return b.build();
}));
Expand Down
Loading

0 comments on commit aa73132

Please sign in to comment.