Skip to content

Commit f18864c

Browse files
authored
feat(http): optimize getAddress method (#5367)
1 parent 83151aa commit f18864c

4 files changed

Lines changed: 260 additions & 11 deletions

File tree

framework/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ dependencies {
8686
compile group: 'org.pf4j', name: 'pf4j', version: '2.5.0'
8787

8888
testImplementation group: 'org.springframework', name: 'spring-test', version: '5.2.0.RELEASE'
89+
testImplementation group: 'org.springframework', name: 'spring-web', version: '5.2.0.RELEASE'
8990

9091
compile group: 'org.zeromq', name: 'jeromq', version: '0.5.3'
9192
compile project(":chainbase")

framework/src/main/java/org/tron/core/services/http/Util.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
import com.google.protobuf.GeneratedMessageV3;
1212
import com.google.protobuf.InvalidProtocolBufferException;
1313
import com.google.protobuf.Message;
14+
15+
import java.io.BufferedReader;
1416
import java.io.IOException;
17+
import java.io.InputStreamReader;
1518
import java.lang.reflect.Constructor;
1619
import java.math.BigDecimal;
1720
import java.nio.charset.Charset;
1821
import java.security.InvalidParameterException;
1922
import java.util.ArrayList;
2023
import java.util.List;
21-
import java.util.stream.Collectors;
2224
import javax.servlet.http.HttpServletRequest;
2325
import javax.servlet.http.HttpServletResponse;
2426
import lombok.extern.slf4j.Slf4j;
2527
import org.apache.commons.lang3.StringUtils;
2628
import org.bouncycastle.util.encoders.Hex;
29+
import org.eclipse.jetty.http.HttpMethod;
2730
import org.eclipse.jetty.util.StringUtil;
2831
import org.tron.api.GrpcAPI;
2932
import org.tron.api.GrpcAPI.BlockList;
@@ -70,6 +73,8 @@ public class Util {
7073
public static final String FUNCTION_SELECTOR = "function_selector";
7174
public static final String FUNCTION_PARAMETER = "parameter";
7275
public static final String CALL_DATA = "data";
76+
public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
77+
public static final String APPLICATION_JSON = "application/json";
7378

7479
public static String printTransactionFee(String transactionFee) {
7580
JSONObject jsonObject = new JSONObject();
@@ -498,16 +503,7 @@ public static void printAccount(Account reply, HttpServletResponse response, Boo
498503
public static byte[] getAddress(HttpServletRequest request) throws Exception {
499504
byte[] address = null;
500505
String addressParam = "address";
501-
String addressStr = request.getParameter(addressParam);
502-
if (StringUtils.isBlank(addressStr)) {
503-
String input = request.getReader().lines()
504-
.collect(Collectors.joining(System.lineSeparator()));
505-
Util.checkBodySize(input);
506-
JSONObject jsonObject = JSON.parseObject(input);
507-
if (jsonObject != null) {
508-
addressStr = jsonObject.getString(addressParam);
509-
}
510-
}
506+
String addressStr = checkGetParam(request, addressParam);
511507
if (StringUtils.isNotBlank(addressStr)) {
512508
if (StringUtils.startsWith(addressStr, Constant.ADD_PRE_FIX_STRING_MAINNET)) {
513509
address = Hex.decode(addressStr);
@@ -518,6 +514,47 @@ public static byte[] getAddress(HttpServletRequest request) throws Exception {
518514
return address;
519515
}
520516

517+
private static String checkGetParam(HttpServletRequest request, String key) throws Exception {
518+
String method = request.getMethod();
519+
String value = null;
520+
521+
if (HttpMethod.GET.toString().toUpperCase().equalsIgnoreCase(method)) {
522+
return request.getParameter(key);
523+
}
524+
if (HttpMethod.POST.toString().toUpperCase().equals(method)) {
525+
String contentType = request.getContentType();
526+
if (StringUtils.isBlank(contentType)) {
527+
return null;
528+
}
529+
if (APPLICATION_JSON.toLowerCase().contains(contentType)) {
530+
value = getRequestValue(request);
531+
if (StringUtils.isBlank(value)) {
532+
return null;
533+
}
534+
535+
JSONObject jsonObject = JSON.parseObject(value);
536+
if (jsonObject != null) {
537+
return jsonObject.getString(key);
538+
}
539+
} else if (APPLICATION_FORM_URLENCODED.toLowerCase().contains(contentType)) {
540+
return request.getParameter(key);
541+
} else {
542+
return null;
543+
}
544+
}
545+
return value;
546+
}
547+
548+
public static String getRequestValue(HttpServletRequest request) throws IOException {
549+
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
550+
String line;
551+
StringBuilder sb = new StringBuilder();
552+
while ((line = reader.readLine()) != null) {
553+
sb.append(line);
554+
}
555+
return sb.toString();
556+
}
557+
521558
public static List<Log> convertLogAddressToTronAddress(TransactionInfo transactionInfo) {
522559
List<Log> newLogList = new ArrayList<>();
523560

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.tron.core.services.http;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import javax.annotation.Resource;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import org.springframework.mock.web.MockHttpServletRequest;
11+
import org.springframework.mock.web.MockHttpServletResponse;
12+
import org.tron.common.BaseTest;
13+
import org.tron.core.Constant;
14+
import org.tron.core.config.args.Args;
15+
16+
public class GetBrokerageServletTest extends BaseTest {
17+
18+
@Resource
19+
private GetBrokerageServlet getBrokerageServlet;
20+
21+
static {
22+
dbPath = "db_GetBrokerageServlet_test";
23+
Args.setParam(
24+
new String[]{
25+
"--output-directory", dbPath,
26+
}, Constant.TEST_CONF
27+
);
28+
}
29+
30+
public MockHttpServletRequest createRequest(String contentType) {
31+
MockHttpServletRequest request = new MockHttpServletRequest();
32+
request.setMethod("POST");
33+
request.setContentType(contentType);
34+
request.setCharacterEncoding("UTF-8");
35+
return request;
36+
}
37+
38+
@Test
39+
public void getBrokerageValueByJsonTest() {
40+
int expect = 20;
41+
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
42+
MockHttpServletRequest request = createRequest("application/json");
43+
request.setContent(jsonParam.getBytes());
44+
MockHttpServletResponse response = new MockHttpServletResponse();
45+
getBrokerageServlet.doPost(request, response);
46+
try {
47+
String contentAsString = response.getContentAsString();
48+
JSONObject result = JSONObject.parseObject(contentAsString);
49+
int brokerage = (int)result.get("brokerage");
50+
Assert.assertEquals(expect, brokerage);
51+
} catch (UnsupportedEncodingException e) {
52+
Assert.fail(e.getMessage());
53+
}
54+
}
55+
56+
@Test
57+
public void getBrokerageValueTest() {
58+
int expect = 20;
59+
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
60+
request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
61+
MockHttpServletResponse response = new MockHttpServletResponse();
62+
getBrokerageServlet.doPost(request, response);
63+
try {
64+
String contentAsString = response.getContentAsString();
65+
JSONObject result = JSONObject.parseObject(contentAsString);
66+
int brokerage = (int)result.get("brokerage");
67+
Assert.assertEquals(expect, brokerage);
68+
} catch (UnsupportedEncodingException e) {
69+
Assert.fail(e.getMessage());
70+
}
71+
}
72+
73+
@Test
74+
public void getByBlankParamTest() {
75+
int expect = 0;
76+
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
77+
request.addParameter("address", "");
78+
MockHttpServletResponse response = new MockHttpServletResponse();
79+
getBrokerageServlet.doPost(request, response);
80+
try {
81+
String contentAsString = response.getContentAsString();
82+
JSONObject result = JSONObject.parseObject(contentAsString);
83+
int brokerage = (int)result.get("brokerage");
84+
Assert.assertEquals(expect, brokerage);
85+
String content = (String) result.get("Error");
86+
Assert.assertNull(content);
87+
} catch (UnsupportedEncodingException e) {
88+
Assert.fail(e.getMessage());
89+
}
90+
}
91+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package org.tron.core.services.http;
2+
3+
import static org.tron.common.utils.Commons.decodeFromBase58Check;
4+
5+
import com.alibaba.fastjson.JSONObject;
6+
7+
import java.io.File;
8+
import java.io.UnsupportedEncodingException;
9+
import javax.annotation.Resource;
10+
11+
import org.apache.commons.lang3.StringUtils;
12+
import org.junit.After;
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
import org.springframework.mock.web.MockHttpServletRequest;
16+
import org.springframework.mock.web.MockHttpServletResponse;
17+
import org.tron.common.BaseTest;
18+
import org.tron.common.utils.FileUtil;
19+
import org.tron.core.Constant;
20+
import org.tron.core.config.args.Args;
21+
import org.tron.core.db.Manager;
22+
import org.tron.core.service.MortgageService;
23+
import org.tron.core.store.DelegationStore;
24+
25+
public class GetRewardServletTest extends BaseTest {
26+
27+
@Resource
28+
private Manager manager;
29+
30+
@Resource
31+
private MortgageService mortgageService;
32+
33+
@Resource
34+
private DelegationStore delegationStore;
35+
36+
@Resource
37+
GetRewardServlet getRewardServlet;
38+
39+
static {
40+
dbPath = "db_GetRewardServlet_test";
41+
Args.setParam(
42+
new String[]{
43+
"--output-directory", dbPath,
44+
}, Constant.TEST_CONF
45+
);
46+
}
47+
48+
public MockHttpServletRequest createRequest(String contentType) {
49+
MockHttpServletRequest request = new MockHttpServletRequest();
50+
request.setMethod("POST");
51+
request.setContentType(contentType);
52+
request.setCharacterEncoding("UTF-8");
53+
return request;
54+
}
55+
56+
public void init() {
57+
manager.getDynamicPropertiesStore().saveChangeDelegation(1);
58+
byte[] sr = decodeFromBase58Check("27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
59+
delegationStore.setBrokerage(0, sr, 10);
60+
delegationStore.setWitnessVote(0, sr, 100000000);
61+
}
62+
63+
@Test
64+
public void getRewardValueByJsonTest() {
65+
init();
66+
int expect = 138181;
67+
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
68+
MockHttpServletRequest request = createRequest("application/json");
69+
MockHttpServletResponse response = new MockHttpServletResponse();
70+
request.setContent(jsonParam.getBytes());
71+
try {
72+
getRewardServlet.doPost(request, response);
73+
String contentAsString = response.getContentAsString();
74+
JSONObject result = JSONObject.parseObject(contentAsString);
75+
int reward = (int)result.get("reward");
76+
Assert.assertEquals(expect, reward);
77+
} catch (UnsupportedEncodingException e) {
78+
Assert.fail(e.getMessage());
79+
}
80+
}
81+
82+
@Test
83+
public void getRewardValueTest() {
84+
init();
85+
int expect = 138181;
86+
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
87+
MockHttpServletResponse response = new MockHttpServletResponse();
88+
mortgageService.payStandbyWitness();
89+
request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
90+
getRewardServlet.doPost(request, response);
91+
try {
92+
String contentAsString = response.getContentAsString();
93+
JSONObject result = JSONObject.parseObject(contentAsString);
94+
int reward = (int)result.get("reward");
95+
Assert.assertEquals(expect, reward);
96+
} catch (UnsupportedEncodingException e) {
97+
Assert.fail(e.getMessage());
98+
}
99+
}
100+
101+
@Test
102+
public void getByBlankParamTest() {
103+
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
104+
MockHttpServletResponse response = new MockHttpServletResponse();
105+
request.addParameter("address", "");
106+
GetRewardServlet getRewardServlet = new GetRewardServlet();
107+
getRewardServlet.doPost(request, response);
108+
try {
109+
String contentAsString = response.getContentAsString();
110+
JSONObject result = JSONObject.parseObject(contentAsString);
111+
int reward = (int)result.get("reward");
112+
Assert.assertEquals(0, reward);
113+
String content = (String) result.get("Error");
114+
Assert.assertNull(content);
115+
} catch (UnsupportedEncodingException e) {
116+
Assert.fail(e.getMessage());
117+
}
118+
}
119+
120+
}

0 commit comments

Comments
 (0)