use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class UtxoLedgerTransactionStorageServiceImpl method getTx.
@Override
public Transaction getTx(NulsDigestData hash) {
if (hash == null) {
return null;
}
byte[] hashBytes = new byte[0];
try {
hashBytes = hash.serialize();
} catch (IOException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
Transaction tx = dbService.getModel(LedgerStorageConstant.DB_NAME_LEDGER_TX, hashBytes, Transaction.class);
if (tx != null) {
tx.setHash(hash);
}
return tx;
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class RedPunishTxProcessor method onCommit.
@Override
public Result onCommit(RedPunishTransaction tx, Object secondaryData) {
RedPunishData punishData = tx.getTxData();
BlockHeader header = (BlockHeader) secondaryData;
BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
PunishLogPo punishLogPo = new PunishLogPo();
punishLogPo.setAddress(punishData.getAddress());
punishLogPo.setHeight(tx.getBlockHeight());
punishLogPo.setRoundIndex(roundData.getRoundIndex());
punishLogPo.setTime(tx.getTime());
punishLogPo.setType(PunishType.RED.getCode());
punishLogPo.setEvidence(punishData.getEvidence());
punishLogPo.setReasonCode(punishData.getReasonCode());
List<AgentPo> agentList = agentStorageService.getList();
AgentPo agent = null;
for (AgentPo agentPo : agentList) {
if (agentPo.getDelHeight() > 0) {
continue;
}
if (Arrays.equals(agentPo.getAgentAddress(), punishLogPo.getAddress())) {
agent = agentPo;
break;
}
}
if (null == agent) {
Log.error("There is no agent can be punished.");
return Result.getSuccess();
}
List<DepositPo> depositPoList = depositStorageService.getList();
List<DepositPo> updatedList = new ArrayList<>();
for (DepositPo po : depositPoList) {
if (po.getDelHeight() >= 0) {
continue;
}
if (!po.getAgentHash().equals(agent.getHash())) {
continue;
}
po.setDelHeight(tx.getBlockHeight());
boolean b = depositStorageService.save(po);
if (!b) {
for (DepositPo po2 : updatedList) {
po2.setDelHeight(-1);
this.depositStorageService.save(po2);
}
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.UPDATE_DEPOSIT_FAILED);
}
updatedList.add(po);
}
boolean success = storageService.save(punishLogPo);
if (!success) {
for (DepositPo po2 : updatedList) {
po2.setDelHeight(-1);
this.depositStorageService.save(po2);
}
throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
}
AgentPo agentPo = agent;
agentPo.setDelHeight(tx.getBlockHeight());
success = agentStorageService.save(agentPo);
if (!success) {
for (DepositPo po2 : updatedList) {
po2.setDelHeight(-1);
this.depositStorageService.save(po2);
}
this.storageService.delete(punishLogPo.getKey());
return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
}
return Result.getSuccess();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class BaseChainTest method initChain.
protected void initChain() {
chain = new Chain();
// new a block header
BlockHeader blockHeader = new BlockHeader();
blockHeader.setHeight(0);
blockHeader.setPreHash(NulsDigestData.calcDigestData("00000000000".getBytes()));
blockHeader.setTime(1L);
blockHeader.setTxCount(0);
// add a round data
BlockRoundData roundData = new BlockRoundData();
roundData.setConsensusMemberCount(1);
roundData.setPackingIndexOfRound(1);
roundData.setRoundIndex(1);
roundData.setRoundStartTime(1L);
try {
blockHeader.setExtend(roundData.serialize());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
// new a block of height 0
Block block = new Block();
block.setHeader(blockHeader);
// chain.setEndBlockHeader(blockHeader);
// // add the block into chain
// chain.getBlockList().add(block);
// chain.setStartBlockHeader(blockHeader);
// chain.setEndBlockHeader(blockHeader);
chain.addBlock(block);
// init some agent
List<Agent> agentList = new ArrayList<>();
Transaction<Agent> agentTx = new CreateAgentTransaction();
Agent agent = new Agent();
agent.setPackingAddress(AddressTool.getAddress(ecKey.getPubKey()));
agent.setAgentAddress(AddressTool.getAddress(new ECKey().getPubKey()));
agent.setRewardAddress(AddressTool.getAddress(ecKey.getPubKey()));
agent.setTime(System.currentTimeMillis());
agent.setDeposit(Na.NA.multiply(20000));
agent.setCommissionRate(0.3d);
agent.setBlockHeight(blockHeader.getHeight());
agentTx.setTxData(agent);
agentTx.setTime(agent.getTime());
agentTx.setBlockHeight(blockHeader.getHeight());
NulsSignData signData = signDigest(agentTx.getHash().getDigestBytes(), ecKey);
agentTx.setTransactionSignature(signData.getSignBytes());
agentTx.getTxData().setTxHash(agentTx.getHash());
// add the agent tx into agent list
agentList.add(agentTx.getTxData());
// set the agent list into chain
chain.setAgentList(agentList);
// new a deposit
Deposit deposit = new Deposit();
deposit.setAddress(AddressTool.getAddress(ecKey.getPubKey()));
deposit.setAgentHash(agentTx.getHash());
deposit.setTime(System.currentTimeMillis());
deposit.setDeposit(Na.NA.multiply(200000));
deposit.setBlockHeight(blockHeader.getHeight());
DepositTransaction depositTx = new DepositTransaction();
depositTx.setTime(deposit.getTime());
depositTx.setTxData(deposit);
depositTx.setBlockHeight(blockHeader.getHeight());
List<Deposit> depositList = new ArrayList<>();
depositList.add(depositTx.getTxData());
chain.setDepositList(depositList);
chain.setYellowPunishList(new ArrayList<>());
chain.setRedPunishList(new ArrayList<>());
chainContainer = new ChainContainer(chain);
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class BaseChainTest method newBlock.
protected Block newBlock(Block preBlock) {
assertNotNull(preBlock);
assertNotNull(preBlock.getHeader());
BlockHeader blockHeader = new BlockHeader();
blockHeader.setHeight(preBlock.getHeader().getHeight() + 1);
blockHeader.setPreHash(preBlock.getHeader().getHash());
blockHeader.setTxCount(1);
MeetingRound round = chainContainer.initRound();
BlockExtendsData nextRoundData = new BlockExtendsData();
nextRoundData.setRoundIndex(round.getIndex() + 1);
nextRoundData.setRoundStartTime(round.getEndTime());
MeetingRound currentRound = chainContainer.getRoundManager().getNextRound(nextRoundData, false);
MeetingMember member = currentRound.getMember(AddressTool.getAddress(ecKey.getPubKey()));
blockHeader.setTime(member.getPackEndTime());
// add a round data
BlockRoundData roundData = new BlockRoundData(preBlock.getHeader().getExtend());
roundData.setConsensusMemberCount(currentRound.getMemberCount());
roundData.setPackingIndexOfRound(member.getPackingIndexOfRound());
roundData.setRoundIndex(currentRound.getIndex());
roundData.setRoundStartTime(currentRound.getStartTime());
try {
blockHeader.setExtend(roundData.serialize());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
// new a block of height 0
Block block = new Block();
block.setHeader(blockHeader);
List<Transaction> txs = new ArrayList<>();
block.setTxs(txs);
txs.add(new TestTransaction());
List<NulsDigestData> txHashList = block.getTxHashList();
blockHeader.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
NulsSignData signData = signDigest(blockHeader.getHash().getDigestBytes(), ecKey);
BlockSignature sig = new BlockSignature();
sig.setSignData(signData);
sig.setPublicKey(ecKey.getPubKey());
blockHeader.setBlockSignature(sig);
return block;
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class ConsensusTool method createBlock.
public static Block createBlock(BlockData blockData, Account account) throws NulsException {
if (null == account) {
throw new NulsRuntimeException(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
// 账户不能加密,否则抛错
if (account.isEncrypted()) {
throw new NulsRuntimeException(AccountErrorCode.ACCOUNT_IS_ALREADY_ENCRYPTED);
}
Block block = new Block();
block.setTxs(blockData.getTxList());
BlockHeader header = new BlockHeader();
block.setHeader(header);
try {
// block.getHeader().setExtend(ArraysTool.concatenate(blockData.getExtendsData().serialize(),new byte[]{0,1,0,1,1}));
block.getHeader().setExtend(blockData.getExtendsData().serialize());
} catch (IOException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
header.setHeight(blockData.getHeight());
header.setTime(blockData.getTime());
header.setPreHash(blockData.getPreHash());
header.setTxCount(blockData.getTxList().size());
List<NulsDigestData> txHashList = new ArrayList<>();
for (int i = 0; i < blockData.getTxList().size(); i++) {
Transaction tx = blockData.getTxList().get(i);
tx.setBlockHeight(header.getHeight());
txHashList.add(tx.getHash());
}
header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
header.setHash(NulsDigestData.calcDigestData(block.getHeader()));
BlockSignature scriptSig = new BlockSignature();
NulsSignData signData = accountService.signDigest(header.getHash().getDigestBytes(), account.getEcKey());
scriptSig.setSignData(signData);
scriptSig.setPublicKey(account.getPubKey());
header.setBlockSignature(scriptSig);
return block;
}
Aggregations