Search in sources :

Example 1 with LedgerInitData

use of com.jd.blockchain.transaction.LedgerInitData in project jdchain-core by blockchain-jd-com.

the class LedgerInitConfiguration method createLedgerInitSettings.

private static LedgerInitData createLedgerInitSettings(LedgerInitProperties ledgerProps, CryptoSetting cryptoSetting, ConsensusConfig consensusConfig, ParticipantProperties[] participants) {
    // 创建初始化配置;
    LedgerInitData initSetting = new LedgerInitData();
    initSetting.setLedgerSeed(ledgerProps.getLedgerSeed());
    initSetting.setIdentityMode(ledgerProps.getIdentityMode());
    initSetting.setLedgerCertificates(ledgerProps.getLedgerCertificates());
    initSetting.setGenesisUsers(ledgerProps.getGenesisUsers());
    initSetting.setCryptoSetting(cryptoSetting);
    initSetting.setConsensusParticipants(participants);
    initSetting.setCreatedTime(ledgerProps.getCreatedTime());
    initSetting.setLedgerDataStructure(ledgerProps.getLedgerDataStructure());
    initSetting.setContractRuntimeConfig(new JVMContractRuntimeConfig(ledgerProps.getContractTimeout(), ledgerProps.getMaxStackDepth()));
    // 创建共识配置;
    try {
        byte[] consensusSettingsBytes = encodeConsensusSettings(consensusConfig.getProvider(), consensusConfig.protocolSettings);
        initSetting.setConsensusProvider(consensusConfig.getProvider().getName());
        initSetting.setConsensusSettings(consensusSettingsBytes);
    } catch (Exception e) {
        throw new LedgerInitException("Create default consensus config failed! --" + e.getMessage(), e);
    }
    return initSetting;
}
Also used : JVMContractRuntimeConfig(com.jd.blockchain.contract.jvm.JVMContractRuntimeConfig) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException)

Example 2 with LedgerInitData

use of com.jd.blockchain.transaction.LedgerInitData in project jdchain-core by blockchain-jd-com.

the class LedgerManagerTest method createLedgerInitSetting.

private LedgerInitSetting createLedgerInitSetting() {
    CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
    for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
        supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
    }
    CryptoConfig defCryptoSetting = new CryptoConfig();
    defCryptoSetting.setSupportedProviders(supportedProviders);
    defCryptoSetting.setAutoVerifyHash(true);
    defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256);
    LedgerInitData initSetting = new LedgerInitData();
    initSetting.setLedgerSeed(BytesUtils.toBytes("A Test Ledger seed!", "UTF-8"));
    initSetting.setCryptoSetting(defCryptoSetting);
    ConsensusParticipantData[] parties = new ConsensusParticipantData[4];
    parties[0] = new ConsensusParticipantData();
    parties[0].setId(0);
    parties[0].setName("John");
    AsymmetricKeypair kp0 = signatureFunction.generateKeypair();
    parties[0].setPubKey(kp0.getPubKey());
    parties[0].setAddress(AddressEncoding.generateAddress(kp0.getPubKey()));
    parties[0].setHostAddress(new NetworkAddress("127.0.0.1", 9000));
    parties[0].setParticipantState(ParticipantNodeState.CONSENSUS);
    parties[1] = new ConsensusParticipantData();
    parties[1].setId(1);
    parties[1].setName("Mary");
    AsymmetricKeypair kp1 = signatureFunction.generateKeypair();
    parties[1].setPubKey(kp1.getPubKey());
    parties[1].setAddress(AddressEncoding.generateAddress(kp1.getPubKey()));
    parties[1].setHostAddress(new NetworkAddress("127.0.0.1", 9010));
    parties[1].setParticipantState(ParticipantNodeState.CONSENSUS);
    parties[2] = new ConsensusParticipantData();
    parties[2].setId(2);
    parties[2].setName("Jerry");
    AsymmetricKeypair kp2 = signatureFunction.generateKeypair();
    parties[2].setPubKey(kp2.getPubKey());
    parties[2].setAddress(AddressEncoding.generateAddress(kp2.getPubKey()));
    parties[2].setHostAddress(new NetworkAddress("127.0.0.1", 9020));
    parties[2].setParticipantState(ParticipantNodeState.CONSENSUS);
    parties[3] = new ConsensusParticipantData();
    parties[3].setId(3);
    parties[3].setName("Tom");
    AsymmetricKeypair kp3 = signatureFunction.generateKeypair();
    parties[3].setPubKey(kp3.getPubKey());
    parties[3].setAddress(AddressEncoding.generateAddress(kp3.getPubKey()));
    parties[3].setHostAddress(new NetworkAddress("127.0.0.1", 9030));
    parties[3].setParticipantState(ParticipantNodeState.CONSENSUS);
    initSetting.setConsensusParticipants(parties);
    initSetting.setLedgerDataStructure(LedgerDataStructure.MERKLE_TREE);
    initSetting.setIdentityMode(IdentityMode.KEYPAIR);
    return initSetting;
}
Also used : ConsensusParticipantData(com.jd.blockchain.transaction.ConsensusParticipantData) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) NetworkAddress(utils.net.NetworkAddress) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig)

Example 3 with LedgerInitData

use of com.jd.blockchain.transaction.LedgerInitData in project jdchain-core by blockchain-jd-com.

the class LedgerTestUtils method createLedgerInitSetting.

public static LedgerInitSetting createLedgerInitSetting(CryptoSetting cryptoSetting, BlockchainKeypair[] partiKeys) {
    LedgerInitData initSetting = new LedgerInitData();
    initSetting.setLedgerSeed(BytesUtils.toBytes("A Test Ledger seed!", "UTF-8"));
    initSetting.setCryptoSetting(cryptoSetting);
    initSetting.setIdentityMode(IdentityMode.KEYPAIR);
    ConsensusParticipantData[] parties = new ConsensusParticipantData[partiKeys.length];
    for (int i = 0; i < parties.length; i++) {
        parties[i] = new ConsensusParticipantData();
        parties[i].setId(0);
        parties[i].setName("Parti-" + i);
        parties[i].setPubKey(partiKeys[i].getPubKey());
        parties[i].setAddress(AddressEncoding.generateAddress(partiKeys[i].getPubKey()));
        parties[i].setHostAddress(new NetworkAddress("192.168.1." + (10 + i), 9000));
        parties[i].setParticipantState(ParticipantNodeState.CONSENSUS);
    }
    initSetting.setConsensusParticipants(parties);
    return initSetting;
}
Also used : ConsensusParticipantData(com.jd.blockchain.transaction.ConsensusParticipantData) NetworkAddress(utils.net.NetworkAddress) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData)

Example 4 with LedgerInitData

use of com.jd.blockchain.transaction.LedgerInitData 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)

Example 5 with LedgerInitData

use of com.jd.blockchain.transaction.LedgerInitData in project jdchain-core by blockchain-jd-com.

the class LedgerInitConfiguration method create.

public static LedgerInitConfiguration create(LedgerInitProperties ledgerInitProps) {
    LedgerInitConfiguration ledgerConfig = new LedgerInitConfiguration();
    CryptoConfig cryptoConfig = createCryptoConfig(ledgerInitProps.getCryptoProperties());
    ledgerConfig.cryptoConfig = cryptoConfig;
    ConsensusConfig consensusConfig = createConsensusConfig(ledgerInitProps);
    ledgerConfig.consensusConfig = consensusConfig;
    ParticipantProperties[] participants = resolveParticipants(ledgerInitProps);
    ledgerConfig.participants = participants;
    LedgerInitData ledgerSettings = createLedgerInitSettings(ledgerInitProps, cryptoConfig, consensusConfig, participants);
    ledgerSettings.setCreatedTime(ledgerInitProps.getCreatedTime());
    ledgerConfig.ledgerSettings = ledgerSettings;
    SecurityInitData securitySettings = createSecurityInitSettings(ledgerInitProps);
    ledgerConfig.securitySettings = securitySettings;
    return ledgerConfig;
}
Also used : SecurityInitData(com.jd.blockchain.ledger.SecurityInitData) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig)

Aggregations

LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)5 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)3 ConsensusParticipantData (com.jd.blockchain.transaction.ConsensusParticipantData)3 NetworkAddress (utils.net.NetworkAddress)3 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)2 JVMContractRuntimeConfig (com.jd.blockchain.contract.jvm.JVMContractRuntimeConfig)1 AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)1 HashDigest (com.jd.blockchain.crypto.HashDigest)1 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)1 ParticipantProperties (com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties)1 LedgerMetadata_V2 (com.jd.blockchain.ledger.LedgerMetadata_V2)1 LedgerSettings (com.jd.blockchain.ledger.LedgerSettings)1 SecurityInitData (com.jd.blockchain.ledger.SecurityInitData)1 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)1 LedgerConfiguration (com.jd.blockchain.ledger.core.LedgerConfiguration)1 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)1 Test (org.junit.Test)1 Bytes (utils.Bytes)1