Search in sources :

Example 1 with LedgerInitException

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

the class LedgerInitConfiguration method createLedgerInitSettings.

private static LedgerInitData createLedgerInitSettings(LedgerInitProperties ledgerProps, CryptoSetting cryptoSetting, ConsensusConfig consensusConfig, ParticipantProperties[] participants) {
    // 创建初始化配置;
    LedgerInitData initSetting = new LedgerInitData();
    initSetting.setLedgerSeed(ledgerProps.getLedgerSeed());
    initSetting.setIdentityMode(ledgerProps.getIdentityMode());
    initSetting.setLedgerCertificates(ledgerProps.getLedgerCertificates());
    initSetting.setGenesisUsers(ledgerProps.getGenesisUsers());
    initSetting.setCryptoSetting(cryptoSetting);
    initSetting.setConsensusParticipants(participants);
    initSetting.setCreatedTime(ledgerProps.getCreatedTime());
    initSetting.setLedgerDataStructure(ledgerProps.getLedgerDataStructure());
    initSetting.setContractRuntimeConfig(new JVMContractRuntimeConfig(ledgerProps.getContractTimeout(), ledgerProps.getMaxStackDepth()));
    // 创建共识配置;
    try {
        byte[] consensusSettingsBytes = encodeConsensusSettings(consensusConfig.getProvider(), consensusConfig.protocolSettings);
        initSetting.setConsensusProvider(consensusConfig.getProvider().getName());
        initSetting.setConsensusSettings(consensusSettingsBytes);
    } catch (Exception e) {
        throw new LedgerInitException("Create default consensus config failed! --" + e.getMessage(), e);
    }
    return initSetting;
}
Also used : JVMContractRuntimeConfig(com.jd.blockchain.contract.jvm.JVMContractRuntimeConfig) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException)

Example 2 with LedgerInitException

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

the class LedgerInitConfiguration method createSecurityInitSettings.

private static SecurityInitData createSecurityInitSettings(LedgerInitProperties ledgerInitProps) {
    // 设置角色;
    SecurityInitData securityInitData = new SecurityInitData();
    securityInitData.setRoles(ledgerInitProps.getRoles());
    // 如果没有默认角色,则创建“默认”角色;
    if (securityInitData.getRolesCount() == 0) {
        securityInitData.addRole(LedgerSecurityManager.DEFAULT_ROLE, LedgerPermission.values(), TransactionPermission.values());
    } else if (!securityInitData.containsRole(LedgerSecurityManager.DEFAULT_ROLE)) {
        // 如果定义了角色,则必须显式地定义“默认”角色;
        throw new LedgerInitException("Miss definition of role[DEFAULT]!");
    }
    // 设置授权;
    for (GenesisUser u : ledgerInitProps.getGenesisUsers()) {
        String[] roles = u.getRoles();
        for (String role : roles) {
            if (!securityInitData.containsRole(role)) {
                throw new LedgerInitException(String.format("The role[%s] authenticated to user[%s] is not defined!", role, AddressEncoding.generateAddress(u.getPubKey())));
            }
        }
        // 去掉对默认角色的授权;
        securityInitData.addUserAuthencation(AddressEncoding.generateAddress(u.getPubKey()), roles, u.getRolesPolicy());
    }
    return securityInitData;
}
Also used : SecurityInitData(com.jd.blockchain.ledger.SecurityInitData) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) GenesisUser(com.jd.blockchain.ledger.GenesisUser)

Example 3 with LedgerInitException

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

the class LedgerInitializer method prepareLedger.

/**
 * 初始化账本数据,返回创始区块;
 *
 * @param ledgerEditor
 * @return
 */
private LedgerBlock prepareLedger(LedgerEditor ledgerEditor, DigitalSignature... nodeSignatures) {
    // 初始化时,自动将参与方注册为账本的用户;
    HashDigest txHash = TxBuilder.computeTxContentHash(initSetting.getCryptoSetting().getHashAlgorithm(), this.initTxContent);
    TxRequestBuilder txReqBuilder = new TxRequestBuilder(txHash, this.initTxContent);
    txReqBuilder.addNodeSignature(nodeSignatures);
    TransactionRequest txRequest = txReqBuilder.buildRequest();
    TransactionBatchProcessor txProcessor = null;
    if (initSetting.getLedgerDataStructure().equals(LedgerDataStructure.MERKLE_TREE)) {
        txProcessor = new TransactionBatchProcessor(FULL_PERMISSION_SECURITY_MANAGER, ledgerEditor, EMPTY_LEDGER_MERKLE, DEFAULT_OP_HANDLE_REG);
    } else {
        txProcessor = new TransactionBatchProcessor(FULL_PERMISSION_SECURITY_MANAGER, ledgerEditor, EMPTY_LEDGER_KV, DEFAULT_OP_HANDLE_REG);
    }
    LedgerEditor.TIMESTAMP_HOLDER.set(initSetting.getCreatedTime());
    TransactionResponse response = txProcessor.schedule(txRequest);
    if (!response.isSuccess()) {
        throw new LedgerInitException("Transaction execution failed in genesis block!");
    }
    txResultsHandle = txProcessor.prepare();
    LedgerEditor.TIMESTAMP_HOLDER.remove();
    return txResultsHandle.getBlock();
}
Also used : TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) TxRequestBuilder(com.jd.blockchain.transaction.TxRequestBuilder) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Example 4 with LedgerInitException

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

the class LedgerInitializeWebController method synchronizeDecision.

@RequestMapping(path = "/legerinit/decision", method = RequestMethod.POST, produces = LedgerInitMessageConverter.CONTENT_TYPE_VALUE, consumes = LedgerInitMessageConverter.CONTENT_TYPE_VALUE)
@Override
public LedgerInitDecision synchronizeDecision(@RequestBody LedgerInitDecision initDecision) {
    int remoteId = initDecision.getParticipantId();
    if (remoteId == currentId) {
        prompter.error("Reject decision because of self-synchronization! --[Id=%s]", remoteId);
        throw new LedgerInitException(String.format("Reject decision because of self-synchronization! --[Id=%s]", remoteId));
    }
    if (this.initializer == null) {
        // 当前参与者尚未准备就绪,返回 null;
        prompter.info("Not ready for genesis block! --[RemoteId=%s][CurrentId=%s]", remoteId, currentId);
        return null;
    }
    prompter.info("Received request of synchronizing decision! --[RemoteId=%s][CurrentId=%s]", remoteId, currentId);
    try {
        // 必须等待本地处理完成,此处线程阻塞
        this.decisionsCountDownLatch.await();
        DecisionResultHandle resultHandle = this.decisions[remoteId];
        if (!validateAndRecordDecision(initDecision, resultHandle)) {
            // 签名无效;
            prompter.error("Reject decision because of invalid signature! --[RemoteId=%s][CurrentId=%s]", remoteId, currentId);
            throw new LedgerInitException(String.format("Reject decision because of invalid signature! --[RemoteId=%s][CurrentId=%s]", remoteId, currentId));
        }
        return localDecision;
    } catch (Exception e) {
        prompter.error(e, "Error occurred while receiving the request of synchronizing decision! --[RemoteId=%s][CurrentId=%s] %s", remoteId, currentId, e.getMessage());
        throw new LedgerInitException("Error occurred while receiving the request of synchronizing decision! --" + e.getMessage(), e);
    }
}
Also used : LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) IOException(java.io.IOException) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with LedgerInitException

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

the class LedgerInitializeWebController method requestPermission.

@RequestMapping(path = "/legerinit/permission/{requesterId}", method = RequestMethod.POST, produces = LedgerInitMessageConverter.CONTENT_TYPE_VALUE, consumes = LedgerInitMessageConverter.CONTENT_TYPE_VALUE)
@Override
public LedgerInitProposal requestPermission(@PathVariable(name = "requesterId") int requesterId, @RequestBody SignatureDigest signature) {
    if (requesterId == currentId) {
        throw new LedgerInitException("There is a id conflict!");
    }
    int retry = 0;
    while (currentId == -1 || ledgerInitConfig == null || localPermission == null) {
        // 本地尚未完成初始化;
        if (retry < 30) {
            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
            // ignore interrupted exception;
            }
        } else {
            return null;
        }
        retry++;
    }
    ParticipantNode[] participants = ledgerInitConfig.getParticipants();
    if (requesterId < 0 || requesterId >= participants.length) {
        throw new LedgerInitException("The id of requester is out of the bound of participant list!");
    }
    byte[] requestCodeBytes = BytesUtils.concat(BytesUtils.toBytes(requesterId), ledgerInitConfig.getLedgerSettings().getLedgerSeed());
    PubKey requesterPubKey = participants[requesterId].getPubKey();
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(requesterPubKey.getAlgorithm());
    if (!signatureFunction.verify(signature, requesterPubKey, requestCodeBytes)) {
        throw new LedgerInitException("The requester signature is invalid!");
    }
    return localPermission;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) ParticipantNode(com.jd.blockchain.ledger.ParticipantNode) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)7 PubKey (com.jd.blockchain.crypto.PubKey)3 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)2 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)2 ParticipantNode (com.jd.blockchain.ledger.ParticipantNode)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 JVMContractRuntimeConfig (com.jd.blockchain.contract.jvm.JVMContractRuntimeConfig)1 HashDigest (com.jd.blockchain.crypto.HashDigest)1 GenesisUser (com.jd.blockchain.ledger.GenesisUser)1 ParticipantProperties (com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties)1 SecurityInitData (com.jd.blockchain.ledger.SecurityInitData)1 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)1 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)1 LedgerInitProposal (com.jd.blockchain.ledger.core.LedgerInitProposal)1 LedgerInitProposalData (com.jd.blockchain.ledger.core.LedgerInitProposalData)1 LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)1 TxRequestBuilder (com.jd.blockchain.transaction.TxRequestBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1