mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 00:05:18 +00:00
add handler logic
This commit is contained in:
parent
dfdb06ce6f
commit
4732c32ab1
@ -1,19 +1,19 @@
|
|||||||
package paychan
|
package paychan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Called when adding routes to a newly created app.
|
|
||||||
// NewHandler returns a handler for "paychan" type messages.
|
// NewHandler returns a handler for "paychan" type messages.
|
||||||
|
// Called when adding routes to a newly created app.
|
||||||
func NewHandler(k Keeper) sdk.Handler {
|
func NewHandler(k Keeper) sdk.Handler {
|
||||||
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case MsgSend:
|
case MsgCreate:
|
||||||
return handleMsgSend(ctx, k, msg)
|
return handleMsgCreate(ctx, k, msg)
|
||||||
case MsgIssue:
|
case MsgClose:
|
||||||
return handleMsgIssue(ctx, k, msg)
|
return handleMsgClose(ctx, k, msg)
|
||||||
default:
|
default:
|
||||||
errMsg := "Unrecognized paychan Msg type: " + reflect.TypeOf(msg).Name()
|
errMsg := "Unrecognized paychan Msg type: " + reflect.TypeOf(msg).Name()
|
||||||
return sdk.ErrUnknownRequest(errMsg).Result()
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
||||||
@ -21,14 +21,30 @@ func NewHandler(k Keeper) sdk.Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO does validation go here or in the keeper?
|
||||||
|
|
||||||
// Handle CreateMsg.
|
// Handle CreateMsg.
|
||||||
func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgSend) sdk.Result {
|
func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result {
|
||||||
// k.CreatePaychan(args...)
|
// TODO maybe remove tags for first version
|
||||||
// handle erros
|
tags, err := k.CreatePaychan(msg.sender, msg.receiver, msg.amount)
|
||||||
|
if err != nil {
|
||||||
|
return err.Result()
|
||||||
|
}
|
||||||
|
// TODO any other information that should be returned in Result?
|
||||||
|
return sdk.Result{
|
||||||
|
Tags: tags
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle CloseMsg.
|
// Handle CloseMsg.
|
||||||
func handleMsgClose(ctx sdk.Context, k Keeper, msg MsgIssue) sdk.Result {
|
func handleMsgClose(ctx sdk.Context, k Keeper, msg MsgClose) sdk.Result {
|
||||||
// k.ClosePaychan(args...)
|
// TODO maybe remove tags for first version
|
||||||
// handle errors
|
tags, err := k.ClosePaychan(msg.sender, msg.receiver, msg.id, msg.receiverAmount)
|
||||||
|
if err != nil {
|
||||||
|
return err.Result()
|
||||||
|
}
|
||||||
|
// These tags can be used to subscribe to channel closures
|
||||||
|
return sdk.Result{
|
||||||
|
Tags: tags
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,15 @@ package paychan
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// keeper of the paychan store
|
// keeper of the paychan store
|
||||||
type Keeper struct {
|
type Keeper struct {
|
||||||
storeKey sdk.StoreKey
|
storeKey sdk.StoreKey
|
||||||
//cdc *wire.Codec // needed?
|
cdc *wire.Codec // needed to serialize objects before putting them in the store
|
||||||
coinKeeper bank.Keeper
|
coinKeeper bank.Keeper
|
||||||
|
|
||||||
// codespace
|
// codespace
|
||||||
@ -16,10 +19,10 @@ type Keeper struct {
|
|||||||
|
|
||||||
// Called when creating new app.
|
// Called when creating new app.
|
||||||
//func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk.CodespaceType) Keeper {
|
//func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk.CodespaceType) Keeper {
|
||||||
func NewKeeper(key sdk.StoreKey, ck bank.Keeper) Keeper {
|
func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper) Keeper {
|
||||||
keeper := Keeper{
|
keeper := Keeper{
|
||||||
storeKey: key,
|
storeKey: key,
|
||||||
//cdc: cdc,
|
cdc: cdc,
|
||||||
coinKeeper: ck,
|
coinKeeper: ck,
|
||||||
//codespace: codespace,
|
//codespace: codespace,
|
||||||
}
|
}
|
||||||
@ -58,7 +61,9 @@ 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, id integer, amt sdk.Coins) (Paychan, 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)
|
||||||
|
id := len(keeper.GetPaychans(sender, receiver)) + 1
|
||||||
// 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)
|
||||||
@ -69,16 +74,18 @@ func (keeer Keeper) CreatePaychan(sender sdk.Address, receiver sdkAddress, id in
|
|||||||
// save to db
|
// save to db
|
||||||
err := k.setPaychan(pych)
|
err := k.setPaychan(pych)
|
||||||
|
|
||||||
return pych, err
|
|
||||||
|
|
||||||
// TODO validation
|
// TODO validation
|
||||||
// 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
|
||||||
|
|
||||||
|
tags := sdk.NewTags()
|
||||||
|
return tags, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close a payment channel and distribute funds to participants.
|
// Close a payment channel and distribute funds to participants.
|
||||||
func (keeper Keeper) ClosePaychan(sender sdk.Address, receiver sdk.Address, id integer, receiverAmt sdk.Coins) sdk.Error {
|
func (keeper Keeper) ClosePaychan(sender sdk.Address, receiver sdk.Address, id integer, receiverAmt sdk.Coins) (sdk.Tags, sdk.Error) {
|
||||||
pych := GetPaychan(ctx, sender, receiver, id)
|
pych := GetPaychan(ctx, sender, receiver, id)
|
||||||
// compute coin distribution
|
// compute coin distribution
|
||||||
senderAmt = pych.balance.Minus(receiverAmt) // Minus sdk.Coins method
|
senderAmt = pych.balance.Minus(receiverAmt) // Minus sdk.Coins method
|
||||||
@ -96,7 +103,13 @@ func (keeper Keeper) ClosePaychan(sender sdk.Address, receiver sdk.Address, id i
|
|||||||
// output coins are less than paychan balance
|
// output coins are less than paychan balance
|
||||||
// sender and receiver addresses exist?
|
// sender and receiver addresses exist?
|
||||||
|
|
||||||
return nil
|
//sdk.NewTags(
|
||||||
|
// "action", []byte("channel closure"),
|
||||||
|
// "receiver", receiver.Bytes(),
|
||||||
|
// "sender", sender.Bytes(),
|
||||||
|
// "id", ??)
|
||||||
|
tags := sdk.NewTags()
|
||||||
|
return tags, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a key to reference a paychan in the blockchain store.
|
// Creates a key to reference a paychan in the blockchain store.
|
||||||
@ -109,4 +122,10 @@ func paychanKey(sender sdk.Address, receiver sdk.Address, id integer) []byte {
|
|||||||
return append(sender.Bytes(), receiver.Bytes()..., idAsBytes...)
|
return append(sender.Bytes(), receiver.Bytes()..., idAsBytes...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get all paychans between a given sender and receiver.
|
||||||
|
func (keeper Keeper) GetPaychans(sender sdk.Address, receiver sdk.Address) []Paychan {
|
||||||
|
var paychans []Paychan
|
||||||
|
// TODO Implement this
|
||||||
|
return paychans
|
||||||
|
}
|
||||||
// maybe getAllPaychans(sender sdk.address) []Paychan
|
// maybe getAllPaychans(sender sdk.address) []Paychan
|
||||||
|
@ -8,10 +8,10 @@ import (
|
|||||||
|
|
||||||
// probably want to convert this to a general purpose "state"
|
// probably want to convert this to a general purpose "state"
|
||||||
struct Paychan {
|
struct Paychan {
|
||||||
balance sdk.Coins
|
|
||||||
sender sdk.Address
|
sender sdk.Address
|
||||||
receiver sdk.Address
|
receiver sdk.Address
|
||||||
id integer
|
id integer
|
||||||
|
balance sdk.Coins
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -40,29 +40,29 @@ struct Paychan {
|
|||||||
|
|
||||||
/////////////// CreatePayChan
|
/////////////// CreatePayChan
|
||||||
// find a less confusing name
|
// find a less confusing name
|
||||||
type CreateMsg struct {
|
type MsgCreate struct {
|
||||||
// maybe just wrap a paychan struct
|
// maybe just wrap a paychan struct
|
||||||
sender sdk.Address
|
sender sdk.Address
|
||||||
receiver sdk.Address
|
receiver sdk.Address
|
||||||
amount sdk.Balance
|
amount sdk.Coins
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CreatMsg) NewCreateMsg() CreateMsg {
|
func (msg CreatMsg) NewMsgCreate() MsgCreate {
|
||||||
return CreateMsg{ }
|
return MsgCreate{ }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CreateMsg) Type() string { return "paychan" }
|
func (msg MsgCreate) Type() string { return "paychan" }
|
||||||
|
|
||||||
func (msg CreateMsg) GetSigners() []sdk.Address {
|
func (msg MsgCreate) GetSigners() []sdk.Address {
|
||||||
// sender
|
// sender
|
||||||
//return []sdk.Address{msg.sender}
|
//return []sdk.Address{msg.sender}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CreateMsg) GetSignBytes() []byte {
|
func (msg MsgCreate) GetSignBytes() []byte {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CreateMsg) ValidateBasic() sdk.Error {
|
func (msg MsgCreate) ValidateBasic() sdk.Error {
|
||||||
// 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
|
||||||
@ -70,7 +70,7 @@ func (msg CreateMsg) ValidateBasic() sdk.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/////////////////
|
/////////////////
|
||||||
type CloseMsg 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
|
||||||
sender sdk.Address
|
sender sdk.Address
|
||||||
@ -79,21 +79,21 @@ type CloseMsg 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 CloseMsg) NewCloseMsg( args... ) CloseMsg {
|
func (msg MsgClose) NewMsgClose( args... ) MsgClose {
|
||||||
return CloseMsg{ args... }
|
return MsgClose{ args... }
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CloseMsg) Type() string { return "paychan" }
|
func (msg MsgClose) Type() string { return "paychan" }
|
||||||
|
|
||||||
func (msg CloseMsg) GetSigners() []sdk.Address {
|
func (msg MsgClose) GetSigners() []sdk.Address {
|
||||||
// sender and receiver
|
// sender and receiver
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CloseMsg) GetSignBytes() []byte {
|
func (msg MsgClose) GetSignBytes() []byte {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg CloseMsg) ValidateBasic() sdk.Error {
|
func (msg MsgClose) ValidateBasic() sdk.Error {
|
||||||
return msg.IBCPacket.ValidateBasic()
|
return msg.IBCPacket.ValidateBasic()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user