Search in sources :

Example 21 with VersioningKVData

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

the class MerkleHashTrieTest method testMerkleTreeDiffNewLeafType.

@Test
public void testMerkleTreeDiffNewLeafType() {
    CryptoSetting cryptoSetting = createCryptoSetting();
    MemoryKVStorage storage = new MemoryKVStorage();
    List<String> newdataListString = new ArrayList<String>();
    List<String> dataListString = new ArrayList<String>();
    List<VersioningKVData<String, byte[]>> dataList = new ArrayList<VersioningKVData<String, byte[]>>();
    List<VersioningKVData<String, byte[]>> newdataList = new ArrayList<VersioningKVData<String, byte[]>>();
    int count = 1;
    int newAddCount = 1;
    VersioningKVData<String, byte[]> orginData0 = new VersioningKVData<String, byte[]>("KEY-0", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    dataList.add(orginData0);
    dataListString.add(orginData0.getKey());
    VersioningKVData<String, byte[]>[] datas = toArray(dataList);
    MerkleHashTrie merkleTree = newMerkleTree_with_committed(datas, cryptoSetting, storage);
    HashDigest rootHash0 = merkleTree.getRootHash();
    assertNotNull(rootHash0);
    assertEquals(count, merkleTree.getTotalKeys());
    assertEquals(count, merkleTree.getTotalRecords());
    // reload and add random key data item;
    MerkleHashTrie merkleTree_reload = new MerkleHashTrie(rootHash0, cryptoSetting, KEY_PREFIX, storage, false);
    assertEquals(count, merkleTree_reload.getTotalKeys());
    assertEquals(count, merkleTree_reload.getTotalRecords());
    assertEquals(rootHash0, merkleTree_reload.getRootHash());
    VersioningKVData<String, byte[]> data0 = new VersioningKVData<String, byte[]>("KEY-1741789838495252", 0L, BytesUtils.concat(BytesUtils.toBytes(0), BytesUtils.toBytes("VALUE")));
    newdataList.add(data0);
    for (int i = 0; i < newdataList.size(); i++) {
        merkleTree_reload.setData(newdataList.get(i).getKey(), newdataList.get(i).getVersion(), newdataList.get(i).getValue());
        newdataListString.add(newdataList.get(i).getKey());
    }
    merkleTree_reload.commit();
    HashDigest rootHash1 = merkleTree_reload.getRootHash();
    assertNotNull(rootHash1);
    assertNotEquals(rootHash0, rootHash1);
    assertEquals(count + newAddCount, merkleTree_reload.getTotalKeys());
    assertEquals(count + newAddCount, merkleTree_reload.getTotalRecords());
    SkippingIterator<KVEntry> diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    // max boundary skip test
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped = diffIterator.skip(newAddCount);
    assertEquals(newAddCount, skipped);
    assertFalse(diffIterator.hasNext());
    // re-interator and random skip test
    int skipNum = 0;
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(-1, diffIterator.getCursor());
    assertTrue(diffIterator.hasNext());
    long skipped1 = diffIterator.skip(skipNum);
    assertEquals(skipNum, skipped1);
    int diffNum = 0;
    // TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
    // while (diffIterator.hasNext()) {
    // MerkleData data = diffIterator.next();
    // assertNotNull(data);
    // assertFalse(dataList.contains(new String(data.getKey())));
    // assertTrue(newdataListString.contains(new String(data.getKey())));
    // diffNum++;
    // }
    // assertEquals(diffNum, diffIterator.getCount() - skipNum);
    // re-interator and next test
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
    int diffNum1 = 0;
    assertEquals(newAddCount, diffIterator.getTotalCount());
    while (diffIterator.hasNext()) {
        KVEntry data = diffIterator.next();
        assertNotNull(data);
        diffNum1++;
    }
    assertFalse(diffIterator.hasNext());
    assertEquals(newAddCount - 1, diffIterator.getCursor());
    assertEquals(newAddCount, diffIterator.getTotalCount());
    assertEquals(diffNum1, diffIterator.getTotalCount());
    // re-interator and test next key consistency
    diffIterator = merkleTree_reload.getKeyDiffIterator(merkleTree);
// TODO: 无效的验证逻辑; by huanghaiquan at 2020-07-15;
// while (diffIterator.hasNext()) {
// MerkleData data = diffIterator.next();
// assertNotNull(data);
// assertFalse(dataList.contains(new String(data.getKey())));
// assertTrue(newdataListString.contains(new String(data.getKey())));
// }
}
Also used : ArrayList(java.util.ArrayList) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) KVEntry(com.jd.blockchain.ledger.merkletree.KVEntry) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 22 with VersioningKVData

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

the class MerkleSequenceDataset method getLatestDataEntry.

public DataEntry<Bytes, byte[]> getLatestDataEntry(long index) {
    if (index < 0 || index + 1 > merkleTree.getDataCount()) {
        throw new IllegalArgumentException("Index out of bound!");
    }
    byte[] bytesValue;
    MerkleDataNode dataNode = merkleTree.getData(index);
    Bytes dataKey = encodeDataKey(dataNode.getKey());
    bytesValue = valueStorage.get(dataKey, dataNode.getVersion());
    DataEntry<Bytes, byte[]> entry = new VersioningKVData<Bytes, byte[]>(dataNode.getKey(), dataNode.getVersion(), bytesValue);
    return entry;
}
Also used : Bytes(utils.Bytes) MerkleDataNode(com.jd.blockchain.ledger.MerkleDataNode) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData)

Example 23 with VersioningKVData

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

the class KvDataset method getDataEntry.

@Override
public DataEntry<Bytes, byte[]> getDataEntry(Bytes key, long version) {
    long latestVersion = getVersion(key);
    if (latestVersion < 0 || version > latestVersion) {
        // by the current merkletree;
        return null;
    }
    version = version < 0 ? latestVersion : version;
    Bytes dataKey = encodeDataKey(key);
    byte[] value = valueStorage.get(dataKey, version);
    if (value == null) {
        throw new DataExistException("Expected value does not exist!");
    }
    return new VersioningKVData<Bytes, byte[]>(key, version, value);
}
Also used : Bytes(utils.Bytes) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData)

Example 24 with VersioningKVData

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

the class MerkleSequenceDataset method getDataEntry.

@Override
public DataEntry<Bytes, byte[]> getDataEntry(Bytes key, long version) {
    long latestVersion = getMerkleVersion(key);
    if (latestVersion < 0 || version > latestVersion) {
        // by the current merkletree;
        return null;
    }
    version = version < 0 ? latestVersion : version;
    Bytes dataKey = encodeDataKey(key);
    byte[] value = valueStorage.get(dataKey, version);
    if (value == null) {
        return null;
    }
    return new VersioningKVData<Bytes, byte[]>(key, version, value);
}
Also used : Bytes(utils.Bytes) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData)

Example 25 with VersioningKVData

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

the class KvDataset method getKvDataEntryAt.

public DataEntry<Bytes, byte[]> getKvDataEntryAt(long index) {
    if (index < 0 || index + 1 > getDataCount()) {
        throw new IllegalArgumentException("Index out of bound!");
    }
    byte[] bytesValue = getValueAt(index);
    Bytes key = new Bytes(bytesValue);
    long latestVersion = getVersion(key);
    if (latestVersion < 0) {
        throw new DataExistException("Expected value version not exist!");
    }
    Bytes dataKey = encodeDataKey(key);
    byte[] value = valueStorage.get(dataKey, latestVersion);
    if (value == null) {
        throw new DataExistException("Expected value does not exist!");
    }
    DataEntry<Bytes, byte[]> entry = new VersioningKVData<Bytes, byte[]>(key, latestVersion, value);
    return entry;
}
Also used : Bytes(utils.Bytes) VersioningKVData(com.jd.blockchain.storage.service.utils.VersioningKVData)

Aggregations

VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)39 HashDigest (com.jd.blockchain.crypto.HashDigest)29 Test (org.junit.Test)28 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)20 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)18 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)18 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)12 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)11 ArrayList (java.util.ArrayList)8 Bytes (utils.Bytes)8 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)6 MerkleTrieData (com.jd.blockchain.ledger.proof.MerkleTrieData)5 SecureRandom (java.security.SecureRandom)5 MerkleProof (com.jd.blockchain.ledger.MerkleProof)3 HashMap (java.util.HashMap)3 Random (java.util.Random)3 MerkleDataNode (com.jd.blockchain.ledger.MerkleDataNode)1 HashSet (java.util.HashSet)1