Search in sources :

Example 1 with UserAccount

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

the class UserStateUpdateOperationHandle method doProcess.

@Override
protected void doProcess(UserStateUpdateOperation op, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.UPDATE_USER_STATE, MultiIDsPolicy.AT_LEAST_ONE);
    UserAccount user = transactionContext.getDataset().getUserAccountSet().getAccount(op.getUserAddress());
    if (null == user) {
        throw new UserDoesNotExistException(String.format("User doesn't exist! --[Address=%s]", op.getUserAddress()));
    }
    // REVOKE 状态不可再恢复
    if (user.getState() == AccountState.REVOKE) {
        throw new IllegalAccountStateException(String.format("Can not change user in REVOKE state! --[Address=%s]", op.getUserAddress()));
    }
    // 操作账本;
    ((UserAccountSetEditor) (transactionContext.getDataset().getUserAccountSet())).setState(op.getUserAddress(), op.getState());
}
Also used : UserAccountSetEditor(com.jd.blockchain.ledger.core.UserAccountSetEditor) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) IllegalAccountStateException(com.jd.blockchain.ledger.IllegalAccountStateException) UserDoesNotExistException(com.jd.blockchain.ledger.UserDoesNotExistException) UserAccount(com.jd.blockchain.ledger.core.UserAccount)

Example 2 with UserAccount

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

use of com.jd.blockchain.ledger.core.UserAccount 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 4 with UserAccount

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

the class LedgerManagerTest method testLedgerInit.

@Test
public void testLedgerInit() {
    // 创建账本初始化配置;
    LedgerInitSetting initSetting = createLedgerInitSetting();
    // 采用基于内存的 Storage;
    MemoryKVStorage storage = new MemoryKVStorage();
    // 新建账本;
    LedgerEditor ldgEdt = LedgerInitializer.createLedgerEditor(initSetting, storage);
    // 创建一个模拟的创世交易;
    TransactionRequest genesisTxReq = LedgerTestUtils.createLedgerInitTxRequest_SHA256(participants);
    // 记录交易,注册用户;
    LedgerTransactionContext txCtx = ldgEdt.newTransaction(genesisTxReq);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) txCtx.getDataset();
    BlockchainKeypair userKP = BlockchainKeyGenerator.getInstance().generate();
    UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
    userAccount.setProperty("Name", "孙悟空", -1);
    userAccount.setProperty("Age", "10000", -1);
    System.out.println("UserAddress=" + userAccount.getAddress());
    // 提交交易结果;
    TransactionResult tx = txCtx.commit(TransactionState.SUCCESS);
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    // 生成区块;
    LedgerBlock genesisBlock = ldgEdt.prepare();
    HashDigest ledgerHash = genesisBlock.getHash();
    assertEquals(0, genesisBlock.getHeight());
    assertNotNull(genesisBlock.getHash());
    assertNull(genesisBlock.getPreviousHash());
    // 创世区块的账本hash 为null;创世区块本身的哈希就代表了账本的哈希;
    assertNull(genesisBlock.getLedgerHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
    assertNull(genesisBlock.getLedgerHash());
    assertNotNull(genesisBlock.getHash());
    // 重新加载并校验结果;
    LedgerManager reloadLedgerManager = new LedgerManager();
    LedgerRepository reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    HashDigest genesisHash = reloadLedgerRepo.getBlockHash(0);
    assertEquals(ledgerHash, genesisHash);
    LedgerBlock latestBlock = reloadLedgerRepo.getLatestBlock();
    assertEquals(0, latestBlock.getHeight());
    assertEquals(ledgerHash, latestBlock.getHash());
    // 创世区块的账本hash 为null;创世区块本身的哈希就代表了账本的哈希;
    assertNull(latestBlock.getLedgerHash());
    LedgerEditor editor1 = reloadLedgerRepo.createNextBlock();
    CryptoSetting cryptoSetting = reloadLedgerRepo.getAdminInfo().getSettings().getCryptoSetting();
    TxBuilder txBuilder = new TxBuilder(ledgerHash, cryptoSetting.getHashAlgorithm());
    BlockchainKeypair dataKey = BlockchainKeyGenerator.getInstance().generate();
    txBuilder.dataAccounts().register(dataKey.getIdentity());
    TransactionRequestBuilder txReqBuilder = txBuilder.prepareRequest();
    DigitalSignature dgtsign = txReqBuilder.signAsEndpoint(userKP);
    TransactionRequest txRequest = txReqBuilder.buildRequest();
    LedgerTransactionContext txCtx1 = editor1.newTransaction(txRequest);
    ((DataAccountSetEditor) (txCtx1.getDataset().getDataAccountSet())).register(dataKey.getAddress(), dataKey.getPubKey(), null);
    txCtx1.commit(TransactionState.SUCCESS);
    LedgerBlock block1 = editor1.prepare();
    editor1.commit();
    assertEquals(1, block1.getHeight());
    assertNotNull(block1.getHash());
    assertEquals(genesisHash, block1.getPreviousHash());
    assertEquals(ledgerHash, block1.getLedgerHash());
    latestBlock = reloadLedgerRepo.getLatestBlock();
    assertEquals(1, latestBlock.getHeight());
    assertEquals(block1.getHash(), latestBlock.getHash());
    showStorageKeys(storage);
    reloadLedgerManager = new LedgerManager();
    reloadLedgerRepo = reloadLedgerManager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    latestBlock = reloadLedgerRepo.getLatestBlock();
    assertEquals(1, latestBlock.getHeight());
    assertEquals(block1.getHash(), latestBlock.getHash());
    DataAccountSet dataAccountSet = reloadLedgerRepo.getDataAccountSet(latestBlock);
    UserAccountSet userAccountSet = reloadLedgerRepo.getUserAccountSet(latestBlock);
    ContractAccountSet contractAccountSet = reloadLedgerRepo.getContractAccountSet(latestBlock);
}
Also used : LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) TxBuilder(com.jd.blockchain.transaction.TxBuilder) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccountSetEditor(com.jd.blockchain.ledger.core.DataAccountSetEditor) HashDigest(com.jd.blockchain.crypto.HashDigest) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet) ContractAccountSet(com.jd.blockchain.ledger.core.ContractAccountSet) UserAccount(com.jd.blockchain.ledger.core.UserAccount) UserAccountSet(com.jd.blockchain.ledger.core.UserAccountSet) Test(org.junit.Test)

Example 5 with UserAccount

use of com.jd.blockchain.ledger.core.UserAccount 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

UserAccount (com.jd.blockchain.ledger.core.UserAccount)11 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)9 HashDigest (com.jd.blockchain.crypto.HashDigest)8 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)6 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)6 Test (org.junit.Test)6 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)5 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)5 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)5 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)5 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)5 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)5 TransactionResponse (com.jd.blockchain.ledger.TransactionResponse)4 DefaultOperationHandleRegisteration (com.jd.blockchain.ledger.core.DefaultOperationHandleRegisteration)4 LedgerDataSet (com.jd.blockchain.ledger.core.LedgerDataSet)4 LedgerSecurityManager (com.jd.blockchain.ledger.core.LedgerSecurityManager)4 OperationHandleRegisteration (com.jd.blockchain.ledger.core.OperationHandleRegisteration)4 TransactionBatchProcessor (com.jd.blockchain.ledger.core.TransactionBatchProcessor)4 IllegalAccountStateException (com.jd.blockchain.ledger.IllegalAccountStateException)2 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)2