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;
}
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;
}
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();
}
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);
}
}
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;
}
Aggregations