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.
// 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)

View File

@ -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"),

View File

@ -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 {