Skip to content

Commit e388ca8

Browse files
committed
refine code according to comments
1 parent 4b5b1ef commit e388ca8

10 files changed

Lines changed: 135 additions & 64 deletions

File tree

‎src/main/java/org/tron/core/WalletSolidity.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,39 @@ public Transaction getTransactionById(ByteString id) {
109109
return null;
110110
}
111111

112-
public TransactionList getTransactionsByTimestamp(long beginTime, long endTime) {
112+
public TransactionList getTransactionsByTimestamp(long beginTime, long endTime, long offset, long limit) {
113113
List<Transaction> transactionsByTimestamp = storeAPI
114-
.getTransactionsByTimestamp(beginTime, endTime);
114+
.getTransactionsByTimestamp(beginTime, endTime, offset, limit);
115115
TransactionList transactionList = TransactionList.newBuilder()
116116
.addAllTransaction(transactionsByTimestamp).build();
117117
return transactionList;
118118
}
119119

120-
public TransactionList getTransactionsFromThis(ByteString thisAddress, int offset , int limit) {
120+
public NumberMessage getTransactionsByTimestampCount(long beginTime, long endTime) {
121+
return NumberMessage.newBuilder().setNum(storeAPI
122+
.getTransactionsByTimestampCount(beginTime, endTime)).build();
123+
}
124+
125+
public TransactionList getTransactionsFromThis(ByteString thisAddress, long offset , long limit) {
121126
List<Transaction> transactionsFromThis = storeAPI
122127
.getTransactionsFromThis(ByteArray.toHexString(thisAddress.toByteArray()),offset , limit);
123128
TransactionList transactionList = TransactionList.newBuilder()
124129
.addAllTransaction(transactionsFromThis).build();
125130
return transactionList;
126131
}
127132

128-
public TransactionList getTransactionsToThis(ByteString toAddress, int offset, int limit) {
133+
public TransactionList getTransactionsToThis(ByteString toAddress, long offset, long limit) {
129134
List<Transaction> transactionsToThis = storeAPI
130135
.getTransactionsToThis(ByteArray.toHexString(toAddress.toByteArray()), offset, limit);
131136
TransactionList transactionList = TransactionList.newBuilder()
132137
.addAllTransaction(transactionsToThis).build();
133138
return transactionList;
134139
}
135-
public NumberMessage getTransactionFromThisCount(ByteString toAddress){
140+
public NumberMessage getTransactionFromThisCount(ByteString toAddress) {
136141
return NumberMessage.newBuilder().setNum(storeAPI.getTransactionsFromThisCount(ByteArray.toHexString(toAddress.toByteArray()))).build();
137142
}
138143

139-
public NumberMessage getTransactionToThisCount(ByteString toAddress){
144+
public NumberMessage getTransactionToThisCount(ByteString toAddress) {
140145
return NumberMessage.newBuilder().setNum(storeAPI.getTransactionsToThisCount(ByteArray.toHexString(toAddress.toByteArray()))).build();
141146
}
142147
}

‎src/main/java/org/tron/core/config/Parameter.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ interface NetConstants {
4242
int NET_MAX_INV_SIZE_IN_MINUTES = 2;
4343
int MSG_CACHE_DURATION_IN_BLOCKS = 5;
4444
}
45+
46+
interface DataBaseConstants {
47+
int TRANSACTIONS_COUNT_LIMIT_MAX = 1000;
48+
}
4549
}

‎src/main/java/org/tron/core/config/args/Args.java‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.beust.jcommander.JCommander;
44
import com.beust.jcommander.Parameter;
5+
import com.sun.org.apache.bcel.internal.generic.FADD;
56
import com.typesafe.config.Config;
67
import com.typesafe.config.ConfigObject;
78
import java.io.BufferedReader;
@@ -199,11 +200,19 @@ public class Args {
199200

200201
@Getter
201202
@Setter
202-
private boolean getTransactionFromThisCountFeature;
203+
private boolean getTransactionsFromThisCountFeature;
203204

204205
@Getter
205206
@Setter
206-
private boolean getTransactionToThisCountFeature;
207+
private boolean getTransactionsToThisCountFeature;
208+
209+
@Getter
210+
@Setter
211+
private boolean getTransactionsByTimestampFeature;
212+
213+
@Getter
214+
@Setter
215+
private boolean getTransactionsByTimestampCountFeature;
207216

208217
public static void clearParam() {
209218
INSTANCE.outputDirectory = "output-directory";
@@ -242,8 +251,10 @@ public static void clearParam() {
242251
INSTANCE.trustNodeAddr = "";
243252
INSTANCE.getTransactionsFromThisFeature = false;
244253
INSTANCE.getTransactionsToThisFeature = false;
245-
INSTANCE.getTransactionFromThisCountFeature = false;
246-
INSTANCE.getTransactionToThisCountFeature = false;
254+
INSTANCE.getTransactionsFromThisCountFeature = false;
255+
INSTANCE.getTransactionsToThisCountFeature = false;
256+
INSTANCE.getTransactionsByTimestampFeature = false;
257+
INSTANCE.getTransactionsByTimestampCountFeature = false;
247258
}
248259

249260
/**
@@ -374,13 +385,17 @@ public static void setParam(final String[] args, final String confFileName) {
374385
.getInt("node.validateSignThreadNum") : Runtime.getRuntime().availableProcessors() / 2;
375386

376387

377-
INSTANCE.getTransactionsFromThisFeature = config.hasPath("apiFeatures.getTransactionsFromThisFeature") && config.getBoolean("apiFeatures.getTransactionsFromThisFeature");
388+
INSTANCE.getTransactionsFromThisFeature = config.hasPath("solidityNodeApiFeatures.getTransactionsFromThisFeature") && config.getBoolean("solidityNodeApiFeatures.getTransactionsFromThisFeature");
389+
390+
INSTANCE.getTransactionsToThisFeature = config.hasPath("solidityNodeApiFeatures.getTransactionsToThisFeature") && config.getBoolean("solidityNodeApiFeatures.getTransactionsToThisFeature");
391+
392+
INSTANCE.getTransactionsFromThisCountFeature = config.hasPath("solidityNodeApiFeatures.getTransactionsFromThisCountFeature") && config.getBoolean("solidityNodeApiFeatures.getTransactionsFromThisCountFeature");
378393

379-
INSTANCE.getTransactionsToThisFeature = config.hasPath("apiFeatures.getTransactionsToThisFeature") && config.getBoolean("apiFeatures.getTransactionsToThisFeature");
394+
INSTANCE.getTransactionsToThisCountFeature = config.hasPath("solidityNodeApiFeatures.getTransactionsToThisCountFeature") && config.getBoolean("solidityNodeApiFeatures.getTransactionsToThisCountFeature");
380395

381-
INSTANCE.getTransactionFromThisCountFeature = config.hasPath("apiFeatures.getTransactionFromThisCountFeature") && config.getBoolean("apiFeatures.getTransactionFromThisCountFeature");
396+
INSTANCE.getTransactionsByTimestampFeature = config.hasPath("solidityNodeApiFeatures.getTransactionsByTimestampFeature") && config.getBoolean("solidityNodeApiFeatures.getTransactionsByTimestampFeature");
382397

383-
INSTANCE.getTransactionsToThisFeature = config.hasPath("apiFeatures.getTransactionToThisCountFeature") && config.getBoolean("apiFeatures.getTransactionToThisCountFeature");
398+
INSTANCE.getTransactionsByTimestampCountFeature = config.hasPath("solidityNodeApiFeatures.getTransactionsByTimestampCountFeature") && config.getBoolean("solidityNodeApiFeatures.getTransactionsByTimestampCountFeature");
384399
}
385400

386401

‎src/main/java/org/tron/core/db/api/StoreAPI.java‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static com.googlecode.cqengine.query.QueryFactory.queryOptions;
1515
import static com.googlecode.cqengine.query.QueryFactory.threshold;
1616
import static com.googlecode.cqengine.query.option.EngineThresholds.INDEX_ORDERING_SELECTIVITY;
17+
import static org.tron.core.config.Parameter.DataBaseConstants.TRANSACTIONS_COUNT_LIMIT_MAX;
1718

1819
import com.google.common.collect.ImmutableList;
1920
import com.google.common.collect.Lists;
@@ -45,7 +46,6 @@ public class StoreAPI {
4546

4647
@Autowired(required = false)
4748
private IndexHelper indexHelper;
48-
private final int TRANSACTIONS_COUNT_LIMIT_MAX = 1000;
4949

5050
/********************************************************************************
5151
* account api *
@@ -207,7 +207,7 @@ public Transaction getTransactionById(String id) throws NonUniqueObjectException
207207
}
208208
}
209209

210-
public List<Transaction> getTransactionsFromThis(String address,int offset,int limit) {
210+
public List<Transaction> getTransactionsFromThis(String address,long offset,long limit) {
211211
if (StringUtils.isEmpty(address)) {
212212
logger.info("address is empty");
213213
return Lists.newArrayList();
@@ -234,7 +234,7 @@ public long getTransactionsFromThisCount(String address) {
234234
}
235235
}
236236

237-
public List<Transaction> getTransactionsToThis(String address,int offset, int limit) {
237+
public List<Transaction> getTransactionsToThis(String address, long offset, long limit) {
238238
if (StringUtils.isEmpty(address)) {
239239
logger.info("address is empty");
240240
return Lists.newArrayList();
@@ -246,7 +246,7 @@ public List<Transaction> getTransactionsToThis(String address,int offset, int li
246246
queryOptions(
247247
orderBy(ascending(TransactionIndex.TIMESTAMP)),
248248
applyThresholds(threshold(INDEX_ORDERING_SELECTIVITY, 1.0))))) {
249-
if(limit > TRANSACTIONS_COUNT_LIMIT_MAX) {
249+
if (limit > TRANSACTIONS_COUNT_LIMIT_MAX) {
250250
limit = TRANSACTIONS_COUNT_LIMIT_MAX;
251251
}
252252
return ImmutableList.copyOf(Streams.stream(resultSet).skip(offset).limit(limit).iterator());
@@ -277,7 +277,7 @@ public List<Transaction> getTransactionsRelatedToAccount(String accountAddress)
277277
}
278278

279279
public List<Transaction> getTransactionsByTimestamp(
280-
long beginInMilliseconds, long endInMilliseconds) {
280+
long beginInMilliseconds, long endInMilliseconds, long offset, long limit) {
281281
if (endInMilliseconds < beginInMilliseconds) {
282282
return Collections.emptyList();
283283
}
@@ -286,7 +286,22 @@ public List<Transaction> getTransactionsByTimestamp(
286286
try (ResultSet<Transaction> resultSet =
287287
index.retrieve(
288288
between(TransactionIndex.TIMESTAMP, beginInMilliseconds, endInMilliseconds))) {
289-
return ImmutableList.copyOf(resultSet);
289+
if (limit > TRANSACTIONS_COUNT_LIMIT_MAX) {
290+
limit = TRANSACTIONS_COUNT_LIMIT_MAX;
291+
}
292+
return ImmutableList.copyOf(Streams.stream(resultSet).skip(offset).limit(limit).iterator());
293+
}
294+
}
295+
296+
public long getTransactionsByTimestampCount(long beginInMilliseconds, long endInMilliseconds) {
297+
if (endInMilliseconds < beginInMilliseconds) {
298+
return 0;
299+
}
300+
Index.Iface<Transaction> index = indexHelper.getTransactionIndex();
301+
try (ResultSet<Transaction> resultSet =
302+
index.retrieve(
303+
between(TransactionIndex.TIMESTAMP, beginInMilliseconds, endInMilliseconds))) {
304+
return resultSet.size();
290305
}
291306
}
292307

‎src/main/java/org/tron/core/services/RpcApiService.java‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.tron.api.GrpcAPI.NodeList;
3030
import org.tron.api.GrpcAPI.NumberMessage;
3131
import org.tron.api.GrpcAPI.TimeMessage;
32+
import org.tron.api.GrpcAPI.TimePaginatedMessage;
3233
import org.tron.api.GrpcAPI.TransactionList;
3334
import org.tron.api.GrpcAPI.WitnessList;
3435
import org.tron.api.WalletGrpc.WalletImplBase;
@@ -104,7 +105,7 @@ public void start() {
104105
.executor(Executors.newFixedThreadPool(args.getRpcThreadNum()));
105106
}
106107
if (args.isSolidityNode()) {
107-
serverBuilder = serverBuilder.addService(new WalletSolidityApi(args));
108+
serverBuilder = serverBuilder.addService(new WalletSolidityApi());
108109
} else {
109110
serverBuilder = serverBuilder.addService(new WalletApi());
110111
}
@@ -184,18 +185,6 @@ public void getDynamicProperties(EmptyMessage request,
184185
*/
185186
private class WalletSolidityApi extends WalletSolidityImplBase {
186187

187-
private boolean getTransactionsFromThisFeature;
188-
private boolean getTransactionsToThisFeature;
189-
private boolean getTransactionsFromThisCountFeature;
190-
private boolean getTransactionsToThisCountFeature;
191-
192-
WalletSolidityApi(Args args) {
193-
this.getTransactionsFromThisFeature = args.isGetTransactionsFromThisFeature();
194-
this.getTransactionsToThisFeature = args.isGetTransactionsToThisFeature();
195-
this.getTransactionsFromThisCountFeature = args.isGetTransactionFromThisCountFeature();
196-
this.getTransactionsToThisCountFeature = args.isGetTransactionsToThisFeature();
197-
}
198-
199188
@Override
200189
public void getAccount(Account request, StreamObserver<Account> responseObserver) {
201190
ByteString addressBs = request.getAddress();
@@ -303,14 +292,37 @@ public void getTransactionById(BytesMessage request,
303292

304293

305294
@Override
306-
public void getTransactionsByTimestamp(TimeMessage request,
295+
public void getTransactionsByTimestamp(TimePaginatedMessage request,
307296
StreamObserver<TransactionList> responseObserver) {
297+
if (! Args.getInstance().isGetTransactionsByTimestampFeature()) {
298+
responseObserver.onNext(null);
299+
responseObserver.onCompleted();
300+
return;
301+
}
302+
TimeMessage timeMessage = request.getTimeMessage();
303+
long beginTime = timeMessage.getBeginInMilliseconds();
304+
long endTime = timeMessage.getEndInMilliseconds();
305+
if (beginTime < 0 || endTime < 0 || endTime < beginTime) {
306+
responseObserver.onNext(null);
307+
} else {
308+
TransactionList reply = walletSolidity.getTransactionsByTimestamp(beginTime, endTime, request.getOffset(), request.getLimit() );
309+
responseObserver.onNext(reply);
310+
}
311+
responseObserver.onCompleted();
312+
}
313+
314+
public void getTransactionsByTimestampCount(TimeMessage request, StreamObserver<NumberMessage> responseObserver) {
315+
if (!Args.getInstance().isGetTransactionsByTimestampCountFeature()) {
316+
responseObserver.onNext(null);
317+
responseObserver.onCompleted();
318+
return;
319+
}
308320
long beginTime = request.getBeginInMilliseconds();
309321
long endTime = request.getEndInMilliseconds();
310322
if (beginTime < 0 || endTime < 0 || endTime < beginTime) {
311323
responseObserver.onNext(null);
312324
} else {
313-
TransactionList reply = walletSolidity.getTransactionsByTimestamp(beginTime, endTime);
325+
NumberMessage reply = walletSolidity.getTransactionsByTimestampCount(beginTime, endTime);
314326
responseObserver.onNext(reply);
315327
}
316328
responseObserver.onCompleted();
@@ -319,7 +331,7 @@ public void getTransactionsByTimestamp(TimeMessage request,
319331
@Override
320332
public void getTransactionsFromThis(AccountPaginated request,
321333
StreamObserver<GrpcAPI.TransactionList> responseObserver) {
322-
if (!this.getTransactionsFromThisFeature) {
334+
if (!Args.getInstance().isGetTransactionsFromThisFeature()) {
323335
responseObserver.onNext(null);
324336
responseObserver.onCompleted();
325337
return;
@@ -337,7 +349,7 @@ public void getTransactionsFromThis(AccountPaginated request,
337349
@Override
338350
public void getTransactionsToThis(AccountPaginated request,
339351
StreamObserver<GrpcAPI.TransactionList> responseObserver) {
340-
if(!this.getTransactionsToThisFeature ) {
352+
if(!Args.getInstance().isGetTransactionsToThisFeature() ) {
341353
responseObserver.onNext(null);
342354
responseObserver.onCompleted();
343355
return;
@@ -354,7 +366,7 @@ public void getTransactionsToThis(AccountPaginated request,
354366

355367
@Override
356368
public void getTransactionsFromThisCount(Account account, StreamObserver<NumberMessage> responseObserver) {
357-
if (!this.getTransactionsFromThisCountFeature) {
369+
if (!Args.getInstance().isGetTransactionsFromThisCountFeature()) {
358370
responseObserver.onNext(null);
359371
responseObserver.onCompleted();
360372
return;
@@ -370,7 +382,7 @@ public void getTransactionsFromThisCount(Account account, StreamObserver<NumberM
370382

371383
@Override
372384
public void getTransactionsToThisCount(Account account, StreamObserver<NumberMessage> responseObserver) {
373-
if(!this.getTransactionsToThisCountFeature ) {
385+
if(!Args.getInstance().isGetTransactionsToThisCountFeature() ) {
374386
responseObserver.onNext(null);
375387
responseObserver.onCompleted();
376388
return;

‎src/main/protos/api/api.proto‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ service WalletSolidity {
368368
}
369369
};
370370
}
371-
rpc GetTransactionsByTimestamp (TimeMessage) returns (TransactionList) {
371+
rpc GetTransactionsByTimestamp (TimePaginatedMessage) returns (TransactionList) {
372372
option (google.api.http) = {
373373
post: "/walletsolidity/gettransactionsbytimestamp"
374374
body: "*"
@@ -377,6 +377,15 @@ service WalletSolidity {
377377
}
378378
};
379379
}
380+
rpc GetTransactionsByTimestampCount (TimeMessage) returns (NumberMessage) {
381+
option (google.api.http) = {
382+
post: "/walletsolidity/gettransactionsbytimestampcount"
383+
body: "*"
384+
additional_bindings {
385+
get: "/walletsolidity/gettransactionsbytimestampcount"
386+
}
387+
};
388+
}
380389
rpc GetTransactionsFromThis (AccountPaginated) returns (TransactionList) {
381390
option (google.api.http) = {
382391
post: "/walletsolidity/gettransactionsfromthis"
@@ -397,19 +406,19 @@ service WalletSolidity {
397406
}
398407
rpc GetTransactionsFromThisCount (Account) returns (NumberMessage) {
399408
option (google.api.http) = {
400-
post: "/walletsolidity/getTransactionsFromThisCount"
409+
post: "/walletsolidity/gettransactionsfromthiscount"
401410
body: "*"
402411
additional_bindings {
403-
get: "/walletsolidity/getTransactionsFromThisCount"
412+
get: "/walletsolidity/gettransactionsfromthiscount"
404413
}
405414
};
406415
}
407416
rpc GetTransactionsToThisCount (Account) returns (NumberMessage) {
408417
option (google.api.http) = {
409-
post: "/walletsolidity/getTransactionsToThisCount"
418+
post: "/walletsolidity/gettransactionstothiscount"
410419
body: "*"
411420
additional_bindings {
412-
get: "/walletsolidity/getTransactionsToThisCount"
421+
get: "/walletsolidity/gettransactionstothiscount"
413422
}
414423
};
415424
}
@@ -513,6 +522,11 @@ message TransactionLimit {
513522
}
514523
message AccountPaginated {
515524
Account account = 1;
516-
int32 offset = 2;
517-
int32 limit = 3;
525+
int64 offset = 2;
526+
int64 limit = 3;
527+
}
528+
message TimePaginatedMessage {
529+
TimeMessage timeMessage = 1;
530+
int64 offset = 2;
531+
int64 limit = 3;
518532
}

‎src/main/resources/config.conf‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,11 @@ block = {
267267
maintenanceTimeInterval = 21600000 // 1 day: 86400000(ms), 6 hours: 21600000(ms)
268268
}
269269

270-
apiFeatures = {
271-
getTransactionsFromThisFeature = true
272-
getTransactionsToThisFeature = true
273-
getTransactionToThisCountFeature= true
274-
getTransactionFromThisCountFeature = true
275-
}
270+
//solidityNodeApiFeatures = {
271+
// getTransactionsFromThisFeature = true
272+
// getTransactionsToThisFeature = true
273+
// getTransactionsToThisCountFeature= true
274+
// getTransactionsFromThisCountFeature = true
275+
// getTransactionsByTimestampFeature = true
276+
// getTransactionsByTimestampCountFeature = true
277+
//}

0 commit comments

Comments
 (0)