use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class HeaderPackerValidator method validate.
@Override
public ValidateResult validate(BlockHeader header) {
BlockHeader preHeader = null;
try {
preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getPreHash());
} catch (NulsException e) {
// todo
}
PocMeetingRound currentRound = consensusManager.getCurrentRound();
if (header.getHeight() == 0) {
return ValidateResult.getSuccessResult();
}
if (preHeader == null) {
return ValidateResult.getSuccessResult();
}
BlockRoundData roundData = null;
try {
roundData = new BlockRoundData(preHeader.getExtend());
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(e.getMessage());
}
if (null == currentRound) {
return ValidateResult.getSuccessResult();
}
if (currentRound.getIndex() == roundData.getRoundIndex() && currentRound.getMemberCount() > (roundData.getPackingIndexOfRound() + 1)) {
if (currentRound.indexOf(header.getPackingAddress()) <= roundData.getPackingIndexOfRound()) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
}
byte[] packingHash160 = AccountTool.getHash160ByAddress(header.getPackingAddress());
byte[] hash160InScriptSig = header.getScriptSig().getSignerHash160();
if (!Arrays.equals(packingHash160, hash160InScriptSig)) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
BlockRoundData nowRoundData = null;
try {
nowRoundData = new BlockRoundData(header.getExtend());
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
if (!isAdjacent(roundData, nowRoundData)) {
return ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class TxSignValidator method validate.
@Override
public ValidateResult validate(Transaction tx) {
byte[] scriptSig = tx.getScriptSig();
tx.setScriptSig(null);
NulsDigestData nulsDigestData;
try {
nulsDigestData = NulsDigestData.calcDigestData(tx.serialize());
} catch (Exception e) {
return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
} finally {
tx.setScriptSig(scriptSig);
}
if (!Arrays.equals(nulsDigestData.getDigestBytes(), tx.getHash().getDigestBytes())) {
return ValidateResult.getFailedResult(ErrorCode.DATA_ERROR);
}
P2PKHScriptSig p2PKHScriptSig = null;
try {
p2PKHScriptSig = new NulsByteBuffer(scriptSig).readNulsData(new P2PKHScriptSig());
} catch (NulsException e) {
return ValidateResult.getFailedResult(ErrorCode.SIGNATURE_ERROR);
}
return p2PKHScriptSig.verifySign(tx.getHash());
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class PocConsensusResource method exitConsensus.
@POST
@Path("/withdraw")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult exitConsensus(WithdrawForm form) {
AssertUtil.canNotEmpty(form);
AssertUtil.canNotEmpty(form.getTxHash());
AssertUtil.canNotEmpty(form.getPassword());
AssertUtil.canNotEmpty(form.getAddress());
Map<String, Object> params = new HashMap<>();
params.put("txHash", form.getTxHash());
Transaction tx = null;
try {
tx = consensusService.stopConsensus(form.getAddress(), form.getPassword(), params);
} catch (NulsException e) {
Log.error(e);
return RpcResult.getFailed(e.getMessage());
} catch (IOException e) {
Log.error(e);
return RpcResult.getFailed(e.getMessage());
}
return RpcResult.getSuccess().setData(tx.getHash().getDigestHex());
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class ConsensusMeetingRunner method coinBaseTx.
private void coinBaseTx(List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_COIN_BASE));
data.setFee(Na.ZERO);
List<ConsensusReward> rewardList = calcReward(txList, self);
Na total = Na.ZERO;
for (ConsensusReward reward : rewardList) {
Coin coin = new Coin();
coin.setNa(reward.getReward());
data.addTo(reward.getAddress(), coin);
total = total.add(reward.getReward());
}
data.setTotalNa(total);
CoinBaseTransaction tx;
try {
tx = new CoinBaseTransaction(data, null);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
tx.setFee(Na.ZERO);
tx.setHash(NulsDigestData.calcDigestData(tx));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
ValidateResult validateResult = tx.verify();
confirmingTxCacheManager.putTx(tx);
if (null == validateResult || validateResult.isFailed()) {
throw new NulsRuntimeException(ErrorCode.CONSENSUS_EXCEPTION);
}
try {
ledgerService.approvalTx(tx);
} catch (NulsException e) {
throw new NulsRuntimeException(e);
}
txList.add(0, tx);
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class ConsensusMeetingRunner method yellowPunishTx.
private void yellowPunishTx(Block bestBlock, List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
BlockRoundData lastBlockRoundData = new BlockRoundData();
try {
lastBlockRoundData.parse(bestBlock.getHeader().getExtend());
} catch (NulsException e) {
Log.error(e);
}
// continuous blocks in the same round
boolean ok = (self.getRoundIndex() == lastBlockRoundData.getRoundIndex()) && (self.getIndexOfRound() == (1 + lastBlockRoundData.getPackingIndexOfRound()));
// continuous blocks between two rounds
ok = ok || (self.getRoundIndex() == (lastBlockRoundData.getRoundIndex() + 1) && self.getIndexOfRound() == 1 && lastBlockRoundData.getPackingIndexOfRound() == lastBlockRoundData.getConsensusMemberCount());
// two rounds
ok = ok || (self.getRoundIndex() - 1) > lastBlockRoundData.getRoundIndex();
if (ok) {
return;
}
List<Address> addressList = new ArrayList<>();
PocMeetingRound round = consensusManager.getCurrentRound();
long roundIndex = lastBlockRoundData.getRoundIndex();
int packingIndex = 0;
if (lastBlockRoundData.getPackingIndexOfRound() == lastBlockRoundData.getConsensusMemberCount()) {
packingIndex = 1;
} else {
packingIndex = lastBlockRoundData.getPackingIndexOfRound() + 1;
}
while (true) {
PocMeetingRound tempRound;
if (roundIndex == self.getRoundIndex()) {
tempRound = round;
} else if (roundIndex == (self.getRoundIndex() - 1)) {
tempRound = round.getPreviousRound();
} else {
break;
}
if (tempRound.getIndex() > round.getIndex()) {
break;
}
if (tempRound.getIndex() == round.getIndex() && packingIndex >= self.getIndexOfRound()) {
break;
}
if (packingIndex >= tempRound.getMemberCount()) {
roundIndex++;
packingIndex = 1;
continue;
}
PocMeetingMember member;
try {
member = tempRound.getMember(packingIndex);
if (null == member) {
break;
}
} catch (Exception e) {
break;
}
packingIndex++;
addressList.add(Address.fromHashs(member.getAgentAddress()));
}
if (addressList.isEmpty()) {
return;
}
YellowPunishTransaction punishTx = new YellowPunishTransaction();
YellowPunishData data = new YellowPunishData();
data.setAddressList(addressList);
data.setHeight(bestBlock.getHeader().getHeight() + 1);
punishTx.setTxData(data);
punishTx.setTime(TimeService.currentTimeMillis());
punishTx.setFee(Na.ZERO);
punishTx.setHash(NulsDigestData.calcDigestData(punishTx));
punishTx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(punishTx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
txList.add(punishTx);
}
Aggregations