Skip to content

Commit 38c5298

Browse files
committed
perf(net): skip wire-byte rewrite when sanitize is a no-op
1 parent 1dd07e7 commit 38c5298

4 files changed

Lines changed: 42 additions & 18 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ public boolean hasWitnessSignature() {
329329
return !getInstance().getBlockHeader().getWitnessSignature().isEmpty();
330330
}
331331

332-
public void sanitize() {
332+
public boolean sanitize() {
333333
boolean blockHasUnknown = !this.block.getUnknownFields().asMap().isEmpty();
334334
boolean headerHasUnknown = !this.block.getBlockHeader().getUnknownFields().asMap().isEmpty();
335335
if (!blockHasUnknown && !headerHasUnknown) {
336-
return;
336+
return false;
337337
}
338338
UnknownFieldSet empty = UnknownFieldSet.getDefaultInstance();
339339
Block.Builder builder = this.block.toBuilder();
@@ -346,6 +346,7 @@ public void sanitize() {
346346
.build());
347347
}
348348
this.block = builder.build();
349+
return true;
349350
}
350351

351352
@Override

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,14 @@ public static boolean validateSignature(Transaction transaction,
495495
return false;
496496
}
497497

498-
public void sanitize() {
499-
if (!this.transaction.getUnknownFields().asMap().isEmpty()) {
500-
this.transaction = transaction.toBuilder()
501-
.setUnknownFields(UnknownFieldSet.getDefaultInstance())
502-
.build();
498+
public boolean sanitize() {
499+
if (this.transaction.getUnknownFields().asMap().isEmpty()) {
500+
return false;
503501
}
502+
this.transaction = this.transaction.toBuilder()
503+
.setUnknownFields(UnknownFieldSet.getDefaultInstance())
504+
.build();
505+
return true;
504506
}
505507

506508
public void resetResult() {

framework/src/main/java/org/tron/core/net/message/adv/BlockMessage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public BlockMessage(BlockCapsule block) {
2929
}
3030

3131
public void sanitize() {
32-
this.block.sanitize();
33-
this.data = this.block.getData();
32+
if (this.block.sanitize()) {
33+
this.data = this.block.getData();
34+
}
3435
}
3536

3637
public BlockId getBlockId() {

framework/src/test/java/org/tron/core/net/message/adv/SanitizeUnknownFieldsTest.java

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

33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertNotEquals;
7+
import static org.junit.Assert.assertSame;
68
import static org.junit.Assert.assertTrue;
79

810
import com.google.protobuf.ByteString;
@@ -67,7 +69,7 @@ public void blockCapsuleSanitizeStripsBlockLevelUnknownFields() {
6769
BlockCapsule capsule = new BlockCapsule(padded);
6870
long originalSize = capsule.getData().length;
6971

70-
capsule.sanitize();
72+
assertTrue("sanitize() should report it mutated the capsule", capsule.sanitize());
7173

7274
assertTrue("Block-level unknown fields should be stripped",
7375
capsule.getInstance().getUnknownFields().asMap().isEmpty());
@@ -85,7 +87,7 @@ public void blockCapsuleSanitizeStripsBlockHeaderOuterUnknownFields() {
8587
BlockCapsule capsule = new BlockCapsule(padded);
8688
long originalSize = capsule.getData().length;
8789

88-
capsule.sanitize();
90+
assertTrue("sanitize() should report it mutated the capsule", capsule.sanitize());
8991

9092
assertTrue("BlockHeader outer unknown fields should be stripped",
9193
capsule.getInstance().getBlockHeader().getUnknownFields().asMap().isEmpty());
@@ -109,11 +111,14 @@ public void blockCapsuleSanitizePreservesBlockHeaderRawData() {
109111
public void blockCapsuleSanitizeIsNoOpOnCleanBlock() {
110112
Block clean = sampleBlock();
111113
BlockCapsule capsule = new BlockCapsule(clean);
112-
byte[] before = capsule.getData();
114+
Block beforeInstance = capsule.getInstance();
115+
byte[] beforeData = capsule.getData();
113116

114-
capsule.sanitize();
117+
assertFalse("sanitize() should report no-op on a clean block", capsule.sanitize());
115118

116-
assertArrayEquals("Clean block should pass through unchanged", before, capsule.getData());
119+
assertSame("Underlying Block reference should not be rebuilt",
120+
beforeInstance, capsule.getInstance());
121+
assertArrayEquals("Clean block should pass through unchanged", beforeData, capsule.getData());
117122
}
118123

119124
// ---- TransactionCapsule.sanitize ----
@@ -124,7 +129,7 @@ public void transactionCapsuleSanitizeStripsTopLevelUnknownFields() {
124129
TransactionCapsule capsule = new TransactionCapsule(padded);
125130
long originalSize = capsule.getData().length;
126131

127-
capsule.sanitize();
132+
assertTrue("sanitize() should report it mutated the capsule", capsule.sanitize());
128133

129134
assertTrue("Transaction-level unknown fields should be stripped",
130135
capsule.getInstance().getUnknownFields().asMap().isEmpty());
@@ -149,11 +154,14 @@ public void transactionCapsuleSanitizePreservesTransactionId() {
149154
public void transactionCapsuleSanitizeIsNoOpOnCleanTransaction() {
150155
Transaction clean = sampleTransaction();
151156
TransactionCapsule capsule = new TransactionCapsule(clean);
152-
byte[] before = capsule.getData();
157+
Transaction beforeInstance = capsule.getInstance();
158+
byte[] beforeData = capsule.getData();
153159

154-
capsule.sanitize();
160+
assertFalse("sanitize() should report no-op on a clean transaction", capsule.sanitize());
155161

156-
assertArrayEquals(before, capsule.getData());
162+
assertSame("Underlying Transaction reference should not be rebuilt",
163+
beforeInstance, capsule.getInstance());
164+
assertArrayEquals(beforeData, capsule.getData());
157165
}
158166

159167
// ---- BlockMessage.sanitize ----
@@ -177,4 +185,16 @@ public void blockMessageSanitizeUpdatesBothCapsuleAndWireBytes() throws Exceptio
177185
assertNotEquals("msg.data should no longer match the padded wire bytes",
178186
paddedBytes.length, msg.getData().length);
179187
}
188+
189+
@Test
190+
public void blockMessageSanitizeSkipsDataRewriteOnCleanBlock() throws Exception {
191+
byte[] cleanBytes = sampleBlock().toByteArray();
192+
BlockMessage msg = new BlockMessage(cleanBytes);
193+
byte[] before = msg.getData();
194+
195+
msg.sanitize();
196+
197+
assertSame("msg.data should not be rewritten on the no-op path",
198+
before, msg.getData());
199+
}
180200
}

0 commit comments

Comments
 (0)