From d3de10e22bb94298841ba9d6d54c9b2a14ff9346 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 7 Jan 2018 15:49:58 -0500 Subject: [PATCH] move sendtx into coinstore --- examples/basecoin/main.go | 6 ++--- x/coinstore/errors.go | 17 +++++++++++++ x/{sendtx => coinstore}/handler.go | 5 ++-- x/{sendtx => coinstore}/tx.go | 38 +++++++++++++----------------- x/{sendtx => coinstore}/tx_test.go | 2 +- x/sendtx/errors.go | 17 ------------- 6 files changed, 39 insertions(+), 46 deletions(-) rename x/{sendtx => coinstore}/handler.go (89%) rename x/{sendtx => coinstore}/tx.go (83%) rename x/{sendtx => coinstore}/tx_test.go (99%) delete mode 100644 x/sendtx/errors.go diff --git a/examples/basecoin/main.go b/examples/basecoin/main.go index 5f1eb78e5..879010cc9 100644 --- a/examples/basecoin/main.go +++ b/examples/basecoin/main.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/types" acm "github.com/cosmos/cosmos-sdk/x/account" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/sendtx" + "github.com/cosmos/cosmos-sdk/x/coinstore" ) func main() { @@ -42,7 +42,7 @@ func main() { // logger.Decorator(), auth.DecoratorFn(acm.NewAccountStore), ).WithHandler( - sendtx.TransferHandlerFn(acm.NewAccountStore), + coinstore.TransferHandlerFn(acm.NewAccountStore), ) // TODO: load genesis @@ -76,7 +76,7 @@ func main() { } func txParser(txBytes []byte) (types.Tx, error) { - var tx sendtx.SendTx + var tx coinstore.SendTx err := json.Unmarshal(txBytes, &tx) return tx, err } diff --git a/x/coinstore/errors.go b/x/coinstore/errors.go index 2e63b4c75..be2c0d996 100644 --- a/x/coinstore/errors.go +++ b/x/coinstore/errors.go @@ -2,6 +2,8 @@ package coinstore import ( + "fmt" + "github.com/cosmos/cosmos-sdk/errors" ) @@ -69,6 +71,21 @@ func ErrUnknownRequest(log string) error { 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 diff --git a/x/sendtx/handler.go b/x/coinstore/handler.go similarity index 89% rename from x/sendtx/handler.go rename to x/coinstore/handler.go index 0fc4886e6..2b7e8d7ec 100644 --- a/x/sendtx/handler.go +++ b/x/coinstore/handler.go @@ -1,15 +1,14 @@ -package sendtx +package coinstore import ( "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/coinstore" ) func TransferHandlerFn(newAccStore func(types.KVStore) types.AccountStore) types.Handler { return func(ctx types.Context, ms types.MultiStore, tx types.Tx) types.Result { accStore := newAccStore(ms.GetKVStore("main")) - cs := coinstore.CoinStore{accStore} + cs := CoinStore{accStore} sendTx, ok := tx.(SendTx) if !ok { diff --git a/x/sendtx/tx.go b/x/coinstore/tx.go similarity index 83% rename from x/sendtx/tx.go rename to x/coinstore/tx.go index e89434e80..7bead83d6 100644 --- a/x/sendtx/tx.go +++ b/x/coinstore/tx.go @@ -1,4 +1,4 @@ -package sendtx +package coinstore import ( "encoding/json" @@ -8,21 +8,15 @@ import ( "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/coin" - "github.com/cosmos/cosmos-sdk/x/coinstore" -) - -type ( - Address = crypto.Address - Coins = coin.Coins ) //----------------------------------------------------------------------------- // TxInput type TxInput struct { - Address Address `json:"address"` - Coins Coins `json:"coins"` - Sequence int64 `json:"sequence"` + Address crypto.Address `json:"address"` + Coins Coins `json:"coins"` + Sequence int64 `json:"sequence"` signature crypto.Signature } @@ -30,16 +24,16 @@ type TxInput struct { // ValidateBasic - validate transaction input func (txIn TxInput) ValidateBasic() error { if len(txIn.Address) == 0 { - return coinstore.ErrInvalidAddress(txIn.Address.String()) + return ErrInvalidAddress(txIn.Address.String()) } if txIn.Sequence < 0 { return ErrInvalidSequence(txIn.Sequence) } if !txIn.Coins.IsValid() { - return coinstore.ErrInvalidCoins(txIn.Coins.String()) + return ErrInvalidCoins(txIn.Coins.String()) } if !txIn.Coins.IsPositive() { - return coinstore.ErrInvalidCoins(txIn.Coins.String()) + return ErrInvalidCoins(txIn.Coins.String()) } return nil } @@ -49,7 +43,7 @@ func (txIn TxInput) String() string { } // 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{ Address: addr, Coins: coins, @@ -58,7 +52,7 @@ func NewTxInput(addr Address, coins Coins) TxInput { } // 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.Sequence = seq return input @@ -68,20 +62,20 @@ func NewTxInputWithSequence(addr Address, coins Coins, seq int64) TxInput { // TxOutput - expected coin movement output, used with SendTx type TxOutput struct { - Address Address `json:"address"` - Coins Coins `json:"coins"` + Address crypto.Address `json:"address"` + Coins Coins `json:"coins"` } // ValidateBasic - validate transaction output func (txOut TxOutput) ValidateBasic() error { if len(txOut.Address) == 0 { - return coinstore.ErrInvalidAddress(txOut.Address.String()) + return ErrInvalidAddress(txOut.Address.String()) } if !txOut.Coins.IsValid() { - return coinstore.ErrInvalidCoins(txOut.Coins.String()) + return ErrInvalidCoins(txOut.Coins.String()) } if !txOut.Coins.IsPositive() { - return coinstore.ErrInvalidCoins(txOut.Coins.String()) + return ErrInvalidCoins(txOut.Coins.String()) } return nil } @@ -91,7 +85,7 @@ func (txOut TxOutput) String() string { } // 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{ Address: addr, Coins: coins, @@ -135,7 +129,7 @@ func (tx SendTx) ValidateBasic() error { } // make sure inputs and outputs match if !totalIn.IsEqual(totalOut) { - return coinstore.ErrInvalidCoins(totalIn.String()) // TODO + return ErrInvalidCoins(totalIn.String()) // TODO } return nil } diff --git a/x/sendtx/tx_test.go b/x/coinstore/tx_test.go similarity index 99% rename from x/sendtx/tx_test.go rename to x/coinstore/tx_test.go index 84b1b1d5b..c4627d487 100644 --- a/x/sendtx/tx_test.go +++ b/x/coinstore/tx_test.go @@ -1,4 +1,4 @@ -package sendtx +package coinstore import ( "testing" diff --git a/x/sendtx/errors.go b/x/sendtx/errors.go deleted file mode 100644 index ffcf426e8..000000000 --- a/x/sendtx/errors.go +++ /dev/null @@ -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) -}