Search in sources :

Example 6 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class MerkleHashDataSetTest method testKeyIndex.

/**
 * 测试存储的增长;
 */
@Test
public void testKeyIndex() {
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    String keyPrefix = "";
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setSupportedProviders(supportedProviders);
    cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256);
    cryptoConfig.setAutoVerifyHash(true);
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleHashDataset mds = new MerkleHashDataset(cryptoConfig, keyPrefix, storage, storage);
    Bytes keyA = Bytes.fromString("A");
    Bytes keyB = Bytes.fromString("B");
    Bytes keyC = Bytes.fromString("C");
    long v;
    v = mds.setValue(keyA, "A".getBytes(), -1);
    assertEquals(0, v);
    v = mds.setValue(keyB, "B".getBytes(), -1);
    assertEquals(0, v);
    v = mds.setValue(keyC, "C".getBytes(), -1);
    assertEquals(0, v);
    mds.commit();
    // 校验读最新值的正确性;
    assertEquals("A", new String(mds.getValue(keyA)));
    assertEquals("B", new String(mds.getValue(keyB)));
    assertEquals("C", new String(mds.getValue(keyC)));
    // 验证读最新数据项的正确性;
    DataEntry<Bytes, byte[]> data;
    data = mds.getDataEntry(keyA);
    assertEquals("A", new String(data.getValue()));
    assertEquals(0, data.getVersion());
    data = mds.getDataEntry(keyB);
    assertEquals("B", new String(data.getValue()));
    assertEquals(0, data.getVersion());
    data = mds.getDataEntry(keyC);
    assertEquals("C", new String(data.getValue()));
    assertEquals(0, data.getVersion());
    // 写入数据,验证版本号增加的正确性;
    v = mds.setValue(keyB, "B1".getBytes(), 0);
    assertEquals(1, v);
    v = mds.setValue(keyC, "C1".getBytes(), 0);
    assertEquals(1, v);
    mds.commit();
    // 校验读最新值的正确性;
    assertEquals("A", new String(mds.getValue(keyA, 0)));
    assertEquals("B1", new String(mds.getValue(keyB, 1)));
    assertEquals("C1", new String(mds.getValue(keyC, 1)));
    // 验证读最新数据项的正确性;
    data = mds.getDataEntry(keyA);
    assertEquals("A", new String(data.getValue()));
    assertEquals(0, data.getVersion());
    data = mds.getDataEntry(keyB);
    assertEquals("B1", new String(data.getValue()));
    assertEquals(1, data.getVersion());
    data = mds.getDataEntry(keyC);
    assertEquals("C1", new String(data.getValue()));
    assertEquals(1, data.getVersion());
}
Also used : Bytes(utils.Bytes) MerkleHashDataset(com.jd.blockchain.ledger.core.MerkleHashDataset) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Example 7 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class MerkleHashDataSetTest method testDataReload.

@Test
public void testDataReload() {
    String keyPrefix = "";
    Random rand = new Random();
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setSupportedProviders(supportedProviders);
    cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256);
    cryptoConfig.setAutoVerifyHash(true);
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleHashDataset mds = new MerkleHashDataset(cryptoConfig, keyPrefix, storage, storage);
    Dataset<String, byte[]> ds = DatasetHelper.map(mds);
    // 初始的时候没有任何数据,总是返回 null;
    DataEntry verKVEntry = ds.getDataEntry("NULL_KEY");
    byte[] vbytes = ds.getValue("NULL_KEY");
    assertNull(verKVEntry);
    assertNull(vbytes);
    Map<String, Long> dataVersions = new HashMap<>();
    Map<String, byte[]> dataValues = new HashMap<>();
    Map<HashDigest, Map<String, KeySnapshot>> history = new LinkedHashMap<>();
    HashDigest rootHash;
    // generate base data sample;
    // + rand.nextInt(1024);
    int count = 1024;
    String key;
    byte[] data = new byte[64];
    long v;
    MerkleProof proof;
    for (int i = 0; i < count; i++) {
        key = "data" + i;
        rand.nextBytes(data);
        v = ds.setValue(key, data, -1);
        dataVersions.put(key, v);
        dataValues.put(key + "_" + v, data);
        assertEquals(v, 0);
    }
    mds.commit();
    // Take snapshot;
    {
        rootHash = mds.getRootHash();
        Map<String, KeySnapshot> snapshot = new HashMap<>();
        for (int i = 0; i < count; i++) {
            key = "data" + i;
            // TODO: 暂时注释掉默克尔证明相关的内容;
            // proof = mds.getProof(key);
            // assertNotNull(proof);
            // assertEquals(rootHash, proof.getRootHash());
            KeySnapshot ks = new KeySnapshot();
            // ks.proof = proof;
            ks.rootHash = mds.getRootHash();
            ks.maxVersion = ds.getVersion(key);
            snapshot.put(key, ks);
        }
        history.put(rootHash, snapshot);
    }
    // verify;
    {
    // TODO: 暂时注释掉默克尔证明相关的内容;
    // MerkleHashDataset mdsReload = new MerkleHashDataset(rootHash, cryptoConfig, keyPrefix, storage, storage, true);
    // // verify every keys;
    // Map<String, KeySnapshot> snapshots = history.get(rootHash);
    // MerkleProof expProof;
    // for (int i = 0; i < count; i++) {
    // key = "data" + i;
    // proof = mdsReload.getProof(key);
    // assertNotNull(proof);
    // assertEquals(rootHash, proof.getRootHash());
    // expProof = snapshots.get(key).rootHash;
    // assertEquals(expProof.toString(), proof.toString());
    // }
    }
    // generate multi-version data sample;
    long expVer;
    long maxVer = 0;
    long minIdx = count;
    long maxIdx = 0;
    for (int t = 0; t < 100; t++) {
        int bound = rand.nextInt(500) + 1;
        for (int i = rand.nextInt(count); i < count; i = i + rand.nextInt(bound) + 1) {
            key = "data" + i;
            rand.nextBytes(data);
            expVer = dataVersions.get(key);
            v = ds.setValue(key, data, expVer);
            assertEquals(v, expVer + 1);
            dataVersions.put(key, v);
            dataValues.put(key + "_" + v, data);
            maxVer = v > maxVer ? v : maxVer;
            minIdx = i < minIdx ? i : minIdx;
            maxIdx = i > maxIdx ? i : maxIdx;
        }
        mds.commit();
        assertNotEquals(rootHash, mds.getRootHash());
        // Take snapshot;
        {
            // TODO: 暂时注释掉默克尔证明相关的内容;
            rootHash = mds.getRootHash();
            Map<String, KeySnapshot> snapshot = new HashMap<>();
            for (int i = 0; i < count; i++) {
                key = "data" + i;
                // TODO: 暂时注释掉默克尔证明相关的内容;
                // proof = mds.getProof(key);
                // assertNotNull(proof);
                // assertEquals(rootHash, proof.getRootHash());
                KeySnapshot ks = new KeySnapshot();
                ks.rootHash = rootHash;
                ks.maxVersion = ds.getVersion(key);
                snapshot.put(key, ks);
            }
            history.put(rootHash, snapshot);
        }
    }
    System.out.println(String.format("total count=%s; from %s to %s,  max version=%s;", count, minIdx, maxIdx, maxVer));
    {
        for (HashDigest hisRootHash : history.keySet()) {
            Map<String, KeySnapshot> snapshot = history.get(hisRootHash);
            MerkleHashDataset mdsReload = new MerkleHashDataset(hisRootHash, cryptoConfig, keyPrefix, storage, storage, true);
            Dataset<String, byte[]> dsReload = DatasetHelper.map(mdsReload);
            assertEquals(hisRootHash, mdsReload.getRootHash());
            // verify every keys;
            for (int i = 0; i < count; i++) {
                key = "data" + i;
                // 最新版本一致;
                long expLatestVersion = snapshot.get(key).maxVersion;
                long actualLatestVersion = dsReload.getVersion(key);
                assertEquals(expLatestVersion, actualLatestVersion);
                // TODO: 暂时注释掉默克尔证明相关的内容;
                // 数据证明一致;
                // proof = mdsReload.getProof(key);
                // assertNotNull(proof);
                // 
                // MerkleProof expProof = snapshot.get(key).rootHash;
                // assertEquals(expProof, proof);
                maxVer = dataVersions.get(key);
                assertTrue(actualLatestVersion > -1);
                assertTrue(actualLatestVersion <= maxVer);
                for (long j = 0; j < actualLatestVersion; j++) {
                    String keyver = key + "_" + j;
                    byte[] expValue = dataValues.get(keyver);
                    byte[] actualValue = dsReload.getValue(key, j);
                    assertTrue(BytesUtils.equals(expValue, actualValue));
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MerkleHashDataset(com.jd.blockchain.ledger.core.MerkleHashDataset) Dataset(utils.Dataset) MerkleHashDataset(com.jd.blockchain.ledger.core.MerkleHashDataset) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) MerkleProof(com.jd.blockchain.ledger.MerkleProof) LinkedHashMap(java.util.LinkedHashMap) DataEntry(utils.DataEntry) Random(java.util.Random) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Example 8 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class AccountSetTest method test.

@Test
public void test() {
    OpeningAccessPolicy accessPolicy = new OpeningAccessPolicy();
    MemoryKVStorage storage = new MemoryKVStorage();
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoConf = new CryptoConfig();
    cryptoConf.setSupportedProviders(supportedProviders);
    cryptoConf.setAutoVerifyHash(true);
    cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256);
    String keyPrefix = "";
    MerkleAccountSetEditor accset = new MerkleAccountSetEditor(cryptoConf, Bytes.fromString(keyPrefix), storage, storage, accessPolicy);
    BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate();
    accset.register(userKey.getAddress(), userKey.getPubKey());
    // 尚未提交之前,可以检索到账户的存在,但版本仍然标记为 -1;
    CompositeAccount userAcc = accset.getAccount(userKey.getAddress());
    assertNotNull(userAcc);
    assertTrue(accset.contains(userKey.getAddress()));
    accset.commit();
    HashDigest rootHash = accset.getRootHash();
    assertNotNull(rootHash);
    MerkleAccountSetEditor reloadAccSet = new MerkleAccountSetEditor(rootHash, cryptoConf, Bytes.fromString(keyPrefix), storage, storage, true, accessPolicy);
    CompositeAccount reloadUserAcc = reloadAccSet.getAccount(userKey.getAddress());
    assertNotNull(reloadUserAcc);
    assertTrue(reloadAccSet.contains(userKey.getAddress()));
    assertEquals(userAcc.getID().getAddress(), reloadUserAcc.getID().getAddress());
    assertEquals(userAcc.getID().getPubKey(), reloadUserAcc.getID().getPubKey());
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleAccountSetEditor(com.jd.blockchain.ledger.core.MerkleAccountSetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) OpeningAccessPolicy(com.jd.blockchain.ledger.core.OpeningAccessPolicy) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) CompositeAccount(com.jd.blockchain.ledger.core.CompositeAccount) Test(org.junit.Test)

Example 9 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class BaseAccountTest method basicTest.

@Test
public void basicTest() {
    String keyPrefix = "";
    MemoryKVStorage testStorage = new MemoryKVStorage();
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoConf = new CryptoConfig();
    cryptoConf.setSupportedProviders(supportedProviders);
    cryptoConf.setAutoVerifyHash(true);
    cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256);
    // OpeningAccessPolicy accPlc = new OpeningAccessPolicy();
    BlockchainKeypair bck = BlockchainKeyGenerator.getInstance().generate();
    // 新建账户;
    MerkleComplecatedAccount baseAccount = new MerkleComplecatedAccount(bck.getIdentity(), cryptoConf, Bytes.fromString(keyPrefix), testStorage, testStorage);
    // 初始化新账户时,先写入PubKey;
    assertTrue(baseAccount.isUpdated());
    assertFalse(baseAccount.isReadonly());
    // 在空白状态下写入数据;
    long v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), 0);
    // 预期失败;
    assertEquals(-1, v);
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), 1);
    // 预期失败;
    assertEquals(-1, v);
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), -1);
    // 预期成功;
    assertEquals(0, v);
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A-1"), -1);
    // 已经存在版本,指定版本号-1,预期导致失败;
    assertEquals(-1, v);
    baseAccount.commit();
    v = 0;
    for (int i = 0; i < 10; i++) {
        long s = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A_" + i), v);
        baseAccount.commit();
        // 预期成功;
        assertEquals(v + 1, s);
        v++;
    }
    v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A_" + v), v + 1);
    // 预期成功;
    assertEquals(-1, v);
    System.out.println("============== commit ==============");
    baseAccount.commit();
}
Also used : MerkleComplecatedAccount(com.jd.blockchain.ledger.core.MerkleComplecatedAccount) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Example 10 with CryptoProvider

use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.

the class LedgerManager method checkCryptoSetting.

/**
 * 检查账本的密码参数设置与本地节点的运行时环境是否匹配;
 *
 * @param cryptoSetting
 * @param ledgerHash
 */
private void checkCryptoSetting(CryptoSetting cryptoSetting, HashDigest ledgerHash) {
    CryptoProvider[] cryptoProviders = cryptoSetting.getSupportedProviders();
    if (cryptoProviders == null || cryptoProviders.length == 0) {
        throw new LedgerException("No supported crypto service providers has been setted in the ledger[" + ledgerHash.toBase58() + "]!");
    }
    for (CryptoProvider cp : cryptoProviders) {
        CryptoProvider regCp = Crypto.getProvider(cp.getName());
        checkCryptoProviderConsistency(regCp, cp);
    }
}
Also used : CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) LedgerException(com.jd.blockchain.ledger.LedgerException)

Aggregations

CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)18 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)15 Test (org.junit.Test)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)6 Bytes (utils.Bytes)6 HashDigest (com.jd.blockchain.crypto.HashDigest)5 Random (java.util.Random)4 CryptoAlgorithm (com.jd.blockchain.crypto.CryptoAlgorithm)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)3 MerkleHashDataset (com.jd.blockchain.ledger.core.MerkleHashDataset)3 LedgerException (com.jd.blockchain.ledger.LedgerException)2 LedgerSettings (com.jd.blockchain.ledger.LedgerSettings)2 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)2 LedgerConfiguration (com.jd.blockchain.ledger.core.LedgerConfiguration)2 ConsensusParticipantData (com.jd.blockchain.transaction.ConsensusParticipantData)2 LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 NetworkAddress (utils.net.NetworkAddress)2 AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)1