Skip to content

Commit

Permalink
create batches works
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Czeladka committed Mar 28, 2023
1 parent 84e0d4c commit 48c1da6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 158 deletions.
1 change: 1 addition & 0 deletions off-chain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Hydra.
## Requirements

Java 17
Aiken with Sha1: 80f2fd746dae3dcfa85b6afbe3b1d1dd1713a89e

## To build

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.List;
import java.util.Optional;

import static org.cardanofoundation.hydrapoc.util.MoreComparators.createOrderComparator;
import static org.cardanofoundation.hydrapoc.util.MoreComparators.createTxHashAndTransactionIndexComparator;

@Component
@RequiredArgsConstructor
Expand All @@ -32,45 +32,57 @@ public class VoteUtxoFinder {
private final PlutusScriptUtil plutusScriptUtil;

public List<Tuple<Utxo, VoteDatum>> getUtxosWithVotes(int batchSize) {
String voteBatchContractAddress = null;
try {
voteBatchContractAddress = plutusScriptUtil.getVoteBatcherContractAddress();
} catch (CborSerializationException e) {
log.error("Error", e);
return Collections.EMPTY_LIST;
}
log.info("getUtxosWithVotes, batch size:" + batchSize);

boolean isContinue = true;
List<Tuple<Utxo, VoteDatum>> utxos = new ArrayList<>();
int page = 0;

while (isContinue) {
List<Utxo> utxoList = utxoSupplier.getPage(voteBatchContractAddress, batchSize, page++, OrderEnum.asc);
List<Utxo> utxoList = utxoSupplier.getPage(getContractAddress(), batchSize, page++, OrderEnum.asc);
if (utxoList.size() == 0) {
isContinue = false;
continue;
}

log.info("create - utxo list before:{}", utxoList.size());

List<Tuple<Utxo, VoteDatum>> utxoTuples = utxoList.stream()
.filter(utxo -> StringUtils.hasLength(utxo.getInlineDatum()))
.map(utxo -> {
Optional<VoteDatum> voteDatum = VoteDatum.deserialize(HexUtil.decodeHexString(utxo.getInlineDatum()));
return new Tuple<>(utxo, voteDatum.orElse(null));
})
.filter(utxoOptionalTuple -> utxoOptionalTuple._2 != null)
.sorted(createOrderComparator())
.sorted(createTxHashAndTransactionIndexComparator())
.toList();

utxos.addAll(utxoTuples);

if (utxos.size() >= batchSize) {
utxos = utxos.subList(0, batchSize);
isContinue = false;
}

log.info("create - utxo after before:{}", utxoTuples.size());

}

log.info(utxos.toString());

return utxos;
}

private String getContractAddress() {
String voteBatchContractAddress = null;
try {
voteBatchContractAddress = plutusScriptUtil.getVoteBatcherContractAddress();
} catch (CborSerializationException e) {
throw new RuntimeException(e);
}
return voteBatchContractAddress;
}

public List<Tuple<Utxo, ResultBatchDatum>> getUtxosWithVoteBatches(int batchSize, long iteration) {
String voteBatchContractAddress = null;
try {
Expand All @@ -89,6 +101,8 @@ public List<Tuple<Utxo, ResultBatchDatum>> getUtxosWithVoteBatches(int batchSize
continue;
}

log.info("reduce - utxo list before:{}", utxoList.size());

val utxoTuples = utxoList.stream()
.filter(utxo -> StringUtils.hasLength(utxo.getInlineDatum()))
.map(utxo -> {
Expand All @@ -97,14 +111,17 @@ public List<Tuple<Utxo, ResultBatchDatum>> getUtxosWithVoteBatches(int batchSize
return new Tuple<>(utxo, resultBatchDatumOptional.orElse(null));
})
.filter(utxoOptionalTuple -> utxoOptionalTuple._2 != null && utxoOptionalTuple._2.getIteration() == iteration)
.sorted(MoreComparators.createOrderComparator())
.sorted(MoreComparators.createTxHashAndTransactionIndexComparator())
.toList();

utxos.addAll(utxoTuples);

if (utxos.size() >= batchSize) {
utxos = utxos.subList(0, batchSize);
isContinue = false;
}

log.info("reduce - utxo list after:{}", utxos.size());
}

log.info(utxos.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class MoreComparators {

public static <T> Comparator<Tuple<Utxo, T>> createOrderComparator() {
public static <T> Comparator<Tuple<Utxo, T>> createTxHashAndTransactionIndexComparator() {
return Comparator.comparing((Function<Tuple<Utxo, T>, String>) t -> t._1.getTxHash())
.thenComparing(t -> t._1.getOutputIndex());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class HashedListTest {
public void test_001() throws JsonProcessingException {
val items = objectMapper.readValue(votesJson, new TypeReference<List<VoteDatum>>() {});

items.stream().forEach(voteDatum -> {
items.forEach(voteDatum -> {
System.out.println(encodeHexString(voteDatum.getVoterKey()));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public void fullMonthy01() throws Exception {
importVotesFromFile();
System.out.println("creating and posting batches...");
createAndPostBatch();
// System.out.println("reducing batches...");
// reduceBatch();
System.out.println("reducing batches...");
reduceBatch();
}

//1. Generate 150 votes
@Test
public void generateVotes() throws Exception {
command.generateVotes(10, 110, "votes.json");
command.generateVotes(5, 27, "votes.json");
var allVotes = randomVoteGenerator.getAllVotes("votes.json");
System.out.println("Generated unique votes count:" + allVotes.size());
}
Expand All @@ -77,7 +77,7 @@ public void importVotesFromFile() throws Exception {
//Thread.sleep(1000); //so that all previous messages are consumed from hydra
var allVotes = randomVoteGenerator.getAllVotes("votes.json");

var batchSize = 10;
var batchSize = 9;

log.info("Starting import of votes, count:" + allVotes.size());
var partitions = Lists.partition(allVotes, batchSize);
Expand Down Expand Up @@ -105,73 +105,51 @@ public void createAndPostBatch() throws Exception {

log.info("Counting votes, count:" + allVotes.size());

int iteration = 0;
for (var votesPart : partitions) {
if (votesPart.size() == batchSize) {
//Thread.sleep(1000);
voteBatcher.createAndPostBatchTransaction(batchSize);
}
//Thread.sleep(1000);
log.info("Iteration: {}", iteration);
voteBatcher.createAndPostBatchTransaction(batchSize);
iteration++;
}

//voteBatcher.createAndPostBatchTransaction(2);

log.info("Counting votes completed.");
}

// 4. Reduce batch of 20 to 1
// 4. Reduce batch of 4 to 1
// Run this test multiple times to reduce batches to 1 batch
@Test
public void reduceBatch() throws Exception {
//Thread.sleep(5000);

voteBatchReducer.postReduceBatchTransaction(5, 1);
voteBatchReducer.postReduceBatchTransaction(5, 1);
voteBatchReducer.postReduceBatchTransaction(5, 1);
voteBatchReducer.postReduceBatchTransaction(5, 1);
voteBatchReducer.postReduceBatchTransaction(5, 1);

// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
//
// voteBatchReducer.postReduceBatchTransaction(5, 1);
// voteBatchReducer.postReduceBatchTransaction(5, 1);
// voteBatchReducer.postReduceBatchTransaction(3, 0);
// voteBatchReducer.postReduceBatchTransaction(3, 0);
// voteBatchReducer.postReduceBatchTransaction(3, 0);

// voteBatchReducer.postReduceBatchTransaction(2, 1);

// var allVotes = randomVoteGenerator.getAllVotes("votes.json");
// var size = Double.valueOf(Math.ceil((double) allVotes.size() / batchSize)).intValue();
//
// log.info("Reducing votes results, size:" + size);
// int itOneSize = allVotes.size() / 9;
// int itTwoSize = itOneSize / 3;
// int itThreeSize = itTwoSize / 3;
//
// for (int i = 0; i < size; i++) {
// Thread.sleep(1000);
// voteBatchReducer.postReduceBatchTransaction(batchSize, 0);
// log.info("Reduce - processing iteration:{}", 0);
// for (int i = 0; i < itOneSize; i++) {
// voteBatchReducer.postReduceBatchTransaction(3, 0);
// }

// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
// voteBatchReducer.postReduceBatchTransaction(5, 0);
//
// voteBatchReducer.postReduceBatchTransaction(4, 1);

// for (int i = 0; i < size; i++) {
// Thread.sleep(100);
// voteBatchReducer.postReduceBatchTransaction(batchSize, 0);
// log.info("Reduce - processing iteration:{}", 1);
// for (int i = 0; i < itTwoSize; i++) {
// voteBatchReducer.postReduceBatchTransaction(3, 1);
// }
//
// Thread.sleep(100);
// voteBatchReducer.postReduceBatchTransaction(3, 1);
//
// Thread.sleep(100);
// voteBatchReducer.postReduceBatchTransaction(3, 1);
//
// Thread.sleep(100);
// voteBatchReducer.postReduceBatchTransaction(2, 2);
//
// log.info("Reducing votes results completed.");
// log.info("Reduce - processing iteration:{}", 2);
// for (int i = 0; i < itThreeSize; i++) {
// voteBatchReducer.postReduceBatchTransaction(3, 2);
// }

}

@Test
Expand Down
4 changes: 2 additions & 2 deletions on-chain/lib/voting/vote.ak
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub type FlatVote {
abstain: VotingPower,
}

fn voting_power_for_choice(v: Vote, c: Choice) -> Option<VotingPower> {
pub fn voting_power_for_choice(v: Vote, c: Choice) -> Option<VotingPower> {
list.find(
[0, 1, 2],
fn(choice: Choice) { list.and([v.choice == choice, c == choice]) },
)
|> option.map(fn(_) { v.voting_power })
}

fn to_flat_vote(v: Vote) -> FlatVote {
pub fn to_flat_vote(v: Vote) -> FlatVote {
let cp =
ChallengeProposal { challenge: v.challenge, proposal: v.proposal }
let y =
Expand Down
4 changes: 2 additions & 2 deletions on-chain/plutus.json

Large diffs are not rendered by default.

Loading

0 comments on commit 48c1da6

Please sign in to comment.