Search in sources :

Example 1 with CoinBaseTransaction

use of io.nuls.ledger.entity.tx.CoinBaseTransaction in project nuls by nuls-io.

the class ConsensusMeetingRunner method coinBaseTx.

private void coinBaseTx(List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_COIN_BASE));
    data.setFee(Na.ZERO);
    List<ConsensusReward> rewardList = calcReward(txList, self);
    Na total = Na.ZERO;
    for (ConsensusReward reward : rewardList) {
        Coin coin = new Coin();
        coin.setNa(reward.getReward());
        data.addTo(reward.getAddress(), coin);
        total = total.add(reward.getReward());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setFee(Na.ZERO);
    tx.setHash(NulsDigestData.calcDigestData(tx));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
    ValidateResult validateResult = tx.verify();
    confirmingTxCacheManager.putTx(tx);
    if (null == validateResult || validateResult.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.CONSENSUS_EXCEPTION);
    }
    try {
        ledgerService.approvalTx(tx);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    txList.add(0, tx);
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Na(io.nuls.core.chain.entity.Na) ConsensusReward(io.nuls.consensus.entity.meeting.ConsensusReward) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 2 with CoinBaseTransaction

use of io.nuls.ledger.entity.tx.CoinBaseTransaction in project nuls by nuls-io.

the class GenesisBlock method initGengsisTxs.

private void initGengsisTxs(Map<String, Object> jsonMap) {
    List<Map<String, Object>> list = (List<Map<String, Object>>) jsonMap.get(CONFIG_FILED_TXS);
    if (null == list || list.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.CONFIG_ERROR);
    }
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, Na.ZERO);
    data.setPriKey(Hex.decode(priKey));
    Na total = Na.ZERO;
    for (Map<String, Object> map : list) {
        String address = (String) map.get(CONFIG_FILED_ADDRESS);
        AssertUtil.canNotEmpty(address, ErrorCode.NULL_PARAMETER);
        Integer nuls = (Integer) map.get(CONFIG_FILED_NULS);
        AssertUtil.canNotEmpty(nuls, ErrorCode.NULL_PARAMETER);
        Integer height = (Integer) map.get(CONFIG_FILED_UNLOCK_HEIGHT);
        Coin coin = new Coin();
        coin.setNa(Na.parseNuls(nuls));
        coin.setUnlockTime(0);
        if (height == null) {
            coin.setUnlockTime(0);
        } else {
            coin.setUnlockHeight(height.longValue());
        }
        data.addTo(address, coin);
        total = total.add(coin.getNa());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx = null;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setTime(this.blockTime);
    tx.setFee(Na.ZERO);
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    P2PKHScriptSig p2PKHScriptSig = new P2PKHScriptSig();
    Account account = null;
    try {
        account = AccountTool.createAccount(priKey);
    } catch (NulsException e) {
        e.printStackTrace();
    }
    AccountService accountService = NulsContext.getServiceBean(AccountService.class);
    P2PKHScriptSig scriptSig = null;
    try {
        scriptSig = accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, "");
    } catch (NulsException e) {
        e.printStackTrace();
    }
    try {
        tx.setScriptSig(scriptSig.serialize());
    } catch (IOException e) {
        e.printStackTrace();
    }
    List<Transaction> txlist = new ArrayList<>();
    // tx.setStatus(TxStatusEnum.AGREED);
    txlist.add(tx);
    setTxs(txlist);
}
Also used : Account(io.nuls.account.entity.Account) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) Coin(io.nuls.ledger.entity.params.Coin) P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) AccountService(io.nuls.account.service.intf.AccountService)

Aggregations

NulsException (io.nuls.core.exception.NulsException)2 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)2 Coin (io.nuls.ledger.entity.params.Coin)2 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 Account (io.nuls.account.entity.Account)1 AccountService (io.nuls.account.service.intf.AccountService)1 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 Na (io.nuls.core.chain.entity.Na)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1 ValidateResult (io.nuls.core.validate.ValidateResult)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1