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_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;
}
use of com.jd.blockchain.ledger.core.DataAccount in project jdchain-core by blockchain-jd-com.
the class TransactionBatchProcessorTest method testTxRollbackByVersionsConflict.
@Test
public void testTxRollbackByVersionsConflict() {
final MemoryKVStorage STORAGE = new MemoryKVStorage();
// 初始化账本到指定的存储库;
HashDigest ledgerHash = LedgerTestUtils.initLedger(STORAGE, parti0, parti1, parti2, parti3);
// 加载账本;
LedgerManager ledgerManager = new LedgerManager();
LedgerRepository ledgerRepo = ledgerManager.register(ledgerHash, STORAGE, LedgerDataStructure.MERKLE_TREE);
// 验证参与方账户的存在;
LedgerDataSet previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
UserAccount user0 = previousBlockDataset.getUserAccountSet().getAccount(parti0.getAddress());
assertNotNull(user0);
boolean partiRegistered = previousBlockDataset.getUserAccountSet().contains(parti0.getAddress());
assertTrue(partiRegistered);
// 注册数据账户;
// 生成新区块;
LedgerEditor newBlockEditor = ledgerRepo.createNextBlock();
OperationHandleRegisteration opReg = new DefaultOperationHandleRegisteration();
LedgerSecurityManager securityManager = getSecurityManager();
TransactionBatchProcessor txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
BlockchainKeypair dataAccountKeypair = BlockchainKeyGenerator.getInstance().generate();
TransactionRequest transactionRequest1 = LedgerTestUtils.createTxRequest_DataAccountReg_SHA256(dataAccountKeypair, ledgerHash, parti0, parti0);
TransactionResponse txResp1 = txbatchProcessor.schedule(transactionRequest1);
LedgerBlock newBlock = newBlockEditor.prepare();
newBlockEditor.commit();
assertEquals(TransactionState.SUCCESS, txResp1.getExecutionState());
DataAccount dataAccount = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress());
assertNotNull(dataAccount);
// 正确写入 KV 数据;
TransactionRequest txreq1 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-1", -1, ledgerHash, parti0, parti0);
TransactionRequest txreq2 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K2", "V-2-1", -1, ledgerHash, parti0, parti0);
TransactionRequest txreq3 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-1", -1, ledgerHash, parti0, parti0);
// 连续写 K1,K1的版本将变为1;
TransactionRequest txreq4 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-2", 0, ledgerHash, parti0, parti0);
newBlockEditor = ledgerRepo.createNextBlock();
previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
txbatchProcessor.schedule(txreq1);
txbatchProcessor.schedule(txreq2);
txbatchProcessor.schedule(txreq3);
txbatchProcessor.schedule(txreq4);
newBlock = newBlockEditor.prepare();
newBlockEditor.commit();
BytesValue v1_0 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 0);
BytesValue v1_1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1", 1);
BytesValue v2 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K2", 0);
BytesValue v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3", 0);
assertNotNull(v1_0);
assertNotNull(v1_1);
assertNotNull(v2);
assertNotNull(v3);
assertEquals("V-1-1", v1_0.getBytes().toUTF8String());
assertEquals("V-1-2", v1_1.getBytes().toUTF8String());
assertEquals("V-2-1", v2.getBytes().toUTF8String());
assertEquals("V-3-1", v3.getBytes().toUTF8String());
// 提交多笔数据写入的交易,包含存在数据版本冲突的交易,验证交易是否正确回滚;
// 先写一笔正确的交易; k3 的版本将变为 1 ;
TransactionRequest txreq5 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K3", "V-3-2", 0, ledgerHash, parti0, parti0);
// 指定冲突的版本号,正确的应该是版本1;
TransactionRequest txreq6 = LedgerTestUtils.createTxRequest_DataAccountWrite_SHA256(dataAccountKeypair.getAddress(), "K1", "V-1-3", 0, ledgerHash, parti0, parti0);
newBlockEditor = ledgerRepo.createNextBlock();
previousBlockDataset = ledgerRepo.getLedgerDataSet(ledgerRepo.getLatestBlock());
txbatchProcessor = new TransactionBatchProcessor(securityManager, newBlockEditor, ledgerRepo, opReg);
txbatchProcessor.schedule(txreq5);
// 预期会产生版本冲突异常; DataVersionConflictionException;
DataVersionConflictException versionConflictionException = null;
try {
txbatchProcessor.schedule(txreq6);
} catch (DataVersionConflictException e) {
versionConflictionException = e;
}
// assertNotNull(versionConflictionException);
newBlock = newBlockEditor.prepare();
newBlockEditor.commit();
BytesValue v1 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K1");
v3 = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getValue("K3");
// k1 的版本仍然为1,没有更新;
long k1_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K1");
assertEquals(1, k1_version);
long k3_version = ledgerRepo.getDataAccountSet().getAccount(dataAccountKeypair.getAddress()).getDataset().getVersion("K3");
assertEquals(1, k3_version);
assertNotNull(v1);
assertNotNull(v3);
assertEquals("V-1-2", v1.getBytes().toUTF8String());
assertEquals("V-3-2", v3.getBytes().toUTF8String());
// // 验证正确性;
// ledgerManager = new LedgerManager();
// ledgerRepo = ledgerManager.register(ledgerHash, STORAGE);
//
// LedgerBlock latestBlock = ledgerRepo.getLatestBlock();
// assertEquals(newBlock.getHash(), latestBlock.getHash());
// assertEquals(1, newBlock.getHeight());
//
// LedgerTransaction tx1 = ledgerRepo.getTransactionSet()
// .get(transactionRequest1.getTransactionHash());
//
// assertNotNull(tx1);
// assertEquals(TransactionState.SUCCESS, tx1.getExecutionState());
}
use of com.jd.blockchain.ledger.core.DataAccount in project jdchain-core by blockchain-jd-com.
the class DataAccountKVSetOperationHandle method doProcess.
@Override
protected void doProcess(DataAccountKVSetOperation kvWriteOp, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
// 权限校验;
SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
securityPolicy.checkEndpointPermission(LedgerPermission.WRITE_DATA_ACCOUNT, MultiIDsPolicy.AT_LEAST_ONE);
// 操作账本;
DataAccount account = transactionContext.getDataset().getDataAccountSet().getAccount(kvWriteOp.getAccountAddress());
if (account == null) {
throw new DataAccountDoesNotExistException(String.format("Data account doesn't exist! --[Address=%s]", kvWriteOp.getAccountAddress()));
}
// 写权限校验
securityPolicy.checkDataPermission(account.getPermission(), DataPermissionType.WRITE);
KVWriteEntry[] writeSet = kvWriteOp.getWriteSet();
long v = -1L;
for (KVWriteEntry kvw : writeSet) {
v = account.getDataset().setValue(kvw.getKey(), TypedValue.wrap(kvw.getValue()), kvw.getExpectedVersion());
if (v < 0) {
throw new DataVersionConflictException();
}
}
}
use of com.jd.blockchain.ledger.core.DataAccount in project jdchain-core by blockchain-jd-com.
the class UncommittedLedgerQueryService method getDataEntries.
@Override
public TypedKVEntry[] getDataEntries(String address, String... keys) {
DataAccount account = transactionContext.getDataset().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 UncommittedLedgerQueryService method getDataEntry.
@Override
public TypedKVEntry getDataEntry(String address, String key, long version) {
DataAccount account = transactionContext.getDataset().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);
}
Aggregations