use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class PocConsensusServiceImpl method registerAgent.
private Transaction registerAgent(Agent agent, Account account, String password) throws IOException, NulsException {
TransactionEvent event = new TransactionEvent();
CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_REGISTER_AGENT));
data.setTotalNa(agent.getDeposit());
data.addFrom(account.getAddress().toString());
Coin coin = new Coin();
coin.setUnlockHeight(0);
coin.setUnlockTime(0);
coin.setNa(agent.getDeposit());
data.addTo(account.getAddress().toString(), coin);
RegisterAgentTransaction tx = null;
try {
tx = new RegisterAgentTransaction(data, password);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
Consensus<Agent> con = new ConsensusAgentImpl();
con.setAddress(account.getAddress().toString());
agent.setStartTime(TimeService.currentTimeMillis());
con.setExtend(agent);
tx.setTxData(con);
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
tx.verifyWithException();
event.setEventBody(tx);
List<String> nodeList = eventBroadcaster.broadcastHashAndCache(event, true);
if (null == nodeList || nodeList.isEmpty()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
}
return tx;
}
use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class PocConsensusServiceImpl method startConsensus.
@Override
public Transaction startConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException {
Account account = this.accountService.getAccount(address);
if (null == account) {
throw new NulsRuntimeException(ErrorCode.FAILED, "The account is not exist,address:" + address);
}
if (paramsMap == null || paramsMap.size() < 2) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
if (!account.validatePassword(password)) {
throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
}
JoinConsensusParam params = new JoinConsensusParam(paramsMap);
if (StringUtils.isNotBlank(params.getIntroduction())) {
Agent agent = new Agent();
agent.setPackingAddress(params.getPackingAddress());
agent.setDeposit(Na.valueOf(params.getDeposit()));
agent.setIntroduction(params.getIntroduction());
agent.setSeed(params.isSeed());
agent.setCommissionRate(params.getCommissionRate());
agent.setAgentName(params.getAgentName());
try {
return this.registerAgent(agent, account, password);
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
}
try {
return this.joinTheConsensus(account, password, params.getDeposit(), params.getAgentHash());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
}
use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class PocConsensusServiceImpl method stopConsensus.
@Override
public Transaction stopConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException, IOException {
AbstractCoinTransaction joinTx = null;
if (null != paramsMap && StringUtils.isNotBlank((String) paramsMap.get("txHash"))) {
PocJoinConsensusTransaction tx = (PocJoinConsensusTransaction) ledgerService.getTx(NulsDigestData.fromDigestHex((String) paramsMap.get("txHash")));
joinTx = tx;
} else {
try {
List<Transaction> txlist = this.ledgerService.getTxList(address, TransactionConstant.TX_TYPE_REGISTER_AGENT);
if (null != txlist || !txlist.isEmpty()) {
joinTx = (AbstractCoinTransaction) txlist.get(0);
}
} catch (Exception e) {
Log.error(e);
}
}
if (null == joinTx) {
throw new NulsRuntimeException(ErrorCode.FAILED, "The related transaction is not exist!");
}
Account account = this.accountService.getAccount(address);
if (null == account) {
throw new NulsRuntimeException(ErrorCode.ACCOUNT_NOT_EXIST, "address:" + address.toString());
}
if (!account.validatePassword(password)) {
throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
}
TransactionEvent event = new TransactionEvent();
CoinTransferData coinTransferData = new CoinTransferData(OperationType.UNLOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_EXIT_CONSENSUS));
coinTransferData.setTotalNa(Na.ZERO);
PocExitConsensusTransaction tx = new PocExitConsensusTransaction(coinTransferData, password);
tx.setTxData(joinTx.getHash());
try {
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
} catch (IOException e) {
Log.error(e);
throw new NulsRuntimeException(ErrorCode.HASH_ERROR, e);
}
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
event.setEventBody(tx);
eventBroadcaster.broadcastHashAndCache(event, true);
return tx;
}
use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class JoinConsensusTxService method onCommit.
@Override
public void onCommit(PocJoinConsensusTransaction tx) throws NulsException {
manager.changeDepositStatus(tx.getTxData().getHexHash(), ConsensusStatusEnum.WAITING);
Consensus<Deposit> cd = tx.getTxData();
cd.getExtend().setTxHash(tx.getHash().getDigestHex());
cd.getExtend().setStatus(ConsensusStatusEnum.WAITING.getCode());
DepositPo po = ConsensusTool.depositToPojo(cd, tx.getHash().getDigestHex());
po.setBlockHeight(tx.getBlockHeight());
po.setTime(tx.getTime());
depositDataService.save(po);
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("agentHash", cd.getExtend().getAgentHash());
List<DepositPo> poList = depositDataService.getList(paramsMap);
long sum = 0L;
for (DepositPo depositPo : poList) {
sum += depositPo.getDeposit();
}
if (sum >= PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_LOWER_LIMIT.getValue()) {
manager.changeAgentStatusByHash(tx.getTxData().getExtend().getAgentHash(), ConsensusStatusEnum.IN);
manager.changeDepositStatusByAgentHash(tx.getTxData().getExtend().getAgentHash(), ConsensusStatusEnum.IN);
AgentPo daPo = this.accountDataService.get(cd.getExtend().getAgentHash());
if (null == daPo) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR, "the agent cannot find,agent hash:" + cd.getExtend().getAgentHash());
}
daPo.setStatus(ConsensusStatusEnum.IN.getCode());
this.accountDataService.updateSelective(daPo);
}
EntrustConsensusNotice notice = new EntrustConsensusNotice();
notice.setEventBody(tx);
NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
}
use of io.nuls.core.exception.NulsRuntimeException in project nuls by nuls-io.
the class AESEncrypt method encrypt.
/**
* 加密
*
* @param plainBytes
* @param iv
* @param aesKey
* @return EncryptedData
*/
public static EncryptedData encrypt(byte[] plainBytes, byte[] iv, KeyParameter aesKey) throws NulsRuntimeException {
Utils.checkNotNull(plainBytes);
Utils.checkNotNull(aesKey);
try {
if (iv == null) {
iv = new byte[16];
SECURE_RANDOM.nextBytes(iv);
}
ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv);
// Encrypt using AES.
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
cipher.init(true, keyWithIv);
byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
final int length2 = cipher.doFinal(encryptedBytes, length1);
return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
} catch (Exception e) {
throw new NulsRuntimeException(e);
}
}
Aggregations