cosmos-sdk/modules/coin/handler.go

132 lines
3.0 KiB
Go
Raw Normal View History

2017-06-30 04:55:25 -07:00
package coin
import (
2017-07-03 09:58:28 -07:00
"fmt"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/tmlibs/log"
2017-06-30 04:55:25 -07:00
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
2017-07-03 12:34:08 -07:00
"github.com/tendermint/basecoin/stack"
2017-06-30 04:55:25 -07:00
"github.com/tendermint/basecoin/types"
)
2017-07-06 02:30:03 -07:00
//NameCoin - name space of the coin module
const NameCoin = "coin"
2017-06-30 04:55:25 -07:00
2017-07-06 02:30:03 -07:00
// Handler includes an accountant
2017-07-03 05:50:33 -07:00
type Handler struct {
Accountant
}
2017-06-30 04:55:25 -07:00
var _ basecoin.Handler = Handler{}
2017-07-06 02:30:03 -07:00
// NewHandler - new accountant handler for the coin module
2017-07-03 05:50:33 -07:00
func NewHandler() Handler {
return Handler{
2017-07-03 12:34:08 -07:00
Accountant: NewAccountant(""),
2017-07-03 05:50:33 -07:00
}
}
2017-07-06 02:30:03 -07:00
// Name - return name space
2017-07-06 02:39:58 -07:00
func (Handler) Name() string {
2017-06-30 04:55:25 -07:00
return NameCoin
}
// CheckTx checks if there is enough money in the account
func (h Handler) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
send, err := checkTx(ctx, tx)
2017-06-30 04:55:25 -07:00
if err != nil {
return res, err
}
// now make sure there is money
for _, in := range send.Inputs {
2017-07-03 12:34:08 -07:00
_, err = h.CheckCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
if err != nil {
return res, err
}
}
2017-06-30 04:55:25 -07:00
// otherwise, we are good
return res, nil
}
// DeliverTx moves the money
func (h Handler) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
send, err := checkTx(ctx, tx)
2017-06-30 04:55:25 -07:00
if err != nil {
return res, err
}
// deduct from all input accounts
for _, in := range send.Inputs {
_, err = h.ChangeCoins(store, in.Address, in.Coins.Negative(), in.Sequence)
if err != nil {
return res, err
}
}
// add to all output accounts
for _, out := range send.Outputs {
// note: sequence number is ignored when adding coins, only checked for subtracting
_, err = h.ChangeCoins(store, out.Address, out.Coins, 0)
if err != nil {
return res, err
}
}
// a-ok!
2017-06-30 04:55:25 -07:00
return basecoin.Result{}, nil
}
2017-07-06 02:30:03 -07:00
// SetOption - sets the genesis account balance
func (h Handler) SetOption(l log.Logger, store types.KVStore, module, key, value string) (log string, err error) {
if module != NameCoin {
return "", errors.ErrUnknownModule(module)
}
if key == "account" {
2017-07-03 09:58:28 -07:00
var acc GenesisAccount
err = data.FromJSON([]byte(value), &acc)
if err != nil {
return "", err
}
acc.Balance.Sort()
addr, err := acc.GetAddr()
if err != nil {
return "", ErrInvalidAddress()
}
2017-07-03 12:34:08 -07:00
// this sets the permission for a public key signature, use that app
actor := stack.SigPerm(addr)
err = storeAccount(store, h.MakeKey(actor), acc.ToAccount())
2017-07-03 09:58:28 -07:00
if err != nil {
return "", err
}
return "Success", nil
}
2017-07-06 02:30:03 -07:00
msg := fmt.Sprintf("Unknown key: %s", key)
return "", errors.ErrInternal(msg)
}
2017-06-30 11:26:17 -07:00
func checkTx(ctx basecoin.Context, tx basecoin.Tx) (send SendTx, err error) {
2017-06-30 04:55:25 -07:00
// check if the tx is proper type and valid
2017-06-30 11:26:17 -07:00
send, ok := tx.Unwrap().(SendTx)
2017-06-30 04:55:25 -07:00
if !ok {
2017-07-03 05:50:33 -07:00
return send, errors.ErrInvalidFormat(tx)
2017-06-30 04:55:25 -07:00
}
2017-06-30 11:26:17 -07:00
err = send.ValidateBasic()
2017-06-30 04:55:25 -07:00
if err != nil {
2017-06-30 11:26:17 -07:00
return send, err
2017-06-30 04:55:25 -07:00
}
// check if all inputs have permission
for _, in := range send.Inputs {
if !ctx.HasPermission(in.Address) {
2017-07-03 05:50:33 -07:00
return send, errors.ErrUnauthorized()
2017-06-30 04:55:25 -07:00
}
}
return send, nil
}