Search in sources :

Example 1 with AccountAddress

use of org.starcoin.types.AccountAddress 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 AccountAddress

use of org.starcoin.types.AccountAddress 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 3 with AccountAddress

use of org.starcoin.types.AccountAddress in project starcoin-java by starcoinorg.

the class StructTagDeserializer method deserialize.

@Override
public StructTag deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    StructTag structTag = null;
    AccountAddress address = null;
    Identifier module = null;
    Identifier name = null;
    List<TypeTag> type_params = new ArrayList<>();
    while (!jsonParser.isClosed()) {
        String token = jsonParser.nextTextValue();
        if (token != null) {
            switch(jsonParser.getCurrentName()) {
                case "struct_tag_type":
                    String[] tokens = token.split("::");
                    address = AccountAddress.valueOf(Hex.decode(tokens[0]));
                    module = new Identifier(tokens[1]);
                    name = new Identifier(tokens[2]);
                    break;
                case "struct_tag_params":
                    // TODO parse
                    break;
            }
        }
    }
    if (address != null && module != null & name != null) {
        structTag = new StructTag(address, module, name, type_params);
    }
    return structTag;
}
Also used : Identifier(org.starcoin.types.Identifier) ArrayList(java.util.ArrayList) StructTag(org.starcoin.types.StructTag) AccountAddress(org.starcoin.types.AccountAddress) TypeTag(org.starcoin.types.TypeTag)

Example 4 with AccountAddress

use of org.starcoin.types.AccountAddress in project starcoin-java by starcoinorg.

the class ModuleDeserializer method deserialize.

@Override
public ModuleId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    JsonNode typeTagNode = jsonParser.getCodec().readTree(jsonParser);
    String name = typeTagNode.get("name").textValue();
    AccountAddress address = AccountAddress.valueOf(Hex.decode(typeTagNode.get("address").textValue()));
    return new ModuleId(address, new Identifier(name));
}
Also used : ModuleId(org.starcoin.types.ModuleId) Identifier(org.starcoin.types.Identifier) JsonNode(com.fasterxml.jackson.databind.JsonNode) AccountAddress(org.starcoin.types.AccountAddress)

Aggregations

AccountAddress (org.starcoin.types.AccountAddress)4 Identifier (org.starcoin.types.Identifier)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Bytes (com.novi.serde.Bytes)1 DeserializationError (com.novi.serde.DeserializationError)1 ArrayList (java.util.ArrayList)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)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 ModuleId (org.starcoin.types.ModuleId)1 StructTag (org.starcoin.types.StructTag)1 TransactionPayload (org.starcoin.types.TransactionPayload)1 TypeTag (org.starcoin.types.TypeTag)1 ProposalCreatedEvent (org.starcoin.types.event.ProposalCreatedEvent)1