Skip to content

Commit d910713

Browse files
authored
feat(api):fix a concurrency issue for toString in BlockCapsule (#5657)
1 parent 440d062 commit d910713

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

chainbase/src/main/java/org/tron/core/capsule/BlockCapsule.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public class BlockCapsule implements ProtoCapsule<Block> {
5656

5757
private Block block;
5858
private List<TransactionCapsule> transactions = new ArrayList<>();
59-
private StringBuilder toStringBuff = new StringBuilder();
6059
private boolean isSwitch;
6160
@Getter
6261
@Setter
@@ -314,7 +313,7 @@ public boolean hasWitnessSignature() {
314313

315314
@Override
316315
public String toString() {
317-
toStringBuff.setLength(0);
316+
StringBuilder toStringBuff = new StringBuilder();
318317

319318
toStringBuff.append("BlockCapsule \n[ ");
320319
toStringBuff.append("hash=").append(getBlockId()).append("\n");

framework/src/test/java/org/tron/core/capsule/BlockCapsuleTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.google.protobuf.ByteString;
44
import java.io.IOException;
5+
import java.util.ArrayList;
56
import java.util.Arrays;
7+
import java.util.List;
68
import lombok.extern.slf4j.Slf4j;
79
import org.junit.AfterClass;
810
import org.junit.Assert;
@@ -146,4 +148,20 @@ public void testGetTimeStamp() {
146148
Assert.assertEquals(1234L, blockCapsule0.getTimeStamp());
147149
}
148150

151+
@Test
152+
public void testConcurrentToString() throws InterruptedException {
153+
List<Thread> threadList = new ArrayList<>();
154+
int n = 10;
155+
for (int i = 0; i < n; i++) {
156+
threadList.add(new Thread(() -> blockCapsule0.toString()));
157+
}
158+
for (int i = 0; i < n; i++) {
159+
threadList.get(i).start();
160+
}
161+
for (int i = 0; i < n; i++) {
162+
threadList.get(i).join();
163+
}
164+
Assert.assertTrue(true);
165+
}
166+
149167
}

0 commit comments

Comments
 (0)