From f4b7a6ce4063fd704a39af59f1a53d4c53f11750 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 25 Oct 2017 19:54:42 +0200 Subject: [PATCH] Updated tx handling for eyes server --- examples/eyes/cmd/eyes/main.go | 9 +++++++-- examples/eyes/parser.go | 32 ++++++++++++++++++++++++++++++++ examples/eyes/tx.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 examples/eyes/parser.go create mode 100644 examples/eyes/tx.go diff --git a/examples/eyes/cmd/eyes/main.go b/examples/eyes/cmd/eyes/main.go index 389950d0a..df30fc921 100644 --- a/examples/eyes/cmd/eyes/main.go +++ b/examples/eyes/cmd/eyes/main.go @@ -8,9 +8,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk" client "github.com/cosmos/cosmos-sdk/client/commands" - "github.com/cosmos/cosmos-sdk/modules/eyes" + eyesmod "github.com/cosmos/cosmos-sdk/modules/eyes" "github.com/cosmos/cosmos-sdk/server/commands" "github.com/cosmos/cosmos-sdk/util" + + "github.com/cosmos/cosmos-sdk/examples/eyes" ) // RootCmd is the entry point for this binary @@ -25,8 +27,11 @@ func BuildApp() sdk.Handler { return sdk.ChainDecorators( util.Logger{}, util.Recovery{}, + eyes.Parser{}, + util.Chain{}, ).WithHandler( - eyes.NewHandler()) + eyesmod.NewHandler(), + ) } func main() { diff --git a/examples/eyes/parser.go b/examples/eyes/parser.go new file mode 100644 index 000000000..b8dd908eb --- /dev/null +++ b/examples/eyes/parser.go @@ -0,0 +1,32 @@ +package eyes + +import sdk "github.com/cosmos/cosmos-sdk" + +// Parser converts bytes into a tx struct +type Parser struct{} + +var _ sdk.Decorator = Parser{} + +// CheckTx makes sure we are on the proper chain +// - fulfills Decorator interface +func (c Parser) CheckTx(ctx sdk.Context, store sdk.SimpleDB, + txBytes interface{}, next sdk.Checker) (res sdk.CheckResult, err error) { + + tx, err := LoadTx(txBytes.([]byte)) + if err != nil { + return res, err + } + return next.CheckTx(ctx, store, tx) +} + +// DeliverTx makes sure we are on the proper chain +// - fulfills Decorator interface +func (c Parser) DeliverTx(ctx sdk.Context, store sdk.SimpleDB, + txBytes interface{}, next sdk.Deliverer) (res sdk.DeliverResult, err error) { + + tx, err := LoadTx(txBytes.([]byte)) + if err != nil { + return res, err + } + return next.DeliverTx(ctx, store, tx) +} diff --git a/examples/eyes/tx.go b/examples/eyes/tx.go new file mode 100644 index 000000000..41db19504 --- /dev/null +++ b/examples/eyes/tx.go @@ -0,0 +1,33 @@ +package eyes + +import ( + wire "github.com/tendermint/go-wire" + + eyesmod "github.com/cosmos/cosmos-sdk/modules/eyes" + "github.com/cosmos/cosmos-sdk/util" +) + +// Tx is what is submitted to the chain. +// This embeds the tx data along with any info we want for +// decorators (just chain for now to demo) +type Tx struct { + Tx eyesmod.EyesTx `json:"tx"` + Chain util.ChainData `json:"chain"` +} + +// GetTx gets the tx info +func (e Tx) GetTx() interface{} { + return e.Tx +} + +// GetChain gets the chain we wish to perform the tx on +// (info for decorators) +func (e Tx) GetChain() util.ChainData { + return e.Chain +} + +// LoadTx parses the input data into our blockchain tx structure +func LoadTx(data []byte) (tx Tx, err error) { + err = wire.ReadBinaryBytes(data, &tx) + return +}