Search in sources :

Example 1 with DbConnection

use of com.jd.blockchain.storage.service.DbConnection in project jdchain-core by blockchain-jd-com.

the class RocksDBStorageTest method test.

@Test
public void test() {
    String dbUri = initEmptyDB("rocksdb_storage_test");
    long expectedVersion;
    try (DbConnectionFactory dbConnFactory = new RocksDBConnectionFactory()) {
        DbConnection conn = dbConnFactory.connect(dbUri);
        VersioningKVStorage verStorage = conn.getStorageService().getVersioningKVStorage();
        ExPolicyKVStorage exStorage = conn.getStorageService().getExPolicyKVStorage();
        expectedVersion = test(verStorage);
        test(exStorage);
    }
    try (DbConnectionFactory dbConnFactory = new RocksDBConnectionFactory()) {
        DbConnection conn = dbConnFactory.connect(dbUri);
        VersioningKVStorage verStorage = conn.getStorageService().getVersioningKVStorage();
        ExPolicyKVStorage exStorage = conn.getStorageService().getExPolicyKVStorage();
        testAfterReload(verStorage, expectedVersion);
        testAfterReload(exStorage);
    }
}
Also used : ExPolicyKVStorage(com.jd.blockchain.storage.service.ExPolicyKVStorage) VersioningKVStorage(com.jd.blockchain.storage.service.VersioningKVStorage) DbConnectionFactory(com.jd.blockchain.storage.service.DbConnectionFactory) RocksDBConnectionFactory(com.jd.blockchain.storage.service.impl.rocksdb.RocksDBConnectionFactory) DbConnection(com.jd.blockchain.storage.service.DbConnection) Test(org.junit.Test)

Example 2 with DbConnection

use of com.jd.blockchain.storage.service.DbConnection in project jdchain-core by blockchain-jd-com.

the class Test method main.

public static void main(String[] args) {
    Bytes key = Bytes.fromString("test111");
    try (Jedis jedis = new Jedis("192.168.151.33", 6379)) {
        jedis.connect();
        // BytesUtils.toBytes(key, "UTF-8");
        byte[] kbytes = key.toBytes();
        byte[] valueBytes = jedis.get(kbytes);
        String value = BytesUtils.toString(valueBytes, "UTF-8");
        System.out.println(String.format("%s=%s", key, value));
    }
    System.out.println("=================================");
    System.out.println("================= test expolicy storage =================");
    RedisConnectionFactory connFactory = new RedisConnectionFactory();
    try (DbConnection conn = connFactory.connect("redis://192.168.151.33:6379")) {
        ExPolicyKVStorage exKVStorage = conn.getStorageService().getExPolicyKVStorage();
        byte[] valueBytes = exKVStorage.get(key);
        String value = BytesUtils.toString(valueBytes, "UTF-8");
        System.out.println(String.format("%s=%s", key, value));
        System.out.println(String.format("%s=%s", key, value));
        boolean success = exKVStorage.set(key, BytesUtils.toBytes("New Value by ExPolicyStorage interface..."), ExPolicy.NOT_EXISTING);
        System.out.println("update key when NX " + (success ? "success" : "fail"));
        success = exKVStorage.set(key, BytesUtils.toBytes("New Value by ExPolicyStorage interface..."), ExPolicy.EXISTING);
        System.out.println("update key when XX " + (success ? "success" : "fail"));
        valueBytes = exKVStorage.get(key);
        value = BytesUtils.toString(valueBytes, "UTF-8");
        System.out.println(String.format("Retrieve... %s=%s", key, value));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.out.println("=================================");
    try (DbConnection conn = connFactory.connect("redis://192.168.151.33:6379/0")) {
        ExPolicyKVStorage exKVStorage = conn.getStorageService().getExPolicyKVStorage();
        byte[] valueBytes = exKVStorage.get(key);
        String value = BytesUtils.toString(valueBytes, "UTF-8");
        System.out.println(String.format("%s=%s", key, value));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.out.println("=================================");
    System.out.println("================= test versioning storage =================");
    Bytes addr = Bytes.fromString("User001");
    try (DbConnection conn = connFactory.connect("redis://192.168.151.33:6379/0")) {
        VersioningKVStorage verStorage = conn.getStorageService().getVersioningKVStorage();
        long version = verStorage.getVersion(addr);
        System.out.println(String.format("Version of key[%s]=%s", addr, version));
        byte[] v1 = BytesUtils.toBytes("value-" + version);
        version = verStorage.set(addr, v1, version);
        System.out.println(String.format("Update[%s] to V1, version=%s", addr, version));
        byte[] v2 = BytesUtils.toBytes("value-" + version);
        version = verStorage.set(addr, v2, version);
        System.out.println(String.format("Update[%s] to V2, version=%s", addr, version));
        byte[] v3 = BytesUtils.toBytes("value-" + version);
        version = verStorage.set(addr, v3, version);
        System.out.println(String.format("Update[%s] to V3, version=%s", addr, version));
        version = verStorage.getVersion(addr);
        System.out.println(String.format("Now the latest version of key[%s]=%s", addr, version));
        for (int i = 0; i <= version; i++) {
            String value = BytesUtils.toString(verStorage.get(addr, i));
            System.out.println(String.format("The version[%s] value of key[%s] is :%s", i, addr, value));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.out.println("=================================");
}
Also used : Bytes(utils.Bytes) Jedis(redis.clients.jedis.Jedis) ExPolicyKVStorage(com.jd.blockchain.storage.service.ExPolicyKVStorage) VersioningKVStorage(com.jd.blockchain.storage.service.VersioningKVStorage) RedisConnectionFactory(com.jd.blockchain.storage.service.impl.redis.RedisConnectionFactory) DbConnection(com.jd.blockchain.storage.service.DbConnection)

Example 3 with DbConnection

use of com.jd.blockchain.storage.service.DbConnection in project jdchain-core by blockchain-jd-com.

the class ManagementController method setConfig.

@Override
public NodeServer setConfig(BindingConfig bindingConfig, HashDigest ledgerHash) {
    // LedgerBindingConfig.BindingConfig bindingConfig = config.getLedger(ledgerHash);
    LedgerQuery ledgerRepository = null;
    NodeServer server = null;
    ParticipantNode currentNode = null;
    LedgerAdminInfo ledgerAdminAccount = null;
    try {
        DbConnection dbConnNew = connFactory.connect(bindingConfig.getDbConnection().getUri(), bindingConfig.getDbConnection().getPassword());
        ledgerRepository = ledgerManager.register(ledgerHash, dbConnNew.getStorageService(), bindingConfig.getDataStructure());
        ledgerAdminAccount = ledgerRepository.getAdminInfo();
        ConsensusProvider provider = getProvider(ledgerAdminAccount);
        // load consensus setting;
        ConsensusViewSettings csSettings = getConsensusSetting(ledgerAdminAccount);
        for (ParticipantNode participantNode : ledgerAdminAccount.getParticipants()) {
            LOGGER.debug("[!!!] ParticipantNode.getAddress().toString()= {}", participantNode.getAddress().toString());
            if (participantNode.getAddress().toString().equals(bindingConfig.getParticipant().getAddress())) {
                currentNode = participantNode;
                break;
            }
        }
        if (currentNode == null) {
            throw new IllegalArgumentException("Current node is not found from the participant settings of ledger[" + ledgerHash.toBase58() + "]!");
        }
        LedgerMetadata_V2 metadata = ledgerRepository.getAdminInfo().getMetadata();
        ledgerIdMode.put(ledgerHash, null != metadata.getIdentityMode() ? metadata.getIdentityMode() : IdentityMode.KEYPAIR);
        if (metadata.getIdentityMode() == IdentityMode.CA) {
            X509Certificate peerCA = CertificateUtils.parseCertificate(ledgerRepository.getUserAccountSet().getAccount(currentNode.getAddress()).getCertificate());
            X509Certificate[] issuers = CertificateUtils.findIssuers(peerCA, CertificateUtils.parseCertificates(metadata.getLedgerCertificates()));
            // 校验根证书
            Arrays.stream(issuers).forEach(issuer -> CertificateUtils.checkCACertificate(issuer));
            CertificateUtils.checkValidityAny(issuers);
            // 校验节点证书
            CertificateUtils.checkCertificateRole(peerCA, CertificateRole.PEER);
            CertificateUtils.checkValidity(peerCA);
        }
        // 处于ACTIVED状态的参与方才会创建共识节点服务
        if (currentNode.getParticipantNodeState() == ParticipantNodeState.CONSENSUS) {
            ServerSettings serverSettings = provider.getServerFactory().buildServerSettings(ledgerHash.toBase58(), csSettings, currentNode.getAddress().toBase58(), bindingConfig.getSslSecurity(), bindingConfig.getExtraProperties());
            ((LedgerStateManager) consensusStateManager).setLatestStateId(ledgerRepository.retrieveLatestBlockHeight());
            Storage consensusRuntimeStorage = getConsensusRuntimeStorage(ledgerHash);
            server = provider.getServerFactory().setupServer(serverSettings, consensusMessageHandler, consensusStateManager, consensusRuntimeStorage);
            ledgerPeers.put(ledgerHash, server);
        }
        ledgerQuerys.put(ledgerHash, ledgerRepository);
        ledgerCurrNodes.put(ledgerHash, currentNode);
        ledgerCryptoSettings.put(ledgerHash, ledgerAdminAccount.getSettings().getCryptoSetting());
        ledgerKeypairs.put(ledgerHash, loadIdentity(currentNode, bindingConfig));
        bindingConfigs.put(ledgerHash, bindingConfig);
    } catch (Exception e) {
        ledgerManager.unregister(ledgerHash);
        throw e;
    }
    return server;
}
Also used : DbConnection(com.jd.blockchain.storage.service.DbConnection) X509Certificate(java.security.cert.X509Certificate) BusinessException(utils.BusinessException) BftsmartConsensusViewSettings(com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings) LedgerStateManager(com.jd.blockchain.peer.consensus.LedgerStateManager) Storage(utils.io.Storage)

Example 4 with DbConnection

use of com.jd.blockchain.storage.service.DbConnection in project jdchain-core by blockchain-jd-com.

the class LedgerInitializeWebController method closeDb.

public void closeDb() {
    if (dbConn != null) {
        DbConnection connection = dbConn;
        dbConn = null;
        try {
            connection.close();
        } catch (IOException e) {
            prompter.error(e, "Error occurred on closing db connection! --" + e.getMessage());
        }
    }
}
Also used : IOException(java.io.IOException) DbConnection(com.jd.blockchain.storage.service.DbConnection)

Aggregations

DbConnection (com.jd.blockchain.storage.service.DbConnection)4 ExPolicyKVStorage (com.jd.blockchain.storage.service.ExPolicyKVStorage)2 VersioningKVStorage (com.jd.blockchain.storage.service.VersioningKVStorage)2 BftsmartConsensusViewSettings (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusViewSettings)1 LedgerStateManager (com.jd.blockchain.peer.consensus.LedgerStateManager)1 DbConnectionFactory (com.jd.blockchain.storage.service.DbConnectionFactory)1 RedisConnectionFactory (com.jd.blockchain.storage.service.impl.redis.RedisConnectionFactory)1 RocksDBConnectionFactory (com.jd.blockchain.storage.service.impl.rocksdb.RocksDBConnectionFactory)1 IOException (java.io.IOException)1 X509Certificate (java.security.cert.X509Certificate)1 Test (org.junit.Test)1 Jedis (redis.clients.jedis.Jedis)1 BusinessException (utils.BusinessException)1 Bytes (utils.Bytes)1 Storage (utils.io.Storage)1