Skip to content

Commit

Permalink
WIP: Add staking runtime messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Dec 2, 2020
1 parent 0c259c7 commit d4a6617
Show file tree
Hide file tree
Showing 19 changed files with 433 additions and 43 deletions.
9 changes: 7 additions & 2 deletions go/consensus/tendermint/apps/roothash/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ package api

type messageKind uint8

// RuntimeMessageNoop is the message kind used when dispatching Noop runtime messages.
var RuntimeMessageNoop = messageKind(0)
var (
// RuntimeMessageNoop is the message kind used when dispatching Noop runtime messages.
RuntimeMessageNoop = messageKind(0)

// RuntimeMessageStaking is the message kind used when dispatching Staking runtime messages.
RuntimeMessageStaking = messageKind(1)
)
10 changes: 9 additions & 1 deletion go/consensus/tendermint/apps/roothash/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@ import (
roothashApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/roothash/api"
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

func (app *rootHashApplication) processRuntimeMessages(
ctx *tmapi.Context,
rtState *roothash.RuntimeState,
msgs []block.Message,
) error {
// Prepare a new context for processing messages.
msgCtx := ctx.WithCallerAddress(staking.NewRuntimeAddress(rtState.Runtime.ID))
msgCtx.SetGasAccountant(tmapi.NewNopGasAccountant()) // Gas was already accounted for.
defer msgCtx.Close()

for i, msg := range msgs {
var err error
switch {
case msg.Noop != nil:
err = app.md.Publish(ctx, roothashApi.RuntimeMessageNoop, &msg.Noop)
err = app.md.Publish(msgCtx, roothashApi.RuntimeMessageNoop, msg.Noop)
case msg.Staking != nil:
err = app.md.Publish(msgCtx, roothashApi.RuntimeMessageStaking, msg.Staking)
default:
// Unsupported message.
err = roothash.ErrInvalidArgument
Expand Down
6 changes: 6 additions & 0 deletions go/consensus/tendermint/apps/roothash/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ func (app *rootHashApplication) executorCommit(
return err
}

/*
msgGasAccountant := func(msg *block.Message) {
// TODO: Charging for gas requires each message to know how to charge for gas?
}
*/

for _, commit := range cc.Commits {
if err = rtState.ExecutorPool.AddExecutorCommitment(ctx, rtState.CurrentBlock, sv, nl, &commit); err != nil { // nolint: gosec
ctx.Logger().Error("failed to add compute commitment to round",
Expand Down
22 changes: 21 additions & 1 deletion go/consensus/tendermint/apps/staking/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
roothashApi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/roothash/api"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

Expand Down Expand Up @@ -44,6 +46,9 @@ func (app *stakingApplication) Dependencies() []string {

func (app *stakingApplication) OnRegister(state api.ApplicationState, md api.MessageDispatcher) {
app.state = state

// Subscribe to messages emitted by other apps.
md.Subscribe(roothashApi.RuntimeMessageStaking, app)
}

func (app *stakingApplication) OnCleanup() {
Expand Down Expand Up @@ -99,7 +104,22 @@ func (app *stakingApplication) BeginBlock(ctx *api.Context, request types.Reques
}

func (app *stakingApplication) ExecuteMessage(ctx *api.Context, kind, msg interface{}) error {
return staking.ErrInvalidArgument
state := stakingState.NewMutableState(ctx.State())

switch kind {
case roothashApi.RuntimeMessageStaking:
m := msg.(*block.StakingMessage)
switch {
case m.Transfer != nil:
return app.transfer(ctx, state, m.Transfer)
case m.Withdraw != nil:
return app.withdraw(ctx, state, m.Withdraw)
default:
return staking.ErrInvalidArgument
}
default:
return staking.ErrInvalidArgument
}
}

func (app *stakingApplication) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
Expand Down
14 changes: 7 additions & 7 deletions go/consensus/tendermint/apps/staking/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (app *stakingApplication) transfer(ctx *api.Context, state *stakingState.Mu
return err
}

fromAddr := staking.NewAddress(ctx.TxSigner())
fromAddr := ctx.CallerAddress()
if fromAddr.IsReserved() || !isTransferPermitted(params, fromAddr) {
return staking.ErrForbidden
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (app *stakingApplication) burn(ctx *api.Context, state *stakingState.Mutabl
return err
}

fromAddr := staking.NewAddress(ctx.TxSigner())
fromAddr := ctx.CallerAddress()
if fromAddr.IsReserved() {
return staking.ErrForbidden
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func (app *stakingApplication) addEscrow(ctx *api.Context, state *stakingState.M
return staking.ErrInvalidArgument
}

fromAddr := staking.NewAddress(ctx.TxSigner())
fromAddr := ctx.CallerAddress()
if fromAddr.IsReserved() {
return staking.ErrForbidden
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func (app *stakingApplication) reclaimEscrow(ctx *api.Context, state *stakingSta
return err
}

toAddr := staking.NewAddress(ctx.TxSigner())
toAddr := ctx.CallerAddress()
if toAddr.IsReserved() {
return staking.ErrForbidden
}
Expand Down Expand Up @@ -396,7 +396,7 @@ func (app *stakingApplication) amendCommissionSchedule(
return err
}

fromAddr := staking.NewAddress(ctx.TxSigner())
fromAddr := ctx.CallerAddress()
if fromAddr.IsReserved() {
return staking.ErrForbidden
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func (app *stakingApplication) allow(
}

// Validate addresses -- if either is reserved or both are equal, the method should fail.
addr := staking.NewAddress(ctx.TxSigner())
addr := ctx.CallerAddress()
if addr.IsReserved() || allow.Beneficiary.IsReserved() {
return staking.ErrForbidden
}
Expand Down Expand Up @@ -529,7 +529,7 @@ func (app *stakingApplication) withdraw(
}

// Validate addresses -- if either is reserved or both are equal, the method should fail.
toAddr := staking.NewAddress(ctx.TxSigner())
toAddr := ctx.CallerAddress()
if toAddr.IsReserved() || withdraw.From.IsReserved() {
return staking.ErrForbidden
}
Expand Down
Loading

0 comments on commit d4a6617

Please sign in to comment.