From f146d9ae0c506f71ee7a812dde7b1f012100f112 Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Tue, 10 Jul 2018 15:44:53 +0100 Subject: [PATCH] add basic msg logic --- internal/x/paychan/types.go | 118 +++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 29 deletions(-) diff --git a/internal/x/paychan/types.go b/internal/x/paychan/types.go index d6fc939b..3dbb0549 100644 --- a/internal/x/paychan/types.go +++ b/internal/x/paychan/types.go @@ -4,8 +4,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -//////// - +// Paychan Type +// Used to represent paychan in keeper module and to serialize. // probably want to convert this to a general purpose "state" struct Paychan { sender sdk.Address @@ -15,7 +15,7 @@ struct Paychan { } -///////////// +// Message Types // Message implement the sdk.Msg interface: @@ -38,62 +38,122 @@ struct Paychan { // GetSigners() []Address // } -/////////////// CreatePayChan -// find a less confusing name + +// A message to create a payment channel. type MsgCreate struct { // maybe just wrap a paychan struct - sender sdk.Address + sender sdk.Address receiver sdk.Address - amount sdk.Coins + amount sdk.Coins } -func (msg CreatMsg) NewMsgCreate() MsgCreate { - return MsgCreate{ } -} +// Create a new message. +// Called in client code when constructing transaction from cli args to send to the network. +// maybe just a placeholder for more advanced future functionality? +// func (msg CreatMsg) NewMsgCreate(sender sdk.Address, receiver sdk.Address, amount sdk.Coins) MsgCreate { +// return MsgCreate{ +// sender +// receiver +// amount +// } +// } func (msg MsgCreate) Type() string { return "paychan" } -func (msg MsgCreate) GetSigners() []sdk.Address { - // sender - //return []sdk.Address{msg.sender} -} - func (msg MsgCreate) GetSignBytes() []byte { - + // TODO create msgCdc in wire.go + b, err := msgCdc.MarshalJSON(struct { + SenderAddr string `json:"sender_addr"` + ReceiverAddr string `json:"receiver_addr"` + Amount sdk.Coins `json:"amount"` + }{ + SenderAddr: sdk.MustBech32ifyAcc(msg.sender), + ReceiverAddr: sdk.MustBech32ifyAcc(msg.receiver), + Amount: msg.amount, + }) + if err != nil { + panic(err) + } + return b } 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 + + // maybe add custom errors + // learn how the errors work + + // example from bank + // if len(in.Address) == 0 { + // return sdk.ErrInvalidAddress(in.Address.String()) + // } + // if !in.Coins.IsValid() { + // return sdk.ErrInvalidCoins(in.Coins.String()) + // } + // if !in.Coins.IsPositive() { + // return sdk.ErrInvalidCoins(in.Coins.String()) + // } } -///////////////// +func (msg MsgCreate) GetSigners() []sdk.Address { + // Only sender must sign to create a paychan + return []sdk.Address{msg.sender} +} + + + + +// A message to close a payment channel. type MsgClose struct { // have to include sender and receiver in msg explicitly (rather than just universal paychanID) // this gives ability to verify signatures with no external information - sender sdk.Address - receiver sdk.Address - id integer + sender sdk.Address + receiver sdk.Address + id integer receiverAmount sdk.Coins // amount the receiver should get - sender amount implicit with paychan balance } -func (msg MsgClose) NewMsgClose( args... ) MsgClose { - return MsgClose{ args... } -} +// func (msg MsgClose) NewMsgClose(sender sdk.Address, receiver sdk.Address, id integer, receiverAmount sdk.Coins) MsgClose { +// return MsgClose{ +// sender +// receiver +// id +// receiverAmount +// } +// } func (msg MsgClose) Type() string { return "paychan" } -func (msg MsgClose) GetSigners() []sdk.Address { - // sender and receiver -} - func (msg MsgClose) GetSignBytes() []byte { - + // TODO create msgCdc in wire.go + b, err := msgCdc.MarshalJSON(struct { + SenderAddr string `json:"sender_addr"` + ReceiverAddr string `json:"receiver_addr"` + Id integer `json:"id"` + ReceiverAmount sdk.Coins `json:"receiver_amount"` + }{ + SenderAddr: sdk.MustBech32ifyAcc(msg.sender), + ReceiverAddr: sdk.MustBech32ifyAcc(msg.receiver), + Id: msg.id + Amount: msg.receiverAmount, + }) + if err != nil { + panic(err) + } + return b } func (msg MsgClose) ValidateBasic() sdk.Error { - return msg.IBCPacket.ValidateBasic() + // TODO implement + //return msg.IBCPacket.ValidateBasic() +} + +func (msg MsgClose) GetSigners() []sdk.Address { + // Both sender and receiver must sign in order to close a channel + retutn []sdk.Address{sender, receiver} }