Search in sources :

Example 6 with LedgerDataSetEditor

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

the class LedgerTestUtils method initLedger.

public static 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 : LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount)

Aggregations

LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)6 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)6 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)6 HashDigest (com.jd.blockchain.crypto.HashDigest)5 UserAccount (com.jd.blockchain.ledger.core.UserAccount)5 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)3 TransactionResult (com.jd.blockchain.ledger.TransactionResult)3 Test (org.junit.Test)3 AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)2 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)2 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)2 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)2 BytesValue (com.jd.blockchain.ledger.BytesValue)1 LedgerInitSetting (com.jd.blockchain.ledger.LedgerInitSetting)1 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)1 ContractAccountSet (com.jd.blockchain.ledger.core.ContractAccountSet)1 DataAccount (com.jd.blockchain.ledger.core.DataAccount)1 DataAccountSet (com.jd.blockchain.ledger.core.DataAccountSet)1