Search in sources :

Example 21 with BlockchainKeypair

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

the class TransactionSetTest method buildTransactionRequest_RandomOperation.

/**
 * 创建交易请求;以随机生成的数据作为交易的操作参数;
 *
 * @param ledgerHash
 * @param defCryptoSetting
 * @return
 */
private TransactionRequest buildTransactionRequest_RandomOperation(HashDigest ledgerHash, CryptoSetting defCryptoSetting) {
    // Build transaction request;
    TxBuilder txBuilder = new TxBuilder(ledgerHash, defCryptoSetting.getHashAlgorithm());
    Operation[] operations = new Operation[5];
    // register user;
    BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate();
    operations[0] = txBuilder.users().register(userKey.getIdentity());
    // register data account;
    BlockchainKeypair dataKey = BlockchainKeyGenerator.getInstance().generate();
    operations[1] = txBuilder.dataAccounts().register(dataKey.getIdentity());
    // set data after registering data account immediately;
    operations[2] = txBuilder.dataAccount(dataKey.getAddress()).setText("A", "Value_A_0", -1).setText("B", "Value_B_0", -1).getOperation();
    // generate random bytes as the bytes of ChainCode to deploy as a smart
    // contract;
    byte[] chainCode = new byte[128];
    rand.nextBytes(chainCode);
    BlockchainKeypair contractKey = BlockchainKeyGenerator.getInstance().generate();
    operations[3] = txBuilder.contracts().deploy(contractKey.getIdentity(), chainCode);
    // invoke smart contract;
    operations[4] = txBuilder.contract(contractKey.getAddress()).invoke("test", BytesDataList.singleText("TestContractArgs"));
    // build transaction request;
    TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
    BlockchainKeypair sponsorKey = BlockchainKeyGenerator.getInstance().generate();
    txReqBuilder.signAsEndpoint(sponsorKey);
    BlockchainKeypair gatewayKey = BlockchainKeyGenerator.getInstance().generate();
    txReqBuilder.signAsNode(gatewayKey);
    TransactionRequest txReq = txReqBuilder.buildRequest();
    return txReq;
}
Also used : TransactionRequestBuilder(com.jd.blockchain.ledger.TransactionRequestBuilder) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) TxBuilder(com.jd.blockchain.transaction.TxBuilder) ContractEventSendOperation(com.jd.blockchain.ledger.ContractEventSendOperation) HashAlgorithmUpdateOperation(com.jd.blockchain.ledger.HashAlgorithmUpdateOperation) ConsensusReconfigOperation(com.jd.blockchain.ledger.ConsensusReconfigOperation) ParticipantRegisterOperation(com.jd.blockchain.ledger.ParticipantRegisterOperation) DataAccountKVSetOperation(com.jd.blockchain.ledger.DataAccountKVSetOperation) ParticipantStateUpdateOperation(com.jd.blockchain.ledger.ParticipantStateUpdateOperation) UserRegisterOperation(com.jd.blockchain.ledger.UserRegisterOperation) ContractCodeDeployOperation(com.jd.blockchain.ledger.ContractCodeDeployOperation) Operation(com.jd.blockchain.ledger.Operation) DataAccountRegisterOperation(com.jd.blockchain.ledger.DataAccountRegisterOperation) ConsensusSettingsUpdateOperation(com.jd.blockchain.ledger.ConsensusSettingsUpdateOperation) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest)

Example 22 with BlockchainKeypair

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

the class TransactionSetTest method testSpecialCase_1.

/**
 * 根据实际运行中一个随机出现的错误中提取到的数据来建立的测试用例,可以更简化地验证正确性;
 *
 * <p>
 *
 * 注:重构了 {@link LedgerTransaction} 和 {@link TransactionContent}
 * 等交易结构相关的类型之后,此用例已经失效; by huanghaiquan on 2020-09-16;
 */
// @Test
public void testSpecialCase_1() {
    CryptoSetting defCryptoSetting = LedgerTestUtils.createDefaultCryptoSetting();
    MemoryKVStorage testStorage = new MemoryKVStorage();
    BufferedKVStorage bufferStorage = new BufferedKVStorage(null, testStorage, testStorage, false);
    // Create a new TransactionSet, it's empty;
    TransactionSetEditor txset = new TransactionSetEditor(defCryptoSetting, keyPrefix, bufferStorage, bufferStorage, LedgerDataStructure.MERKLE_TREE);
    assertTrue(txset.isUpdated());
    assertFalse(txset.isReadonly());
    assertNull(txset.getRootHash());
    HashDigest ledgerHash = Crypto.resolveAsHashDigest(Base58Utils.decode("j5iF5xJ7KN4kjRrhD3EUKVSPmHz2bExxp3h9avqxcnnzch"));
    assertEquals("j5iF5xJ7KN4kjRrhD3EUKVSPmHz2bExxp3h9avqxcnnzch", ledgerHash.toBase58());
    BlockchainKeypair parti0 = LedgerTestUtils.createKeyPair("7VeRLBwqTAz8oRazEazeaEfqei46sk2FzvBgyHMUBJvrUEGT", "7VeRUm27GbrsX9HbQSZguChLp24HZYub6s5FJ7FjBht8BmbA");
    BlockchainKeypair userKeypair1 = LedgerTestUtils.createKeyPair("7VeRKf3GFLFcBfzvtzmtyMXEoX2HYGEJ4j7CmHcnRV99W5Dp", "7VeRYQjeAaQY5Po8MMtmGNHA2SniqLXmJaZwBS5K8zTtMAU1");
    TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg_SHA256(userKeypair1, ledgerHash, 1580315317127L, parti0, parti0);
    // TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_UserReg(userKeypair1, ledgerHash, 202001202020L,
    // parti0, parti0);
    System.out.printf("\r\n ===||=== transactionRequest1.getTransactionHash()=[%s]\r\n", transactionRequest1.getTransactionHash().toBase58());
    // assertEquals("j5sXmpcomtM2QMUNWeQWsF8bNFFnyeXoCjVAekEeLSscgY", transactionRequest1.getTransactionHash().toBase58());
    assertEquals("j5wPGKT5CUzwi8j6VfCWaP2p9YZ6WVWtMANp9HbHWzvhgG", transactionRequest1.getTransactionHash().toBase58());
    TransactionStagedSnapshot txSnapshot = new TransactionStagedSnapshot();
    txSnapshot.setAdminAccountHash(Crypto.resolveAsHashDigest(Base58Utils.decode("j5taeK6cpmJGcn8QbEYCqadna6s7NDSheDTK6NJdU4mFhh")));
    txSnapshot.setUserAccountSetHash(Crypto.resolveAsHashDigest(Base58Utils.decode("j5oQDSob92mCoGSHtrXa9soqgAtMyjwfRMt2kj7igXXJrP")));
    TransactionResult tx = new TransactionResultData(transactionRequest1.getTransactionHash(), 1, TransactionState.SUCCESS, txSnapshot);
    txset.addTransaction(transactionRequest1, tx);
    LedgerTransaction tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
    assertNotNull(tx_query);
    txset.commit();
    bufferStorage.commit();
    tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
    TransactionState tx_state = txset.getState(transactionRequest1.getTransactionHash());
    assertNotNull(tx_query);
    assertEquals(0, tx_state.CODE);
    HashDigest txsetRootHash = txset.getRootHash();
    txset = new TransactionSetEditor(-1, txsetRootHash, defCryptoSetting, keyPrefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE, false);
    tx_query = txset.getTransaction(transactionRequest1.getTransactionHash());
    tx_state = txset.getState(transactionRequest1.getTransactionHash());
    assertNotNull(tx_query);
    assertEquals(0, tx_state.CODE);
}
Also used : TransactionState(com.jd.blockchain.ledger.TransactionState) TransactionStagedSnapshot(com.jd.blockchain.ledger.core.TransactionStagedSnapshot) TransactionResult(com.jd.blockchain.ledger.TransactionResult) CryptoSetting(com.jd.blockchain.ledger.CryptoSetting) BufferedKVStorage(com.jd.blockchain.storage.service.utils.BufferedKVStorage) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) TransactionSetEditor(com.jd.blockchain.ledger.core.TransactionSetEditor) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) TransactionResultData(com.jd.blockchain.ledger.core.TransactionResultData)

Example 23 with BlockchainKeypair

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

the class UserRoleDatasetTest method testAddUserRoles.

@Test
public void testAddUserRoles() {
    CryptoConfig cryptoConfig = new CryptoConfig();
    cryptoConfig.setAutoVerifyHash(true);
    cryptoConfig.setSupportedProviders(SUPPORTED_PROVIDERS);
    cryptoConfig.setHashAlgorithm(HASH_ALGORITHM);
    MemoryKVStorage testStorage = new MemoryKVStorage();
    String prefix = "user-roles/";
    UserRoleDatasetEditor userRolesDataset = new UserRoleDatasetEditor(cryptoConfig, prefix, testStorage, testStorage, LedgerDataStructure.MERKLE_TREE);
    BlockchainKeypair bckp = BlockchainKeyGenerator.getInstance().generate();
    String[] authRoles = { "DEFAULT", "MANAGER" };
    userRolesDataset.addUserRoles(bckp.getAddress(), RolesPolicy.UNION, authRoles);
    userRolesDataset.commit();
    assertEquals(1, userRolesDataset.getUserCount());
    UserRoles userRoles = userRolesDataset.getUserRoles(bckp.getAddress());
    assertNotNull(userRoles);
    String[] roles = userRoles.getRoles();
    assertEquals(2, roles.length);
    assertArrayEquals(authRoles, roles);
    assertEquals(RolesPolicy.UNION, userRoles.getPolicy());
}
Also used : UserRoleDatasetEditor(com.jd.blockchain.ledger.core.UserRoleDatasetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) UserRoles(com.jd.blockchain.ledger.UserRoles) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Test(org.junit.Test)

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