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());
}
}
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;
}
}
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;
}
Aggregations