use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.
the class AgentDepositValidator method validate.
@Override
public ValidateResult validate(RegisterAgentTransaction tx) {
ValidateResult result = ValidateResult.getSuccessResult();
Agent agent = tx.getTxData().getExtend();
if (null == agent) {
return ValidateResult.getFailedResult(ErrorCode.NULL_PARAMETER);
}
if (PocConsensusConstant.AGENT_DEPOSIT_LOWER_LIMIT.isGreaterThan(agent.getDeposit())) {
return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (!agent.getDeposit().equals(tx.getCoinData().getTotalNa())) {
return ValidateResult.getFailedResult(SeverityLevelEnum.FLAGRANT_FOUL, ErrorCode.DEPOSIT_ERROR);
}
return result;
}
use of io.nuls.core.validate.ValidateResult 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.core.validate.ValidateResult in project nuls by nuls-io.
the class BlockMaintenanceThread method checkGenesisBlock.
public void checkGenesisBlock() throws Exception {
Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
ValidateResult result = genesisBlock.verify();
if (result.isFailed()) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR, result.getMessage());
}
Block localGenesisBlock = this.blockService.getGengsisBlock();
if (null == localGenesisBlock) {
for (Transaction tx : genesisBlock.getTxs()) {
ledgerService.approvalTx(tx);
}
this.blockService.saveBlock(genesisBlock);
return;
}
localGenesisBlock.verify();
String logicHash = genesisBlock.getHeader().getHash().getDigestHex();
String localHash = localGenesisBlock.getHeader().getHash().getDigestHex();
if (!logicHash.equals(localHash)) {
throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
}
}
use of io.nuls.core.validate.ValidateResult 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.validate.ValidateResult in project nuls by nuls-io.
the class ConsensusMeetingRunner method packing.
private void packing(PocMeetingMember self) throws NulsException, IOException {
Block bestBlock = context.getBestBlock();
List<Transaction> txList = txCacheManager.getTxList();
txList.sort(TxTimeComparator.getInstance());
BlockData bd = new BlockData();
bd.setHeight(bestBlock.getHeader().getHeight() + 1);
bd.setPreHash(bestBlock.getHeader().getHash());
BlockRoundData roundData = new BlockRoundData();
roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
roundData.setPackingIndexOfRound(self.getIndexOfRound());
roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
bd.setRoundData(roundData);
List<Integer> outTxList = new ArrayList<>();
List<NulsDigestData> outHashList = new ArrayList<>();
List<NulsDigestData> hashList = new ArrayList<>();
long totalSize = 0L;
for (int i = 0; i < txList.size(); i++) {
if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
break;
}
Transaction tx = txList.get(i);
totalSize += tx.size();
if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
break;
}
outHashList.add(tx.getHash());
ValidateResult result = tx.verify();
if (result.isFailed()) {
Log.error(result.getMessage());
outTxList.add(i);
continue;
}
try {
ledgerService.approvalTx(tx);
} catch (Exception e) {
Log.error(e);
outTxList.add(i);
continue;
}
confirmingTxCacheManager.putTx(tx);
}
txCacheManager.removeTx(hashList);
for (int i = outTxList.size() - 1; i >= 0; i--) {
txList.remove(i);
}
txCacheManager.removeTx(outHashList);
if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
addOrphanTx(txList, totalSize, self);
}
addConsensusTx(bestBlock, txList, self);
bd.setTxList(txList);
Log.info("txCount:" + txList.size());
Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
ValidateResult result = newBlock.verify();
if (result.isFailed()) {
Log.warn("packing block error:" + result.getMessage());
for (Transaction tx : newBlock.getTxs()) {
if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
continue;
}
ledgerService.rollbackTx(tx);
}
return;
}
confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
blockManager.addBlock(newBlock, false, null);
BlockHeaderEvent event = new BlockHeaderEvent();
event.setEventBody(newBlock.getHeader());
eventBroadcaster.broadcastAndCache(event, false);
PackedBlockNotice notice = new PackedBlockNotice();
notice.setEventBody(newBlock.getHeader());
eventBroadcaster.publishToLocal(notice);
}
Aggregations