Search in sources :

Example 1 with LedgerMetadata_V2

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

the class GatewayQueryServiceHandler method initLedgerBaseSettings.

/**
 * 初始化账本的基本配置
 *
 * @param ledgerAdminInfo 账本信息
 *
 * @return
 */
private LedgerInitAttributes initLedgerBaseSettings(LedgerAdminInfo ledgerAdminInfo) {
    LedgerMetadata_V2 ledgerMetadata = ledgerAdminInfo.getMetadata();
    LedgerInitAttributes ledgerBaseSettings = new LedgerInitAttributes();
    // 设置参与方
    ledgerBaseSettings.setParticipantNodes(ledgerAdminInfo.getParticipants());
    // 设置共识设置
    ledgerBaseSettings.setConsensusSettings(initConsensusSettings(ledgerAdminInfo));
    // 设置参与方根Hash
    ledgerBaseSettings.setParticipantsHash(ledgerMetadata.getParticipantsHash());
    // 设置算法配置
    ledgerBaseSettings.setCryptoSetting(ledgerAdminInfo.getSettings().getCryptoSetting());
    // 设置种子
    ledgerBaseSettings.setSeed(initSeed(ledgerMetadata.getSeed()));
    // 设置共识协议
    ledgerBaseSettings.setConsensusProtocol(ledgerAdminInfo.getSettings().getConsensusProvider());
    // 设置账本结构版本号
    ledgerBaseSettings.setLedgerStructureVersion(ledgerMetadata.getLedgerStructureVersion());
    // 设置什么认证模式
    ledgerBaseSettings.setIdentityMode(ledgerMetadata.getIdentityMode());
    // 设置账本证书
    ledgerBaseSettings.setLedgerCertificates(ledgerMetadata.getLedgerCertificates());
    // 设置创世用户
    ledgerBaseSettings.setGenesisUsers(ledgerMetadata.getGenesisUsers());
    // 设置合约运行时配置
    ledgerBaseSettings.setContractRuntimeConfig(ledgerMetadata.getContractRuntimeConfig());
    return ledgerBaseSettings;
}
Also used : LedgerInitAttributes(com.jd.blockchain.sdk.LedgerInitAttributes) LedgerMetadata_V2(com.jd.blockchain.ledger.LedgerMetadata_V2)

Example 2 with LedgerMetadata_V2

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

the class LedgerAdminDatasetTest method verifyRealoadingSettings.

private void verifyRealoadingSettings(LedgerAdminDataSetEditor actualAccount, HashDigest expAccRootHash, LedgerMetadata_V2 expMeta, LedgerSettings expLedgerSettings) {
    // 验证基本信息;
    assertFalse(actualAccount.isUpdated());
    assertTrue(actualAccount.isReadonly());
    assertEquals(expAccRootHash, actualAccount.getHash());
    // verify metadata;
    LedgerMetadata_V2 actualMeta = actualAccount.getMetadata();
    assertArrayEquals(expMeta.getSeed(), actualMeta.getSeed());
    assertEquals(expMeta.getParticipantsHash(), actualMeta.getParticipantsHash());
    assertNotNull(actualMeta.getSettingsHash());
    assertEquals(expMeta.getSettingsHash(), actualMeta.getSettingsHash());
    assertNotNull(actualMeta.getRolePrivilegesHash());
    assertEquals(expMeta.getRolePrivilegesHash(), actualMeta.getRolePrivilegesHash());
    assertNotNull(actualMeta.getUserRolesHash());
    assertEquals(expMeta.getUserRolesHash(), actualMeta.getUserRolesHash());
    LedgerSettings actualLedgerSettings = actualAccount.getSettings();
    assertEquals(expLedgerSettings.getConsensusSetting(), actualLedgerSettings.getConsensusSetting());
    assertEquals(expLedgerSettings.getConsensusProvider(), actualLedgerSettings.getConsensusProvider());
    assertEquals(expLedgerSettings.getCryptoSetting().getAutoVerifyHash(), actualLedgerSettings.getCryptoSetting().getAutoVerifyHash());
    assertEquals(expLedgerSettings.getCryptoSetting().getHashAlgorithm(), actualLedgerSettings.getCryptoSetting().getHashAlgorithm());
}
Also used : LedgerMetadata_V2(com.jd.blockchain.ledger.LedgerMetadata_V2) LedgerSettings(com.jd.blockchain.ledger.LedgerSettings)

Example 3 with LedgerMetadata_V2

use of com.jd.blockchain.ledger.LedgerMetadata_V2 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

LedgerMetadata_V2 (com.jd.blockchain.ledger.LedgerMetadata_V2)3 LedgerSettings (com.jd.blockchain.ledger.LedgerSettings)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)1 HashDigest (com.jd.blockchain.crypto.HashDigest)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)1 LedgerConfiguration (com.jd.blockchain.ledger.core.LedgerConfiguration)1 LedgerInitAttributes (com.jd.blockchain.sdk.LedgerInitAttributes)1 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)1 ConsensusParticipantData (com.jd.blockchain.transaction.ConsensusParticipantData)1 LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)1 Test (org.junit.Test)1 Bytes (utils.Bytes)1 NetworkAddress (utils.net.NetworkAddress)1