|
| 1 | +package org.tron.core.net.message.adv; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import com.google.protobuf.ByteString; |
| 8 | +import com.google.protobuf.UnknownFieldSet; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | +import org.mockito.Mockito; |
| 12 | +import org.tron.common.overlay.message.Message; |
| 13 | +import org.tron.core.store.DynamicPropertiesStore; |
| 14 | +import org.tron.protos.Protocol.Block; |
| 15 | +import org.tron.protos.Protocol.BlockHeader; |
| 16 | +import org.tron.protos.Protocol.Transaction; |
| 17 | +import org.tron.protos.Protocol.Transactions; |
| 18 | + |
| 19 | +/** |
| 20 | + * Verifies that the three P2P message constructors that take raw bytes |
| 21 | + * (BlockMessage, TransactionMessage, TransactionsMessage) strip unknown |
| 22 | + * protobuf fields at positions not covered by any consensus hash, while |
| 23 | + * leaving the signed regions byte-identical so transaction id and block hash |
| 24 | + * remain stable. |
| 25 | + */ |
| 26 | +public class SanitizeUnknownFieldsTest { |
| 27 | + |
| 28 | + /** |
| 29 | + * 1 KiB length-delimited unknown field at tag 99999, large enough that any |
| 30 | + * sanitize-stripped message is noticeably shorter than the padded input. |
| 31 | + */ |
| 32 | + private static final UnknownFieldSet PADDING = UnknownFieldSet.newBuilder() |
| 33 | + .addField(99999, UnknownFieldSet.Field.newBuilder() |
| 34 | + .addLengthDelimited(ByteString.copyFrom(new byte[1024])) |
| 35 | + .build()) |
| 36 | + .build(); |
| 37 | + |
| 38 | + @BeforeClass |
| 39 | + public static void setUp() { |
| 40 | + // Mock dynamicPropertiesStore so Message.isFilter() returns false |
| 41 | + // (mock's primitive-long getter returns 0L by default). |
| 42 | + Message.setDynamicPropertiesStore(Mockito.mock(DynamicPropertiesStore.class)); |
| 43 | + } |
| 44 | + |
| 45 | + private static BlockHeader.raw sampleRawHeader() { |
| 46 | + return BlockHeader.raw.newBuilder() |
| 47 | + .setNumber(100) |
| 48 | + .setTimestamp(123456789L) |
| 49 | + .build(); |
| 50 | + } |
| 51 | + |
| 52 | + private static Block sampleBlock() { |
| 53 | + return Block.newBuilder() |
| 54 | + .setBlockHeader(BlockHeader.newBuilder().setRawData(sampleRawHeader()).build()) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + |
| 58 | + private static Transaction sampleTransaction() { |
| 59 | + return Transaction.newBuilder() |
| 60 | + .setRawData(Transaction.raw.newBuilder().setTimestamp(123456789L).build()) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + // ---- BlockMessage ---- |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testBlockMessageStripsBlockLevelUnknownFields() throws Exception { |
| 68 | + Block padded = sampleBlock().toBuilder().setUnknownFields(PADDING).build(); |
| 69 | + byte[] paddedBytes = padded.toByteArray(); |
| 70 | + |
| 71 | + BlockMessage msg = new BlockMessage(paddedBytes); |
| 72 | + |
| 73 | + assertTrue("Block-level unknown fields should be stripped", |
| 74 | + msg.getBlockCapsule().getInstance().getUnknownFields().asMap().isEmpty()); |
| 75 | + assertTrue("msg.data should be canonical (no padding)", |
| 76 | + msg.getData().length < paddedBytes.length); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testBlockMessageStripsBlockHeaderOuterUnknownFields() throws Exception { |
| 81 | + BlockHeader paddedHeader = BlockHeader.newBuilder() |
| 82 | + .setRawData(sampleRawHeader()) |
| 83 | + .setUnknownFields(PADDING) |
| 84 | + .build(); |
| 85 | + Block padded = Block.newBuilder().setBlockHeader(paddedHeader).build(); |
| 86 | + byte[] paddedBytes = padded.toByteArray(); |
| 87 | + |
| 88 | + BlockMessage msg = new BlockMessage(paddedBytes); |
| 89 | + |
| 90 | + assertTrue("BlockHeader outer unknown fields should be stripped", |
| 91 | + msg.getBlockCapsule().getInstance().getBlockHeader() |
| 92 | + .getUnknownFields().asMap().isEmpty()); |
| 93 | + assertTrue(msg.getData().length < paddedBytes.length); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testBlockMessagePreservesBlockHeaderRawData() throws Exception { |
| 98 | + Block clean = sampleBlock(); |
| 99 | + Block padded = clean.toBuilder().setUnknownFields(PADDING).build(); |
| 100 | + |
| 101 | + BlockMessage msg = new BlockMessage(padded.toByteArray()); |
| 102 | + |
| 103 | + assertEquals("BlockHeader.raw_data must be byte-identical so block hash matches", |
| 104 | + clean.getBlockHeader().getRawData(), |
| 105 | + msg.getBlockCapsule().getInstance().getBlockHeader().getRawData()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testBlockMessageCleanBlockPassesThroughUnchanged() throws Exception { |
| 110 | + Block clean = sampleBlock(); |
| 111 | + byte[] cleanBytes = clean.toByteArray(); |
| 112 | + |
| 113 | + BlockMessage msg = new BlockMessage(cleanBytes); |
| 114 | + |
| 115 | + assertArrayEquals("Clean block bytes should pass through unchanged", |
| 116 | + cleanBytes, msg.getData()); |
| 117 | + } |
| 118 | + |
| 119 | + // ---- TransactionMessage ---- |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testTransactionMessageStripsTopLevelUnknownFields() throws Exception { |
| 123 | + Transaction padded = sampleTransaction().toBuilder().setUnknownFields(PADDING).build(); |
| 124 | + byte[] paddedBytes = padded.toByteArray(); |
| 125 | + |
| 126 | + TransactionMessage msg = new TransactionMessage(paddedBytes); |
| 127 | + |
| 128 | + assertTrue("Transaction-level unknown fields should be stripped", |
| 129 | + msg.getTransactionCapsule().getInstance().getUnknownFields().asMap().isEmpty()); |
| 130 | + assertTrue(msg.getData().length < paddedBytes.length); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testTransactionMessagePreservesTransactionId() throws Exception { |
| 135 | + Transaction clean = sampleTransaction(); |
| 136 | + Transaction padded = clean.toBuilder().setUnknownFields(PADDING).build(); |
| 137 | + |
| 138 | + TransactionMessage cleanMsg = new TransactionMessage(clean.toByteArray()); |
| 139 | + TransactionMessage paddedMsg = new TransactionMessage(padded.toByteArray()); |
| 140 | + |
| 141 | + assertEquals("Padding outside raw_data must not change tx id", |
| 142 | + cleanMsg.getTransactionCapsule().getTransactionId(), |
| 143 | + paddedMsg.getTransactionCapsule().getTransactionId()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testTransactionMessageCleanTransactionPassesThroughUnchanged() throws Exception { |
| 148 | + Transaction clean = sampleTransaction(); |
| 149 | + byte[] cleanBytes = clean.toByteArray(); |
| 150 | + |
| 151 | + TransactionMessage msg = new TransactionMessage(cleanBytes); |
| 152 | + |
| 153 | + assertArrayEquals(cleanBytes, msg.getData()); |
| 154 | + } |
| 155 | + |
| 156 | + // ---- TransactionsMessage ---- |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testTransactionsMessageStripsWrapperUnknownFields() throws Exception { |
| 160 | + Transactions padded = Transactions.newBuilder() |
| 161 | + .addTransactions(sampleTransaction()) |
| 162 | + .setUnknownFields(PADDING) |
| 163 | + .build(); |
| 164 | + byte[] paddedBytes = padded.toByteArray(); |
| 165 | + |
| 166 | + TransactionsMessage msg = new TransactionsMessage(paddedBytes); |
| 167 | + |
| 168 | + assertTrue("Wrapper unknown fields should be stripped", |
| 169 | + msg.getTransactions().getUnknownFields().asMap().isEmpty()); |
| 170 | + assertTrue(msg.getData().length < paddedBytes.length); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testTransactionsMessageStripsNestedTxUnknownFields() throws Exception { |
| 175 | + Transaction paddedTx = sampleTransaction().toBuilder().setUnknownFields(PADDING).build(); |
| 176 | + Transactions wrapper = Transactions.newBuilder().addTransactions(paddedTx).build(); |
| 177 | + byte[] paddedBytes = wrapper.toByteArray(); |
| 178 | + |
| 179 | + TransactionsMessage msg = new TransactionsMessage(paddedBytes); |
| 180 | + |
| 181 | + assertTrue("Nested tx unknown fields should be stripped", |
| 182 | + msg.getTransactions().getTransactions(0) |
| 183 | + .getUnknownFields().asMap().isEmpty()); |
| 184 | + assertTrue(msg.getData().length < paddedBytes.length); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testTransactionsMessageCleanWrapperPassesThroughUnchanged() throws Exception { |
| 189 | + Transactions clean = Transactions.newBuilder() |
| 190 | + .addTransactions(sampleTransaction()) |
| 191 | + .build(); |
| 192 | + byte[] cleanBytes = clean.toByteArray(); |
| 193 | + |
| 194 | + TransactionsMessage msg = new TransactionsMessage(cleanBytes); |
| 195 | + |
| 196 | + assertArrayEquals(cleanBytes, msg.getData()); |
| 197 | + } |
| 198 | +} |
0 commit comments