diff --git a/p2p/pkg/preconfirmation/store/store.go b/p2p/pkg/preconfirmation/store/store.go index 943955d88..03dd69c54 100644 --- a/p2p/pkg/preconfirmation/store/store.go +++ b/p2p/pkg/preconfirmation/store/store.go @@ -209,18 +209,42 @@ func (s *Store) GetCommitments(blockNum int64) ([]*Commitment, error) { return commitments, nil } -func (s *Store) GetAllCommitments() ([]*Commitment, error) { +type ListOpts struct { + Page int + Limit int +} + +var defaultListOpts = &ListOpts{ + Page: 0, + Limit: 100, +} + +func (s *Store) ListCommitments(opts *ListOpts) ([]*Commitment, error) { + if opts == nil { + opts = defaultListOpts + } + s.mu.RLock() defer s.mu.RUnlock() - commitments := make([]*Commitment, 0) + start := opts.Page * opts.Limit + count := 0 + + commitments := make([]*Commitment, 0, opts.Limit) err := s.st.WalkPrefix(commitmentNS, func(key string, value []byte) bool { + if count < start { + count++ + return false // Skip this entry + } commitment := new(Commitment) err := msgpack.Unmarshal(value, commitment) if err != nil { return false } commitments = append(commitments, commitment) + if len(commitments) >= opts.Limit { + return true // Stop walking once we reach the limit + } return false }) if err != nil { diff --git a/p2p/pkg/preconfirmation/store/store_test.go b/p2p/pkg/preconfirmation/store/store_test.go index e8e6e9103..2234ce53c 100644 --- a/p2p/pkg/preconfirmation/store/store_test.go +++ b/p2p/pkg/preconfirmation/store/store_test.go @@ -527,7 +527,7 @@ func TestStore_GetAllCommitments(t *testing.T) { t.Fatal(err) } - allCommitments, err := st.GetAllCommitments() + allCommitments, err := st.ListCommitments(nil) if err != nil { t.Fatal(err) } diff --git a/p2p/pkg/preconfirmation/tracker/tracker_test.go b/p2p/pkg/preconfirmation/tracker/tracker_test.go index 12347a38c..0cef52801 100644 --- a/p2p/pkg/preconfirmation/tracker/tracker_test.go +++ b/p2p/pkg/preconfirmation/tracker/tracker_test.go @@ -402,7 +402,7 @@ func TestTracker(t *testing.T) { var cmts []*store.Commitment start := time.Now() for { - cmts, err := st.GetAllCommitments() + cmts, err := st.ListCommitments(nil) if err != nil { t.Fatal(err) } @@ -455,7 +455,7 @@ func TestTracker(t *testing.T) { if time.Since(start) > 15*time.Second { t.Fatal("timeout waiting for tracker to finish") } - cmts, err = st.GetAllCommitments() + cmts, err = st.ListCommitments(nil) if err != nil { t.Fatal(err) } diff --git a/p2p/pkg/rpc/bidder/service.go b/p2p/pkg/rpc/bidder/service.go index e3ab8eef8..8965606c0 100644 --- a/p2p/pkg/rpc/bidder/service.go +++ b/p2p/pkg/rpc/bidder/service.go @@ -107,7 +107,7 @@ type ProviderRegistryContract interface { type CommitmentStore interface { GetCommitments(blockNumber int64) ([]*preconfstore.Commitment, error) - GetAllCommitments() ([]*preconfstore.Commitment, error) + ListCommitments(opts *preconfstore.ListOpts) ([]*preconfstore.Commitment, error) } type BlockTrackerContract interface { @@ -750,10 +750,17 @@ func (s *Service) GetBidInfo( page, limit = int(req.Page), int(req.Limit) ) + if limit == 0 { + limit = defaultLimit + } + if req.BlockNumber != 0 { cmts, err = s.cs.GetCommitments(req.BlockNumber) } else { - cmts, err = s.cs.GetAllCommitments() + cmts, err = s.cs.ListCommitments(&preconfstore.ListOpts{ + Page: page, + Limit: limit, + }) } if err != nil { return nil, status.Errorf(codes.Internal, "getting commitments: %v", err) @@ -761,10 +768,6 @@ func (s *Service) GetBidInfo( if len(cmts) == 0 { return &bidderapiv1.GetBidInfoResponse{}, nil } - if limit == 0 { - limit = defaultLimit - } - cmts = cmts[min(page*limit, len(cmts)):min((page+1)*limit, len(cmts))] blockBids := make([]*bidderapiv1.GetBidInfoResponse_BlockBidInfo, 0) LOOP: for _, c := range cmts { diff --git a/p2p/pkg/rpc/bidder/service_test.go b/p2p/pkg/rpc/bidder/service_test.go index 4e2d0b4d0..34acdd62e 100644 --- a/p2p/pkg/rpc/bidder/service_test.go +++ b/p2p/pkg/rpc/bidder/service_test.go @@ -205,7 +205,7 @@ type testStore struct { commitments []*preconfstore.Commitment } -func (t *testStore) GetAllCommitments() ([]*preconfstore.Commitment, error) { +func (t *testStore) ListCommitments(_ *preconfstore.ListOpts) ([]*preconfstore.Commitment, error) { return t.commitments, nil } diff --git a/p2p/pkg/rpc/provider/service.go b/p2p/pkg/rpc/provider/service.go index 79841ebb2..cba9868e6 100644 --- a/p2p/pkg/rpc/provider/service.go +++ b/p2p/pkg/rpc/provider/service.go @@ -73,7 +73,7 @@ type Watcher interface { type CommitmentStore interface { GetCommitments(blockNumber int64) ([]*preconfstore.Commitment, error) - GetAllCommitments() ([]*preconfstore.Commitment, error) + ListCommitments(opts *preconfstore.ListOpts) ([]*preconfstore.Commitment, error) } type OptsGetter func(ctx context.Context) (*bind.TransactOpts, error) @@ -517,10 +517,17 @@ func (s *Service) GetCommitmentInfo( page, limit = int(req.Page), int(req.Limit) ) + if limit == 0 { + limit = defaultLimit + } + if req.BlockNumber != 0 { cmts, err = s.cs.GetCommitments(req.BlockNumber) } else { - cmts, err = s.cs.GetAllCommitments() + cmts, err = s.cs.ListCommitments(&preconfstore.ListOpts{ + Page: page, + Limit: limit, + }) } if err != nil { return nil, status.Errorf(codes.Internal, "getting commitments: %v", err) @@ -531,10 +538,6 @@ func (s *Service) GetCommitmentInfo( }, nil } - if limit == 0 { - limit = defaultLimit - } - cmts = cmts[min(page*limit, len(cmts)):min((page+1)*limit, len(cmts))] blockCommitments := make([]*providerapiv1.CommitmentInfoResponse_BlockCommitments, 0) for _, c := range cmts { if len(blockCommitments) == 0 || blockCommitments[len(blockCommitments)-1].BlockNumber != c.Bid.BlockNumber { diff --git a/p2p/pkg/rpc/provider/service_test.go b/p2p/pkg/rpc/provider/service_test.go index b6b0461a9..33e7f9c30 100644 --- a/p2p/pkg/rpc/provider/service_test.go +++ b/p2p/pkg/rpc/provider/service_test.go @@ -152,7 +152,7 @@ type testStore struct { commitments []*preconfstore.Commitment } -func (t *testStore) GetAllCommitments() ([]*preconfstore.Commitment, error) { +func (t *testStore) ListCommitments(_ *preconfstore.ListOpts) ([]*preconfstore.Commitment, error) { return t.commitments, nil }