|
2 | 2 |
|
3 | 3 | import com.google.protobuf.Any; |
4 | 4 | import com.google.protobuf.ByteString; |
| 5 | +import com.google.protobuf.UnknownFieldSet; |
5 | 6 | import java.lang.reflect.Field; |
6 | 7 | import java.lang.reflect.Method; |
7 | 8 | import java.time.ZonedDateTime; |
8 | 9 | import java.util.ArrayList; |
| 10 | +import java.util.Collections; |
9 | 11 | import java.util.List; |
10 | 12 | import java.util.Map; |
11 | 13 | import java.util.concurrent.BlockingQueue; |
|
25 | 27 | import org.tron.common.TestConstants; |
26 | 28 | import org.tron.common.runtime.TvmTestUtils; |
27 | 29 | import org.tron.common.utils.ByteArray; |
| 30 | +import org.tron.common.utils.ReflectUtils; |
28 | 31 | import org.tron.core.ChainBaseManager; |
| 32 | +import org.tron.core.Constant; |
29 | 33 | import org.tron.core.config.args.Args; |
30 | 34 | import org.tron.core.exception.P2pException; |
31 | 35 | import org.tron.core.exception.P2pException.TypeEnum; |
@@ -199,6 +203,7 @@ public void testRejectedExecution() throws Exception { |
199 | 203 | // 2 transfer transactions, submit throws on the first → catch + break, only called once |
200 | 204 | handler.processMessage(peer, msg); |
201 | 205 |
|
| 206 | + Assert.assertTrue(peer.getAdvInvRequest().isEmpty()); |
202 | 207 | Mockito.verify(mockPool, Mockito.times(1)).submit(Mockito.any(Runnable.class)); |
203 | 208 | } finally { |
204 | 209 | closeHandlerAndOriginalPool(handler, originalPool); |
@@ -389,11 +394,42 @@ public void testDuplicateTransactionRejected() throws Exception { |
389 | 394 | } catch (P2pException e) { |
390 | 395 | Assert.assertEquals(P2pException.TypeEnum.BAD_MESSAGE, e.getType()); |
391 | 396 | } |
| 397 | + Assert.assertEquals(1, advInvRequest.size()); |
| 398 | + Assert.assertTrue(advInvRequest.containsKey(item)); |
392 | 399 | } finally { |
393 | 400 | handler.close(); |
394 | 401 | } |
395 | 402 | } |
396 | 403 |
|
| 404 | + @Test |
| 405 | + public void testInvalidBatchPreservesRequests() throws Exception { |
| 406 | + TransactionsMsgHandler handler = new TransactionsMsgHandler(); |
| 407 | + ExecutorService originalPool = null; |
| 408 | + try { |
| 409 | + ExecutorService pool = Mockito.mock(ExecutorService.class); |
| 410 | + originalPool = replaceTrxHandlePool(handler, pool); |
| 411 | + List<Protocol.Transaction> transactions = new ArrayList<>( |
| 412 | + buildTransferMessage(2).getTransactions().getTransactionsList()); |
| 413 | + // The first transaction passes; the second fails signature-length validation. |
| 414 | + transactions.set(1, transactions.get(1).toBuilder() |
| 415 | + .addSignature(ByteString.copyFrom(new byte[64])).build()); |
| 416 | + TransactionsMessage msg = new TransactionsMessage(transactions); |
| 417 | + PeerConnection peer = Mockito.mock(PeerConnection.class); |
| 418 | + stubAdvInvRequest(peer, msg); |
| 419 | + Map<Item, Long> requests = new ConcurrentHashMap<>(peer.getAdvInvRequest()); |
| 420 | + Assert.assertEquals(2, requests.size()); |
| 421 | + |
| 422 | + P2pException exception = Assert.assertThrows(P2pException.class, |
| 423 | + () -> handler.processMessage(peer, msg)); |
| 424 | + |
| 425 | + Assert.assertEquals(TypeEnum.BAD_TRX, exception.getType()); |
| 426 | + Assert.assertEquals(requests, peer.getAdvInvRequest()); |
| 427 | + Mockito.verify(pool, Mockito.never()).submit(Mockito.any(Runnable.class)); |
| 428 | + } finally { |
| 429 | + closeHandlerAndOriginalPool(handler, originalPool); |
| 430 | + } |
| 431 | + } |
| 432 | + |
397 | 433 | @Test |
398 | 434 | public void testInvalidSigLength() throws Exception { |
399 | 435 | TransactionsMsgHandler handler = new TransactionsMsgHandler(); |
@@ -492,6 +528,149 @@ public void testInvalidSigLength() throws Exception { |
492 | 528 | } |
493 | 529 | } |
494 | 530 |
|
| 531 | + @Test |
| 532 | + public void testSanitizeBeforeQueueAdmission() throws Exception { |
| 533 | + for (boolean smart : new boolean[] {false, true}) { |
| 534 | + Protocol.Transaction base = admissionTransaction(smart); |
| 535 | + Protocol.Transaction clean = base.toBuilder().setRawData(base.getRawData().toBuilder() |
| 536 | + .setUnknownFields(padding(32))).build(); |
| 537 | + Protocol.Transaction padded = clean.toBuilder().setUnknownFields(padding(600 * 1024)) |
| 538 | + .build(); |
| 539 | + assertAdmission(padded, clean); |
| 540 | + } |
| 541 | + } |
| 542 | + |
| 543 | + @Test |
| 544 | + public void testOversizedTransactionDroppedBeforeQueueAdmission() throws Exception { |
| 545 | + for (boolean smart : new boolean[] {false, true}) { |
| 546 | + Protocol.Transaction base = admissionTransaction(smart); |
| 547 | + Protocol.Transaction oversized = base.toBuilder().setRawData(base.getRawData().toBuilder() |
| 548 | + .setData(ByteString.copyFrom(new byte[600 * 1024]))).build(); |
| 549 | + assertAdmission(oversized, null); |
| 550 | + } |
| 551 | + } |
| 552 | + |
| 553 | + @Test |
| 554 | + public void testQueueAdmissionSizeBoundary() throws Exception { |
| 555 | + int limit = (int) Constant.TRANSACTION_MAX_BYTE_SIZE; |
| 556 | + for (boolean smart : new boolean[] {false, true}) { |
| 557 | + Protocol.Transaction.raw.Builder raw = admissionTransaction(smart).getRawData().toBuilder() |
| 558 | + .setData(ByteString.copyFrom(new byte[limit])); |
| 559 | + int overhead = Protocol.Transaction.newBuilder().setRawData(raw).build() |
| 560 | + .getSerializedSize() - limit; |
| 561 | + Protocol.Transaction exact = Protocol.Transaction.newBuilder().setRawData( |
| 562 | + raw.setData(ByteString.copyFrom(new byte[limit - overhead]))).build(); |
| 563 | + Assert.assertEquals(limit, exact.getSerializedSize()); |
| 564 | + assertAdmission(exact, exact); |
| 565 | + Protocol.Transaction over = exact.toBuilder().setRawData( |
| 566 | + raw.setData(ByteString.copyFrom(new byte[limit - overhead + 1]))).build(); |
| 567 | + Assert.assertEquals(limit + 1, over.getSerializedSize()); |
| 568 | + assertAdmission(over, null); |
| 569 | + } |
| 570 | + } |
| 571 | + |
| 572 | + @Test |
| 573 | + public void testResultsClearedBeforeQueueAdmission() throws Exception { |
| 574 | + for (boolean smart : new boolean[] {false, true}) { |
| 575 | + Protocol.Transaction expected = admissionTransaction(smart).toBuilder() |
| 576 | + .addSignature(ByteString.copyFrom(new byte[65])).build(); |
| 577 | + // Even a single result must be discarded, including its known and unknown fields. |
| 578 | + Protocol.Transaction.Result result = Protocol.Transaction.Result.newBuilder() |
| 579 | + .setFee(123456).setWithdrawAmount(77) |
| 580 | + .setContractRet(Protocol.Transaction.Result.contractResult.REVERT) |
| 581 | + .setUnknownFields(padding(32)).build(); |
| 582 | + assertAdmission(expected.toBuilder().addRet(result).build(), expected); |
| 583 | + assertAdmission(expected.toBuilder().addRet(result).addRet(result).build(), expected); |
| 584 | + } |
| 585 | + } |
| 586 | + |
| 587 | + @Test |
| 588 | + public void testOversizedResultsClearedBeforeQueueAdmission() throws Exception { |
| 589 | + for (boolean smart : new boolean[] {false, true}) { |
| 590 | + Protocol.Transaction expected = admissionTransaction(smart).toBuilder() |
| 591 | + .addSignature(ByteString.copyFrom(new byte[65])).build(); |
| 592 | + // Padding in the first result must be removed before the transaction size check. |
| 593 | + Protocol.Transaction padded = expected.toBuilder().addRet( |
| 594 | + Protocol.Transaction.Result.newBuilder().setUnknownFields(padding(600 * 1024))).build(); |
| 595 | + Assert.assertTrue(padded.getSerializedSize() > Constant.TRANSACTION_MAX_BYTE_SIZE); |
| 596 | + assertAdmission(padded, expected); |
| 597 | + } |
| 598 | + } |
| 599 | + |
| 600 | + private void assertAdmission(Protocol.Transaction input, Protocol.Transaction expected) |
| 601 | + throws Exception { |
| 602 | + TransactionsMsgHandler handler = new TransactionsMsgHandler(); |
| 603 | + ExecutorService originalPool = null; |
| 604 | + try { |
| 605 | + ExecutorService pool = Mockito.mock(ExecutorService.class); |
| 606 | + List<Runnable> tasks = new ArrayList<>(); |
| 607 | + Mockito.when(pool.submit(Mockito.any(Runnable.class))).thenAnswer(call -> { |
| 608 | + tasks.add(call.getArgument(0)); |
| 609 | + return Mockito.mock(Future.class); |
| 610 | + }); |
| 611 | + originalPool = replaceTrxHandlePool(handler, pool); |
| 612 | + TronNetDelegate delegate = Mockito.mock(TronNetDelegate.class); |
| 613 | + AdvService adv = Mockito.mock(AdvService.class); |
| 614 | + ReflectUtils.setFieldValue(handler, "tronNetDelegate", delegate); |
| 615 | + ReflectUtils.setFieldValue(handler, "advService", adv); |
| 616 | + ReflectUtils.setFieldValue(handler, "chainBaseManager", Mockito.mock(ChainBaseManager.class)); |
| 617 | + PeerConnection peer = Mockito.mock(PeerConnection.class); |
| 618 | + TransactionsMessage message = new TransactionsMessage(Collections.singletonList(input)); |
| 619 | + stubAdvInvRequest(peer, message); |
| 620 | + handler.processMessage(peer, message); |
| 621 | + BlockingQueue<?> smartQueue = (BlockingQueue<?>) |
| 622 | + ReflectUtils.getFieldObject(handler, "smartContractQueue"); |
| 623 | + if (expected == null) { |
| 624 | + Assert.assertTrue(smartQueue.isEmpty()); |
| 625 | + Mockito.verify(pool, Mockito.never()).submit(Mockito.any(Runnable.class)); |
| 626 | + } else { |
| 627 | + TransactionMessage admitted; |
| 628 | + if (input.getRawData().getContract(0).getType() |
| 629 | + == Protocol.Transaction.Contract.ContractType.TriggerSmartContract) { |
| 630 | + Assert.assertEquals(1, smartQueue.size()); |
| 631 | + admitted = (TransactionMessage) ReflectUtils.getFieldObject(smartQueue.peek(), "msg"); |
| 632 | + } else { |
| 633 | + Assert.assertEquals(1, tasks.size()); |
| 634 | + // Run only after admission completes, exercising the transaction retained by the task. |
| 635 | + tasks.get(0).run(); |
| 636 | + org.mockito.ArgumentCaptor<TransactionMessage> captured = |
| 637 | + org.mockito.ArgumentCaptor.forClass(TransactionMessage.class); |
| 638 | + Mockito.verify(adv).broadcast(captured.capture()); |
| 639 | + admitted = captured.getValue(); |
| 640 | + } |
| 641 | + Protocol.Transaction retained = admitted.getTransactionCapsule().getInstance(); |
| 642 | + Protocol.Transaction serialized = Protocol.Transaction.parseFrom(admitted.getData()); |
| 643 | + Assert.assertEquals(expected, retained); |
| 644 | + Assert.assertEquals(0, retained.getRetCount()); |
| 645 | + Assert.assertEquals(0, serialized.getRetCount()); |
| 646 | + Assert.assertArrayEquals(input.getRawData().toByteArray(), |
| 647 | + retained.getRawData().toByteArray()); |
| 648 | + Assert.assertEquals(input.getSignatureList(), retained.getSignatureList()); |
| 649 | + Assert.assertArrayEquals(expected.toByteArray(), admitted.getData()); |
| 650 | + Assert.assertEquals(new TransactionMessage(input).getMessageId(), admitted.getMessageId()); |
| 651 | + } |
| 652 | + Assert.assertTrue(peer.getAdvInvRequest().isEmpty()); |
| 653 | + Mockito.verify(peer, Mockito.never()).disconnect(Mockito.any()); |
| 654 | + } finally { |
| 655 | + closeHandlerAndOriginalPool(handler, originalPool); |
| 656 | + } |
| 657 | + } |
| 658 | + |
| 659 | + private Protocol.Transaction admissionTransaction(boolean smart) { |
| 660 | + if (smart) { |
| 661 | + return TvmTestUtils.generateTriggerSmartContractAndGetTransaction( |
| 662 | + ByteArray.fromHexString("121212a9cf"), ByteArray.fromHexString("121212a9cf"), |
| 663 | + ByteArray.fromHexString("123456"), 100, 100000000, 0, 0); |
| 664 | + } |
| 665 | + return buildTransferMessage(1).getTransactions().getTransactions(0); |
| 666 | + } |
| 667 | + |
| 668 | + private UnknownFieldSet padding(int size) { |
| 669 | + return UnknownFieldSet.newBuilder().addField(99999, |
| 670 | + UnknownFieldSet.Field.newBuilder().addLengthDelimited(ByteString.copyFrom(new byte[size])) |
| 671 | + .build()).build(); |
| 672 | + } |
| 673 | + |
495 | 674 | @Test |
496 | 675 | public void testIsBusyWithCachedTransactions() throws Exception { |
497 | 676 | TransactionsMsgHandler handler = new TransactionsMsgHandler(); |
|
0 commit comments