Search in sources :

Example 6 with Coin

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;
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry) NulsException(io.nuls.kernel.exception.NulsException)

Example 7 with Coin

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;
    }
}
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 8 with Coin

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;
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry)

Example 9 with Coin

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();
    }
}
Also used : Coin(io.nuls.kernel.model.Coin) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) RpcClientResult(io.nuls.kernel.model.RpcClientResult) Result(io.nuls.kernel.model.Result) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 10 with Coin

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);
}
Also used : Coin(io.nuls.kernel.model.Coin) Script(io.nuls.kernel.script.Script) CoinData(io.nuls.kernel.model.CoinData) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Aggregations

Coin (io.nuls.kernel.model.Coin)31 NulsException (io.nuls.kernel.exception.NulsException)16 Na (io.nuls.kernel.model.Na)8 Entry (io.nuls.db.model.Entry)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 CoinData (io.nuls.kernel.model.CoinData)5 TransactionSignature (io.nuls.kernel.script.TransactionSignature)4 ContractBalance (io.nuls.contract.ledger.module.ContractBalance)3 Result (io.nuls.kernel.model.Result)3 LedgerUtil.asString (io.nuls.ledger.util.LedgerUtil.asString)3 ArrayList (java.util.ArrayList)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)2 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)2 ECKey (io.nuls.core.tools.crypto.ECKey)2 RpcClientResult (io.nuls.kernel.model.RpcClientResult)2 Transaction (io.nuls.kernel.model.Transaction)2 Script (io.nuls.kernel.script.Script)2