Skip to content

Commit

Permalink
[Tokenomics] SettleSessionAccounting first implementation (emissions=…
Browse files Browse the repository at this point in the history
…burn) (#323)

Implement `SettleSessionAccounting` where emissions=burn.

- Issue: #243 

---

Co-authored-by: Redouane Lakrache <[email protected]>
Co-authored-by: Dima Kniazev <[email protected]>
Co-authored-by: Bryan White <[email protected]>
  • Loading branch information
4 people authored Feb 2, 2024
1 parent 254225d commit 66e3320
Show file tree
Hide file tree
Showing 23 changed files with 674 additions and 55 deletions.
7 changes: 5 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"path/filepath"

// this line is used by starport scaffolding # stargate/app/moduleImport

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
dbm "github.com/cometbft/cometbft-db"
Expand Down Expand Up @@ -108,8 +110,6 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
"github.com/spf13/cast"

appparams "github.com/pokt-network/poktroll/app/params"
"github.com/pokt-network/poktroll/docs"
applicationmodule "github.com/pokt-network/poktroll/x/application"
Expand All @@ -133,6 +133,7 @@ import (
tokenomicsmodule "github.com/pokt-network/poktroll/x/tokenomics"
tokenomicsmodulekeeper "github.com/pokt-network/poktroll/x/tokenomics/keeper"
tokenomicsmoduletypes "github.com/pokt-network/poktroll/x/tokenomics/types"
"github.com/spf13/cast"
)

const (
Expand Down Expand Up @@ -660,6 +661,8 @@ func New(
keys[tokenomicsmoduletypes.MemStoreKey],
app.GetSubspace(tokenomicsmoduletypes.ModuleName),
app.BankKeeper,
app.ApplicationKeeper,
app.SupplierKeeper,
authority,
)
tokenomicsModule := tokenomicsmodule.NewAppModule(appCodec, app.TokenomicsKeeper, app.AccountKeeper, app.BankKeeper)
Expand Down
5 changes: 3 additions & 2 deletions e2e/tests/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ import (

tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/app"
"github.com/pokt-network/poktroll/testutil/testclient"
apptypes "github.com/pokt-network/poktroll/x/application/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/session.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Feature: Session Namespace
Scenario: Supplier completes claim/proof lifecycle for a valid session
Given the user has the pocketd binary installed
When the supplier "supplier1" has serviced a session with "5" relays for service "svc1" for application "app1"
And after the supplier creates a claim for the session for service "svc1" for application "app1"
And the user should wait for "5" seconds
Then the claim created by supplier "supplier1" for service "svc1" for application "app1" should be persisted on-chain
# TODO_IMPROVE: ...
# And an event should be emitted...
Expand Down
13 changes: 13 additions & 0 deletions e2e/tests/tokenomics.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Tokenomics Namespaces

# This test
Scenario: Basic tokenomics validation that Supplier mint equals Application burn
Given the user has the pocketd binary installed
And an account exists for "supplier1"
And an account exists for "app1"
When the supplier "supplier1" has serviced a session with "20" relays for service "svc1" for application "app1"
And the user should wait for "5" seconds
# TODO_UPNEXT(@Olshansk, #359): Expand on the two expectations below after integrating the tokenomics module
# into the supplier module.
# Then the account balance of "supplier1" should be "1000" uPOKT "more" than before
# And the account balance of "app1" should be "1000" uPOKT "less" than before
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/athanorlabs/go-dleq v0.1.0
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/ibc-go/v7 v7.3.1
Expand All @@ -51,7 +50,6 @@ require (
golang.org/x/crypto v0.15.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/sync v0.5.0
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb
google.golang.org/grpc v1.59.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -98,6 +96,7 @@ require (
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
Expand Down Expand Up @@ -294,6 +293,7 @@ require (
google.golang.org/api v0.143.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/sdk/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
sdkerrors "cosmossdk.io/errors"
)

// TODO_TECHDEBT: Do a source code wise find-replace using regex pattern match
// of `sdkerrors\.Wrapf\(([a-zA-Z]+), ` with `$1.Wrapf(`
var (
codespace = "poktrollsdk"
ErrSDKHandleRelay = sdkerrors.Register(codespace, 1, "internal error handling relay request")
Expand Down
66 changes: 58 additions & 8 deletions testutil/keeper/tokenomics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,74 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/sample"
mocks "github.com/pokt-network/poktroll/testutil/tokenomics/mocks"
apptypes "github.com/pokt-network/poktroll/x/application/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
"github.com/pokt-network/poktroll/x/tokenomics/keeper"
"github.com/pokt-network/poktroll/x/tokenomics/types"
)

func TokenomicsKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
// TODO_TECHDEBT: Replace `AnyTimes` w/ `Times/MinTimes/MaxTimes` as the tests
// mature to be explicit about the number of expected tests.

func TokenomicsKeeper(t testing.TB) (
k *keeper.Keeper, s sdk.Context,
appAddr, supplierAddr string,
) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

// Initialize the in-memory tendermint database
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

// Initialize the codec and other necessary components
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
ctrl := gomock.NewController(t)

// The on-chain governance address
authority := authtypes.NewModuleAddress("gov").String()

ctrl := gomock.NewController(t)
// Prepare the test application
application := apptypes.Application{
Address: sample.AccAddress(),
Stake: &sdk.Coin{Denom: "upokt", Amount: sdk.NewInt(100000)},
}

// Prepare the test supplier
supplier := sharedtypes.Supplier{
Address: sample.AccAddress(),
Stake: &sdk.Coin{Denom: "upokt", Amount: sdk.NewInt(100000)},
}

// Mock the application keeper
mockApplicationKeeper := mocks.NewMockApplicationKeeper(ctrl)
mockApplicationKeeper.EXPECT().
GetApplication(gomock.Any(), gomock.Eq(application.Address)).
Return(application, true).
AnyTimes()
mockApplicationKeeper.EXPECT().
GetApplication(gomock.Any(), gomock.Not(application.Address)).
Return(apptypes.Application{}, false).
AnyTimes()
mockApplicationKeeper.EXPECT().
SetApplication(gomock.Any(), gomock.Any()).
AnyTimes()

// Mock the supplier keeper
mockSupplierKeeper := mocks.NewMockSupplierKeeper(ctrl)
mockSupplierKeeper.EXPECT().
GetSupplier(gomock.Any(), supplier.Address).
Return(supplier, true).
AnyTimes()

// Mock the bank keeper
mockBankKeeper := mocks.NewMockBankKeeper(ctrl)
mockBankKeeper.EXPECT().
MintCoins(gomock.Any(), gomock.Any(), gomock.Any()).
Expand All @@ -45,36 +92,39 @@ func TokenomicsKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
BurnCoins(gomock.Any(), gomock.Any(), gomock.Any()).
AnyTimes()
mockBankKeeper.EXPECT().
SendCoinsFromModuleToAccount(gomock.Any(), gomock.Any(), types.ModuleName, gomock.Any()).
SendCoinsFromModuleToAccount(gomock.Any(), suppliertypes.ModuleName, gomock.Any(), gomock.Any()).
AnyTimes()
mockBankKeeper.EXPECT().
SendCoinsFromModuleToModule(gomock.Any(), types.ModuleName, gomock.Any(), gomock.Any()).
SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), apptypes.ModuleName, gomock.Any()).
AnyTimes()
mockBankKeeper.EXPECT().
SendCoinsFromAccountToModule(gomock.Any(), types.ModuleName, gomock.Any(), gomock.Any()).
UndelegateCoinsFromModuleToAccount(gomock.Any(), apptypes.ModuleName, gomock.Any(), gomock.Any()).
AnyTimes()

// Initialize the tokenomics keeper
paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
memStoreKey,
"TokenomicsParams",
)
k := keeper.NewKeeper(
tokenomicsKeeper := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
paramsSubspace,

mockBankKeeper,
mockApplicationKeeper,
mockSupplierKeeper,

authority,
)

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

// Initialize params
k.SetParams(ctx, types.DefaultParams())
tokenomicsKeeper.SetParams(ctx, types.DefaultParams())

return k, ctx
return tokenomicsKeeper, ctx, application.Address, supplier.Address
}
1 change: 1 addition & 0 deletions x/session/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ var (
ErrSessionInvalidAppAddress = sdkerrors.Register(ModuleName, 5, "invalid application address for session")
ErrSessionInvalidService = sdkerrors.Register(ModuleName, 6, "invalid service in session")
ErrSessionInvalidBlockHeight = sdkerrors.Register(ModuleName, 7, "invalid block height for session")
ErrSessionInvalidSessionId = sdkerrors.Register(ModuleName, 8, "invalid sessionId")
)
7 changes: 3 additions & 4 deletions x/session/types/query_get_session_request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

sharedhelpers "github.com/pokt-network/poktroll/x/shared/helpers"
Expand All @@ -24,17 +23,17 @@ func NewQueryGetSessionRequest(appAddress, serviceId string, blockHeight int64)
func (query *QueryGetSessionRequest) ValidateBasic() error {
// Validate the application address
if _, err := sdk.AccAddressFromBech32(query.ApplicationAddress); err != nil {
return sdkerrors.Wrapf(ErrSessionInvalidAppAddress, "invalid app address for session being retrieved %s; (%v)", query.ApplicationAddress, err)
return ErrSessionInvalidAppAddress.Wrapf("invalid app address for session being retrieved %s; (%v)", query.ApplicationAddress, err)
}

// Validate the Service ID
if !sharedhelpers.IsValidService(query.Service) {
return sdkerrors.Wrapf(ErrSessionInvalidService, "invalid service for session being retrieved %s;", query.Service)
return ErrSessionInvalidService.Wrapf("invalid service for session being retrieved %s;", query.Service)
}

// Validate the height for which a session is being retrieved
if query.BlockHeight < 0 { // Note that `0` defaults to the latest height rather than genesis
return sdkerrors.Wrapf(ErrSessionInvalidBlockHeight, "invalid block height for session being retrieved %d;", query.BlockHeight)
return ErrSessionInvalidBlockHeight.Wrapf("invalid block height for session being retrieved %d;", query.BlockHeight)
}
return nil
}
34 changes: 34 additions & 0 deletions x/session/types/session_header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types

import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TODO_TECHDEBT: Make sure this is used everywhere we validate components
// of the session header.
func (sh *SessionHeader) ValidateBasic() error {
// Validate the application address
if _, err := sdk.AccAddressFromBech32(sh.ApplicationAddress); err != nil {
return sdkerrors.Wrapf(ErrSessionInvalidAppAddress, "invalid application address: %s; (%v)", sh.ApplicationAddress, err)
}

// Validate the session ID
// TODO_TECHDEBT: Introduce a `SessionId#ValidateBasic` method.
if sh.SessionId == "" {
return sdkerrors.Wrapf(ErrSessionInvalidSessionId, "invalid session ID: %s", sh.SessionId)
}

// Validate the service
// TODO_TECHDEBT: Introduce a `Service#ValidateBasic` method.
if sh.Service == nil {
return sdkerrors.Wrapf(ErrSessionInvalidService, "invalid service: %s", sh.Service)
}

// Check if session end height is greater than session start height
if sh.SessionEndBlockHeight <= sh.SessionStartBlockHeight {
return sdkerrors.Wrapf(ErrSessionInvalidBlockHeight, "session end block height cannot be less than or equal to session start block height")
}

return nil
}
86 changes: 86 additions & 0 deletions x/session/types/session_header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package types_test

import (
"testing"

"github.com/stretchr/testify/require"

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

func TestSessionHeader_ValidateBasic(t *testing.T) {
tests := []struct {
desc string
sh types.SessionHeader
err error
}{
{
desc: "invalid - invalid application address",
sh: types.SessionHeader{
ApplicationAddress: "invalid_address",
SessionId: "valid_session_id",
Service: &sharedtypes.Service{},
SessionStartBlockHeight: 100,
SessionEndBlockHeight: 101,
},
err: types.ErrSessionInvalidAppAddress,
},
{
desc: "invalid - empty session id",
sh: types.SessionHeader{
ApplicationAddress: sample.AccAddress(),
SessionId: "",
Service: &sharedtypes.Service{},
SessionStartBlockHeight: 100,
SessionEndBlockHeight: 101,
},
err: types.ErrSessionInvalidSessionId,
},
{
desc: "invalid - nil service",
sh: types.SessionHeader{
ApplicationAddress: sample.AccAddress(),
SessionId: "valid_session_id",
Service: nil,
SessionStartBlockHeight: 100,
SessionEndBlockHeight: 101,
},
err: types.ErrSessionInvalidService,
},
{
desc: "invalid - start block height greater than end block height",
sh: types.SessionHeader{
ApplicationAddress: sample.AccAddress(),
SessionId: "valid_session_id",
Service: &sharedtypes.Service{},
SessionStartBlockHeight: 100,
SessionEndBlockHeight: 99,
},
err: types.ErrSessionInvalidBlockHeight,
},
{
desc: "valid",
sh: types.SessionHeader{
ApplicationAddress: sample.AccAddress(),
SessionId: "valid_session_id",
Service: &sharedtypes.Service{},
SessionStartBlockHeight: 100,
SessionEndBlockHeight: 101,
},
err: nil,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
err := tt.sh.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
} else {
require.NoError(t, err)
}
})
}
}
Loading

0 comments on commit 66e3320

Please sign in to comment.