Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coin #312

Merged
merged 10 commits into from
Dec 21, 2017
Merged

Coin #312

Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package errors

import "fmt"
import (
"fmt"

"github.com/pkg/errors"
)

const (
// ABCI Response Codes
CodeInternalError = 1
CodeTxParseError = 2
CodeBadNonce = 3
CodeUnauthorized = 4
CodeInsufficientFunds = 5
CodeUnknownRequest = 6
CodeInternalError uint32 = 1
CodeTxParseError = 2
CodeBadNonce = 3
CodeUnauthorized = 4
CodeInsufficientFunds = 5
CodeUnknownRequest = 6
)

// NOTE: Don't stringer this, we'll put better messages in later.
Expand Down Expand Up @@ -65,6 +69,8 @@ func UnknownRequest(log string) sdkError {
type ABCIError interface {
ABCICode() uint32
ABCILog() string

Error() string
}

/*
Expand Down Expand Up @@ -132,3 +138,24 @@ func (err sdkError) WithCause(cause error) sdkError {
copy.cause = cause
return copy
}

// HasErrorCode checks if this error would return the named error code
func HasErrorCode(err error, code uint32) bool {
// XXX Get the cause if not ABCIError
if abciErr, ok := err.(ABCIError); ok {
return abciErr.ABCICode() == code
}
return code == CodeInternalError
}

func IsSameError(pattern error, err error) bool {
return err != nil && (errors.Cause(err) == errors.Cause(pattern))
}

func WithCode(err error, code uint32) sdkError {
return sdkError{
code: code,
cause: err,
log: "",
}
}
2 changes: 1 addition & 1 deletion exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ type (
Decorator = types.Decorator

// Type aliases for other modules.
MultiStore = store.MultiStore modules.
MultiStore = store.MultiStore
)
2 changes: 1 addition & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"context"

abci "github.com/tendermint/abci/types"
)

Expand All @@ -11,12 +12,12 @@ type Context struct {
// it's probably not what you want to do.
}

func NewContext(header tm.Header, isCheckTx bool, txBytes []byte) Context {
func NewContext(header abci.Header, isCheckTx bool, txBytes []byte) Context {
c := Context{
Context: context.Background(),
}
c = c.setBlockHeader(header)
c = c.setBlockHeight(int64(header.Height))
c = c.setBlockHeight(header.Height)
c = c.setChainID(header.ChainID)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL. Blame protobuf. I don't know if we can force the name change, maybe gogo lets us do that too ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh haha you did it! #hero

c = c.setIsCheckTx(isCheckTx)
c = c.setTxBytes(txBytes)
Expand Down Expand Up @@ -48,8 +49,8 @@ const (
contextKeyTxBytes
)

func (c Context) BlockHeader() tm.Header {
return c.Value(contextKeyBlockHeader).(tm.Header)
func (c Context) BlockHeader() abci.Header {
return c.Value(contextKeyBlockHeader).(abci.Header)
}

func (c Context) BlockHeight() int64 {
Expand All @@ -69,18 +70,18 @@ func (c Context) TxBytes() []byte {
}

// Unexposed to prevent overriding.
func (c Context) setBlockHeader(header tm.Header) Context {
func (c Context) setBlockHeader(header abci.Header) Context {
return c.WithValueSDK(contextKeyBlockHeader, header)
}

// Unexposed to prevent overriding.
func (c Context) setBlockHeight(height int64) Context {
return c.WithValueSDK(contextKeyBlockHeight, header)
return c.WithValueSDK(contextKeyBlockHeight, height)
}

// Unexposed to prevent overriding.
func (c Context) setChainID(chainID string) Context {
return c.WithValueSDK(contextKeyChainID, header)
return c.WithValueSDK(contextKeyChainID, chainID)
}

// Unexposed to prevent overriding.
Expand Down
8 changes: 6 additions & 2 deletions types/decorators.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package types

import (
"github.com/cosmos/cosmos-sdk/store"
)

// A Decorator executes before/during/after a handler to enhance functionality.
type Decorator func(ctx Context, ms MultiStore, tx Tx, next Handler) Result
type Decorator func(ctx Context, ms store.MultiStore, tx Tx, next Handler) Result

// Return a decorated handler
func Decorate(dec Decorator, next Handler) Handler {
return func(ctx Context, ms MultiStore, tx Tx) Result {
return func(ctx Context, ms store.MultiStore, tx Tx) Result {
return dec(ctx, ms, tx, next)
}
}
Expand Down
39 changes: 39 additions & 0 deletions types/decorators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
"testing"

"github.com/cosmos/cosmos-sdk/store"
"github.com/stretchr/testify/assert"
)

func TestDecorate(t *testing.T) {

var calledDec1, calledDec2, calledHandler bool
dec1 := func(ctx Context, ms store.MultiStore, tx Tx, next Handler) Result {
calledDec1 = true
next(ctx, ms, tx)
return Result{}
}

dec2 := func(ctx Context, ms store.MultiStore, tx Tx, next Handler) Result {
calledDec2 = true
next(ctx, ms, tx)
return Result{}
}

handler := func(ctx Context, ms store.MultiStore, tx Tx) Result {
calledHandler = true
return Result{}
}

decoratedHandler := ChainDecorators(dec1, dec2).WithHandler(handler)

var ctx Context
var ms store.MultiStore
var tx Tx
decoratedHandler(ctx, ms, tx)
assert.True(t, calledDec1)
assert.True(t, calledDec2)
assert.True(t, calledHandler)
}
4 changes: 2 additions & 2 deletions types/handler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package types

import (
"github.com/cosmos/cosmos-sdk"
sdk "github.com/cosmos/cosmos-sdk"
)

// Handler handles both ABCI DeliverTx and CheckTx requests.
// Iff ABCI.CheckTx, ctx.IsCheckTx() returns true.
type Handler func(ctx Context, ms MultiStore, tx Tx)
type Handler func(ctx Context, ms sdk.MultiStore, tx Tx) Result
17 changes: 1 addition & 16 deletions types/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,7 @@ package types

import crypto "github.com/tendermint/go-crypto"

type Signature interface {
CryptoSig() crypto.Signature
Sequence() int
}

// StdSignature is a simple way to prevent replay attacks.
// There must be better strategies, but this is simplest.
type StdSignature struct {
crypto.Signature
Sequence int
}

func (ss StdSignature) CryptoSig() crypto.Signature {
return ss.Signature
}

func (ss StdSignature) Sequence() int {
return ss.Sequence
Sequence int64
}
2 changes: 1 addition & 1 deletion types/tx_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ type Tx interface {
// CONTRACT: If the signature is missing (ie the Msg is
// invalid), then the corresponding signature is
// .Empty().
Signatures() []Signature
Signatures() []StdSignature
}
10 changes: 5 additions & 5 deletions x/auth/context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/types"
)

/*
Expand All @@ -13,7 +13,7 @@ import (
var acc accounts.Account

accounts.SetAccount(ctx, acc)
acc2, ok := accounts.GetAccount(ctx)
acc2 := accounts.GetAccount(ctx)

*/

Expand All @@ -24,10 +24,10 @@ const (
contextKeyAccount contextKey = iota
)

func SetAccount(ctx sdk.Context, account Account) sdk.Context {
return ctx.WithValue(contextKeyAccount, account)
func SetAccount(ctx types.Context, account Account) types.Context {
return ctx.WithValueSDK(contextKeyAccount, account)
}

func GetAccount(ctx sdk.Context) (Account, bool) {
func GetAccount(ctx types.Context) Account {
return ctx.Value(contextKeyAccount).(Account)
}
35 changes: 0 additions & 35 deletions x/auth/decorator.go

This file was deleted.

File renamed without changes.
Loading