Search in sources :

Example 1 with Bytes

use of utils.Bytes in project jdchain-core by blockchain-jd-com.

the class JVMContractEngine method setupContract.

@Override
public ContractCode setupContract(ContractInfo contractInfo) {
    Bytes address = contractInfo.getAddress();
    long version = contractInfo.getChainCodeVersion();
    String codeName = getCodeName(address, version);
    ContractCode contractCode = contracts.get(version);
    if (null == contractCode) {
        synchronized (JVMContractEngine.class) {
            contractCode = contracts.get(version);
            if (null == contractCode) {
                contractCode = loaders.get(contractInfo.getLang()).loadContract(address, version, contractInfo.getChainCode());
                if (null != contractCode) {
                    contracts.put(codeName, contractCode);
                }
            }
        }
    }
    return contractCode;
}
Also used : Bytes(utils.Bytes) ContractCode(com.jd.blockchain.contract.engine.ContractCode)

Example 2 with Bytes

use of utils.Bytes in project jdchain-core by blockchain-jd-com.

the class UserAuthorizeOperationHandle method doProcess.

@Override
protected void doProcess(UserAuthorizeOperation operation, LedgerTransactionContext transactionContext, TransactionRequestExtension request, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.CONFIGURE_ROLES, MultiIDsPolicy.AT_LEAST_ONE);
    // 操作账本;
    UserRolesEntry[] urcfgs = operation.getUserRolesAuthorizations();
    UserAuthorizationSettings userRoleDataset = transactionContext.getDataset().getAdminDataset().getAdminSettings().getAuthorizations();
    RolePrivilegeSettings rolesSettings = transactionContext.getDataset().getAdminDataset().getAdminSettings().getRolePrivileges();
    if (urcfgs != null) {
        for (UserRolesEntry urcfg : urcfgs) {
            // 
            String[] authRoles = urcfg.getAuthorizedRoles();
            Arrays.stream(authRoles).forEach(role -> {
                if (!rolesSettings.contains(role)) {
                    throw new RoleDoesNotExistException(String.format("Role doesn't exist! --[Role=%s]", role));
                }
            });
            for (Bytes address : urcfg.getUserAddresses()) {
                UserRoles ur = userRoleDataset.getUserRoles(address);
                if (ur == null) {
                    // 这是新的授权;
                    RolesPolicy policy = urcfg.getPolicy();
                    if (policy == null) {
                        policy = RolesPolicy.UNION;
                    }
                    ((UserRoleDatasetEditor) userRoleDataset).addUserRoles(address, policy, authRoles);
                } else {
                    // 更改之前的授权;
                    ur.addRoles(authRoles);
                    ur.removeRoles(urcfg.getUnauthorizedRoles());
                    // 如果请求中设置了策略,才进行更新;
                    RolesPolicy policy = urcfg.getPolicy();
                    if (policy != null) {
                        ur.setPolicy(policy);
                    }
                    ((UserRoleDatasetEditor) userRoleDataset).updateUserRoles(ur);
                }
            }
        }
    }
}
Also used : UserRolesEntry(com.jd.blockchain.ledger.UserAuthorizeOperation.UserRolesEntry) Bytes(utils.Bytes) RolesPolicy(com.jd.blockchain.ledger.RolesPolicy) UserRoleDatasetEditor(com.jd.blockchain.ledger.core.UserRoleDatasetEditor) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) UserRoles(com.jd.blockchain.ledger.UserRoles) RoleDoesNotExistException(com.jd.blockchain.ledger.RoleDoesNotExistException) UserAuthorizationSettings(com.jd.blockchain.ledger.UserAuthorizationSettings) RolePrivilegeSettings(com.jd.blockchain.ledger.RolePrivilegeSettings)

Example 3 with Bytes

use of utils.Bytes in project jdchain-core by blockchain-jd-com.

the class ConsensusSettingsUpdateOperationHandle method updateProperties.

private void updateProperties(ConsensusSettingsUpdateOperation op, LedgerAdminDataSet adminAccountDataSet, LedgerQuery previousBlockDataset) {
    ConsensusProvider provider = null;
    ConsensusViewSettings consensusSettings = null;
    provider = ConsensusProviders.getProvider(((LedgerAdminDataSetEditor) adminAccountDataSet).getSettings().getConsensusProvider());
    consensusSettings = provider.getSettingsFactory().getConsensusSettingsEncoder().decode(((LedgerAdminDataSetEditor) adminAccountDataSet).getSettings().getConsensusSetting().toBytes());
    // update consensus settings according to properties config
    ConsensusViewSettings newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().updateSettings(consensusSettings, PropertiesUtils.createProperties(op.getProperties()));
    LedgerSettings ledgerSetting = new LedgerConfiguration(((LedgerAdminDataSetEditor) adminAccountDataSet).getSettings().getConsensusProvider(), new Bytes(provider.getSettingsFactory().getConsensusSettingsEncoder().encode(newConsensusSettings)), ((LedgerAdminDataSetEditor) adminAccountDataSet).getPreviousSetting().getCryptoSetting());
    // set new consensus settings to ledger
    ((LedgerAdminDataSetEditor) adminAccountDataSet).setLedgerSetting(ledgerSetting);
}
Also used : Bytes(utils.Bytes)

Example 4 with Bytes

use of utils.Bytes in project jdchain-core by blockchain-jd-com.

the class ConsensusSettingsUpdateOperationHandle method switchConsensus.

private void switchConsensus(ConsensusSettingsUpdateOperation op, LedgerAdminDataSet adminAccountDataSet, LedgerQuery previousBlockDataset) {
    ConsensusProvider provider = ConsensusProviders.getProvider(op.getProvider());
    int participantCount = (int) adminAccountDataSet.getParticipantDataset().getParticipantCount();
    ParticipantNode[] participantNodes = adminAccountDataSet.getAdminSettings().getParticipants();
    ArrayList<Replica> replicas = new ArrayList<>();
    for (int i = 0; i < participantCount; i++) {
        if (participantNodes[i].getParticipantNodeState() == ParticipantNodeState.CONSENSUS) {
            Replica replica = new ReplicaImpl(participantNodes[i].getId(), participantNodes[i].getAddress(), participantNodes[i].getName(), participantNodes[i].getPubKey());
            replicas.add(replica);
        }
    }
    // create new consensus settings according to properties config
    ConsensusViewSettings newConsensusSettings = provider.getSettingsFactory().getConsensusSettingsBuilder().createSettings(PropertiesUtils.createProperties(op.getProperties()), replicas.toArray(new Replica[replicas.size()]));
    LedgerSettings ledgerSetting = new LedgerConfiguration(op.getProvider(), new Bytes(provider.getSettingsFactory().getConsensusSettingsEncoder().encode(newConsensusSettings)), ((LedgerAdminDataSetEditor) adminAccountDataSet).getPreviousSetting().getCryptoSetting());
    // set new consensus settings to ledger
    ((LedgerAdminDataSetEditor) adminAccountDataSet).setLedgerSetting(ledgerSetting);
}
Also used : ArrayList(java.util.ArrayList) Bytes(utils.Bytes)

Example 5 with Bytes

use of utils.Bytes in project jdchain-core by blockchain-jd-com.

the class TransactionSetEditor method loadResultKv.

private TransactionResult loadResultKv(long seq) {
    // transaction has only one version;
    Bytes key = encodeKvResultKey(seq);
    byte[] txBytes = txStateSet.getValue(key, 0);
    if (txBytes == null) {
        return null;
    }
    return BinaryProtocol.decode(txBytes, TransactionResult.class);
}
Also used : Bytes(utils.Bytes)

Aggregations

Bytes (utils.Bytes)122 HashDigest (com.jd.blockchain.crypto.HashDigest)16 Test (org.junit.Test)16 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)13 LedgerException (com.jd.blockchain.ledger.LedgerException)10 Random (java.util.Random)10 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)8 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)7 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)6 MerkleProof (com.jd.blockchain.ledger.MerkleProof)6 MerkleProofException (com.jd.blockchain.ledger.core.MerkleProofException)6 PubKey (com.jd.blockchain.crypto.PubKey)5 IllegalTransactionException (com.jd.blockchain.ledger.IllegalTransactionException)5 MerkleDataNode (com.jd.blockchain.ledger.MerkleDataNode)5 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)5 HashFunction (com.jd.blockchain.crypto.HashFunction)4 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)4 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)4 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)3 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)3