From 0303f2aaaa3c87e5408a759a0921e5f2be068e3d Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Thu, 6 Jul 2017 05:30:03 -0400 Subject: [PATCH] golinted up to and incl modules --- app/genesis.go | 2 +- errors/common.go | 28 ++++++++++++---------------- errors/main.go | 5 +---- modules/coin/errors.go | 1 + modules/coin/genesis.go | 3 +++ modules/coin/handler.go | 17 +++++++++-------- modules/coin/store.go | 9 ++++++++- modules/coin/tx.go | 12 +++++++++++- modules/fee/errors.go | 1 + modules/fee/handler.go | 12 ++++++++---- version/version.go | 1 + 11 files changed, 56 insertions(+), 35 deletions(-) diff --git a/app/genesis.go b/app/genesis.go index 1549ca7071..87c60227f9 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -8,7 +8,7 @@ import ( cmn "github.com/tendermint/tmlibs/common" ) -// LoadGenesis - Load the genesis json file +// LoadGenesis - Load the genesis file into memory func (app *Basecoin) LoadGenesis(path string) error { genDoc, err := loadGenesis(path) if err != nil { diff --git a/errors/common.go b/errors/common.go index ac9e2809a8..43d722c8ad 100644 --- a/errors/common.go +++ b/errors/common.go @@ -1,11 +1,7 @@ +//nolint package errors -/** -* Copyright (C) 2017 Ethan Frey -**/ - import ( - rawerr "errors" "fmt" "github.com/pkg/errors" @@ -14,17 +10,17 @@ import ( ) var ( - errDecoding = rawerr.New("Error decoding input") - errUnauthorized = rawerr.New("Unauthorized") - errInvalidSignature = rawerr.New("Invalid Signature") - errTooLarge = rawerr.New("Input size too large") - errMissingSignature = rawerr.New("Signature missing") - errTooManySignatures = rawerr.New("Too many signatures") - errNoChain = rawerr.New("No chain id provided") - errWrongChain = rawerr.New("Wrong chain for tx") - errUnknownTxType = rawerr.New("Tx type unknown") - errInvalidFormat = rawerr.New("Invalid format") - errUnknownModule = rawerr.New("Unknown module") + errDecoding = fmt.Errorf("Error decoding input") + errUnauthorized = fmt.Errorf("Unauthorized") + errInvalidSignature = fmt.Errorf("Invalid Signature") + errTooLarge = fmt.Errorf("Input size too large") + errMissingSignature = fmt.Errorf("Signature missing") + errTooManySignatures = fmt.Errorf("Too many signatures") + errNoChain = fmt.Errorf("No chain id provided") + errWrongChain = fmt.Errorf("Wrong chain for tx") + errUnknownTxType = fmt.Errorf("Tx type unknown") + errInvalidFormat = fmt.Errorf("Invalid format") + errUnknownModule = fmt.Errorf("Unknown module") ) func ErrUnknownTxType(tx basecoin.Tx) TMError { diff --git a/errors/main.go b/errors/main.go index f19a7f692e..21f261148a 100644 --- a/errors/main.go +++ b/errors/main.go @@ -1,9 +1,5 @@ package errors -/** -* Copyright (C) 2017 Ethan Frey -**/ - import ( "fmt" @@ -23,6 +19,7 @@ type causer interface { Cause() error } +// TMError is the tendermint abci return type with stack trace type TMError interface { stackTracer ErrorCode() abci.CodeType diff --git a/modules/coin/errors.go b/modules/coin/errors.go index d9b2fea6c0..7b86f6343f 100644 --- a/modules/coin/errors.go +++ b/modules/coin/errors.go @@ -1,3 +1,4 @@ +//nolint package coin import ( diff --git a/modules/coin/genesis.go b/modules/coin/genesis.go index 7ce32cd8d2..4b26fa1e68 100644 --- a/modules/coin/genesis.go +++ b/modules/coin/genesis.go @@ -13,6 +13,7 @@ import ( /**** code to parse accounts from genesis docs ***/ +// GenesisAccount - genesis account parameters type GenesisAccount struct { Address data.Bytes `json:"address"` // this from types.Account (don't know how to embed this properly) @@ -21,6 +22,7 @@ type GenesisAccount struct { Balance types.Coins `json:"coins"` } +// ToAccount - GenesisAccount struct to a basecoin Account func (g GenesisAccount) ToAccount() Account { return Account{ Sequence: g.Sequence, @@ -28,6 +30,7 @@ func (g GenesisAccount) ToAccount() Account { } } +// GetAddr - Get the address of the genesis account func (g GenesisAccount) GetAddr() ([]byte, error) { noAddr, noPk := len(g.Address) == 0, g.PubKey.Empty() diff --git a/modules/coin/handler.go b/modules/coin/handler.go index def6689c29..9ed218a34a 100644 --- a/modules/coin/handler.go +++ b/modules/coin/handler.go @@ -12,24 +12,25 @@ import ( "github.com/tendermint/basecoin/types" ) -const ( - NameCoin = "coin" -) +//NameCoin - name space of the coin module +const NameCoin = "coin" -// Handler writes +// Handler includes an accountant type Handler struct { Accountant } var _ basecoin.Handler = Handler{} +// NewHandler - new accountant handler for the coin module func NewHandler() Handler { return Handler{ Accountant: NewAccountant(""), } } -func (_ Handler) Name() string { +// Name - return name space +func (h Handler) Name() string { return NameCoin } @@ -80,6 +81,7 @@ func (h Handler) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoi return basecoin.Result{}, nil } +// 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) @@ -103,10 +105,9 @@ func (h Handler) SetOption(l log.Logger, store types.KVStore, module, key, value } return "Success", nil - } else { - msg := fmt.Sprintf("Unknown key: %s", key) - return "", errors.ErrInternal(msg) } + msg := fmt.Sprintf("Unknown key: %s", key) + return "", errors.ErrInternal(msg) } func checkTx(ctx basecoin.Context, tx basecoin.Tx) (send SendTx, err error) { diff --git a/modules/coin/store.go b/modules/coin/store.go index 9490f302b3..b5d14ca850 100644 --- a/modules/coin/store.go +++ b/modules/coin/store.go @@ -10,10 +10,13 @@ import ( "github.com/tendermint/basecoin/types" ) +// Accountant - custom object to manage coins for the coin module +// TODO prefix should be post-fix if maintaining the same key space type Accountant struct { Prefix []byte } +// NewAccountant - create the new accountant with prefix information func NewAccountant(prefix string) Accountant { if prefix == "" { prefix = NameCoin @@ -23,6 +26,7 @@ func NewAccountant(prefix string) Accountant { } } +// GetAccount - Get account from store and address func (a Accountant) GetAccount(store types.KVStore, addr basecoin.Actor) (Account, error) { acct, err := loadAccount(store, a.MakeKey(addr)) @@ -68,7 +72,7 @@ func (a Accountant) updateCoins(store types.KVStore, addr basecoin.Actor, coins if seq != acct.Sequence+1 { return acct, ErrInvalidSequence() } - acct.Sequence += 1 + acct.Sequence++ } // check amount @@ -81,6 +85,8 @@ func (a Accountant) updateCoins(store types.KVStore, addr basecoin.Actor, coins return acct, nil } +// MakeKey - generate key bytes from address using accountant prefix +// TODO Prefix -> PostFix for consistent namespace func (a Accountant) MakeKey(addr basecoin.Actor) []byte { key := addr.Bytes() if len(a.Prefix) > 0 { @@ -89,6 +95,7 @@ func (a Accountant) MakeKey(addr basecoin.Actor) []byte { return key } +// Account - coin account structure type Account struct { Coins types.Coins `json:"coins"` Sequence int `json:"sequence"` diff --git a/modules/coin/tx.go b/modules/coin/tx.go index c5f7370c01..c6b9c66a73 100644 --- a/modules/coin/tx.go +++ b/modules/coin/tx.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/tendermint/basecoin" - "github.com/tendermint/basecoin/types" ) @@ -20,12 +19,14 @@ const ( //----------------------------------------------------------------------------- +// TxInput - expected coin movement outputs, used with SendTx type TxInput struct { Address basecoin.Actor `json:"address"` Coins types.Coins `json:"coins"` Sequence int `json:"sequence"` // Nonce: Must be 1 greater than the last committed TxInput } +// ValidateBasic - validate transaction input func (txIn TxInput) ValidateBasic() error { if txIn.Address.App == "" { return ErrInvalidAddress() @@ -50,6 +51,7 @@ func (txIn TxInput) String() string { return fmt.Sprintf("TxInput{%v,%v,%v}", txIn.Address, txIn.Coins, txIn.Sequence) } +// NewTxInput - create a transaction input, used with SendTx func NewTxInput(addr basecoin.Actor, coins types.Coins, sequence int) TxInput { input := TxInput{ Address: addr, @@ -61,11 +63,13 @@ func NewTxInput(addr basecoin.Actor, coins types.Coins, sequence int) TxInput { //----------------------------------------------------------------------------- +// TxOutput - expected coin movement output, used with SendTx type TxOutput struct { Address basecoin.Actor `json:"address"` Coins types.Coins `json:"coins"` } +// ValidateBasic - validate transaction output func (txOut TxOutput) ValidateBasic() error { if txOut.Address.App == "" { return ErrInvalidAddress() @@ -87,6 +91,7 @@ func (txOut TxOutput) String() string { return fmt.Sprintf("TxOutput{%X,%v}", txOut.Address, txOut.Coins) } +// NewTxOutput - create a transaction output, used with SendTx func NewTxOutput(addr basecoin.Actor, coins types.Coins) TxOutput { output := TxOutput{ Address: addr, @@ -97,6 +102,8 @@ func NewTxOutput(addr basecoin.Actor, coins types.Coins) TxOutput { //----------------------------------------------------------------------------- +// SendTx - high level transaction of the coin module +// Satisfies: TxInner type SendTx struct { Inputs []TxInput `json:"inputs"` Outputs []TxOutput `json:"outputs"` @@ -104,10 +111,12 @@ type SendTx struct { var _ basecoin.Tx = NewSendTx(nil, nil) +// NewSendTx - new SendTx func NewSendTx(in []TxInput, out []TxOutput) basecoin.Tx { return SendTx{Inputs: in, Outputs: out}.Wrap() } +// ValidateBasic - validate the send transaction func (tx SendTx) ValidateBasic() error { // this just makes sure all the inputs and outputs are properly formatted, // not that they actually have the money inside @@ -142,6 +151,7 @@ func (tx SendTx) String() string { return fmt.Sprintf("SendTx{%v->%v}", tx.Inputs, tx.Outputs) } +// Wrap - used to satisfy TxInner func (tx SendTx) Wrap() basecoin.Tx { return basecoin.Tx{tx} } diff --git a/modules/fee/errors.go b/modules/fee/errors.go index 0b6d7fcaf8..f29d3cbe16 100644 --- a/modules/fee/errors.go +++ b/modules/fee/errors.go @@ -1,3 +1,4 @@ +//nolint package fee import ( diff --git a/modules/fee/handler.go b/modules/fee/handler.go index f711cac953..7f4f579f07 100644 --- a/modules/fee/handler.go +++ b/modules/fee/handler.go @@ -7,10 +7,10 @@ import ( "github.com/tendermint/basecoin/types" ) -const ( - NameFee = "fee" -) +// NameFee - namespace for the fee module +const NameFee = "fee" +// AccountChecker - interface used by SimpleFeeHandler type AccountChecker interface { // Get amount checks the current amount GetAmount(store types.KVStore, addr basecoin.Actor) (types.Coins, error) @@ -20,13 +20,15 @@ type AccountChecker interface { ChangeAmount(store types.KVStore, addr basecoin.Actor, coins types.Coins) (types.Coins, error) } +// SimpleFeeHandler - checker object for fee checking type SimpleFeeHandler struct { AccountChecker MinFee types.Coins stack.PassOption } -func (_ SimpleFeeHandler) Name() string { +// Name - return the namespace for the fee module +func (SimpleFeeHandler) Name() string { return NameFee } @@ -34,6 +36,7 @@ var _ stack.Middleware = SimpleFeeHandler{} // Yes, I know refactor a bit... really too late already +// CheckTx - check the transaction func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) { feeTx, ok := tx.Unwrap().(*Fee) if !ok { @@ -57,6 +60,7 @@ func (h SimpleFeeHandler) CheckTx(ctx basecoin.Context, store types.KVStore, tx return basecoin.Result{Log: "Valid tx"}, nil } +// DeliverTx - send the fee handler transaction func (h SimpleFeeHandler) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) { feeTx, ok := tx.Unwrap().(*Fee) if !ok { diff --git a/version/version.go b/version/version.go index 1e25edb4a6..c89c513ace 100644 --- a/version/version.go +++ b/version/version.go @@ -1,3 +1,4 @@ +//nolint package version const Maj = "0"