Search in sources :

Example 1 with TypedValue

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"));
}
Also used : Bytes(utils.Bytes) MerkleAccountSetEditor(com.jd.blockchain.ledger.core.MerkleAccountSetEditor) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) OpeningAccessPolicy(com.jd.blockchain.ledger.core.OpeningAccessPolicy) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) CompositeAccount(com.jd.blockchain.ledger.core.CompositeAccount) TypedValue(com.jd.blockchain.ledger.TypedValue) Test(org.junit.Test)

Example 2 with TypedValue

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;
}
Also used : OperationResult(com.jd.blockchain.ledger.OperationResult) OperationResultData(com.jd.blockchain.ledger.OperationResultData) TypedValue(com.jd.blockchain.ledger.TypedValue)

Example 3 with TypedValue

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);
}
Also used : TypeMapper(com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper) Bytes(utils.Bytes) DataChangedListener(com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener) BytesValue(com.jd.blockchain.ledger.BytesValue) TypedValue(com.jd.blockchain.ledger.TypedValue)

Example 4 with TypedValue

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);
}
Also used : TypeMapper(com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper) Bytes(utils.Bytes) DataChangedListener(com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener) BytesValue(com.jd.blockchain.ledger.BytesValue) TypedValue(com.jd.blockchain.ledger.TypedValue)

Example 5 with TypedValue

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()]);
}
Also used : EventInfo(com.jd.blockchain.ledger.EventInfo) ArrayList(java.util.ArrayList) Event(com.jd.blockchain.ledger.Event) TypedValue(com.jd.blockchain.ledger.TypedValue)

Aggregations

TypedValue (com.jd.blockchain.ledger.TypedValue)7 Bytes (utils.Bytes)3 BytesValue (com.jd.blockchain.ledger.BytesValue)2 DataChangedListener (com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener)2 TypeMapper (com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)1 Event (com.jd.blockchain.ledger.Event)1 EventInfo (com.jd.blockchain.ledger.EventInfo)1 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)1 LedgerException (com.jd.blockchain.ledger.LedgerException)1 OperationResult (com.jd.blockchain.ledger.OperationResult)1 OperationResultData (com.jd.blockchain.ledger.OperationResultData)1 TypedKVData (com.jd.blockchain.ledger.TypedKVData)1 TypedKVEntry (com.jd.blockchain.ledger.TypedKVEntry)1 CompositeAccount (com.jd.blockchain.ledger.core.CompositeAccount)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 DataAccount (com.jd.blockchain.ledger.core.DataAccount)1 DataAccountSet (com.jd.blockchain.ledger.core.DataAccountSet)1 IteratorDataset (com.jd.blockchain.ledger.core.IteratorDataset)1 LedgerQuery (com.jd.blockchain.ledger.core.LedgerQuery)1