Search in sources :

Example 1 with DataAccountSet

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

the class ContractLedgerQueryService method getDataEntries.

@Override
public TypedKVEntry[] getDataEntries(String address, int fromIndex, int count) {
    DataAccountSet dataAccountSet = ledgerQuery.getDataAccountSet();
    DataAccount dataAccount = dataAccountSet.getAccount(Bytes.fromBase58(address));
    QueryArgs queryArgs = QueryUtils.calFromIndexAndCount(fromIndex, count, (int) dataAccount.getDataset().getDataCount());
    SkippingIterator<DataEntry<String, TypedValue>> iterator = ((IteratorDataset) dataAccount.getDataset()).kvIterator();
    iterator.skip(queryArgs.getFrom());
    TypedKVEntry[] typedKVEntries = iterator.next(queryArgs.getCount(), TypedKVEntry.class, entry -> new TypedKVData(entry.getKey(), entry.getVersion(), entry.getValue()));
    return typedKVEntries;
}
Also used : DataAccount(com.jd.blockchain.ledger.core.DataAccount) DataEntry(utils.DataEntry) TypedKVEntry(com.jd.blockchain.ledger.TypedKVEntry) TypedKVData(com.jd.blockchain.ledger.TypedKVData) QueryArgs(utils.query.QueryArgs) IteratorDataset(com.jd.blockchain.ledger.core.IteratorDataset) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet)

Example 2 with DataAccountSet

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

the class UncommittedLedgerQueryService method getDataAccounts.

@Override
public BlockchainIdentity[] getDataAccounts(int fromIndex, int count) {
    DataAccountSet dataAccountSet = transactionContext.getDataset().getDataAccountSet();
    QueryArgs queryArgs = QueryUtils.calFromIndexAndCountDescend(fromIndex, count, (int) dataAccountSet.getTotal());
    SkippingIterator<BlockchainIdentity> it = dataAccountSet.identityIterator();
    it.skip(queryArgs.getFrom());
    return it.next(queryArgs.getCount(), BlockchainIdentity.class);
}
Also used : QueryArgs(utils.query.QueryArgs) BlockchainIdentity(com.jd.blockchain.ledger.BlockchainIdentity) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet)

Example 3 with DataAccountSet

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

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

the class LedgerQueryController method getDataEntries.

@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, path = GET_LATEST_KV_LIST)
@Override
public TypedKVEntry[] getDataEntries(@PathVariable(name = "ledgerHash") HashDigest ledgerHash, @PathVariable(name = "address") String address, @RequestParam("keys") String... keys) {
    if (keys == null || keys.length == 0) {
        return null;
    }
    LedgerQuery ledger = ledgerService.getLedger(ledgerHash);
    LedgerBlock block = ledger.getLatestBlock();
    DataAccountSet dataAccountSet = ledger.getDataAccountSet(block);
    DataAccount dataAccount = dataAccountSet.getAccount(Bytes.fromBase58(address));
    if (dataAccount == null) {
        return null;
    }
    TypedKVEntry[] entries = new TypedKVEntry[keys.length];
    long ver;
    for (int i = 0; i < entries.length; i++) {
        ver = dataAccount.getDataset().getVersion(keys[i]);
        if (ver < 0) {
            entries[i] = new TypedKVData(keys[i], -1, null);
        } else {
            BytesValue value = dataAccount.getDataset().getValue(keys[i], ver);
            entries[i] = new TypedKVData(keys[i], ver, value);
        }
    }
    return entries;
}
Also used : DataAccount(com.jd.blockchain.ledger.core.DataAccount) TypedKVEntry(com.jd.blockchain.ledger.TypedKVEntry) TypedKVData(com.jd.blockchain.ledger.TypedKVData) LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerQuery(com.jd.blockchain.ledger.core.LedgerQuery) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet) BytesValue(com.jd.blockchain.ledger.BytesValue) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with DataAccountSet

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

the class LedgerQueryController method getDataEntries.

@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, path = GET_KV_VERSION_LIST)
@Override
public TypedKVEntry[] getDataEntries(@PathVariable(name = "ledgerHash") HashDigest ledgerHash, @PathVariable(name = "address") String address, @RequestBody KVInfoVO kvInfoVO) {
    // parse kvInfoVO;
    List<String> keyList = new ArrayList<>();
    List<Long> versionList = new ArrayList<>();
    if (kvInfoVO != null) {
        for (KVDataVO kvDataVO : kvInfoVO.getData()) {
            for (Long version : kvDataVO.getVersion()) {
                keyList.add(kvDataVO.getKey());
                versionList.add(version);
            }
        }
    }
    String[] keys = keyList.toArray(new String[keyList.size()]);
    Long[] versions = versionList.toArray(new Long[versionList.size()]);
    if (keys == null || keys.length == 0) {
        return null;
    }
    if (versions == null || versions.length == 0) {
        return null;
    }
    if (keys.length != versions.length) {
        throw new ContractException("keys.length!=versions.length!");
    }
    LedgerQuery ledger = ledgerService.getLedger(ledgerHash);
    LedgerBlock block = ledger.getLatestBlock();
    DataAccountSet dataAccountSet = ledger.getDataAccountSet(block);
    DataAccount dataAccount = dataAccountSet.getAccount(Bytes.fromBase58(address));
    if (dataAccount == null) {
        return null;
    }
    TypedKVEntry[] entries = new TypedKVEntry[keys.length];
    long ver = -1;
    for (int i = 0; i < entries.length; i++) {
        // ver = dataAccount.getDataVersion(Bytes.fromString(keys[i]));
        ver = versions[i];
        if (ver < 0) {
            entries[i] = new TypedKVData(keys[i], -1, null);
        } else {
            if (dataAccount.getDataset().getDataCount() == 0 || dataAccount.getDataset().getValue(keys[i], ver) == null) {
                // is the address is not exist; the result is null;
                entries[i] = new TypedKVData(keys[i], -1, null);
            } else {
                BytesValue value = dataAccount.getDataset().getValue(keys[i], ver);
                entries[i] = new TypedKVData(keys[i], ver, value);
            }
        }
    }
    return entries;
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerQuery(com.jd.blockchain.ledger.core.LedgerQuery) ContractException(com.jd.blockchain.contract.ContractException) ArrayList(java.util.ArrayList) KVDataVO(com.jd.blockchain.ledger.KVDataVO) BytesValue(com.jd.blockchain.ledger.BytesValue) DataAccount(com.jd.blockchain.ledger.core.DataAccount) TypedKVEntry(com.jd.blockchain.ledger.TypedKVEntry) TypedKVData(com.jd.blockchain.ledger.TypedKVData) DataAccountSet(com.jd.blockchain.ledger.core.DataAccountSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DataAccountSet (com.jd.blockchain.ledger.core.DataAccountSet)14 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)9 LedgerQuery (com.jd.blockchain.ledger.core.LedgerQuery)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 DataAccount (com.jd.blockchain.ledger.core.DataAccount)6 QueryArgs (utils.query.QueryArgs)6 TypedKVData (com.jd.blockchain.ledger.TypedKVData)5 TypedKVEntry (com.jd.blockchain.ledger.TypedKVEntry)5 BlockchainIdentity (com.jd.blockchain.ledger.BlockchainIdentity)3 IteratorDataset (com.jd.blockchain.ledger.core.IteratorDataset)3 DataEntry (utils.DataEntry)3 BytesValue (com.jd.blockchain.ledger.BytesValue)2 ContractException (com.jd.blockchain.contract.ContractException)1 HashDigest (com.jd.blockchain.crypto.HashDigest)1 KVDataVO (com.jd.blockchain.ledger.KVDataVO)1 TypedValue (com.jd.blockchain.ledger.TypedValue)1 ContractAccountSet (com.jd.blockchain.ledger.core.ContractAccountSet)1 DataAccountSetEditor (com.jd.blockchain.ledger.core.DataAccountSetEditor)1 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)1 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)1