Skip to content

Commit

Permalink
feat(http): optimize getAddress method (tronprotocol#5367)
Browse files Browse the repository at this point in the history
  • Loading branch information
forfreeday authored Aug 18, 2023
1 parent 83151aa commit f18864c
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 11 deletions.
1 change: 1 addition & 0 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ dependencies {
compile group: 'org.pf4j', name: 'pf4j', version: '2.5.0'

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

compile group: 'org.zeromq', name: 'jeromq', version: '0.5.3'
compile project(":chainbase")
Expand Down
59 changes: 48 additions & 11 deletions framework/src/main/java/org/tron/core/services/http/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.util.encoders.Hex;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.util.StringUtil;
import org.tron.api.GrpcAPI;
import org.tron.api.GrpcAPI.BlockList;
Expand Down Expand Up @@ -70,6 +73,8 @@ public class Util {
public static final String FUNCTION_SELECTOR = "function_selector";
public static final String FUNCTION_PARAMETER = "parameter";
public static final String CALL_DATA = "data";
public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String APPLICATION_JSON = "application/json";

public static String printTransactionFee(String transactionFee) {
JSONObject jsonObject = new JSONObject();
Expand Down Expand Up @@ -498,16 +503,7 @@ public static void printAccount(Account reply, HttpServletResponse response, Boo
public static byte[] getAddress(HttpServletRequest request) throws Exception {
byte[] address = null;
String addressParam = "address";
String addressStr = request.getParameter(addressParam);
if (StringUtils.isBlank(addressStr)) {
String input = request.getReader().lines()
.collect(Collectors.joining(System.lineSeparator()));
Util.checkBodySize(input);
JSONObject jsonObject = JSON.parseObject(input);
if (jsonObject != null) {
addressStr = jsonObject.getString(addressParam);
}
}
String addressStr = checkGetParam(request, addressParam);
if (StringUtils.isNotBlank(addressStr)) {
if (StringUtils.startsWith(addressStr, Constant.ADD_PRE_FIX_STRING_MAINNET)) {
address = Hex.decode(addressStr);
Expand All @@ -518,6 +514,47 @@ public static byte[] getAddress(HttpServletRequest request) throws Exception {
return address;
}

private static String checkGetParam(HttpServletRequest request, String key) throws Exception {
String method = request.getMethod();
String value = null;

if (HttpMethod.GET.toString().toUpperCase().equalsIgnoreCase(method)) {
return request.getParameter(key);
}
if (HttpMethod.POST.toString().toUpperCase().equals(method)) {
String contentType = request.getContentType();
if (StringUtils.isBlank(contentType)) {
return null;
}
if (APPLICATION_JSON.toLowerCase().contains(contentType)) {
value = getRequestValue(request);
if (StringUtils.isBlank(value)) {
return null;
}

JSONObject jsonObject = JSON.parseObject(value);
if (jsonObject != null) {
return jsonObject.getString(key);
}
} else if (APPLICATION_FORM_URLENCODED.toLowerCase().contains(contentType)) {
return request.getParameter(key);
} else {
return null;
}
}
return value;
}

public static String getRequestValue(HttpServletRequest request) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}

public static List<Log> convertLogAddressToTronAddress(TransactionInfo transactionInfo) {
List<Log> newLogList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.tron.core.services.http;

import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tron.common.BaseTest;
import org.tron.core.Constant;
import org.tron.core.config.args.Args;

public class GetBrokerageServletTest extends BaseTest {

@Resource
private GetBrokerageServlet getBrokerageServlet;

static {
dbPath = "db_GetBrokerageServlet_test";
Args.setParam(
new String[]{
"--output-directory", dbPath,
}, Constant.TEST_CONF
);
}

public MockHttpServletRequest createRequest(String contentType) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType(contentType);
request.setCharacterEncoding("UTF-8");
return request;
}

@Test
public void getBrokerageValueByJsonTest() {
int expect = 20;
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
MockHttpServletRequest request = createRequest("application/json");
request.setContent(jsonParam.getBytes());
MockHttpServletResponse response = new MockHttpServletResponse();
getBrokerageServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
int brokerage = (int)result.get("brokerage");
Assert.assertEquals(expect, brokerage);
} catch (UnsupportedEncodingException e) {
Assert.fail(e.getMessage());
}
}

@Test
public void getBrokerageValueTest() {
int expect = 20;
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
MockHttpServletResponse response = new MockHttpServletResponse();
getBrokerageServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
int brokerage = (int)result.get("brokerage");
Assert.assertEquals(expect, brokerage);
} catch (UnsupportedEncodingException e) {
Assert.fail(e.getMessage());
}
}

@Test
public void getByBlankParamTest() {
int expect = 0;
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
request.addParameter("address", "");
MockHttpServletResponse response = new MockHttpServletResponse();
getBrokerageServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
int brokerage = (int)result.get("brokerage");
Assert.assertEquals(expect, brokerage);
String content = (String) result.get("Error");
Assert.assertNull(content);
} catch (UnsupportedEncodingException e) {
Assert.fail(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.tron.core.services.http;

import static org.tron.common.utils.Commons.decodeFromBase58Check;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.tron.common.BaseTest;
import org.tron.common.utils.FileUtil;
import org.tron.core.Constant;
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
import org.tron.core.service.MortgageService;
import org.tron.core.store.DelegationStore;

public class GetRewardServletTest extends BaseTest {

@Resource
private Manager manager;

@Resource
private MortgageService mortgageService;

@Resource
private DelegationStore delegationStore;

@Resource
GetRewardServlet getRewardServlet;

static {
dbPath = "db_GetRewardServlet_test";
Args.setParam(
new String[]{
"--output-directory", dbPath,
}, Constant.TEST_CONF
);
}

public MockHttpServletRequest createRequest(String contentType) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("POST");
request.setContentType(contentType);
request.setCharacterEncoding("UTF-8");
return request;
}

public void init() {
manager.getDynamicPropertiesStore().saveChangeDelegation(1);
byte[] sr = decodeFromBase58Check("27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
delegationStore.setBrokerage(0, sr, 10);
delegationStore.setWitnessVote(0, sr, 100000000);
}

@Test
public void getRewardValueByJsonTest() {
init();
int expect = 138181;
String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}";
MockHttpServletRequest request = createRequest("application/json");
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(jsonParam.getBytes());
try {
getRewardServlet.doPost(request, response);
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
int reward = (int)result.get("reward");
Assert.assertEquals(expect, reward);
} catch (UnsupportedEncodingException e) {
Assert.fail(e.getMessage());
}
}

@Test
public void getRewardValueTest() {
init();
int expect = 138181;
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
MockHttpServletResponse response = new MockHttpServletResponse();
mortgageService.payStandbyWitness();
request.addParameter("address", "27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh");
getRewardServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
int reward = (int)result.get("reward");
Assert.assertEquals(expect, reward);
} catch (UnsupportedEncodingException e) {
Assert.fail(e.getMessage());
}
}

@Test
public void getByBlankParamTest() {
MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded");
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("address", "");
GetRewardServlet getRewardServlet = new GetRewardServlet();
getRewardServlet.doPost(request, response);
try {
String contentAsString = response.getContentAsString();
JSONObject result = JSONObject.parseObject(contentAsString);
int reward = (int)result.get("reward");
Assert.assertEquals(0, reward);
String content = (String) result.get("Error");
Assert.assertNull(content);
} catch (UnsupportedEncodingException e) {
Assert.fail(e.getMessage());
}
}

}

0 comments on commit f18864c

Please sign in to comment.