Search in sources :

Example 1 with DataEntry

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

the class ContractLedgerQueryService method getDataEntries.

@Override
public TypedKVEntry[] getDataEntries(String address, int fromIndex, int count) {
    DataAccountSet dataAccountSet = ledgerQuery.getDataAccountSet();
    DataAccount dataAccount = dataAccountSet.getAccount(Bytes.fromBase58(address));
    QueryArgs queryArgs = QueryUtils.calFromIndexAndCount(fromIndex, count, (int) dataAccount.getDataset().getDataCount());
    SkippingIterator<DataEntry<String, TypedValue>> iterator = ((IteratorDataset) dataAccount.getDataset()).kvIterator();
    iterator.skip(queryArgs.getFrom());
    TypedKVEntry[] typedKVEntries = iterator.next(queryArgs.getCount(), TypedKVEntry.class, entry -> new TypedKVData(entry.getKey(), entry.getVersion(), entry.getValue()));
    return typedKVEntries;
}
Also used : DataAccount(com.jd.blockchain.ledger.core.DataAccount) DataEntry(utils.DataEntry) TypedKVEntry(com.jd.blockchain.ledger.TypedKVEntry) TypedKVData(com.jd.blockchain.ledger.TypedKVData) QueryArgs(utils.query.QueryArgs) IteratorDataset(com.jd.blockchain.ledger.core.IteratorDataset) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet)

Example 2 with DataEntry

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

the class MultiLedgerQueryService method getDataEntries.

@Override
public TypedKVEntry[] getDataEntries(HashDigest ledgerHash, String address, int fromIndex, int count) {
    checkLedgerHash(ledgerHash);
    LedgerBlock block = ledger.getLatestBlock();
    DataAccountSet dataAccountSet = ledger.getDataAccountSet(block);
    DataAccount dataAccount = dataAccountSet.getAccount(Bytes.fromBase58(address));
    QueryArgs queryArgs = QueryUtils.calFromIndexAndCount(fromIndex, count, (int) dataAccount.getDataset().getDataCount());
    SkippingIterator<DataEntry<String, TypedValue>> iterator = ((IteratorDataset) dataAccount.getDataset()).kvIterator();
    iterator.skip(queryArgs.getFrom());
    TypedKVEntry[] typedKVEntries = iterator.next(queryArgs.getCount(), TypedKVEntry.class, new Mapper<DataEntry<String, TypedValue>, TypedKVEntry>() {

        @Override
        public TypedKVEntry from(DataEntry<String, TypedValue> entry) {
            return new TypedKVData(entry.getKey(), entry.getVersion(), entry.getValue());
        }
    });
    return typedKVEntries;
}
Also used : DataEntry(utils.DataEntry) QueryArgs(utils.query.QueryArgs)

Example 3 with DataEntry

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

the class MerkleHashDataSetTest method testInsertSameData.

@Test
public void testInsertSameData() {
    String keyPrefix = "";
    Random rand = new Random();
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    MerkleHashDataset mds = new MerkleHashDataset(cryptoSetting, 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];
    rand.nextBytes(data);
    long v;
    MerkleProof proof;
    for (int i = 0; i < count; i++) {
        key = "data" + i;
        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.rootHash = mds.getRootHash();
            ks.maxVersion = ds.getVersion(key);
            snapshot.put(key, ks);
        }
        history.put(rootHash, snapshot);
    }
    // verify;
    {
        MerkleHashDataset mdsReload = new MerkleHashDataset(rootHash, cryptoSetting, keyPrefix, storage, storage, true);
        Dataset<String, byte[]> dsReload = DatasetHelper.map(mdsReload);
        // verify every keys;
        Map<String, KeySnapshot> snapshot = history.get(rootHash);
        MerkleProof expProof;
        for (int i = 0; i < count; i++) {
            key = "data" + i;
            // TODO: 暂时注释掉默克尔证明相关的内容;
            // proof = mdsReload.getProof(key);
            // assertNotNull(proof);
            // assertEquals(rootHash, proof.getRootHash());
            // expProof = snapshot.get(key).rootHash;
            // assertEquals(expProof.toString(), proof.toString());
            byte[] value = dsReload.getValue(key);
            assertTrue(BytesUtils.equals(data, value));
        }
    }
}
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) MerkleProof(com.jd.blockchain.ledger.MerkleProof) LinkedHashMap(java.util.LinkedHashMap) DataEntry(utils.DataEntry) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) 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) Test(org.junit.Test)

Example 4 with DataEntry

use of utils.DataEntry 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 5 with DataEntry

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

the class MerkleSequenceDataset method getLatestDataEntries.

public DataEntry<Bytes, byte[]>[] getLatestDataEntries(long fromIndex, int count) {
    if (count > LedgerConsts.MAX_LIST_COUNT) {
        throw new IllegalArgumentException("Count exceed the upper limit[" + LedgerConsts.MAX_LIST_COUNT + "]!");
    }
    if (fromIndex < 0 || (fromIndex + count) > merkleTree.getDataCount()) {
        throw new IllegalArgumentException("Index out of bound!");
    }
    if (count == 0) {
        return EMPTY_ENTRIES;
    }
    @SuppressWarnings("unchecked") DataEntry<Bytes, byte[]>[] values = new DataEntry[count];
    byte[] bytesValue;
    for (int i = 0; i < count; i++) {
        MerkleDataNode dataNode = merkleTree.getData(fromIndex + i);
        Bytes dataKey = encodeDataKey(dataNode.getKey());
        bytesValue = valueStorage.get(dataKey, dataNode.getVersion());
        values[i] = new VersioningKVData<Bytes, byte[]>(dataNode.getKey(), dataNode.getVersion(), bytesValue);
    }
    return values;
}
Also used : DataEntry(utils.DataEntry) Bytes(utils.Bytes) MerkleDataNode(com.jd.blockchain.ledger.MerkleDataNode)

Aggregations

DataEntry (utils.DataEntry)10 QueryArgs (utils.query.QueryArgs)4 TypedKVData (com.jd.blockchain.ledger.TypedKVData)3 TypedKVEntry (com.jd.blockchain.ledger.TypedKVEntry)3 DataAccount (com.jd.blockchain.ledger.core.DataAccount)3 DataAccountSet (com.jd.blockchain.ledger.core.DataAccountSet)3 IteratorDataset (com.jd.blockchain.ledger.core.IteratorDataset)3 Bytes (utils.Bytes)3 HashDigest (com.jd.blockchain.crypto.HashDigest)2 MerkleProof (com.jd.blockchain.ledger.MerkleProof)2 MerkleHashDataset (com.jd.blockchain.ledger.core.MerkleHashDataset)2 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Random (java.util.Random)2 Test (org.junit.Test)2 Dataset (utils.Dataset)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)1 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)1