Search in sources :

Example 1 with MemoryKVStorage

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

the class LedgerSecurityManagerTest method testGetSecurityPolicy.

@Test
public void testGetSecurityPolicy() {
    MemoryKVStorage testStorage = new MemoryKVStorage();
    // 定义不同角色用户的 keypair;
    final BlockchainKeypair kpManager = BlockchainKeyGenerator.getInstance().generate();
    final BlockchainKeypair kpEmployee = BlockchainKeyGenerator.getInstance().generate();
    final BlockchainKeypair kpDevoice = BlockchainKeyGenerator.getInstance().generate();
    final BlockchainKeypair kpPlatform = BlockchainKeyGenerator.getInstance().generate();
    // 定义角色和权限;
    final String ROLE_ADMIN = "ID_ADMIN";
    final String ROLE_OPERATOR = "OPERATOR";
    final String ROLE_DATA_COLLECTOR = "DATA_COLLECTOR";
    final String ROLE_PLATFORM = "PLATFORM";
    // 定义管理员角色的权限:【账本权限只允许:注册用户、注册数据账户】【交易权限只允许:调用账本直接操作】
    final Privileges PRIVILEGES_ADMIN = Privileges.configure().enable(LedgerPermission.REGISTER_USER, LedgerPermission.REGISTER_DATA_ACCOUNT).enable(TransactionPermission.DIRECT_OPERATION);
    // 定义操作员角色的权限:【账本权限只允许:写入数据账户】【交易权限只允许:调用合约】
    final Privileges PRIVILEGES_OPERATOR = Privileges.configure().enable(LedgerPermission.WRITE_DATA_ACCOUNT).enable(TransactionPermission.CONTRACT_OPERATION);
    // 定义数据收集器角色的权限:【账本权限只允许:写入数据账户】【交易权限只允许:调用账本直接操作】
    final Privileges PRIVILEGES_DATA_COLLECTOR = Privileges.configure().enable(LedgerPermission.WRITE_DATA_ACCOUNT).enable(TransactionPermission.DIRECT_OPERATION);
    // 定义平台角色的权限:【账本权限只允许:签署合约】 (只允许作为节点签署交易,不允许作为终端发起交易指令)
    final Privileges PRIVILEGES_PLATFORM = Privileges.configure().enable(LedgerPermission.APPROVE_TX);
    RolePrivilegeDataset rolePrivilegeDataset = createRolePrivilegeDataset(testStorage);
    long v = rolePrivilegeDataset.addRolePrivilege(ROLE_ADMIN, PRIVILEGES_ADMIN);
    assertTrue(v > -1);
    v = rolePrivilegeDataset.addRolePrivilege(ROLE_OPERATOR, PRIVILEGES_OPERATOR);
    assertTrue(v > -1);
    v = rolePrivilegeDataset.addRolePrivilege(ROLE_DATA_COLLECTOR, PRIVILEGES_DATA_COLLECTOR);
    assertTrue(v > -1);
    v = rolePrivilegeDataset.addRolePrivilege(ROLE_PLATFORM, PRIVILEGES_PLATFORM);
    assertTrue(v > -1);
    rolePrivilegeDataset.commit();
    // 为用户分配角色;
    String[] managerRoles = new String[] { ROLE_ADMIN, ROLE_OPERATOR };
    String[] employeeRoles = new String[] { ROLE_OPERATOR };
    String[] devoiceRoles = new String[] { ROLE_DATA_COLLECTOR };
    String[] platformRoles = new String[] { ROLE_PLATFORM };
    UserRoleDatasetEditor userRolesDataset = createUserRoleDataset(testStorage);
    userRolesDataset.addUserRoles(kpManager.getAddress(), RolesPolicy.UNION, managerRoles);
    userRolesDataset.addUserRoles(kpEmployee.getAddress(), RolesPolicy.UNION, employeeRoles);
    userRolesDataset.addUserRoles(kpDevoice.getAddress(), RolesPolicy.UNION, devoiceRoles);
    userRolesDataset.addUserRoles(kpPlatform.getAddress(), RolesPolicy.UNION, platformRoles);
    userRolesDataset.commit();
    ParticipantCollection partisQuery = Mockito.mock(ParticipantCollection.class);
    UserAccountSet usersQuery = Mockito.mock(UserAccountSet.class);
    // 创建安全管理器;
    LedgerSecurityManager securityManager = new LedgerSecurityManagerImpl(rolePrivilegeDataset, userRolesDataset, partisQuery, usersQuery);
    // 定义终端用户列表;终端用户一起共同具有 ADMIN、OPERATOR 角色;
    final Map<Bytes, BlockchainKeypair> endpoints = new HashMap<>();
    endpoints.put(kpManager.getAddress(), kpManager);
    endpoints.put(kpEmployee.getAddress(), kpEmployee);
    // 定义节点参与方列表;
    final Map<Bytes, BlockchainKeypair> nodes = new HashMap<>();
    nodes.put(kpPlatform.getAddress(), kpPlatform);
    // 创建一项与指定的终端用户和节点参与方相关的安全策略;
    SecurityPolicy policy = securityManager.getSecurityPolicy(endpoints.keySet(), nodes.keySet());
    // 校验安全策略的正确性;
    LedgerPermission[] ledgerPermissions = LedgerPermission.values();
    for (LedgerPermission p : ledgerPermissions) {
        // 终端节点有 ADMIN 和 OPERATOR 两种角色的合并权限;
        if (p == LedgerPermission.REGISTER_USER || p == LedgerPermission.REGISTER_DATA_ACCOUNT || p == LedgerPermission.WRITE_DATA_ACCOUNT) {
            assertTrue(policy.isEndpointEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
        } else {
            assertFalse(policy.isEndpointEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
        }
        if (p == LedgerPermission.APPROVE_TX) {
            // 共识参与方只有 PLATFORM 角色的权限:核准交易;
            assertTrue(policy.isNodeEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
        } else {
            assertFalse(policy.isNodeEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
        }
    }
    TransactionPermission[] transactionPermissions = TransactionPermission.values();
    for (TransactionPermission p : transactionPermissions) {
        // 终端节点有 ADMIN 和 OPERATOR 两种角色的合并权限;
        if (p == TransactionPermission.DIRECT_OPERATION || p == TransactionPermission.CONTRACT_OPERATION) {
            assertTrue(policy.isEndpointEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
        } else {
            assertFalse(policy.isEndpointEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
        }
        assertFalse(policy.isNodeEnable(p, MultiIDsPolicy.AT_LEAST_ONE));
    }
}
Also used : RolePrivilegeDataset(com.jd.blockchain.ledger.core.RolePrivilegeDataset) LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) TransactionPermission(com.jd.blockchain.ledger.TransactionPermission) HashMap(java.util.HashMap) ParticipantCollection(com.jd.blockchain.ledger.core.ParticipantCollection) LedgerPermission(com.jd.blockchain.ledger.LedgerPermission) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) Bytes(utils.Bytes) UserRoleDatasetEditor(com.jd.blockchain.ledger.core.UserRoleDatasetEditor) LedgerSecurityManagerImpl(com.jd.blockchain.ledger.core.LedgerSecurityManagerImpl) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Privileges(com.jd.blockchain.ledger.Privileges) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) UserAccountSet(com.jd.blockchain.ledger.core.UserAccountSet) Test(org.junit.Test)

Example 2 with MemoryKVStorage

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

the class LedgerEditorTest method testMerkleDataSet1.

@Test
public void testMerkleDataSet1() {
    CryptoSetting setting = LedgerTestUtils.createDefaultCryptoSetting();
    Bytes keyPrefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX).concat(MerkleHashDataset.MERKLE_TREE_PREFIX);
    MemoryKVStorage storage = new MemoryKVStorage();
    byte[] key = Base58Utils.decode("j5q7n8ShYqKVitaobZrERtBK7GowGGZ54RuaUeWjLsdPYY");
    HashDigest valueHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5o6mMnMQqE5fJKJ93FzXPnu4vFCfpBKp7u4r8tUUaFRK8"));
    long version = 0;
    MerkleHashTrie merkleTree = new MerkleHashTrie(setting, keyPrefix, storage);
    merkleTree.setData(key, version, valueHash.toBytes());
    merkleTree.commit();
    MerkleTrieData data = merkleTree.getData(key);
    assertNotNull(data);
    merkleTree = new MerkleHashTrie(merkleTree.getRootHash(), setting, keyPrefix, storage, false);
    data = merkleTree.getData(key);
    assertNotNull(data);
// MerkleDataSet1 mkds = new MerkleDataSet1(setting, keyPrefix, storage, storage);
// HashDigest ledgerHash = new HashDigest(Base58Utils.decode("j5mxiw6RiHP7fhrySjYji1ER5aRe6d2quYHArtwUfsyoHZ"));
// 
// BlockchainKeypair user1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp",
// "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
// TransactionRequest req1 = LedgerTestUtils.createTxRequest_UserReg(user1, ledgerHash, 1580315317127L, parti0,
// parti0);
// // 引发错误的参数:ts=1580315317127;
// // txhash=j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG
// System.out.printf("\r\n ===||=== transactionRequest1.getTransactionContent().getHash()=[%s]\r\n",
// req1.getTransactionContent().getHash().toBase58());
}
Also used : Bytes(utils.Bytes) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleTrieData(com.jd.blockchain.ledger.proof.MerkleTrieData) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 3 with MemoryKVStorage

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

the class LedgerEditorTest method testWriteDataAccoutKvOp.

@SuppressWarnings("unused")
@Test
public void testWriteDataAccoutKvOp() {
    MemoryKVStorage storage = new MemoryKVStorage();
    LedgerEditor ldgEdt = createLedgerInitEditor(storage);
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    DataAccount dataAccount = ldgDS.getDataAccountSet().register(dataKP.getAddress(), dataKP.getPubKey(), null);
    dataAccount.getDataset().setValue("A", TypedValue.fromText("abc"), -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    LedgerBlock block = ldgEdt.prepare();
    // 提交数据,写入存储;
    ldgEdt.commit();
    // 预期这是第1个区块;
    assertNotNull(block);
    assertNotNull(block.getHash());
    assertEquals(0, block.getHeight());
    // 验证数据读写的一致性;
    BytesValue bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    String textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    // 验证重新加载的正确性;
    LedgerManager manager = new LedgerManager();
    HashDigest ledgerHash = block.getHash();
    LedgerRepository repo = manager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    dataAccount = repo.getDataAccountSet().getAccount(dataKP.getAddress());
    assertNotNull(dataAccount);
    bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    LedgerTransaction tx_init = repo.getTransactionSet().getTransaction(tx.getTransactionHash());
    assertNotNull(tx_init);
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccount(com.jd.blockchain.ledger.core.DataAccount) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 4 with MemoryKVStorage

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

the class LedgerManagerTest method testLedgerInit.

@Test
public void testLedgerInit() {
    // 创建账本初始化配置;
    LedgerInitSetting initSetting = createLedgerInitSetting();
    // 采用基于内存的 Storage;
    MemoryKVStorage storage = new MemoryKVStorage();
    // 新建账本;
    LedgerEditor ldgEdt = LedgerInitializer.createLedgerEditor(initSetting, storage);
    // 创建一个模拟的创世交易;
    TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(participants);
    // 记录交易,注册用户;
    LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) txCtx.getDataset();
    BlockchainKeypair userKP = BlockchainKeyGenerator.getInstance().generate();
    UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
    userAccount.setProperty("Name", "孙悟空", -1);
    userAccount.setProperty("Age", "10000", -1);
    System.out.println("UserAddress=" + userAccount.getAddress());
    // 提交交易结果;
    TransactionResult tx = txCtx.commit(TransactionState.SUCCESS);
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    // 生成区块;
    LedgerBlock genesisBlock = ldgEdt.prepare();
    HashDigest ledgerHash = genesisBlock.getHash();
    assertEquals(0, genesisBlock.getHeight());
    assertNotNull(genesisBlock.getHash());
    assertNull(genesisBlock.getPreviousHash());
    // 创世区块的账本hash 为null;创世区块本身的哈希就代表了账本的哈希;
    assertNull(genesisBlock.getLedgerHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
    assertNull(genesisBlock.getLedgerHash());
    assertNotNull(genesisBlock.getHash());
    // 重新加载并校验结果;
    LedgerManager reloadLedgerManager = new LedgerManager();
    LedgerRepository reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    HashDigest genesisHash = reloadLedgerRepo.getBlockHash(0);
    assertEquals(ledgerHash, genesisHash);
    LedgerBlock latestBlock = reloadLedgerRepo.getLatestBlock();
    assertEquals(0, latestBlock.getHeight());
    assertEquals(ledgerHash, latestBlock.getHash());
    // 创世区块的账本hash 为null;创世区块本身的哈希就代表了账本的哈希;
    assertNull(latestBlock.getLedgerHash());
    LedgerEditor editor1 = reloadLedgerRepo.createNextBlock();
    CryptoSetting cryptoSetting = reloadLedgerRepo.getAdminInfo().getSettings().getCryptoSetting();
    TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    BlockchainKeypair dataKey = BlockchainKeyGenerator.getInstance().generate();
    txBuilder.dataAccounts().register(dataKey.getIdentity());
    TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
    DigitalSignature dgtsign = txReqBuilder.signAsEndpoint(userKP);
    TransactionRequest txRequest = txReqBuilder.buildRequest();
    LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest);
    ((DataAccountSetEditor) (txCtx1.getDataset().getDataAccountSet())).register(dataKey.getAddress(), dataKey.getPubKey(), null);
    txCtx1.commit(TransactionState.SUCCESS);
    LedgerBlock block1 = editor1.prepare();
    editor1.commit();
    assertEquals(1, block1.getHeight());
    assertNotNull(block1.getHash());
    assertEquals(genesisHash, block1.getPreviousHash());
    assertEquals(ledgerHash, block1.getLedgerHash());
    latestBlock = reloadLedgerRepo.getLatestBlock();
    assertEquals(1, latestBlock.getHeight());
    assertEquals(block1.getHash(), latestBlock.getHash());
    showStorageKeys(storage);
    reloadLedgerManager = new LedgerManager();
    reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    latestBlock = reloadLedgerRepo.getLatestBlock();
    assertEquals(1, latestBlock.getHeight());
    assertEquals(block1.getHash(), latestBlock.getHash());
    DataAccountSet dataAccountSet = reloadLedgerRepo.getDataAccountSet(latestBlock);
    UserAccountSet userAccountSet = reloadLedgerRepo.getUserAccountSet(latestBlock);
    ContractAccountSet contractAccountSet = reloadLedgerRepo.getContractAccountSet(latestBlock);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) TxBuilder(com.jd.blockchain.transaction.TxBuilder) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccountSetEditor(com.jd.blockchain.ledger.core.DataAccountSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet) ContractAccountSet(com.jd.blockchain.ledger.core.ContractAccountSet) UserAccount(com.jd.blockchain.ledger.core.UserAccount) UserAccountSet(com.jd.blockchain.ledger.core.UserAccountSet) Test(org.junit.Test)

Example 5 with MemoryKVStorage

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

the class MerkleHashTriePerformanceTest method testPerformace1.

private void testPerformace1(int round, int count) {
    System.out.printf("------------- Performance test: MerkleHashTrie --------------\r\n", round, count);
    CryptoSetting setting = LedgerTestUtils.createDefaultCryptoSetting();
    Bytes prefix = Bytes.fromString(LedgerTestUtils.LEDGER_KEY_PREFIX);
    MemoryKVStorage storage = new MemoryKVStorage();
    Random rand = new Random();
    byte[] value = new byte[128];
    rand.nextBytes(value);
    long startTs = System.currentTimeMillis();
    MerkleHashTrie merkleTree = new MerkleHashTrie(setting, prefix, storage);
    String key;
    for (int r = 0; r < round; r++) {
        for (int i = 0; i < count; i++) {
            key = "KEY-" + r + "-" + i;
            merkleTree.setData(key, 0, value);
        }
        merkleTree.commit();
    }
    long elapsedTs = System.currentTimeMillis() - startTs;
    long totalCount = count * round;
    double tps = round * 1000.0D / elapsedTs;
    double kps = round * count * 1000.0D / elapsedTs;
    System.out.printf("--[Performance]:: TotalKeys=%s; Round=%s; Count=%s; Times=%sms; TPS=%.2f; KPS=%.2f\r\n\r\n", totalCount, round, count, elapsedTs, tps, kps);
}
Also used : Bytes(utils.Bytes) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) Random(java.util.Random) MerkleHashTrie(com.jd.blockchain.ledger.proof.MerkleHashTrie) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage)

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