Search in sources :

Example 1 with LedgerEditor

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

the class LedgerEditorTest method testGennesisBlockCreation.

/**
 * 测试创建账本;
 */
@Test
public void testGennesisBlockCreation() {
    LedgerEditor ldgEdt = createLedgerInitEditor();
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair userKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
    userAccount.setProperty("Name", "孙悟空", -1);
    userAccount.setProperty("Age", "10000", -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    TransactionRequest genesisTxReq = genisisTxCtx.getTransactionRequest();
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    LedgerBlock block = ldgEdt.prepare();
    assertEquals(0, block.getHeight());
    assertNotNull(block.getHash());
    assertNull(block.getLedgerHash());
    assertNull(block.getPreviousHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) Test(org.junit.Test)

Example 2 with LedgerEditor

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

the class LedgerEditorTest method testWriteDataAccoutKvOp.

@SuppressWarnings("unused")
@Test
public void testWriteDataAccoutKvOp() {
    MemoryKVStorage storage = new MemoryKVStorage();
    LedgerEditor ldgEdt = createLedgerInitEditor(storage);
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    DataAccount dataAccount = ldgDS.getDataAccountSet().register(dataKP.getAddress(), dataKP.getPubKey(), null);
    dataAccount.getDataset().setValue("A", TypedValue.fromText("abc"), -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    LedgerBlock block = ldgEdt.prepare();
    // 提交数据,写入存储;
    ldgEdt.commit();
    // 预期这是第1个区块;
    assertNotNull(block);
    assertNotNull(block.getHash());
    assertEquals(0, block.getHeight());
    // 验证数据读写的一致性;
    BytesValue bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    String textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    // 验证重新加载的正确性;
    LedgerManager manager = new LedgerManager();
    HashDigest ledgerHash = block.getHash();
    LedgerRepository repo = manager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    dataAccount = repo.getDataAccountSet().getAccount(dataKP.getAddress());
    assertNotNull(dataAccount);
    bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    LedgerTransaction tx_init = repo.getTransactionSet().getTransaction(tx.getTransactionHash());
    assertNotNull(tx_init);
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccount(com.jd.blockchain.ledger.core.DataAccount) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 3 with LedgerEditor

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

the class ContractInvokingTest method testReadNewWritting.

// @Test
public void testReadNewWritting() {
    // 初始化账本到指定的存储库;
    HashDigest ledgerHash = initLedger(storage, parti0, parti1, parti2, parti3);
    // 重新加载账本;
    LedgerManager ledgerManager = new LedgerManager();
    LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    // 创建合约处理器;
    ContractInvokingHandle contractInvokingHandle = new ContractInvokingHandle();
    // 创建和加载合约实例;
    BlockchainKeypair contractKey = BlockchainKeyGenerator.getInstance().generate();
    Bytes contractAddress = contractKey.getAddress();
    TxTestContractImpl contractInstance = new TxTestContractImpl();
    contractInvokingHandle.setup(contractAddress, TxTestContract.class, contractInstance);
    // 注册合约处理器;
    DefaultOperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
    opReg.registerHandle(contractInvokingHandle);
    // 发布指定地址合约
    deploy(ledgerRepo, ledgerManager, opReg, ledgerHash, contractKey);
    // 创建新区块的交易处理器;
    LedgerBlock preBlock = ledgerRepo.getLatestBlock();
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(preBlock);
    // 加载合约
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(getSecurityManager(), newBlockEditor, ledgerRepo, opReg);
    String key = TxTestContractImpl.KEY;
    String value = "VAL";
    CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
    TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    BlockchainKeypair kpDataAccount = BlockchainKeyGenerator.getInstance().generate();
    contractInstance.setDataAddress(kpDataAccount.getAddress());
    txBuilder.dataAccounts().register(kpDataAccount.getIdentity());
    TransactionRequestBuilder txReqBuilder1 = txBuilder.prepareRequest();
    txReqBuilder1.signAsEndpoint(parti0);
    txReqBuilder1.signAsNode(parti0);
    TransactionRequest txReq1 = txReqBuilder1.buildRequest();
    // 构建基于接口调用合约的交易请求,用于测试合约调用;
    txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    TxTestContract contractProxy = txBuilder.contract(contractAddress, TxTestContract.class);
    BooleanValueHolder readableHolder = decode(contractProxy.testReadable());
    TransactionRequestBuilder txReqBuilder2 = txBuilder.prepareRequest();
    txReqBuilder2.signAsEndpoint(parti0);
    txReqBuilder2.signAsNode(parti0);
    TransactionRequest txReq2 = txReqBuilder2.buildRequest();
    TransactionResponse resp1 = txbatchProcessor.schedule(txReq1);
    TransactionResponse resp2 = txbatchProcessor.schedule(txReq2);
    // 提交区块;
    TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
    txResultHandle.commit();
    BytesValue latestValue = ledgerRepo.getDataAccountSet().getAccount(kpDataAccount.getAddress()).getDataset().getValue(key, -1);
    System.out.printf("latest value=[%s] %s \r\n", latestValue.getType(), latestValue.getBytes().toUTF8String());
    boolean readable = readableHolder.get();
    assertTrue(readable);
    LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
    assertEquals(preBlock.getHeight() + 1, latestBlock.getHeight());
    assertEquals(resp1.getBlockHeight(), latestBlock.getHeight());
    assertEquals(resp1.getBlockHash(), latestBlock.getHash());
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) DefaultOperationHandleRegisteration(com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) TxBuilder(com.jd.blockchain.transaction.TxBuilder) Matchers.anyString(org.mockito.Matchers.anyString) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TxTestContract(test.com.jd.blockchain.ledger.TxTestContract) Bytes(utils.Bytes) HashDigest(com.jd.blockchain.crypto.HashDigest) TxTestContractImpl(test.com.jd.blockchain.ledger.TxTestContractImpl) BooleanValueHolder(com.jd.blockchain.transaction.BooleanValueHolder) TransactionBatchResultHandle(com.jd.blockchain.service.TransactionBatchResultHandle)

Example 4 with LedgerEditor

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

the class ContractInvokingTest 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.getLedgerHash());
    assertNull(block.getPreviousHash());
    // 提交数据,写入存储;
    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)

Example 5 with LedgerEditor

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

the class ContractInvokingTest method deploy.

private void deploy(LedgerRepository ledgerRepo, LedgerManager ledgerManager, DefaultOperationHandleRegisteration opReg, HashDigest ledgerHash, BlockchainKeypair contractKey) {
    // 创建新区块的交易处理器;
    LedgerBlock preBlock = ledgerRepo.getLatestBlock();
    LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(preBlock);
    // 加载合约
    LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
    LedgerSecurityManager securityManager = getSecurityManager();
    TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
    // 构建基于接口调用合约的交易请求,用于测试合约调用;
    CryptoSetting cryptoSetting = ledgerRepo.getAdminInfo().getSettings().getCryptoSetting();
    TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    txBuilder.contracts().deploy(contractKey.getIdentity(), chainCode());
    TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
    txReqBuilder.signAsEndpoint(parti0);
    txReqBuilder.signAsNode(parti0);
    TransactionRequest txReq = txReqBuilder.buildRequest();
    TransactionResponse resp = txbatchProcessor.schedule(txReq);
    OperationResult[] opResults = resp.getOperationResults();
    assertNull(opResults);
    // 提交区块;
    TransactionBatchResultHandle txResultHandle = txbatchProcessor.prepare();
    txResultHandle.commit();
}
Also used : LedgerSecurityManager(com.jd.blockchain.ledger.core.LedgerSecurityManager) TransactionBatchProcessor(com.jd.blockchain.ledger.core.TransactionBatchProcessor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) TxBuilder(com.jd.blockchain.transaction.TxBuilder) LedgerDataSet(com.jd.blockchain.ledger.core.LedgerDataSet) TransactionBatchResultHandle(com.jd.blockchain.service.TransactionBatchResultHandle)

Aggregations

LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)17 HashDigest (com.jd.blockchain.crypto.HashDigest)12 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)10 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)10 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)10 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)10 Test (org.junit.Test)10 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)9 UserAccount (com.jd.blockchain.ledger.core.UserAccount)9 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)8 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)8 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)7 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)7 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)7 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)6 TxBuilder (com.jd.blockchain.transaction.TxBuilder)6 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)5 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)5 TransactionBatchResultHandle (com.jd.blockchain.service.TransactionBatchResultHandle)5