Skip to content

Merge master into release_v4.7.4 branch #5669

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

Merged
merged 10 commits into from
Jan 12, 2024
Next Next commit
feat(api):fix a concurrency issue for toString in BlockCapsule (#5657)
  • Loading branch information
o85372940 authored Jan 11, 2024
commit d9107135c4a159bb52f226ded124b7d024e8cf89
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class BlockCapsule implements ProtoCapsule<Block> {

private Block block;
private List<TransactionCapsule> transactions = new ArrayList<>();
private StringBuilder toStringBuff = new StringBuilder();
private boolean isSwitch;
@Getter
@Setter
Expand Down Expand Up @@ -314,7 +313,7 @@ public boolean hasWitnessSignature() {

@Override
public String toString() {
toStringBuff.setLength(0);
StringBuilder toStringBuff = new StringBuilder();

toStringBuff.append("BlockCapsule \n[ ");
toStringBuff.append("hash=").append(getBlockId()).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.google.protobuf.ByteString;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.Assert;
Expand Down Expand Up @@ -146,4 +148,20 @@ public void testGetTimeStamp() {
Assert.assertEquals(1234L, blockCapsule0.getTimeStamp());
}

@Test
public void testConcurrentToString() throws InterruptedException {
List<Thread> threadList = new ArrayList<>();
int n = 10;
for (int i = 0; i < n; i++) {
threadList.add(new Thread(() -> blockCapsule0.toString()));
}
for (int i = 0; i < n; i++) {
threadList.get(i).start();
}
for (int i = 0; i < n; i++) {
threadList.get(i).join();
}
Assert.assertTrue(true);
}

}