use of io.nuls.core.chain.entity.Transaction 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.chain.entity.Transaction in project nuls by nuls-io.
the class ExitConsensusTxService method onRollback.
@Override
public void onRollback(PocExitConsensusTransaction tx) throws NulsException {
Transaction joinTx = ledgerService.getTx(tx.getTxData());
if (joinTx.getType() == TransactionConstant.TX_TYPE_REGISTER_AGENT) {
RegisterAgentTransaction raTx = (RegisterAgentTransaction) joinTx;
Consensus<Agent> ca = raTx.getTxData();
ca.getExtend().setStatus(ConsensusStatusEnum.IN.getCode());
manager.cacheAgent(ca);
AgentPo agentPo = new AgentPo();
agentPo.setId(raTx.getTxData().getHexHash());
agentPo.setStatus(ConsensusStatusEnum.IN.getCode());
this.agentDataService.updateSelective(agentPo);
DepositPo dpo = new DepositPo();
dpo.setId(raTx.getTxData().getHexHash());
dpo.setStatus(ConsensusStatusEnum.IN.getCode());
this.depositDataService.updateSelectiveByAgentHash(dpo);
CancelConsensusNotice notice = new CancelConsensusNotice();
notice.setEventBody(tx);
NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
// cache delegates
Map<String, Object> params = new HashMap<>();
params.put("agentHash", raTx.getTxData().getHexHash());
List<DepositPo> polist = this.depositDataService.getList(params);
if (null == polist || polist.isEmpty()) {
return;
}
for (DepositPo po : polist) {
Consensus<Deposit> cd = ConsensusTool.fromPojo(po);
this.manager.cacheDeposit(cd);
}
this.ledgerService.unlockTxRollback(tx.getTxData().getDigestHex());
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("agentHash", ca.getHexHash());
List<DepositPo> poList = depositDataService.getList(paramsMap);
for (DepositPo po : poList) {
this.ledgerService.unlockTxRollback(po.getTxHash());
}
return;
}
PocJoinConsensusTransaction pjcTx = (PocJoinConsensusTransaction) joinTx;
Consensus<Deposit> cd = pjcTx.getTxData();
cd.getExtend().setStatus(ConsensusStatusEnum.IN.getCode());
manager.cacheDeposit(cd);
DepositPo dPo = this.depositDataService.get(cd.getHexHash());
if (dPo == null) {
dPo = ConsensusTool.depositToPojo(cd, tx.getHash().getDigestHex());
this.depositDataService.save(dPo);
}
StopConsensusNotice notice = new StopConsensusNotice();
notice.setEventBody(tx);
NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
this.ledgerService.unlockTxRollback(tx.getTxData().getDigestHex());
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class TransactionManager method getInstance.
public static Transaction getInstance(NulsByteBuffer byteBuffer) throws Exception {
int txType = (int) new NulsByteBuffer(byteBuffer.getPayloadByCursor()).readVarInt();
Class<? extends Transaction> txClass = getTxClass(txType);
if (null == txClass) {
throw new NulsRuntimeException(ErrorCode.FAILED, "transaction type not exist!");
}
Transaction tx = byteBuffer.readNulsData(txClass.getConstructor().newInstance());
return tx;
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class AccountServiceImpl method importSave.
private void importSave(List<Account> accounts) throws Exception {
List<AccountPo> accountPoList = new ArrayList<>();
for (Account account : accounts) {
AccountPo accountPo = new AccountPo();
AccountTool.toPojo(account, accountPo);
List<TransactionLocalPo> transactionPos = new ArrayList<>();
for (Transaction tx : account.getMyTxs()) {
TransactionLocalPo po = UtxoTransferTool.toLocalTransactionPojo(tx);
transactionPos.add(po);
}
accountPo.setMyTxs(transactionPos);
accountPoList.add(accountPo);
}
accountAliasDBService.importAccount(accountPoList);
}
use of io.nuls.core.chain.entity.Transaction in project nuls by nuls-io.
the class PocConsensusResource method createAgent.
@POST
@Path("/agent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Create an agent for consensus!")
public RpcResult createAgent(CreateAgentForm form) throws NulsException {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getAgentAddress());
AssertUtil.canNotEmpty(form.getAgentName());
AssertUtil.canNotEmpty(form.getPackingAddress());
AssertUtil.canNotEmpty(form.getDeposit());
AssertUtil.canNotEmpty(form.getRemark());
AssertUtil.canNotEmpty(form.getPassword());
if (!Address.validAddress(form.getPackingAddress()) || !Address.validAddress(form.getAgentAddress())) {
throw new NulsRuntimeException(ErrorCode.PARAMETER_ERROR);
}
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("deposit", form.getDeposit());
paramsMap.put("packingAddress", form.getPackingAddress());
paramsMap.put("introduction", form.getRemark());
paramsMap.put("commissionRate", form.getCommissionRate());
paramsMap.put("agentName", form.getAgentName());
Transaction tx = consensusService.startConsensus(form.getAgentAddress(), form.getPassword(), paramsMap);
return RpcResult.getSuccess().setData(tx.getHash().getDigestHex());
}
Aggregations