Skip to content

Commit

Permalink
Merge remote-tracking branch 'pokt/main' into issues/584/refactor/smt…
Browse files Browse the repository at this point in the history
…-root

* pokt/main:
  [TODOs] add `EventApplicationOverserviced` event (#669)
  [Supplier] Deny supplier staking with unknown services (#693)
  • Loading branch information
bryanchriswhite committed Jul 17, 2024
2 parents 35e253c + d8b4218 commit 91f9cd9
Show file tree
Hide file tree
Showing 22 changed files with 1,218 additions and 109 deletions.
715 changes: 689 additions & 26 deletions api/poktroll/tokenomics/event.pulsar.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ genesis:
service:
params:
add_service_fee: "1000000000"
serviceList:
- id: anvil
name: ""
proof:
params:
proof_request_probability: "0.25"
Expand Down
4 changes: 0 additions & 4 deletions localnet/poktrolld/config/supplier1_stake_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
# so that the stake command causes a state change.
stake_amount: 1000069upokt
services:
- service_id: svc1
endpoints:
- publicly_exposed_url: http://localhost:8081
rpc_type: JSON_RPC
# The endpoint URL for the Anvil service is provided via the RelayMiner.
# The RelayMiner acts as a proxy, forwarding requests to the actual Anvil data node behind it.
# This setup allows for flexible and dynamic service provisioning within the network.
Expand Down
9 changes: 9 additions & 0 deletions proto/poktroll/tokenomics/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package poktroll.tokenomics;
option go_package = "github.com/pokt-network/poktroll/x/tokenomics/types";

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";
import "poktroll/proof/claim.proto";
import "poktroll/proof/requirement.proto";

Expand Down Expand Up @@ -34,3 +35,11 @@ message EventRelayMiningDifficultyUpdated {
uint64 prev_num_relays_ema = 4;
uint64 new_num_relays_ema = 5;
}

// EventApplicationOverserviced is emitted when an application has less stake
// than the expected burn.
message EventApplicationOverserviced {
string application_addr = 1;
cosmos.base.v1beta1.Coin expected_burn = 2;
cosmos.base.v1beta1.Coin effective_burn = 3;
}
2 changes: 2 additions & 0 deletions testutil/integration/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,14 @@ func NewCompleteIntegrationApp(t *testing.T) *App {
logger,
authority.String(),
bankKeeper,
serviceKeeper,
)
supplierModule := supplier.NewAppModule(
cdc,
supplierKeeper,
accountKeeper,
bankKeeper,
serviceKeeper,
)

// Prepare the session keeper and module
Expand Down
14 changes: 14 additions & 0 deletions testutil/keeper/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
applicationmocks "github.com/pokt-network/poktroll/testutil/application/mocks"
gatewaymocks "github.com/pokt-network/poktroll/testutil/gateway/mocks"
"github.com/pokt-network/poktroll/testutil/proof/mocks"
servicemocks "github.com/pokt-network/poktroll/testutil/service/mocks"
sessionmocks "github.com/pokt-network/poktroll/testutil/session/mocks"
suppliermocks "github.com/pokt-network/poktroll/testutil/supplier/mocks"
appkeeper "github.com/pokt-network/poktroll/x/application/keeper"
Expand All @@ -37,6 +38,8 @@ import (
"github.com/pokt-network/poktroll/x/proof/keeper"
"github.com/pokt-network/poktroll/x/proof/types"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
servicekeeper "github.com/pokt-network/poktroll/x/service/keeper"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedkeeper "github.com/pokt-network/poktroll/x/shared/keeper"
Expand Down Expand Up @@ -118,6 +121,7 @@ func NewProofModuleKeepers(t testing.TB, opts ...ProofKeepersOpt) (_ *ProofModul
gatewaytypes.StoreKey,
authtypes.StoreKey,
sharedtypes.StoreKey,
servicetypes.StoreKey,
)

// Construct a multistore & mount store keys for each keeper that will interact with the state store.
Expand Down Expand Up @@ -179,13 +183,23 @@ func NewProofModuleKeepers(t testing.TB, opts ...ProofKeepersOpt) (_ *ProofModul
)
require.NoError(t, appKeeper.SetParams(ctx, apptypes.DefaultParams()))

// Construct a service keeper need by the supplier keeper.
serviceKeeper := servicekeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[types.StoreKey]),
log.NewNopLogger(),
authority.String(),
servicemocks.NewMockBankKeeper(ctrl),
)

// Construct a real supplier keeper to add suppliers to sessions.
supplierKeeper := supplierkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[suppliertypes.StoreKey]),
log.NewNopLogger(),
authority.String(),
suppliermocks.NewMockBankKeeper(ctrl),
serviceKeeper,
)
require.NoError(t, supplierKeeper.SetParams(ctx, suppliertypes.DefaultParams()))

Expand Down
16 changes: 16 additions & 0 deletions testutil/keeper/supplier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/supplier/mocks"
servicekeeper "github.com/pokt-network/poktroll/x/service/keeper"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
"github.com/pokt-network/poktroll/x/supplier/keeper"
"github.com/pokt-network/poktroll/x/supplier/types"
)
Expand All @@ -42,20 +44,34 @@ func SupplierKeeper(t testing.TB) (keeper.Keeper, context.Context) {
mockBankKeeper := mocks.NewMockBankKeeper(ctrl)
mockBankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, gomock.Any()).AnyTimes()
mockBankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, gomock.Any(), gomock.Any()).AnyTimes()
mockBankKeeper.EXPECT().SpendableCoins(gomock.Any(), gomock.Any()).AnyTimes()

serviceKeeper := servicekeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
mockBankKeeper,
)

k := keeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
mockBankKeeper,
serviceKeeper,
)

ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())

// Initialize params
require.NoError(t, k.SetParams(ctx, types.DefaultParams()))

// Add existing services used in the test.
serviceKeeper.SetService(ctx, sharedtypes.Service{Id: "svcId"})
serviceKeeper.SetService(ctx, sharedtypes.Service{Id: "svcId2"})

return k, ctx
}

Expand Down
12 changes: 12 additions & 0 deletions testutil/keeper/tokenomics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
proofkeeper "github.com/pokt-network/poktroll/x/proof/keeper"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
servicekeeper "github.com/pokt-network/poktroll/x/service/keeper"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessionkeeper "github.com/pokt-network/poktroll/x/session/keeper"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedkeeper "github.com/pokt-network/poktroll/x/shared/keeper"
Expand Down Expand Up @@ -290,13 +292,23 @@ func NewTokenomicsModuleKeepers(
)
require.NoError(t, appKeeper.SetParams(ctx, apptypes.DefaultParams()))

// Construct a service keeper needed by the supplier keeper.
serviceKeeper := servicekeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[servicetypes.StoreKey]),
log.NewNopLogger(),
authority.String(),
bankKeeper,
)

// Construct a real supplier keeper to add suppliers to sessions.
supplierKeeper := supplierkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[suppliertypes.StoreKey]),
log.NewNopLogger(),
authority.String(),
bankKeeper,
serviceKeeper,
)
require.NoError(t, supplierKeeper.SetParams(ctx, suppliertypes.DefaultParams()))

Expand Down
4 changes: 2 additions & 2 deletions x/service/keeper/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (k Keeper) RemoveService(
store.Delete(types.ServiceKey(serviceId))
}

// GetAllService returns all service
func (k Keeper) GetAllService(ctx context.Context) (services []sharedtypes.Service) {
// GetAllServices returns all services
func (k Keeper) GetAllServices(ctx context.Context) (services []sharedtypes.Service) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.ServiceKeyPrefix))
iterator := storetypes.KVStorePrefixIterator(store, []byte{})
Expand Down
2 changes: 1 addition & 1 deletion x/service/keeper/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ func TestServiceGetAll(t *testing.T) {
services := createNServices(keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(services),
nullify.Fill(keeper.GetAllService(ctx)),
nullify.Fill(keeper.GetAllServices(ctx)),
)
}
2 changes: 1 addition & 1 deletion x/service/module/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ExportGenesis(ctx context.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)

genesis.ServiceList = k.GetAllService(ctx)
genesis.ServiceList = k.GetAllServices(ctx)
// this line is used by starport scaffolding # genesis/module/export

return genesis
Expand Down
1 change: 1 addition & 0 deletions x/service/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type BankKeeper interface {
// than "delegate" funds from one account to another which is more closely
// linked to staking.
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
}
7 changes: 5 additions & 2 deletions x/supplier/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type (
// should be the x/gov module account.
authority string

bankKeeper types.BankKeeper
bankKeeper types.BankKeeper
serviceKeeper types.ServiceKeeper
}
)

Expand All @@ -32,6 +33,7 @@ func NewKeeper(
authority string,

bankKeeper types.BankKeeper,
serviceKeeper types.ServiceKeeper,
) Keeper {
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(fmt.Sprintf("invalid authority address: %s", authority))
Expand All @@ -43,7 +45,8 @@ func NewKeeper(
authority: authority,
logger: logger,

bankKeeper: bankKeeper,
bankKeeper: bankKeeper,
serviceKeeper: serviceKeeper,
}
}

Expand Down
8 changes: 8 additions & 0 deletions x/supplier/keeper/msg_server_stake_supplier.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func (k msgServer) StakeSupplier(ctx context.Context, msg *types.MsgStakeSupplie
return nil, err
}

// Check if the services the supplier is staking for exist
for _, serviceConfig := range msg.Services {
if _, serviceFound := k.serviceKeeper.GetService(ctx, serviceConfig.Service.Id); !serviceFound {
logger.Error(fmt.Sprintf("service %q does not exist", serviceConfig.Service.Id))
return nil, types.ErrSupplierServiceNotFound.Wrapf("service %q does not exist", serviceConfig.Service.Id)
}
}

// Check if the supplier already exists or not
var err error
var coinsToEscrow sdk.Coin
Expand Down
30 changes: 30 additions & 0 deletions x/supplier/keeper/msg_server_stake_supplier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,33 @@ func TestMsgServer_StakeSupplier_FailLoweringStake(t *testing.T) {
require.Equal(t, int64(100), supplierFound.Stake.Amount.Int64())
require.Len(t, supplierFound.Services, 1)
}

func TestMsgServer_StakeSupplier_FailWithNonExistingService(t *testing.T) {
k, ctx := keepertest.SupplierKeeper(t)
srv := keeper.NewMsgServerImpl(k)

// Prepare the supplier
supplierAddr := sample.AccAddress()
stakeMsg := &types.MsgStakeSupplier{
Address: supplierAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Services: []*sharedtypes.SupplierServiceConfig{
{
Service: &sharedtypes.Service{
Id: "newService",
},
Endpoints: []*sharedtypes.SupplierEndpoint{
{
Url: "http://localhost:8080",
RpcType: sharedtypes.RPCType_JSON_RPC,
Configs: make([]*sharedtypes.ConfigOption, 0),
},
},
},
},
}

// Stake the supplier & verify that it fails because the service does not exist.
_, err := srv.StakeSupplier(ctx, stakeMsg)
require.ErrorIs(t, err, types.ErrSupplierServiceNotFound)
}
6 changes: 6 additions & 0 deletions x/supplier/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,22 @@ type AppModule struct {
keeper keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
serviceKeeper types.ServiceKeeper
}

func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
serviceKeeper types.ServiceKeeper,
) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(cdc),
keeper: keeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
serviceKeeper: serviceKeeper,
}
}

Expand Down Expand Up @@ -179,6 +182,7 @@ type ModuleInputs struct {

AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
ServiceKeeper types.ServiceKeeper
}

type ModuleOutputs struct {
Expand All @@ -200,12 +204,14 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.Logger,
authority.String(),
in.BankKeeper,
in.ServiceKeeper,
)
m := NewAppModule(
in.Cdc,
k,
in.AccountKeeper,
in.BankKeeper,
in.ServiceKeeper,
)

return ModuleOutputs{SupplierKeeper: k, Module: m}
Expand Down
1 change: 1 addition & 0 deletions x/supplier/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ var (
ErrSupplierInvalidSessionId = sdkerrors.Register(ModuleName, 1107, "invalid session ID")
ErrSupplierInvalidService = sdkerrors.Register(ModuleName, 1108, "invalid service in supplier")
ErrSupplierInvalidSessionEndHeight = sdkerrors.Register(ModuleName, 1109, "invalid session ending height")
ErrSupplierServiceNotFound = sdkerrors.Register(ModuleName, 1110, "service not found")
)
8 changes: 8 additions & 0 deletions x/supplier/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// AccountKeeper defines the expected interface for the Account module.
Expand All @@ -16,6 +18,12 @@ type AccountKeeper interface {

// BankKeeper defines the expected interface for the Bank module.
type BankKeeper interface {
SpendableCoins(context.Context, sdk.AccAddress) sdk.Coins
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
}

// ServiceKeeper defines the expected interface for the Service module.
type ServiceKeeper interface {
GetService(ctx context.Context, serviceId string) (sharedtypes.Service, bool)
}
Loading

0 comments on commit 91f9cd9

Please sign in to comment.