use of io.nuls.core.exception.NulsException 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);
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class UtxoTxInputsValidator method validate.
@Override
public ValidateResult validate(AbstractCoinTransaction tx) {
UtxoData data = (UtxoData) tx.getCoinData();
for (int i = 0; i < data.getInputs().size(); i++) {
UtxoInput input = data.getInputs().get(i);
UtxoOutput output = input.getFrom();
if (output == null && tx.getStatus() == TxStatusEnum.CACHED) {
return ValidateResult.getFailedResult(ErrorCode.ORPHAN_TX);
}
if (tx.getStatus() == TxStatusEnum.CACHED) {
if (!output.isUsable()) {
return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
}
} else if (tx.getStatus() == TxStatusEnum.AGREED) {
if (!output.isSpend()) {
return ValidateResult.getFailedResult(ErrorCode.UTXO_STATUS_CHANGE);
}
}
byte[] owner = output.getOwner();
P2PKHScriptSig p2PKHScriptSig = null;
try {
p2PKHScriptSig = P2PKHScriptSig.createFromBytes(tx.getScriptSig());
} catch (NulsException e) {
return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
}
byte[] user = p2PKHScriptSig.getSignerHash160();
if (!Arrays.equals(owner, user)) {
return ValidateResult.getFailedResult(ErrorCode.INVALID_OUTPUT);
}
return ValidateResult.getSuccessResult();
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class ConnectionManager method receiveMessage.
public void receiveMessage(ByteBuffer buffer, Node node) {
List<NulsMessage> list;
try {
buffer.flip();
if (!node.isAlive()) {
buffer.clear();
return;
}
list = new ArrayList<>();
while (buffer.hasRemaining()) {
NulsMessage message = new NulsMessage(buffer);
list.add(message);
}
for (NulsMessage message : list) {
if (MessageFilterChain.getInstance().doFilter(message)) {
BaseEvent event = EventManager.getInstance(message.getData());
MsgLog.info("get(" + node.getId() + "):\n" + Hex.encode(message.getHeader().serialize()) + "--" + Hex.encode(message.getData()));
processMessage(event, node);
}
}
} catch (NulsException e) {
// todo
Log.error(e);
} catch (Exception e) {
// todo
Log.error(e);
return;
} finally {
buffer.clear();
}
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class UtxoLedgerServiceImpl method transfer.
private Result transfer(CoinTransferData coinData, String password, String remark) {
TransferTransaction tx = null;
try {
tx = UtxoTransactionTool.getInstance().createTransferTx(coinData, password, remark);
ValidateResult result = tx.verify();
if (result.isFailed()) {
throw new NulsException(result.getErrorCode());
}
byte[] txbytes = tx.serialize();
TransferTransaction new_tx = new NulsByteBuffer(txbytes).readNulsData(new TransferTransaction());
result = new_tx.verify();
if (result.isFailed()) {
throw new NulsException(result.getErrorCode());
}
TransactionEvent event = new TransactionEvent();
event.setEventBody(tx);
eventBroadcaster.broadcastAndCacheAysn(event, true);
} catch (Exception e) {
Log.error(e);
return new Result(false, e.getMessage());
}
return new Result(true, "OK", tx.getHash().getDigestHex());
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class NulsByteBuffer method readUint32LE.
public long readUint32LE() throws NulsException {
try {
long u = Utils.readUint32LE(payload, cursor);
cursor += 4;
return u;
} catch (ArrayIndexOutOfBoundsException e) {
throw new NulsException(ErrorCode.DATA_PARSE_ERROR, e);
}
}
Aggregations