Skip to content

Commit

Permalink
feat(api): optimize compatibility (tronprotocol#5408)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcmyf authored Aug 22, 2023
1 parent f20e11b commit ac90c45
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ public static byte[] getOwner(Transaction.Contract contract) {
}
}
return owner.toByteArray();
} catch (InvalidProtocolBufferException invalidProtocolBufferException) {
logger.warn("InvalidProtocolBufferException occurred because {}, please verify the interface "
+ "input parameters", invalidProtocolBufferException.getMessage());
return new byte[0];
} catch (Exception ex) {
logger.error(ex.getMessage());
return new byte[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tron.core.services.http;

import static org.tron.core.services.http.Util.existVisible;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -8,29 +11,43 @@
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.core.Wallet;


@Component
@Slf4j(topic = "API")
public class GetAssetIssueListServlet extends RateLimiterServlet {

@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
AssetIssueList reply = wallet.getAssetIssueList();
if (reply != null) {
response.getWriter().println(JsonFormat.printToString(reply, visible));
} else {
response.getWriter().println("{}");
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
try {
PostParams params = PostParams.getPostParams(request);
boolean visible = Util.getVisible(request);
if (!existVisible(request)) {
visible = params.isVisible();
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

private void response(HttpServletResponse response, boolean visible) throws IOException {
AssetIssueList reply = wallet.getAssetIssueList();
if (reply != null) {
response.getWriter().println(JsonFormat.printToString(reply, visible));
} else {
response.getWriter().println("{}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
String ownerAddress = request.getParameter("ownerAddress");
if (ownerAddress == null) {
ownerAddress = request.getParameter("owner_address");
}
if (visible) {
ownerAddress = Util.getHexAddress(ownerAddress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ public class GetBlockByNumServlet extends RateLimiterServlet {
@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
fillResponse(Util.getVisible(request), Long.parseLong(request.getParameter("num")), response);
long num = 0;
String numStr = request.getParameter("num");
if (numStr != null) {
num = Long.parseLong(numStr);
}
fillResponse(Util.getVisible(request), num, response);
} catch (Exception e) {
Util.processError(e, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
PostParams params = PostParams.getPostParams(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public class GetCanDelegatedMaxSizeServlet extends RateLimiterServlet {
@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
int type = Integer.parseInt(request.getParameter("type"));
int type = 0;
String typeStr = request.getParameter("type");
if (typeStr != null) {
type = Integer.parseInt(typeStr);
}
String ownerAddress = request.getParameter("owner_address");
if (visible) {
ownerAddress = Util.getHexAddress(ownerAddress);
Expand All @@ -34,6 +39,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
PostParams params = PostParams.getPostParams(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@ public class GetCanWithdrawUnfreezeAmountServlet extends RateLimiterServlet {
@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
String ownerAddress = request.getParameter("owner_address");
long timestamp = Long.valueOf(request.getParameter("timestamp"));
long timestamp = 0;
String timestampStr = request.getParameter("timestamp");
if (timestampStr != null) {
timestamp = Long.parseLong(timestampStr);
}
if (visible) {
ownerAddress = Util.getHexAddress(ownerAddress);
}
fillResponse(visible,
ByteString.copyFrom(ByteArray.fromHexString(ownerAddress)),
timestamp,
response);
ByteString.copyFrom(ByteArray.fromHexString(ownerAddress)),
timestamp,
response);
} catch (Exception e) {
Util.processError(e, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
PostParams params = PostParams.getPostParams(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.tron.core.services.http;

import static org.tron.core.services.http.Util.existVisible;
import static org.tron.core.services.http.Util.getVisible;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -8,29 +12,43 @@
import org.tron.core.Wallet;
import org.tron.protos.Protocol.Block;


@Component
@Slf4j(topic = "API")
public class GetNowBlockServlet extends RateLimiterServlet {

@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
Block reply = wallet.getNowBlock();
if (reply != null) {
response.getWriter().println(Util.printBlock(reply, visible));
} else {
response.getWriter().println("{}");
}
boolean visible = getVisible(request);
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
try {
PostParams params = PostParams.getPostParams(request);
boolean visible = getVisible(request);
if (!existVisible(request)) {
visible = params.isVisible();
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

private void response(HttpServletResponse response, boolean visible) throws IOException {
Block reply = wallet.getNowBlock();
if (reply != null) {
response.getWriter().println(Util.printBlock(reply, visible));
} else {
response.getWriter().println("{}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tron.core.services.http;

import static org.tron.core.services.http.Util.existVisible;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -8,29 +11,43 @@
import org.tron.api.GrpcAPI.NodeList;
import org.tron.core.Wallet;


@Component
@Slf4j(topic = "API")
public class ListNodesServlet extends RateLimiterServlet {

@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
NodeList reply = wallet.listNodes();
if (reply != null) {
response.getWriter().println(JsonFormat.printToString(reply, visible));
} else {
response.getWriter().println("{}");
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
try {
PostParams params = PostParams.getPostParams(request);
boolean visible = Util.getVisible(request);
if (!existVisible(request)) {
visible = params.isVisible();
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

private void response(HttpServletResponse response, boolean visible) throws IOException {
NodeList reply = wallet.listNodes();
if (reply != null) {
response.getWriter().println(JsonFormat.printToString(reply, visible));
} else {
response.getWriter().println("{}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tron.core.services.http;

import static org.tron.core.services.http.Util.existVisible;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -8,29 +11,43 @@
import org.tron.api.GrpcAPI.ProposalList;
import org.tron.core.Wallet;


@Component
@Slf4j(topic = "API")
public class ListProposalsServlet extends RateLimiterServlet {

@Autowired
private Wallet wallet;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
boolean visible = Util.getVisible(request);
ProposalList reply = wallet.getProposalList();
if (reply != null) {
response.getWriter().println(JsonFormat.printToString(reply, visible));
} else {
response.getWriter().println("{}");
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
doGet(request, response);
try {
PostParams params = PostParams.getPostParams(request);
boolean visible = Util.getVisible(request);
if (!existVisible(request)) {
visible = params.isVisible();
}
response(response, visible);
} catch (Exception e) {
Util.processError(e, response);
}
}

private void response(HttpServletResponse response, boolean visible) throws IOException {
ProposalList reply = wallet.getProposalList();
if (reply != null) {
response.getWriter().println(JsonFormat.printToString(reply, visible));
} else {
response.getWriter().println("{}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tron.core.services.http;

import static org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED;
import static org.tron.core.services.http.Util.getJsonString;

import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import lombok.Getter;
Expand All @@ -21,6 +24,9 @@ public PostParams(String params, boolean visible) {
public static PostParams getPostParams(HttpServletRequest request) throws Exception {
String input = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
Util.checkBodySize(input);
if (APPLICATION_FORM_URLENCODED.getMimeType().equals(request.getContentType())) {
input = getJsonString(input);
}
boolean visible = Util.getVisiblePost(input);
return new PostParams(input, visible);
}
Expand Down
Loading

0 comments on commit ac90c45

Please sign in to comment.