Search in sources :

Example 1 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class ParticipantDataset method addConsensusParticipant.

/**
 * 加入新的共识参与方; <br>
 * 如果指定的共识参与方已经存在,则引发 {@link LedgerException} 异常;
 *
 * @param participant
 */
public void addConsensusParticipant(ParticipantNode participant) {
    Bytes key = encodeKey(participant.getAddress());
    byte[] participantBytes = BinaryProtocol.encode(participant, ParticipantNode.class);
    long nv = dataset.setValue(key, participantBytes, -1);
    if (nv < 0) {
        throw new LedgerException("Participant already exist! --[id=" + key + "]");
    }
    if (ledgerDataStructure.equals(LedgerDataStructure.KV)) {
        // 为参与方维护添加的顺序
        Bytes index = PARTISET_SEQUENCE_KEY_PREFIX.concat(Bytes.fromString(String.valueOf(dataset.getDataCount() + parti_index_in_block)));
        nv = dataset.setValue(index, key.toBytes(), -1);
        if (nv < 0) {
            throw new LedgerException("Participant seq already exist! --[id=" + key + "]");
        }
        parti_index_in_block++;
    }
}
Also used : Bytes(utils.Bytes) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 2 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class MerkleSequenceTree method saveNode.

private void saveNode(AbstractMerkleNode merkleNode) {
    Bytes key = encodeNodeKey(merkleNode.getNodeHash());
    boolean nx = kvStorage.set(key, merkleNode.toBytes(), ExPolicy.NOT_EXISTING);
    if (!nx) {
        throw new LedgerException("Merkle node already exist!");
    }
}
Also used : Bytes(utils.Bytes) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 3 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class MerkleSequenceDataset method setValue.

@Override
public long setValue(Bytes key, byte[] value) {
    if (readonly) {
        throw new IllegalArgumentException("This merkle dataset is readonly!");
    }
    if (value.length > MAX_SIZE_OF_VALUE) {
        throw new IllegalArgumentException("The size of value is great than the max size[" + MAX_SIZE_OF_VALUE + "]!");
    }
    Bytes dataKey = encodeDataKey(key);
    // creating ;
    long sn = snGenerator.generate(key);
    long newVersion = valueStorage.set(dataKey, value, -1);
    if (newVersion < 0) {
        return -1;
    }
    byte[] snBytes = BytesUtils.toBytes(sn);
    Bytes snKey = encodeSNKey(key);
    boolean nx = snStorage.set(snKey, snBytes, ExPolicy.NOT_EXISTING);
    if (!nx) {
        throw new LedgerException("SN already exist! --[KEY=" + key + "]");
    }
    // update merkle tree;
    merkleTree.setData(sn, key, newVersion, value);
    return newVersion;
}
Also used : Bytes(utils.Bytes) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 4 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class MerkleAccountSetEditor method register.

/**
 * 注册一个新账户; <br>
 *
 * 如果账户已经存在,则会引发 {@link LedgerException} 异常; <br>
 *
 * 如果指定的地址和公钥不匹配,则会引发 {@link LedgerException} 异常;
 *
 * @param address 区块链地址;
 * @param pubKey  公钥;
 * @return 注册成功的账户对象;
 */
public CompositeAccount register(BlockchainIdentity accountId) {
    if (isReadonly()) {
        throw new IllegalArgumentException("This AccountSet is readonly!");
    }
    Bytes address = accountId.getAddress();
    PubKey pubKey = accountId.getPubKey();
    verifyAddressEncoding(address, pubKey);
    InnerMerkleAccount cachedAcc = latestAccountsCache.get(address);
    if (cachedAcc != null) {
        if (cachedAcc.getVersion() < 0) {
            // 同一个新账户已经注册,但尚未提交,所以重复注册不会引起任何变化;
            return cachedAcc;
        }
        // 相同的账户已经存在;
        throw new LedgerException("The registering account already exist!", TransactionState.ACCOUNT_REGISTER_CONFLICT);
    }
    long version = merkleDataset.getVersion(address);
    if (version >= 0) {
        throw new LedgerException("The registering account already exist!", TransactionState.ACCOUNT_REGISTER_CONFLICT);
    }
    if (!accessPolicy.checkRegistering(address, pubKey)) {
        throw new LedgerException("Account Registering was rejected for the access policy!");
    }
    Bytes prefix = keyPrefix.concat(address);
    InnerMerkleAccount acc = createInstance(accountId, cryptoSetting, prefix);
    latestAccountsCache.put(address, acc);
    updated = true;
    return acc;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) Bytes(utils.Bytes) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 5 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class LedgerManager method checkCryptoProviderConsistency.

/**
 * 检查密码服务提供者的信息是否匹配;
 *
 * @param registeredProvider
 * @param settingProvider
 */
private void checkCryptoProviderConsistency(CryptoProvider registeredProvider, CryptoProvider settingProvider) {
    if (registeredProvider == null) {
        throw new LedgerException("Crypto service provider[" + settingProvider.getName() + "] has not registered in the runtime environment of current peer!");
    }
    CryptoAlgorithm[] runtimeAlgothms = registeredProvider.getAlgorithms();
    CryptoAlgorithm[] settingAlgothms = settingProvider.getAlgorithms();
    if (runtimeAlgothms.length != settingAlgothms.length) {
        throw new LedgerException("Crypto service provider[" + settingProvider.getName() + "] has not registered in runtime of current peer!");
    }
    HashMap<Short, CryptoAlgorithm> runtimeAlgothmMap = new HashMap<Short, CryptoAlgorithm>();
    for (CryptoAlgorithm alg : runtimeAlgothms) {
        runtimeAlgothmMap.put(alg.code(), alg);
    }
    for (CryptoAlgorithm alg : settingAlgothms) {
        CryptoAlgorithm regAlg = runtimeAlgothmMap.get(alg.code());
        if (regAlg == null) {
            throw new LedgerException(String.format("Crypto algorithm[%s] is not defined by provider[%s] in runtime of current peer!", alg.toString(), registeredProvider.getName()));
        }
        if (!regAlg.name().equals(alg.name())) {
            throw new LedgerException(String.format("Crypto algorithm[%s] do not match the same code algorithm[%s] defined by provider[%s] in runtime of current peer!", alg.name(), regAlg.name(), registeredProvider.getName()));
        }
    }
}
Also used : HashMap(java.util.HashMap) LedgerException(com.jd.blockchain.ledger.LedgerException) CryptoAlgorithm(com.jd.blockchain.crypto.CryptoAlgorithm)

Aggregations

LedgerException (com.jd.blockchain.ledger.LedgerException)17 Bytes (utils.Bytes)10 HashMap (java.util.HashMap)3 CryptoAlgorithm (com.jd.blockchain.crypto.CryptoAlgorithm)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)2 PubKey (com.jd.blockchain.crypto.PubKey)2 HashDigest (com.jd.blockchain.crypto.HashDigest)1 ContractExecuteException (com.jd.blockchain.ledger.ContractExecuteException)1 Event (com.jd.blockchain.ledger.Event)1 RolePrivileges (com.jd.blockchain.ledger.RolePrivileges)1 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)1 TypedValue (com.jd.blockchain.ledger.TypedValue)1 LedgerAdminDataSet (com.jd.blockchain.ledger.core.LedgerAdminDataSet)1 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 PublicKey (java.security.PublicKey)1 X509Certificate (java.security.cert.X509Certificate)1