Merge pull request #161 from tendermint/unstable-cleanup

Unstable cleanup
This commit is contained in:
Ethan Frey 2017-07-08 21:08:32 +02:00 committed by GitHub
commit ad94d060d8
17 changed files with 105 additions and 122 deletions

View File

@ -34,6 +34,8 @@ type Basecoin struct {
logger log.Logger logger log.Logger
} }
var _ abci.Application = &Basecoin{}
// NewBasecoin - create a new instance of the basecoin application // NewBasecoin - create a new instance of the basecoin application
func NewBasecoin(handler basecoin.Handler, eyesCli *eyes.Client, logger log.Logger) *Basecoin { func NewBasecoin(handler basecoin.Handler, eyesCli *eyes.Client, logger log.Logger) *Basecoin {
state := sm.NewState(eyesCli, logger.With("module", "state")) state := sm.NewState(eyesCli, logger.With("module", "state"))

View File

@ -1,23 +0,0 @@
package commands
import "encoding/hex"
// Returns true for non-empty hex-string prefixed with "0x"
func isHex(s string) bool {
if len(s) > 2 && s[:2] == "0x" {
_, err := hex.DecodeString(s[2:])
if err != nil {
return false
}
return true
}
return false
}
// StripHex remove the first two hex bytes
func StripHex(s string) string {
if isHex(s) {
return s[2:]
}
return s
}

View File

@ -1,24 +0,0 @@
package commands
import (
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHex(t *testing.T) {
assert := assert.New(t)
//test isHex
hexNoPrefix := hex.EncodeToString([]byte("foobar"))
hexWPrefix := "0x" + hexNoPrefix
str := "foobar"
strWPrefix := "0xfoobar"
assert.True(isHex(hexWPrefix), "isHex not identifying hex with 0x prefix")
assert.False(isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix")
assert.False(isHex(str), "isHex shouldn't identify non-hex string")
assert.False(isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix")
assert.True(StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters")
}

View File

@ -19,15 +19,15 @@ func (h holder) Unwrap() validate {
return h.validate return h.validate
} }
type DemoTx struct { type demoTx struct {
Age int Age int
} }
func (t DemoTx) Wrap() holder { func (t demoTx) Wrap() holder {
return holder{t} return holder{t}
} }
func (t DemoTx) ValidateBasic() error { func (t demoTx) ValidateBasic() error {
return nil return nil
} }
@ -45,7 +45,7 @@ func TestErrorMatches(t *testing.T) {
{errWrongChain, ErrWrongChain("hakz"), true}, {errWrongChain, ErrWrongChain("hakz"), true},
{errUnknownTxType, ErrUnknownTxType(holder{}), true}, {errUnknownTxType, ErrUnknownTxType(holder{}), true},
{errUnknownTxType, ErrUnknownTxType("some text here..."), true}, {errUnknownTxType, ErrUnknownTxType("some text here..."), true},
{errUnknownTxType, ErrUnknownTxType(DemoTx{5}.Wrap()), true}, {errUnknownTxType, ErrUnknownTxType(demoTx{5}.Wrap()), true},
} }
for i, tc := range cases { for i, tc := range cases {

View File

@ -1,5 +1,5 @@
/* /*
package auth contains generic Signable implementations that can be used Package auth contains generic Signable implementations that can be used
by your application or tests to handle authentication needs. by your application or tests to handle authentication needs.
It currently supports transaction data as opaque bytes and either single It currently supports transaction data as opaque bytes and either single
@ -71,14 +71,13 @@ func NewSig(tx basecoin.Tx) *OneSig {
return &OneSig{Tx: tx} return &OneSig{Tx: tx}
} }
//nolint
func (s *OneSig) Wrap() basecoin.Tx { func (s *OneSig) Wrap() basecoin.Tx {
return basecoin.Tx{s} return basecoin.Tx{s}
} }
func (s *OneSig) Next() basecoin.Tx { func (s *OneSig) Next() basecoin.Tx {
return s.Tx return s.Tx
} }
func (s *OneSig) ValidateBasic() error { func (s *OneSig) ValidateBasic() error {
return s.Tx.ValidateBasic() return s.Tx.ValidateBasic()
} }
@ -143,14 +142,13 @@ func NewMulti(tx basecoin.Tx) *MultiSig {
return &MultiSig{Tx: tx} return &MultiSig{Tx: tx}
} }
// nolint
func (s *MultiSig) Wrap() basecoin.Tx { func (s *MultiSig) Wrap() basecoin.Tx {
return basecoin.Tx{s} return basecoin.Tx{s}
} }
func (s *MultiSig) Next() basecoin.Tx { func (s *MultiSig) Next() basecoin.Tx {
return s.Tx return s.Tx
} }
func (s *MultiSig) ValidateBasic() error { func (s *MultiSig) ValidateBasic() error {
return s.Tx.ValidateBasic() return s.Tx.ValidateBasic()
} }
@ -204,6 +202,7 @@ func (s *MultiSig) Signers() ([]crypto.PubKey, error) {
return keys, nil return keys, nil
} }
// Sign - sign the transaction with private key
func Sign(tx keys.Signable, key crypto.PrivKey) error { func Sign(tx keys.Signable, key crypto.PrivKey) error {
msg := tx.SignBytes() msg := tx.SignBytes()
pubkey := key.PubKey() pubkey := key.PubKey()

View File

@ -22,18 +22,21 @@ func init() {
} }
/**** MultiTx ******/ /**** MultiTx ******/
// MultiTx - a transaction containing multiple transactions
type MultiTx struct { type MultiTx struct {
Txs []basecoin.Tx `json:"txs"` Txs []basecoin.Tx `json:"txs"`
} }
var _ basecoin.TxInner = &MultiTx{}
//nolint - TxInner Functions
func NewMultiTx(txs ...basecoin.Tx) basecoin.Tx { func NewMultiTx(txs ...basecoin.Tx) basecoin.Tx {
return (MultiTx{Txs: txs}).Wrap() return (MultiTx{Txs: txs}).Wrap()
} }
func (mt MultiTx) Wrap() basecoin.Tx { func (mt MultiTx) Wrap() basecoin.Tx {
return basecoin.Tx{mt} return basecoin.Tx{mt}
} }
func (mt MultiTx) ValidateBasic() error { func (mt MultiTx) ValidateBasic() error {
for _, t := range mt.Txs { for _, t := range mt.Txs {
err := t.ValidateBasic() err := t.ValidateBasic()
@ -52,14 +55,15 @@ type ChainTx struct {
ChainID string `json:"chain_id"` ChainID string `json:"chain_id"`
} }
var _ basecoin.TxInner = &ChainTx{}
//nolint - TxInner Functions
func NewChainTx(chainID string, tx basecoin.Tx) basecoin.Tx { func NewChainTx(chainID string, tx basecoin.Tx) basecoin.Tx {
return (ChainTx{Tx: tx, ChainID: chainID}).Wrap() return (ChainTx{Tx: tx, ChainID: chainID}).Wrap()
} }
func (c ChainTx) Wrap() basecoin.Tx { func (c ChainTx) Wrap() basecoin.Tx {
return basecoin.Tx{c} return basecoin.Tx{c}
} }
func (c ChainTx) ValidateBasic() error { func (c ChainTx) ValidateBasic() error {
// TODO: more checks? chainID? // TODO: more checks? chainID?
return c.Tx.ValidateBasic() return c.Tx.ValidateBasic()

View File

@ -118,10 +118,10 @@ func (coins Coins) IsValid() bool {
// //
// TODO: handle empty coins! // TODO: handle empty coins!
// Currently appends an empty coin ... // Currently appends an empty coin ...
func (coinsA Coins) Plus(coinsB Coins) Coins { func (coins Coins) Plus(coinsB Coins) Coins {
sum := []Coin{} sum := []Coin{}
indexA, indexB := 0, 0 indexA, indexB := 0, 0
lenA, lenB := len(coinsA), len(coinsB) lenA, lenB := len(coins), len(coinsB)
for { for {
if indexA == lenA { if indexA == lenA {
if indexB == lenB { if indexB == lenB {
@ -129,9 +129,9 @@ func (coinsA Coins) Plus(coinsB Coins) Coins {
} }
return append(sum, coinsB[indexB:]...) return append(sum, coinsB[indexB:]...)
} else if indexB == lenB { } else if indexB == lenB {
return append(sum, coinsA[indexA:]...) return append(sum, coins[indexA:]...)
} }
coinA, coinB := coinsA[indexA], coinsB[indexB] coinA, coinB := coins[indexA], coinsB[indexB]
switch strings.Compare(coinA.Denom, coinB.Denom) { switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1: case -1:
sum = append(sum, coinA) sum = append(sum, coinA)
@ -168,15 +168,15 @@ func (coins Coins) Negative() Coins {
} }
// Minus subtracts a set of coins from another (adds the inverse) // Minus subtracts a set of coins from another (adds the inverse)
func (coinsA Coins) Minus(coinsB Coins) Coins { func (coins Coins) Minus(coinsB Coins) Coins {
return coinsA.Plus(coinsB.Negative()) return coins.Plus(coinsB.Negative())
} }
// IsGTE returns True iff coinsA is NonNegative(), and for every // IsGTE returns True iff coins is NonNegative(), and for every
// currency in coinsB, the currency is present at an equal or greater // currency in coinsB, the currency is present at an equal or greater
// amount in coinsB // amount in coinsB
func (coinsA Coins) IsGTE(coinsB Coins) bool { func (coins Coins) IsGTE(coinsB Coins) bool {
diff := coinsA.Minus(coinsB) diff := coins.Minus(coinsB)
if len(diff) == 0 { if len(diff) == 0 {
return true return true
} }
@ -189,12 +189,12 @@ func (coins Coins) IsZero() bool {
} }
// IsEqual returns true if the two sets of Coins have the same value // IsEqual returns true if the two sets of Coins have the same value
func (coinsA Coins) IsEqual(coinsB Coins) bool { func (coins Coins) IsEqual(coinsB Coins) bool {
if len(coinsA) != len(coinsB) { if len(coins) != len(coinsB) {
return false return false
} }
for i := 0; i < len(coinsA); i++ { for i := 0; i < len(coins); i++ {
if coinsA[i] != coinsB[i] { if coins[i] != coinsB[i] {
return false return false
} }
} }

View File

@ -18,6 +18,8 @@ func init() {
/**** Fee ****/ /**** Fee ****/
var _ basecoin.TxLayer = &Fee{}
// Fee attaches a fee payment to the embedded tx // Fee attaches a fee payment to the embedded tx
type Fee struct { type Fee struct {
Tx basecoin.Tx `json:"tx"` Tx basecoin.Tx `json:"tx"`
@ -26,19 +28,17 @@ type Fee struct {
// Gas coin.Coin `json:"gas"` // ????? // Gas coin.Coin `json:"gas"` // ?????
} }
// nolint - TxInner Functions
func NewFee(tx basecoin.Tx, fee coin.Coin, payer basecoin.Actor) basecoin.Tx { func NewFee(tx basecoin.Tx, fee coin.Coin, payer basecoin.Actor) basecoin.Tx {
return (&Fee{Tx: tx, Fee: fee, Payer: payer}).Wrap() return (&Fee{Tx: tx, Fee: fee, Payer: payer}).Wrap()
} }
func (f *Fee) ValidateBasic() error { func (f *Fee) ValidateBasic() error {
// TODO: more checks // TODO: more checks
return f.Tx.ValidateBasic() return f.Tx.ValidateBasic()
} }
func (f *Fee) Wrap() basecoin.Tx { func (f *Fee) Wrap() basecoin.Tx {
return basecoin.Tx{f} return basecoin.Tx{f}
} }
func (f *Fee) Next() basecoin.Tx { func (f *Fee) Next() basecoin.Tx {
return f.Tx return f.Tx
} }

View File

@ -4,6 +4,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/tendermint/basecoin/tests" "github.com/tendermint/basecoin/tests"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
) )

View File

@ -9,11 +9,11 @@ import (
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tendermint/rpc/lib/client"
"github.com/tendermint/tendermint/rpc/lib/types"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
_ "github.com/tendermint/tendermint/rpc/core/types" // Register RPCResponse > Result types _ "github.com/tendermint/tendermint/rpc/core/types" // Register RPCResponse > Result types
"github.com/tendermint/tendermint/rpc/lib/client"
"github.com/tendermint/tendermint/rpc/lib/types"
cmn "github.com/tendermint/tmlibs/common"
) )
func main() { func main() {

View File

@ -23,6 +23,7 @@ type secureContext struct {
log.Logger log.Logger
} }
// NewContext - create a new secureContext
func NewContext(chain string, logger log.Logger) basecoin.Context { func NewContext(chain string, logger log.Logger) basecoin.Context {
return secureContext{ return secureContext{
id: nonce(rand.Int63()), id: nonce(rand.Int63()),

View File

@ -33,10 +33,15 @@ type RawTx struct {
data.Bytes data.Bytes
} }
var _ basecoin.TxInner = RawTx{}
// nolint
func NewRawTx(d []byte) basecoin.Tx {
return RawTx{data.Bytes(d)}.Wrap()
}
func (r RawTx) Wrap() basecoin.Tx { func (r RawTx) Wrap() basecoin.Tx {
return basecoin.Tx{r} return basecoin.Tx{r}
} }
func (r RawTx) ValidateBasic() error { func (r RawTx) ValidateBasic() error {
if len(r.Bytes) > rawMaxSize { if len(r.Bytes) > rawMaxSize {
return errors.ErrTooLarge() return errors.ErrTooLarge()
@ -44,10 +49,6 @@ func (r RawTx) ValidateBasic() error {
return nil return nil
} }
func NewRawTx(d []byte) basecoin.Tx {
return RawTx{data.Bytes(d)}.Wrap()
}
// OKHandler just used to return okay to everything // OKHandler just used to return okay to everything
type OKHandler struct { type OKHandler struct {
Log string Log string
@ -56,7 +57,8 @@ type OKHandler struct {
var _ basecoin.Handler = OKHandler{} var _ basecoin.Handler = OKHandler{}
func (_ OKHandler) Name() string { // Name - return handler's name
func (OKHandler) Name() string {
return NameOK return NameOK
} }
@ -77,18 +79,19 @@ type EchoHandler struct {
var _ basecoin.Handler = EchoHandler{} var _ basecoin.Handler = EchoHandler{}
func (_ EchoHandler) Name() string { // Name - return handler's name
func (EchoHandler) Name() string {
return NameEcho return NameEcho
} }
// CheckTx always returns an empty success tx // CheckTx always returns an empty success tx
func (_ EchoHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (EchoHandler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
data, err := data.ToWire(tx) data, err := data.ToWire(tx)
return basecoin.Result{Data: data}, err return basecoin.Result{Data: data}, err
} }
// DeliverTx always returns an empty success tx // DeliverTx always returns an empty success tx
func (_ EchoHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) { func (EchoHandler) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
data, err := data.ToWire(tx) data, err := data.ToWire(tx)
return basecoin.Result{Data: data}, err return basecoin.Result{Data: data}, err
} }
@ -101,7 +104,8 @@ type FailHandler struct {
var _ basecoin.Handler = FailHandler{} var _ basecoin.Handler = FailHandler{}
func (_ FailHandler) Name() string { // Name - return handler's name
func (FailHandler) Name() string {
return NameFail return NameFail
} }
@ -124,7 +128,8 @@ type PanicHandler struct {
var _ basecoin.Handler = PanicHandler{} var _ basecoin.Handler = PanicHandler{}
func (_ PanicHandler) Name() string { // Name - return handler's name
func (PanicHandler) Name() string {
return NamePanic return NamePanic
} }

View File

@ -1,3 +1,4 @@
//nolint
package stack package stack
import ( import (

View File

@ -1,3 +1,4 @@
//nolint
package stack package stack
import ( import (
@ -19,57 +20,70 @@ type Middleware interface {
} }
type CheckerMiddle interface { type CheckerMiddle interface {
CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) CheckTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error)
} }
type CheckerMiddleFunc func(basecoin.Context, state.KVStore, basecoin.Tx, basecoin.Checker) (basecoin.Result, error) type CheckerMiddleFunc func(basecoin.Context, state.KVStore,
basecoin.Tx, basecoin.Checker) (basecoin.Result, error)
func (c CheckerMiddleFunc) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) { func (c CheckerMiddleFunc) CheckTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) {
return c(ctx, store, tx, next) return c(ctx, store, tx, next)
} }
type DeliverMiddle interface { type DeliverMiddle interface {
DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx,
next basecoin.Deliver) (basecoin.Result, error)
} }
type DeliverMiddleFunc func(basecoin.Context, state.KVStore, basecoin.Tx, basecoin.Deliver) (basecoin.Result, error) type DeliverMiddleFunc func(basecoin.Context, state.KVStore,
basecoin.Tx, basecoin.Deliver) (basecoin.Result, error)
func (d DeliverMiddleFunc) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) { func (d DeliverMiddleFunc) DeliverTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) {
return d(ctx, store, tx, next) return d(ctx, store, tx, next)
} }
type SetOptionMiddle interface { type SetOptionMiddle interface {
SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) SetOption(l log.Logger, store state.KVStore, module,
key, value string, next basecoin.SetOptioner) (string, error)
} }
type SetOptionMiddleFunc func(log.Logger, state.KVStore, string, string, string, basecoin.SetOptioner) (string, error) type SetOptionMiddleFunc func(log.Logger, state.KVStore,
string, string, string, basecoin.SetOptioner) (string, error)
func (c SetOptionMiddleFunc) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) { func (c SetOptionMiddleFunc) SetOption(l log.Logger, store state.KVStore,
module, key, value string, next basecoin.SetOptioner) (string, error) {
return c(l, store, module, key, value, next) return c(l, store, module, key, value, next)
} }
// holders // holders
type PassCheck struct{} type PassCheck struct{}
func (_ PassCheck) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) { func (_ PassCheck) CheckTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, next basecoin.Checker) (basecoin.Result, error) {
return next.CheckTx(ctx, store, tx) return next.CheckTx(ctx, store, tx)
} }
type PassDeliver struct{} type PassDeliver struct{}
func (_ PassDeliver) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) { func (_ PassDeliver) DeliverTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, next basecoin.Deliver) (basecoin.Result, error) {
return next.DeliverTx(ctx, store, tx) return next.DeliverTx(ctx, store, tx)
} }
type PassOption struct{} type PassOption struct{}
func (_ PassOption) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) { func (_ PassOption) SetOption(l log.Logger, store state.KVStore, module,
key, value string, next basecoin.SetOptioner) (string, error) {
return next.SetOption(l, store, module, key, value) return next.SetOption(l, store, module, key, value)
} }
type NopOption struct{} type NopOption struct{}
func (_ NopOption) SetOption(l log.Logger, store state.KVStore, module, key, value string, next basecoin.SetOptioner) (string, error) { func (_ NopOption) SetOption(l log.Logger, store state.KVStore, module,
key, value string, next basecoin.SetOptioner) (string, error) {
return "", nil return "", nil
} }
@ -98,14 +112,17 @@ func (w wrapped) Name() string {
return w.h.Name() return w.h.Name()
} }
func (w wrapped) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, _ basecoin.Checker) (basecoin.Result, error) { func (w wrapped) CheckTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, _ basecoin.Checker) (basecoin.Result, error) {
return w.h.CheckTx(ctx, store, tx) return w.h.CheckTx(ctx, store, tx)
} }
func (w wrapped) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, _ basecoin.Deliver) (basecoin.Result, error) { func (w wrapped) DeliverTx(ctx basecoin.Context, store state.KVStore,
tx basecoin.Tx, _ basecoin.Deliver) (basecoin.Result, error) {
return w.h.DeliverTx(ctx, store, tx) return w.h.DeliverTx(ctx, store, tx)
} }
func (w wrapped) SetOption(l log.Logger, store state.KVStore, module, key, value string, _ basecoin.SetOptioner) (string, error) { func (w wrapped) SetOption(l log.Logger, store state.KVStore,
module, key, value string, _ basecoin.SetOptioner) (string, error) {
return w.h.SetOption(l, store, module, key, value) return w.h.SetOption(l, store, module, key, value)
} }