-
Notifications
You must be signed in to change notification settings - Fork 7
add client filtering flag #75
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"sync/atomic" | ||
"time" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/ipfs/go-cid" | ||
"github.com/libp2p/go-libp2p-core/crypto" | ||
core "github.com/libp2p/go-libp2p-core/peer" | ||
|
@@ -121,6 +122,8 @@ type AuctionFilters struct { | |
DealDuration MinMaxFilter | ||
// DealSize sets the min and max deal size to bid on. | ||
DealSize MinMaxFilter | ||
// ClientAddressWhitelist if not empty only allows bidding in auctions from a clients list. | ||
ClientAddressWhitelist []string | ||
} | ||
|
||
// Validate ensures AuctionFilters are valid. | ||
|
@@ -131,6 +134,11 @@ func (f *AuctionFilters) Validate() error { | |
if err := f.DealDuration.Validate(); err != nil { | ||
return fmt.Errorf("invalid deal size filter: %v", err) | ||
} | ||
for _, clientAddress := range f.ClientAddressWhitelist { | ||
if _, err := address.NewFromString(clientAddress); err != nil { | ||
return fmt.Errorf("invalid client address filter %s: %s", clientAddress, err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
|
@@ -443,6 +451,19 @@ func (s *Service) filterAuction(auction *pb.Auction) (rejectReason string) { | |
s.auctionFilters.DealDuration.Max) | ||
} | ||
|
||
if len(s.auctionFilters.ClientAddressWhitelist) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gist of PR. |
||
var isWhitelistedClient bool | ||
for _, clientAddress := range s.auctionFilters.ClientAddressWhitelist { | ||
if clientAddress == auction.ClientAddress { | ||
isWhitelistedClient = true | ||
break | ||
} | ||
} | ||
if !isWhitelistedClient { | ||
return fmt.Sprintf("auction is from client %s which isn't whitelisted", auction.ClientAddress) | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package service | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
pb "github.com/textileio/bidbot/gen/v1" | ||
core "github.com/textileio/bidbot/lib/auction" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func TestClientFilter(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Start with no client filters | ||
s := Service{ | ||
auctionFilters: AuctionFilters{ | ||
DealDuration: MinMaxFilter{ | ||
Min: core.MinDealDuration, | ||
Max: core.MaxDealDuration, | ||
}, | ||
DealSize: MinMaxFilter{ | ||
Min: 1, | ||
Max: 10, | ||
}, | ||
}, | ||
} | ||
auction := &pb.Auction{ | ||
DealSize: 5, | ||
DealDuration: core.MinDealDuration, | ||
EndsAt: timestamppb.New(time.Now().Add(time.Minute)), | ||
ClientAddress: "f2kb4izxsxu2jyyslzwmv2sfbrgpld56efedgru5i", | ||
} | ||
|
||
t.Run("accept-any-client-address", func(t *testing.T) { | ||
require.Empty(t, s.filterAuction(auction)) | ||
}) | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test that if no whitelist is provided, we're accepting any auction. |
||
|
||
s.auctionFilters.ClientAddressWhitelist = []string{"f144zep4gitj73rrujd3jw6iprljicx6vl4wbeavi"} | ||
t.Run("reject-single-client-address", func(t *testing.T) { | ||
require.Contains(t, s.filterAuction(auction), "f2kb4izxsxu2jyyslzwmv2sfbrgpld56efedgru5i") | ||
}) | ||
Comment on lines
+40
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test with a whitelist only containing Then test if an auction from client The rejection message should be a string explaining the cause of rejection, which must say something about |
||
|
||
auction.ClientAddress = s.auctionFilters.ClientAddressWhitelist[0] | ||
t.Run("accept-single-client-address", func(t *testing.T) { | ||
require.Empty(t, s.filterAuction(auction)) | ||
}) | ||
Comment on lines
+45
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test the case where the auction client address matches a value in the whitelist, and check that the auction isn't rejected. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,22 @@ func TestNew(t *testing.T) { | |
}) | ||
require.Error(t, err) | ||
|
||
// Incorrect string representation of whitelisted client address. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Augment an existing test to check fast-fail in parsing a malformed client address in a whitelist. |
||
_, err = newService(t, func(config *service.Config) { | ||
config.AuctionFilters = service.AuctionFilters{ | ||
DealDuration: service.MinMaxFilter{ | ||
Min: 10, | ||
Max: 20, | ||
}, | ||
DealSize: service.MinMaxFilter{ | ||
Min: 10, | ||
Max: 20, | ||
}, | ||
ClientAddressWhitelist: []string{"invalidClientAddr"}, | ||
} | ||
}) | ||
require.Error(t, err) | ||
|
||
// Good config | ||
s, err := newService(t, nil) | ||
require.NoError(t, err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just updating
golangci-lint
.