From af4c28e1b7e57b09d7680da6f42e87ec5731abba Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Wed, 11 Jul 2018 22:02:07 +0100 Subject: [PATCH] plan out validation --- internal/x/paychan/handler.go | 3 ++- internal/x/paychan/keeper.go | 13 +++++++++++-- internal/x/paychan/types.go | 17 ++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/internal/x/paychan/handler.go b/internal/x/paychan/handler.go index cb89735d..47840412 100644 --- a/internal/x/paychan/handler.go +++ b/internal/x/paychan/handler.go @@ -21,9 +21,9 @@ func NewHandler(k Keeper) sdk.Handler { } } -// TODO does validation go here or in the keeper? // Handle CreateMsg. +// Leaves validation to the keeper methods. func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result { // TODO maybe remove tags for first version tags, err := k.CreatePaychan(msg.sender, msg.receiver, msg.amount) @@ -37,6 +37,7 @@ func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result { } // Handle CloseMsg. +// Leaves validation to the keeper methods. func handleMsgClose(ctx sdk.Context, k Keeper, msg MsgClose) sdk.Result { // TODO maybe remove tags for first version tags, err := k.ClosePaychan(msg.sender, msg.receiver, msg.id, msg.receiverAmount) diff --git a/internal/x/paychan/keeper.go b/internal/x/paychan/keeper.go index 1a22136b..74aa0ddb 100644 --- a/internal/x/paychan/keeper.go +++ b/internal/x/paychan/keeper.go @@ -8,6 +8,8 @@ import ( ) // keeper of the paychan store +// Handles validation internally. Does not rely on calling code to do validation. +// Aim to keep public methids safe, private ones not necessaily. type Keeper struct { storeKey sdk.StoreKey cdc *wire.Codec // needed to serialize objects before putting them in the store @@ -63,7 +65,7 @@ func (keeper Keeper) setPaychan(pych Paychan) sdk.Error { // Create a new payment channel and lock up sender funds. func (keeer Keeper) CreatePaychan(sender sdk.Address, receiver sdkAddress, amt sdk.Coins) (sdk.Tags, sdk.Error) { // Calculate next id (num existing paychans plus 1) - id := len(keeper.GetPaychans(sender, receiver)) + 1 + id := len(keeper.GetPaychans(sender, receiver)) + 1 // TODO check for overflow? // subtract coins from sender k.coinKeeper.SubtractCoins(ctx, sender, amt) // create new Paychan struct (create ID) @@ -76,9 +78,13 @@ func (keeer Keeper) CreatePaychan(sender sdk.Address, receiver sdkAddress, amt s // TODO validation + // coins valid and positive + // sender has enough coins - done in Subtract method // receiver address exists? // paychan doesn't exist already + // sender and receiver different? + tags := sdk.NewTags() return tags, err @@ -99,9 +105,12 @@ func (keeper Keeper) ClosePaychan(sender sdk.Address, receiver sdk.Address, id i // TODO validation + // id ≥ 0 + // coins valid and positive // paychan exists - // output coins are less than paychan balance + // output coins are equal to paychan balance // sender and receiver addresses exist? + // overflow in sender and receiver balances? //sdk.NewTags( // "action", []byte("channel closure"), diff --git a/internal/x/paychan/types.go b/internal/x/paychan/types.go index 3dbb0549..5eb3bf10 100644 --- a/internal/x/paychan/types.go +++ b/internal/x/paychan/types.go @@ -79,13 +79,12 @@ func (msg MsgCreate) GetSignBytes() []byte { func (msg MsgCreate) ValidateBasic() sdk.Error { // TODO implement - // verify msg as much as possible without using external information (such as account balance) - // are all fields present - // are all fields valid - // maybe check if sender and receiver is different + // Validate msg as an optimisation to avoid all validation going to keeper. It's run before the sigs are checked by the auth module. + // Validate without external information (such as account balance) - // maybe add custom errors - // learn how the errors work + // check if all fields present / not 0 valued + // do coin checks for amount + // check if Address valid? // example from bank // if len(in.Address) == 0 { @@ -149,7 +148,11 @@ func (msg MsgClose) GetSignBytes() []byte { func (msg MsgClose) ValidateBasic() sdk.Error { // TODO implement - //return msg.IBCPacket.ValidateBasic() + + // check if all fields present / not 0 valued + // check id ≥ 0 + // do coin checks for amount + // check if Address valid? } func (msg MsgClose) GetSigners() []sdk.Address {