Skip to content

Commit

Permalink
Feat: Prevent Providers with Matching Domains Attesting (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Jul 7, 2023
2 parents 1a92a63 + 91c1f4f commit f537ff0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 11 deletions.
14 changes: 10 additions & 4 deletions x/storage/keeper/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ func (suite *KeeperTestSuite) TestMakeAttestation() {
addresses, err := testutil.CreateTestAddresses("jkl", 50)
suite.NoError(err)

for _, address := range addresses {
for i, address := range addresses {
realProvider := types.Providers{
Address: address,
Ip: fmt.Sprintf("https://test%d.com", i),
}

suite.storageKeeper.SetProviders(suite.ctx, realProvider)
suite.storageKeeper.SetActiveProviders(suite.ctx, types.ActiveProviders{
Address: address,
})
Expand All @@ -148,10 +154,10 @@ func (suite *KeeperTestSuite) TestMakeAttestation() {
Provider: addresses[10],
Startblock: "",
Endblock: "",
Filesize: "",
Filesize: "10",
Proofverified: "false",
Proofsmissed: "",
Blocktoprove: "",
Proofsmissed: "0",
Blocktoprove: "0",
Creator: "",
Merkle: "",
Fid: "",
Expand Down
8 changes: 7 additions & 1 deletion x/storage/keeper/msg_server_attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ func (k Keeper) RequestAttestation(ctx sdk.Context, cid string, creator string)
return nil, sdkerrors.Wrapf(types.ErrAttestAlreadyExists, "attestation form already exists")
}

providers := k.GetActiveProviders(ctx) // get a random list of active providers
dealProvider := deal.Provider
provider, found := k.GetProviders(ctx, dealProvider)
if !found {
return nil, sdkerrors.Wrapf(types.ErrProviderNotFound, "cannot find provider matching deal")
}

providers := k.GetActiveProviders(ctx, provider.Ip) // get a random list of active providers
params := k.GetParams(ctx)

if len(providers) < int(params.AttestFormSize) {
Expand Down
10 changes: 8 additions & 2 deletions x/storage/keeper/msg_server_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (k msgServer) Report(goCtx context.Context, msg *types.MsgReport) (*types.M
}

func (k Keeper) RequestReport(ctx sdk.Context, cid string) ([]string, error) {
_, found := k.GetActiveDeals(ctx, cid)
deal, found := k.GetActiveDeals(ctx, cid)
if !found {
return nil, sdkerrors.Wrapf(types.ErrDealNotFound, "cannot find active deal for report form")
}
Expand All @@ -75,7 +75,13 @@ func (k Keeper) RequestReport(ctx sdk.Context, cid string) ([]string, error) {
return nil, sdkerrors.Wrapf(types.ErrAttestAlreadyExists, "report form already exists")
}

providers := k.GetActiveProviders(ctx) // get a random list of active providers
dealProvider := deal.Provider
provider, found := k.GetProviders(ctx, dealProvider)
if !found {
return nil, sdkerrors.Wrapf(types.ErrProviderNotFound, "cannot find provider matching deal")
}

providers := k.GetActiveProviders(ctx, provider.Ip) // get a random list of active providers
params := k.GetParams(ctx)

if len(providers) < int(params.AttestFormSize) {
Expand Down
51 changes: 50 additions & 1 deletion x/storage/keeper/providers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"net/url"
"strings"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
Expand All @@ -12,8 +15,54 @@ const (
)

// GetActiveProviders returns a list of recently active providers in a random order
func (k Keeper) GetActiveProviders(ctx sdk.Context) []types.ActiveProviders {
func (k Keeper) GetActiveProviders(ctx sdk.Context, filterAddress string) []types.ActiveProviders {
providers := k.GetAllActiveProviders(ctx)
allowedProviders := make([]types.ActiveProviders, 0)

filterDomain := ""
filterTLD := ""

if len(filterAddress) > 0 {
url, err := url.Parse(filterAddress)
if err != nil {
return nil
}

parts := strings.Split(url.Hostname(), ".")
partCount := len(parts)
if partCount >= 2 {
filterDomain = parts[partCount-2]
filterTLD = parts[partCount-1]
}
}

for _, provider := range providers {
providerAccount, found := k.GetProviders(ctx, provider.Address)
if !found {
continue
}
url, err := url.Parse(providerAccount.Ip)
if err != nil {
continue
}

parts := strings.Split(url.Hostname(), ".")
partCount := len(parts)
if partCount < 2 {
continue
}

domain := parts[partCount-2]
tld := parts[partCount-1]

if domain == filterDomain && tld == filterTLD {
continue
}

allowedProviders = append(allowedProviders, provider)

}
providers = allowedProviders

size := len(providers)

Expand Down
14 changes: 12 additions & 2 deletions x/storage/keeper/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,27 @@ func (suite *KeeperTestSuite) TestActiveProviders() {

user := testAddresses[0]

realProvider := types.Providers{
Address: user,
Ip: "https://test.com",
}

suite.storageKeeper.SetProviders(suite.ctx, realProvider)

provider := types.ActiveProviders{
Address: user,
}

suite.storageKeeper.SetActiveProviders(suite.ctx, provider)

foundProvider := suite.storageKeeper.GetActiveProviders(suite.ctx)
foundProvider := suite.storageKeeper.GetActiveProviders(suite.ctx, "")
suite.Require().Equal(1, len(foundProvider))

foundProvider = suite.storageKeeper.GetActiveProviders(suite.ctx, "https://test.com")
suite.Require().Equal(0, len(foundProvider))

suite.storageKeeper.RemoveAllActiveProviders(suite.ctx)

foundProvider = suite.storageKeeper.GetActiveProviders(suite.ctx)
foundProvider = suite.storageKeeper.GetActiveProviders(suite.ctx, "")
suite.Require().Equal(0, len(foundProvider))
}
10 changes: 9 additions & 1 deletion x/storage/keeper/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ func (suite *KeeperTestSuite) TestMakeReport() {
addresses, err := testutil.CreateTestAddresses("jkl", 50)
suite.NoError(err)

for _, address := range addresses {
for i, address := range addresses {
realProvider := types.Providers{
Address: address,
Ip: fmt.Sprintf("https://test%d.com", i),
BurnedContracts: "0",
}

suite.storageKeeper.SetProviders(suite.ctx, realProvider)

suite.storageKeeper.SetActiveProviders(suite.ctx, types.ActiveProviders{
Address: address,
})
Expand Down

0 comments on commit f537ff0

Please sign in to comment.