@@ -12,6 +12,7 @@ import (
12
12
"os"
13
13
"sort"
14
14
"testing"
15
+ "time"
15
16
16
17
"github.com/stretchr/testify/require"
17
18
cbg "github.com/whyrusleeping/cbor-gen"
@@ -1604,3 +1605,185 @@ readLoop:
1604
1605
t .Fatalf ("failed to pack with tq=0.01; packed %d, minimum packing: %d" , gasLimit , minGasLimit )
1605
1606
}
1606
1607
}
1608
+
1609
+ func TestRealWorldSelectionTiming (t * testing.T ) {
1610
+ //stm: @TOKEN_WALLET_NEW_001, @TOKEN_WALLET_SIGN_001, @CHAIN_MEMPOOL_SELECT_001
1611
+
1612
+ // load test-messages.json.gz and rewrite the messages so that
1613
+ // 1) we map each real actor to a test actor so that we can sign the messages
1614
+ // 2) adjust the nonces so that they start from 0
1615
+ file , err := os .Open ("test-messages2.json.gz" )
1616
+ if err != nil {
1617
+ t .Fatal (err )
1618
+ }
1619
+
1620
+ gzr , err := gzip .NewReader (file )
1621
+ if err != nil {
1622
+ t .Fatal (err )
1623
+ }
1624
+
1625
+ dec := json .NewDecoder (gzr )
1626
+
1627
+ var msgs []* types.SignedMessage
1628
+ baseNonces := make (map [address.Address ]uint64 )
1629
+
1630
+ readLoop:
1631
+ for {
1632
+ m := new (types.SignedMessage )
1633
+ err := dec .Decode (m )
1634
+ switch err {
1635
+ case nil :
1636
+ msgs = append (msgs , m )
1637
+ nonce , ok := baseNonces [m .Message .From ]
1638
+ if ! ok || m .Message .Nonce < nonce {
1639
+ baseNonces [m .Message .From ] = m .Message .Nonce
1640
+ }
1641
+
1642
+ case io .EOF :
1643
+ break readLoop
1644
+
1645
+ default :
1646
+ t .Fatal (err )
1647
+ }
1648
+ }
1649
+
1650
+ actorMap := make (map [address.Address ]address.Address )
1651
+ actorWallets := make (map [address.Address ]* wallet.Wallet )
1652
+
1653
+ for _ , m := range msgs {
1654
+ baseNonce := baseNonces [m .Message .From ]
1655
+
1656
+ localActor , ok := actorMap [m .Message .From ]
1657
+ if ! ok {
1658
+ w := newWallet (t )
1659
+
1660
+ a , err := w .NewAddress (context .Background (), address .SECP256K1 )
1661
+ if err != nil {
1662
+ t .Fatal (err )
1663
+ }
1664
+
1665
+ actorMap [m .Message .From ] = a
1666
+ actorWallets [a ] = w
1667
+ localActor = a
1668
+ }
1669
+
1670
+ w , ok := actorWallets [localActor ]
1671
+ if ! ok {
1672
+ t .Fatalf ("failed to lookup wallet for actor %s" , localActor )
1673
+ }
1674
+
1675
+ m .Message .From = localActor
1676
+ m .Message .Nonce -= baseNonce
1677
+
1678
+ sig , err := w .WalletSign (context .TODO (), localActor , m .Message .Cid ().Bytes (), types.MsgMeta {})
1679
+ if err != nil {
1680
+ t .Fatal (err )
1681
+ }
1682
+
1683
+ m .Signature = * sig
1684
+ }
1685
+
1686
+ mp , tma := makeTestMpool ()
1687
+
1688
+ block := tma .nextBlockWithHeight (uint64 (UpgradeBreezeHeight ) + 10 )
1689
+ ts := mkTipSet (block )
1690
+ tma .applyBlock (t , block )
1691
+
1692
+ for _ , a := range actorMap {
1693
+ tma .setBalance (a , 1000000 )
1694
+ }
1695
+
1696
+ tma .baseFee = types .NewInt (800_000_000 )
1697
+
1698
+ sort .Slice (msgs , func (i , j int ) bool {
1699
+ return msgs [i ].Message .Nonce < msgs [j ].Message .Nonce
1700
+ })
1701
+
1702
+ // add the messages
1703
+ for _ , m := range msgs {
1704
+ mustAdd (t , mp , m )
1705
+ }
1706
+
1707
+ // do message selection and check block packing
1708
+ minGasLimit := int64 (0.9 * float64 (constants .BlockGasLimit ))
1709
+
1710
+ // greedy first
1711
+ start := time .Now ()
1712
+ selected , err := mp .SelectMessages (context .Background (), ts , 1.0 )
1713
+ if err != nil {
1714
+ t .Fatal (err )
1715
+ }
1716
+ t .Logf ("selected %d messages in %s" , len (selected ), time .Since (start ))
1717
+
1718
+ gasLimit := int64 (0 )
1719
+ for _ , m := range selected {
1720
+ gasLimit += m .Message .GasLimit
1721
+ }
1722
+ if gasLimit < minGasLimit {
1723
+ t .Fatalf ("failed to pack with tq=1.0; packed %d, minimum packing: %d" , gasLimit , minGasLimit )
1724
+ }
1725
+
1726
+ // high quality ticket
1727
+ start = time .Now ()
1728
+ selected , err = mp .SelectMessages (context .Background (), ts , .8 )
1729
+ if err != nil {
1730
+ t .Fatal (err )
1731
+ }
1732
+ t .Logf ("selected %d messages in %s" , len (selected ), time .Since (start ))
1733
+
1734
+ gasLimit = int64 (0 )
1735
+ for _ , m := range selected {
1736
+ gasLimit += m .Message .GasLimit
1737
+ }
1738
+ if gasLimit < minGasLimit {
1739
+ t .Fatalf ("failed to pack with tq=0.8; packed %d, minimum packing: %d" , gasLimit , minGasLimit )
1740
+ }
1741
+
1742
+ // mid quality ticket
1743
+ start = time .Now ()
1744
+ selected , err = mp .SelectMessages (context .Background (), ts , .4 )
1745
+ if err != nil {
1746
+ t .Fatal (err )
1747
+ }
1748
+ t .Logf ("selected %d messages in %s" , len (selected ), time .Since (start ))
1749
+
1750
+ gasLimit = int64 (0 )
1751
+ for _ , m := range selected {
1752
+ gasLimit += m .Message .GasLimit
1753
+ }
1754
+ if gasLimit < minGasLimit {
1755
+ t .Fatalf ("failed to pack with tq=0.4; packed %d, minimum packing: %d" , gasLimit , minGasLimit )
1756
+ }
1757
+
1758
+ // low quality ticket
1759
+ start = time .Now ()
1760
+ selected , err = mp .SelectMessages (context .Background (), ts , .1 )
1761
+ if err != nil {
1762
+ t .Fatal (err )
1763
+ }
1764
+ t .Logf ("selected %d messages in %s" , len (selected ), time .Since (start ))
1765
+
1766
+ gasLimit = int64 (0 )
1767
+ for _ , m := range selected {
1768
+ gasLimit += m .Message .GasLimit
1769
+ }
1770
+ if gasLimit < minGasLimit {
1771
+ t .Fatalf ("failed to pack with tq=0.1; packed %d, minimum packing: %d" , gasLimit , minGasLimit )
1772
+ }
1773
+
1774
+ // very low quality ticket
1775
+ start = time .Now ()
1776
+ selected , err = mp .SelectMessages (context .Background (), ts , .01 )
1777
+ if err != nil {
1778
+ t .Fatal (err )
1779
+ }
1780
+ t .Logf ("selected %d messages in %s" , len (selected ), time .Since (start ))
1781
+
1782
+ gasLimit = int64 (0 )
1783
+ for _ , m := range selected {
1784
+ gasLimit += m .Message .GasLimit
1785
+ }
1786
+ if gasLimit < minGasLimit {
1787
+ t .Fatalf ("failed to pack with tq=0.01; packed %d, minimum packing: %d" , gasLimit , minGasLimit )
1788
+ }
1789
+ }
0 commit comments