Search in sources :

Example 6 with BlockchainKeypair

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());
}
Also used : ConsensusParticipantData(com.jd.blockchain.transaction.ConsensusParticipantData) NetworkAddress(utils.net.NetworkAddress) LedgerInitSetting(com.jd.blockchain.ledger.LedgerInitSetting) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) Test(org.junit.Test)

Example 7 with BlockchainKeypair

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"));
}
Also used : Bytes(utils.Bytes) MerkleAccountSetEditor(com.jd.blockchain.ledger.core.MerkleAccountSetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) OpeningAccessPolicy(com.jd.blockchain.ledger.core.OpeningAccessPolicy) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) CompositeAccount(com.jd.blockchain.ledger.core.CompositeAccount) TypedValue(com.jd.blockchain.ledger.TypedValue) Test(org.junit.Test)

Example 8 with BlockchainKeypair

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());
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) MerkleAccountSetEditor(com.jd.blockchain.ledger.core.MerkleAccountSetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) OpeningAccessPolicy(com.jd.blockchain.ledger.core.OpeningAccessPolicy) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) CompositeAccount(com.jd.blockchain.ledger.core.CompositeAccount) Test(org.junit.Test)

Example 9 with BlockchainKeypair

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();
}
Also used : MerkleComplecatedAccount(com.jd.blockchain.ledger.core.MerkleComplecatedAccount) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

Example 10 with BlockchainKeypair

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);
        });
    }
}
Also used : BftsmartMessageService(com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService) BftsmartConsensusClient(com.jd.blockchain.consensus.bftsmart.client.BftsmartConsensusClient) BftsmartClientIncomingConfig(com.jd.blockchain.consensus.bftsmart.BftsmartClientIncomingConfig) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) BftsmartSessionCredentialConfig(com.jd.blockchain.consensus.bftsmart.client.BftsmartSessionCredentialConfig) BftsmartClientConfig(com.jd.blockchain.consensus.bftsmart.client.BftsmartClientConfig)

Aggregations

BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)23 Test (org.junit.Test)16 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)14 HashDigest (com.jd.blockchain.crypto.HashDigest)11 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)9 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)8 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)8 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)7 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)7 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)6 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)5 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)5 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)5 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)5 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)5 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)5 UserAccount (com.jd.blockchain.ledger.core.UserAccount)5 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)4 TransactionResult (com.jd.blockchain.ledger.TransactionResult)4 NetworkAddress (utils.net.NetworkAddress)4