Skip to content

Commit

Permalink
mintsStore implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Jan 22, 2019
1 parent e6d8e6b commit 4fed27f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 38 deletions.
11 changes: 0 additions & 11 deletions core/src/main/java/host/furszy/zerocoinj/store/MintsStore.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package host.furszy.zerocoinj.store.coins;

import host.furszy.zerocoinj.store.coins.StoredMint;

import java.math.BigInteger;
import java.util.List;

public interface MintsStore {

boolean put(StoredMint storedMint);

StoredMint get(BigInteger commitmentValue);

List<StoredMint> list();

void deleteStore();

boolean update(StoredMint storedMint);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package host.furszy.zerocoinj.store;
package host.furszy.zerocoinj.store.coins;


import com.zerocoinj.core.CoinDenomination;
Expand All @@ -9,18 +9,31 @@

public class StoredMint {

private Commitment commitment;
// Only stored commitment value and randomness
private BigInteger commitmentValue;
// Content of the commitment
private BigInteger serial;
//
private CoinDenomination denom;
private Sha256Hash parentTxId;
// Height in which the coin was added to the accumulator
private int mintHeight;

// Height for the witness accumulator calculation
private int computedUpToHeight;
private BigInteger acc;
private BigInteger accWit;

public StoredMint(Commitment commitment, BigInteger serial, CoinDenomination denom, Sha256Hash parentTxId, int mintHeight, int computedUpToHeight, BigInteger acc, BigInteger accWit) {
this.commitment = commitment;
public StoredMint(
BigInteger commitmentValue,
BigInteger serial,
CoinDenomination denom,
Sha256Hash parentTxId,
int mintHeight,
int computedUpToHeight,
BigInteger acc,
BigInteger accWit) {

this.commitmentValue = commitmentValue;
this.serial = serial;
this.denom = denom;
this.parentTxId = parentTxId;
Expand All @@ -30,8 +43,8 @@ public StoredMint(Commitment commitment, BigInteger serial, CoinDenomination den
this.accWit = accWit;
}

public Commitment getCommitment() {
return commitment;
public BigInteger getCommitmentValue() {
return commitmentValue;
}

public BigInteger getSerial() {
Expand Down Expand Up @@ -77,7 +90,7 @@ public void setAccWit(BigInteger accWit) {
@Override
public String toString() {
return "StoredMint{" +
"commitment=" + commitment +
"commitmentValue=" + commitmentValue +
", serial=" + serial +
", denom=" + denom +
", parentTxId=" + parentTxId +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package host.furszy.zerocoinj.wallet;

import com.google.common.collect.Lists;
import com.zerocoinj.core.CoinDenomination;
import com.zerocoinj.core.CoinSpend;
import com.zerocoinj.core.SpendType;
Expand All @@ -11,7 +10,6 @@
import com.zerocoinj.core.context.ZerocoinContext;
import host.furszy.zerocoinj.protocol.GenWitMessage;
import host.furszy.zerocoinj.protocol.PubcoinsMessage;
import host.furszy.zerocoinj.store.StoredMint;
import org.pivxj.core.*;
import org.pivxj.core.listeners.OnGetDataResponseEventListener;
import org.pivxj.script.Script;
Expand All @@ -29,7 +27,6 @@
import java.util.*;
import java.util.concurrent.*;

import static com.google.common.primitives.Ints.min;
import static host.furszy.zerocoinj.protocol.PubcoinsMessage.ERROR_CODES.NO_ENOUGH_MINTS;

public class ZCSpendRequest implements Callable<Transaction>,OnGetDataResponseEventListener {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/pivxj/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import com.zerocoinj.JniBridge;
import com.zerocoinj.core.context.ZerocoinContext;
import host.furszy.zerocoinj.store.AccStore;
import host.furszy.zerocoinj.store.AccStoreImp;
import host.furszy.zerocoinj.store.MintsStore;
import host.furszy.zerocoinj.store.coins.MintsStore;
import org.pivxj.core.listeners.BlockChainListener;
import org.pivxj.store.FlatDB;
import org.pivxj.store.MasternodeDB;
Expand Down
63 changes: 50 additions & 13 deletions core/src/main/java/org/pivxj/wallet/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import host.furszy.zerocoinj.protocol.AccValueMessage;
import host.furszy.zerocoinj.protocol.GenWitMessage;
import host.furszy.zerocoinj.store.AccStoreException;
import host.furszy.zerocoinj.store.coins.StoredMint;
import host.furszy.zerocoinj.wallet.CannotCompleteSendRequestException;
import host.furszy.zerocoinj.wallet.ZCSpendRequest;
import net.jcip.annotations.*;
Expand Down Expand Up @@ -4289,24 +4290,60 @@ public ZCoin loadZcoin(TransactionOutput output){
}

private GenWitMessage newGenWitMessage(ZCoin zCoin, long tweak) throws AccStoreException {
// height, todo: randomize this a little bit
int startHeight = (zCoin.getHeight() - (zCoin.getHeight() % 10)) - new Random().nextInt(50);

// First check if we already have this mint stored
StoredMint storedMint = null;
if (context.mintsStore != null) {
storedMint = context.mintsStore.get(zCoin.getCommitment().getCommitmentValue());
}

// height, randomize this
int startHeight;
BigInteger accValue = null;
if (storedMint == null){
int randomNumber = new Random().nextInt(200);
startHeight = (zCoin.getHeight() - (zCoin.getHeight() % 10)) - (randomNumber - randomNumber % 10 );
accValue = context.accStore.get(startHeight, zCoin.getCoinDenomination());


// Store mint
context.mintsStore.put(
new StoredMint(
zCoin.getCommitment().getCommitmentValue(),
zCoin.getSerial(),
zCoin.getCoinDenomination(),
zCoin.getParentTxId(),
zCoin.getHeight(),
startHeight,
accValue,
accValue
)
);
}else {
startHeight = storedMint.getComputedUpToHeight();
accValue = storedMint.getAccWit();
if (accValue == null){
accValue = context.accStore.get(startHeight, zCoin.getCoinDenomination());
if (accValue != null){
// Update mint
storedMint.setAccWit(accValue);
storedMint.setAcc(accValue);
storedMint.setComputedUpToHeight(startHeight);
context.mintsStore.update(storedMint); // Check if update worked correctly..
}
}
}

// TODO: Cargar aquí el witness value base de este mensaje desde la db de accumulators value
//TODO: Recordar que esto puede causar que se acumulen bloques dos veces.. tengo que pasarle el bloque exacto donde se quedó el ultimo.
// TODO: Recordar que esto puede causar que se acumulen bloques dos veces.. tengo que pasarle el bloque exacto donde se quedó el ultimo.
// TODO: Si quiero agregarle randomness deberia pedir los acc value que ocurrieron antes de este mint..
// Get the checkpoint added at the next multiple of 10
//int nHeightCheckpoint = zCoin.getHeight() - (10 + (zCoin.getHeight() % 10));
// TODO: This is just for the first implementation, should be changed following the protocol..
int nHeightCheckpoint = zCoin.getHeight() - ((zCoin.getHeight() % 10));
startHeight = nHeightCheckpoint;
// TODO: Change this for the last known witness..
BigInteger accValue = context.accStore.get(nHeightCheckpoint, zCoin.getCoinDenomination());
// TODO: This is just for the first implementation, should be changed following the final protocol specs..
// TODO: Add the last known witness..

if (accValue == null){
// TODO: Request the value and put all of this in waiting status or reject the work..
log.error("## there is no accValue for nHeightCheckpoint: " + nHeightCheckpoint + ", denom: " + zCoin.getCoinDenomination() + ", for coin: " + zCoin);
log.error("## there is no accValue for nHeightCheckpoint: " + startHeight + ", denom: " + zCoin.getCoinDenomination() + ", for coin: " + zCoin);
// Look for an older one here..
throw new AccStoreException("Value not found", zCoin, nHeightCheckpoint);
throw new AccStoreException("WitValue not found", zCoin, startHeight);
}

GenWitMessage genWitMessage = new GenWitMessage(
Expand Down
1 change: 0 additions & 1 deletion tools/src/main/java/org/pivxj/tools/BuildCheckpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import sun.applet.Main;

import java.io.DataOutputStream;
import java.io.File;
Expand Down

0 comments on commit 4fed27f

Please sign in to comment.