|
5 | 5 | import java.lang.reflect.Field; |
6 | 6 | import java.lang.reflect.Method; |
7 | 7 | import java.util.ArrayList; |
8 | | -import java.util.Arrays; |
9 | 8 | import java.util.Collections; |
10 | | -import java.util.List; |
11 | 9 | import java.util.Map; |
12 | | -import org.bouncycastle.util.encoders.Hex; |
13 | 10 | import org.junit.Assert; |
14 | 11 | import org.junit.Test; |
15 | 12 | import org.mockito.Mockito; |
16 | | -import org.tron.common.crypto.SignInterface; |
17 | | -import org.tron.common.crypto.SignUtils; |
18 | | -import org.tron.common.utils.PublicMethod; |
19 | 13 | import org.tron.common.utils.Sha256Hash; |
20 | 14 | import org.tron.consensus.base.Param; |
21 | 15 | import org.tron.core.ChainBaseManager; |
@@ -95,88 +89,4 @@ public void testValidPbftSignPaddedSigOsakaRejected() throws Exception { |
95 | 89 | Collections.singletonList(ByteString.copyFrom(new byte[21]))); |
96 | 90 | Assert.assertFalse(valid); |
97 | 91 | } |
98 | | - |
99 | | - @Test |
100 | | - public void testValidPbftSignDedupesByAddress() throws Exception { |
101 | | - PbftDataSyncHandler pbftDataSyncHandler = new PbftDataSyncHandler(); |
102 | | - DynamicPropertiesStore dynamicPropertiesStore = Mockito.mock(DynamicPropertiesStore.class); |
103 | | - ChainBaseManager chainBaseManager = Mockito.mock(ChainBaseManager.class); |
104 | | - Mockito.when(chainBaseManager.getDynamicPropertiesStore()).thenReturn(dynamicPropertiesStore); |
105 | | - Mockito.when(dynamicPropertiesStore.signatureMaxSizeChecked()).thenReturn(true); |
106 | | - |
107 | | - Field field = PbftDataSyncHandler.class.getDeclaredField("chainBaseManager"); |
108 | | - field.setAccessible(true); |
109 | | - field.set(pbftDataSyncHandler, chainBaseManager); |
110 | | - |
111 | | - Param.getInstance().setAgreeNodeCount(2); |
112 | | - Method method = PbftDataSyncHandler.class.getDeclaredMethod("validPbftSign", |
113 | | - Protocol.PBFTMessage.Raw.class, java.util.List.class, java.util.List.class); |
114 | | - method.setAccessible(true); |
115 | | - |
116 | | - Protocol.PBFTMessage.Raw raw = Protocol.PBFTMessage.Raw.newBuilder() |
117 | | - .setViewN(1) |
118 | | - .setEpoch(0) |
119 | | - .setDataType(Protocol.PBFTMessage.DataType.BLOCK) |
120 | | - .setMsgType(Protocol.PBFTMessage.MsgType.COMMIT) |
121 | | - .setData(ByteString.copyFromUtf8("block")) |
122 | | - .build(); |
123 | | - |
124 | | - SignInterface signer = SignUtils.fromPrivate( |
125 | | - Hex.decode(PublicMethod.getRandomPrivateKey()), true); |
126 | | - byte[] sig65 = signer.Base64toBytes( |
127 | | - signer.signHash(Sha256Hash.hash(true, raw.toByteArray()))); |
128 | | - List<ByteString> paddedSigs = Arrays.asList( |
129 | | - ByteString.copyFrom(sig65), |
130 | | - ByteString.copyFrom(Arrays.copyOf(sig65, 66)), |
131 | | - ByteString.copyFrom(Arrays.copyOf(sig65, 67)), |
132 | | - ByteString.copyFrom(Arrays.copyOf(sig65, 68))); |
133 | | - List<ByteString> currentSrList = Collections.singletonList( |
134 | | - ByteString.copyFrom(signer.getAddress())); |
135 | | - |
136 | | - boolean valid = (boolean) method.invoke(pbftDataSyncHandler, raw, |
137 | | - paddedSigs, currentSrList); |
138 | | - Assert.assertFalse("trailing-byte padded duplicates must not inflate quorum", valid); |
139 | | - } |
140 | | - |
141 | | - @Test |
142 | | - public void testValidPbftSignAcceptsQuorumDespiteOneBadSig() throws Exception { |
143 | | - PbftDataSyncHandler pbftDataSyncHandler = new PbftDataSyncHandler(); |
144 | | - DynamicPropertiesStore dynamicPropertiesStore = Mockito.mock(DynamicPropertiesStore.class); |
145 | | - ChainBaseManager chainBaseManager = Mockito.mock(ChainBaseManager.class); |
146 | | - Mockito.when(chainBaseManager.getDynamicPropertiesStore()).thenReturn(dynamicPropertiesStore); |
147 | | - Mockito.when(dynamicPropertiesStore.signatureMaxSizeChecked()).thenReturn(true); |
148 | | - |
149 | | - Field field = PbftDataSyncHandler.class.getDeclaredField("chainBaseManager"); |
150 | | - field.setAccessible(true); |
151 | | - field.set(pbftDataSyncHandler, chainBaseManager); |
152 | | - |
153 | | - Param.getInstance().setAgreeNodeCount(3); |
154 | | - Method method = PbftDataSyncHandler.class.getDeclaredMethod("validPbftSign", |
155 | | - Protocol.PBFTMessage.Raw.class, java.util.List.class, java.util.List.class); |
156 | | - method.setAccessible(true); |
157 | | - |
158 | | - Protocol.PBFTMessage.Raw raw = Protocol.PBFTMessage.Raw.newBuilder() |
159 | | - .setViewN(1) |
160 | | - .setEpoch(0) |
161 | | - .setDataType(Protocol.PBFTMessage.DataType.BLOCK) |
162 | | - .setMsgType(Protocol.PBFTMessage.MsgType.COMMIT) |
163 | | - .setData(ByteString.copyFromUtf8("block")) |
164 | | - .build(); |
165 | | - byte[] dataHash = Sha256Hash.hash(true, raw.toByteArray()); |
166 | | - |
167 | | - List<ByteString> srSignList = new ArrayList<>(); |
168 | | - List<ByteString> currentSrList = new ArrayList<>(); |
169 | | - for (int i = 0; i < 3; i++) { |
170 | | - SignInterface signer = SignUtils.fromPrivate( |
171 | | - Hex.decode(PublicMethod.getRandomPrivateKey()), true); |
172 | | - srSignList.add(ByteString.copyFrom(signer.Base64toBytes(signer.signHash(dataHash)))); |
173 | | - currentSrList.add(ByteString.copyFrom(signer.getAddress())); |
174 | | - } |
175 | | - // An attacker-injected too-short signature must not bring down the whole message. |
176 | | - srSignList.add(ByteString.copyFrom(new byte[64])); |
177 | | - |
178 | | - boolean valid = (boolean) method.invoke(pbftDataSyncHandler, raw, |
179 | | - srSignList, currentSrList); |
180 | | - Assert.assertTrue("quorum of valid sigs must still pass when one sig is malformed", valid); |
181 | | - } |
182 | 92 | } |
0 commit comments