Skip to content

Commit

Permalink
queue next best deadline in case that currently committed does not ge…
Browse files Browse the repository at this point in the history
…t confirmed #26
  • Loading branch information
de-luxe committed Nov 17, 2016
1 parent 8c3f70a commit 478814a
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 37 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -4,7 +4,7 @@

<groupId>burstcoin</groupId>
<artifactId>burstcoin-jminer</artifactId>
<version>0.4.5-RELEASE</version>
<version>0.4.6-SNAPSHOT</version>
<packaging>jar</packaging>

<name>burstcoin-jminer</name>
Expand All @@ -23,7 +23,7 @@
<start-class>burstcoin.jminer.JMinerApplication</start-class>
<java.version>1.8</java.version>
<jetty.version>9.4.0.M0</jetty.version>
<jocl.version>0.1.9</jocl.version>
<jocl.version>2.0.0</jocl.version>
<saphir-hash.version>3.0.1</saphir-hash.version>
<bcprov-jdk15on.version>1.54</bcprov-jdk15on.version>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion run.bat
@@ -1 +1 @@
java -jar -d64 -XX:+UseG1GC burstcoin-jminer-0.4.5-RELEASE.jar
java -jar -d64 -XX:+UseG1GC burstcoin-jminer-0.4.6-SNAPSHOT.jar
6 changes: 3 additions & 3 deletions src/main/java/burstcoin/jminer/core/network/Network.java
Expand Up @@ -181,7 +181,7 @@ public void checkPoolInfo()
}
}

public void commitResult(long blockNumber, long calculatedDeadline, BigInteger nonce, BigInteger chunkPartStartNonce, long totalCapacity)
public void commitResult(long blockNumber, long calculatedDeadline, BigInteger nonce, BigInteger chunkPartStartNonce, long totalCapacity, BigInteger result)
{
if(poolMining)
{
Expand All @@ -197,14 +197,14 @@ public void commitResult(long blockNumber, long calculatedDeadline, BigInteger n
{
NetworkSubmitPoolNonceTask networkSubmitPoolNonceTask = context.getBean(NetworkSubmitPoolNonceTask.class);
networkSubmitPoolNonceTask.init(blockNumber, numericAccountId, poolServer, connectionTimeout, nonce,
chunkPartStartNonce, calculatedDeadline, totalCapacity);
chunkPartStartNonce, calculatedDeadline, totalCapacity, result);
networkPool.execute(networkSubmitPoolNonceTask);
}
}
else
{
NetworkSubmitSoloNonceTask networkSubmitSoloNonceTask = context.getBean(NetworkSubmitSoloNonceTask.class);
networkSubmitSoloNonceTask.init(blockNumber, passPhrase, soloServer, connectionTimeout, nonce, chunkPartStartNonce, calculatedDeadline);
networkSubmitSoloNonceTask.init(blockNumber, passPhrase, soloServer, connectionTimeout, nonce, chunkPartStartNonce, calculatedDeadline, result);
networkPool.execute(networkSubmitSoloNonceTask);
}
}
Expand Down
Expand Up @@ -38,6 +38,7 @@ public class NetworkResultConfirmedEvent
private BigInteger nonce;

private BigInteger chunkPartStartNonce;
private BigInteger result;

/**
* Instantiates a new Network result confirmed event.
Expand All @@ -47,7 +48,7 @@ public class NetworkResultConfirmedEvent
* @param nonce the nonce
* @param chunkPartStartNonce the chunk part start nonce
*/
public NetworkResultConfirmedEvent(long blockNumber, long deadline, BigInteger nonce, BigInteger chunkPartStartNonce)
public NetworkResultConfirmedEvent(long blockNumber, long deadline, BigInteger nonce, BigInteger chunkPartStartNonce, BigInteger result)
{
super(blockNumber);

Expand All @@ -56,6 +57,7 @@ public NetworkResultConfirmedEvent(long blockNumber, long deadline, BigInteger n
this.nonce = nonce;

this.chunkPartStartNonce = chunkPartStartNonce;
this.result = result;
}

/**
Expand Down Expand Up @@ -97,4 +99,9 @@ public BigInteger getNonce()
{
return nonce;
}

public BigInteger getResult()
{
return result;
}
}
Expand Up @@ -52,6 +52,7 @@ public interface Handler
private long strangeDeadline;

private BigInteger chunkPartStartNonce;
private BigInteger result;

/**
* Instantiates a new Network result error event.
Expand All @@ -62,7 +63,7 @@ public interface Handler
* @param strangeDeadline the strange deadline
* @param chunkPartStartNonce the chunk part start nonce
*/
public NetworkResultErrorEvent(long blockNumber, BigInteger nonce, long calculatedDeadline, long strangeDeadline, BigInteger chunkPartStartNonce)
public NetworkResultErrorEvent(long blockNumber, BigInteger nonce, long calculatedDeadline, long strangeDeadline, BigInteger chunkPartStartNonce, BigInteger result)
{
super(blockNumber);

Expand All @@ -72,6 +73,7 @@ public NetworkResultErrorEvent(long blockNumber, BigInteger nonce, long calculat
this.strangeDeadline = strangeDeadline;

this.chunkPartStartNonce = chunkPartStartNonce;
this.result = result;
}

/**
Expand Down Expand Up @@ -123,4 +125,9 @@ public long getStrangeDeadline()
{
return strangeDeadline;
}

public BigInteger getResult()
{
return result;
}
}
Expand Up @@ -74,9 +74,10 @@ public class NetworkSubmitPoolNonceTask
private BigInteger chunkPartStartNonce;
private long calculatedDeadline;
private long totalCapacity;
private BigInteger result;

public void init(long blockNumber, String numericAccountId, String poolServer, long connectionTimeout, BigInteger nonce, BigInteger chunkPartStartNonce,
long calculatedDeadline, long totalCapacity)
long calculatedDeadline, long totalCapacity, BigInteger result)
{
this.connectionTimeout = connectionTimeout;

Expand All @@ -88,6 +89,7 @@ public void init(long blockNumber, String numericAccountId, String poolServer, l
this.chunkPartStartNonce = chunkPartStartNonce;
this.calculatedDeadline = calculatedDeadline;
this.totalCapacity = totalCapacity;
this.result = result;
}

@Override
Expand All @@ -97,6 +99,8 @@ public void run()
{
long gb = totalCapacity / 1000 / 1000 / 1000;

// LOG.info(HEADER_MINER_NAME);

ContentResponse response = httpClient.POST(poolServer + "/burst")
.param("requestType", "submitNonce")
.param("accountId", numericAccountId)
Expand All @@ -112,7 +116,7 @@ public void run()
LOG.info("dl '" + calculatedDeadline + "' not accepted by pool!");
LOG.debug("Error code: '"+error.getErrorCode()+"'.");
LOG.debug("Error description: '"+error.getErrorDescription()+"'.");
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce, result));
}
else
{
Expand All @@ -122,26 +126,26 @@ public void run()
{
if(calculatedDeadline == result.getDeadline())
{
publisher.publishEvent(new NetworkResultConfirmedEvent(blockNumber, result.getDeadline(), nonce, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultConfirmedEvent(blockNumber, result.getDeadline(), nonce, chunkPartStartNonce, this.result));
}
else
{
// in general if deadlines do not match, we end up in errorCode above
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, result.getDeadline(), chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, result.getDeadline(), chunkPartStartNonce, this.result));
}
}
else
{
LOG.warn("Error: Submit nonce to pool not successful: " + response.getContentAsString());
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce, this.result));
}
}
}
catch(TimeoutException timeoutException)
{
LOG.warn("Nonce was committed to pool, but not confirmed ... caused by connectionTimeout,"
+ " currently '" + (connectionTimeout / 1000) + " sec.' try increasing it!");
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce, this.result));
}
catch(ExecutionException e)
{
Expand All @@ -159,12 +163,12 @@ public void run()
{
LOG.warn("Error: Failed to submit nonce to pool: " + e.getMessage());
}
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce, this.result));
}
catch(Exception e)
{
LOG.warn("Error: Failed to submit nonce to pool: " + e.getMessage());
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, -1L /*not delivered*/, chunkPartStartNonce, this.result));
}
}
}
Expand Up @@ -65,21 +65,22 @@ public class NetworkSubmitSoloNonceTask
private long blockNumber;
private BigInteger chunkPartStartNonce;
private long calculatedDeadline;
private BigInteger result;
private long connectionTimeout;

/**
* Init void.
*
* @param blockNumber the block number
* @param blockNumber the block number
* @param passPhrase the pass phrase
* @param soloServer the solo server
* @param connectionTimeout the connection timeout
* @param nonce the nonce
* @param chunkPartStartNonce the chunk part start nonce
* @param calculatedDeadline the calculated deadline
* @param result
*/
public void init(long blockNumber, String passPhrase, String soloServer, long connectionTimeout, BigInteger nonce, BigInteger chunkPartStartNonce,
long calculatedDeadline)
long calculatedDeadline, BigInteger result)
{
this.connectionTimeout = connectionTimeout;

Expand All @@ -90,6 +91,7 @@ public void init(long blockNumber, String passPhrase, String soloServer, long co
this.blockNumber = blockNumber;
this.chunkPartStartNonce = chunkPartStartNonce;
this.calculatedDeadline = calculatedDeadline;
this.result = result;
}

@Override
Expand All @@ -111,11 +113,11 @@ public void run()
{
if(calculatedDeadline == result.getDeadline())
{
publisher.publishEvent(new NetworkResultConfirmedEvent(blockNumber, result.getDeadline(), nonce, chunkPartStartNonce));
publisher.publishEvent(new NetworkResultConfirmedEvent(blockNumber, result.getDeadline(), nonce, chunkPartStartNonce, this.result));
}
else
{
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, result.getDeadline(), chunkPartStartNonce));
publisher.publishEvent(new NetworkResultErrorEvent(blockNumber, nonce, calculatedDeadline, result.getDeadline(), chunkPartStartNonce, this.result));
}
}
else
Expand Down
60 changes: 44 additions & 16 deletions src/main/java/burstcoin/jminer/core/round/Round.java
Expand Up @@ -97,6 +97,10 @@ public class Round
private BigInteger lowest;
private long bestCommittedDeadline;

// cache for next lowest
private CheckerResultEvent queuedEvent;
private BigInteger lowestCommitted;

// dev pool
private boolean devPool;
private List<DevPoolResult> devPoolResults;
Expand All @@ -106,13 +110,6 @@ public class Round
private Set<BigInteger> runningChunkPartStartNonces;
private Plots plots;

/**
* Instantiates a new Round.
*
* @param reader the reader
* @param checker the checker
* @param network the network
*/
@Autowired
public Round(Reader reader, Checker checker, Network network)
{
Expand All @@ -139,6 +136,8 @@ private void initNewRound(Plots plots)
runningChunkPartStartNonces = new HashSet<>(plots.getChunkPartStartNonces().keySet());
roundStartDate = new Date();
lowest = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
lowestCommitted = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);
queuedEvent = null;
devPoolResults = new ArrayList<>();
bestCommittedDeadline = Long.MAX_VALUE;
devPoolCommitsThisRound = devPoolCommitsPerRound;
Expand Down Expand Up @@ -180,11 +179,6 @@ public void run()
}
}

/**
* Handle message.
*
* @param event the event
*/
@EventListener
public void handleMessage(CheckerResultEvent event)
{
Expand All @@ -206,9 +200,6 @@ public void handleMessage(CheckerResultEvent event)
}
else
{
// todo there will be a lot of skipped, makes no sense, cause lowest not used here, remove or make adjustable by setting.
// publisher.publishEvent(new RoundSingleResultSkippedEvent(this, event.getBlockNumber(), nonce, event.getChunkPartStartNonce(), calculatedDeadline,
// targetDeadline, poolMining));
runningChunkPartStartNonces.remove(event.getChunkPartStartNonce());
triggerFinishRoundEvent(event.getBlockNumber());
}
Expand All @@ -220,7 +211,7 @@ public void handleMessage(CheckerResultEvent event)
lowest = event.getResult();
if(calculatedDeadline < targetDeadline)
{
network.commitResult(blockNumber, calculatedDeadline, nonce, event.getChunkPartStartNonce(), plots.getSize());
network.commitResult(blockNumber, calculatedDeadline, nonce, event.getChunkPartStartNonce(), plots.getSize(), event.getResult());

// ui event
fireEvent(new RoundSingleResultEvent(this, event.getBlockNumber(), nonce, event.getChunkPartStartNonce(), calculatedDeadline, poolMining));
Expand All @@ -235,6 +226,22 @@ public void handleMessage(CheckerResultEvent event)
triggerFinishRoundEvent(event.getBlockNumber());
}
}
// remember next lowest in case that lowest fails to commit
else if(calculatedDeadline < targetDeadline
&& event.getResult().compareTo(lowestCommitted) < 0
&& (queuedEvent == null || event.getResult().compareTo(queuedEvent.getResult()) < 0 ))
{
if(queuedEvent != null)
{
// remove previous queued
runningChunkPartStartNonces.remove(queuedEvent.getChunkPartStartNonce());
}
LOG.debug("new queued dl '" + calculatedDeadline +"'.");
queuedEvent = event;

// todo not sure if / why needed?!
triggerFinishRoundEvent(event.getBlockNumber());
}
else
{
// chunkPartStartNonce finished
Expand Down Expand Up @@ -321,6 +328,18 @@ public void handleMessage(NetworkResultConfirmedEvent event)
{
if(blockNumber == event.getBlockNumber())
{
lowestCommitted = event.getResult();

// if queuedLowest exist and is higher than lowestCommitted, remove queuedLowest
if(queuedEvent != null && lowestCommitted.compareTo(queuedEvent.getResult()) < 0 )
{
BigInteger dl = queuedEvent.getResult().divide(BigInteger.valueOf(baseTarget));
LOG.debug("remove queued dl '" + dl +"'.");

runningChunkPartStartNonces.remove(queuedEvent.getChunkPartStartNonce());
queuedEvent = null;
}

runningChunkPartStartNonces.remove(event.getChunkPartStartNonce());
bestCommittedDeadline = event.getDeadline();
triggerFinishRoundEvent(event.getBlockNumber());
Expand Down Expand Up @@ -360,6 +379,15 @@ public void handleMessage(NetworkResultErrorEvent event)
{
if(blockNumber == event.getBlockNumber())
{
// reset lowest to lowestCommitted, as it does not commit successful.
lowest = lowestCommitted;
// in case that queued result is lower than committedLowest, commit queued again.
if(queuedEvent != null && lowestCommitted.compareTo(queuedEvent.getResult()) < 0)
{
LOG.debug("trigger commit queued deadline ...");
handleMessage(queuedEvent);
}

runningChunkPartStartNonces.remove(event.getChunkPartStartNonce());
triggerFinishRoundEvent(event.getBlockNumber());
}
Expand Down

0 comments on commit 478814a

Please sign in to comment.