Search in sources :

Example 11 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class ScriptTransactionTestTool method test.

// @Test
public void test() throws Exception {
    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)

Example 12 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class ConvertCoinTool method convertCoin.

public static Coin convertCoin(InputDto utxo) {
    Coin coin = new Coin();
    byte[] txHashBytes = Hex.decode(utxo.getFromHash());
    coin.setOwner(ArraysTool.concatenate(txHashBytes, new VarInt(utxo.getFromIndex()).encode()));
    coin.setLockTime(utxo.getLockTime());
    coin.setNa(Na.valueOf(utxo.getValue()));
    coin.setTempOwner(AddressTool.getAddress(utxo.getAddress()));
    return coin;
}
Also used : Coin(io.nuls.kernel.model.Coin) VarInt(io.nuls.kernel.utils.VarInt)

Example 13 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class CreateAgentTxValidator method validate.

@Override
public ValidateResult validate(CreateAgentTransaction tx) {
    Agent agent = tx.getTxData();
    if (null == agent) {
        return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    if (!AddressTool.validNormalAddress(agent.getPackingAddress())) {
        return ValidateResult.getFailedResult(getClass().getName(), AccountErrorCode.ADDRESS_ERROR);
    }
    if (Arrays.equals(agent.getAgentAddress(), agent.getPackingAddress())) {
        return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENTADDR_AND_PACKING_SAME);
    }
    if (Arrays.equals(agent.getRewardAddress(), agent.getPackingAddress())) {
        return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.REWARDADDR_PACKING_SAME);
    }
    if (tx.getTime() <= 0) {
        return ValidateResult.getFailedResult(getClass().getName(), KernelErrorCode.DATA_ERROR);
    }
    double commissionRate = agent.getCommissionRate();
    if (commissionRate < PocConsensusProtocolConstant.MIN_COMMISSION_RATE || commissionRate > PocConsensusProtocolConstant.MAX_COMMISSION_RATE) {
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), PocConsensusErrorCode.COMMISSION_RATE_OUT_OF_RANGE);
    }
    if (PocConsensusProtocolConstant.AGENT_DEPOSIT_LOWER_LIMIT.isGreaterThan(agent.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (PocConsensusProtocolConstant.AGENT_DEPOSIT_UPPER_LIMIT.isLessThan(agent.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!isDepositOk(agent.getDeposit(), tx.getCoinData())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(tx.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    try {
        if (!SignatureUtil.containsAddress(tx, agent.getAgentAddress())) {
            ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
            result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
            return result;
        }
    } catch (NulsException e) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    CoinData coinData = tx.getCoinData();
    Set<String> addressSet = new HashSet<>();
    int lockCount = 0;
    for (Coin coin : coinData.getTo()) {
        if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
            lockCount++;
        }
        // addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
        addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
    }
    if (lockCount > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (addressSet.size() > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Coin(io.nuls.kernel.model.Coin) NulsException(io.nuls.kernel.exception.NulsException) CoinData(io.nuls.kernel.model.CoinData) ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) HashSet(java.util.HashSet)

Example 14 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class StopAgentTxValidator method validate.

@Override
public ValidateResult validate(StopAgentTransaction data) throws NulsException {
    AgentPo agentPo = agentStorageService.get(data.getTxData().getCreateTxHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(data.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    if (!SignatureUtil.containsAddress(data, agentPo.getAgentAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    if (data.getCoinData().getTo() == null || data.getCoinData().getTo().isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    List<DepositPo> allDepositList = depositStorageService.getList();
    Map<NulsDigestData, DepositPo> depositMap = new HashMap<>();
    Na totalNa = agentPo.getDeposit();
    DepositPo ownDeposit = new DepositPo();
    ownDeposit.setDeposit(agentPo.getDeposit());
    ownDeposit.setAddress(agentPo.getAgentAddress());
    depositMap.put(data.getTxData().getCreateTxHash(), ownDeposit);
    for (DepositPo deposit : allDepositList) {
        if (deposit.getDelHeight() > -1L && (data.getBlockHeight() == -1L || deposit.getDelHeight() < data.getBlockHeight())) {
            continue;
        }
        if (!deposit.getAgentHash().equals(agentPo.getHash())) {
            continue;
        }
        depositMap.put(deposit.getTxHash(), deposit);
        totalNa = totalNa.add(deposit.getDeposit());
    }
    Na fromTotal = Na.ZERO;
    Map<String, Na> verifyToMap = new HashMap<>();
    for (Coin coin : data.getCoinData().getFrom()) {
        if (coin.getLockTime() != -1L) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        NulsDigestData txHash = new NulsDigestData();
        txHash.parse(coin.getOwner(), 0);
        DepositPo deposit = depositMap.remove(txHash);
        if (deposit == null) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        if (deposit.getAgentHash() == null && !coin.getNa().equals(agentPo.getDeposit())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        } else if (!deposit.getDeposit().equals(coin.getNa())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        fromTotal = fromTotal.add(coin.getNa());
        if (deposit.getAgentHash() == null) {
            continue;
        }
        String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
        Na na = verifyToMap.get(address);
        if (null == na) {
            na = deposit.getDeposit();
        } else {
            na = na.add(deposit.getDeposit());
        }
        verifyToMap.put(address, na);
    }
    if (!depositMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (!totalNa.equals(fromTotal)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Na ownToCoin = ownDeposit.getDeposit().subtract(data.getFee());
    long ownLockTime = 0L;
    for (Coin coin : data.getCoinData().getTo()) {
        // String address = AddressTool.getStringAddressByBytes(coin.());
        String address = AddressTool.getStringAddressByBytes(coin.getAddress());
        Na na = verifyToMap.get(address);
        if (null != na && na.equals(coin.getNa())) {
            verifyToMap.remove(address);
            continue;
        }
        if (ownToCoin != null && Arrays.equals(coin.getAddress(), ownDeposit.getAddress()) && coin.getNa().equals(ownToCoin)) {
            ownToCoin = null;
            ownLockTime = coin.getLockTime();
            continue;
        } else {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
    }
    if (ownLockTime < (data.getTime() + PocConsensusConstant.STOP_AGENT_LOCK_TIME)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    } else if (data.getBlockHeight() <= 0 && ownLockTime < (TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME - 300000L)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    }
    if (!verifyToMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) Coin(io.nuls.kernel.model.Coin) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 15 with Coin

use of io.nuls.kernel.model.Coin in project nuls by nuls-io.

the class DepositTxValidator method validate.

@Override
public ValidateResult validate(DepositTransaction tx) throws NulsException {
    if (null == tx || null == tx.getTxData() || null == tx.getTxData().getAgentHash() || null == tx.getTxData().getDeposit() || null == tx.getTxData().getAddress()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Deposit deposit = tx.getTxData();
    AgentPo agentPo = agentStorageService.get(deposit.getAgentHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    List<DepositPo> poList = this.getDepositListByAgent(deposit.getAgentHash());
    if (null != poList && poList.size() >= PocConsensusProtocolConstant.MAX_ACCEPT_NUM_OF_DEPOSIT) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_OVER_COUNT);
    }
    Na limit = PocConsensusProtocolConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
    Na max = PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
    Na total = Na.ZERO;
    for (DepositPo cd : poList) {
        total = total.add(cd.getDeposit());
    }
    if (limit.isGreaterThan(deposit.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (max.isLessThan(total.add(deposit.getDeposit()))) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!isDepositOk(deposit.getDeposit(), tx.getCoinData())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(tx.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    if (!SignatureUtil.containsAddress(tx, deposit.getAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    CoinData coinData = tx.getCoinData();
    Set<String> addressSet = new HashSet<>();
    int lockCount = 0;
    for (Coin coin : coinData.getTo()) {
        if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
            lockCount++;
        }
        // addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
        addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
    }
    if (lockCount > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (addressSet.size() > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CoinData(io.nuls.kernel.model.CoinData) ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) Coin(io.nuls.kernel.model.Coin) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

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