use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.
the class CryptoConfig method setSupportedProviders.
public void setSupportedProviders(CryptoProvider[] supportedProviders) {
HashMap<String, CryptoProvider> providers = new HashMap<String, CryptoProvider>();
HashMap<String, CryptoAlgorithm> nameAlgorithms = new HashMap<String, CryptoAlgorithm>();
HashMap<Short, CryptoAlgorithm> codeAlgorithms = new HashMap<Short, CryptoAlgorithm>();
if (supportedProviders != null) {
// 检查是否存在重复的提供者以及算法;
for (CryptoProvider cryptoProvider : supportedProviders) {
if (providers.containsKey(cryptoProvider.getName())) {
throw new LedgerException("Duplicate crypto providers [" + cryptoProvider.getName() + "]!");
}
CryptoAlgorithm[] algorithms = cryptoProvider.getAlgorithms();
for (CryptoAlgorithm alg : algorithms) {
if (nameAlgorithms.containsKey(alg.name())) {
throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider " + cryptoProvider.getName() + "!");
}
if (codeAlgorithms.containsKey(alg.code())) {
throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider" + cryptoProvider.getName() + "!");
}
nameAlgorithms.put(alg.name(), alg);
codeAlgorithms.put(alg.code(), alg);
}
providers.put(cryptoProvider.getName(), cryptoProvider);
}
}
this.providers = providers;
this.nameAlgorithms = nameAlgorithms;
this.codeAlgorithms = codeAlgorithms;
this.cryptoProviders = supportedProviders;
}
use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.
the class EventGroupPublisher method publish.
/**
* 发布事件
*
* @param event
* @return
*/
@Override
public long publish(Event event) {
Bytes key = encodeKey(event.getName());
long newSequence = events.setValue(key, BinaryProtocol.encode(event, Event.class), event.getSequence() - 1);
if (newSequence < 0) {
throw new LedgerException("Event sequence conflict! --[" + key + "]");
}
// 属于新发布的事件名
if (ledgerDataStructure.equals(LedgerDataStructure.KV)) {
if (newSequence == 0) {
long nv = events.setValue(SYSTEMEVENT_SEQUENCE_KEY_PREFIX.concat(Bytes.fromString(String.valueOf(events.getDataCount() + event_index_in_block))), key.toBytes(), -1);
if (nv < 0) {
throw new LedgerException("Event seq already exist! --[id=" + key + "]");
}
event_index_in_block++;
}
}
return newSequence;
}
use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.
the class UserAccount method setDataPubKey.
public void setDataPubKey(PubKey pubKey, long version) {
TypedValue value = TypedValue.fromPubKey(dataPubKey);
long newVersion = getHeaders().setValue(DATA_PUB_KEY, value, version);
if (newVersion > -1) {
dataPubKey = pubKey;
} else {
throw new LedgerException("Data public key was updated failed!");
}
}
use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.
the class RootCAUpdateOperationHandle method doProcess.
@Override
protected void doProcess(RootCAUpdateOperation op, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
// 权限校验;
SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
securityPolicy.checkEndpointPermission(LedgerPermission.UPDATE_ROOT_CA, MultiIDsPolicy.AT_LEAST_ONE);
LedgerAdminDataSet adminDataset = transactionContext.getDataset().getAdminDataset();
if (adminDataset.getAdminSettings().getMetadata().getIdentityMode() == IdentityMode.CA) {
String[] ledgerCAs = adminDataset.getAdminSettings().getMetadata().getLedgerCertificates();
Map<PublicKey, String> ledgerCAMap = new HashMap<>();
for (int i = 0; i < ledgerCAs.length; i++) {
X509Certificate cert = CertificateUtils.parseCertificate(ledgerCAs[i]);
PublicKey publicKey = cert.getPublicKey();
ledgerCAMap.put(publicKey, ledgerCAs[i]);
}
String[] certificatesAdd = op.getCertificatesAdd();
for (String cert : certificatesAdd) {
X509Certificate certificate = CertificateUtils.parseCertificate(cert);
CertificateUtils.checkCACertificate(certificate);
CertificateUtils.checkValidity(certificate);
if (!ledgerCAMap.containsKey(certificate.getPublicKey())) {
ledgerCAMap.put(certificate.getPublicKey(), cert);
} else {
throw new LedgerException("Certificate [" + CertificateUtils.toPEMString(certificate) + "] already exists in the ledger!");
}
}
String[] certificatesUpdate = op.getCertificatesUpdate();
for (String cert : certificatesUpdate) {
X509Certificate certificate = CertificateUtils.parseCertificate(cert);
CertificateUtils.checkCACertificate(certificate);
CertificateUtils.checkValidity(certificate);
if (ledgerCAMap.containsKey(certificate.getPublicKey())) {
ledgerCAMap.put(certificate.getPublicKey(), cert);
} else {
throw new LedgerException("Certificate [" + CertificateUtils.toPEMString(certificate) + "] not exists in the ledger!");
}
}
String[] certificatesRemove = op.getCertificatesRemove();
for (String cert : certificatesRemove) {
X509Certificate certificate = CertificateUtils.parseCertificate(cert);
CertificateUtils.checkCACertificate(certificate);
if (ledgerCAMap.containsKey(certificate.getPublicKey())) {
ledgerCAMap.remove(certificate.getPublicKey(), cert);
} else {
throw new LedgerException("Certificate [" + CertificateUtils.toPEMString(certificate) + "] not exists in the ledger!");
}
}
if (ledgerCAMap.size() == 0) {
throw new LedgerException("At least one root certificate is required!");
}
((LedgerAdminDataSetEditor) adminDataset).updateLedgerCA(ledgerCAMap.values().toArray(new String[0]));
} else {
throw new LedgerException("Not in CA identity mode!");
}
}
use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.
the class TransactionSetEditor method commit.
@Override
public synchronized void commit() {
if (ledgerDataStructure.equals(LedgerDataStructure.MERKLE_TREE)) {
txSequence.commit();
HashDigest txReqRootHash = txSequence.getRootHash();
long v = txStateSet.setValue(TX_REQUEST_ROOT_HASH, txReqRootHash.toBytes(), txRequestBlockID);
if (v < 0) {
throw new LedgerException("Fail to save the root hash of transaction request!");
}
txStateSet.commit();
txRequestBlockID = v;
} else {
origin_txIndex = txIndex;
saveTotalByHeight();
txStateSet.commit();
}
}
Aggregations