Search in sources :

Example 1 with RewardItem

use of io.nuls.consensus.poc.model.RewardItem in project nuls by nuls-io.

the class PocRewardCacheService method addRewardItem.

private void addRewardItem(long height, long time, Coin coin, long startTime) {
    // String address = AddressTool.getStringAddressByBytes(coin.());
    String address = AddressTool.getStringAddressByBytes(coin.getAddress());
    Map<Long, RewardItem> map = todayRewardMap.get(address);
    if (null == map) {
        map = new HashMap<>();
        todayRewardMap.put(address, map);
    }
    if (time > startTime) {
        map.put(height, new RewardItem(time, coin.getNa()));
    }
    Na tna = todayMap.get(address);
    if (tna == null) {
        tna = Na.ZERO;
    }
    tna = tna.add(coin.getNa());
    todayMap.put(address, tna);
    Na na = totalMap.get(address);
    if (na == null) {
        na = Na.ZERO;
    }
    na = na.add(coin.getNa());
    totalMap.put(address, na);
    if (height > totalRewardHeight) {
        totalReward = totalReward.add(coin.getNa());
    }
}
Also used : Na(io.nuls.kernel.model.Na) RewardItem(io.nuls.consensus.poc.model.RewardItem)

Example 2 with RewardItem

use of io.nuls.consensus.poc.model.RewardItem in project nuls by nuls-io.

the class PocRewardCacheService method rollback.

public void rollback(Block block) {
    if (block.getHeader().getHeight() > endHeight) {
        return;
    }
    CoinBaseTransaction tx = (CoinBaseTransaction) block.getTxs().get(0);
    if (null != tx.getCoinData().getTo() && !tx.getCoinData().getTo().isEmpty()) {
        for (Coin coin : tx.getCoinData().getTo()) {
            // String address = AddressTool.getStringAddressByBytes(coin.());
            String address = AddressTool.getStringAddressByBytes(coin.getAddress());
            Map<Long, RewardItem> map = todayRewardMap.get(address);
            if (null == map) {
                continue;
            }
            map.remove(block.getHeader().getHeight());
            Na na = totalMap.get(address);
            if (na == null) {
                continue;
            }
            na = na.subtract(coin.getNa());
            todayMap.put(address, na);
        }
    }
    if (endHeight == block.getHeader().getHeight()) {
        endHeight = block.getHeader().getHeight() - 1;
    }
}
Also used : Coin(io.nuls.kernel.model.Coin) Na(io.nuls.kernel.model.Na) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) RewardItem(io.nuls.consensus.poc.model.RewardItem)

Example 3 with RewardItem

use of io.nuls.consensus.poc.model.RewardItem in project nuls by nuls-io.

the class PocRewardCacheService method calcRewards.

public void calcRewards() {
    List<String> list = new ArrayList<>(todayRewardMap.keySet());
    Na na = Na.ZERO;
    Map<String, Na> resultMap = new HashMap<>();
    long startTime = TimeService.currentTimeMillis() - 24 * 3600000L;
    for (String address : list) {
        Na reward = Na.ZERO;
        List<RewardItem> rewardItems = new ArrayList<>(todayRewardMap.get(address).values());
        for (RewardItem item : rewardItems) {
            if (item.getTime() < startTime) {
                continue;
            }
            reward = reward.add(item.getNa());
        }
        na = na.add(reward);
        resultMap.put(address, reward);
    }
    this.todayReward = na;
    this.todayMap = resultMap;
}
Also used : Na(io.nuls.kernel.model.Na) RewardItem(io.nuls.consensus.poc.model.RewardItem)

Aggregations

RewardItem (io.nuls.consensus.poc.model.RewardItem)3 Na (io.nuls.kernel.model.Na)3 Coin (io.nuls.kernel.model.Coin)1 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)1