use of io.nuls.ledger.event.TransactionEvent in project nuls by nuls-io.
the class UtxoLedgerServiceImpl method lock.
@Override
public Result lock(String address, String password, Na amount, long unlockTime, String remark) {
LockNulsTransaction tx = null;
try {
CoinTransferData coinData = new CoinTransferData(OperationType.LOCK, amount, address, getTxFee(TransactionConstant.TX_TYPE_LOCK));
coinData.addTo(address, new Coin(amount, unlockTime));
tx = UtxoTransactionTool.getInstance().createLockNulsTx(coinData, password, remark);
tx.verify();
TransactionEvent event = new TransactionEvent();
event.setEventBody(tx);
eventBroadcaster.broadcastAndCacheAysn(event, true);
} catch (Exception e) {
Log.error(e);
try {
rollbackTx(tx);
} catch (NulsException e1) {
Log.error(e1);
}
return new Result(false, e.getMessage());
}
return new Result(true, "OK", tx.getHash().getDigestHex());
}
use of io.nuls.ledger.event.TransactionEvent in project nuls by nuls-io.
the class PocConsensusServiceImpl method joinTheConsensus.
private Transaction joinTheConsensus(Account account, String password, long amount, String agentHash) throws IOException, NulsException {
AssertUtil.canNotEmpty(account);
AssertUtil.canNotEmpty(password);
if (amount < PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT.getValue()) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
AssertUtil.canNotEmpty(agentHash);
TransactionEvent event = new TransactionEvent();
Consensus<Deposit> ca = new ConsensusDepositImpl();
ca.setAddress(account.getAddress().toString());
Deposit deposit = new Deposit();
deposit.setAgentHash(agentHash);
deposit.setDeposit(Na.valueOf(amount));
deposit.setStartTime(TimeService.currentTimeMillis());
ca.setExtend(deposit);
CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_JOIN_CONSENSUS));
data.setTotalNa(deposit.getDeposit());
data.addFrom(account.getAddress().toString());
Coin coin = new Coin();
coin.setUnlockHeight(0);
coin.setUnlockTime(0);
coin.setNa(deposit.getDeposit());
data.addTo(account.getAddress().toString(), coin);
PocJoinConsensusTransaction tx = null;
try {
tx = new PocJoinConsensusTransaction(data, password);
} catch (NulsException e) {
throw new NulsRuntimeException(e);
}
tx.setTime(TimeService.currentTimeMillis());
tx.setTxData(ca);
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
tx.verifyWithException();
event.setEventBody(tx);
List<String> nodeList = eventBroadcaster.broadcastAndCache(event, true);
if (null == nodeList || nodeList.isEmpty()) {
throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
}
return tx;
}
use of io.nuls.ledger.event.TransactionEvent in project nuls by nuls-io.
the class TransactionResource method forwardTransaction.
@POST
@Produces(MediaType.APPLICATION_JSON)
public RpcResult forwardTransaction(TxForm form) {
Transaction tx = null;
try {
tx = form.getTx();
} catch (Exception e) {
Log.error(e);
}
if (tx == null) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
ValidateResult result = tx.verify();
if (result.isFailed() && ErrorCode.ORPHAN_TX != result.getErrorCode()) {
return RpcResult.getFailed(ErrorCode.DATA_ERROR);
}
TransactionEvent event = new TransactionEvent();
event.setEventBody(tx);
List<String> list = eventBroadcaster.broadcastAndCache(event, true);
return RpcResult.getSuccess().setData(list);
}
use of io.nuls.ledger.event.TransactionEvent 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.ledger.event.TransactionEvent 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;
}
Aggregations