Search in sources :

Example 1 with LedgerSettings

use of com.jd.blockchain.ledger.LedgerSettings 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 2 with LedgerSettings

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

the class LedgerMetaDataTest method testSerialize_LedgerSetting.

@Test
public void testSerialize_LedgerSetting() {
    // LedgerCodes.METADATA_LEDGER_SETTING
    Random rand = new Random();
    byte[] csSettingsBytes = new byte[8];
    rand.nextBytes(csSettingsBytes);
    String consensusProvider = "testprovider";
    // ConsensusConfig consensusConfig = new ConsensusConfig();
    // consensusConfig.setValue(settingValue);
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setSupportedProviders(supportedProviders);
    cryptoConfig.setAutoVerifyHash(true);
    cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256);
    LedgerConfiguration ledgerConfiguration = new LedgerConfiguration(consensusProvider, new Bytes(csSettingsBytes), cryptoConfig);
    byte[] encodeBytes = BinaryProtocol.encode(ledgerConfiguration, LedgerSettings.class);
    LedgerSettings deLedgerConfiguration = BinaryProtocol.decode(encodeBytes);
    // verify start
    assertTrue(ledgerConfiguration.getConsensusSetting().equals(deLedgerConfiguration.getConsensusSetting()));
    assertEquals(ledgerConfiguration.getCryptoSetting().getAutoVerifyHash(), deLedgerConfiguration.getCryptoSetting().getAutoVerifyHash());
    assertEquals(ledgerConfiguration.getCryptoSetting().getHashAlgorithm(), deLedgerConfiguration.getCryptoSetting().getHashAlgorithm());
    return;
}
Also used : Bytes(utils.Bytes) Random(java.util.Random) LedgerConfiguration(com.jd.blockchain.ledger.core.LedgerConfiguration) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) LedgerSettings(com.jd.blockchain.ledger.LedgerSettings) Test(org.junit.Test)

Example 3 with LedgerSettings

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

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