Search in sources :

Example 1 with LedgerInitSetting

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

the class LedgerInitSettingSerializeTest method test_ledgerinitsetting_ParticipantCertData.

// @Test
// public void test_ledgerinitsetting_ConsensusParticipantConfig() {
// }
@Test
public void test_ledgerinitsetting_ParticipantCertData() {
    ParticipantCertData[] parties = new ParticipantCertData[4];
    BlockchainKeypair[] keys = new BlockchainKeypair[parties.length];
    for (int i = 0; i < parties.length; i++) {
        keys[i] = BlockchainKeyGenerator.getInstance().generate();
        parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()), "Participant[" + i + "]", keys[i].getPubKey(), ParticipantNodeState.CONSENSUS);
    }
    ParticipantCertData[] 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 : ParticipantCertData(com.jd.blockchain.ledger.core.ParticipantCertData) LedgerInitSetting(com.jd.blockchain.ledger.LedgerInitSetting) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) Test(org.junit.Test)

Example 2 with LedgerInitSetting

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

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

the class BlockFullRollBackTest method initLedger.

private HashDigest initLedger(MemoryKVStorage storage, BlockchainKeypair... partiKeys) {
    // 创建初始化配置;
    LedgerInitSetting initSetting = LedgerTestUtils.createLedgerInitSetting(partiKeys);
    // 创建账本;
    LedgerEditor ldgEdt = LedgerTransactionalEditor.createEditor(initSetting, LEDGER_KEY_PREFIX, storage, storage, LedgerDataStructure.MERKLE_TREE);
    TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(partiKeys);
    LedgerTransactionContext genisisTxCtx = ldgEdt.newTransaction(genesisTxReq);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    for (int i = 0; i < partiKeys.length; i++) {
        UserAccount userAccount = ldgDS.getUserAccountSet().register(partiKeys[i].getAddress(), partiKeys[i].getPubKey());
        userAccount.setProperty("Name", "参与方-" + i, -1);
        userAccount.setProperty("Share", "" + (10 + i), -1);
    }
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    LedgerBlock block = ldgEdt.prepare();
    assertEquals(0, block.getHeight());
    assertNotNull(block.getHash());
    assertNull(block.getPreviousHash());
    // 创世区块的账本哈希为 null;
    assertNull(block.getLedgerHash());
    assertNotNull(block.getHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
    HashDigest ledgerHash = block.getHash();
    return ledgerHash;
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerInitSetting(com.jd.blockchain.ledger.LedgerInitSetting) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Aggregations

LedgerInitSetting (com.jd.blockchain.ledger.LedgerInitSetting)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 Test (org.junit.Test)2 HashDigest (com.jd.blockchain.crypto.HashDigest)1 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)1 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)1 TransactionResult (com.jd.blockchain.ledger.TransactionResult)1 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)1 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)1 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)1 ParticipantCertData (com.jd.blockchain.ledger.core.ParticipantCertData)1 UserAccount (com.jd.blockchain.ledger.core.UserAccount)1 ConsensusParticipantData (com.jd.blockchain.transaction.ConsensusParticipantData)1 NetworkAddress (utils.net.NetworkAddress)1