去掉依赖
This commit is contained in:
1 parent
7cb16bc84d
commit
4c4fd99fc4
6 files changed
+90
-24
No files matched your search
@@ -22,10 +22,9 @@ import org.bouncycastle.math.ec.ECPoint;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.tron.api.GrpcAPI;
|
||||
import org.tron.api.WalletGrpc;
|
||||
import org.tron.trident.api.GrpcAPI;
|
||||
import org.tron.trident.api.WalletGrpc;
|
||||
import com.ruoyi.system.chain.TronKey;
|
||||
import org.tron.protos.Protocol;
|
||||
import org.tron.trident.abi.FunctionEncoder;
|
||||
import org.tron.trident.abi.datatypes.Address;
|
||||
import org.tron.trident.abi.datatypes.Function;
|
||||
@@ -190,9 +189,10 @@ public class TronNileServiceImpl implements ITronService {
|
||||
try {
|
||||
WalletGrpc.WalletBlockingStub stub = WalletGrpc.newBlockingStub(channel);
|
||||
byte[] address = Base58Check.base58ToBytes(ownerAddr);
|
||||
Protocol.Account account = Protocol.Account.newBuilder()
|
||||
// Trident 使用专用地址请求消息,保留原始 21 字节 TRON 地址。
|
||||
GrpcAPI.AccountAddressMessage account = GrpcAPI.AccountAddressMessage.newBuilder()
|
||||
.setAddress(ByteString.copyFrom(address)).build();
|
||||
GrpcAPI.AccountResourceMessage res = stub.getAccountResource(account);
|
||||
Response.AccountResourceMessage res = stub.getAccountResource(account);
|
||||
long bandwidth = Math.max(0L, res.getFreeNetLimit() - res.getFreeNetUsed())
|
||||
+ Math.max(0L, res.getNetLimit() - res.getNetUsed());
|
||||
long energy = Math.max(0L, res.getEnergyLimit() - res.getEnergyUsed());
|
||||
|
||||
@@ -20,10 +20,9 @@ import org.bouncycastle.crypto.digests.SM3Digest;
|
||||
import org.bouncycastle.jcajce.provider.digest.Keccak;
|
||||
import org.bouncycastle.math.ec.ECPoint;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.tron.api.GrpcAPI;
|
||||
import org.tron.api.WalletGrpc;
|
||||
import org.tron.trident.api.GrpcAPI;
|
||||
import org.tron.trident.api.WalletGrpc;
|
||||
import com.ruoyi.system.chain.TronKey;
|
||||
import org.tron.protos.Protocol;
|
||||
import org.tron.trident.abi.FunctionEncoder;
|
||||
import org.tron.trident.abi.datatypes.Address;
|
||||
import org.tron.trident.abi.datatypes.Function;
|
||||
@@ -231,9 +230,10 @@ public class TronProServiceImpl implements ITronService {
|
||||
try {
|
||||
WalletGrpc.WalletBlockingStub stub = WalletGrpc.newBlockingStub(channel);
|
||||
byte[] address = Base58Check.base58ToBytes(ownerAddr);
|
||||
Protocol.Account account = Protocol.Account.newBuilder()
|
||||
// Trident 使用专用地址请求消息,保留原始 21 字节 TRON 地址。
|
||||
GrpcAPI.AccountAddressMessage account = GrpcAPI.AccountAddressMessage.newBuilder()
|
||||
.setAddress(ByteString.copyFrom(address)).build();
|
||||
GrpcAPI.AccountResourceMessage res = stub.getAccountResource(account);
|
||||
Response.AccountResourceMessage res = stub.getAccountResource(account);
|
||||
long bandwidth = Math.max(0L, res.getFreeNetLimit() - res.getFreeNetUsed())
|
||||
+ Math.max(0L, res.getNetLimit() - res.getNetUsed());
|
||||
long energy = Math.max(0L, res.getEnergyLimit() - res.getEnergyUsed());
|
||||
|
||||
@@ -23,10 +23,9 @@ import org.bouncycastle.math.ec.ECPoint;
|
||||
import org.bouncycastle.util.encoders.Hex;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.tron.api.GrpcAPI;
|
||||
import org.tron.api.WalletGrpc;
|
||||
import org.tron.trident.api.GrpcAPI;
|
||||
import org.tron.trident.api.WalletGrpc;
|
||||
import com.ruoyi.system.chain.TronKey;
|
||||
import org.tron.protos.Protocol;
|
||||
import org.tron.trident.abi.FunctionEncoder;
|
||||
import org.tron.trident.abi.datatypes.Address;
|
||||
import org.tron.trident.abi.datatypes.Function;
|
||||
@@ -264,9 +263,10 @@ public class TronShastaServiceImpl implements ITronService {
|
||||
try {
|
||||
WalletGrpc.WalletBlockingStub stub = WalletGrpc.newBlockingStub(channel);
|
||||
byte[] address = Base58Check.base58ToBytes(ownerAddr);
|
||||
Protocol.Account account = Protocol.Account.newBuilder()
|
||||
// Trident 使用专用地址请求消息,保留原始 21 字节 TRON 地址。
|
||||
GrpcAPI.AccountAddressMessage account = GrpcAPI.AccountAddressMessage.newBuilder()
|
||||
.setAddress(ByteString.copyFrom(address)).build();
|
||||
GrpcAPI.AccountResourceMessage res = stub.getAccountResource(account);
|
||||
Response.AccountResourceMessage res = stub.getAccountResource(account);
|
||||
long bandwidth = Math.max(0L, res.getFreeNetLimit() - res.getFreeNetUsed())
|
||||
+ Math.max(0L, res.getNetLimit() - res.getNetUsed());
|
||||
long energy = Math.max(0L, res.getEnergyLimit() - res.getEnergyUsed());
|
||||
|
||||
@@ -4,9 +4,9 @@ import io.grpc.ManagedChannel;
|
||||
import io.grpc.ManagedChannelBuilder;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
import org.tron.api.GrpcAPI;
|
||||
import org.tron.api.WalletGrpc;
|
||||
import org.tron.protos.Protocol;
|
||||
import org.tron.trident.api.GrpcAPI;
|
||||
import org.tron.trident.api.WalletGrpc;
|
||||
import org.tron.trident.proto.Chain;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -25,7 +25,7 @@ public class TronGrpcConnectivityTest {
|
||||
private void assertLatestBlock(String host) throws Exception {
|
||||
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, 50051).usePlaintext().build();
|
||||
try {
|
||||
Protocol.Block block = WalletGrpc.newBlockingStub(channel)
|
||||
Chain.Block block = WalletGrpc.newBlockingStub(channel)
|
||||
.withDeadlineAfter(60, TimeUnit.SECONDS)
|
||||
.getNowBlock(GrpcAPI.EmptyMessage.getDefaultInstance());
|
||||
assertTrue(host + " 未返回有效区块", block.hasBlockHeader()
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import com.ruoyi.system.domain.vo.EnergyAndBandwidthVo;
|
||||
import com.ruoyi.system.service.ITronService;
|
||||
import io.grpc.Server;
|
||||
import io.grpc.ServerBuilder;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.Test;
|
||||
import org.tron.trident.api.GrpcAPI;
|
||||
import org.tron.trident.api.WalletGrpc;
|
||||
import org.tron.trident.proto.Response;
|
||||
import org.tron.trident.utils.Base58Check;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** 使用本地 gRPC 节点验证三个网络实现,避免依赖公网或广播交易。 */
|
||||
public class TronResourceGrpcTest {
|
||||
@Test
|
||||
public void allNetworksKeepResourceQueryAndNonNegativeAvailability() throws Exception {
|
||||
String address = "TMVQGm1qAQYVdetCeGRRkTWYYrLXuHK2HC";
|
||||
Server server = ServerBuilder.forPort(0).addService(new WalletGrpc.WalletImplBase() {
|
||||
@Override
|
||||
public void getAccountResource(GrpcAPI.AccountAddressMessage request,
|
||||
StreamObserver<Response.AccountResourceMessage> observer) {
|
||||
// 同时验证地址字节和协议字段编号,防止包迁移导致请求字段错位。
|
||||
if (!request.getAddress().equals(com.google.protobuf.ByteString.copyFrom(
|
||||
Base58Check.base58ToBytes(address)))
|
||||
|| GrpcAPI.AccountAddressMessage.ADDRESS_FIELD_NUMBER != 3) {
|
||||
observer.onError(io.grpc.Status.INVALID_ARGUMENT.asRuntimeException());
|
||||
return;
|
||||
}
|
||||
observer.onNext(Response.AccountResourceMessage.newBuilder()
|
||||
.setFreeNetLimit(600).setFreeNetUsed(100)
|
||||
.setNetLimit(1000).setNetUsed(1200)
|
||||
.setEnergyLimit(2000).setEnergyUsed(2200).build());
|
||||
observer.onCompleted();
|
||||
}
|
||||
}).build().start();
|
||||
final int port = server.getPort();
|
||||
try {
|
||||
ITronService[] services = {
|
||||
new TronProServiceImpl(null) {
|
||||
@Override public String grpcUrl() { return "127.0.0.1"; }
|
||||
@Override public Integer grpcPort() { return port; }
|
||||
},
|
||||
new TronNileServiceImpl(null) {
|
||||
@Override public String grpcUrl() { return "127.0.0.1"; }
|
||||
@Override public Integer grpcPort() { return port; }
|
||||
},
|
||||
new TronShastaServiceImpl(null) {
|
||||
@Override public String grpcUrl() { return "127.0.0.1"; }
|
||||
@Override public Integer grpcPort() { return port; }
|
||||
}
|
||||
};
|
||||
for (ITronService service : services) {
|
||||
EnergyAndBandwidthVo result = service.energyAndBandwidth(address);
|
||||
assertEquals(0, BigDecimal.valueOf(500).compareTo(result.getBandwidth()));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo(result.getEnergy()));
|
||||
assertEquals(Long.valueOf(2000), result.getEnergyAllNum());
|
||||
assertEquals(Long.valueOf(2200), result.getEnergyUsedNum());
|
||||
assertEquals(Long.valueOf(0), result.getBandwidthStakedNum());
|
||||
assertEquals(Long.valueOf(1000), result.getBandwidthStakedAllNum());
|
||||
assertEquals(Long.valueOf(500), result.getBandwidthFreeNum());
|
||||
assertEquals(Long.valueOf(600), result.getBandwidthFreeAllNum());
|
||||
}
|
||||
} finally {
|
||||
server.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user