use of io.nuls.kernel.model.Coin in project nuls by nuls-io.
the class ContractBalanceManager method getCoinListByAddress.
public List<Coin> getCoinListByAddress(byte[] address) {
List<Coin> coinList = new ArrayList<>();
List<Entry<byte[], byte[]>> rawList = contractUtxoStorageService.loadAllCoinList();
for (Entry<byte[], byte[]> coinEntry : rawList) {
Coin coin = new Coin();
try {
coin.parse(coinEntry.getValue(), 0);
} catch (NulsException e) {
Log.info("parse coin form db error");
continue;
}
if (Arrays.equals(coin.getAddress(), address)) {
coin.setOwner(coinEntry.getKey());
coin.setKey(asString(coinEntry.getKey()));
coinList.add(coin);
}
}
return coinList;
}
use of io.nuls.kernel.model.Coin 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.kernel.model.Coin in project nuls by nuls-io.
the class TotalCoinTask method doStatistics.
private void doStatistics() throws NulsException {
long height = NulsContext.getInstance().getBestHeight();
List<Entry<byte[], byte[]>> coinBytesList = getUtxoLedgerUtxoStorageService().getAllUtxoEntryBytes();
long totalNuls = 0;
long lockedNuls = 0;
Coin coin = new Coin();
for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
coin.parse(coinEntryBytes.getValue(), 0);
totalNuls += coin.getNa().getValue();
if (coin.getLockTime() == -1 || coin.getLockTime() > System.currentTimeMillis() || (coin.getLockTime() < 1531152000000L && coin.getLockTime() > height)) {
lockedNuls += coin.getNa().getValue();
}
}
NulsContext.totalNuls = totalNuls;
NulsContext.lockedNuls = lockedNuls;
}
use of io.nuls.kernel.model.Coin in project nuls by nuls-io.
the class UtxoResource method getUtxoByAddressAndAmount.
@GET
@Path("/amount/{address}/{amount}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "根据address和amount查询UTXO")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = AccountUtxoDto.class) })
public RpcClientResult getUtxoByAddressAndAmount(@ApiParam(name = "address", value = "地址", required = true) @PathParam("address") String address, @ApiParam(name = "amount", value = "金额", required = true) @PathParam("amount") Long amount) {
if (StringUtils.isBlank(address) || amount == null) {
return Result.getFailed(LedgerErrorCode.NULL_PARAMETER).toRpcClientResult();
}
if (!AddressTool.validAddress(address)) {
return Result.getFailed(LedgerErrorCode.PARAMETER_ERROR).toRpcClientResult();
}
Result result = null;
try {
List<Coin> coinList = getAllUtxoByAddress(address);
Na amountNa = Na.valueOf(amount.longValue());
AccountUtxoDto accountUtxoDto = new AccountUtxoDto();
List<UtxoDto> list = new LinkedList<>();
Na values = Na.ZERO;
for (Coin coin : coinList) {
if (!coin.usable()) {
continue;
}
if (coin.getNa().equals(Na.ZERO)) {
continue;
}
list.add(new UtxoDto(coin));
values = values.add(coin.getNa());
if (values.isGreaterOrEquals(amountNa)) {
break;
}
}
accountUtxoDto.setUtxoDtoList(list);
result = Result.getSuccess().setData(accountUtxoDto);
return result.toRpcClientResult();
} catch (Exception e) {
Log.error(e);
result = Result.getFailed(LedgerErrorCode.SYS_UNKOWN_EXCEPTION);
return result.toRpcClientResult();
}
}
use of io.nuls.kernel.model.Coin in project nuls by nuls-io.
the class ScriptTransactionTestTool method test1.
// @Test
public void test1() throws IOException {
NulsContext.MAIN_NET_VERSION = 2;
TransferTransaction tx = new TransferTransaction();
tx.setRemark("test script".getBytes());
CoinData data = new CoinData();
Coin coin = new Coin();
coin.setOwner(ArraysTool.concatenate(Hex.decode("0020dab71b3cd376e2ccf2f290e384d2917cc0929f8de582f63a01fc15144fe38371"), new byte[] { 0 }));
coin.setNa(Na.parseNuls(9997));
coin.setLockTime(0);
List<Coin> from = new ArrayList<>();
from.add(coin);
data.setFrom(from);
Coin toCoin = new Coin();
toCoin.setLockTime(0);
Script script = ScriptBuilder.createOutputScript(AddressTool.getAddress("NsdvuzHyQJEJkz4LEKweDeCs97845xN9"), 1);
toCoin.setOwner(script.getProgram());
toCoin.setNa(Na.parseNuls(9994));
List<Coin> to = new ArrayList<>();
to.add(toCoin);
data.setTo(to);
tx.setCoinData(data);
// ECKey ecKey = ECKey.fromPrivate(new BigInteger(1,Hex.decode("00b491621168dffd80c4684f7445ef378ba4d381b2fe2a7b1fbf905864ed8fbeb9")));
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("4b19caef601a45531b7068430a5b0e380a004001f14bfec025ddf16d5d87fa8e")));
List<ECKey> signEckeys = new ArrayList<>();
signEckeys.add(ecKey);
List<ECKey> scriptEckeys = new ArrayList<>();
SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
String param = "{\"txHex\": \"" + Hex.encode(tx.serialize()) + "\"}";
String res = post("http://127.0.0.1:7001/api/accountledger/transaction/valiTransaction", param, "utf-8");
System.out.println(res);
res = post("http://127.0.0.1:7001/api/accountledger/transaction/broadcast", param, "utf-8");
System.out.println(res);
}
Aggregations