use of com.jd.blockchain.ledger.core.DataAccount 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;
}
use of com.jd.blockchain.ledger.core.DataAccount in project jdchain-core by blockchain-jd-com.
the class ContractLedgerQueryService method getDataEntry.
@Override
public TypedKVEntry getDataEntry(String address, String key, long version) {
DataAccount account = ledgerQuery.getDataAccountSet().getAccount(address);
if (null == account) {
return null;
}
BytesValue value = account.getDataset().getValue(key, version);
if (null == value) {
return null;
}
return new TypedKVData(key, version, value);
}
use of com.jd.blockchain.ledger.core.DataAccount in project jdchain-core by blockchain-jd-com.
the class ContractLedgerQueryService method getDataEntries.
@Override
public TypedKVEntry[] getDataEntries(String address, String... keys) {
DataAccount account = ledgerQuery.getDataAccountSet().getAccount(address);
TypedKVEntry[] entries = new TypedKVEntry[keys.length];
long ver;
for (int i = 0; i < entries.length; i++) {
final String currKey = keys[i];
ver = account == null ? -1 : account.getDataset().getVersion(currKey);
if (ver < 0) {
entries[i] = new TypedKVData(currKey, -1, null);
} else {
BytesValue value = account.getDataset().getValue(currKey, ver);
entries[i] = new TypedKVData(currKey, ver, value);
}
}
return entries;
}
use of com.jd.blockchain.ledger.core.DataAccount 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);
}
use of com.jd.blockchain.ledger.core.DataAccount 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;
}
Aggregations