use of com.jd.blockchain.ledger.BlockchainKeypair in project jdchain-core by blockchain-jd-com.
the class LedgerInitSettingSerializeTest method test_ledgerinitsetting_ConsensusParticipantData.
@Test
public void test_ledgerinitsetting_ConsensusParticipantData() {
ConsensusParticipantData[] parties = new ConsensusParticipantData[4];
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length];
for (int i = 0; i < parties.length; i++) {
keys[i] = BlockchainKeyGenerator.getInstance().generate();
parties[i] = new ConsensusParticipantData();
// parties[i].setId(i);
parties[i].setAddress(AddressEncoding.generateAddress(keys[i].getPubKey()));
parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i));
parties[i].setName("Participant[" + i + "]");
parties[i].setPubKey(keys[i].getPubKey());
parties[i].setParticipantState(ParticipantNodeState.CONSENSUS);
}
ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4);
ledgerInitSettingData.setConsensusParticipants(parties1);
ledgerInitSettingData.setLedgerDataStructure(LedgerDataStructure.MERKLE_TREE);
ledgerInitSettingData.setIdentityMode(IdentityMode.KEYPAIR);
byte[] encode = BinaryProtocol.encode(ledgerInitSettingData, LedgerInitSetting.class);
LedgerInitSetting decode = BinaryProtocol.decode(encode);
for (int i = 0; i < ledgerInitSettingData.getConsensusParticipants().length; i++) {
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getAddress(), decode.getConsensusParticipants()[i].getAddress());
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getName(), decode.getConsensusParticipants()[i].getName());
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getPubKey(), decode.getConsensusParticipants()[i].getPubKey());
}
assertArrayEquals(ledgerInitSettingData.getLedgerSeed(), decode.getLedgerSeed());
assertArrayEquals(ledgerInitSettingData.getConsensusSettings().toBytes(), decode.getConsensusSettings().toBytes());
assertEquals(ledgerInitSettingData.getCryptoSetting().getHashAlgorithm(), decode.getCryptoSetting().getHashAlgorithm());
assertEquals(ledgerInitSettingData.getCryptoSetting().getAutoVerifyHash(), decode.getCryptoSetting().getAutoVerifyHash());
assertEquals(ledgerInitSettingData.getConsensusProvider(), decode.getConsensusProvider());
}
use of com.jd.blockchain.ledger.BlockchainKeypair in project jdchain-core by blockchain-jd-com.
the class MerkleAccountSetTest method testRegister.
@Test
public void testRegister() {
final OpeningAccessPolicy POLICY = new OpeningAccessPolicy();
final MemoryKVStorage STORAGE = new MemoryKVStorage();
Bytes KEY_PREFIX = Bytes.fromString("/ACCOUNT");
CryptoConfig cryptoConfig = new CryptoConfig();
cryptoConfig.setSupportedProviders(LedgerTestUtils.getContextProviders());
cryptoConfig.setAutoVerifyHash(true);
cryptoConfig.setHashAlgorithm(Crypto.getAlgorithm("SHA256"));
MerkleAccountSetEditor accountsetEditor = new MerkleAccountSetEditor(cryptoConfig, KEY_PREFIX, STORAGE, STORAGE, POLICY);
BlockchainKeypair key1 = BlockchainKeyGenerator.getInstance().generate();
accountsetEditor.register(key1.getIdentity());
accountsetEditor.commit();
CompositeAccount acc1 = accountsetEditor.getAccount(key1.getAddress());
assertNotNull(acc1);
assertEquals(0, accountsetEditor.getVersion(key1.getAddress()));
acc1.getDataset().setValue("K1", TypedValue.fromText("V0"), -1);
TypedValue v1 = acc1.getDataset().getValue("K1");
assertNotNull(v1);
assertEquals(0, acc1.getDataset().getVersion("K1"));
accountsetEditor.commit();
v1 = acc1.getDataset().getValue("K1");
assertNotNull(v1);
assertEquals(0, acc1.getDataset().getVersion("K1"));
}
use of com.jd.blockchain.ledger.BlockchainKeypair in project jdchain-core by blockchain-jd-com.
the class AccountSetTest method test.
@Test
public void test() {
OpeningAccessPolicy accessPolicy = new OpeningAccessPolicy();
MemoryKVStorage storage = new MemoryKVStorage();
CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
}
CryptoConfig cryptoConf = new CryptoConfig();
cryptoConf.setSupportedProviders(supportedProviders);
cryptoConf.setAutoVerifyHash(true);
cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256);
String keyPrefix = "";
MerkleAccountSetEditor accset = new MerkleAccountSetEditor(cryptoConf, Bytes.fromString(keyPrefix), storage, storage, accessPolicy);
BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate();
accset.register(userKey.getAddress(), userKey.getPubKey());
// 尚未提交之前,可以检索到账户的存在,但版本仍然标记为 -1;
CompositeAccount userAcc = accset.getAccount(userKey.getAddress());
assertNotNull(userAcc);
assertTrue(accset.contains(userKey.getAddress()));
accset.commit();
HashDigest rootHash = accset.getRootHash();
assertNotNull(rootHash);
MerkleAccountSetEditor reloadAccSet = new MerkleAccountSetEditor(rootHash, cryptoConf, Bytes.fromString(keyPrefix), storage, storage, true, accessPolicy);
CompositeAccount reloadUserAcc = reloadAccSet.getAccount(userKey.getAddress());
assertNotNull(reloadUserAcc);
assertTrue(reloadAccSet.contains(userKey.getAddress()));
assertEquals(userAcc.getID().getAddress(), reloadUserAcc.getID().getAddress());
assertEquals(userAcc.getID().getPubKey(), reloadUserAcc.getID().getPubKey());
}
use of com.jd.blockchain.ledger.BlockchainKeypair in project jdchain-core by blockchain-jd-com.
the class BaseAccountTest method basicTest.
@Test
public void basicTest() {
String keyPrefix = "";
MemoryKVStorage testStorage = new MemoryKVStorage();
CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length];
for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) {
supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]);
}
CryptoConfig cryptoConf = new CryptoConfig();
cryptoConf.setSupportedProviders(supportedProviders);
cryptoConf.setAutoVerifyHash(true);
cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256);
// OpeningAccessPolicy accPlc = new OpeningAccessPolicy();
BlockchainKeypair bck = BlockchainKeyGenerator.getInstance().generate();
// 新建账户;
MerkleComplecatedAccount baseAccount = new MerkleComplecatedAccount(bck.getIdentity(), cryptoConf, Bytes.fromString(keyPrefix), testStorage, testStorage);
// 初始化新账户时,先写入PubKey;
assertTrue(baseAccount.isUpdated());
assertFalse(baseAccount.isReadonly());
// 在空白状态下写入数据;
long v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), 0);
// 预期失败;
assertEquals(-1, v);
v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), 1);
// 预期失败;
assertEquals(-1, v);
v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A"), -1);
// 预期成功;
assertEquals(0, v);
v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A-1"), -1);
// 已经存在版本,指定版本号-1,预期导致失败;
assertEquals(-1, v);
baseAccount.commit();
v = 0;
for (int i = 0; i < 10; i++) {
long s = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A_" + i), v);
baseAccount.commit();
// 预期成功;
assertEquals(v + 1, s);
v++;
}
v = baseAccount.getDataset().setValue("A", TypedValue.fromText("VALUE_A_" + v), v + 1);
// 预期成功;
assertEquals(-1, v);
System.out.println("============== commit ==============");
baseAccount.commit();
}
use of com.jd.blockchain.ledger.BlockchainKeypair in project jdchain-core by blockchain-jd-com.
the class ProxyClientTest method proxyClientSend.
public void proxyClientSend(BftsmartNodeServer nodeServer) {
BftsmartClientIncomingConfig clientIncomingConfig = new BftsmartClientIncomingConfig();
BlockchainKeypair keyPair = BlockchainKeyGenerator.getInstance().generate();
clientIncomingConfig.setPubKey(keyPair.getPubKey());
clientIncomingConfig.setSessionCredential(new BftsmartSessionCredentialConfig(0, 10, System.currentTimeMillis()));
clientIncomingConfig.setViewSettings(nodeServer.getConsensusSetting());
clientIncomingConfig.setTomConfig(BinarySerializeUtils.serialize(nodeServer.getTomConfig()));
clientIncomingConfig.setTopology(BinarySerializeUtils.serialize(nodeServer.getTopology()));
BftsmartClientConfig clientSettings = new BftsmartClientConfig(clientIncomingConfig);
BftsmartConsensusClient consensusClient = new BftsmartConsensusClient(clientSettings);
bytes = new byte[1024];
BftsmartMessageService messageService = (BftsmartMessageService) consensusClient.getMessageService();
for (int j = 0; j < number; j++) {
txSendPools.execute(() -> {
random.nextBytes(bytes);
messageService.sendOrdered(bytes);
});
}
}
Aggregations