tip: 586
title: GRPC Implementation for Resource Price Interfaces
author: lxcmyf@gmail.com
discussions-to: https://github.com/tronprotocol/tips/issues/586
category : Core
status: Final
type: Standards Track
created: 2023-08-29
GRPC is a high-performance, general-purpose RPC framework open-sourced by Google. This TIP aims to improve gRPC calls for some specific interfaces in Java-tron.
Currently, the interfaces /wallet/getbandwidthprices
and /wallet/getenergyprices
provided by Java-tron do not support gRPC calls. This TIP will implement this function.
Currently, /wallet/getbandwidthprices
and /wallet/getenergyprices
only support HTTP calls. However, this limits developers and users who want to use these interfaces through methods like gRPC, since they won't be able to access historical unit price information of resources through this method. Additionally, it makes it inflexible for external API use.
As mentioned above, Java-tron will modify the api.proto
file to add gRPC implementations of /wallet/getbandwidthprices
and /wallet/getenergyprices
:
rpc GetBandwidthPrices (EmptyMessage) returns (PricesResponseMessage) {
}
rpc GetEnergyPrices (EmptyMessage) returns (PricesResponseMessage) {
}
message PricesResponseMessage {
string prices = 1;
}
Since the underlying implementation of the two interfaces /wallet/getbandwidthprices
and /wallet/getenergyprices
has been completed, it is only necessary to open the gRPC implementation entry for them.
Since the protobuf protocol has been defined above, it is only necessary to add the gRPC implementation code in RpcApiService.java
:
@Override
public void getBandwidthPrices(EmptyMessage request,
StreamObserver<PricesResponseMessage> responseObserver) {
try {
responseObserver.onNext(wallet.getBandwidthPrices());
} catch (Exception e) {
responseObserver.onError(getRunTimeException(e));
}
responseObserver.onCompleted();
}
@Override
public void getEnergyPrices(EmptyMessage request,
StreamObserver<PricesResponseMessage> responseObserver) {
try {
responseObserver.onNext(wallet.getEnergyPrices());
} catch (Exception e) {
responseObserver.onError(getRunTimeException(e));
}
responseObserver.onCompleted();
}
In addition, they also support solidity and PBFT API, so gRPC implementations need to be added in RpcApiServiceOnSolidity.java
and RpcApiServiceOnPBFT.java
respectively as well:
RpcApiServiceOnSolidity.java
@Override
public void getBandwidthPrices(EmptyMessage request,
StreamObserver<PricesResponseMessage> responseObserver) {
walletOnSolidity.futureGet(
() -> rpcApiService.getWalletSolidityApi().getBandwidthPrices(request, responseObserver));
}
@Override
public void getEnergyPrices(EmptyMessage request,
StreamObserver<PricesResponseMessage> responseObserver) {
walletOnSolidity.futureGet(
() -> rpcApiService.getWalletSolidityApi().getEnergyPrices(request, responseObserver));
}
RpcApiServiceOnPBFT.java
@Override
public void getBandwidthPrices(EmptyMessage request,
StreamObserver<PricesResponseMessage> responseObserver) {
walletOnPBFT.futureGet(
() -> rpcApiService.getWalletSolidityApi().getBandwidthPrices(request, responseObserver));
}
@Override
public void getEnergyPrices(EmptyMessage request,
StreamObserver<PricesResponseMessage> responseObserver) {
walletOnPBFT.futureGet(
() -> rpcApiService.getWalletSolidityApi().getEnergyPrices(request, responseObserver));
}