Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PerformanceBalance async interface bug #214

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 52 additions & 41 deletions src/main/java/org/fisco/bcos/sdk/demo/perf/PerformanceBalance.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.v3.model.ConstantConfig;
import org.fisco.bcos.sdk.v3.model.CryptoType;
import org.fisco.bcos.sdk.v3.model.RetCode;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback;
Expand Down Expand Up @@ -115,7 +114,7 @@ public static void start(
+ committeePath);
RateLimiter limiter = RateLimiter.create(qps);
// get governor cryptoSuite
CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
CryptoSuite cryptoSuite = new CryptoSuite(client.getCryptoType());
cryptoSuite.loadAccount("pem", committeePath, "");
CryptoKeyPair committee = cryptoSuite.getCryptoKeyPair();
BalanceService balanceService = new BalanceService(client, committee);
Expand Down Expand Up @@ -157,27 +156,31 @@ public static void start(
CryptoSuite[] cryptoSuites = new CryptoSuite[userCount];
IntStream.range(0, userCount)
.parallel()
.forEach(i -> cryptoSuites[i] = new CryptoSuite(CryptoType.ECDSA_TYPE));
.forEach(i -> cryptoSuites[i] = new CryptoSuite(client.getCryptoType()));

// init eoa accounts, use async
System.out.println("===2. init eoa accounts===");
for (int i = 0; i < userCount; i++) {
CompletableFuture<RetCode> retCodeCompletableFuture = new CompletableFuture<>();
balanceService.addBalanceAsync(
cryptoSuites[i].getCryptoKeyPair().getAddress(),
"100000000",
Convert.Unit.WEI,
new PrecompiledCallback() {
@Override
public void onResponse(RetCode retCode) {
if (!(retCode.getCode() == 0)) {
System.out.println(
"init eoa account failed, status: "
+ retCode.getCode()
+ ", message: "
+ retCode.getMessage());
}
}
retCode -> {
retCodeCompletableFuture.complete(retCode);
});
try {
RetCode retCode = retCodeCompletableFuture.get();
if (!(retCode.getCode() == 0)) {
System.out.println(
"init eoa account failed, status: "
+ retCode.getCode()
+ ", message: "
+ retCode.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}

// start eoa transfer
Expand Down Expand Up @@ -286,15 +289,15 @@ public void onResponse(
case "contractTransfer":
// create contract accounts
System.out.println("===1. create contract accounts===");
if (userCount < 2) {
System.out.println("userCount must be greater than 2");
if (userCount % 10 != 0) {
System.out.println("userCount must be multiple of 10");
return;
}
List<String> contractsAddress = new ArrayList<>();

List<CompletableFuture<List<String>>> allFutures = new ArrayList<>();

int batchSize = 100;
int batchSize = 10;
int numTasks = userCount / batchSize;

for (int i = 0; i < numTasks; i++) {
Expand Down Expand Up @@ -337,22 +340,26 @@ public void onResponse(
// init contract balance
System.out.println("===2. init contract balance===");
for (int i = 0; i < userCount; i++) {
CompletableFuture<RetCode> retCodeCompletableFuture = new CompletableFuture<>();
balanceService.addBalanceAsync(
contractsAddress.get(i),
"100000000",
Convert.Unit.WEI,
new PrecompiledCallback() {
@Override
public void onResponse(RetCode retCode) {
if (!(retCode.getCode() == 0)) {
System.out.println(
"init contract account failed, status: "
+ retCode.getCode()
+ ", message: "
+ retCode.getMessage());
}
}
retCode -> {
retCodeCompletableFuture.complete(retCode);
});
try {
RetCode retCode = retCodeCompletableFuture.get();
if (!(retCode.getCode() == 0)) {
System.out.println(
"init contract account failed, status: "
+ retCode.getCode()
+ ", message: "
+ retCode.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}

// start contract transfer
Expand Down Expand Up @@ -459,31 +466,35 @@ public void onResponse(
CryptoSuite[] eoaCryptoSuites = new CryptoSuite[userCount];
IntStream.range(0, userCount)
.parallel()
.forEach(i -> eoaCryptoSuites[i] = new CryptoSuite(CryptoType.ECDSA_TYPE));
.forEach(i -> eoaCryptoSuites[i] = new CryptoSuite(client.getCryptoType()));

// init eoa accounts, use async
System.out.println("===2. init eoa accounts===");
for (int i = 0; i < userCount; i++) {
CompletableFuture<RetCode> retCodeCompletableFuture = new CompletableFuture<>();
balanceService.addBalanceAsync(
eoaCryptoSuites[i].getCryptoKeyPair().getAddress(),
"100000000",
Convert.Unit.WEI,
new PrecompiledCallback() {
@Override
public void onResponse(RetCode retCode) {
if (!(retCode.getCode() == 0)) {
System.out.println(
"init eoa account failed, status: "
+ retCode.getCode()
+ ", message: "
+ retCode.getMessage());
}
}
retCode -> {
retCodeCompletableFuture.complete(retCode);
});
try {
RetCode retCode = retCodeCompletableFuture.get();
if (!(retCode.getCode() == 0)) {
System.out.println(
"init eoa account failed, status: "
+ retCode.getCode()
+ ", message: "
+ retCode.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}

// PerformanceBalancePrecompiled Test
System.out.println("===2. PerformanceBalancePrecompiled Test===");
System.out.println("===3. PerformanceBalancePrecompiled Test===");
Collector collector2 = new Collector();
collector2.setTotal(total);
for (int i = 0; i < total; i++) {
Expand Down