@@ -1468,3 +1468,113 @@ func TestEuclidV2TransitionVerification(t *testing.T) {
14681468	_ , err  =  chain .InsertChain (blocks )
14691469	assert .NoError (t , err )
14701470}
1471+ 
1472+ // TestBlockIntervalWithWorkerDeadline tests the block interval calculation 
1473+ // that simulates the actual worker deadline calculation logic 
1474+ func  TestBlockIntervalWithWorkerDeadline (t  * testing.T ) {
1475+ 	tests  :=  []struct  {
1476+ 		name              string 
1477+ 		period            uint64 
1478+ 		blocksPerSecond   uint64 
1479+ 		expectedInterval  time.Duration 
1480+ 		blocks            int  // number of blocks to simulate 
1481+ 	}{
1482+ 		{
1483+ 			name :             "1 second period, 1 block per second" ,
1484+ 			period :           1 ,
1485+ 			blocksPerSecond :  1 ,
1486+ 			expectedInterval : 1000  *  time .Millisecond ,
1487+ 			blocks :           5 ,
1488+ 		},
1489+ 		{
1490+ 			name :             "1 second period, 2 blocks per second" ,
1491+ 			period :           1 ,
1492+ 			blocksPerSecond :  2 ,
1493+ 			expectedInterval : 500  *  time .Millisecond ,
1494+ 			blocks :           6 ,
1495+ 		},
1496+ 		{
1497+ 			name :             "1 second period, 4 blocks per second" ,
1498+ 			period :           1 ,
1499+ 			blocksPerSecond :  4 ,
1500+ 			expectedInterval : 250  *  time .Millisecond ,
1501+ 			blocks :           8 ,
1502+ 		},
1503+ 		{
1504+ 			name :             "2 second period, 2 blocks per second" ,
1505+ 			period :           2 ,
1506+ 			blocksPerSecond :  2 ,
1507+ 			expectedInterval : 500  *  time .Millisecond ,
1508+ 			blocks :           2 ,
1509+ 		},
1510+ 	}
1511+ 
1512+ 	for  _ , tt  :=  range  tests  {
1513+ 		t .Run (tt .name , func (t  * testing.T ) {
1514+ 			config  :=  & params.SystemContractConfig {
1515+ 				Period :          tt .period ,
1516+ 				BlocksPerSecond : tt .blocksPerSecond ,
1517+ 				RelaxedPeriod :   false ,
1518+ 			}
1519+ 
1520+ 			// Start with a future timestamp to avoid time.Now() interference 
1521+ 			currentTime  :=  uint64 (time .Now ().Unix ()) +  3600  // 1 hour in the future 
1522+ 			var  deadlines  []time.Time 
1523+ 			var  timestamps  []uint64 
1524+ 
1525+ 			for  i  :=  0 ; i  <  tt .blocks ; i ++  {
1526+ 				// Create header for current block 
1527+ 				header  :=  & types.Header {
1528+ 					Time :   currentTime ,
1529+ 					Number : new (big.Int ).SetUint64 (uint64 (i )),
1530+ 				}
1531+ 
1532+ 				// Simulate the worker deadline calculation logic from newWork 
1533+ 				deadline  :=  CalculateBlockDeadline (config , header )
1534+ 				deadlines  =  append (deadlines , deadline )
1535+ 
1536+ 				// Simulate timestamp calculation manually for next iteration 
1537+ 				// This mimics the CalcTimestamp logic but simplified for testing 
1538+ 				blocksPerSecond  :=  system_contract .CalcBlocksPerSecond (tt .blocksPerSecond )
1539+ 				blocksPerPeriod  :=  blocksPerSecond  *  tt .period 
1540+ 				nextBlockNumber  :=  uint64 (i  +  1 )
1541+ 
1542+ 				var  newTimestamp  uint64 
1543+ 				if  nextBlockNumber % blocksPerPeriod  ==  0  &&  nextBlockNumber  >  0  {
1544+ 					// Period boundary - increment timestamp 
1545+ 					newTimestamp  =  currentTime  +  tt .period 
1546+ 				} else  {
1547+ 					// Within period - keep same timestamp 
1548+ 					newTimestamp  =  currentTime 
1549+ 				}
1550+ 
1551+ 				timestamps  =  append (timestamps , newTimestamp )
1552+ 
1553+ 				// Update currentTime for next iteration (simulate blockchain progression) 
1554+ 				currentTime  =  newTimestamp 
1555+ 			}
1556+ 
1557+ 			// Verify the intervals between deadlines 
1558+ 			for  i  :=  1 ; i  <  len (deadlines ); i ++  {
1559+ 				interval  :=  deadlines [i ].Sub (deadlines [i - 1 ])
1560+ 
1561+ 				// Allow small tolerance for timing precision 
1562+ 				tolerance  :=  10  *  time .Millisecond 
1563+ 				if  interval  <  tt .expectedInterval - tolerance  ||  interval  >  tt .expectedInterval + tolerance  {
1564+ 					t .Errorf ("Block %d interval: got %v, want %v (±%v)" ,
1565+ 						i , interval , tt .expectedInterval , tolerance )
1566+ 				}
1567+ 			}
1568+ 
1569+ 			// Note: Timestamp progression logic is verified in TestTimestampIncrementLogic 
1570+ 			// Here we focus on deadline intervals which is what really matters for block production timing 
1571+ 			blocksPerSecond  :=  system_contract .CalcBlocksPerSecond (tt .blocksPerSecond )
1572+ 			blocksPerPeriod  :=  blocksPerSecond  *  tt .period 
1573+ 
1574+ 			t .Logf ("Test %s completed successfully:" , tt .name )
1575+ 			t .Logf ("  - Expected interval: %v" , tt .expectedInterval )
1576+ 			t .Logf ("  - Blocks per period: %d" , blocksPerPeriod )
1577+ 			t .Logf ("  - Period length: %d seconds" , tt .period )
1578+ 		})
1579+ 	}
1580+ }
0 commit comments