use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class TxCoinValidator method validate.
@Override
public ValidateResult validate(Transaction tx) throws NulsException {
try {
if (tx.getCoinData() == null) {
return ValidateResult.getSuccessResult();
}
List<Coin> toList = tx.getCoinData().getTo();
if (toList == null || toList.size() == 0) {
return ValidateResult.getSuccessResult();
}
Set<String> fromAddressSet = null;
byte[] owner;
String address;
Map<String, Integer> addressSet = MapUtil.createHashMap(toList.size());
for (Coin coin : toList) {
owner = coin.getOwner();
if (owner.length == Address.ADDRESS_LENGTH && owner[2] == NulsContext.P2SH_ADDRESS_TYPE) {
return ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.COIN_OWNER_ERROR);
}
if (tx.isSystemTx() && tx.getType() != ContractConstant.TX_TYPE_CONTRACT_TRANSFER) {
continue;
}
address = AddressTool.getStringAddressByBytes(coin.getAddress());
if (fromAddressSet == null) {
fromAddressSet = SignatureUtil.getAddressFromTX(tx);
}
if (fromAddressSet != null && fromAddressSet.contains(address)) {
continue;
}
Integer count = addressSet.get(address);
if (count == null) {
addressSet.put(address, count = 1);
} else {
addressSet.put(address, ++count);
}
if (count > 2) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.INVALID_AMOUNT);
}
}
} catch (Exception e) {
Log.error(e);
return ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.DATA_ERROR);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class Test method main.
public static void main(String[] args) {
String str = "2102ac3f8f73d6a0f23d9ecd797fb14f96e82a032bb972f1ee39b4bf443d07a5d55e004630440220668f5538cdbbd90e3dfc1ea1197ddb03cd8628dedfc77afad795140f3867560302202453d0991dc254ae2a5f40f915ef22e0c0e9ba315516d22eaa2e002fc9239147";
TransactionSignature signature = new TransactionSignature();
try {
signature.parse(str.getBytes(), 0);
} catch (NulsException e) {
Log.error(e);
}
signature.getP2PHKSignatures().forEach(p -> {
System.out.println(Arrays.toString(p.getPublicKey()));
});
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class BlockHeaderPoTest method serializeAndParse.
/**
* 验证区块头实体的序列化和反序列化的正确性
* Verify the correctness of serialization and deserialization of block header entities.
*/
@Test
public void serializeAndParse() {
BlockHeaderPo po = new BlockHeaderPo();
po.setHeight(1286L);
po.setExtend("extends".getBytes());
po.setMerkleHash(NulsDigestData.calcDigestData("merkleHash".getBytes()));
try {
po.setPackingAddress("address".getBytes());
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
po.setScriptSign(new BlockSignature());
po.setTime(12345678901L);
po.setTxCount(3);
List<NulsDigestData> txHashList = new ArrayList<>();
txHashList.add(NulsDigestData.calcDigestData("first-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("second-tx-hash".getBytes()));
txHashList.add(NulsDigestData.calcDigestData("third-tx-hash".getBytes()));
po.setTxHashList(txHashList);
byte[] bytes = new byte[0];
try {
bytes = po.serialize();
} catch (IOException e) {
Log.error(e);
}
BlockHeaderPo newPo = new BlockHeaderPo();
try {
newPo.parse(bytes, 0);
} catch (NulsException e) {
Log.error(e);
}
assertNull(newPo.getHash());
assertEquals(po.getHeight(), newPo.getHeight());
assertEquals(po.getPreHash(), newPo.getPreHash());
assertEquals(po.getMerkleHash(), newPo.getMerkleHash());
assertTrue(Arrays.equals(po.getExtend(), newPo.getExtend()));
assertTrue(Arrays.equals(po.getPackingAddress(), newPo.getPackingAddress()));
assertTrue(Arrays.equals(po.getScriptSign().getPublicKey(), newPo.getScriptSign().getPublicKey()));
assertEquals(po.getScriptSign().getSignData(), newPo.getScriptSign().getSignData());
assertEquals(po.getTime(), newPo.getTime());
assertEquals(po.getTxCount(), newPo.getTxCount());
assertEquals(po.getTxHashList().get(0), newPo.getTxHashList().get(0));
assertEquals(po.getTxHashList().get(1), newPo.getTxHashList().get(1));
assertEquals(po.getTxHashList().get(2), newPo.getTxHashList().get(2));
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class TransferTransaction method getInfo.
@Override
public String getInfo(byte[] address) {
Long value = 0L;
byte[] fromHash, owner;
int fromIndex;
NulsDigestData fromHashObj;
Transaction fromTx;
Coin fromUtxo;
for (Coin from : coinData.getFrom()) {
owner = from.getOwner();
// owner拆分出txHash和index
fromHash = LedgerUtil.getTxHashBytes(owner);
fromIndex = LedgerUtil.getIndex(owner);
// 查询from UTXO
fromHashObj = new NulsDigestData();
try {
fromHashObj.parse(fromHash, 0);
} catch (NulsException e) {
return "--";
}
Result<Transaction> result = getAccountLedgerService().getUnconfirmedTransaction(fromHashObj);
if (result.isSuccess() && result.getData() != null) {
fromTx = result.getData();
} else {
fromTx = getLedgerService().getTx(fromHashObj);
}
fromUtxo = fromTx.getCoinData().getTo().get(fromIndex);
// if (Arrays.equals(address, fromUtxo.()))
if (Arrays.equals(address, fromUtxo.getAddress())) {
value = value - fromUtxo.getNa().getValue();
}
}
for (Coin to : coinData.getTo()) {
if (Arrays.equals(address, to.getAddress())) {
value = value + to.getNa().getValue();
}
}
long divide = (long) Math.pow(10, SMALLEST_UNIT_EXPONENT);
BigDecimal decimal = new BigDecimal(value).divide(BigDecimal.valueOf(divide));
if (decimal.doubleValue() > 0) {
return "+" + decimal.toPlainString();
}
return decimal.toPlainString();
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoAccountsServiceImpl method getInputAddress.
private byte[] getInputAddress(Coin from) {
byte[] fromHash;
int fromIndex;
byte[] owner = from.getOwner();
// owner拆分出txHash和index
fromHash = UtxoAccountsUtil.getTxHashBytes(owner);
fromIndex = UtxoAccountsUtil.getIndex(owner);
NulsDigestData fromHashObj = new NulsDigestData();
try {
fromHashObj.parse(fromHash, 0);
Transaction outPutTx = utxoAccountsStorageService.getTx(fromHashObj);
return outPutTx.getCoinData().getTo().get(fromIndex).getOwner();
} catch (NulsException e) {
Log.error(e);
return null;
}
}
Aggregations