Skip to content

Commit

Permalink
Update for 2.0 Semux API
Browse files Browse the repository at this point in the history
  • Loading branch information
orogvany committed May 23, 2018
1 parent 3283b61 commit e6fdee2
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 231 deletions.
22 changes: 4 additions & 18 deletions config/semuxpool.properties
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
# #######################################
# General/required configuration
# #######################################

# Delegate Address
delegateAddress = <FILL IN DELEGATE ADDRESS>

# Pull pool profits out to a more secure account
# To leave them on the delegate, put delegate address here
# Using this limits your exposure to bugs in this software
#
# Additionally, you can specify multiple addresses with percents
# You can specify multiple addresses with percents
# if you want multiple pool operator members.
# This also works if you want to run a private pool. You could set pool fee to 1.0
# and then balance your payouts just with this mechanism
#
# Format: address:0.25,address2:0.75
# Note: these numbers need to add up to 1.0
Expand All @@ -34,6 +26,9 @@ poolFeePercent = 0.20
# format is 0xVoterOnesAddress, 0xVoter2sAddress, ...
voterWhiteList =

# Similarly, if someone is abusing your pool, stop paying them.
voterBlackList =

# Connection for REST API
apiHost = localhost
apiUser = user
Expand Down Expand Up @@ -93,15 +88,6 @@ dataDirectory = payouts
# the note to apply on payments
paymentNote = semuxpool.com

# Minimum number of blocks forged before paying out
# once per hour is 120
# once per day is 2880
# once per week is 20160
# Note: this is only used if 'payoutTime' is not selected.
# It is currently uncommon to want to pay on a block schedule, and
# this is left merely for legacy purposes
payoutEveryNBlocks = 2880

# % of profits to donate to developer for beer
# to keep working on this.
# This is purely optional, set it to 0 if you wish, but
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<dependency>
<groupId>com.github.orogvany</groupId>
<artifactId>semux-java-client</artifactId>
<version>78c521ed03560413e0694921fb6e51ca6b6a3464</version>
<version>eb01d60b5b318080c27825311128ef3c358505f6</version>
</dependency>

<!-- http client -->
Expand Down
158 changes: 51 additions & 107 deletions src/main/java/com/semuxpool/pool/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
/**
* Polls for new blocks and manages state
*/
public class Pool implements Runnable
{
public class Pool implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(Pool.class);

private final int loggingInterval;
private final SemuxClient client;
private final Persistence persistence;
private final String delegateAddress;
private final StatusLogger statusLogger = new StatusLogger();
private final Integer payOutNBlocks;
private final BlockResultFactory blockResultFactory;
private final long fee;
private final PoolPayer payer;
Expand All @@ -47,13 +45,11 @@ public class Pool implements Runnable
private PayoutFactory payoutFactory;

public Pool(SemuxClient client, Persistence persistence, String delegateAddress,
Integer payOutNBlocks, BlockResultFactory blockResultFactory, long fee,
PoolPayer payer, Long startBlock, LocalTime payoutTime, int loggingInterval)
{
BlockResultFactory blockResultFactory, long fee,
PoolPayer payer, Long startBlock, LocalTime payoutTime, int loggingInterval) {
this.client = client;
this.persistence = persistence;
this.delegateAddress = delegateAddress;
this.payOutNBlocks = payOutNBlocks;
this.blockResultFactory = blockResultFactory;
this.fee = fee;
this.payer = payer;
Expand All @@ -63,8 +59,7 @@ public Pool(SemuxClient client, Persistence persistence, String delegateAddress,
}

@Override
public void run()
{
public void run() {
initializePoolState();

long lastLog = 0;
Expand All @@ -73,34 +68,26 @@ public void run()
//once we're up to a block we
boolean isSynced = false;
//noinspection InfiniteLoopStatement
while (true)
{
if (System.currentTimeMillis() > (lastLog + loggingInterval))
{
while (true) {
if (System.currentTimeMillis() > (lastLog + loggingInterval)) {
statusLogger.logState(poolState);
lastLog = System.currentTimeMillis();
}
try
{
if (currentBlock % 10000 == 0)
{
try {
if (currentBlock % 10000 == 0) {
logger.info("Processed up to " + currentBlock);
}
Block block = getBlock(currentBlock);
if (block != null)
{
if (ourBlock(block))
{
if (block != null) {
if (ourBlock(block)) {
logger.info("Forged block " + block.getNumber());
blockResults.add(blockResultFactory.getBlockResult(block));
}

if (shouldPay(currentBlock, isSynced))
{
if (shouldPay(currentBlock, isSynced)) {
logger.info("Calculating payments...");
Payout payout = payoutFactory.getPayoutForBlockResults(blockResults, poolState.getCurrentBlock());
if (payout != null && !payout.getPayouts().isEmpty())
{
if (payout != null && !payout.getPayouts().isEmpty()) {
//pay out
payer.pay(payout, poolState);
//update running state
Expand All @@ -109,55 +96,39 @@ public void run()
blockResults.clear();
//set the current paid up to block
poolState.setLastPayoutDate(payout.getDate());
}
else
{
} else {
poolState.setLastPayoutDate(new Date());
}
poolState.setCurrentBlock(currentBlock + 1);
}

//next loop will add new block
currentBlock++;
}
else if (!isSynced)
{
} else if (!isSynced) {
logger.info("All caught up.");
//when we get a null block, we're up to date
isSynced = true;
}
}
catch (IOException e)
{
} catch (IOException e) {
logger.error("Error connecting to API", e);
}
catch (SemuxException e)
{
} catch (SemuxException e) {
logger.error("Error from API", e);
}

try
{
if (isSynced)
{
try {
if (isSynced) {
Thread.sleep(10000);
}
}
catch (InterruptedException e)
{
} catch (InterruptedException e) {
//ignore
}
}
}

private Block getBlock(long currentBlock)
{
try
{
private Block getBlock(long currentBlock) {
try {
return client.getBlock(currentBlock);
}
catch (IOException | SemuxException e)
{
} catch (IOException | SemuxException e) {
//most likely block not found
}
return null;
Expand All @@ -170,57 +141,38 @@ private Block getBlock(long currentBlock)
* @param isSynced if we're synced to latest block
* @return if it is time to flush payments
*/
private boolean shouldPay(long currentBlock, boolean isSynced)
{
if (payoutTime != null)
{
LocalDateTime currentTime = LocalDateTime.now();
private boolean shouldPay(long currentBlock, boolean isSynced) {
LocalDateTime currentTime = LocalDateTime.now();

Date lastPayout = poolState.getLastPayoutDate();
if (lastPayout != null)
{
LocalDateTime lastPaymentDate = LocalDateTime.ofInstant(lastPayout.toInstant(), ZoneId.systemDefault());
Date lastPayout = poolState.getLastPayoutDate();
if (lastPayout != null) {
LocalDateTime lastPaymentDate = LocalDateTime.ofInstant(lastPayout.toInstant(), ZoneId.systemDefault());

LocalDate dateOfPayment = lastPaymentDate.toLocalDate();
dateOfPayment = dateOfPayment.plusDays(1);
LocalDate dateOfPayment = lastPaymentDate.toLocalDate();
dateOfPayment = dateOfPayment.plusDays(1);

LocalDateTime targetDate = LocalDateTime.of(dateOfPayment, payoutTime);
if (currentTime.compareTo(targetDate) > 0)
{
if (isSynced)
{
logger.info("Time is " + payoutTime + ", time to pay!");
}
return isSynced;
}
else if (isSynced)
{
logger.info("Block " + currentBlock + " processed: waiting until " + payoutTime.toString() + " to pay.");
LocalDateTime targetDate = LocalDateTime.of(dateOfPayment, payoutTime);
if (currentTime.compareTo(targetDate) > 0) {
if (isSynced) {
logger.info("Time is " + payoutTime + ", time to pay!");
}
return isSynced;
} else if (isSynced) {
logger.info("Block " + currentBlock + " processed: waiting until " + payoutTime.toString() + " to pay.");
}
else
{
} else {

//is it past that hour today
if (currentTime.toLocalTime().compareTo(payoutTime) > 0)
{
if (isSynced)
{
logger.info("Time is pays " + payoutTime + ", time to pay!");
}
return isSynced;
}
else if (isSynced)
{
logger.info("Block " + currentBlock + " processed: No previous payment date - waiting until " + payoutTime.toString());
//is it past that hour today
if (currentTime.toLocalTime().compareTo(payoutTime) > 0) {
if (isSynced) {
logger.info("Time is pays " + payoutTime + ", time to pay!");
}
return isSynced;
} else if (isSynced) {
logger.info("Block " + currentBlock + " processed: No previous payment date - waiting until " + payoutTime.toString());
}
return false;
}
else
{
return (currentBlock > (poolState.getCurrentBlock() + payOutNBlocks)) && isSynced;
}
return false;
}

/**
Expand All @@ -229,34 +181,26 @@ else if (isSynced)
* @param block block
* @return true if we forged
*/
private boolean ourBlock(Block block)
{
private boolean ourBlock(Block block) {
return delegateAddress.contains(block.getCoinbase());
}

private void initializePoolState()
{
try
{
private void initializePoolState() {
try {
poolState = persistence.loadPoolState();
// also create the delegateName map
Delegate delegate = client.getDelegate(delegateAddress);
String delegateName = delegate.getName();
//create the PayoutFactory
payoutFactory = new PayoutFactory(delegateName, blockResultFactory.getPoolProfitsAddress(), fee);

if (startBlock > poolState.getCurrentBlock())
{
if (startBlock > poolState.getCurrentBlock()) {
poolState.setCurrentBlock(startBlock);
}
}
catch (IOException e)
{
} catch (IOException e) {
logger.error("Unable to load pool state", e);
System.exit(-1);
}
catch (SemuxException e)
{
} catch (SemuxException e) {
logger.error("Unable to connect to API", e);
System.exit(-1);
}
Expand Down
Loading

0 comments on commit e6fdee2

Please sign in to comment.