Search in sources :

Example 6 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.

the class MerkleSequenceTreeTest method testDataVersionModify.

/**
 * 测试单独修改版本而不变更数据时,是否能够正确地更新 merkle 树;;
 */
@Test
public void testDataVersionModify() {
    CryptoSetting setting = Mockito.mock(CryptoSetting.class);
    when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256.code());
    when(setting.getAutoVerifyHash()).thenReturn(true);
    // 保存所有写入的数据节点的 SN-Hash 映射表;
    TreeMap<Long, HashDigest> dataNodes = new TreeMap<>();
    MerkleNode nd;
    // 测试从空的树开始,顺序增加数据节点;
    ExistancePolicyKVStorageMap storage = new ExistancePolicyKVStorageMap();
    // 创建空的的树;
    MerkleSequenceTree mkt = new MerkleSequenceTree(setting, keyPrefix, storage);
    long sn = 0;
    // 加入 4097 条数据记录,预期构成以一颗 4 层 16 叉树;
    int count = 4097;
    byte[] dataBuf = new byte[16];
    Random rand = new Random();
    for (int i = 0; i < count; i++) {
        rand.nextBytes(dataBuf);
        nd = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        dataNodes.put(sn, nd.getNodeHash());
        sn++;
    }
    mkt.commit();
    byte[] dataOfMaxSnNode = Arrays.copyOf(dataBuf, dataBuf.length);
    // 检查节点数;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    // count-1;
    long expectedMaxSN = 4096;
    long maxSN = mkt.getMaxSn();
    assertEquals(expectedMaxSN, maxSN);
    // 预期扩展到 4 层;
    assertEquals(4, mkt.getLevel());
    // 路径节点 + 数据节点;
    // 预期扩展为 4 层16叉树,由 3 层满16叉树扩展 1 新分支(4个路径节点)而形成;
    long expectedNodes = getMaxPathNodeCount(3) + 4 + 4097;
    assertEquals(expectedNodes, storage.getCount());
    // 仅仅更新最大的 sn 的数据节点的版本(由 0 升级为 1),预期将产生 4 个更新的路径节点和 1 个新的数据节点;
    long currNodes = expectedNodes;
    mkt.setData(maxSN, "KEY-" + maxSN, 1, dataOfMaxSnNode);
    mkt.commit();
    // 验证;
    maxSN = mkt.getMaxSn();
    assertEquals(expectedMaxSN, maxSN);
    // 预期扩展到 4 层;
    assertEquals(4, mkt.getLevel());
    // 路径节点 + 数据节点;
    // 预期将产生 4 个更新的路径节点和 1 个新的数据节点;
    expectedNodes = currNodes + 4 + 1;
    assertEquals(expectedNodes, storage.getCount());
}
Also used : CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) Random(java.util.Random) ExistancePolicyKVStorageMap(com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap) MerkleNode(com.jd.blockchain.ledger.MerkleNode) MerkleSequenceTree(com.jd.blockchain.ledger.proof.MerkleSequenceTree) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 7 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.

the class MerkleSequenceTreeTest method testDataModify.

/**
 * 测试以单次提交的方式顺序地插入数据;
 */
@Test
public void testDataModify() {
    CryptoSetting setting = Mockito.mock(CryptoSetting.class);
    when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256.code());
    when(setting.getAutoVerifyHash()).thenReturn(true);
    // 保存所有写入的数据节点的 SN-Hash 映射表;
    TreeMap<Long, HashDigest> dataNodes = new TreeMap<>();
    MerkleNode nd;
    // 测试从空的树开始,顺序增加数据节点;
    ExistancePolicyKVStorageMap storage = new ExistancePolicyKVStorageMap();
    // 创建空的的树;
    MerkleSequenceTree mkt = new MerkleSequenceTree(setting, keyPrefix, storage);
    long sn = 0;
    // 加入 4097 条数据记录,预期构成以一颗 4 层 16 叉树;
    int count = 4097;
    byte[] dataBuf = new byte[16];
    Random rand = new Random();
    for (int i = 0; i < count; i++) {
        rand.nextBytes(dataBuf);
        nd = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        dataNodes.put(sn, nd.getNodeHash());
        sn++;
    }
    mkt.commit();
    // 检查节点数;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    // count-1;
    long expectedMaxSN = 4096;
    long maxSN = mkt.getMaxSn();
    assertEquals(expectedMaxSN, maxSN);
    // 预期扩展到 4 层;
    assertEquals(4, mkt.getLevel());
    // 路径节点 + 数据节点;
    // 预期扩展为 4 层16叉树,由 3 层满16叉树扩展 1 新分支(4个路径节点)而形成;
    long expectedNodes = getMaxPathNodeCount(3) + 4 + 4097;
    assertEquals(expectedNodes, storage.getCount());
    // 覆盖到每一路分支修改数据节点;
    int storageDataCountBefore = storage.getCount();
    // maxSn = 4096;
    int i;
    for (i = 0; i <= maxSN; i += 16) {
        rand.nextBytes(dataBuf);
        sn = i;
        nd = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        dataNodes.put(sn, nd.getNodeHash());
    }
    mkt.commit();
    // 检查节点数;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    maxSN = mkt.getMaxSn();
    assertEquals(expectedMaxSN, maxSN);
    // 由于覆盖到每一个分支节点,全部分支节点都重新生成,因此:
    // 新增节点数=修改的数据节点数 + 全部分支节点数;
    long addCounts = i / 16 + getMaxPathNodeCount(3) + 4;
    assertEquals(storageDataCountBefore + addCounts, storage.getCount());
// TODO: 暂时注释掉默克尔证明相关的内容;
// 验证每一个数据节点都产生了存在性证明;
// MerkleProof proof = null;
// for (Long n : dataNodes.keySet()) {
// proof = mkt.getProof(n.longValue());
// assertNotNull(proof);
// HashDigest expHash = dataNodes.get(n);
// assertEquals(expHash.toBase58(), proof.getDataHash().toBase58());
// }
}
Also used : CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) Random(java.util.Random) ExistancePolicyKVStorageMap(com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap) MerkleNode(com.jd.blockchain.ledger.MerkleNode) MerkleSequenceTree(com.jd.blockchain.ledger.proof.MerkleSequenceTree) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 8 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.

the class MerkleSequenceTreeTest method testSequenceInsert_MultiCommit.

/**
 * 测试以多次提交的方式顺序地插入数据;
 */
@Test
public void testSequenceInsert_MultiCommit() {
    CryptoSetting setting = Mockito.mock(CryptoSetting.class);
    when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256.code());
    when(setting.getAutoVerifyHash()).thenReturn(true);
    // 测试从空的树开始,顺序增加数据节点;
    ExistancePolicyKVStorageMap kvs1 = new ExistancePolicyKVStorageMap();
    MerkleSequenceTree mkt = new MerkleSequenceTree(setting, keyPrefix, kvs1);
    long sn = 0;
    // 初始化,加入10条记录,预期目前的树只有一层;
    int count = 10;
    byte[] dataBuf = new byte[16];
    Random rand = new Random();
    for (int i = 0; i < count; i++) {
        rand.nextBytes(dataBuf);
        mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        sn++;
    }
    mkt.commit();
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    long maxSN = mkt.getMaxSn();
    assertEquals(sn - 1, maxSN);
    assertEquals(1, mkt.getLevel());
    // 路径节点 + 数据节点;1 层只有一个路径节点;
    long expectedNodes = 1 + count;
    assertEquals(11, expectedNodes);
    assertEquals(expectedNodes, kvs1.getCount());
    // 再加入 6 条记录;总共 16 条,预期目前的树也只有一层;
    for (int i = 0; i < 6; i++) {
        rand.nextBytes(dataBuf);
        mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        sn++;
    }
    mkt.commit();
    count = 16;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    maxSN = mkt.getMaxSn();
    assertEquals(sn - 1, maxSN);
    // 预期只有 1 层;
    assertEquals(1, mkt.getLevel());
    // 路径节点 + 数据节点;1 层只有一个路径节点;新增了1个路径节点,以及新增 6 个数据节点;
    // 注:上一次未填充满的路径节点由于哈希变化而重新生成,因此多出 1 条;
    expectedNodes = expectedNodes + 1 + 6;
    assertEquals(expectedNodes, kvs1.getCount());
    // 再加入 10 条记录;总共 26 条数据,预期目前的树有2层[17-32];
    for (int i = 0; i < 10; i++) {
        rand.nextBytes(dataBuf);
        mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        sn++;
    }
    mkt.commit();
    count = 26;
    assertEquals(count, mkt.getDataCount());
    // 预期扩展到 2 层;
    assertEquals(2, mkt.getLevel());
    // 路径节点 + 数据节点;扩展到 2 层, 新增加了2个路径节点(包括:新的根节点和其中1个子节点),以及10个新增的数据节点;
    expectedNodes = expectedNodes + 2 + 10;
    assertEquals(expectedNodes, kvs1.getCount());
    // 再加入 230 条记录,总共 256 条数据记录,预期构成以一颗满的 2 层 16 叉树;
    for (int i = 0; i < 230; i++) {
        rand.nextBytes(dataBuf);
        mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        sn++;
    }
    mkt.commit();
    count = 256;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    maxSN = mkt.getMaxSn();
    assertEquals(sn - 1, maxSN);
    // 预期仍然维持 2 层;
    assertEquals(2, mkt.getLevel());
    // 路径节点 + 数据节点;
    // 由于原来的两个未满的路径节点重新计算 hash 而新增加了2个路径节点,新增14个 Level(1) 的路径节点,加上230个新增数据记录;
    expectedNodes = expectedNodes + 2 + 14 + 230;
    assertEquals(expectedNodes, kvs1.getCount());
    // 再加入 3840 条记录,总共 4096 条数据记录,预期构成以一颗满的 3 层 16 叉树;
    for (int i = 0; i < 3840; i++) {
        rand.nextBytes(dataBuf);
        mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        sn++;
    }
    mkt.commit();
    count = 4096;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    maxSN = mkt.getMaxSn();
    assertEquals(sn - 1, maxSN);
    // 预期扩展到 3 层;
    assertEquals(3, mkt.getLevel());
    // 路径节点 + 数据节点;
    // 预期扩展为 3 层的满16叉树,新增256的路径节点,加上3840个新增数据记录;
    expectedNodes = expectedNodes + 256 + 3840;
    assertEquals(expectedNodes, kvs1.getCount());
    // 再加入 1 条记录,总共 4097 条数据记录,预期构成以一颗 4 层 16 叉树;
    for (int i = 0; i < 1; i++) {
        rand.nextBytes(dataBuf);
        mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        sn++;
    }
    mkt.commit();
    count = 4097;
    assertEquals(count, mkt.getDataCount());
    // 检查最大序列号的正确性;
    maxSN = mkt.getMaxSn();
    assertEquals(sn - 1, maxSN);
    // 预期扩展到 4 层;
    assertEquals(4, mkt.getLevel());
    // 路径节点 + 数据节点;
    // 预期扩展为 4 层16叉树,新增4个路径节点,加上1个新增数据记录;
    expectedNodes = expectedNodes + 4 + 1;
    assertEquals(expectedNodes, kvs1.getCount());
// TODO: 暂时注释掉默克尔证明相关的内容;
// 验证每一个数据节点都产生了存在性证明;
// for (int i = 0; i <= maxSN; i++) {
// proof = mkt.getProof(i);
// assertNotNull(proof);
// }
}
Also used : CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) Random(java.util.Random) ExistancePolicyKVStorageMap(com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap) MerkleSequenceTree(com.jd.blockchain.ledger.proof.MerkleSequenceTree) Test(org.junit.Test)

Example 9 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.

the class MerkleSequenceTreeTest method testMerkleReload.

/**
 * 测试从存储重新加载 Merkle 树的正确性;
 */
/**
 */
// TODO: 暂时注释掉默克尔证明相关的内容;
// @Test
public void testMerkleReload() {
    CryptoSetting setting = Mockito.mock(CryptoSetting.class);
    when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256.code());
    when(setting.getAutoVerifyHash()).thenReturn(true);
    // 保存所有写入的数据节点的 SN-Hash 映射表;
    TreeMap<Long, HashDigest> expectedDataNodes = new TreeMap<>();
    MerkleNode nd;
    // 测试从空的树开始,顺序增加数据节点;
    ExistancePolicyKVStorageMap storage = new ExistancePolicyKVStorageMap();
    // 创建空的的树;
    MerkleSequenceTree mkt = new MerkleSequenceTree(setting, keyPrefix, storage);
    long sn = 0;
    // 加入 4097 条数据记录,预期构成以一颗 4 层 16 叉树;
    int count = 4097;
    byte[] dataBuf = new byte[16];
    Random rand = new Random();
    for (int i = 0; i < count; i++) {
        rand.nextBytes(dataBuf);
        nd = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        expectedDataNodes.put(sn, nd.getNodeHash());
        sn++;
    }
    mkt.commit();
    // 记录一次提交的根哈希以及部分节点信息,用于后续的加载校验;
    HashDigest r1_rootHash = mkt.getRootHash();
    long r1_dataCount = mkt.getDataCount();
    long r1_maxSN = mkt.getMaxSn();
    long r1_sn1 = r1_maxSN;
    String r1_proof1 = mkt.getProof(r1_sn1).toString();
    long r1_sn2 = 1024;
    String r1_proof2 = mkt.getProof(r1_sn2).toString();
    {
        // 检查节点数;
        assertEquals(count, mkt.getDataCount());
        // 检查最大序列号的正确性;
        long maxSN = mkt.getMaxSn();
        // count-1;
        long expectedMaxSN = 4096;
        assertEquals(expectedMaxSN, maxSN);
        // 预期扩展到 4 层;
        assertEquals(4, mkt.getLevel());
        // 路径节点 + 数据节点;
        // 预期扩展为 4 层16叉树,由 3 层满16叉树扩展 1 新分支(4个路径节点)而形成;
        long expectedNodes = getMaxPathNodeCount(3) + 4 + 4097;
        assertEquals(expectedNodes, storage.getCount());
        // 重新加载,判断数据是否正确;
        MerkleSequenceTree r1_mkt = new MerkleSequenceTree(r1_rootHash, setting, keyPrefix, storage, true);
        {
            // 验证每一个数据节点都产生了存在性证明;
            MerkleProof proof = null;
            HashDigest expectedNodeHash = null;
            MerkleDataNode reallyDataNode = null;
            for (long n = 0; n < maxSN; n++) {
                expectedNodeHash = expectedDataNodes.get(n);
                reallyDataNode = r1_mkt.getData(n);
                assertEquals(expectedNodeHash, reallyDataNode.getNodeHash());
                proof = r1_mkt.getProof(n);
                assertNotNull(proof);
                assertEquals(expectedNodeHash, proof.getDataHash());
            }
        }
    }
    // 覆盖到每一路分支修改数据节点;
    int storageDataCountBefore = storage.getCount();
    // maxSn = 4096;
    long maxSN = mkt.getMaxSn();
    int i;
    for (i = 0; i <= maxSN; i += 16) {
        rand.nextBytes(dataBuf);
        sn = i;
        nd = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        expectedDataNodes.put(sn, nd.getNodeHash());
    }
    mkt.commit();
    // 记录一次提交的根哈希以及部分节点信息,用于后续的加载校验;
    HashDigest r2_rootHash = mkt.getRootHash();
    long r2_dataCount = mkt.getDataCount();
    long r2_maxSN = mkt.getMaxSn();
    long r2_sn1 = r1_sn1;
    String r2_proof1 = mkt.getProof(r2_sn1).toString();
    long r2_sn2 = r1_sn2;
    String r2_proof2 = mkt.getProof(r2_sn2).toString();
    {
        // 检查节点数;
        assertEquals(count, mkt.getDataCount());
        assertEquals(r1_dataCount, r2_dataCount);
        // 检查最大序列号的正确性;
        maxSN = mkt.getMaxSn();
        // count-1;
        long expectedMaxSN = 4096;
        assertEquals(expectedMaxSN, maxSN);
        // 由于覆盖到每一个分支节点,全部分支节点都重新生成,因此:
        // 新增节点数=修改的数据节点数 + 全部分支节点数;
        long addCounts = i / 16 + getMaxPathNodeCount(3) + 4;
        assertEquals(storageDataCountBefore + addCounts, storage.getCount());
    }
    // 新插入数据;
    final int NEW_INSERTED_COUNT = 18;
    for (i = 0; i < NEW_INSERTED_COUNT; i++) {
        rand.nextBytes(dataBuf);
        sn = maxSN + 1 + i;
        nd = mkt.setData(sn, "KEY-" + sn, 0, dataBuf);
        expectedDataNodes.put(sn, nd.getNodeHash());
    }
    mkt.commit();
    {
        // 验证每一个数据节点都产生了存在性证明;
        MerkleProof proof = null;
        for (Long n : expectedDataNodes.keySet()) {
            proof = mkt.getProof(n.longValue());
            assertNotNull(proof);
            assertEquals(expectedDataNodes.get(n), proof.getDataHash());
        }
    }
    // 记录一次提交的根哈希以及部分节点信息,用于后续的加载校验;
    HashDigest r3_rootHash = mkt.getRootHash();
    long r3_maxSN = mkt.getMaxSn();
    long r3_sn1 = r2_sn1;
    String r3_proof1 = mkt.getProof(r3_sn1).toString();
    long r3_sn2 = r2_sn2;
    String r3_proof2 = mkt.getProof(r3_sn2).toString();
    long r3_sn3 = 4096 + NEW_INSERTED_COUNT - 5;
    String r3_proof3 = mkt.getProof(r3_sn3).toString();
    {
        // 检查节点数;
        assertEquals(count + NEW_INSERTED_COUNT, mkt.getDataCount());
        // 检查最大序列号的正确性;
        maxSN = mkt.getMaxSn();
        // count-1;
        long expectedMaxSN = 4096 + NEW_INSERTED_COUNT;
        assertEquals(expectedMaxSN, maxSN);
    }
    // --------------------
    // 重新从存储加载生成新的 MerkleTree 实例,验证与初始实例的一致性;
    // 从第 2 轮提交的 Merkle 根哈希加载;
    MerkleSequenceTree r1_mkt = new MerkleSequenceTree(r1_rootHash, setting, keyPrefix, storage, true);
    assertEquals(r1_maxSN, r1_mkt.getMaxSn());
    assertEquals(r1_rootHash, r1_mkt.getRootHash());
    assertEquals(r1_dataCount, r1_mkt.getDataCount());
    assertEquals(r1_proof1, r1_mkt.getProof(r1_sn1).toString());
    assertEquals(r1_proof2, r1_mkt.getProof(r1_sn2).toString());
    // 从第 2 轮提交的 Merkle 根哈希加载;
    // 第 2 轮生成的 Merkle 树是对第 1 轮的数据的全部节点的修改,因此同一个 SN 的节点的证明是不同的;
    MerkleSequenceTree r2_mkt = new MerkleSequenceTree(r2_rootHash, setting, keyPrefix, storage, true);
    assertEquals(r1_maxSN, r2_mkt.getMaxSn());
    assertEquals(r1_dataCount, r2_mkt.getDataCount());
    assertNotEquals(r1_rootHash, r2_mkt.getRootHash());
    assertNotEquals(r1_proof1, r2_mkt.getProof(r1_sn1).toString());
    assertNotEquals(r1_proof2, r2_mkt.getProof(r1_sn2).toString());
    assertEquals(r2_maxSN, r2_mkt.getMaxSn());
    assertEquals(r2_rootHash, r2_mkt.getRootHash());
    assertEquals(r2_dataCount, r2_mkt.getDataCount());
    assertEquals(r2_proof1, r2_mkt.getProof(r2_sn1).toString());
    assertEquals(r2_proof2, r2_mkt.getProof(r2_sn2).toString());
    // 从第 3 轮提交的 Merkle 根哈希加载;
    // 第 3 轮生成的 Merkle 树是在第 2 轮的数据基础上做新增,因此非新增的同一个 SN 的节点的Merkle证明是相同的;
    MerkleSequenceTree r3_mkt = new MerkleSequenceTree(r3_rootHash, setting, keyPrefix, storage, true);
    assertEquals(r2_maxSN + NEW_INSERTED_COUNT, r3_mkt.getMaxSn());
    assertNotEquals(r2_rootHash, r3_mkt.getRootHash());
    assertEquals(r2_dataCount + NEW_INSERTED_COUNT, r3_mkt.getDataCount());
    assertEquals(r3_maxSN, r3_mkt.getMaxSn());
    assertEquals(r3_rootHash, r3_mkt.getRootHash());
    assertEquals(r3_proof1, r3_mkt.getProof(r3_sn1).toString());
    assertEquals(r3_proof2, r3_mkt.getProof(r3_sn2).toString());
    assertEquals(r3_proof3, r3_mkt.getProof(r3_sn3).toString());
    // 验证每一个数据节点都产生了存在性证明;
    {
        MerkleProof proof = null;
        for (Long n : expectedDataNodes.keySet()) {
            proof = r3_mkt.getProof(n.longValue());
            assertNotNull(proof);
            assertEquals(expectedDataNodes.get(n), proof.getDataHash());
        }
    }
}
Also used : MerkleSequenceTree(com.jd.blockchain.ledger.proof.MerkleSequenceTree) MerkleProof(com.jd.blockchain.ledger.MerkleProof) TreeMap(java.util.TreeMap) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) Random(java.util.Random) ExistancePolicyKVStorageMap(com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap) MerkleDataNode(com.jd.blockchain.ledger.MerkleDataNode) MerkleNode(com.jd.blockchain.ledger.MerkleNode)

Example 10 with CryptoSetting

use of com.jd.blockchain.ledger.CryptoSetting in project jdchain-core by blockchain-jd-com.

the class TransactionBatchProcessorTest method testTxRollback.

@Test
public void testTxRollback() {
    System.out.println("------------ keys -----------");
    System.out.printf("Parti0 -- PUB:[%s]; PRIV:[%s]\r\n", parti0.getPubKey().toBase58(), parti0.getPrivKey().toBase58());
    System.out.printf("Parti1 -- PUB:[%s]; PRIV:[%s]\r\n", parti1.getPubKey().toBase58(), parti1.getPrivKey().toBase58());
    System.out.printf("Parti2 -- PUB:[%s]; PRIV:[%s]\r\n", parti2.getPubKey().toBase58(), parti2.getPrivKey().toBase58());
    System.out.printf("Parti3 -- PUB:[%s]; PRIV:[%s]\r\n", parti3.getPubKey().toBase58(), parti3.getPrivKey().toBase58());
    System.out.println("------------ end-keys -----------");
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    System.out.printf("\r\n------------ LEDGER [%s] -----------\r\n", ledgerHash.toBase58());
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    CryptoSetting cryptoSetting = ledgerRepo.getAdminSettings().getSettings().getCryptoSetting();
    // 验证参与方账户的存在;
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
    assertNotNull(user0);
    boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
    assertTrue(partiRegistered);
    // 生成新区块;
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 注册新用户;
    // BlockchainKeypair userKeypair1 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair1, ledgerHash, 1580315317127L, parti0, parti0);
    // 错误参数:ts=1580315317127; txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
    System.out.printf("\r\n ===||=== transactionRequest1.getTransactionHash()=[%s]\r\n", transactionRequest1.getTransactionHash().toBase58());
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    // BlockchainKeypair userKeypair2 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair2 = LedgerTestUtils.createKeyPair("7VeRKSnDFveTfLLMsLZDmmhGmgf7i142XHgBFjnrKuS95tY3", "7VeRTiJ2TpQD9aBi29ajnqdntgoVBANmC3oCbHThKb5tzfTJ");
    TransactionRequest transactionRequest2 = LedgerTestUtils.createTxRequest_MultiOPs_WithNotExistedDataAccount_SHA256(userKeypair2, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest2.getTransactionHash()=[%s]\r\n", transactionRequest2.getTransactionHash().toBase58());
    TransactionResponse txResp2 = txbatchProcessor.schedule(transactionRequest2);
    // BlockchainKeypair userKeypair3 = BlockchainKeyGenerator.getInstance().generate();
    BlockchainKeypair userKeypair3 = LedgerTestUtils.createKeyPair("7VeRDoaSexqLWKkaZyrQwdwSuE9n5nszduMrYBfYRfEkREQV", "7VeRdFtTuLfrzCYJzQ6enQUkGTc83ATgjr8WbmfjBQuTFpHt");
    TransactionRequest transactionRequest3 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair3, ledgerHash, 202001202020L, parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest3.getTransactionHash()=[%s]\r\n", transactionRequest3.getTransactionHash().toBase58());
    TransactionResponse txResp3 = txbatchProcessor.schedule(transactionRequest3);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    // 在重新加载之前验证一次; 
    long blockHeight = newBlock.getHeight();
    assertEquals(1, blockHeight);
    HashDigest blockHash = newBlock.getHash();
    assertNotNull(blockHash);
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, txResp2.getExecutionState());
    assertEquals(TransactionState.SUCCESS, txResp3.getExecutionState());
    LedgerTransaction tx1 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest1.getTransactionHash());
    LedgerTransaction tx2 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest2.getTransactionHash());
    LedgerTransaction tx3 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx3);
    assertEquals(TransactionState.SUCCESS, tx3.getResult().getExecutionState());
    assertNotNull(tx2);
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, tx2.getResult().getExecutionState());
    assertNotNull(tx1);
    assertEquals(TransactionState.SUCCESS, tx1.getResult().getExecutionState());
    HashDigest txsetRootHash = ledgerRepo.getTransactionSet().getRootHash();
    // 单独加载交易集合;
    TransactionSetEditor txset = new TransactionSetEditor(-1, txsetRootHash, cryptoSetting, "LDG://3A3dP4", STORAGE, STORAGE, LedgerDataStructure.MERKLE_TREE, false);
    tx1 = txset.getTransaction(transactionRequest1.getTransactionHash());
    // tx2 = txset.get(transactionRequest2.getTransactionHash());
    tx3 = txset.getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx3);
    // assertNotNull(tx2);
    assertNotNull(tx1);
    // 重新加载之后验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(blockHash, latestBlock.getHash());
    assertEquals(blockHeight, latestBlock.getHeight());
    assertEquals(txsetRootHash, ledgerRepo.getTransactionSet().getRootHash());
    tx1 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest1.getTransactionHash());
    tx2 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest2.getTransactionHash());
    tx3 = ledgerRepo.getTransactionSet().getTransaction(transactionRequest3.getTransactionHash());
    assertNotNull(tx1);
    assertEquals(TransactionState.SUCCESS, tx1.getResult().getExecutionState());
    assertNotNull(tx2);
    assertEquals(TransactionState.DATA_ACCOUNT_DOES_NOT_EXIST, tx2.getResult().getExecutionState());
    assertNotNull(tx3);
    assertEquals(TransactionState.SUCCESS, tx3.getResult().getExecutionState());
    LedgerDataSet ledgerDS = ledgerRepo.getLedgerDataSet(latestBlock);
    boolean existUser1 = ledgerDS.getUserAccountSet().contains(userKeypair1.getAddress());
    boolean existUser2 = ledgerDS.getUserAccountSet().contains(userKeypair2.getAddress());
    boolean existUser3 = ledgerDS.getUserAccountSet().contains(userKeypair3.getAddress());
    assertTrue(existUser1);
    assertFalse(existUser2);
    assertTrue(existUser3);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) UserAccount(com.jd.blockchain.ledger.core.UserAccount) OperationHandleRegisteration(com.jd.blockchain.ledger.core.OperationHandleRegisteration) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) Test(org.junit.Test)

Aggregations

CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)35 Test (org.junit.Test)26 HashDigest (com.jd.blockchain.crypto.HashDigest)23 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)23 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)15 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)12 Random (java.util.Random)11 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)10 ArrayList (java.util.ArrayList)8 MerkleSequenceTree (com.jd.blockchain.ledger.proof.MerkleSequenceTree)7 ExistancePolicyKVStorageMap (com.jd.blockchain.storage.service.utils.ExistancePolicyKVStorageMap)7 MerkleNode (com.jd.blockchain.ledger.MerkleNode)4 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)4 TransactionSetEditor (com.jd.blockchain.ledger.core.TransactionSetEditor)4 MerkleTrieData (com.jd.blockchain.ledger.proof.MerkleTrieData)4 TreeMap (java.util.TreeMap)4 Bytes (utils.Bytes)4 TransactionResult (com.jd.blockchain.ledger.TransactionResult)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)2