Search in sources :

Example 1 with DeserializationError

use of com.novi.serde.DeserializationError in project starcoin-search by starcoinorg.

the class TransactionService method getProposalEvents.

public Result<Event> getProposalEvents(String network, String eventAddress) throws IOException {
    SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.TRANSACTION_EVENT_INDEX));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(ELASTICSEARCH_MAX_HITS);
    searchSourceBuilder.from(0);
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.filter(QueryBuilders.matchQuery("tag_name", ServiceUtils.proposalCreatedEvent));
    boolQuery.must(QueryBuilders.rangeQuery("transaction_index").gt(0));
    searchSourceBuilder.query(boolQuery);
    searchRequest.source(searchSourceBuilder);
    searchSourceBuilder.trackTotalHits(true);
    searchSourceBuilder.sort("timestamp", SortOrder.DESC);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    Result<Event> events = getSearchUnescapeResult(searchResponse, Event.class);
    List<Event> proposalEvents = new ArrayList<>();
    byte[] addressBytes = ByteUtils.hexToByteArray(eventAddress);
    AccountAddress proposer = null;
    try {
        proposer = AccountAddress.bcsDeserialize(addressBytes);
    } catch (DeserializationError deserializationError) {
        deserializationError.printStackTrace();
    }
    for (Event event : events.getContents()) {
        byte[] proposalBytes = ByteUtils.hexToByteArray(event.getData());
        try {
            ProposalCreatedEvent payload = ProposalCreatedEvent.bcsDeserialize(proposalBytes);
            if (payload.proposer.equals(proposer)) {
                proposalEvents.add(event);
            }
        } catch (DeserializationError deserializationError) {
            deserializationError.printStackTrace();
        }
    }
    events.setContents(proposalEvents);
    events.setTotal(proposalEvents.size());
    return events;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) DeserializationError(com.novi.serde.DeserializationError) ProposalCreatedEvent(org.starcoin.types.event.ProposalCreatedEvent) ProposalCreatedEvent(org.starcoin.types.event.ProposalCreatedEvent) AccountAddress(org.starcoin.types.AccountAddress) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with DeserializationError

use of com.novi.serde.DeserializationError in project starcoin-search by starcoinorg.

the class ElasticSearchHandler method addToHolders.

private void addToHolders(Event event, String tagName, String tagModule, String blockAuthor, Set<AddressHolder> holders, String eventAddress) {
    byte[] bytes = Hex.decode(event.getData());
    try {
        if (tagName.equalsIgnoreCase(Constant.DEPOSIT_EVENT)) {
            DepositEvent inner = DepositEvent.bcsDeserialize(bytes);
            String sb = inner.token_code.address + "::" + inner.token_code.module + "::" + inner.token_code.name;
            holders.add(new AddressHolder(eventAddress, sb));
            tokenCodeList.add(inner.token_code);
        } else if (tagName.equalsIgnoreCase(Constant.WITHDRAW_EVENT)) {
            if (tagModule.equalsIgnoreCase(Constant.EVENT_TREASURY_MODULE)) {
                holders.add(new AddressHolder(blockAuthor, STCTypeTag));
            } else {
                WithdrawEvent inner = WithdrawEvent.bcsDeserialize(bytes);
                String sb = inner.token_code.address + "::" + inner.token_code.module + "::" + inner.token_code.name;
                holders.add(new AddressHolder(eventAddress, sb));
                tokenCodeList.add(inner.token_code);
            }
        }
    } catch (DeserializationError deserializationError) {
        logger.error("decode event data error:{}", event, deserializationError);
    }
}
Also used : DeserializationError(com.novi.serde.DeserializationError) WithdrawEvent(org.starcoin.types.event.WithdrawEvent) DepositEvent(org.starcoin.types.event.DepositEvent)

Example 3 with DeserializationError

use of com.novi.serde.DeserializationError in project starcoin-search by starcoinorg.

the class HolderHistoryHandle method addHolders.

private void addHolders(Set<ElasticSearchHandler.AddressHolder> holders, Result<EventFull> result) {
    if (result == null) {
        return;
    }
    List<EventFull> eventFullList = result.getContents();
    for (EventFull event : eventFullList) {
        try {
            DepositEvent inner = DepositEvent.bcsDeserialize(Hex.decode(event.getData()));
            String sb = inner.token_code.address + "::" + inner.token_code.module + "::" + inner.token_code.name;
            holders.add(new ElasticSearchHandler.AddressHolder(event.getEventAddress(), sb));
        } catch (DeserializationError deserializationError) {
            logger.error("decode event data error:", deserializationError);
        }
    }
    logger.info("add ok: {}", holders.size());
}
Also used : EventFull(org.starcoin.bean.EventFull) DeserializationError(com.novi.serde.DeserializationError) DepositEvent(org.starcoin.types.event.DepositEvent)

Example 4 with DeserializationError

use of com.novi.serde.DeserializationError 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)

Example 5 with DeserializationError

use of com.novi.serde.DeserializationError in project starcoin-search by starcoinorg.

the class TransactionPayloadHandle method executeInternal.

@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) {
    if (client == null) {
        init();
    }
    TransferOffset transactionPayloadRemoteOffset = ServiceUtils.getRemoteOffset(client, index);
    logger.info("handle txn payload: {}", transactionPayloadRemoteOffset);
    if (transactionPayloadRemoteOffset == null) {
        // init offset
        transactionPayloadRemoteOffset = new TransferOffset();
        transactionPayloadRemoteOffset.setTimestamp("0");
        ServiceUtils.setRemoteOffset(client, index, transactionPayloadRemoteOffset);
        logger.info("offset not init, init ok!");
    }
    try {
        List<Transaction> transactionList = elasticSearchHandler.getTransactionByTimestamp(transactionPayloadRemoteOffset.getTimestamp());
        if (!transactionList.isEmpty()) {
            List<SwapTransaction> swapTransactionList = new ArrayList<>();
            elasticSearchHandler.addUserTransactionToList(transactionList);
            elasticSearchHandler.bulkAddPayload(index, transactionList, objectMapper, swapTransactionList);
            // add es success and add swap txn
            if (!swapTransactionList.isEmpty()) {
                Map<String, BigDecimal> tokenPriceMap = new HashMap<>();
                for (SwapTransaction swapTransaction : swapTransactionList) {
                    List<String> tokenList = new ArrayList<>();
                    tokenList.add(swapTransaction.getTokenA());
                    tokenList.add(swapTransaction.getTokenB());
                    swapTransaction.setAmountA(ServiceUtils.divideScalingFactor(stateRPCClient, swapTransaction.getTokenA(), swapTransaction.getAmountA()));
                    swapTransaction.setAmountB(ServiceUtils.divideScalingFactor(stateRPCClient, swapTransaction.getTokenB(), swapTransaction.getAmountB()));
                    boolean isSwap = SwapType.isSwap(swapTransaction.getSwapType());
                    BigDecimal value = getTotalValue(tokenPriceMap, swapTransaction.getTokenA(), swapTransaction.getAmountA(), swapTransaction.getTokenB(), swapTransaction.getAmountB(), isSwap);
                    if (value != null) {
                        swapTransaction.setTotalValue(value);
                    } else {
                        int retry = 3;
                        while (retry > 0) {
                            // get oracle price
                            logger.info("token price not cache, load from oracle: {}, {}", swapTransaction.getTokenA(), swapTransaction.getTokenB());
                            long priceTime = swapTransaction.getTimestamp() - 300000 * (6 - retry);
                            List<org.starcoin.bean.OracleTokenPair> oracleTokenPairs = swapApiClient.getProximatePriceRounds(network, tokenList, String.valueOf(priceTime));
                            if (oracleTokenPairs != null && !oracleTokenPairs.isEmpty()) {
                                BigDecimal priceA = null;
                                OracleTokenPair oracleTokenA = oracleTokenPairs.get(0);
                                if (oracleTokenA != null) {
                                    priceA = new BigDecimal(oracleTokenA.getPrice());
                                    priceA = priceA.movePointLeft(oracleTokenA.getDecimals());
                                    tokenPriceMap.put(swapTransaction.getTokenA(), priceA);
                                    logger.info("get oracle price1 ok: {}", oracleTokenA);
                                }
                                // get tokenB price
                                BigDecimal priceB = null;
                                OracleTokenPair oracleTokenB = oracleTokenPairs.get(1);
                                if (oracleTokenB != null) {
                                    priceB = new BigDecimal(oracleTokenB.getPrice());
                                    priceB = priceB.movePointLeft(oracleTokenB.getDecimals());
                                    tokenPriceMap.put(swapTransaction.getTokenB(), priceB);
                                    logger.info("get oracle price2 ok: {}", oracleTokenB);
                                }
                                BigDecimal zero = new BigDecimal(0);
                                if (isSwap) {
                                    if (priceA != null && priceA.compareTo(zero) == 1) {
                                        swapTransaction.setTotalValue(priceA.multiply(swapTransaction.getAmountA()));
                                        break;
                                    }
                                    if (priceB != null && priceB.compareTo(zero) == 1) {
                                        swapTransaction.setTotalValue(priceB.multiply(swapTransaction.getAmountB()));
                                        break;
                                    }
                                    logger.warn("get oracle price null: {}, {}, {}", swapTransaction.getTokenA(), swapTransaction.getTokenB(), swapTransaction.getTimestamp());
                                    retry--;
                                } else {
                                    // add or remove
                                    boolean isExistB = priceB != null && priceB.compareTo(zero) == 1;
                                    if (priceA != null && priceA.compareTo(zero) == 1) {
                                        BigDecimal valueA = priceA.multiply(swapTransaction.getAmountA());
                                        if (isExistB) {
                                            swapTransaction.setTotalValue(priceB.multiply(swapTransaction.getAmountB()).add(valueA));
                                        } else {
                                            swapTransaction.setTotalValue(valueA.multiply(new BigDecimal(2)));
                                        }
                                        break;
                                    } else {
                                        if (isExistB) {
                                            swapTransaction.setTotalValue(priceB.multiply(swapTransaction.getAmountB()).multiply(new BigDecimal(2)));
                                            break;
                                        } else {
                                            logger.warn("get oracle price null: {}, {}, {}", swapTransaction.getTokenA(), swapTransaction.getTokenB(), swapTransaction.getTimestamp());
                                            retry--;
                                        }
                                    }
                                }
                            } else {
                                logger.warn("getProximatePriceRounds null: {}, {}, {}", swapTransaction.getTokenA(), swapTransaction.getTokenB(), swapTransaction.getTimestamp());
                                retry--;
                            }
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                swapTxnService.saveList(swapTransactionList);
                logger.info("save swap txn ok: {}", swapTransactionList.size());
            }
            // update offset
            Transaction last = transactionList.get(transactionList.size() - 1);
            TransferOffset currentOffset = new TransferOffset();
            currentOffset.setTimestamp(String.valueOf(last.getTimestamp()));
            ServiceUtils.setRemoteOffset(client, index, currentOffset);
            logger.info("update payload ok: {}", currentOffset);
        } else {
            logger.warn("get txn_info null");
        }
    } catch (IOException | DeserializationError | JSONRPC2SessionException e) {
        logger.warn("handle transaction payload error:", e);
    }
}
Also used : HashMap(java.util.HashMap) DeserializationError(com.novi.serde.DeserializationError) JSONRPC2SessionException(com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) Transaction(org.starcoin.bean.Transaction)

Aggregations

DeserializationError (com.novi.serde.DeserializationError)5 DepositEvent (org.starcoin.types.event.DepositEvent)2 JSONRPC2SessionException (com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 EventFull (org.starcoin.bean.EventFull)1 Transaction (org.starcoin.bean.Transaction)1 AccountAddress (org.starcoin.types.AccountAddress)1 TransactionPayload (org.starcoin.types.TransactionPayload)1 ProposalCreatedEvent (org.starcoin.types.event.ProposalCreatedEvent)1 WithdrawEvent (org.starcoin.types.event.WithdrawEvent)1