use of org.xel.computation.CommandNewWork in project elastic-core-maven by OrdinaryDude.
the class CreateWork method processRequest.
@Override
protected JSONStreamAware processRequest(final HttpServletRequest req) throws NxtException {
final String programCode = ParameterParser.getParameterMultipart(req, "source_code");
final String deadline = ParameterParser.getParameterMultipart(req, "work_deadline");
final String xelPerBounty = ParameterParser.getParameterMultipart(req, "xel_per_bounty");
final String xelPerPow = ParameterParser.getParameterMultipart(req, "xel_per_pow");
final String bountiesPerIteration = ParameterParser.getParameterMultipart(req, "bounty_limit_per_iteration");
final String numberOfIterations = ParameterParser.getParameterMultipart(req, "iterations");
final String cap_number_pow = ParameterParser.getParameterMultipart(req, "cap_pow");
if (programCode == null || programCode.length() == 0)
return JSONResponses.MISSING_PROGAMCODE;
else if (deadline == null)
return JSONResponses.MISSING_DEADLINE;
else if (xelPerBounty == null)
return JSONResponses.MISSING_XEL_PER_BOUNTY;
else if (xelPerPow == null)
return JSONResponses.MISSING_XEL_PER_POW;
else if (bountiesPerIteration == null)
return JSONResponses.MISSING_BOUNTYLIMIT;
else if (numberOfIterations == null)
return JSONResponses.MISSING_ITERATIOS;
else if (cap_number_pow == null)
return JSONResponses.MISSING_CAPPOW;
int numeric_deadline = Integer.parseInt(deadline);
long numeric_xelPerPow = Long.parseLong(xelPerPow);
long numeric_xelPerBounty = Long.parseLong(xelPerBounty);
int numeric_bountiesPerIteration = Integer.parseInt(bountiesPerIteration);
int numeric_numberOfIterations = Integer.parseInt(numberOfIterations);
int numeric_cap_number_pow = Integer.parseInt(cap_number_pow);
if (!Commons.checkRange(ComputationConstants.DEADLINE_MIN, ComputationConstants.DEADLINE_MAX, numeric_deadline))
return JSONResponses.MISSING_DEADLINE;
if (!Commons.checkRange(ComputationConstants.XEL_POW_MIN, ComputationConstants.XEL_POW_MAX, numeric_xelPerPow))
return JSONResponses.MISSING_XEL_PER_POW;
if (!Commons.checkRange(ComputationConstants.XEL_BTY_MIN, ComputationConstants.XEL_BTY_MAX, numeric_xelPerBounty))
return JSONResponses.MISSING_XEL_PER_BOUNTY;
if (!Commons.checkRange(ComputationConstants.BTY_PER_ITER_MIN, ComputationConstants.BTY_PER_ITER_MAX, numeric_bountiesPerIteration))
return JSONResponses.MISSING_BOUNTYLIMIT;
if (!Commons.checkRange(ComputationConstants.ITER_MIN, ComputationConstants.ITER_MAX, numeric_numberOfIterations))
return JSONResponses.MISSING_ITERATIOS;
if (!Commons.checkRange(ComputationConstants.POW_MIN, ComputationConstants.POW_MAX, numeric_cap_number_pow))
return JSONResponses.MISSING_CAPPOW;
CommandNewWork work = new CommandNewWork(numeric_cap_number_pow, (short) numeric_deadline, numeric_xelPerPow, numeric_xelPerBounty, numeric_bountiesPerIteration, numeric_numberOfIterations, programCode.getBytes());
try {
MessageEncoder.push(work, ParameterParser.getSecretPhrase(req, true));
return JSONResponses.EVERYTHING_ALRIGHT;
} catch (IOException e) {
return JSONResponses.ERROR_INCORRECT_REQUEST;
}
}
use of org.xel.computation.CommandNewWork in project elastic-core-maven by OrdinaryDude.
the class Work method addWork.
public static void addWork(final Transaction transaction, final CommandNewWork attachment) {
final Work shuffling = new Work(transaction, attachment);
Work.workTable.insert(shuffling);
Work.listeners.notify(shuffling, Event.WORK_CREATED);
}
use of org.xel.computation.CommandNewWork in project elastic-core-maven by OrdinaryDude.
the class WorkTest method newWorkTest.
@Test
public void newWorkTest() throws NxtException, IOException {
redeemPubkeyhash();
String code = readFile("src/test/testfiles/op2.epl", Charset.forName("UTF-8"));
String doublecheckcode = new String(code.getBytes());
System.out.println("[!!]\tcode length: " + code.length());
CommandNewWork work = new CommandNewWork(100, (short) 15, 1000001, 1000001, 10, 10, code.getBytes());
MessageEncoder.push(work, AbstractForgingTest.testForgingSecretPhrase);
// Mine a bit so the work gets confirmed
AbstractBlockchainTest.forgeNumberOfBlocks(1, AbstractForgingTest.testForgingSecretPhrase);
// Test work db table
Assert.assertEquals(1, Work.getCount());
Assert.assertEquals(1, Work.getActiveCount());
long id = 0;
try (DbIterator<Work> wxx = Work.getActiveWork()) {
Work w = wxx.next();
id = w.getId();
System.out.println("Found work in DB with id = " + Long.toUnsignedString(w.getId()));
}
CommandCancelWork cancel = new CommandCancelWork(id);
MessageEncoder.push(cancel, AbstractForgingTest.testForgingSecretPhrase);
// Mine a bit so the work gets confirmed
AbstractBlockchainTest.forgeNumberOfBlocks(5, AbstractForgingTest.testForgingSecretPhrase);
System.out.println("LAST BLOCK:");
System.out.println(Nxt.getBlockchain().getLastBlock().getJSONObject().toJSONString());
// Test work db table
Assert.assertEquals(1, Work.getCount());
Assert.assertEquals(0, Work.getActiveCount());
Assert.assertEquals(7, Nxt.getBlockchain().getLastLocallyProcessedHeight());
}
use of org.xel.computation.CommandNewWork in project elastic-core-maven by OrdinaryDude.
the class WorkTest method newWorkTestWithNaturalTimeout.
@Test
public void newWorkTestWithNaturalTimeout() throws NxtException, IOException {
redeemPubkeyhash();
String code = readFile("src/test/testfiles/btc.epl", Charset.forName("UTF-8"));
System.out.println("[!!]\tcode length: " + code.length());
CommandNewWork work = new CommandNewWork(100, (short) 15, 1000001, 1000001, 10, 10, code.getBytes());
MessageEncoder.push(work, AbstractForgingTest.testForgingSecretPhrase);
// Mine a bit so the work gets confirmed
AbstractBlockchainTest.forgeNumberOfBlocks(1, AbstractForgingTest.testForgingSecretPhrase);
// Test work db table
Assert.assertEquals(1, Work.getCount());
Assert.assertEquals(1, Work.getActiveCount());
// Mine a bit so the work times out
AbstractBlockchainTest.forgeNumberOfBlocks(20, AbstractForgingTest.testForgingSecretPhrase);
// Test work db table
Assert.assertEquals(1, Work.getCount());
Assert.assertEquals(0, Work.getActiveCount());
Assert.assertEquals(22, Nxt.getBlockchain().getLastLocallyProcessedHeight());
}
use of org.xel.computation.CommandNewWork in project elastic-core-maven by OrdinaryDude.
the class WorkTest method newWorkTestWithEnoughPOWs.
@Test
public void newWorkTestWithEnoughPOWs() throws NxtException, IOException {
redeemPubkeyhash();
String code = readFile("src/test/testfiles/btc.epl", Charset.forName("UTF-8"));
CommandNewWork work = new CommandNewWork(10, (short) 100, 1000001, 1000001, 10, 10, code.getBytes());
MessageEncoder.push(work, AbstractForgingTest.testForgingSecretPhrase);
// Mine a bit so the work gets confirmed
AbstractBlockchainTest.forgeNumberOfBlocks(1, AbstractForgingTest.testForgingSecretPhrase);
long id = 0;
Work w;
try (DbIterator<Work> wxx = Work.getActiveWork()) {
w = wxx.next();
id = w.getId();
}
// Test work db table
Assert.assertEquals(1, Work.getCount());
Assert.assertEquals(1, Work.getActiveCount());
byte[] m = new byte[32];
byte[] m2 = new byte[32];
byte[] testarray = new byte[32 * 4];
for (int i = 0; i < 25; ++i) {
m[0] = (byte) (m[0] + 1);
CommandPowBty pow = new CommandPowBty(id, true, m, new byte[16], testarray, 0, w.getCurrentRound());
m2[0] = (byte) (m2[0] + 2);
m2[1] = 1;
CommandPowBty pow2 = new CommandPowBty(id, true, m2, new byte[16], testarray, 0, w.getCurrentRound());
try {
MessageEncoder.push(pow, AbstractForgingTest.testForgingSecretPhrase);
} catch (Exception e) {
Logger.logDebugMessage("Could not push POW: " + e.getMessage());
}
try {
MessageEncoder.push(pow2, AbstractForgingTest.testForgingSecretPhrase);
} catch (Exception e) {
Logger.logDebugMessage("Could not push POW: " + e.getMessage());
}
// Mine a bit so the work times out
AbstractBlockchainTest.forgeNumberOfBlocks(1, AbstractForgingTest.testForgingSecretPhrase);
}
AbstractBlockchainTest.forgeNumberOfBlocks(6, AbstractForgingTest.testForgingSecretPhrase);
// After getting enough Pow work must be closed
// Test work db table
Assert.assertEquals(1, Work.getCount());
Assert.assertEquals(0, Work.getActiveCount());
}
Aggregations