Skip to content

Commit

Permalink
fix: add END_BLOCK controller call
Browse files Browse the repository at this point in the history
Also, remove the `action.committed` field; it is a red herring.
  • Loading branch information
michaelfig committed Mar 8, 2020
1 parent 15ae4c6 commit b115b55
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func NewSwingSetApp(
)

app.mm.SetOrderBeginBlockers(distr.ModuleName, slashing.ModuleName, staking.ModuleName, swingset.ModuleName)
app.mm.SetOrderEndBlockers(staking.ModuleName)
app.mm.SetOrderEndBlockers(swingset.ModuleName, staking.ModuleName)

// Sets the order of Genesis - Order matters, genutil is to always come last
// NOTE: The genutil module must occur after staking so that pools are
Expand Down
72 changes: 72 additions & 0 deletions packages/cosmic-swingset/x/swingset/blockers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package swingset

import (
"encoding/json"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
)

type beginBlockAction struct {
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}

type endBlockAction struct {
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}

func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, keeper Keeper) error {
// Create a "storagePort" that the controller can use to communicate with the
// storageHandler
storagePort := RegisterPortHandler(NewUnlimitedStorageHandler(ctx, keeper))
defer UnregisterPortHandler(storagePort)

action := &beginBlockAction{
Type: "BEGIN_BLOCK",
StoragePort: storagePort,
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
}
b, err := json.Marshal(action)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

_, err = keeper.CallToController(string(b))

// fmt.Fprintln(os.Stderr, "Returned from SwingSet", out, err)
return err
}

func EndBlock(ctx sdk.Context, req abci.RequestEndBlock, keeper Keeper) ([]abci.ValidatorUpdate, error) {
// Create a "storagePort" that the controller can use to communicate with the
// storageHandler
storagePort := RegisterPortHandler(NewUnlimitedStorageHandler(ctx, keeper))
defer UnregisterPortHandler(storagePort)

action := &endBlockAction{
Type: "END_BLOCK",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
StoragePort: storagePort,
}
b, err := json.Marshal(action)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

_, err = keeper.CallToController(string(b))

// fmt.Fprintln(os.Stderr, "Returned from SwingSet", out, err)
if err != nil {
return nil, err
}
return []abci.ValidatorUpdate{}, nil
}
31 changes: 31 additions & 0 deletions packages/cosmic-swingset/x/swingset/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package swingset

import (
"errors"
"fmt"
)

type PortHandler interface {
Receive(string) (string, error)
}

var portToHandler = map[int]PortHandler{}
var lastPort = 0

func RegisterPortHandler(portHandler PortHandler) int {
lastPort++
portToHandler[lastPort] = portHandler
return lastPort
}
func UnregisterPortHandler(portNum int) error {
delete(portToHandler, portNum)
return nil
}

func ReceiveFromController(portNum int, msg string) (string, error) {
handler := portToHandler[portNum]
if handler == nil {
return "", errors.New(fmt.Sprintf("Unregistered port %d", portNum))
}
return handler.Receive(msg)
}
84 changes: 6 additions & 78 deletions packages/cosmic-swingset/x/swingset/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"

// "github.com/Agoric/cosmic-swingset/x/swingset/internal/types"
Expand All @@ -21,15 +20,6 @@ type deliverInboundAction struct {
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Committed bool `json:"committed"`
}

type beginBlockAction struct {
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Committed bool `json:"committed"`
}

// NewHandler returns a handler for "swingset" type messages.
Expand All @@ -45,35 +35,6 @@ func NewHandler(keeper Keeper) sdk.Handler {
}
}

func BeginBlock(ctx sdk.Context, keeper Keeper) {
handleMsgBeginBlock(ctx, keeper)
}

type PortHandler interface {
Receive(string) (string, error)
}

var portToHandler = map[int]PortHandler{}
var lastPort = 0

func RegisterPortHandler(portHandler PortHandler) int {
lastPort++
portToHandler[lastPort] = portHandler
return lastPort
}
func UnregisterPortHandler(portNum int) error {
delete(portToHandler, portNum)
return nil
}

func ReceiveFromController(portNum int, msg string) (string, error) {
handler := portToHandler[portNum]
if handler == nil {
return "", errors.New("Unregistered port " + fmt.Sprintf("%d", portNum))
}
return handler.Receive(msg)
}

func mailboxPeer(key string) (string, error) {
path := strings.Split(key, ".")
if len(path) != 2 || path[0] != "mailbox" {
Expand All @@ -90,61 +51,28 @@ func handleMsgDeliverInbound(ctx sdk.Context, keeper Keeper, msg MsgDeliverInbou
messages[i][1] = message
}

storageHandler := NewStorageHandler(ctx, keeper)
// Allow the storageHandler to consume unlimited gas.
storageHandler.Context = storageHandler.Context.WithGasMeter(sdk.NewInfiniteGasMeter())
// Create a "storagePort" that the controller can use to communicate with the
// storageHandler
storagePort := RegisterPortHandler(NewUnlimitedStorageHandler(ctx, keeper))
defer UnregisterPortHandler(storagePort)

newPort := RegisterPortHandler(storageHandler)
action := &deliverInboundAction{
Type: "DELIVER_INBOUND",
Peer: msg.Peer,
Messages: messages,
Ack: msg.Ack,
StoragePort: newPort,
StoragePort: storagePort,
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
Committed: !ctx.IsCheckTx(),
}
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)
b, err := json.Marshal(action)
if err != nil {
return nil, err
}

_, err = keeper.CallToController(string(b))
// fmt.Fprintln(os.Stderr, "Returned from SwingSet", out, err)
UnregisterPortHandler(newPort)
if err != nil {
return nil, err
}
return &sdk.Result{}, nil
}

func handleMsgBeginBlock(ctx sdk.Context, keeper Keeper) (*sdk.Result, error) {
storageHandler := NewStorageHandler(ctx, keeper)

// Allow the storageHandler to consume unlimited gas.
storageHandler.Context = storageHandler.Context.WithGasMeter(sdk.NewInfiniteGasMeter())

newPort := RegisterPortHandler(storageHandler)

action := &beginBlockAction{
Type: "BEGIN_BLOCK",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
StoragePort: newPort,
Committed: !ctx.IsCheckTx(),
}
b, err := json.Marshal(action)
if err != nil {
fmt.Fprintln(os.Stderr, "Error marshalling", err)
return nil, err
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}

_, err = keeper.CallToController(string(b))

// fmt.Fprintln(os.Stderr, "Returned from SwingSet", out, err)
UnregisterPortHandler(newPort)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions packages/cosmic-swingset/x/swingset/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
}

func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
BeginBlock(ctx, am.keeper)
// TODO: error handling?
BeginBlock(ctx, req, am.keeper)
}

func (am AppModule) EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
// TODO: error handling?
valUpdate, _ := EndBlock(ctx, req, am.keeper)
return valUpdate
}

func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
Expand Down
9 changes: 9 additions & 0 deletions packages/cosmic-swingset/x/swingset/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func NewStorageHandler(context sdk.Context, keeper Keeper) *storageHandler {
}
}

func NewUnlimitedStorageHandler(context sdk.Context, keeper Keeper) *storageHandler {
storageHandler := NewStorageHandler(context, keeper)

// Allow the storageHandler to consume unlimited gas.
storageHandler.Context = storageHandler.Context.WithGasMeter(sdk.NewInfiniteGasMeter())

return storageHandler
}

func (sh *storageHandler) Receive(str string) (ret string, err error) {
msg := new(storageMessage)
err = json.Unmarshal([]byte(str), &msg)
Expand Down

0 comments on commit b115b55

Please sign in to comment.