-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also, remove the `action.committed` field; it is a red herring.
- Loading branch information
1 parent
15ae4c6
commit b115b55
Showing
6 changed files
with
125 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters