|
4 | 4 | "context" |
5 | 5 | "math/big" |
6 | 6 | "testing" |
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/holiman/uint256" |
9 | 10 | "github.com/stretchr/testify/require" |
@@ -530,3 +531,204 @@ func TestSnapshot(t *testing.T) { |
530 | 531 | }) |
531 | 532 | } |
532 | 533 | } |
| 534 | + |
| 535 | +func TestCustomBlockTimeValidation(t *testing.T) { |
| 536 | + t.Parallel() |
| 537 | + |
| 538 | + addr1 := common.HexToAddress("0x1") |
| 539 | + |
| 540 | + testCases := []struct { |
| 541 | + name string |
| 542 | + blockTime time.Duration |
| 543 | + consensusPeriod uint64 |
| 544 | + blockNumber uint64 |
| 545 | + expectError bool |
| 546 | + description string |
| 547 | + }{ |
| 548 | + { |
| 549 | + name: "blockTime is zero (default) - should succeed", |
| 550 | + blockTime: 0, |
| 551 | + consensusPeriod: 2, |
| 552 | + blockNumber: 1, |
| 553 | + expectError: false, |
| 554 | + description: "Default blockTime of 0 should use standard consensus delay", |
| 555 | + }, |
| 556 | + { |
| 557 | + name: "blockTime equals consensus period - should succeed", |
| 558 | + blockTime: 2 * time.Second, |
| 559 | + consensusPeriod: 2, |
| 560 | + blockNumber: 1, |
| 561 | + expectError: false, |
| 562 | + description: "Custom blockTime equal to consensus period should be valid", |
| 563 | + }, |
| 564 | + { |
| 565 | + name: "blockTime greater than consensus period - should succeed", |
| 566 | + blockTime: 5 * time.Second, |
| 567 | + consensusPeriod: 2, |
| 568 | + blockNumber: 1, |
| 569 | + expectError: false, |
| 570 | + description: "Custom blockTime greater than consensus period should be valid", |
| 571 | + }, |
| 572 | + { |
| 573 | + name: "blockTime less than consensus period - should fail", |
| 574 | + blockTime: 1 * time.Second, |
| 575 | + consensusPeriod: 2, |
| 576 | + blockNumber: 1, |
| 577 | + expectError: true, |
| 578 | + description: "Custom blockTime less than consensus period should be invalid", |
| 579 | + }, |
| 580 | + } |
| 581 | + |
| 582 | + for _, tc := range testCases { |
| 583 | + t.Run(tc.name, func(t *testing.T) { |
| 584 | + sp := &fakeSpanner{vals: []*valset.Validator{{Address: addr1, VotingPower: 1}}} |
| 585 | + borCfg := ¶ms.BorConfig{ |
| 586 | + Sprint: map[string]uint64{"0": 64}, |
| 587 | + Period: map[string]uint64{"0": tc.consensusPeriod}, |
| 588 | + RioBlock: big.NewInt(0), // Enable Rio from genesis |
| 589 | + } |
| 590 | + chain, b := newChainAndBorForTest(t, sp, borCfg, true, addr1) |
| 591 | + b.blockTime = tc.blockTime |
| 592 | + |
| 593 | + // Get genesis block as parent |
| 594 | + genesis := chain.HeaderChain().GetHeaderByNumber(0) |
| 595 | + require.NotNil(t, genesis) |
| 596 | + |
| 597 | + header := &types.Header{ |
| 598 | + Number: big.NewInt(int64(tc.blockNumber)), |
| 599 | + ParentHash: genesis.Hash(), |
| 600 | + } |
| 601 | + |
| 602 | + err := b.Prepare(chain.HeaderChain(), header) |
| 603 | + |
| 604 | + if tc.expectError { |
| 605 | + require.Error(t, err, tc.description) |
| 606 | + require.Contains(t, err.Error(), "less than the consensus block time", tc.description) |
| 607 | + } else { |
| 608 | + require.NoError(t, err, tc.description) |
| 609 | + } |
| 610 | + }) |
| 611 | + } |
| 612 | +} |
| 613 | + |
| 614 | +func TestCustomBlockTimeCalculation(t *testing.T) { |
| 615 | + t.Parallel() |
| 616 | + |
| 617 | + addr1 := common.HexToAddress("0x1") |
| 618 | + |
| 619 | + t.Run("sequential blocks with custom blockTime", func(t *testing.T) { |
| 620 | + sp := &fakeSpanner{vals: []*valset.Validator{{Address: addr1, VotingPower: 1}}} |
| 621 | + borCfg := ¶ms.BorConfig{ |
| 622 | + Sprint: map[string]uint64{"0": 64}, |
| 623 | + Period: map[string]uint64{"0": 2}, |
| 624 | + RioBlock: big.NewInt(0), |
| 625 | + } |
| 626 | + chain, b := newChainAndBorForTest(t, sp, borCfg, true, addr1) |
| 627 | + b.blockTime = 5 * time.Second |
| 628 | + |
| 629 | + genesis := chain.HeaderChain().GetHeaderByNumber(0) |
| 630 | + require.NotNil(t, genesis) |
| 631 | + baseTime := genesis.Time |
| 632 | + |
| 633 | + header1 := &types.Header{ |
| 634 | + Number: big.NewInt(1), |
| 635 | + ParentHash: genesis.Hash(), |
| 636 | + } |
| 637 | + err := b.Prepare(chain.HeaderChain(), header1) |
| 638 | + require.NoError(t, err) |
| 639 | + |
| 640 | + require.False(t, header1.ActualTime.IsZero(), "ActualTime should be set") |
| 641 | + expectedTime := time.Unix(int64(baseTime), 0).Add(5 * time.Second) |
| 642 | + require.Equal(t, expectedTime.Unix(), header1.ActualTime.Unix()) |
| 643 | + }) |
| 644 | + |
| 645 | + t.Run("lastMinedBlockTime is zero (first block)", func(t *testing.T) { |
| 646 | + sp := &fakeSpanner{vals: []*valset.Validator{{Address: addr1, VotingPower: 1}}} |
| 647 | + borCfg := ¶ms.BorConfig{ |
| 648 | + Sprint: map[string]uint64{"0": 64}, |
| 649 | + Period: map[string]uint64{"0": 2}, |
| 650 | + RioBlock: big.NewInt(0), |
| 651 | + } |
| 652 | + chain, b := newChainAndBorForTest(t, sp, borCfg, true, addr1) |
| 653 | + b.blockTime = 3 * time.Second |
| 654 | + |
| 655 | + genesis := chain.HeaderChain().GetHeaderByNumber(0) |
| 656 | + require.NotNil(t, genesis) |
| 657 | + baseTime := genesis.Time |
| 658 | + |
| 659 | + header := &types.Header{ |
| 660 | + Number: big.NewInt(1), |
| 661 | + ParentHash: genesis.Hash(), |
| 662 | + } |
| 663 | + |
| 664 | + err := b.Prepare(chain.HeaderChain(), header) |
| 665 | + require.NoError(t, err) |
| 666 | + |
| 667 | + expectedTime := time.Unix(int64(baseTime), 0).Add(3 * time.Second) |
| 668 | + require.Equal(t, expectedTime.Unix(), header.ActualTime.Unix()) |
| 669 | + }) |
| 670 | + |
| 671 | + t.Run("lastMinedBlockTime before parent time (fallback)", func(t *testing.T) { |
| 672 | + sp := &fakeSpanner{vals: []*valset.Validator{{Address: addr1, VotingPower: 1}}} |
| 673 | + borCfg := ¶ms.BorConfig{ |
| 674 | + Sprint: map[string]uint64{"0": 64}, |
| 675 | + Period: map[string]uint64{"0": 2}, |
| 676 | + RioBlock: big.NewInt(0), |
| 677 | + } |
| 678 | + chain, b := newChainAndBorForTest(t, sp, borCfg, true, addr1) |
| 679 | + b.blockTime = 4 * time.Second |
| 680 | + |
| 681 | + genesis := chain.HeaderChain().GetHeaderByNumber(0) |
| 682 | + require.NotNil(t, genesis) |
| 683 | + baseTime := genesis.Time |
| 684 | + |
| 685 | + if baseTime > 10 { |
| 686 | + b.lastMinedBlockTime = time.Unix(int64(baseTime-10), 0) |
| 687 | + } else { |
| 688 | + b.lastMinedBlockTime = time.Unix(0, 0) |
| 689 | + } |
| 690 | + |
| 691 | + header := &types.Header{ |
| 692 | + Number: big.NewInt(1), |
| 693 | + ParentHash: genesis.Hash(), |
| 694 | + } |
| 695 | + |
| 696 | + err := b.Prepare(chain.HeaderChain(), header) |
| 697 | + require.NoError(t, err) |
| 698 | + |
| 699 | + expectedTime := time.Unix(int64(baseTime), 0).Add(4 * time.Second) |
| 700 | + require.Equal(t, expectedTime.Unix(), header.ActualTime.Unix()) |
| 701 | + }) |
| 702 | +} |
| 703 | + |
| 704 | +func TestCustomBlockTimeBackwardCompatibility(t *testing.T) { |
| 705 | + t.Parallel() |
| 706 | + |
| 707 | + addr1 := common.HexToAddress("0x1") |
| 708 | + |
| 709 | + t.Run("blockTime is zero uses standard CalcProducerDelay", func(t *testing.T) { |
| 710 | + sp := &fakeSpanner{vals: []*valset.Validator{{Address: addr1, VotingPower: 1}}} |
| 711 | + borCfg := ¶ms.BorConfig{ |
| 712 | + Sprint: map[string]uint64{"0": 64}, |
| 713 | + Period: map[string]uint64{"0": 2}, |
| 714 | + ProducerDelay: map[string]uint64{"0": 3}, |
| 715 | + BackupMultiplier: map[string]uint64{"0": 2}, |
| 716 | + RioBlock: big.NewInt(0), |
| 717 | + } |
| 718 | + chain, b := newChainAndBorForTest(t, sp, borCfg, true, addr1) |
| 719 | + b.blockTime = 0 |
| 720 | + |
| 721 | + genesis := chain.HeaderChain().GetHeaderByNumber(0) |
| 722 | + require.NotNil(t, genesis) |
| 723 | + |
| 724 | + header := &types.Header{ |
| 725 | + Number: big.NewInt(1), |
| 726 | + ParentHash: genesis.Hash(), |
| 727 | + } |
| 728 | + |
| 729 | + err := b.Prepare(chain.HeaderChain(), header) |
| 730 | + require.NoError(t, err) |
| 731 | + |
| 732 | + require.True(t, header.ActualTime.IsZero(), "ActualTime should not be set when blockTime is 0") |
| 733 | + }) |
| 734 | +} |
0 commit comments