Skip to content

Commit

Permalink
added userVerification to the pairing function
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlavanet committed Mar 22, 2022
1 parent 410bfc0 commit 69ed3fb
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 26 deletions.
23 changes: 12 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,6 @@ func New(
&stakingKeeper, govRouter,
)

app.ServicerKeeper = *servicermodulekeeper.NewKeeper(
appCodec,
keys[servicermoduletypes.StoreKey],
keys[servicermoduletypes.MemStoreKey],
app.GetSubspace(servicermoduletypes.ModuleName),

app.BankKeeper,
app.AccountKeeper,
app.EvidenceKeeper,
app.SpecKeeper,
)
servicerModule := servicermodule.NewAppModule(appCodec, app.ServicerKeeper, app.AccountKeeper, app.BankKeeper, app.SpecKeeper)

app.UserKeeper = *usermodulekeeper.NewKeeper(
Expand All @@ -411,6 +400,18 @@ func New(
app.AccountKeeper,
app.SpecKeeper,
)
app.ServicerKeeper = *servicermodulekeeper.NewKeeper(
appCodec,
keys[servicermoduletypes.StoreKey],
keys[servicermoduletypes.MemStoreKey],
app.GetSubspace(servicermoduletypes.ModuleName),

app.BankKeeper,
app.AccountKeeper,
app.EvidenceKeeper,
app.SpecKeeper,
app.UserKeeper,
)
userModule := usermodule.NewAppModule(appCodec, app.UserKeeper, app.AccountKeeper, app.BankKeeper)

// this line is used by starport scaffolding # stargate/app/keeperDefinition
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ require (
github.com/tendermint/starport v0.19.2
github.com/tendermint/tendermint v0.34.14
github.com/tendermint/tm-db v0.6.4
google.golang.org/genproto v0.0.0-20220302033224-9aa15565e42a
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e
google.golang.org/grpc v1.45.0
google.golang.org/protobuf v1.27.1
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2157,8 +2157,8 @@ google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1/go.mod h1:9lPAdzaE
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
google.golang.org/genproto v0.0.0-20210828152312-66f60bf46e71/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY=
google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20220314164441-57ef72a4c106 h1:ErU+UA6wxadoU8nWrsy5MZUVBs75K17zUCsUCIfrXCE=
google.golang.org/genproto v0.0.0-20220314164441-57ef72a4c106/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e h1:fNKDNuUyC4WH+inqDMpfXDdfvwfYILbsX+oskGZ8hxg=
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
1 change: 1 addition & 0 deletions testutil/keeper/servicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func ServicerKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
nil,
nil,
nil,
nil,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
5 changes: 3 additions & 2 deletions x/servicer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
accountKeeper types.AccountKeeper
evidenceKeeper types.EvidenceKeeper
specKeeper types.SpecKeeper
userKeeper types.UserKeeper
}
)

Expand All @@ -31,7 +32,7 @@ func NewKeeper(
memKey sdk.StoreKey,
ps paramtypes.Subspace,

bankKeeper types.BankKeeper, accountKeeper types.AccountKeeper, evidenceKeeper types.EvidenceKeeper, specKeeper types.SpecKeeper,
bankKeeper types.BankKeeper, accountKeeper types.AccountKeeper, evidenceKeeper types.EvidenceKeeper, specKeeper types.SpecKeeper, userKeeper types.UserKeeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
Expand All @@ -44,7 +45,7 @@ func NewKeeper(
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
bankKeeper: bankKeeper, accountKeeper: accountKeeper, evidenceKeeper: evidenceKeeper, specKeeper: specKeeper,
bankKeeper: bankKeeper, accountKeeper: accountKeeper, evidenceKeeper: evidenceKeeper, specKeeper: specKeeper, userKeeper: userKeeper,
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/servicer/keeper/msg_server_proof_of_work.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (k msgServer) ProofOfWork(goCtx context.Context, msg *types.MsgProofOfWork)
}
//TODO: validate CU requested is valid for the user and not too big, this requires the user module
//TODO: validate the user request only holds supported apis
possibleServicerAddresses, err := k.Keeper.GetPairingForClient(ctx, *msg.BlockOfWork, uint64(clientRequest.Spec_id), clientAddr)
_, possibleServicerAddresses, err := k.Keeper.GetPairingForClient(ctx, *msg.BlockOfWork, uint64(clientRequest.Spec_id), clientAddr)
if err != nil {
return nil, fmt.Errorf("error on pairing for addresses : %s and %s, err: %s", clientAddr, msg.Creator, err)
}
Expand Down
56 changes: 48 additions & 8 deletions x/servicer/keeper/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,69 @@ import (
"github.com/lavanet/lava/x/servicer/types"
)

func (k Keeper) GetPairingForClient(ctx sdk.Context, block types.BlockNum, specID uint64, clientAddress sdk.AccAddress) ([]sdk.AccAddress, error) {
//TODO: client stake needs to be verified
//first argument has all metadata, second argument is only the addresses
func (k Keeper) GetPairingForClient(ctx sdk.Context, block types.BlockNum, specID uint64, clientAddress sdk.AccAddress) ([]types.StakeMap, []sdk.AccAddress, error) {
spec, found := k.specKeeper.GetSpec(ctx, specID)
if !found {
return nil, fmt.Errorf("spec not found for id given: %d", specID)
return nil, nil, fmt.Errorf("spec not found for id given: %d", specID)
}
specStakeStorage, found := k.GetSpecStakeStorage(ctx, spec.Name)
if !found {
return nil, fmt.Errorf("no specStakeStorage for spec name: %s", spec.Name)
return nil, nil, fmt.Errorf("no specStakeStorage for spec name: %s", spec.Name)
}
stakedServicers := specStakeStorage.StakeStorage.Staked

verifiedUser := false
userSpecStakeStorageForSpec, found := k.userKeeper.GetSpecStakeStorage(ctx, spec.Name)
allStakedUsersForSpec := userSpecStakeStorageForSpec.StakeStorage.StakedUsers
for _, stakedUser := range allStakedUsersForSpec {
userAddr, err := sdk.AccAddressFromBech32(stakedUser.Index)
if err != nil {
panic(fmt.Sprintf("invalid servicer address saved in keeper %s, err: %s", stakedUser.Index, err))
}
if userAddr.Equals(clientAddress) {
verifiedUser = true
break
}
}
if !verifiedUser {
return nil, nil, fmt.Errorf("client: %s isn't staked for spec %s", clientAddress, spec.Name)
}

return k.calculatePairingForClient(ctx, stakedServicers, block, clientAddress)
}

func (k Keeper) calculatePairingForClient(ctx sdk.Context, stakedServicers []types.StakeMap, block types.BlockNum, clientAddress sdk.AccAddress) (addrList []sdk.AccAddress, err error) {
//TODO: need to do the pairing function, right now i just return the staked servicers addresses
func (k Keeper) calculatePairingForClient(ctx sdk.Context, stakedServicers []types.StakeMap, block types.BlockNum, clientAddress sdk.AccAddress) (validServicers []types.StakeMap, addrList []sdk.AccAddress, err error) {
//create a list of valid servicers (deadline reached)
for _, stakeMap := range stakedServicers {
if stakeMap.Deadline.Num > uint64(ctx.BlockHeight()) {
//servicer deadline wasn't reached yet
continue
}
validServicers = append(validServicers, stakeMap)
}

//calculates a hash and randomly chooses the servicers
k.returnSubsetOfServicersByStake(validServicers, 2, uint(block.Num))

for _, stakeMap := range validServicers {
servicerAddress := stakeMap.Index
servicerAccAddr, err := sdk.AccAddressFromBech32(servicerAddress)
if err != nil {
panic(fmt.Sprintf("invalid servicer address saved in keeper %s", servicerAddress))
panic(fmt.Sprintf("invalid servicer address saved in keeper %s, err: %s", servicerAddress, err))
}
addrList = append(addrList, servicerAccAddr)
}
return addrList, nil
return validServicers, addrList, nil
}

func (k Keeper) returnSubsetOfServicersByStake(servicersMaps []types.StakeMap, count uint, block uint) (returnedServicers []types.StakeMap) {
//TODO: need to do the pairing function, right now i just return the first staked servicers addresses
for _, stakedServicer := range servicersMaps {
if uint(len(returnedServicers)) >= count {
return returnedServicers
}
returnedServicers = append(returnedServicers, stakedServicer)
}
return returnedServicers
}
5 changes: 5 additions & 0 deletions x/servicer/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
spectypes "github.com/lavanet/lava/x/spec/types"
usertypes "github.com/lavanet/lava/x/user/types"
)

type EvidenceKeeper interface {
Expand Down Expand Up @@ -32,3 +33,7 @@ type SpecKeeper interface {
IsSpecIDFoundAndActive(ctx sdk.Context, id uint64) (bool, bool)
GetSpec(ctx sdk.Context, id uint64) (val spectypes.Spec, found bool)
}

type UserKeeper interface {
GetSpecStakeStorage(sdk.Context, string) (usertypes.SpecStakeStorage, bool)
}

0 comments on commit 69ed3fb

Please sign in to comment.