Search in sources :

Example 56 with Result

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

the class TransactionServiceImpl method commitTx.

/**
 * 确认交易时调用的方法,对交易相关的业务进行提交操作
 * Identify the method that is invoked during the transaction and submit the transaction related business.
 *
 * @param tx            操作的交易/The transaction of the operation
 * @param secondaryData 辅助数据(可以为空)/Secondary data (available for null)
 * @return 操作结果/operating results
 */
@Override
public Result commitTx(Transaction tx, Object secondaryData) {
    List<TransactionProcessor> processorList = TransactionManager.getProcessorList(tx.getClass());
    List<TransactionProcessor> commitedProcessorList = new ArrayList<>();
    for (TransactionProcessor processor : processorList) {
        Result result = processor.onCommit(tx, secondaryData);
        if (result.isSuccess()) {
            commitedProcessorList.add(processor);
        } else {
            for (int i = commitedProcessorList.size() - 1; i >= 0; i--) {
                TransactionProcessor processor1 = commitedProcessorList.get(i);
                processor1.onRollback(tx, secondaryData);
            }
            return result;
        }
    }
    return Result.getSuccess();
}
Also used : TransactionProcessor(io.nuls.kernel.processor.TransactionProcessor) ArrayList(java.util.ArrayList) ValidateResult(io.nuls.kernel.validate.ValidateResult) Result(io.nuls.kernel.model.Result)

Example 57 with Result

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

the class TransactionServiceImpl method rollbackTx.

/**
 * 回滚交易时调用的方法,对交易相关的业务进行回退操作
 * The method invoked when the transaction is rolled back and the transaction related business is returned.
 *
 * @param tx            操作的交易/The transaction of the operation
 * @param secondaryData 辅助数据(可以为空)/Secondary data (available for null)
 * @return 操作结果/operating results
 */
@Override
public Result rollbackTx(Transaction tx, Object secondaryData) {
    if (null == tx) {
        return Result.getSuccess();
    }
    List<TransactionProcessor> processorList = TransactionManager.getProcessorList(tx.getClass());
    List<TransactionProcessor> rollbackedList = new ArrayList<>();
    for (TransactionProcessor processor : processorList) {
        Result result = processor.onRollback(tx, secondaryData);
        if (result.isSuccess()) {
            rollbackedList.add(processor);
        } else {
            for (int i = rollbackedList.size() - 1; i >= 0; i--) {
                TransactionProcessor processor1 = rollbackedList.get(i);
                processor1.onCommit(tx, secondaryData);
            }
            return result;
        }
    }
    try {
        ledgerService.rollbackTx(tx);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
    return Result.getSuccess();
}
Also used : TransactionProcessor(io.nuls.kernel.processor.TransactionProcessor) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) ValidateResult(io.nuls.kernel.validate.ValidateResult) Result(io.nuls.kernel.model.Result)

Example 58 with Result

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

the class BlockHeaderStorageServiceImpl method afterPropertiesSet.

/**
 * 创建存储表,创建失败时如果是因为已存在则正常,否则抛出异常
 * Create a storage table, or throw an exception if it is normal if it is already existing.
 */
@Override
public void afterPropertiesSet() {
    Result result = this.dbService.createArea(ProtocolStorageConstant.DB_NAME_BLOCK_HEADER_INDEX);
    if (result.isFailed() && !DBErrorCode.DB_AREA_EXIST.equals(result.getErrorCode())) {
        throw new NulsRuntimeException(result.getErrorCode());
    }
    result = this.dbService.createArea(ProtocolStorageConstant.DB_NAME_BLOCK_HEADER);
    if (result.isFailed() && !DBErrorCode.DB_AREA_EXIST.equals(result.getErrorCode())) {
        throw new NulsRuntimeException(result.getErrorCode());
    }
    try {
        bestBlockKey = NulsDigestData.calcDigestData(ProtocolStorageConstant.BEST_BLOCK_HASH_INDEX.getBytes()).serialize();
    } catch (IOException e) {
        throw new NulsRuntimeException(e.getCause());
    }
}
Also used : NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) Result(io.nuls.kernel.model.Result)

Example 59 with Result

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

the class BlockHeaderStorageServiceImplTest method saveBlockHeader.

public void saveBlockHeader() {
    Result result = service.saveBlockHeader(entity);
    assertTrue(result.isSuccess());
}
Also used : Result(io.nuls.kernel.model.Result)

Example 60 with Result

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

the class AccountBaseServiceTest method getPrivateKeyTest.

@Test
public void getPrivateKeyTest() {
    List<Account> accounts = this.accountService.createAccount(1, "nuls123456").getData();
    Account account = accounts.get(0);
    Result result = accountBaseService.getPrivateKey(account.getAddress().toString(), "nuls123456");
    assertTrue(result.isSuccess());
    try {
        account.unlock("nuls123456");
    } catch (NulsException e) {
        e.printStackTrace();
    }
    assertArrayEquals(Hex.decode((String) result.getData()), account.getPriKey());
    List<Account> accounts2 = this.accountService.createAccount(1, "").getData();
    Account account2 = accounts2.get(0);
    Result result2 = accountBaseService.getPrivateKey(account2.getAddress().toString(), "");
    assertTrue(result2.isSuccess());
    assertArrayEquals(Hex.decode((String) result2.getData()), account2.getPriKey());
}
Also used : Account(io.nuls.account.model.Account) NulsException(io.nuls.kernel.exception.NulsException) Result(io.nuls.kernel.model.Result) Test(org.junit.Test)

Aggregations

Result (io.nuls.kernel.model.Result)70 NulsException (io.nuls.kernel.exception.NulsException)16 IOException (java.io.IOException)15 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)12 ValidateResult (io.nuls.kernel.validate.ValidateResult)11 AccountPo (io.nuls.account.storage.po.AccountPo)7 RpcClientResult (io.nuls.kernel.model.RpcClientResult)7 ArrayList (java.util.ArrayList)7 Account (io.nuls.account.model.Account)6 NulsDigestData (io.nuls.kernel.model.NulsDigestData)5 Test (org.junit.Test)5 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)4 BatchOperation (io.nuls.db.service.BatchOperation)4 Address (io.nuls.kernel.model.Address)4 Block (io.nuls.kernel.model.Block)4 BlockHeader (io.nuls.kernel.model.BlockHeader)4 Transaction (io.nuls.kernel.model.Transaction)4 Node (io.nuls.network.model.Node)4 NotFound (io.nuls.protocol.model.NotFound)4 ApiOperation (io.swagger.annotations.ApiOperation)4