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

feat(net): fix bug of method toString in BlockCapsule #5594

Merged
Merged
Show file tree
Hide file tree
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
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not replace the origin field with StringBuffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lurais toStringBuff is only used in toString() method, using local variable makes code easier to understand.


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);
}

}