0g-chain/internal/x/paychan/keeper.go

113 lines
3.2 KiB
Go
Raw Normal View History

2018-07-08 22:09:07 +00:00
package paychan
2018-07-09 18:46:51 +00:00
import (
"strconv"
)
2018-07-08 22:09:07 +00:00
// keeper of the paychan store
type Keeper struct {
2018-07-09 18:46:51 +00:00
storeKey sdk.StoreKey
//cdc *wire.Codec // needed?
2018-07-08 22:09:07 +00:00
coinKeeper bank.Keeper
// codespace
2018-07-09 18:46:51 +00:00
//codespace sdk.CodespaceType // ??
2018-07-08 22:09:07 +00:00
}
2018-07-09 18:46:51 +00:00
// Called when creating new app.
//func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk.CodespaceType) Keeper {
func NewKeeper(key sdk.StoreKey, ck bank.Keeper) Keeper {
2018-07-08 22:09:07 +00:00
keeper := Keeper{
2018-07-09 18:46:51 +00:00
storeKey: key,
//cdc: cdc,
2018-07-08 22:09:07 +00:00
coinKeeper: ck,
2018-07-09 18:46:51 +00:00
//codespace: codespace,
2018-07-08 22:09:07 +00:00
}
return keeper
}
// bunch of business logic ...
2018-07-09 18:46:51 +00:00
// Reteive a payment channel struct from the blockchain store.
// They are indexed by a concatenation of sender address, receiver address, and an integer.
func (keeper Keeper) GetPaychan(ctx sdk.Context, sender sdk.Address, receiver sdk.Address, id integer) (Paychan, bool) {
// Return error as second argument instead of bool?
var pych Paychan
2018-07-08 22:09:07 +00:00
// load from DB
2018-07-09 18:46:51 +00:00
store := ctx.KVStore(k.storeKey)
bz := store.Get(paychanKey(sender, receiver, id))
if bz == nil {
return pych, false
}
// unmarshal
k.cdc.MustUnmarshalBinary(bz, &pych)
2018-07-08 22:09:07 +00:00
// return
2018-07-09 18:46:51 +00:00
return pych, true
2018-07-08 22:09:07 +00:00
}
2018-07-09 18:46:51 +00:00
// Store payment channel struct in blockchain store.
2018-07-08 22:09:07 +00:00
func (keeper Keeper) setPaychan(pych Paychan) sdk.Error {
2018-07-09 18:46:51 +00:00
store := ctx.KVStore(k.storeKey)
2018-07-08 22:09:07 +00:00
// marshal
2018-07-09 18:46:51 +00:00
bz := k.cdc.MustMarshalBinary(pych)
2018-07-08 22:09:07 +00:00
// write to db
2018-07-09 18:46:51 +00:00
pychKey := paychanKey(pych.sender, pych.receiver, pych.id)
store.Set(pychKey, bz)
// TODO handler errors
2018-07-08 22:09:07 +00:00
}
2018-07-09 18:46:51 +00:00
// 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) {
2018-07-08 22:09:07 +00:00
// subtract coins from sender
2018-07-09 18:46:51 +00:00
k.coinKeeper.SubtractCoins(ctx, sender, amt)
2018-07-08 22:09:07 +00:00
// create new Paychan struct (create ID)
2018-07-09 18:46:51 +00:00
pych := Paychan{sender,
receiver,
id,
balance: amt}
2018-07-08 22:09:07 +00:00
// save to db
2018-07-09 18:46:51 +00:00
err := k.setPaychan(pych)
2018-07-08 22:09:07 +00:00
2018-07-09 18:46:51 +00:00
return pych, err
// TODO validation
// sender has enough coins - done in Subtract method
2018-07-08 22:09:07 +00:00
// receiver address exists?
// paychan doesn't exist already
}
2018-07-09 18:46:51 +00:00
// 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 {
pych := GetPaychan(ctx, sender, receiver, id)
// compute coin distribution
senderAmt = pych.balance.Minus(receiverAmt) // Minus sdk.Coins method
2018-07-08 22:09:07 +00:00
// add coins to sender
2018-07-09 18:46:51 +00:00
k.coinKeeper.AddCoins(ctx, sender, senderAmt)
2018-07-08 22:09:07 +00:00
// add coins to receiver
2018-07-09 18:46:51 +00:00
k.coinKeeper.AddCoins(ctx, receiver, receiverAmt)
2018-07-08 22:09:07 +00:00
// delete paychan from db
2018-07-09 18:46:51 +00:00
pychKey := paychanKey(pych.sender, pych.receiver, pych.id)
store.Delete(pychKey)
2018-07-08 22:09:07 +00:00
2018-07-09 18:46:51 +00:00
// TODO validation
2018-07-08 22:09:07 +00:00
// paychan exists
// output coins are less than paychan balance
// sender and receiver addresses exist?
2018-07-09 18:46:51 +00:00
return nil
2018-07-08 22:09:07 +00:00
}
2018-07-09 18:46:51 +00:00
// Creates a key to reference a paychan in the blockchain store.
func paychanKey(sender sdk.Address, receiver sdk.Address, id integer) []byte {
//sdk.Address is just a slice of bytes under a different name
//convert id to string then to byte slice
idAsBytes := []byte(strconv.Itoa(id))
2018-07-08 22:09:07 +00:00
// concat sender and receiver and integer ID
2018-07-09 18:46:51 +00:00
return append(sender.Bytes(), receiver.Bytes()..., idAsBytes...)
2018-07-08 22:09:07 +00:00
}
// maybe getAllPaychans(sender sdk.address) []Paychan