use of io.nuls.kernel.model.CoinData in project nuls by nuls-io.
the class BifurcationUtil method validate.
public ValidateResult validate(BlockHeader header) {
ValidateResult result = ValidateResult.getSuccessResult();
if (NulsContext.MAIN_NET_VERSION <= 1) {
return result;
}
if (ConsensusConfig.getSeedNodeStringList().indexOf(AddressTool.getStringAddressByBytes(header.getPackingAddress())) >= 0) {
return result;
}
if (header.getHeight() == 0L) {
return result;
}
if (header.getHeight() > NulsContext.getInstance().getBestHeight()) {
return result;
}
BlockHeader otherBlockHeader = blockService.getBlockHeader(header.getHeight()).getData();
if (null != otherBlockHeader && !otherBlockHeader.getHash().equals(header.getHash()) && Arrays.equals(otherBlockHeader.getPackingAddress(), header.getPackingAddress())) {
Log.info("-+-+-+-+-+-+-+-+- Received block with the same height and different hashes as the latest local block -+-+-+-+-+-+-+-+- ");
Log.info("-+-+-+-+-+-+-+-+- height:" + header.getHeight() + ", hash of received block:" + header.getHash().getDigestHex() + ", hash of local latest block:" + otherBlockHeader.getHash().getDigestHex());
Log.info("-+-+-+-+-+-+-+-+- Packing address of received block:" + AddressTool.getStringAddressByBytes(header.getPackingAddress()) + ", Packing address of local latest block:" + AddressTool.getStringAddressByBytes(otherBlockHeader.getPackingAddress()));
List<Agent> agentList = PocConsensusContext.getChainManager().getMasterChain().getChain().getAgentList();
Agent agent = null;
for (Agent a : agentList) {
if (a.getDelHeight() > 0) {
continue;
}
if (Arrays.equals(a.getPackingAddress(), header.getPackingAddress())) {
agent = a;
break;
}
}
if (null == agent) {
return result;
}
recordEvidence(agent, header, otherBlockHeader);
if (!isRedPunish(agent)) {
return result;
}
RedPunishTransaction redPunishTransaction = new RedPunishTransaction();
RedPunishData redPunishData = new RedPunishData();
redPunishData.setAddress(agent.getAgentAddress());
long txTime = 0;
try {
// 连续3轮 每一轮两个区块头作为证据 一共 3 * 2 个区块头作为证据
byte[][] headers = new byte[NulsContext.REDPUNISH_BIFURCATION * 2][];
List<Evidence> list = bifurcationEvidenceMap.get(AddressTool.getStringAddressByBytes(agent.getPackingAddress()));
for (int i = 0; i < list.size() && i < NulsContext.REDPUNISH_BIFURCATION; i++) {
Evidence evidence = list.get(i);
int s = i * 2;
headers[s] = evidence.getBlockHeader1().serialize();
headers[++s] = evidence.getBlockHeader2().serialize();
txTime = (evidence.getBlockHeader1().getTime() + evidence.getBlockHeader2().getTime()) / 2;
}
redPunishData.setEvidence(ArraysTool.concatenate(headers));
} catch (Exception e) {
Log.error(e);
return result;
}
redPunishData.setReasonCode(PunishReasonEnum.BIFURCATION.getCode());
redPunishTransaction.setTxData(redPunishData);
redPunishTransaction.setTime(txTime);
CoinData coinData = null;
try {
coinData = ConsensusTool.getStopAgentCoinData(agent, redPunishTransaction.getTime() + PocConsensusConstant.RED_PUNISH_LOCK_TIME);
} catch (IOException e) {
Log.error(e);
return result;
}
redPunishTransaction.setCoinData(coinData);
try {
redPunishTransaction.setHash(NulsDigestData.calcDigestData(redPunishTransaction.serializeForHash()));
} catch (IOException e) {
Log.error(e);
return result;
}
TxMemoryPool.getInstance().add(redPunishTransaction, false);
return result;
}
return result;
}
use of io.nuls.kernel.model.CoinData in project nuls by nuls-io.
the class ScriptTransactionTestTool method test1.
// @Test
public void test1() throws IOException {
NulsContext.MAIN_NET_VERSION = 2;
TransferTransaction tx = new TransferTransaction();
tx.setRemark("test script".getBytes());
CoinData data = new CoinData();
Coin coin = new Coin();
coin.setOwner(ArraysTool.concatenate(Hex.decode("0020dab71b3cd376e2ccf2f290e384d2917cc0929f8de582f63a01fc15144fe38371"), new byte[] { 0 }));
coin.setNa(Na.parseNuls(9997));
coin.setLockTime(0);
List<Coin> from = new ArrayList<>();
from.add(coin);
data.setFrom(from);
Coin toCoin = new Coin();
toCoin.setLockTime(0);
Script script = ScriptBuilder.createOutputScript(AddressTool.getAddress("NsdvuzHyQJEJkz4LEKweDeCs97845xN9"), 1);
toCoin.setOwner(script.getProgram());
toCoin.setNa(Na.parseNuls(9994));
List<Coin> to = new ArrayList<>();
to.add(toCoin);
data.setTo(to);
tx.setCoinData(data);
// ECKey ecKey = ECKey.fromPrivate(new BigInteger(1,Hex.decode("00b491621168dffd80c4684f7445ef378ba4d381b2fe2a7b1fbf905864ed8fbeb9")));
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("4b19caef601a45531b7068430a5b0e380a004001f14bfec025ddf16d5d87fa8e")));
List<ECKey> signEckeys = new ArrayList<>();
signEckeys.add(ecKey);
List<ECKey> scriptEckeys = new ArrayList<>();
SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
String param = "{\"txHex\": \"" + Hex.encode(tx.serialize()) + "\"}";
String res = post("http://127.0.0.1:7001/api/accountledger/transaction/valiTransaction", param, "utf-8");
System.out.println(res);
res = post("http://127.0.0.1:7001/api/accountledger/transaction/broadcast", param, "utf-8");
System.out.println(res);
}
use of io.nuls.kernel.model.CoinData in project nuls by nuls-io.
the class ScriptTransactionTestTool method test.
// @Test
public void test() throws Exception {
NulsContext.MAIN_NET_VERSION = 2;
TransferTransaction tx = new TransferTransaction();
tx.setRemark("test script".getBytes());
CoinData data = new CoinData();
Coin coin = new Coin();
coin.setOwner(ArraysTool.concatenate(Hex.decode("0020dab71b3cd376e2ccf2f290e384d2917cc0929f8de582f63a01fc15144fe38371"), new byte[] { 0 }));
coin.setNa(Na.parseNuls(9997));
coin.setLockTime(0);
List<Coin> from = new ArrayList<>();
from.add(coin);
data.setFrom(from);
Coin toCoin = new Coin();
toCoin.setLockTime(0);
Script script = ScriptBuilder.createOutputScript(AddressTool.getAddress("NsdvuzHyQJEJkz4LEKweDeCs97845xN9"), 1);
toCoin.setOwner(script.getProgram());
toCoin.setNa(Na.parseNuls(9994));
List<Coin> to = new ArrayList<>();
to.add(toCoin);
data.setTo(to);
tx.setCoinData(data);
// ECKey ecKey = ECKey.fromPrivate(new BigInteger(1,Hex.decode("00b491621168dffd80c4684f7445ef378ba4d381b2fe2a7b1fbf905864ed8fbeb9")));
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("4b19caef601a45531b7068430a5b0e380a004001f14bfec025ddf16d5d87fa8e")));
List<ECKey> signEckeys = new ArrayList<>();
signEckeys.add(ecKey);
List<ECKey> scriptEckeys = new ArrayList<>();
SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
String param = "{\"txHex\": \"" + Hex.encode(tx.serialize()) + "\"}";
String res = post("http://127.0.0.1:7001/api/accountledger/transaction/valiTransaction", param, "utf-8");
System.out.println(res);
res = post("http://127.0.0.1:7001/api/accountledger/transaction/broadcast", param, "utf-8");
System.out.println(res);
}
use of io.nuls.kernel.model.CoinData in project nuls by nuls-io.
the class CreateAgentTxValidator method validate.
@Override
public ValidateResult validate(CreateAgentTransaction tx) {
Agent agent = tx.getTxData();
if (null == agent) {
return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
}
if (!AddressTool.validNormalAddress(agent.getPackingAddress())) {
return ValidateResult.getFailedResult(getClass().getName(), AccountErrorCode.ADDRESS_ERROR);
}
if (Arrays.equals(agent.getAgentAddress(), agent.getPackingAddress())) {
return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENTADDR_AND_PACKING_SAME);
}
if (Arrays.equals(agent.getRewardAddress(), agent.getPackingAddress())) {
return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.REWARDADDR_PACKING_SAME);
}
if (tx.getTime() <= 0) {
return ValidateResult.getFailedResult(getClass().getName(), KernelErrorCode.DATA_ERROR);
}
double commissionRate = agent.getCommissionRate();
if (commissionRate < PocConsensusProtocolConstant.MIN_COMMISSION_RATE || commissionRate > PocConsensusProtocolConstant.MAX_COMMISSION_RATE) {
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), PocConsensusErrorCode.COMMISSION_RATE_OUT_OF_RANGE);
}
if (PocConsensusProtocolConstant.AGENT_DEPOSIT_LOWER_LIMIT.isGreaterThan(agent.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (PocConsensusProtocolConstant.AGENT_DEPOSIT_UPPER_LIMIT.isLessThan(agent.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
}
if (!isDepositOk(agent.getDeposit(), tx.getCoinData())) {
return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
}
TransactionSignature sig = new TransactionSignature();
try {
sig.parse(tx.getTransactionSignature(), 0);
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
}
try {
if (!SignatureUtil.containsAddress(tx, agent.getAgentAddress())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
} catch (NulsException e) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
CoinData coinData = tx.getCoinData();
Set<String> addressSet = new HashSet<>();
int lockCount = 0;
for (Coin coin : coinData.getTo()) {
if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
lockCount++;
}
// addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
}
if (lockCount > 1) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
if (addressSet.size() > 1) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.kernel.model.CoinData in project nuls by nuls-io.
the class DepositTxValidator method validate.
@Override
public ValidateResult validate(DepositTransaction tx) throws NulsException {
if (null == tx || null == tx.getTxData() || null == tx.getTxData().getAgentHash() || null == tx.getTxData().getDeposit() || null == tx.getTxData().getAddress()) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
Deposit deposit = tx.getTxData();
AgentPo agentPo = agentStorageService.get(deposit.getAgentHash());
if (null == agentPo || agentPo.getDelHeight() > 0) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
}
List<DepositPo> poList = this.getDepositListByAgent(deposit.getAgentHash());
if (null != poList && poList.size() >= PocConsensusProtocolConstant.MAX_ACCEPT_NUM_OF_DEPOSIT) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_OVER_COUNT);
}
Na limit = PocConsensusProtocolConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
Na max = PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
Na total = Na.ZERO;
for (DepositPo cd : poList) {
total = total.add(cd.getDeposit());
}
if (limit.isGreaterThan(deposit.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (max.isLessThan(total.add(deposit.getDeposit()))) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
}
if (!isDepositOk(deposit.getDeposit(), tx.getCoinData())) {
return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
}
TransactionSignature sig = new TransactionSignature();
try {
sig.parse(tx.getTransactionSignature(), 0);
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
}
if (!SignatureUtil.containsAddress(tx, deposit.getAddress())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
CoinData coinData = tx.getCoinData();
Set<String> addressSet = new HashSet<>();
int lockCount = 0;
for (Coin coin : coinData.getTo()) {
if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
lockCount++;
}
// addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
}
if (lockCount > 1) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
if (addressSet.size() > 1) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
return ValidateResult.getSuccessResult();
}
Aggregations