Skip to content

Commit

Permalink
Refine blockSigner for Java submit block on-time
Browse files Browse the repository at this point in the history
  • Loading branch information
AionJayT committed Apr 7, 2020
1 parent 708f845 commit dee0778
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Binary file added block_signer-1.1.jar
Binary file not shown.
Binary file modified block_signer.jar
Binary file not shown.
7 changes: 4 additions & 3 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<property name="dir.dist" value="${dir.local.workspace}/dist"/>
<property name="junit.heap" value="32m"/>
<property name="junit.threads" value="4"/>
<property name="version" value="1.1"/>

<target name="clean_build" depends="cleanmain">
<antcall target="buildmain">
Expand Down Expand Up @@ -39,7 +40,7 @@
</classpath>
</javac>

<jar destfile="${dir.build.main}/${ant.project.name}.jar">
<jar destfile="${dir.build.main}/${ant.project.name}-${version}.jar">
<fileset dir="${dir.build.main}"/>
<manifest>
<attribute name="Main-Class" value="org.aion.staker.BlockSigner"/>
Expand All @@ -48,8 +49,8 @@
</jar>

<move includeemptydirs="false" todir="${dir.dist}">
<file file="${dir.build.main}/${ant.project.name}.jar"/>
<file file="${dir.build.main}/${ant.project.name}-${version}.jar"/>
</move>
</target>

</project>
</project>
36 changes: 19 additions & 17 deletions src/org/aion/staker/BlockSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.aion.staker.chain.Transaction;
import org.aion.staker.utils.Logger;
import org.aion.staker.utils.PrivateKey;
import org.aion.util.bytes.ByteUtil;
import org.aion.util.conversions.Hex;

import java.security.InvalidKeyException;
Expand Down Expand Up @@ -84,30 +85,32 @@ public static void main(String[] args) throws InvalidKeySpecException, Interrupt

logger.log("Producing blocks now..");

byte[] lastSubmittedHash = null;
while(true) {
try {
byte[] oldSeed = rpc.getSeed();
byte[] nextSeed = getNextSeed(oldSeed);
byte[] blockHashToSign = rpc.submitSeed(nextSeed, stakerPrivateKey.getPublicKeyBytes(), coinbase.toByteArray());
if (blockHashToSign != null) {
byte[] signature = MessageSigner.signMessageFromKeyBytes(stakerPrivateKey.getPrivateKeyBytes(), blockHashToSign);
boolean submissionResult;
do {
submissionResult = Boolean.parseBoolean(rpc.submitSignature(signature, blockHashToSign));
if (submissionResult) {
logger.log("Block submitted successfully. Sleeping for " + submitSeedSleepTimeMillis + " ms");
if (nextSeed == null) {
logger.log("Could not submit POS blocks. A POW block is expected at the current block number. Sleeping for " + submitSeedSleepTimeMillis + " ms");
} else {
byte[] blockHashToSign = rpc.submitSeed(nextSeed, stakerPrivateKey.getPublicKeyBytes(), coinbase.toByteArray());
if (blockHashToSign != null) {
if (!Arrays.equals(lastSubmittedHash, blockHashToSign)) {
byte[] signature = MessageSigner.signMessageFromKeyBytes(stakerPrivateKey.getPrivateKeyBytes(), blockHashToSign);
boolean submissionResult = Boolean.parseBoolean(rpc.submitSignature(signature, blockHashToSign));
if (submissionResult) {
logger.log("Block submitted successfully. BlockMineHash: " + ByteUtil.toHexString(blockHashToSign));
lastSubmittedHash = blockHashToSign;
}
} else {
logger.log("This staker must wait longer to submit this block. Sleeping for " + submitSignatureSleepTimeMillis + " ms");
Thread.sleep(submitSignatureSleepTimeMillis);
logger.log("Block already submitted, blockMineHash: " + ByteUtil.toHexString(blockHashToSign));
}
} while (!submissionResult && Arrays.equals(oldSeed, rpc.getSeed()));

} else {
logger.log("Could not submit the POS block. Waiting for a POW block to be generated. Sleeping for " + submitSeedSleepTimeMillis + " ms");
} else {
logger.log("Could not submit the POS block. Waiting for a POW block to be generated. Sleeping for " + submitSeedSleepTimeMillis + " ms");
}
}
} catch (Exception e) {
logger.log("Block Signer caught exception " + e.getMessage());
logger.log("Sleeping for " + submitSeedSleepTimeMillis + " ms");
logger.log("Block Signer caught exception, " + Arrays.toString(e.getStackTrace()));
}

Thread.sleep(submitSeedSleepTimeMillis);
Expand All @@ -120,7 +123,6 @@ private static byte[] getNextSeed(byte[] oldSeed) throws NoSuchAlgorithmExceptio
if (oldSeed != null) {
seed = MessageSigner.signMessageFromKeyBytes(stakerPrivateKey.getPrivateKeyBytes(), oldSeed);
} else {
logger.log("Unity fork has not been reached yet.");
seed = null;
}
return seed;
Expand Down

0 comments on commit dee0778

Please sign in to comment.