Skip to content

Commit

Permalink
Merge pull request #1691 from mesg-foundation/feature/default-exec-price
Browse files Browse the repository at this point in the history
Add min price for execution
  • Loading branch information
Nicolas Mahé authored Mar 2, 2020
2 parents 4d4b806 + 58e3cf9 commit 593c4cd
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 46 deletions.
12 changes: 11 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func NewInitApp(
app.subspaces[staking.ModuleName] = app.paramsKeeper.Subspace(staking.DefaultParamspace)
app.subspaces[distr.ModuleName] = app.paramsKeeper.Subspace(distr.DefaultParamspace)
app.subspaces[slashing.ModuleName] = app.paramsKeeper.Subspace(slashing.DefaultParamspace)
app.subspaces[execution.ModuleName] = app.paramsKeeper.Subspace(execution.DefaultParamspace)

// The AccountKeeper handles address -> account lookups
app.accountKeeper = auth.NewAccountKeeper(
Expand Down Expand Up @@ -246,7 +247,16 @@ func NewInitApp(
app.processKeeper = process.NewKeeper(app.cdc, keys[process.StoreKey], app.instanceKeeper, app.ownershipKeeper)
app.serviceKeeper = service.NewKeeper(app.cdc, keys[service.StoreKey], app.ownershipKeeper)
app.runnerKeeper = runner.NewKeeper(app.cdc, keys[runner.StoreKey], app.instanceKeeper)
app.executionKeeper = execution.NewKeeper(app.cdc, keys[execution.StoreKey], app.bankKeeper, app.serviceKeeper, app.instanceKeeper, app.runnerKeeper, app.processKeeper)
app.executionKeeper = execution.NewKeeper(
app.cdc,
keys[execution.StoreKey],
app.bankKeeper,
app.serviceKeeper,
app.instanceKeeper,
app.runnerKeeper,
app.processKeeper,
app.subspaces[execution.ModuleName],
)

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Config struct {

IpfsEndpoint string `validate:"required"`

DefaultExecutionPrice string `validate:"required"`

Server struct {
Address string `validate:"required"`
}
Expand Down Expand Up @@ -95,6 +97,8 @@ func defaultConfig() (*Config, error) {

c.IpfsEndpoint = "http://ipfs.app.mesg.com:8080/ipfs/"

c.DefaultExecutionPrice = "10000atto" // /x/execution/internal/type/params.go#DefaultMinPrice

c.Server.Address = ":50052"
c.Log.Format = "text"
c.Log.Level = "info"
Expand Down
4 changes: 2 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func main() {
}

// init gRPC server.
server := grpc.New(mc, ep, b)
server := grpc.New(mc, ep, b, cfg.DefaultExecutionPrice)
logrus.WithField("module", "main").Infof("starting MESG Engine version %s", version.Version)
defer func() {
logrus.WithField("module", "main").Info("stopping grpc server")
Expand All @@ -220,7 +220,7 @@ func main() {
}()

logrus.WithField("module", "main").Info("starting process engine")
orch := orchestrator.New(mc, ep)
orch := orchestrator.New(mc, ep, cfg.DefaultExecutionPrice)
defer func() {
logrus.WithField("module", "main").Info("stopping orchestrator")
orch.Stop()
Expand Down
20 changes: 13 additions & 7 deletions e2e/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,27 @@ func testExecution(t *testing.T) {
TaskKey: "task1",
EventHash: hash.Int(1),
ExecutorHash: executorHash,
Price: "50atto",
Price: "50000atto",
Inputs: inputs,
})
require.NoError(t, err)

execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash))
executorAddress := sdk.AccAddress(crypto.AddressHash(executorHash))
serviceAddress := sdk.AccAddress(crypto.AddressHash(testServiceHash))

// check balance of execution before completed
t.Run("execution balance before completed", func(t *testing.T) {
coins := sdk.Coins{}
execAddress := sdk.AccAddress(crypto.AddressHash(resp.Hash))
lcdGet(t, "bank/balances/"+execAddress.String(), &coins)
require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(50)))
require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(50000)))
})

var executorBalance sdk.Coins
var serviceBalance sdk.Coins
lcdGet(t, "bank/balances/"+executorAddress.String(), &executorBalance)
lcdGet(t, "bank/balances/"+serviceAddress.String(), &serviceBalance)

_, err = streamInProgress.Recv()
require.NoError(t, err)

Expand All @@ -230,16 +238,14 @@ func testExecution(t *testing.T) {
// check balance of executor
t.Run("executor balance", func(t *testing.T) {
coins := sdk.Coins{}
executorAddress := sdk.AccAddress(crypto.AddressHash(executorHash))
lcdGet(t, "bank/balances/"+executorAddress.String(), &coins)
require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45)))
require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(45000).Add(executorBalance.AmountOf("atto"))))
})
// check balance of service
t.Run("service balance", func(t *testing.T) {
coins := sdk.Coins{}
serviceAddress := sdk.AccAddress(crypto.AddressHash(testServiceHash))
lcdGet(t, "bank/balances/"+serviceAddress.String(), &coins)
require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5)))
require.True(t, coins.AmountOf("atto").Equal(sdk.NewInt(5000).Add(serviceBalance.AmountOf("atto"))))
})
// check balance of execution
t.Run("execution balance", func(t *testing.T) {
Expand Down
12 changes: 7 additions & 5 deletions orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
)

// New creates a new Process instance
func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher) *Orchestrator {
func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, execPrice string) *Orchestrator {
return &Orchestrator{
mc: mc,
ep: ep,
ErrC: make(chan error),
stopC: make(chan bool),
mc: mc,
ep: ep,
ErrC: make(chan error),
stopC: make(chan bool),
execPrice: execPrice,
}
}

Expand Down Expand Up @@ -273,6 +274,7 @@ func (s *Orchestrator) processTask(nodeKey string, task *process.Process_Node_Ta
TaskKey: task.TaskKey,
Inputs: data,
ExecutorHash: executor.Hash,
Price: s.execPrice,
Tags: nil,
})
return err
Expand Down
2 changes: 2 additions & 0 deletions orchestrator/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ type Orchestrator struct {

ErrC chan error
stopC chan bool

execPrice string
}
10 changes: 7 additions & 3 deletions server/grpc/api/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import (

// ExecutionServer serve execution functions.
type ExecutionServer struct {
mc *cosmos.ModuleClient
mc *cosmos.ModuleClient
execPrice string
}

// NewExecutionServer creates a new ExecutionServer.
func NewExecutionServer(mc *cosmos.ModuleClient) *ExecutionServer {
return &ExecutionServer{mc: mc}
func NewExecutionServer(mc *cosmos.ModuleClient, execPrice string) *ExecutionServer {
return &ExecutionServer{mc: mc, execPrice: execPrice}
}

// Create creates an execution.
func (s *ExecutionServer) Create(ctx context.Context, req *api.CreateExecutionRequest) (*api.CreateExecutionResponse, error) {
if req.Price == "" {
req.Price = s.execPrice
}
exec, err := s.mc.CreateExecution(req)
if err != nil {
return nil, err
Expand Down
20 changes: 11 additions & 9 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ import (

// Server contains the server config.
type Server struct {
instance *grpc.Server
mc *cosmos.ModuleClient
ep *publisher.EventPublisher
b *builder.Builder
instance *grpc.Server
mc *cosmos.ModuleClient
ep *publisher.EventPublisher
b *builder.Builder
execPrice string
}

// New returns a new gRPC server.
func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, b *builder.Builder) *Server {
func New(mc *cosmos.ModuleClient, ep *publisher.EventPublisher, b *builder.Builder, execPrice string) *Server {
return &Server{
mc: mc,
ep: ep,
b: b,
mc: mc,
ep: ep,
b: b,
execPrice: execPrice,
}
}

Expand Down Expand Up @@ -82,7 +84,7 @@ func validateInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryS
// register all server
func (s *Server) register() {
protobuf_api.RegisterEventServer(s.instance, api.NewEventServer(s.ep))
protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.mc))
protobuf_api.RegisterExecutionServer(s.instance, api.NewExecutionServer(s.mc, s.execPrice))
protobuf_api.RegisterInstanceServer(s.instance, api.NewInstanceServer(s.mc))
protobuf_api.RegisterServiceServer(s.instance, api.NewServiceServer(s.mc))
protobuf_api.RegisterProcessServer(s.instance, api.NewProcessServer(s.mc))
Expand Down
2 changes: 1 addition & 1 deletion server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestServerServe(t *testing.T) {
s := New(nil, nil, nil)
s := New(nil, nil, nil, "10000atto")
go func() {
time.Sleep(500 * time.Millisecond)
s.Close()
Expand Down
4 changes: 3 additions & 1 deletion x/execution/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) []abci.ValidatorUpdate {
k.SetParams(ctx, data.Params)
return []abci.ValidatorUpdate{}
}

// ExportGenesis writes the current store values
// to a genesis file, which can be imported again
// with InitGenesis
func ExportGenesis(ctx sdk.Context, k Keeper) (data types.GenesisState) {
return types.NewGenesisState()
params := k.GetParams(ctx)
return types.NewGenesisState(params)
}
14 changes: 13 additions & 1 deletion x/execution/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/params"
executionpb "github.com/mesg-foundation/engine/execution"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/protobuf/api"
Expand All @@ -30,10 +31,11 @@ type Keeper struct {
instanceKeeper types.InstanceKeeper
runnerKeeper types.RunnerKeeper
processKeeper types.ProcessKeeper
paramstore params.Subspace
}

// NewKeeper creates a execution keeper
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper) Keeper {
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper, serviceKeeper types.ServiceKeeper, instanceKeeper types.InstanceKeeper, runnerKeeper types.RunnerKeeper, processKeeper types.ProcessKeeper, paramstore params.Subspace) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
Expand All @@ -42,6 +44,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, bankKeeper types.BankKeeper,
instanceKeeper: instanceKeeper,
runnerKeeper: runnerKeeper,
processKeeper: processKeeper,
paramstore: paramstore.WithKeyTable(types.ParamKeyTable()),
}
}

Expand All @@ -56,6 +59,15 @@ func (k *Keeper) Create(ctx sdk.Context, msg types.MsgCreateExecution) (*executi
if err != nil {
return nil, err
}

minPriceCoin, err := sdk.ParseCoins(k.MinPrice(ctx))
if err != nil {
return nil, err
}
if !price.IsAllGTE(minPriceCoin) {
return nil, fmt.Errorf("execution price too low. Min value: %q", minPriceCoin.String())
}

run, err := k.runnerKeeper.Get(ctx, msg.Request.ExecutorHash)
if err != nil {
return nil, err
Expand Down
30 changes: 30 additions & 0 deletions x/execution/internal/keeper/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mesg-foundation/engine/x/execution/internal/types"
)

// Default parameter namespace
const (
DefaultParamspace = types.ModuleName
)

// MinPrice - Minimum price of an execution
func (k Keeper) MinPrice(ctx sdk.Context) string {
var coins string
k.paramstore.Get(ctx, types.KeyMinPrice, &coins)
return coins
}

// SetParams will populate all the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}

// GetParams returns all the params of the module
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
var params types.Params
k.paramstore.GetParamSet(ctx, &params)
return params
}
12 changes: 8 additions & 4 deletions x/execution/internal/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package types

// GenesisState - all instance state that must be provided at genesis
type GenesisState struct{}
type GenesisState struct {
Params Params `json:"params" yaml:"params"`
}

// NewGenesisState creates a new GenesisState object
func NewGenesisState() GenesisState {
return GenesisState{}
func NewGenesisState(params Params) GenesisState {
return GenesisState{Params: params}
}

// DefaultGenesisState - default GenesisState used by Cosmos Hub
func DefaultGenesisState() GenesisState {
return GenesisState{}
return GenesisState{
Params: DefaultParams(),
}
}

// ValidateGenesis validates the instance genesis parameters
Expand Down
Loading

0 comments on commit 593c4cd

Please sign in to comment.