Browse Source

Cache and bug fix

master
Slixe 1 year ago
parent
commit
e026d8d2fc
6 changed files with 161 additions and 43 deletions
  1. +12
    -5
      src/main/java/fr/slixe/tipbot/ArangoDatabaseService.java
  2. +38
    -0
      src/main/java/fr/slixe/tipbot/Cache.java
  3. +75
    -0
      src/main/java/fr/slixe/tipbot/Info.java
  4. +3
    -3
      src/main/java/fr/slixe/tipbot/TipBot.java
  5. +6
    -1
      src/main/java/fr/slixe/tipbot/command/DepositCommand.java
  6. +27
    -34
      src/main/java/fr/slixe/tipbot/command/InfoCommand.java

+ 12
- 5
src/main/java/fr/slixe/tipbot/ArangoDatabaseService.java View File

@@ -7,9 +7,9 @@ import java.util.Map;
import javax.inject.Singleton;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.krobot.config.ConfigProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoCursor;
@@ -23,7 +23,7 @@ import fr.slixe.dero4j.util.MapBuilder;
@Singleton
public class ArangoDatabaseService
{
private static final Logger log = LoggerFactory.getLogger("Arango");
private static final Logger log = LoggerContext.getContext().getLogger("Arango");
@Inject
private ConfigProvider config;
@@ -64,7 +64,7 @@ public class ArangoDatabaseService
}
catch (Exception ignored)
{
System.err.println("Couldn't connect to ArangoDB! Please verify your config file.");
log.error("Couldn't connect to ArangoDB! Please verify your config file.");
System.exit(1);
}
@@ -133,7 +133,14 @@ public class ArangoDatabaseService
public User getUser(String userId)
{
return users.getDocument(userId, User.class);
User user = users.getDocument(userId, User.class);
if (user == null) {
user = createUser(userId);
users.insertDocument(user);
}
return user;
}
public void updateUser(User user)


+ 38
- 0
src/main/java/fr/slixe/tipbot/Cache.java View File

@@ -0,0 +1,38 @@
package fr.slixe.tipbot;

import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.json.JSONObject;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import fr.slixe.dero4j.RequestException;

@Singleton
public final class Cache {

private static final Logger log = LoggerContext.getContext().getLogger("Cache");
@Inject
private TipBot bot;
private Info info;
public Info getInfo()
{
if (info == null || (info.getMillis() + 30 * 1000L < System.currentTimeMillis()))
{
log.info("Updating 'Info'");
try {
JSONObject json = bot.getDaemon().getInfo();
info = Info.fromJson(json);
} catch (RequestException e)
{
log.error("Can't update 'Info' !");
}
}

return info;
}
}

+ 75
- 0
src/main/java/fr/slixe/tipbot/Info.java View File

@@ -0,0 +1,75 @@
package fr.slixe.tipbot;

import java.math.BigInteger;

import org.json.JSONObject;

public class Info {

private int height;
private int topoHeight;
private double blockTime;
private BigInteger difficulty;
private int txMempool;
private int totalSupply;
private String daemonVersion;

private final long millis;
public Info(int height, int topoHeight, double blockTime, BigInteger difficulty, int txMempool, int totalSupply, String daemonVersion)
{
this.height = height;
this.topoHeight = topoHeight;
this.blockTime = blockTime;
this.difficulty = difficulty;
this.txMempool = txMempool;
this.totalSupply = totalSupply;
this.daemonVersion = daemonVersion;
this.millis = System.currentTimeMillis();
}

public int getHeight() {
return height;
}

public int getTopoHeight() {
return topoHeight;
}

public double getBlockTime() {
return blockTime;
}

public BigInteger getDifficulty() {
return difficulty;
}

public int getTxMempool() {
return txMempool;
}

public int getTotalSupply() {
return totalSupply;
}

public String getDaemonVersion() {
return daemonVersion;
}

public long getMillis()
{
return millis;
}

public static Info fromJson(JSONObject json)
{
return new Info(json.getInt("height"),
json.getInt("topoheight"),
json.getDouble("averageblocktime50"),
json.getBigInteger("difficulty"),
json.getInt("tx_pool_size"),
json.getInt("total_supply"),
json.getString("version"));
}
}

+ 3
- 3
src/main/java/fr/slixe/tipbot/TipBot.java View File

@@ -130,8 +130,8 @@ public class TipBot extends KrobotModule {
BigDecimal fee;
try {
fee = wallet.getApi().estimateFee(user.getWithdrawAddress(), amount);
amount = amount.subtract(fee);
wallet.getApi().transfer(user.getWithdrawAddress(), amount);
BigDecimal amountWithoutFee = amount.subtract(fee);
wallet.getApi().transfer(user.getWithdrawAddress(), amountWithoutFee);
} catch (RequestException ignored)
{
continue;
@@ -139,7 +139,7 @@ public class TipBot extends KrobotModule {
wallet.removeFunds(user.getKey(), amount);
}
return dialog("Withdraw Mass", "All coins have been sent back.");
}).filter(roleFilter);


+ 6
- 1
src/main/java/fr/slixe/tipbot/command/DepositCommand.java View File

@@ -54,6 +54,11 @@ public class DepositCommand implements CommandHandler {
}

String userId = wallet.getDB().getUserIdFromPaymentId(payment.getPaymentId());
if (userId == null) {
throw new CommandException("No user found for this tx hash.");
}

tx = new Transaction(payment.getTxHash(), userId, payment.getBlockHeight(), payment.getAmount());
wallet.getDB().addTx(tx);
wallet.addUnconfirmedFunds(userId, tx.getAmount());
@@ -67,7 +72,7 @@ public class DepositCommand implements CommandHandler {
builder.append("**Confirmations:** ").append(tx.getConfirmations()).append("\n");

chan.sendMessage(bot.dialog("Deposit", builder.toString())).queue();
return null;
}



+ 27
- 34
src/main/java/fr/slixe/tipbot/command/InfoCommand.java View File

@@ -1,14 +1,20 @@
package fr.slixe.tipbot.command;
import java.text.DateFormat;
import java.util.Date;
import javax.inject.Inject;
import org.json.JSONObject;
import org.krobot.MessageContext;
import org.krobot.command.ArgumentMap;
import org.krobot.command.Command;
import org.krobot.command.CommandHandler;
import org.krobot.util.Dialog;
import org.krobot.util.Markdown;
import fr.slixe.dero4j.RequestException;
import fr.slixe.tipbot.Cache;
import fr.slixe.tipbot.Info;
import fr.slixe.tipbot.TipBot;
import fr.slixe.tipbot.Wallet;
import net.dv8tion.jda.core.entities.MessageChannel;
@@ -17,12 +23,17 @@ import net.dv8tion.jda.core.entities.PrivateChannel;
@Command(value = "info", desc = "DERO Network information", errorMP = true)
public class InfoCommand implements CommandHandler
{
private static final DateFormat dateFormat = DateFormat.getInstance();
@Inject
private Wallet wallet;
@Inject
private TipBot bot;
@Inject
private Cache cache;
@Override
public Object handle(MessageContext ctx, ArgumentMap args) throws Exception
{
@@ -37,46 +48,28 @@ public class InfoCommand implements CommandHandler
try {
walletHeight = this.wallet.getApi().getHeight();
} catch (RequestException e) {
e.printStackTrace();
throw new CommandException("Wallet isn't available!");
chan.sendMessage(bot.dialog("Wallet information", "**Height**: " + walletHeight)).queue();
} catch (RequestException ignored) {
chan.sendMessage(Dialog.error("Wallet information", "Wallet isn't available.")).queue();
//throw new CommandException("Wallet isn't available!");
}
chan.sendMessage(bot.dialog("Wallet information", "Height: " + walletHeight)).queue();
Info info = cache.getInfo();
int height; //stable_height or height?
int topoHeight; //topoheight
double blockTime;
int difficulty;
int txMempool; //tx_pool_size
int totalSupply;
String daemonVersion;
try {
JSONObject json = bot.getDaemon().getInfo();
height = json.getInt("height");
topoHeight = json.getInt("topoheight");
blockTime = json.getDouble("averageblocktime50");
difficulty = json.getInt("difficulty");
txMempool = json.getInt("tx_pool_size");
totalSupply = json.getInt("total_supply");
daemonVersion = json.getString("version");
} catch (RequestException e)
{
throw new CommandException("Daemon isn't available!");
if (info == null) {
throw new CommandException("The daemon isn't available.");
}
StringBuilder builder = new StringBuilder();
builder.append("Height / Topoheight: ").append(height + " / " + topoHeight).append("\n");
builder.append("Average Block Time: ").append(blockTime).append("s").append("\n");
builder.append("Difficulty: ").append(difficulty).append("\n");
builder.append("Mempool: ").append(txMempool).append("\n");
builder.append("Total Supply: ").append(totalSupply).append("\n");
builder.append("Daemon Version: ").append(daemonVersion).append("\n");
builder.append("**Height / Topoheight:** ").append(info.getHeight() + " / " + info.getTopoHeight()).append("\n");
builder.append("**Average Block Time:** ").append(info.getBlockTime()).append("s").append("\n");
builder.append("**Difficulty:** ").append(info.getDifficulty()).append("\n");
builder.append("**Mempool:** ").append(info.getTxMempool()).append("\n");
builder.append("**Total Supply:** ").append(info.getTotalSupply()).append("\n");
builder.append("**Daemon Version:** ").append(info.getDaemonVersion()).append("\n");
builder.append("\n").append(Markdown.italic(dateFormat.format(new Date(info.getMillis()))));
chan.sendMessage(bot.dialog("Network information", builder.toString())).queue();
return null;
}
}
}

Loading…
Cancel
Save