Commit a6f94adb authored by zeroleak's avatar zeroleak
Browse files

add liquidities up to max-anonymity-set when round is ready

parent 85fcfb00
......@@ -47,8 +47,8 @@ server.round.liquidity-timeout = 60
Round will start when *server.round.anonymity-set-target* (mustMix + liquidities) are registered.<br/>
If this target is not met after *server.round.anonymity-set-adjust-timeout*, it will be gradually decreased to *server.round.anonymity-set-min*.<br/>
At the beginning of the round, only mustMix can register and liquidities are placed on a waiting pool.<br/>
After *server.round.liquidity-timeout*, liquidities are added as soon as *server.round.must-mix-min* is reached, up to *server.round.anonymity-set-max* inputs (mustMix + liquidities).
At the beginning of the round, only mustMix can register. Meanwhile, liquidities connecting are placed on a waiting pool.<br/>
After *server.round.liquidity-timeout* or when current *anonymity-set-target* is reached, liquidities are added as soon as *server.round.must-mix-min* is reached, up to *server.round.anonymity-set-max* inputs for the round.
### Testing
```
......
......@@ -106,7 +106,7 @@ public class RoundLimitsManager {
default:
// should never use this default value
timeToWait = 10000;
timeToWait = 30000;
break;
}
return timeToWait;
......@@ -119,6 +119,9 @@ public class RoundLimitsManager {
long elapsedTime = timeoutWatcher.computeElapsedTime();
if (!round.isAcceptLiquidities() && computeTimeToWaitAcceptLiquidities(elapsedTime) <= 0) {
// accept liquidities
if (log.isDebugEnabled()) {
log.debug("accepting liquidities now (liquidityTimeout elapsed)");
}
round.setAcceptLiquidities(true);
addLiquidities(round);
}
......@@ -180,9 +183,29 @@ public class RoundLimitsManager {
this.roundWatchers.put(roundId, roundLimitsWatcher);
}
if (round.isAcceptLiquidities() && round.getNbInputsLiquidities() == 0 && round.hasMinMustMixReached()) {
// we just reached minMustMix and acceptLiquidities was enabled earlier => add liquidities now
addLiquidities(round);
// avoid concurrent liquidity management
if (round.getNbInputsLiquidities() == 0) {
if (!round.isAcceptLiquidities()) {
if (roundService.isRegisterInputReady(round)) {
// round is ready to start => add liquidities now
if (log.isDebugEnabled()) {
log.debug("adding liquidities now (round is ready to start)");
}
round.setAcceptLiquidities(true);
addLiquidities(round);
}
}
else {
if (round.hasMinMustMixReached()) {
// round is not ready to start but minMustMix reached and liquidities accepted => ad liquidities now
if (log.isDebugEnabled()) {
log.debug("adding liquidities now (minMustMix reached and acceptLiquidities=true)");
}
addLiquidities(round);
}
}
}
}
......@@ -193,14 +216,18 @@ public class RoundLimitsManager {
return;
}
int liquiditiesToAdd = round.getTargetAnonymitySet() - round.getNbInputs();
int liquiditiesToAdd = round.getMaxAnonymitySet() - round.getNbInputs();
if (liquiditiesToAdd > 0) {
// round needs liquidities
try {
LiquidityPool liquidityPool = getLiquidityPool(round);
if (log.isDebugEnabled()) {
log.debug("Adding up to " + liquiditiesToAdd + " liquidities... ("+liquidityPool.getNbLiquidities()+" available)");
}
int liquiditiesAdded = 0;
while (liquiditiesAdded < liquiditiesToAdd && liquidityPool.hasLiquidity()) {
log.info("Adding liquidity " + (liquiditiesAdded + 1) + "/" + liquiditiesToAdd);
log.info("Adding liquidity " + (liquiditiesAdded + 1) + "(" + liquiditiesToAdd + " max.)");
RegisteredLiquidity randomLiquidity = liquidityPool.peekRandomLiquidity();
try {
roundService.addLiquidity(round, randomLiquidity);
......@@ -210,10 +237,6 @@ public class RoundLimitsManager {
// ignore the error and continue with more liquidity
}
}
int missingLiquidities = liquiditiesToAdd - liquiditiesAdded;
if (missingLiquidities > 0) {
log.warn("Not enough liquidities to start the round now! " + missingLiquidities + " liquidities missing");
}
}
catch(Exception e) {
log.error("Unexpected exception", e);
......
......@@ -110,9 +110,22 @@ public class RoundService {
RegisteredLiquidity registeredInputQueued = new RegisteredLiquidity(registeredInput, signedBordereauToReply);
liquidityPool.registerLiquidity(registeredInputQueued);
log.info(" • queued liquidity: " + registeredInputQueued.getRegisteredInput().getInput() + " (" + liquidityPool.getNbLiquidities() + " liquidities in pool)");
logRoundStatus(round);
}
private synchronized void registerInput(Round round, RegisteredInput registeredInput, byte[] signedBordereauToReply, boolean isLiquidity) throws IllegalInputException, RoundException {
// registerInput + response
doRegisterInput(round, registeredInput, signedBordereauToReply, isLiquidity);
// check round limits
roundLimitsManager.onInputRegistered(round);
// check round ready
checkRegisterInputReady(round);
}
private void doRegisterInput(Round round, RegisteredInput registeredInput, byte[] signedBordereauToReply, boolean isLiquidity) throws IllegalInputException, RoundException {
TxOutPoint input = registeredInput.getInput();
String username = registeredInput.getUsername();
......@@ -131,18 +144,16 @@ public class RoundService {
// add immediately to round inputs
round.registerInput(registeredInput);
log.info(" • registered "+(isLiquidity ? "liquidity" : "mustMix")+": " + registeredInput.getInput());
roundLimitsManager.onInputRegistered(round);
logRoundStatus(round);
// response
RegisterInputResponse registerInputResponse = new RegisterInputResponse();
registerInputResponse.signedBordereau = signedBordereauToReply;
webSocketService.sendPrivate(username, registerInputResponse);
checkRegisterInputReady(round);
}
public void addLiquidity(Round round, RegisteredLiquidity randomLiquidity) throws Exception {
registerInput(round, randomLiquidity.getRegisteredInput(), randomLiquidity.getSignedBordereau(), true);
doRegisterInput(round, randomLiquidity.getRegisteredInput(), randomLiquidity.getSignedBordereau(), true);
}
private boolean isRegisterLiquiditiesOpen(Round round) {
......@@ -150,7 +161,7 @@ public class RoundService {
// wait to get enough mustMix before accepting liquidities
return false;
}
if (round.isAcceptLiquidities()) {
if (!round.isAcceptLiquidities()) {
return false;
}
return true;
......@@ -174,7 +185,7 @@ public class RoundService {
return (input.getValue() == spendAmount);
}
protected boolean isRegisterInputReady(Round round) {
public boolean isRegisterInputReady(Round round) {
if (round.getNbInputs() == 0) {
return false;
}
......@@ -202,7 +213,6 @@ public class RoundService {
}
public void checkRegisterInputReady(Round round) {
logRoundStatus(round);
if (isRegisterInputReady(round)) {
changeRoundStatus(round.getRoundId(), RoundStatus.REGISTER_OUTPUT);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment