Skip to content

Commit

Permalink
Merge pull request #33 from cardano-foundation/demo
Browse files Browse the repository at this point in the history
feat: connect wallet list popup
  • Loading branch information
cfuisat authored Jul 12, 2023
2 parents 51de31d + 534d5f5 commit 6cb8e06
Show file tree
Hide file tree
Showing 76 changed files with 3,170 additions and 1,009 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cardano.foundation.voting.shell;

import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.domain.*;
import org.cardano.foundation.voting.service.transaction_submit.L1SubmissionService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,6 +15,7 @@
import static org.cardano.foundation.voting.utils.MoreUUID.shortUUID;

@ShellComponent
@Slf4j
public class CFVotingAdminCommands {

private final static String EVENT_NAME = "CIP-1694_Pre_Ratification";
Expand All @@ -23,6 +25,8 @@ public class CFVotingAdminCommands {

@ShellMethod(key = "create-cip-1694-event", value = "Create a CIP-1694 voting event")
public String createVoltairePreRatificationEvent() {
log.info("Creating CIP-1694 event...");

CreateEventCommand createEventCommand = CreateEventCommand.builder()
.id(EVENT_NAME + "_" + shortUUID(4))
.startEpoch(Optional.of(80))
Expand All @@ -43,6 +47,8 @@ public String createVoltairePreRatificationEvent() {

@ShellMethod(key = "create-cip-1694-category", value = "Create a CIP-1694 category")
public String createVoltairePreRatificationCategory(@ShellOption String event) {
log.info("Creating CIP-1694 category...");

Proposal yesProposal = Proposal.builder()
.id(java.util.UUID.randomUUID().toString())
.name("YES")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.cardano.foundation.voting.shell;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@ShellComponent
@Slf4j
public class CFVotingAppCommands {

@Autowired
private HttpClient httpClient;

@Value("${voting.app.base.url:http://localhost:8080}")
private String votingAppBaseUrl;

@ShellMethod(key = "full-metadata-scan", value = "Invokes full metadata scan on voting-app")
public String votingAppFullMetadataScan() throws IOException, InterruptedException {
log.info("Invoking full metadata scan on voting-app...");

var metadataScanRequest = HttpRequest.newBuilder()
.uri(URI.create(votingAppBaseUrl + "/api/admin/full-metadata-scan") )
.POST(HttpRequest.BodyPublishers.noBody())
.header("Content-Type", "application/json")
.build();

var response = httpClient.send(metadataScanRequest, HttpResponse.BodyHandlers.discarding());

if (response.statusCode() >= 200 && response.statusCode() < 300) {
return "Successfully en-queued metadata scan request.";
}

return "Error en-queuing metadata scan request: " + response.statusCode() + " " + response.body();
}

}
10 changes: 6 additions & 4 deletions backend-services/voting-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")

//spring-boot-starter-rsocket

testImplementation("org.springframework.boot:spring-boot-starter-test")

runtimeOnly("io.micrometer:micrometer-registry-prometheus")

implementation("org.cardanofoundation:cip30-data-signature-parser:0.0.6")
// TODO release to maven central
implementation("org.cardanofoundation:cip30-data-signature-parser:0.0.9-SNAPSHOT")

implementation("org.flywaydb:flyway-core")

implementation("com.google.guava:guava:32.1.1-jre")

implementation("org.zalando:problem-spring-web:0.29.1")

implementation("org.zalando:jackson-datatype-problem:0.27.1")
Expand Down Expand Up @@ -78,7 +79,8 @@ dependencies {

implementation("org.postgresql:postgresql")

implementation("org.cardanofoundation:merkle-tree-java:0.0.6-SNAPSHOT") // TODO release to maven central
// TODO release to maven central
implementation("org.cardanofoundation:merkle-tree-java:0.0.6-SNAPSHOT")
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.cardano.foundation.voting.service.blockchain_state.blockfrost.BlockfrostBlockchainDataStakePoolService;
import org.cardano.foundation.voting.service.blockchain_state.blockfrost.BlockfrostBlockchainDataTipService;
import org.cardano.foundation.voting.service.blockchain_state.blockfrost.BlockfrostBlockchainDataTransactionDetailsService;
import org.cardano.foundation.voting.service.blockchain_state.cardano_submit_api.CardanoSubmitApiBlockchainTransactionSubmissionService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -29,8 +28,8 @@ public BlockchainDataStakePoolService blockchainDataStakePoolService() {

@Bean
public BlockchainTransactionSubmissionService transactionSubmissionService() {
//return new BlockchainTransactionSubmissionService.Noop();
return new CardanoSubmitApiBlockchainTransactionSubmissionService();
return new BlockchainTransactionSubmissionService.Noop();
//return new CardanoSubmitApiBlockchainTransactionSubmissionService();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public static class ByCategory {
public static class ByEvent {

private String event;
private Votes votes;
private long totalVotesCount;
private long totalVotingPower;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ public int getScore() {
}

public static FinalityScore fromConfirmations(int transactionsConfirmations) {
if (transactionsConfirmations > 2160) {
if (transactionsConfirmations > 2160) { // 1,5 days
return FinalityScore.FINAL;
}

if (transactionsConfirmations > 100) {
return VERY_HIGH;
}

if (transactionsConfirmations > 8) {
if (transactionsConfirmations >= 8) {
return HIGH;
}

if (transactionsConfirmations > 5) {
if (transactionsConfirmations >= 4) {
return MEDIUM;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class VoteReceipt {

private String proposal;

private String proposalText;

private String votingPower;

private String voterStakingAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public class Vote extends AbstractTimestampEntity {
@Nullable
private Long votingPower; // makes sense only for STAKE_BASED or BALANCE_BASED events

@Column(name = "network", nullable = false)
private CardanoNetwork network;

@Column(name = "voted_at_slot", nullable = false)
private long votedAtSlot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository
Expand All @@ -17,7 +16,12 @@ public interface VoteRepository extends JpaRepository<Vote, String> {

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

@Query("SELECT COUNT(v), SUM(v.votingPower) FROM Vote v WHERE v.eventId = ?1")
Map<String, Object> countAllByEventId(String eventId);
@Query("SELECT COUNT(v) as totalVoteCount, SUM(v.votingPower) as totalVotingPower FROM Vote v WHERE v.eventId = ?1")
List<EventVoteCount> countAllByEventId(String eventId);

interface EventVoteCount {
long getTotalVoteCount();
long getTotalVotingPower();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ public Either<Problem, Optional<Account>> findAccount(String eventName, String s
}
var event = maybeEvent.orElseThrow();

var stakeAddressE = stakeAddressVerificationService.checkStakeAddress(stakeAddress);
if (stakeAddressE.isLeft()) {
return Either.left(stakeAddressE.getLeft());
var stakeAddressCheckE = stakeAddressVerificationService.checkIfAddressIsStakeAddress(stakeAddress);
if (stakeAddressCheckE.isLeft()) {
return Either.left(stakeAddressCheckE.getLeft());
}

var stakeAddressNetworkCheck = stakeAddressVerificationService.checkStakeAddressNetwork(stakeAddress);
if (stakeAddressNetworkCheck.isLeft()) {
return Either.left(stakeAddressNetworkCheck.getLeft());
}

if (!List.of(STAKE_BASED, BALANCE_BASED).contains(event.getVotingEventType())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cardano.foundation.voting.service.address;

import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.client.util.HexUtil;
import io.vavr.control.Either;
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.domain.CardanoNetwork;
Expand All @@ -18,7 +20,33 @@ public class StakeAddressVerificationService {
@Autowired
private CardanoNetwork cardanoNetwork;

public Either<Problem, Boolean> checkStakeAddress(String stakeAddress) {
public Either<Problem, Boolean> checkIfAddressIsStakeAddress(String address) {
if (!address.startsWith("stake")) {
return Either.left(Problem.builder()
.withTitle("NOT_STAKE_ADDRESS")
.withDetail("Address is not a stakeAddress, address:" + address)
.withStatus(BAD_REQUEST)
.build());
}

return Either.right(true);
}

public Either<Problem, Boolean> checkIfAddressIsStakeAddress(byte[] address) {
var addr = new Address(address);

if (!addr.isStakeKeyHashInDelegationPart()) {
return Either.left(Problem.builder()
.withTitle("NOT_STAKE_ADDRESS")
.withDetail("Address is not a stakeAddress, address:" + HexUtil.encodeHexString(address))
.withStatus(BAD_REQUEST)
.build());
}

return Either.right(true);
}

public Either<Problem, Boolean> checkStakeAddressNetwork(String stakeAddress) {
if (isMainnet(stakeAddress) && cardanoNetwork.isMainnet()) {
return Either.right(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.cardano.foundation.voting.service.leader_board;

import com.google.common.collect.Iterables;
import io.vavr.control.Either;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.cardano.foundation.voting.domain.CardanoNetwork;
import org.cardano.foundation.voting.domain.Leaderboard;
Expand All @@ -11,7 +13,10 @@
import org.springframework.stereotype.Service;
import org.zalando.problem.Problem;

import java.util.Map;

import static org.zalando.problem.Status.BAD_REQUEST;
import static org.zalando.problem.Status.INTERNAL_SERVER_ERROR;

@Service
@Slf4j
Expand Down Expand Up @@ -43,10 +48,22 @@ public Either<Problem, Leaderboard.ByEvent> getEventLeaderboard(String event) {
var e = maybeEvent.orElseThrow();

var votes = voteRepository.countAllByEventId(event);
if (votes.isEmpty()) {
return Either.left(Problem.builder()
.withTitle("INTERNAL_ERROR")
.withDetail("No votes for event, event:" + event)
.withStatus(INTERNAL_SERVER_ERROR)
.build()
);
}
var eventVoteCount = Iterables.getOnlyElement(votes);
var votingPower = eventVoteCount.getTotalVotingPower();
var voteCount = eventVoteCount.getTotalVoteCount();

return Either.right(Leaderboard.ByEvent.builder()
.event(event)
//.votes(votes)
.totalVotesCount(voteCount)
.totalVotingPower(votingPower)
.build()
);
}
Expand All @@ -62,6 +79,7 @@ public Either<Problem, Leaderboard.ByCategory> getCategoryLeaderboard(String eve
.build()
);
}

var e = maybeEvent.orElseThrow();

var maybeCategory = e.findCategoryByName(category);
Expand All @@ -74,18 +92,23 @@ public Either<Problem, Leaderboard.ByCategory> getCategoryLeaderboard(String eve
.build()
);
}
var cat = maybeCategory.orElseThrow();

// TODO uncomment
// if (!expirationService.isEventFinished(e) && !e.isCategoryResultsWhileVoting()) {
// return Either.left(Problem.builder()
// .withTitle("VOTING_RESULTS_NOT_AVAILABLE")
// .withDetail("Voting results not yet available, category:" + category)
// .withStatus(BAD_REQUEST)
// .build()
// );
// }

if (!expirationService.isEventFinished(e) && !e.isCategoryResultsWhileVoting()) {
return Either.left(Problem.builder()
.withTitle("VOTING_RESULTS_NOT_AVAILABLE")
.withDetail("Voting results not yet available, category:" + category)
.withStatus(BAD_REQUEST)
.build()
);
}

// TODO: implement this
return Either.right(Leaderboard.ByCategory.builder()
.category(cat.getId())
.proposals(Map.of("YES", new Leaderboard.Votes(10, 3_000),
"NO", new Leaderboard.Votes(10, 2_000),
"ABSTAIN", new Leaderboard.Votes(10, 1_000)))
.build()
);
}
Expand Down
Loading

0 comments on commit 6cb8e06

Please sign in to comment.