move sendtx into coinstore

This commit is contained in:
Ethan Buchman 2018-01-07 15:49:58 -05:00 committed by Jae Kwon
parent e908cfbb6f
commit d3de10e22b
6 changed files with 39 additions and 46 deletions

View File

@ -14,7 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types"
acm "github.com/cosmos/cosmos-sdk/x/account" acm "github.com/cosmos/cosmos-sdk/x/account"
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/sendtx" "github.com/cosmos/cosmos-sdk/x/coinstore"
) )
func main() { func main() {
@ -42,7 +42,7 @@ func main() {
// logger.Decorator(), // logger.Decorator(),
auth.DecoratorFn(acm.NewAccountStore), auth.DecoratorFn(acm.NewAccountStore),
).WithHandler( ).WithHandler(
sendtx.TransferHandlerFn(acm.NewAccountStore), coinstore.TransferHandlerFn(acm.NewAccountStore),
) )
// TODO: load genesis // TODO: load genesis
@ -76,7 +76,7 @@ func main() {
} }
func txParser(txBytes []byte) (types.Tx, error) { func txParser(txBytes []byte) (types.Tx, error) {
var tx sendtx.SendTx var tx coinstore.SendTx
err := json.Unmarshal(txBytes, &tx) err := json.Unmarshal(txBytes, &tx)
return tx, err return tx, err
} }

View File

@ -2,6 +2,8 @@
package coinstore package coinstore
import ( import (
"fmt"
"github.com/cosmos/cosmos-sdk/errors" "github.com/cosmos/cosmos-sdk/errors"
) )
@ -69,6 +71,21 @@ func ErrUnknownRequest(log string) error {
return newError(CodeUnknownRequest, log) return newError(CodeUnknownRequest, log)
} }
//----------------------------------------
// TODO: clean up
func ErrNoInputs() error {
return fmt.Errorf("No inputs")
}
func ErrNoOutputs() error {
return fmt.Errorf("No outputs")
}
func ErrInvalidSequence(seq int64) error {
return fmt.Errorf("Bad sequence %d", seq)
}
//---------------------------------------- //----------------------------------------
// Misc // Misc

View File

@ -1,15 +1,14 @@
package sendtx package coinstore
import ( import (
"github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/coinstore"
) )
func TransferHandlerFn(newAccStore func(types.KVStore) types.AccountStore) types.Handler { func TransferHandlerFn(newAccStore func(types.KVStore) types.AccountStore) types.Handler {
return func(ctx types.Context, ms types.MultiStore, tx types.Tx) types.Result { return func(ctx types.Context, ms types.MultiStore, tx types.Tx) types.Result {
accStore := newAccStore(ms.GetKVStore("main")) accStore := newAccStore(ms.GetKVStore("main"))
cs := coinstore.CoinStore{accStore} cs := CoinStore{accStore}
sendTx, ok := tx.(SendTx) sendTx, ok := tx.(SendTx)
if !ok { if !ok {

View File

@ -1,4 +1,4 @@
package sendtx package coinstore
import ( import (
"encoding/json" "encoding/json"
@ -8,21 +8,15 @@ import (
"github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/coin" "github.com/cosmos/cosmos-sdk/x/coin"
"github.com/cosmos/cosmos-sdk/x/coinstore"
)
type (
Address = crypto.Address
Coins = coin.Coins
) )
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TxInput // TxInput
type TxInput struct { type TxInput struct {
Address Address `json:"address"` Address crypto.Address `json:"address"`
Coins Coins `json:"coins"` Coins Coins `json:"coins"`
Sequence int64 `json:"sequence"` Sequence int64 `json:"sequence"`
signature crypto.Signature signature crypto.Signature
} }
@ -30,16 +24,16 @@ type TxInput struct {
// ValidateBasic - validate transaction input // ValidateBasic - validate transaction input
func (txIn TxInput) ValidateBasic() error { func (txIn TxInput) ValidateBasic() error {
if len(txIn.Address) == 0 { if len(txIn.Address) == 0 {
return coinstore.ErrInvalidAddress(txIn.Address.String()) return ErrInvalidAddress(txIn.Address.String())
} }
if txIn.Sequence < 0 { if txIn.Sequence < 0 {
return ErrInvalidSequence(txIn.Sequence) return ErrInvalidSequence(txIn.Sequence)
} }
if !txIn.Coins.IsValid() { if !txIn.Coins.IsValid() {
return coinstore.ErrInvalidCoins(txIn.Coins.String()) return ErrInvalidCoins(txIn.Coins.String())
} }
if !txIn.Coins.IsPositive() { if !txIn.Coins.IsPositive() {
return coinstore.ErrInvalidCoins(txIn.Coins.String()) return ErrInvalidCoins(txIn.Coins.String())
} }
return nil return nil
} }
@ -49,7 +43,7 @@ func (txIn TxInput) String() string {
} }
// NewTxInput - create a transaction input, used with SendTx // NewTxInput - create a transaction input, used with SendTx
func NewTxInput(addr Address, coins Coins) TxInput { func NewTxInput(addr crypto.Address, coins Coins) TxInput {
input := TxInput{ input := TxInput{
Address: addr, Address: addr,
Coins: coins, Coins: coins,
@ -58,7 +52,7 @@ func NewTxInput(addr Address, coins Coins) TxInput {
} }
// NewTxInputWithSequence - create a transaction input, used with SendTx // NewTxInputWithSequence - create a transaction input, used with SendTx
func NewTxInputWithSequence(addr Address, coins Coins, seq int64) TxInput { func NewTxInputWithSequence(addr crypto.Address, coins Coins, seq int64) TxInput {
input := NewTxInput(addr, coins) input := NewTxInput(addr, coins)
input.Sequence = seq input.Sequence = seq
return input return input
@ -68,20 +62,20 @@ func NewTxInputWithSequence(addr Address, coins Coins, seq int64) TxInput {
// TxOutput - expected coin movement output, used with SendTx // TxOutput - expected coin movement output, used with SendTx
type TxOutput struct { type TxOutput struct {
Address Address `json:"address"` Address crypto.Address `json:"address"`
Coins Coins `json:"coins"` Coins Coins `json:"coins"`
} }
// ValidateBasic - validate transaction output // ValidateBasic - validate transaction output
func (txOut TxOutput) ValidateBasic() error { func (txOut TxOutput) ValidateBasic() error {
if len(txOut.Address) == 0 { if len(txOut.Address) == 0 {
return coinstore.ErrInvalidAddress(txOut.Address.String()) return ErrInvalidAddress(txOut.Address.String())
} }
if !txOut.Coins.IsValid() { if !txOut.Coins.IsValid() {
return coinstore.ErrInvalidCoins(txOut.Coins.String()) return ErrInvalidCoins(txOut.Coins.String())
} }
if !txOut.Coins.IsPositive() { if !txOut.Coins.IsPositive() {
return coinstore.ErrInvalidCoins(txOut.Coins.String()) return ErrInvalidCoins(txOut.Coins.String())
} }
return nil return nil
} }
@ -91,7 +85,7 @@ func (txOut TxOutput) String() string {
} }
// NewTxOutput - create a transaction output, used with SendTx // NewTxOutput - create a transaction output, used with SendTx
func NewTxOutput(addr Address, coins Coins) TxOutput { func NewTxOutput(addr crypto.Address, coins Coins) TxOutput {
output := TxOutput{ output := TxOutput{
Address: addr, Address: addr,
Coins: coins, Coins: coins,
@ -135,7 +129,7 @@ func (tx SendTx) ValidateBasic() error {
} }
// make sure inputs and outputs match // make sure inputs and outputs match
if !totalIn.IsEqual(totalOut) { if !totalIn.IsEqual(totalOut) {
return coinstore.ErrInvalidCoins(totalIn.String()) // TODO return ErrInvalidCoins(totalIn.String()) // TODO
} }
return nil return nil
} }

View File

@ -1,4 +1,4 @@
package sendtx package coinstore
import ( import (
"testing" "testing"

View File

@ -1,17 +0,0 @@
package sendtx
import "fmt"
// TODO! Deal coherently with this and coinstore/errors.go
func ErrNoInputs() error {
return fmt.Errorf("No inputs")
}
func ErrNoOutputs() error {
return fmt.Errorf("No outputs")
}
func ErrInvalidSequence(seq int64) error {
return fmt.Errorf("Bad sequence %d", seq)
}