Search in sources :

Example 36 with MemoryKVStorage

use of com.jd.blockchain.storage.service.utils.MemoryKVStorage 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 37 with MemoryKVStorage

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

the class RolePrivilegeDatasetTest method testAddRolePrivilege.

@Test
public void testAddRolePrivilege() {
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setAutoVerifyHash(true);
    cryptoConfig.setSupportedProviders(SUPPORTED_PROVIDERS);
    cryptoConfig.setHashAlgorithm(HASH_ALGORITHM);
    MemoryKVStorage testStorage = new MemoryKVStorage();
    String roleName = "DEFAULT";
    String prefix = "role-privilege/";
    RolePrivilegeDataset rolePrivilegeDataset = new RolePrivilegeDataset(cryptoConfig, prefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE);
    rolePrivilegeDataset.addRolePrivilege(roleName, new LedgerPermission[] { LedgerPermission.REGISTER_USER }, new TransactionPermission[] { TransactionPermission.CONTRACT_OPERATION });
    rolePrivilegeDataset.commit();
    RolePrivileges rolePrivilege = rolePrivilegeDataset.getRolePrivilege(roleName);
    assertNotNull(rolePrivilege);
    HashDigest rootHash = rolePrivilegeDataset.getRootHash();
    RolePrivilegeDataset newRolePrivilegeDataset = new RolePrivilegeDataset(-1, rootHash, cryptoConfig, prefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
    rolePrivilege = newRolePrivilegeDataset.getRolePrivilege(roleName);
    assertNotNull(rolePrivilege);
    assertTrue(rolePrivilege.getLedgerPrivilege().isEnable(LedgerPermission.REGISTER_USER));
    assertTrue(rolePrivilege.getTransactionPrivilege().isEnable(TransactionPermission.CONTRACT_OPERATION));
}
Also used : RolePrivilegeDataset(com.jd.blockchain.ledger.core.RolePrivilegeDataset) RolePrivileges(com.jd.blockchain.ledger.RolePrivileges) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Example 38 with MemoryKVStorage

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

the class TransactionBatchProcessorTest method testSingleTxProcess.

@Test
public void testSingleTxProcess() {
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    // 验证交易总数;创始区块的交易数预期为 1 ——只有一笔账本初始化的交易;
    long totalCount = ledgerRepo.getTransactionSet().getTotalCount();
    assertEquals(1, totalCount);
    // 验证参与方账户的存在;
    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 userKeypair = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest = LedgerTestUtils.createTxRequest_UserReg(userKeypair, ledgerHash, parti0, parti0);
    TransactionResponse txResp = txbatchProcessor.schedule(transactionRequest);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    // 验证正确性;
    ledgerManager = new LedgerManager();
    ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(newBlock.getHash(), latestBlock.getHash());
    assertEquals(1, newBlock.getHeight());
    // 再次验证交易数;在新增一笔交易之后,交易数预期为 2 ;
    totalCount = ledgerRepo.getTransactionSet().getTotalCount();
    assertEquals(2, totalCount);
    assertEquals(TransactionState.SUCCESS, txResp.getExecutionState());
}
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) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) 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)

Example 39 with MemoryKVStorage

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

the class TransactionBatchProcessorTest method testTxRollbackByVersionsConflict.

@Test
public void testTxRollbackByVersionsConflict() {
    final MemoryKVStorage STORAGE = new MemoryKVStorage();
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
    // 加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
    // 验证参与方账户的存在;
    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 dataAccountKeypair = BlockchainKeyGenerator.getInstance().generate();
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_DataAccountReg_SHA256(dataAccountKeypair, ledgerHash, parti0, parti0);
    TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
    LedgerBlock newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
    DataAccount dataAccount = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress());
    assertNotNull(dataAccount);
    // 正确写入 KV 数据;
    TransactionRequest txreq1 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-1", -1, ledgerHash, parti0, parti0);
    TransactionRequest txreq2 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K2", "V-2-1", -1, ledgerHash, parti0, parti0);
    TransactionRequest txreq3 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-1", -1, ledgerHash, parti0, parti0);
    // 连续写 K1,K1的版本将变为1;
    TransactionRequest txreq4 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-2", 0, ledgerHash, parti0, parti0);
    newBlockEditor = ledgerRepo.createNextBlock();
    previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    txbatchProcessor.schedule(txreq1);
    txbatchProcessor.schedule(txreq2);
    txbatchProcessor.schedule(txreq3);
    txbatchProcessor.schedule(txreq4);
    newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    BytesValue v1_0 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 0);
    BytesValue v1_1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 1);
    BytesValue v2 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K2", 0);
    BytesValue v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3", 0);
    assertNotNull(v1_0);
    assertNotNull(v1_1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertEquals("V-1-1", v1_0.getBytes().toUTF8String());
    assertEquals("V-1-2", v1_1.getBytes().toUTF8String());
    assertEquals("V-2-1", v2.getBytes().toUTF8String());
    assertEquals("V-3-1", v3.getBytes().toUTF8String());
    // 提交多笔数据写入的交易,包含存在数据版本冲突的交易,验证交易是否正确回滚;
    // 先写一笔正确的交易; k3 的版本将变为 1 ;
    TransactionRequest txreq5 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-2", 0, ledgerHash, parti0, parti0);
    // 指定冲突的版本号,正确的应该是版本1;
    TransactionRequest txreq6 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-3", 0, ledgerHash, parti0, parti0);
    newBlockEditor = ledgerRepo.createNextBlock();
    previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
    txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    txbatchProcessor.schedule(txreq5);
    // 预期会产生版本冲突异常; DataVersionConflictionException;
    DataVersionConflictException versionConflictionException = null;
    try {
        txbatchProcessor.schedule(txreq6);
    } catch (DataVersionConflictException e) {
        versionConflictionException = e;
    }
    // assertNotNull(versionConflictionException);
    newBlock = newBlockEditor.prepare();
    newBlockEditor.commit();
    BytesValue v1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1");
    v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3");
    // k1 的版本仍然为1,没有更新;
    long k1_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K1");
    assertEquals(1, k1_version);
    long k3_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K3");
    assertEquals(1, k3_version);
    assertNotNull(v1);
    assertNotNull(v3);
    assertEquals("V-1-2", v1.getBytes().toUTF8String());
    assertEquals("V-3-2", v3.getBytes().toUTF8String());
// // 验证正确性;
// ledgerManager = new LedgerManager();
// ledgerRepo = ledgerManager.register(ledgerHash, STORAGE);
// 
// LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
// assertEquals(newBlock.getHash(), latestBlock.getHash());
// assertEquals(1, newBlock.getHeight());
// 
// LedgerTransaction tx1 = ledgerRepo.getTransactionSet()
// .get(transactionRequest1.getTransactionHash());
// 
// assertNotNull(tx1);
// assertEquals(TransactionState.SUCCESS, tx1.getExecutionState());
}
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) DataVersionConflictException(com.jd.blockchain.ledger.DataVersionConflictException) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) DataAccount(com.jd.blockchain.ledger.core.DataAccount) TransactionResponse(com.jd.blockchain.ledger.TransactionResponse) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) 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)

Example 40 with MemoryKVStorage

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

the class LedgerAdminDatasetTest method testSerialization.

@Test
public void testSerialization() {
    String keyPrefix = "";
    LedgerInitData initSetting = new LedgerInitData();
    ConsensusParticipantData[] parties = new ConsensusParticipantData[5];
    BlockchainKeypair[] bckeys = new BlockchainKeypair[parties.length];
    for (int i = 0; i < parties.length; i++) {
        bckeys[i] = BlockchainKeyGenerator.getInstance().generate();
        parties[i] = new ConsensusParticipantData();
        parties[i].setId(i);
        parties[i].setAddress(AddressEncoding.generateAddress(bckeys[i].getPubKey()));
        parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i));
        parties[i].setName("Participant[" + i + "]");
        parties[i].setPubKey(bckeys[i].getPubKey());
        parties[i].setParticipantState(ParticipantNodeState.CONSENSUS);
    }
    ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4);
    initSetting.setConsensusParticipants(parties1);
    byte[] csSysSettingBytes = new byte[64];
    rand.nextBytes(csSysSettingBytes);
    initSetting.setConsensusSettings(new Bytes(csSysSettingBytes));
    initSetting.setConsensusProvider("consensus-provider");
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoSetting = new CryptoConfig();
    cryptoSetting.setSupportedProviders(supportedProviders);
    cryptoSetting.setAutoVerifyHash(true);
    cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256);
    initSetting.setCryptoSetting(cryptoSetting);
    initSetting.setIdentityMode(IdentityMode.KEYPAIR);
    byte[] ledgerSeed = new byte[16];
    rand.nextBytes(ledgerSeed);
    initSetting.setLedgerSeed(ledgerSeed);
    MemoryKVStorage testStorage = new MemoryKVStorage();
    // Create intance with init setting;
    LedgerAdminDataSetEditor ledgerAdminDataset = new LedgerAdminDataSetEditor(initSetting, keyPrefix, testStorage, testStorage);
    ledgerAdminDataset.getRolePrivileges().addRolePrivilege("DEFAULT", new LedgerPermission[] { LedgerPermission.CONFIGURE_ROLES, LedgerPermission.REGISTER_USER, LedgerPermission.APPROVE_TX }, new TransactionPermission[] { TransactionPermission.DIRECT_OPERATION, TransactionPermission.CONTRACT_OPERATION });
    ledgerAdminDataset.getAuthorizations().addUserRoles(parties[0].getAddress(), RolesPolicy.UNION, "DEFAULT");
    // New created instance is updated until being committed;
    assertTrue(ledgerAdminDataset.isUpdated());
    // Hash of account is null until being committed;
    assertNull(ledgerAdminDataset.getHash());
    LedgerMetadata_V2 meta = ledgerAdminDataset.getMetadata();
    assertNull(meta.getParticipantsHash());
    // Commit, and check the storage keys;
    ledgerAdminDataset.commit();
    // New created instance isn't updated after being committed;
    assertFalse(ledgerAdminDataset.isUpdated());
    // Hash of account isn't null after being committed;
    assertNotNull(ledgerAdminDataset.getHash());
    meta = ledgerAdminDataset.getMetadata();
    assertNotNull(meta.getParticipantsHash());
    assertNotNull(meta.getSettingsHash());
    assertNotNull(meta.getRolePrivilegesHash());
    assertNotNull(meta.getUserRolesHash());
    assertNotNull(ledgerAdminDataset.getRolePrivileges().getRolePrivilege("DEFAULT"));
    // ----------------------
    // Reload account from storage with readonly mode, and check the integrity of
    // data;
    HashDigest adminAccHash = ledgerAdminDataset.getHash();
    LedgerAdminDataSetEditor reloadAdminAccount1 = new LedgerAdminDataSetEditor(-1, adminAccHash, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
    LedgerMetadata_V2 meta2 = reloadAdminAccount1.getMetadata();
    assertNotNull(meta2.getParticipantsHash());
    assertNotNull(meta2.getSettingsHash());
    assertNotNull(meta2.getRolePrivilegesHash());
    assertNotNull(meta2.getUserRolesHash());
    // verify realod settings of admin account;
    verifyRealoadingSettings(reloadAdminAccount1, adminAccHash, ledgerAdminDataset.getMetadata(), ledgerAdminDataset.getSettings());
    // verify the consensus participant list;
    verifyRealoadingParities(reloadAdminAccount1, parties1);
    // It will throw exeception because of this account is readonly;
    verifyReadonlyState(reloadAdminAccount1);
    verifyRealoadingRoleAuthorizations(reloadAdminAccount1, ledgerAdminDataset.getRolePrivileges(), ledgerAdminDataset.getAuthorizations());
    // --------------
    // 重新加载,并进行修改;
    LedgerAdminDataSetEditor reloadAdminAccount2 = new LedgerAdminDataSetEditor(-1, adminAccHash, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, false);
    LedgerConfiguration newSetting = new LedgerConfiguration(reloadAdminAccount2.getPreviousSetting());
    byte[] newCsSettingBytes = new byte[64];
    rand.nextBytes(newCsSettingBytes);
    newSetting.setConsensusSetting(new Bytes(newCsSettingBytes));
    newSetting.getCryptoSetting().setAutoVerifyHash(false);
    reloadAdminAccount2.setLedgerSetting(newSetting);
    reloadAdminAccount2.addParticipant(parties[4]);
    reloadAdminAccount2.getRolePrivileges().addRolePrivilege("ADMIN", new LedgerPermission[] { LedgerPermission.APPROVE_TX }, new TransactionPermission[] { TransactionPermission.DIRECT_OPERATION });
    reloadAdminAccount2.getRolePrivileges().disablePermissions("DEFAULT", TransactionPermission.CONTRACT_OPERATION);
    reloadAdminAccount2.getAuthorizations().addUserRoles(parties[1].getAddress(), RolesPolicy.UNION, "DEFAULT", "ADMIN");
    reloadAdminAccount2.commit();
    LedgerSettings newlyLedgerSettings = reloadAdminAccount2.getSettings();
    // record the new account hash;
    HashDigest newAccHash = reloadAdminAccount2.getHash();
    LedgerMetadata_V2 newMeta = reloadAdminAccount2.getMetadata();
    // load the last version of account and verify again;
    LedgerAdminDataSetEditor previousAdminAccount = new LedgerAdminDataSetEditor(-1, adminAccHash, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
    verifyRealoadingSettings(previousAdminAccount, adminAccHash, ledgerAdminDataset.getMetadata(), ledgerAdminDataset.getSettings());
    verifyRealoadingParities(previousAdminAccount, parties1);
    verifyReadonlyState(previousAdminAccount);
    // load the hash of new committing;
    LedgerAdminDataSetEditor newlyAdminAccount = new LedgerAdminDataSetEditor(-1, newAccHash, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, true);
    verifyRealoadingSettings(newlyAdminAccount, newAccHash, newMeta, newlyLedgerSettings);
    verifyRealoadingParities(newlyAdminAccount, parties);
    verifyReadonlyState(newlyAdminAccount);
// System.out.println("========= [LedgerAdminAccount Test] Show generated storage keys... =======");
// testStorage.printStoragedKeys();
}
Also used : ConsensusParticipantData(com.jd.blockchain.transaction.ConsensusParticipantData) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) LedgerAdminDataSetEditor(com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor) LedgerSettings(com.jd.blockchain.ledger.LedgerSettings) Bytes(utils.Bytes) NetworkAddress(utils.net.NetworkAddress) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerMetadata_V2(com.jd.blockchain.ledger.LedgerMetadata_V2) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) LedgerConfiguration(com.jd.blockchain.ledger.core.LedgerConfiguration) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Aggregations

MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)61 Test (org.junit.Test)50 HashDigest (com.jd.blockchain.crypto.HashDigest)39 CryptoSetting (com.jd.blockchain.ledger.CryptoSetting)23 TreeOptions (com.jd.blockchain.ledger.merkletree.TreeOptions)21 VersioningKVData (com.jd.blockchain.storage.service.utils.VersioningKVData)18 KVEntry (com.jd.blockchain.ledger.merkletree.KVEntry)17 MerkleHashTrie (com.jd.blockchain.ledger.proof.MerkleHashTrie)15 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)14 Bytes (utils.Bytes)13 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)9 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)9 Random (java.util.Random)9 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)8 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)8 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)8 ArrayList (java.util.ArrayList)8 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)7 MerkleHashSortTree (com.jd.blockchain.ledger.merkletree.MerkleHashSortTree)7 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)6