add basic msg logic

This commit is contained in:
rhuairahrighairigh 2018-07-10 15:44:53 +01:00
parent 4732c32ab1
commit f146d9ae0c

View File

@ -4,8 +4,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" 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" // probably want to convert this to a general purpose "state"
struct Paychan { struct Paychan {
sender sdk.Address sender sdk.Address
@ -15,7 +15,7 @@ struct Paychan {
} }
///////////// // Message Types
// Message implement the sdk.Msg interface: // Message implement the sdk.Msg interface:
@ -38,8 +38,8 @@ struct Paychan {
// GetSigners() []Address // GetSigners() []Address
// } // }
/////////////// CreatePayChan
// find a less confusing name // A message to create a payment channel.
type MsgCreate struct { type MsgCreate struct {
// maybe just wrap a paychan struct // maybe just wrap a paychan struct
sender sdk.Address sender sdk.Address
@ -47,29 +47,67 @@ type MsgCreate struct {
amount sdk.Coins amount sdk.Coins
} }
func (msg CreatMsg) NewMsgCreate() MsgCreate { // Create a new message.
return MsgCreate{ } // 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) Type() string { return "paychan" }
func (msg MsgCreate) GetSigners() []sdk.Address {
// sender
//return []sdk.Address{msg.sender}
}
func (msg MsgCreate) GetSignBytes() []byte { 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 { func (msg MsgCreate) ValidateBasic() sdk.Error {
// TODO implement
// verify msg as much as possible without using external information (such as account balance) // verify msg as much as possible without using external information (such as account balance)
// are all fields present // are all fields present
// are all fields valid // are all fields valid
// maybe check if sender and receiver is different // 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 { type MsgClose struct {
// have to include sender and receiver in msg explicitly (rather than just universal paychanID) // have to include sender and receiver in msg explicitly (rather than just universal paychanID)
// this gives ability to verify signatures with no external information // this gives ability to verify signatures with no external information
@ -79,21 +117,43 @@ type MsgClose struct {
receiverAmount sdk.Coins // amount the receiver should get - sender amount implicit with paychan balance receiverAmount sdk.Coins // amount the receiver should get - sender amount implicit with paychan balance
} }
func (msg MsgClose) NewMsgClose( args... ) MsgClose { // func (msg MsgClose) NewMsgClose(sender sdk.Address, receiver sdk.Address, id integer, receiverAmount sdk.Coins) MsgClose {
return MsgClose{ args... } // return MsgClose{
} // sender
// receiver
// id
// receiverAmount
// }
// }
func (msg MsgClose) Type() string { return "paychan" } func (msg MsgClose) Type() string { return "paychan" }
func (msg MsgClose) GetSigners() []sdk.Address {
// sender and receiver
}
func (msg MsgClose) GetSignBytes() []byte { 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 { 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}
} }