plan out validation

This commit is contained in:
rhuairahrighairigh 2018-07-11 22:02:07 +01:00
parent f146d9ae0c
commit af4c28e1b7
3 changed files with 23 additions and 10 deletions

View File

@ -21,9 +21,9 @@ func NewHandler(k Keeper) sdk.Handler {
} }
} }
// TODO does validation go here or in the keeper?
// Handle CreateMsg. // Handle CreateMsg.
// Leaves validation to the keeper methods.
func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result { func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result {
// TODO maybe remove tags for first version // TODO maybe remove tags for first version
tags, err := k.CreatePaychan(msg.sender, msg.receiver, msg.amount) 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. // Handle CloseMsg.
// Leaves validation to the keeper methods.
func handleMsgClose(ctx sdk.Context, k Keeper, msg MsgClose) sdk.Result { func handleMsgClose(ctx sdk.Context, k Keeper, msg MsgClose) sdk.Result {
// TODO maybe remove tags for first version // TODO maybe remove tags for first version
tags, err := k.ClosePaychan(msg.sender, msg.receiver, msg.id, msg.receiverAmount) tags, err := k.ClosePaychan(msg.sender, msg.receiver, msg.id, msg.receiverAmount)

View File

@ -8,6 +8,8 @@ import (
) )
// keeper of the paychan store // 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 { type Keeper struct {
storeKey sdk.StoreKey storeKey sdk.StoreKey
cdc *wire.Codec // needed to serialize objects before putting them in the store 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. // 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) { func (keeer Keeper) CreatePaychan(sender sdk.Address, receiver sdkAddress, amt sdk.Coins) (sdk.Tags, sdk.Error) {
// Calculate next id (num existing paychans plus 1) // 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 // subtract coins from sender
k.coinKeeper.SubtractCoins(ctx, sender, amt) k.coinKeeper.SubtractCoins(ctx, sender, amt)
// create new Paychan struct (create ID) // create new Paychan struct (create ID)
@ -76,9 +78,13 @@ func (keeer Keeper) CreatePaychan(sender sdk.Address, receiver sdkAddress, amt s
// TODO validation // TODO validation
// coins valid and positive
// sender has enough coins - done in Subtract method // sender has enough coins - done in Subtract method
// receiver address exists? // receiver address exists?
// paychan doesn't exist already // paychan doesn't exist already
// sender and receiver different?
tags := sdk.NewTags() tags := sdk.NewTags()
return tags, err return tags, err
@ -99,9 +105,12 @@ func (keeper Keeper) ClosePaychan(sender sdk.Address, receiver sdk.Address, id i
// TODO validation // TODO validation
// id ≥ 0
// coins valid and positive
// paychan exists // paychan exists
// output coins are less than paychan balance // output coins are equal to paychan balance
// sender and receiver addresses exist? // sender and receiver addresses exist?
// overflow in sender and receiver balances?
//sdk.NewTags( //sdk.NewTags(
// "action", []byte("channel closure"), // "action", []byte("channel closure"),

View File

@ -79,13 +79,12 @@ func (msg MsgCreate) GetSignBytes() []byte {
func (msg MsgCreate) ValidateBasic() sdk.Error { func (msg MsgCreate) ValidateBasic() sdk.Error {
// TODO implement // TODO implement
// verify msg as much as possible without using external information (such as account balance) // Validate msg as an optimisation to avoid all validation going to keeper. It's run before the sigs are checked by the auth module.
// are all fields present // Validate without external information (such as account balance)
// are all fields valid
// maybe check if sender and receiver is different
// maybe add custom errors // check if all fields present / not 0 valued
// learn how the errors work // do coin checks for amount
// check if Address valid?
// example from bank // example from bank
// if len(in.Address) == 0 { // if len(in.Address) == 0 {
@ -149,7 +148,11 @@ func (msg MsgClose) GetSignBytes() []byte {
func (msg MsgClose) ValidateBasic() sdk.Error { func (msg MsgClose) ValidateBasic() sdk.Error {
// TODO implement // 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 { func (msg MsgClose) GetSigners() []sdk.Address {