Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions p2p/pkg/preconfirmation/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/pkg/preconfirmation/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/pkg/preconfirmation/tracker/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 9 additions & 6 deletions p2p/pkg/rpc/bidder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -750,21 +750,24 @@ 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)
}
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 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/pkg/rpc/bidder/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 9 additions & 6 deletions p2p/pkg/rpc/provider/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion p2p/pkg/rpc/provider/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading