use of com.jd.blockchain.crypto.CryptoProvider 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();
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerInitOperationTest method initCfg.
@Before
public void initCfg() {
DataContractRegistry.register(LedgerInitSetting.class);
DataContractRegistry.register(LedgerInitOperation.class);
Random rand = new Random();
seed = new byte[8];
rand.nextBytes(seed);
csSysSettingBytes = new byte[64];
rand.nextBytes(csSysSettingBytes);
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);
ledgerInitSettingData.setConsensusSettings(new Bytes(csSysSettingBytes));
ledgerInitSettingData.setConsensusProvider("cons-provider");
ledgerInitSettingData.setLedgerSeed(seed);
ledgerInitSettingData.setCryptoSetting(cryptoConfig);
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerMetaDataTest method testSerialize_CryptoSetting.
// @Test
// public void testSerialize_ConsensusSetting() {
// //LedgerCodes.METADATA_LEDGER_SETTING_CONSENSUS
// Random rand = new Random();
// byte[] settingValue = new byte[8];
// rand.nextBytes(settingValue);
//
// ConsensusConfig consensusConfig = new ConsensusConfig();
// consensusConfig.setValue(settingValue);
// byte[] encodeBytes = BinaryEncodingUtils.encode(consensusConfig,
// ConsensusSetting.class);
// ConsensusSetting deConsensusConfig = BinaryEncodingUtils.decode(encodeBytes);
//
// //verify start
// assertArrayEquals(consensusConfig.getValue(), deConsensusConfig.getValue());
//
// return;
// }
@Test
public void testSerialize_CryptoSetting() {
// LedgerCodes.METADATA_LEDGER_SETTING_CRYPTO
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);
byte[] encodeBytes = BinaryProtocol.encode(cryptoConfig, CryptoSetting.class);
CryptoSetting deCryptoConfig = BinaryProtocol.decode(encodeBytes);
// verify start
assertEquals(cryptoConfig.getHashAlgorithm(), deCryptoConfig.getHashAlgorithm());
assertEquals(cryptoConfig.getAutoVerifyHash(), deCryptoConfig.getAutoVerifyHash());
return;
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerTestUtils method createDefaultCryptoSetting.
public static CryptoSetting createDefaultCryptoSetting() {
CryptoProvider[] supportedProviders = getContextProviders();
CryptoConfig cryptoSetting = new CryptoConfig();
cryptoSetting.setSupportedProviders(supportedProviders);
cryptoSetting.setAutoVerifyHash(true);
cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256);
return cryptoSetting;
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class MerkleHashDataSetTest method testStorageIncreasement.
/**
* TODO:测试存储的增长;
*/
// @Test
public void testStorageIncreasement() {
CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
}
String keyPrefix = "";
CryptoConfig cryptoConfig = new CryptoConfig();
cryptoConfig.setSupportedProviders(supportedProviders);
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256);
cryptoConfig.setAutoVerifyHash(true);
MemoryKVStorage storage = new MemoryKVStorage();
MerkleHashDataset mds = new MerkleHashDataset(cryptoConfig, keyPrefix, storage, storage);
Dataset<String, byte[]> ds = DatasetHelper.map(mds);
long v;
v = ds.setValue("A", "A".getBytes(), -1);
assertEquals(0, v);
v = ds.setValue("B", "B".getBytes(), -1);
assertEquals(0, v);
v = ds.setValue("C", "C".getBytes(), -1);
assertEquals(0, v);
mds.commit();
byte[] va = ds.getValue("A");
assertNotNull(va);
assertEquals("A", new String(va));
byte[] vc = ds.getValue("C");
DataEntry<String, byte[]> ventry = ds.getDataEntry("C");
assertNotNull(vc);
assertNotNull(ventry);
assertEquals("C", new String(vc));
assertEquals("C", ventry.getKey());
HashDigest root1 = mds.getRootHash();
// 1个KV项的存储KEY的数量= 1 + Merkle节点数量;
// 所以:3 项;
// so the expected item count in storage is 10;
// int expStorageCount = 10; // 3 数据项, 7 个 Merkle 节点(1 个根节点,3 个叶子节点,3 个数据节点);
// assertEquals(expStorageCount, storage.getStorageCount());
ds.setValue("B", "B".getBytes(), 0);
ds.setValue("C", "C".getBytes(), 0);
mds.commit();
HashDigest root2 = mds.getRootHash();
assertNotEquals(root1, root2);
// Version changed only;仅仅增加 merkle 节点,此时 Merkle 树只有 1 层路径节点,因此只更新2个数据节点和 1
// 个路径节点;(注:版本值是在同一个 key 下按序列保存的);
// expStorageCount = expStorageCount + 5; // 增加了
// // mds.print();
// assertEquals(expStorageCount, storage.getStorageCount());
ds.setValue("D", "DValue".getBytes(), -1);
mds.commit();
HashDigest root3 = mds.getRootHash();
assertNotEquals(root2, root3);
assertNotEquals(root1, root3);
// New key added, include 1 versioning kv, 1 sn key, 2 merkle nodes;
// String[] keys = StringUtils.toStringArray(storage.keySet());
// Bytes[] keys = storage.keySet().toArray(new Bytes[0]);
// expStorageCount = expStorageCount + 1 + 1 + 2;
// assertEquals(expStorageCount, storage.getStorageCount());
// Check rollback function: Add some keys, and then rollback;
v = ds.setValue("E", "E-values".getBytes(), -1);
assertEquals(v, 0);
String expEValue = new String(ds.getValue("E"));
assertEquals(expEValue, "E-values");
v = ds.setValue("F", "F-values".getBytes(), -1);
assertEquals(v, 0);
String expFValue = new String(ds.getValue("F"));
assertEquals(expFValue, "F-values");
v = ds.setValue("E", "E-values-1".getBytes(), 0);
assertEquals(v, 1);
expEValue = new String(ds.getValue("E"));
assertEquals(expEValue, "E-values-1");
mds.cancel();
byte[] bv = ds.getValue("E");
assertNull(bv);
bv = ds.getValue("F");
assertNull(bv);
v = ds.getVersion("E");
assertEquals(-1, v);
v = ds.getVersion("F");
assertEquals(-1, v);
// // Expect that states has been recover;
// keys = storage.keySet().toArray(new Bytes[0]);
// assertEquals(expStorageCount, storage.getStorageCount());
// ------
System.out.println("\r\n------------- storage keys --------------");
Set<Bytes> storageKeys = storage.getStorageKeySet();
int i = 0;
for (Bytes k : storageKeys) {
System.out.println(String.format("keys[%s]=%s", i, k));
i++;
}
}
Aggregations