Skip to content

Commit

Permalink
Feature: voted on support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Czeladka authored and matiwinnetou committed Sep 18, 2023
1 parent c41c329 commit 5543fa2
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public WebMvcConfigurer corsConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","));
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("GET", "HEAD", "POST", "PUT")
.allowedHeaders("*");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.formLogin(AbstractHttpConfigurer::disable)

.authorizeHttpRequests(requests -> {
requests
// SECURED by JWT auth
requests.requestMatchers(new AntPathRequestMatcher("/api/vote/receipt/**", GET.name())).authenticated()
.requestMatchers(new AntPathRequestMatcher("/api/vote/voted-on/**", GET.name())).authenticated()
// SECURED by JWT auth
.requestMatchers(new AntPathRequestMatcher("/api/vote/receipt/**", GET.name())).authenticated()
// SECURED by Web3 auth
.requestMatchers(new AntPathRequestMatcher("/api/vote/receipt", GET.name())).authenticated()
// SECURED by Web3 auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public WebMvcConfigurer corsConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","));
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("GET", "HEAD", "POST")
.allowedHeaders("*");
}
};
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
@Repository
public interface VoteRepository extends JpaRepository<Vote, String> {

@Query("SELECT v.categoryId as categoryId, v.proposalId as proposalId FROM Vote v WHERE v.eventId = :eventId AND v.stakeAddress = :stakeAddress ORDER BY v.votedAtSlot, v.idNumericHash ASC")
@Query("SELECT v.categoryId as categoryId, v.proposalId as proposalId FROM Vote v WHERE v.eventId = :eventId AND v.voterStakingAddress = :stakeAddress ORDER BY v.votedAtSlot, v.idNumericHash ASC")
List<CategoryProposalProjection> getVotedOn(@Param("eventId") String eventId, @Param("stakeAddress") String stakeAddress);

@Query("SELECT v FROM Vote v WHERE v.eventId = :eventId ORDER BY v.votedAtSlot, v.idNumericHash ASC")

List<CompactVote> findAllCompactVotesByEventId(@Param("eventId") String eventId);

Optional<Vote> findByEventIdAndCategoryIdAndVoterStakingAddress(String eventId, String categoryId, String voterStakeAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ protected void doFilterInternal(HttpServletRequest request,
var maybeEventDetails = eventDetailsE.get();
if (maybeEventDetails.isEmpty()) {
var problem = Problem.builder()
.withTitle("EVENT_NOT_FOUND")
.withDetail("Event not found, id:" + eventId)
.withTitle("UNRECOGNISED_EVENT")
.withDetail("Event not found, id: " + eventId)
.withStatus(BAD_REQUEST)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ protected void doFilterInternal(HttpServletRequest request,
if (maybeEventDetails.isEmpty()) {
var problem = Problem.builder()
.withTitle("UNRECOGNISED_EVENT")
.withDetail("Unrecognised event, event:" + eventId)
.withDetail("Event not found, id: " + eventId)
.withStatus(BAD_REQUEST)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.client.ChainFollowerClient;
import org.cardano.foundation.voting.client.UserVerificationClient;
import org.cardano.foundation.voting.domain.CategoryProposalPair;
import org.cardano.foundation.voting.domain.VoteReceipt;
import org.cardano.foundation.voting.domain.entity.Vote;
import org.cardano.foundation.voting.domain.entity.VoteMerkleProof;
Expand Down Expand Up @@ -55,7 +54,7 @@ public class DefaultVoteService implements VoteService {
@Override
@Transactional(readOnly = true)
@Timed(value = "service.vote.getVotedOn", histogram = true)
public Either<Problem, List<CategoryProposalPair>> getVotedOn(JwtAuthenticationToken auth) {
public Either<Problem, List<VoteRepository.CategoryProposalProjection>> getVotedOn(JwtAuthenticationToken auth) {
var jwtEventId = auth.eventDetails().id();
var jwtStakeAddress = auth.getStakeAddress();

Expand All @@ -67,10 +66,7 @@ public Either<Problem, List<CategoryProposalPair>> getVotedOn(JwtAuthenticationT
.build());
}

var votedOn = voteRepository.getVotedOn(jwtEventId, jwtStakeAddress).stream()
.map(r -> new CategoryProposalPair(r.getCategoryId(), r.getCategoryId())).toList();

return Either.right(votedOn);
return Either.right(voteRepository.getVotedOn(jwtEventId, jwtStakeAddress));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.cardano.foundation.voting.service.vote;

import io.vavr.control.Either;
import org.cardano.foundation.voting.domain.CategoryProposalPair;
import org.cardano.foundation.voting.domain.VoteReceipt;
import org.cardano.foundation.voting.domain.entity.Vote;
import org.cardano.foundation.voting.repository.VoteRepository;
Expand All @@ -15,7 +14,7 @@ public interface VoteService {

List<VoteRepository.CompactVote> findAllCompactVotesByEventId(String eventId);

Either<Problem, List<CategoryProposalPair>> getVotedOn(JwtAuthenticationToken auth);
Either<Problem, List<VoteRepository.CategoryProposalProjection>> getVotedOn(JwtAuthenticationToken auth);

Either<Problem, Boolean> isVoteChangingPossible(String voteId, JwtAuthenticationToken auth);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -22,7 +23,7 @@

import static org.springframework.aot.hint.ExecutableMode.INVOKE;

@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ErrorMvcAutoConfiguration.class })
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ErrorMvcAutoConfiguration.class, UserDetailsServiceAutoConfiguration.class })
@EnableJpaRepositories("org.cardano.foundation.voting.repository")
@EntityScan(basePackages = "org.cardano.foundation.voting.domain.entity")
@ComponentScan(basePackages = {
Expand All @@ -47,7 +48,7 @@ public static void main(String[] args) {
@Bean
public CommandLineRunner onStart() {
return (args) -> {
log.info("Voting Ledger Follower App started.");
log.info("Voting Ledger follower App started.");
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public class SpringWebConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","));
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("GET")
.allowedHeaders("*");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
Expand All @@ -23,17 +21,22 @@ public class AccountResource {

@RequestMapping(value = "/{eventId}/{stakeAddress}", method = GET, produces = "application/json")
@Timed(value = "resource.account.find", histogram = true)
public ResponseEntity<?> findAccount(@PathVariable("eventId") String eventId, @PathVariable String stakeAddress) {
public ResponseEntity<?> findAccount(@PathVariable("eventId") String eventId,
@PathVariable String stakeAddress) {
return accountService.findAccount(eventId, stakeAddress)
.fold(problem -> {
return ResponseEntity.status(Objects.requireNonNull(problem.getStatus()).getStatusCode()).body(problem);
return ResponseEntity
.status(problem.getStatus().getStatusCode())
.body(problem);
},
maybeAccount -> {
if (maybeAccount.isEmpty()) {
return ResponseEntity.notFound().build();
}

return ResponseEntity.ok().body(maybeAccount.orElseThrow());
var account = maybeAccount.orElseThrow();

return ResponseEntity.ok().body(account);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@ public class BlockchainDataResource {
@Timed(value = "resource.blockchain.tip", histogram = true)
public ResponseEntity<?> tip() {
return blockchainDataChainTipService.getChainTip()
.fold(problem -> ResponseEntity.status(problem.getStatus().getStatusCode()).body(problem),
chainTip -> ResponseEntity.ok().body(chainTip));
.fold(problem -> {
return ResponseEntity
.status(problem.getStatus().getStatusCode())
.body(problem);
},
chainTip -> {
return ResponseEntity
.ok()
.body(chainTip);
});
}

@RequestMapping(value = "/tx-details/{txHash}", method = GET, produces = "application/json")
@Timed(value = "resource.tx-details", histogram = true)
public ResponseEntity<?> txDetails(@PathVariable String txHash) {
return blockchainDataTransactionDetailsService.getTransactionDetails(txHash)
.fold(problem -> ResponseEntity.status(Objects.requireNonNull(problem.getStatus()).getStatusCode()).body(problem),
.fold(problem -> {
return ResponseEntity
.status(problem.getStatus().getStatusCode())
.body(problem);
},
maybeTxDetails -> {
if (maybeTxDetails.isEmpty()) {
return ResponseEntity.notFound().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
Expand All @@ -26,8 +24,14 @@ public class MerkleRootHashResource {
public ResponseEntity<?> isValidMerkleRootHash(@PathVariable("eventId") String eventId,
@PathVariable("merkleRootHashHex") String merkleRootHashHex) {
return merkleRootHashService.isPresent(eventId, merkleRootHashHex)
.fold(problem -> ResponseEntity.status(Objects.requireNonNull(problem.getStatus()).getStatusCode()).body(problem),
isMerkleRootPresentResult -> ResponseEntity.ok().body(isMerkleRootPresentResult)
.fold(problem -> {
return ResponseEntity
.status(problem.getStatus().getStatusCode())
.body(problem);
},
isMerkleRootPresentResult -> {
return ResponseEntity.ok().body(isMerkleRootPresentResult);
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.domain.presentation.EventPresentation;
import org.cardano.foundation.voting.service.reference_data.ReferencePresentationService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -24,7 +23,9 @@ public class ReferenceDataResource {
@Timed(value = "resource.reference.event", histogram = true)
public ResponseEntity<?> getEventByName(@PathVariable("eventId") String eventId) {
return referencePresentationService.findEventReference(eventId)
.fold(problem -> ResponseEntity.status(problem.getStatus().getStatusCode()).body(problem),
.fold(problem -> {
return ResponseEntity.status(problem.getStatus().getStatusCode()).body(problem);
},
maybeEventPresentation -> {
if (maybeEventPresentation.isEmpty()) {
return ResponseEntity.notFound().build();
Expand All @@ -41,8 +42,12 @@ public ResponseEntity<?> getEventByName(@PathVariable("eventId") String eventId)
@Timed(value = "resource.reference.events", histogram = true)
public ResponseEntity<?> events() {
return referencePresentationService.eventsSummaries()
.fold(problem -> ResponseEntity.status(problem.getStatus().getStatusCode()).body(problem),
eventSummaries -> ResponseEntity.ok().body(eventSummaries)
.fold(problem -> {
return ResponseEntity.status(problem.getStatus().getStatusCode()).body(problem);
},
eventSummaries -> {
return ResponseEntity.ok().body(eventSummaries);
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public WebMvcConfigurer corsConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins(allowedOrigins.split(","));
.allowedOrigins(allowedOrigins.split(","))
.allowedMethods("POST")
.allowedHeaders("*");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
Expand All @@ -29,8 +27,16 @@ public ResponseEntity<?> verifyVote(@RequestBody @Valid VoteVerificationRequest
log.info("Received vote verification request: {}", voteVerificationRequest);

return voteVerificationService.verifyVoteProof(voteVerificationRequest)
.fold(problem -> ResponseEntity.status(Objects.requireNonNull(problem.getStatus()).getStatusCode()).body(problem),
voteVerificationResult -> ResponseEntity.ok().body(voteVerificationResult)
.fold(problem -> {
return ResponseEntity
.status(problem.getStatus().getStatusCode())
.body(problem);
},
voteVerificationResult -> {
return ResponseEntity
.ok()
.body(voteVerificationResult);
}
);
}

Expand Down
20 changes: 10 additions & 10 deletions scripts/cip_30_sign.sc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def signCIP30LoginEnvelope(): Unit = {
"slot": "${lastSlot}",
"data": {
"address": "${stakeAddress}",
"event": "CIP-1694_Pre_Ratification_4619",
"event": "CIP-1694_Pre_Ratification_3316",
"network": "PREPROD",
"role": "VOTER"
}
Expand Down Expand Up @@ -89,15 +89,15 @@ def signCIP30VoteCastEnvelope(): Unit = {
{
"uri": "https://evoting.cardano.org/voltaire",
"action": "CAST_VOTE",
"actionText": "Vote",
"actionText": "Cast Vote",
"slot": "${lastSlot}",
"data": {
"id": "${voteId}",
"address": "${stakeAddress}",
"event": "CF_SUMMIT_2023_24DC",
"category": "BEST_DEX",
"proposal": "be79ce1f-3cf1-4335-bd07-98f6f24f0f12",
"votingPower": "9997463457",
"event": "CIP-1694_Pre_Ratification_3316",
"category": "MIN_VIABLE_GOV_STRUCTURE",
"proposal": "ABSTAIN",
"votingPower": "9980230980",
"network": "PREPROD",
"votedAt": "${lastSlot}"
}
Expand Down Expand Up @@ -143,8 +143,8 @@ def signCIP30ViewVoteReceiptEnvelope(): Unit = {
"data": {
"id": "${voteId}",
"address": "${stakeAddress}",
"event": "CF_SUMMIT_2023_24DC",
"category": "BEST_DEX",
"event": "CIP-1694_Pre_Ratification_3316",
"category": "CHANGE_GOV_STRUCTURE",
"network": "PREPROD"
}
}
Expand Down Expand Up @@ -188,6 +188,6 @@ def latestAbsoluteSlot(mapper: ObjectMapper): Long = {
@main
def main() = {
//signCIP30LoginEnvelope()
//signCIP30VoteCastEnvelope()
signCIP30ViewVoteReceiptEnvelope()
signCIP30VoteCastEnvelope()
//signCIP30ViewVoteReceiptEnvelope()
}
Loading

0 comments on commit 5543fa2

Please sign in to comment.