Search in sources :

Example 1 with BytesValue

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

the class PythonContractCode method doProcessEvent.

@Override
protected BytesValue doProcessEvent(Object contractInstance, ContractEventContext eventContext) {
    Context context = (Context) contractInstance;
    // 参数解析,仅支持 String/int/long/boolean/byte[]
    BytesValue[] values = eventContext.getArgs().getValues();
    Object[] args = new Object[values.length];
    for (int i = 0; i < values.length; i++) {
        switch(values[i].getType()) {
            case TEXT:
                args[i] = BytesUtils.toString(values[i].getBytes().toBytes());
                break;
            case INT32:
                args[i] = BytesUtils.toInt(values[i].getBytes().toBytes());
                break;
            case INT64:
                args[i] = BytesUtils.toLong(values[i].getBytes().toBytes());
                break;
            case BOOLEAN:
                args[i] = BytesUtils.toBoolean(values[i].getBytes().toBytes()[0]);
                break;
            default:
                args[i] = values[i].getBytes().toBytes();
                break;
        }
    }
    context.getBindings(LANG).putMember("args", args);
    Value result = context.eval(LANG, eventContext.getEvent() + "(*args)");
    // 解析返回值,仅支持String/int/long/boolean/byte[]
    if (result.isString()) {
        // String
        return TypedValue.fromText(result.asString());
    } else if (result.isNumber() && result.fitsInLong()) {
        // long
        return TypedValue.fromInt64(result.asLong());
    } else if (result.isNumber() && result.fitsInInt()) {
        // int
        return TypedValue.fromInt32(result.asInt());
    } else if (result.isBoolean()) {
        // boolean
        return TypedValue.fromBoolean(result.asBoolean());
    } else if (result.hasArrayElements()) {
        // byte[]
        return TypedValue.fromBytes(result.as(byte[].class));
    } else {
        return null;
    }
}
Also used : ContractEventContext(com.jd.blockchain.contract.ContractEventContext) Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) TypedValue(com.jd.blockchain.ledger.TypedValue) BytesValue(com.jd.blockchain.ledger.BytesValue) BytesValue(com.jd.blockchain.ledger.BytesValue)

Example 2 with BytesValue

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

the class JavaScriptContractCode method doProcessEvent.

@Override
protected BytesValue doProcessEvent(Object contractInstance, ContractEventContext eventContext) {
    Context context = (Context) contractInstance;
    // 参数解析,仅支持 String/int/long/boolean/byte[]
    BytesValue[] values = eventContext.getArgs().getValues();
    Object[] args = new Object[values.length];
    for (int i = 0; i < values.length; i++) {
        switch(values[i].getType()) {
            case TEXT:
                args[i] = BytesUtils.toString(values[i].getBytes().toBytes());
                break;
            case INT32:
                args[i] = BytesUtils.toInt(values[i].getBytes().toBytes());
                break;
            case INT64:
                args[i] = BytesUtils.toLong(values[i].getBytes().toBytes());
                break;
            case BOOLEAN:
                args[i] = BytesUtils.toBoolean(values[i].getBytes().toBytes()[0]);
                break;
            default:
                args[i] = values[i].getBytes().toBytes();
                break;
        }
    }
    context.getBindings("js").putMember("args", args);
    Value result = context.eval("js", eventContext.getEvent() + "(...args)");
    // 解析返回值,仅支持String/int/long/boolean/byte[]
    if (result.isString()) {
        // String
        return TypedValue.fromText(result.asString());
    } else if (result.isNumber() && result.fitsInLong()) {
        // long
        return TypedValue.fromInt64(result.asLong());
    } else if (result.isNumber() && result.fitsInInt()) {
        // int
        return TypedValue.fromInt32(result.asInt());
    } else if (result.isBoolean()) {
        // boolean
        return TypedValue.fromBoolean(result.asBoolean());
    } else if (result.hasArrayElements()) {
        // byte[]
        return TypedValue.fromBytes(result.as(byte[].class));
    } else {
        return null;
    }
}
Also used : ContractEventContext(com.jd.blockchain.contract.ContractEventContext) Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) TypedValue(com.jd.blockchain.ledger.TypedValue) BytesValue(com.jd.blockchain.ledger.BytesValue) BytesValue(com.jd.blockchain.ledger.BytesValue)

Example 3 with BytesValue

use of com.jd.blockchain.ledger.BytesValue 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);
}
Also used : DataAccount(com.jd.blockchain.ledger.core.DataAccount) TypedKVData(com.jd.blockchain.ledger.TypedKVData) BytesValue(com.jd.blockchain.ledger.BytesValue)

Example 4 with BytesValue

use of com.jd.blockchain.ledger.BytesValue 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;
}
Also used : DataAccount(com.jd.blockchain.ledger.core.DataAccount) TypedKVEntry(com.jd.blockchain.ledger.TypedKVEntry) TypedKVData(com.jd.blockchain.ledger.TypedKVData) BytesValue(com.jd.blockchain.ledger.BytesValue)

Example 5 with BytesValue

use of com.jd.blockchain.ledger.BytesValue 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);
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccount(com.jd.blockchain.ledger.core.DataAccount) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Aggregations

BytesValue (com.jd.blockchain.ledger.BytesValue)16 DataAccount (com.jd.blockchain.ledger.core.DataAccount)10 TypedKVData (com.jd.blockchain.ledger.TypedKVData)8 TypedKVEntry (com.jd.blockchain.ledger.TypedKVEntry)6 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)4 TypedValue (com.jd.blockchain.ledger.TypedValue)4 KVDataVO (com.jd.blockchain.ledger.KVDataVO)3 ArrayList (java.util.ArrayList)3 ContractEventContext (com.jd.blockchain.contract.ContractEventContext)2 HashDigest (com.jd.blockchain.crypto.HashDigest)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 DataAccountSet (com.jd.blockchain.ledger.core.DataAccountSet)2 DataChangedListener (com.jd.blockchain.ledger.core.DatasetHelper.DataChangedListener)2 TypeMapper (com.jd.blockchain.ledger.core.DatasetHelper.TypeMapper)2 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)2 LedgerManager (com.jd.blockchain.ledger.core.LedgerManager)2 LedgerQuery (com.jd.blockchain.ledger.core.LedgerQuery)2 LedgerRepository (com.jd.blockchain.ledger.core.LedgerRepository)2 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)2 Context (org.graalvm.polyglot.Context)2