use of com.jd.blockchain.ledger.TypedValue in project jdchain-core by blockchain-jd-com.
the class MerkleAccountSetTest method testRegister.
@Test
public void testRegister() {
final OpeningAccessPolicy POLICY = new OpeningAccessPolicy();
final MemoryKVStorage STORAGE = new MemoryKVStorage();
Bytes KEY_PREFIX = Bytes.fromString("/ACCOUNT");
CryptoConfig cryptoConfig = new CryptoConfig();
cryptoConfig.setSupportedProviders(LedgerTestUtils.getContextProviders());
cryptoConfig.setAutoVerifyHash(true);
cryptoConfig.setHashAlgorithm(Crypto.getAlgorithm("SHA256"));
MerkleAccountSetEditor accountsetEditor = new MerkleAccountSetEditor(cryptoConfig, KEY_PREFIX, STORAGE, STORAGE, POLICY);
BlockchainKeypair key1 = BlockchainKeyGenerator.getInstance().generate();
accountsetEditor.register(key1.getIdentity());
accountsetEditor.commit();
CompositeAccount acc1 = accountsetEditor.getAccount(key1.getAddress());
assertNotNull(acc1);
assertEquals(0, accountsetEditor.getVersion(key1.getAddress()));
acc1.getDataset().setValue("K1", TypedValue.fromText("V0"), -1);
TypedValue v1 = acc1.getDataset().getValue("K1");
assertNotNull(v1);
assertEquals(0, acc1.getDataset().getVersion("K1"));
accountsetEditor.commit();
v1 = acc1.getDataset().getValue("K1");
assertNotNull(v1);
assertEquals(0, acc1.getDataset().getVersion("K1"));
}
use of com.jd.blockchain.ledger.TypedValue in project jdchain-core by blockchain-jd-com.
the class TransactionDecorator method initOperationResults.
private OperationResult[] initOperationResults(OperationResult[] opResults) {
OperationResult[] operationResults = null;
if (opResults != null && opResults.length > 0) {
operationResults = new OperationResult[opResults.length];
for (int i = 0; i < opResults.length; i++) {
OperationResult opResult = opResults[i];
TypedValue value = TypedValue.wrap(opResult.getResult());
operationResults[i] = new OperationResultData(opResult.getIndex(), value);
}
}
return operationResults;
}
use of com.jd.blockchain.ledger.TypedValue in project jdchain-core by blockchain-jd-com.
the class MerkleComplecatedAccount method initializeDatasets.
private void initializeDatasets(HashDigest headerRoot, HashDigest dataRoot, CryptoSetting cryptoSetting, Bytes keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly) {
// // 加载“根数据集”
// this.rootDataset = new MerkleHashDataset(rootHash, cryptoSetting, keyPrefix, exStorage, verStorage, readonly);
// 初始化数据修改监听器;
DataChangedListener<String, TypedValue> dataChangedListener = new DataChangedListener<String, TypedValue>() {
@Override
public void onChanged(String key, TypedValue value, long expectedVersion, long newVersion) {
onUpdated(key, value, expectedVersion, newVersion);
}
};
TypeMapper<byte[], TypedValue> valueMapper = new TypeMapper<byte[], TypedValue>() {
@Override
public byte[] encode(TypedValue t2) {
return BinaryProtocol.encode(t2, BytesValue.class);
}
@Override
public TypedValue decode(byte[] t1) {
BytesValue v = BinaryProtocol.decode(t1, BytesValue.class);
return TypedValue.wrap(v);
}
};
// 加载“头数据集”;
// HashDigest headerRoot = loadHeaderRoot();
Bytes headerPrefix = keyPrefix.concat(HEADER_PREFIX);
this.headerDataset = new MerkleHashDataset(headerRoot, cryptoSetting, headerPrefix, exStorage, verStorage, readonly);
this.typedHeader = DatasetHelper.listen(DatasetHelper.map(headerDataset, valueMapper), dataChangedListener);
// 加载“主数据集”
// HashDigest dataRoot = loadDataRoot();
Bytes dataPrefix = keyPrefix.concat(DATA_PREFIX);
this.dataDataset = new MerkleHashDataset(dataRoot, cryptoSetting, dataPrefix, exStorage, verStorage, readonly);
this.typedData = DatasetHelper.listen(DatasetHelper.map(dataDataset, valueMapper), dataChangedListener);
}
use of com.jd.blockchain.ledger.TypedValue in project jdchain-core by blockchain-jd-com.
the class KvComplecatedAccount method initializeDatasets.
private void initializeDatasets(HashDigest headerRoot, HashDigest dataRoot, CryptoSetting cryptoSetting, Bytes keyPrefix, ExPolicyKVStorage exStorage, VersioningKVStorage verStorage, boolean readonly) {
// 初始化数据修改监听器;
DataChangedListener<String, TypedValue> dataChangedListenerHeader = new DataChangedListener<String, TypedValue>() {
@Override
public void onChanged(String key, TypedValue value, long expectedVersion, long newVersion) {
onUpdated(key, value, "header", expectedVersion, newVersion);
}
};
DataChangedListener<String, TypedValue> dataChangedListenerData = new DataChangedListener<String, TypedValue>() {
@Override
public void onChanged(String key, TypedValue value, long expectedVersion, long newVersion) {
onUpdated(key, value, "data", expectedVersion, newVersion);
}
};
TypeMapper<byte[], TypedValue> valueMapper = new TypeMapper<byte[], TypedValue>() {
@Override
public byte[] encode(TypedValue t2) {
return BinaryProtocol.encode(t2, BytesValue.class);
}
@Override
public TypedValue decode(byte[] t1) {
BytesValue v = BinaryProtocol.decode(t1, BytesValue.class);
return TypedValue.wrap(v);
}
};
// 加载“头数据集”;
// HashDigest headerRoot = loadHeaderRoot();
Bytes headerPrefix = keyPrefix.concat(HEADER_PREFIX);
this.headerDataset = new KvDataset(preblockHeight, headerRoot, DatasetType.HDKVS, cryptoSetting, headerPrefix, exStorage, verStorage, readonly);
this.typedHeader = DatasetHelper.listen(DatasetHelper.map(headerDataset, valueMapper), dataChangedListenerHeader);
// 加载“主数据集”
// HashDigest dataRoot = loadDataRoot();
Bytes dataPrefix = keyPrefix.concat(DATA_PREFIX);
this.dataDataset = new KvDataset(preblockHeight, dataRoot, DatasetType.DTKVS, cryptoSetting, dataPrefix, exStorage, verStorage, readonly);
this.typedData = DatasetHelper.listen(DatasetHelper.map(dataDataset, valueMapper), dataChangedListenerData);
}
use of com.jd.blockchain.ledger.TypedValue in project jdchain-core by blockchain-jd-com.
the class EventPublishingAccount method getEvents.
@Override
public Event[] getEvents(String eventName, long fromSequence, int count) {
List<Event> events = new ArrayList<>();
Dataset<String, TypedValue> ds = mklAccount.getDataset();
long maxVersion = mklAccount.getDataset().getVersion(eventName) + 1;
for (int i = 0; i < count && i <= maxVersion; i++) {
TypedValue tv = ds.getValue(eventName, fromSequence + i);
if (null == tv || tv.isNil()) {
break;
}
Event event = BinaryProtocol.decode(tv.bytesValue());
events.add(new EventInfo(event));
}
return events.toArray(new Event[events.size()]);
}
Aggregations