|
4 | 4 |
|
5 | 5 | import com.google.protobuf.ByteString; |
6 | 6 | import java.lang.reflect.Field; |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.util.Collections; |
7 | 9 | import org.junit.Assert; |
8 | 10 | import org.junit.Test; |
9 | 11 | import org.mockito.Mockito; |
|
17 | 19 | import org.tron.core.capsule.TransactionCapsule; |
18 | 20 | import org.tron.core.config.args.Args; |
19 | 21 | import org.tron.core.db.Manager; |
| 22 | +import org.tron.core.exception.BadBlockException; |
20 | 23 | import org.tron.core.exception.P2pException; |
21 | 24 | import org.tron.core.exception.P2pException.TypeEnum; |
| 25 | +import org.tron.core.exception.ValidateScheduleException; |
| 26 | +import org.tron.core.metrics.MetricsService; |
| 27 | +import org.tron.core.net.peer.PeerConnection; |
22 | 28 | import org.tron.core.store.DynamicPropertiesStore; |
| 29 | +import org.tron.core.store.WitnessScheduleStore; |
23 | 30 | import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
24 | 31 | import org.tron.protos.contract.BalanceContract.TransferContract; |
25 | 32 |
|
@@ -185,5 +192,127 @@ public void testValidBlockMerkleRoot() throws Exception { |
185 | 192 | } catch (P2pException e) { |
186 | 193 | Assert.assertEquals(TypeEnum.BLOCK_MERKLE_INVALID, e.getType()); |
187 | 194 | } |
| 195 | + PeerConnection peer = new PeerConnection(); |
| 196 | + WitnessScheduleStore schedule = mock(WitnessScheduleStore.class); |
| 197 | + setField(tronNetDelegate, "witnessScheduleStore", schedule); |
| 198 | + Mockito.when(schedule.getActiveWitnesses()) |
| 199 | + .thenReturn(Collections.singletonList(tampered.getWitnessAddress())); |
| 200 | + try { |
| 201 | + tronNetDelegate.validAdvBlock(peer, tampered); |
| 202 | + Assert.fail("Invalid Merkle root must still be rejected for an active witness"); |
| 203 | + } catch (P2pException e) { |
| 204 | + Assert.assertEquals(TypeEnum.BLOCK_MERKLE_INVALID, e.getType()); |
| 205 | + } |
| 206 | + Assert.assertEquals(0, peer.getLastInactiveWitnessBlockTime()); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void testValidBlockDoesNotCheckWitnessActivity() throws Exception { |
| 211 | + TronNetDelegate delegate = Mockito.spy(new TronNetDelegate()); |
| 212 | + WitnessScheduleStore schedule = mock(WitnessScheduleStore.class); |
| 213 | + setField(delegate, "witnessScheduleStore", schedule); |
| 214 | + BlockCapsule block = new BlockCapsule(1, Sha256Hash.ZERO_HASH, 0, |
| 215 | + ByteString.copyFromUtf8("witness")); |
| 216 | + block.setMerkleRoot(); |
| 217 | + Mockito.doNothing().when(delegate).validSignature(block); |
| 218 | + delegate.validBlock(block); |
| 219 | + Mockito.verify(schedule, Mockito.never()).getActiveWitnesses(); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void testValidAdvBlockChecksSignatureAfterWitnessActivity() throws Exception { |
| 224 | + TronNetDelegate delegate = Mockito.spy(new TronNetDelegate()); |
| 225 | + WitnessScheduleStore schedule = mock(WitnessScheduleStore.class); |
| 226 | + setField(delegate, "witnessScheduleStore", schedule); |
| 227 | + Mockito.when(schedule.getActiveWitnesses()).thenReturn(Collections.emptyList()); |
| 228 | + Mockito.doReturn(21_600_000L).when(delegate).getMaintenanceTimeInterval(); |
| 229 | + BlockCapsule block = new BlockCapsule(1, Sha256Hash.ZERO_HASH, 0, ByteString.EMPTY); |
| 230 | + block.setMerkleRoot(); |
| 231 | + P2pException failure = new P2pException(TypeEnum.BLOCK_SIGN_INVALID, "invalid signature"); |
| 232 | + Mockito.doThrow(failure).when(delegate).validSignature(block); |
| 233 | + PeerConnection peer = Mockito.spy(new PeerConnection()); |
| 234 | + Mockito.doReturn(new InetSocketAddress("127.0.0.1", 18888)) |
| 235 | + .when(peer).getInetSocketAddress(); |
| 236 | + try { |
| 237 | + delegate.validAdvBlock(peer, block); |
| 238 | + Assert.fail("Invalid signature must still be rejected after witness activity handling"); |
| 239 | + } catch (P2pException e) { |
| 240 | + Assert.assertSame(failure, e); |
| 241 | + } |
| 242 | + Assert.assertTrue(peer.getLastInactiveWitnessBlockTime() > 0); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + public void testRepeatedInactiveWitnessIsRejectedBeforeBasicValidation() throws Exception { |
| 247 | + TronNetDelegate delegate = Mockito.spy(new TronNetDelegate()); |
| 248 | + WitnessScheduleStore schedule = mock(WitnessScheduleStore.class); |
| 249 | + setField(delegate, "witnessScheduleStore", schedule); |
| 250 | + Mockito.when(schedule.getActiveWitnesses()).thenReturn(Collections.emptyList()); |
| 251 | + Mockito.doReturn(21_600_000L).when(delegate).getMaintenanceTimeInterval(); |
| 252 | + BlockCapsule block = new BlockCapsule(1, Sha256Hash.ZERO_HASH, 0, ByteString.EMPTY); |
| 253 | + PeerConnection peer = Mockito.spy(new PeerConnection()); |
| 254 | + Mockito.doReturn(new InetSocketAddress("127.0.0.1", 18888)) |
| 255 | + .when(peer).getInetSocketAddress(); |
| 256 | + peer.setLastInactiveWitnessBlockTime(System.currentTimeMillis()); |
| 257 | + |
| 258 | + try { |
| 259 | + delegate.validAdvBlock(peer, block); |
| 260 | + Assert.fail("Repeated inactive witness must be rejected before basic validation"); |
| 261 | + } catch (P2pException e) { |
| 262 | + Assert.assertEquals(TypeEnum.BAD_BLOCK, e.getType()); |
| 263 | + } |
| 264 | + Mockito.verify(delegate, Mockito.never()).validBlock(block); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + public void testValidAdvBlockAcceptsActiveWitnessWithoutChangingHistory() throws Exception { |
| 269 | + TronNetDelegate delegate = Mockito.spy(new TronNetDelegate()); |
| 270 | + WitnessScheduleStore schedule = mock(WitnessScheduleStore.class); |
| 271 | + setField(delegate, "witnessScheduleStore", schedule); |
| 272 | + BlockCapsule block = new BlockCapsule(1, Sha256Hash.ZERO_HASH, 0, |
| 273 | + ByteString.copyFromUtf8("witness")); |
| 274 | + block.setMerkleRoot(); |
| 275 | + Mockito.doNothing().when(delegate).validSignature(block); |
| 276 | + Mockito.when(schedule.getActiveWitnesses()) |
| 277 | + .thenReturn(Collections.singletonList(block.getWitnessAddress())); |
| 278 | + PeerConnection peer = new PeerConnection(); |
| 279 | + peer.setLastInactiveWitnessBlockTime(86_400_000L); |
| 280 | + |
| 281 | + Assert.assertTrue(delegate.validAdvBlock(peer, block)); |
| 282 | + |
| 283 | + Mockito.verify(delegate).validSignature(block); |
| 284 | + Assert.assertEquals(86_400_000L, peer.getLastInactiveWitnessBlockTime()); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + public void testProcessBlockWrapsScheduleFailureWithCause() throws Exception { |
| 289 | + assertProcessBlockFailure(new ValidateScheduleException("invalid scheduled witness")); |
| 290 | + } |
| 291 | + |
| 292 | + @Test |
| 293 | + public void testProcessBlockWrapsMerkleFailureWithCause() throws Exception { |
| 294 | + assertProcessBlockFailure(new BadBlockException( |
| 295 | + BadBlockException.TypeEnum.CALC_MERKLE_ROOT_FAILED, "invalid merkle root")); |
| 296 | + } |
| 297 | + |
| 298 | + private void assertProcessBlockFailure(Exception failure) throws Exception { |
| 299 | + TronNetDelegate delegate = new TronNetDelegate(); |
| 300 | + Manager manager = mock(Manager.class); |
| 301 | + ChainBaseManager chainManager = mock(ChainBaseManager.class); |
| 302 | + setField(delegate, "dbManager", manager); |
| 303 | + setField(delegate, "chainBaseManager", chainManager); |
| 304 | + setField(delegate, "metricsService", mock(MetricsService.class)); |
| 305 | + Mockito.when(manager.getBlockedTimer()).thenReturn(new ThreadLocal<>()); |
| 306 | + Mockito.when(chainManager.getHeadBlockId()) |
| 307 | + .thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 0)); |
| 308 | + BlockCapsule block = new BlockCapsule(1, Sha256Hash.ZERO_HASH, 0, ByteString.EMPTY); |
| 309 | + Mockito.doThrow(failure).when(manager).pushBlock(block); |
| 310 | + try { |
| 311 | + delegate.processBlock(block, true); |
| 312 | + Assert.fail("Expected block validation failure"); |
| 313 | + } catch (P2pException e) { |
| 314 | + Assert.assertEquals(TypeEnum.BAD_BLOCK, e.getType()); |
| 315 | + Assert.assertSame(failure, e.getCause()); |
| 316 | + } |
188 | 317 | } |
189 | 318 | } |
0 commit comments