Search in sources :

Example 1 with TransactionPayload

use of org.starcoin.types.TransactionPayload in project starcoin-search by starcoinorg.

the class ElasticSearchHandler method buildTransferRequest.

private List<IndexRequest> buildTransferRequest(Transaction transaction, String indexName) {
    List<IndexRequest> requests = new ArrayList<>();
    UserTransaction userTransaction = transaction.getUserTransaction();
    if (userTransaction == null) {
        return requests;
    }
    RawTransaction rawTransaction = userTransaction.getRawTransaction();
    if (rawTransaction == null) {
        return requests;
    }
    TransactionPayload payload = rawTransaction.getTransactionPayload();
    if (!(payload.getClass() == TransactionPayload.ScriptFunction.class)) {
        // todo script and package handle
        logger.warn("other type must handle in future: {}", payload.getClass());
        return requests;
    }
    org.starcoin.types.ScriptFunction function = ((TransactionPayload.ScriptFunction) payload).value;
    String functionName = function.function.value;
    if (functionName.equals("peer_to_peer") || functionName.equals("peer_to_peer_v2")) {
        Transfer transfer = new Transfer();
        transfer.setTxnHash(transaction.getTransactionHash());
        transfer.setSender(rawTransaction.getSender());
        transfer.setTimestamp(transaction.getTimestamp());
        transfer.setIdentifier(functionName);
        transfer.setReceiver(Hex.encode(function.args.get(0)));
        String amount = Hex.encode(function.args.get(1));
        if (functionName.equals("peer_to_peer")) {
            amount = Hex.encode(function.args.get(2));
        }
        transfer.setAmount(amount);
        transfer.setTypeTag(getTypeTags(function.ty_args));
        IndexRequest request = new IndexRequest(indexName);
        request.source(JSON.toJSONString(transfer), XContentType.JSON);
        requests.add(request);
        return requests;
    } else if (functionName.equals("batch_peer_to_peer") || functionName.equals("batch_peer_to_peer_v2")) {
        // batch handle
        Bytes addresses = function.args.get(0);
        byte[] addressBytes = addresses.content();
        byte[] amountBytes;
        if (functionName.equals("batch_peer_to_peer_v2")) {
            amountBytes = function.args.get(1).content();
        } else {
            amountBytes = function.args.get(2).content();
        }
        int size = addressBytes.length / AccountAddress.LENGTH;
        for (int i = 0; i < size; i++) {
            byte[] addressByte = Arrays.copyOfRange(addressBytes, i * AccountAddress.LENGTH, (i + 1) * AccountAddress.LENGTH);
            AccountAddress address = AccountAddress.valueOf(addressByte);
            byte[] amountByte = Arrays.copyOfRange(amountBytes, i * AccountAddress.LENGTH, (i + 1) * AccountAddress.LENGTH);
            String amount = Hex.encode(amountByte);
            Transfer transfer = new Transfer();
            transfer.setTxnHash(transaction.getTransactionHash());
            transfer.setSender(rawTransaction.getSender());
            transfer.setTimestamp(transaction.getTimestamp());
            transfer.setIdentifier(functionName);
            transfer.setReceiver(address.toString());
            transfer.setAmount(amount);
            transfer.setTypeTag(getTypeTags(function.ty_args));
            IndexRequest request = new IndexRequest(indexName);
            request.source(JSON.toJSONString(transfer), XContentType.JSON);
            requests.add(request);
        }
        logger.info("batch transfer handle: {}", size);
    } else {
        logger.warn("other scripts not support: {}", function.function.value);
    }
    return requests;
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) AccountAddress(org.starcoin.types.AccountAddress) Bytes(com.novi.serde.Bytes) TransactionPayload(org.starcoin.types.TransactionPayload)

Example 2 with TransactionPayload

use of org.starcoin.types.TransactionPayload in project starcoin-search by starcoinorg.

the class ElasticSearchHandler method bulkAddPayload.

public void bulkAddPayload(String payloadIndex, List<Transaction> transactionList, ObjectMapper objectMapper, List<SwapTransaction> swapTransactionList) throws IOException, DeserializationError {
    BulkRequest bulkRequest = new BulkRequest();
    List<org.starcoin.bean.TransactionPayload> transactionPayloadList = new ArrayList<>();
    for (Transaction transaction : transactionList) {
        if (transaction.getUserTransaction() != null) {
            RawTransaction rawTransaction = transaction.getUserTransaction().getRawTransaction();
            String payload = rawTransaction.getPayload();
            TransactionPayload transactionPayload = TransactionPayload.bcsDeserialize(Hex.decode(payload));
            TransactionPayloadInfo payloadInfo = new TransactionPayloadInfo(transactionPayload, transaction.getTimestamp(), transaction.getTransactionHash());
            // swap txn filter
            if (transactionPayload instanceof TransactionPayload.ScriptFunction) {
                TransactionPayload.ScriptFunction scriptFunctionPayload = (TransactionPayload.ScriptFunction) transactionPayload;
                String function = scriptFunctionPayload.value.function.toString();
                if (SwapType.isSwapType(function)) {
                    // todo add farm swap function
                    if (scriptFunctionPayload.value.ty_args.size() > 1 && scriptFunctionPayload.value.args.size() > 1) {
                        // parse token and amount
                        String tokenA = StructTagUtil.structTagToString(((org.starcoin.types.TypeTag.Struct) scriptFunctionPayload.value.ty_args.get(0)).value);
                        String tokenB = StructTagUtil.structTagToString(((org.starcoin.types.TypeTag.Struct) scriptFunctionPayload.value.ty_args.get(1)).value);
                        BigInteger argFirst = ServiceUtils.deserializeU128(scriptFunctionPayload.value.args.get(0));
                        BigInteger argSecond = ServiceUtils.deserializeU128(scriptFunctionPayload.value.args.get(1));
                        SwapTransaction swapTransaction = new SwapTransaction();
                        swapTransaction.setTransactionHash(transaction.getTransactionHash());
                        swapTransaction.setTimestamp(transaction.getTimestamp());
                        swapTransaction.setAccount(rawTransaction.getSender());
                        swapTransaction.setTokenA(tokenA);
                        swapTransaction.setTokenB(tokenB);
                        swapTransaction.setAmountA(new BigDecimal(argFirst));
                        swapTransaction.setAmountB(new BigDecimal(argSecond));
                        swapTransaction.setSwapType(SwapType.fromValue(function));
                        swapTransactionList.add(swapTransaction);
                    } else {
                        logger.warn("swap txn args error: {}", transaction.getTransactionHash());
                    }
                }
            }
            IndexRequest request = new IndexRequest(payloadIndex);
            request.id(transaction.getTransactionHash()).source(objectMapper.writeValueAsString(payloadInfo), XContentType.JSON);
            bulkRequest.add(request);
            org.starcoin.bean.TransactionPayload payload1 = new org.starcoin.bean.TransactionPayload();
            payload1.setTransactionHash(transaction.getTransactionHash());
            payload1.setJson(ServiceUtils.getJsonString(transactionPayload));
            transactionPayloadList.add(payload1);
        }
    }
    BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    logger.info("bulk txn payload result: {}", response.buildFailureMessage());
    if (!transactionPayloadList.isEmpty()) {
        transactionPayloadService.savePayload(transactionPayloadList);
        logger.info("save txn payload to pg ok: {}", transactionPayloadList.size());
    }
}
Also used : TransactionPayloadInfo(org.starcoin.bean.TransactionPayloadInfo) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) BigDecimal(java.math.BigDecimal) SwapTransaction(org.starcoin.bean.SwapTransaction) SwapTransaction(org.starcoin.bean.SwapTransaction) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BigInteger(java.math.BigInteger) TransactionPayload(org.starcoin.types.TransactionPayload) org.starcoin.bean(org.starcoin.bean) STCTypeTag(org.starcoin.api.TokenContractRPCClient.STCTypeTag)

Example 3 with TransactionPayload

use of org.starcoin.types.TransactionPayload in project starcoin-search by starcoinorg.

the class ServiceUtils method addBlockToList.

static void addBlockToList(TransactionRPCClient transactionRPCClient, List<Block> blockList, Block block) throws JSONRPC2SessionException {
    List<Transaction> transactionList = transactionRPCClient.getBlockTransactions(block.getHeader().getBlockHash());
    if (transactionList == null) {
        return;
    }
    for (Transaction transaction : transactionList) {
        BlockMetadata metadata;
        Transaction userTransaction = transactionRPCClient.getTransactionByHash(transaction.getTransactionHash());
        if (userTransaction != null) {
            UserTransaction inner = userTransaction.getUserTransaction();
            metadata = userTransaction.getBlockMetadata();
            if (metadata != null) {
                transaction.setBlockMetadata(metadata);
                block.setBlockMetadata(metadata);
            }
            if (inner != null) {
                try {
                    RawTransaction rawTransaction = inner.getRawTransaction();
                    TransactionPayload payload = TransactionPayload.bcsDeserialize(Hex.decode(rawTransaction.getPayload()));
                    if (TransactionPayload.Script.class.equals(payload.getClass())) {
                        transaction.setTransactionType(TransactionType.Script);
                    } else if (TransactionPayload.Package.class.equals(payload.getClass())) {
                        transaction.setTransactionType(TransactionType.Package);
                    } else if (TransactionPayload.ScriptFunction.class.equals(payload.getClass())) {
                        transaction.setTransactionType(TransactionType.ScriptFunction);
                    } else {
                        logger.warn("payload class error: {}", payload.getClass());
                    }
                    rawTransaction.setTransactionPayload(payload);
                    inner.setRawTransaction(rawTransaction);
                    transaction.setUserTransaction(inner);
                } catch (DeserializationError deserializationError) {
                    logger.error("Deserialization payload error:", deserializationError);
                }
            }
        } else {
            logger.warn("get txn by hash is null: {}", transaction.getTransactionHash());
        }
        transaction.setTimestamp(block.getHeader().getTimestamp());
        // set events
        List<Event> events = transactionRPCClient.getTransactionEvents(transaction.getTransactionHash());
        if (events != null && (!events.isEmpty())) {
            transaction.setEvents(events);
        } else {
            logger.warn("current txn event is null: {}", transaction.getTransactionHash());
        }
    }
    block.setTransactionList(transactionList);
    blockList.add(block);
}
Also used : DeserializationError(com.novi.serde.DeserializationError) TransactionPayload(org.starcoin.types.TransactionPayload)

Aggregations

TransactionPayload (org.starcoin.types.TransactionPayload)3 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 Bytes (com.novi.serde.Bytes)1 DeserializationError (com.novi.serde.DeserializationError)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 STCTypeTag (org.starcoin.api.TokenContractRPCClient.STCTypeTag)1 org.starcoin.bean (org.starcoin.bean)1 SwapTransaction (org.starcoin.bean.SwapTransaction)1 TransactionPayloadInfo (org.starcoin.bean.TransactionPayloadInfo)1 AccountAddress (org.starcoin.types.AccountAddress)1