From b4275640b1b4005fdd91da47fd9249591f5134b8 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 18:58:40 +0200 Subject: [PATCH 01/17] Create BlockInfo struct, use standard context.Context --- context.go | 165 +++++++++++++++++++---------------------------------- handler.go | 11 ++-- time.go | 48 ---------------- 3 files changed, 64 insertions(+), 160 deletions(-) diff --git a/context.go b/context.go index df8e184d..45d0aede 100644 --- a/context.go +++ b/context.go @@ -23,8 +23,6 @@ to avoid lower-level modules overwriting the value package weave import ( - "context" - "fmt" "regexp" "time" @@ -33,17 +31,6 @@ import ( "github.com/tendermint/tendermint/libs/log" ) -type contextKey int // local to the weave module - -const ( - contextKeyHeader contextKey = iota - contextKeyHeight - contextKeyChainID - contextKeyLogger - contextKeyTime - contextCommitInfo -) - var ( // DefaultLogger is used for all context that have not // set anything themselves @@ -53,122 +40,86 @@ var ( IsValidChainID = regexp.MustCompile(`^[a-zA-Z0-9_\-]{6,20}$`).MatchString ) -// Context is just an alias for the standard implementation. -// We use functions to extend it to our domain -type Context = context.Context +type BlockInfo struct { + header abci.Header + commitInfo CommitInfo + chainID string + logger log.Logger +} -// WithHeader sets the block header for the Context. -// panics if called with header already set -func WithHeader(ctx Context, header abci.Header) Context { - if _, ok := GetHeader(ctx); ok { - panic("Header already set") +// NewBlockInfo creates a BlockInfo struct with current context of where it is being executed +func NewBlockInfo(header abci.Header, commitInfo CommitInfo, chainID string, logger log.Logger) (BlockInfo, error) { + if !IsValidChainID(chainID) { + return BlockInfo{}, errors.Wrap(errors.ErrInput, "chainID invalid") + } + if logger == nil { + logger = DefaultLogger } - return context.WithValue(ctx, contextKeyHeader, header) + return BlockInfo{ + header: header, + commitInfo: commitInfo, + chainID: chainID, + logger: logger, + }, nil } -// GetHeader returns the current block header -// ok is false if no header set in this Context -func GetHeader(ctx Context) (abci.Header, bool) { - val, ok := ctx.Value(contextKeyHeader).(abci.Header) - return val, ok +func (b BlockInfo) Header() abci.Header { + return b.header } -// WithCommitInfo sets the info on who signed the block in this Context. -// Panics if already set. -func WithCommitInfo(ctx Context, info CommitInfo) Context { - if _, ok := GetCommitInfo(ctx); ok { - panic("CommitInfo already set") - } - return context.WithValue(ctx, contextCommitInfo, info) +func (b BlockInfo) CommitInfo() CommitInfo { + return b.commitInfo } -// GetCommitInfo returns the info on validators that signed -// this block. Returns false if not present. -func GetCommitInfo(ctx Context) (CommitInfo, bool) { - val, ok := ctx.Value(contextCommitInfo).(CommitInfo) - return val, ok +func (b BlockInfo) ChainID() string { + return b.chainID } -// WithHeight sets the block height for the Context. -// panics if called with height already set -func WithHeight(ctx Context, height int64) Context { - if _, ok := GetHeight(ctx); ok { - panic("Height already set") - } - return context.WithValue(ctx, contextKeyHeight, height) +func (b BlockInfo) Height() int64 { + return b.header.Height } -// GetHeight returns the current block height -// ok is false if no height set in this Context -func GetHeight(ctx Context) (int64, bool) { - val, ok := ctx.Value(contextKeyHeight).(int64) - return val, ok +func (b BlockInfo) BlockTime() time.Time { + return b.header.Time } -// WithBlockTime sets the block time for the context. Block time is always -// represented in UTC. -func WithBlockTime(ctx Context, t time.Time) Context { - return context.WithValue(ctx, contextKeyTime, t.UTC()) +func (b BlockInfo) UnixTime() UnixTime { + return AsUnixTime(b.header.Time) } -// BlockTime returns current block wall clock time as declared in the context. -// An error is returned if a block time is not present in the context or if the -// zero time value is found. -func BlockTime(ctx Context) (time.Time, error) { - val, ok := ctx.Value(contextKeyTime).(time.Time) - if !ok { - return time.Time{}, errors.Wrap(errors.ErrHuman, "block time not present in the context") - } - if val.IsZero() { - // This is a special case when a zero time value was attached - // to the context. Even though it is present it is not a valid - // value. - return val, errors.Wrap(errors.ErrHuman, "zero value block time in the context") - } - return val, nil +func (b BlockInfo) Logger() log.Logger { + return b.logger } -// WithChainID sets the chain id for the Context. -// panics if called with chain id already set -func WithChainID(ctx Context, chainID string) Context { - if ctx.Value(contextKeyChainID) != nil { - panic("Chain ID already set") - } - if !IsValidChainID(chainID) { - panic(fmt.Sprintf("Invalid chain ID: %s", chainID)) - } - return context.WithValue(ctx, contextKeyChainID, chainID) -} - -// GetChainID returns the current chain id -// panics if chain id not already set (should never happen) -func GetChainID(ctx Context) string { - if x := ctx.Value(contextKeyChainID); x == nil { - panic("Chain id is not in context") - } - return ctx.Value(contextKeyChainID).(string) +// WithLogInfo accepts keyvalue pairs, and returns another +// context like this, after passing all the keyvals to the +// Logger +func (b BlockInfo) WithLogInfo(keyvals ...interface{}) BlockInfo { + b.logger = b.logger.With(keyvals...) + return b } -// WithLogger sets the logger for this Context -func WithLogger(ctx Context, logger log.Logger) Context { - // Logger can be overridden below... no problem - return context.WithValue(ctx, contextKeyLogger, logger) +// IsExpired returns true if given time is in the past as compared to the "now" +// as declared for the block. Expiration is inclusive, meaning that if current +// time is equal to the expiration time than this function returns true. +func (b BlockInfo) IsExpired(t UnixTime) bool { + return t <= b.UnixTime() } -// GetLogger returns the currently set logger, or -// DefaultLogger if none was set -func GetLogger(ctx Context) log.Logger { - val, ok := ctx.Value(contextKeyLogger).(log.Logger) - if !ok { - return DefaultLogger - } - return val +// InThePast returns true if given time is in the past compared to the current +// time as declared in the context. Context "now" should come from the block +// header. +// Keep in mind that this function is not inclusive of current time. It given +// time is equal to "now" then this function returns false. +func (b BlockInfo) InThePast(t time.Time) bool { + return t.Before(b.BlockTime()) } -// WithLogInfo accepts keyvalue pairs, and returns another -// context like this, after passing all the keyvals to the -// Logger -func WithLogInfo(ctx Context, keyvals ...interface{}) Context { - logger := GetLogger(ctx).With(keyvals...) - return WithLogger(ctx, logger) +// InTheFuture returns true if given time is in the future compared to the +// current time as declared in the context. Context "now" should come from the +// block header. +// Keep in mind that this function is not inclusive of current time. It given +// time is equal to "now" then this function returns false. +func (b BlockInfo) InTheFuture(t time.Time) bool { + return t.After(b.BlockTime()) } diff --git a/handler.go b/handler.go index 04709794..75877323 100644 --- a/handler.go +++ b/handler.go @@ -1,6 +1,7 @@ package weave import ( + "context" "encoding/json" abci "github.com/tendermint/tendermint/abci/types" @@ -17,27 +18,27 @@ type Handler interface { // It is its own interface to allow better type controls in the next // arguments in Decorator type Checker interface { - Check(ctx Context, store KVStore, tx Tx) (*CheckResult, error) + Check(ctx context.Context, store KVStore, tx Tx) (*CheckResult, error) } // Deliverer is a subset of Handler to execute a transaction. // It is its own interface to allow better type controls in the next // arguments in Decorator type Deliverer interface { - Deliver(ctx Context, store KVStore, tx Tx) (*DeliverResult, error) + Deliver(ctx context.Context, store KVStore, tx Tx) (*DeliverResult, error) } // Decorator wraps a Handler to provide common functionality // like authentication, or fee-handling, to many Handlers type Decorator interface { - Check(ctx Context, store KVStore, tx Tx, next Checker) (*CheckResult, error) - Deliver(ctx Context, store KVStore, tx Tx, next Deliverer) (*DeliverResult, error) + Check(ctx context.Context, store KVStore, tx Tx, next Checker) (*CheckResult, error) + Deliver(ctx context.Context, store KVStore, tx Tx, next Deliverer) (*DeliverResult, error) } // Ticker is a method that is called the beginning of every block, // which can be used to perform periodic or delayed tasks type Ticker interface { - Tick(ctx Context, store KVStore) (TickResult, error) + Tick(ctx context.Context, store KVStore) (TickResult, error) } // Registry is an interface to register your handler, diff --git a/time.go b/time.go index cfc29682..3b9a0dd5 100644 --- a/time.go +++ b/time.go @@ -1,7 +1,6 @@ package weave import ( - "context" "encoding/json" "fmt" "time" @@ -94,53 +93,6 @@ func (t UnixTime) String() string { return t.Time().UTC().String() } -// IsExpired returns true if given time is in the past as compared to the "now" -// as declared for the block. Expiration is inclusive, meaning that if current -// time is equal to the expiration time than this function returns true. -// -// This function panic if the block time is not provided in the context. This -// must never happen. The panic is here to prevent from broken setup to be -// processing data incorrectly. -func IsExpired(ctx Context, t UnixTime) bool { - blockNow, err := BlockTime(ctx) - if err != nil { - panic(fmt.Sprintf("%+v", err)) - } - return t <= AsUnixTime(blockNow) -} - -// InThePast returns true if given time is in the past compared to the current -// time as declared in the context. Context "now" should come from the block -// header. -// Keep in mind that this function is not inclusive of current time. It given -// time is equal to "now" then this function returns false. -// This function panic if the block time is not provided in the context. This -// must never happen. The panic is here to prevent from broken setup to be -// processing data incorrectly. -func InThePast(ctx context.Context, t time.Time) bool { - now, err := BlockTime(ctx) - if err != nil { - panic(fmt.Sprintf("%+v", err)) - } - return t.Before(now) -} - -// InTheFuture returns true if given time is in the future compared to the -// current time as declared in the context. Context "now" should come from the -// block header. -// Keep in mind that this function is not inclusive of current time. It given -// time is equal to "now" then this function returns false. -// This function panic if the block time is not provided in the context. This -// must never happen. The panic is here to prevent from broken setup to be -// processing data incorrectly. -func InTheFuture(ctx context.Context, t time.Time) bool { - now, err := BlockTime(ctx) - if err != nil { - panic(fmt.Sprintf("%+v", err)) - } - return t.After(now) -} - // UnixDuration represents a time duration with granularity of a second. This // type should be used mostly for protobuf message declarations. type UnixDuration int32 From 3f7482bff8222d214955d340e6346127d956434e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:22:38 +0200 Subject: [PATCH 02/17] Rename context.go to blockinfo.go and fix tests --- context.go => blockinfo.go | 21 ++++-------- blockinfo_test.go | 65 +++++++++++++++++++++++++++++++++++++ context_test.go | 66 -------------------------------------- time_test.go | 62 +++++++++++++---------------------- 4 files changed, 94 insertions(+), 120 deletions(-) rename context.go => blockinfo.go (84%) create mode 100644 blockinfo_test.go delete mode 100644 context_test.go diff --git a/context.go b/blockinfo.go similarity index 84% rename from context.go rename to blockinfo.go index 45d0aede..0a4abbad 100644 --- a/context.go +++ b/blockinfo.go @@ -4,21 +4,12 @@ together the various subpackages, as well as implementations of some of the simpler components (when interfaces would be too much overhead). -We pass context through context.Context between -app, middleware, and handlers. To do so, weave defines -some common keys to store info, such as block height and -chain id. Each extension, such as auth, may add its own -keys to enrich the context with specific data. - -There should exist two functions for every XYZ of type T -that we want to support in Context: - - WithXYZ(Context, T) Context - GetXYZ(Context) (val T, ok bool) - -WithXYZ may error/panic if the value was previously set -to avoid lower-level modules overwriting the value -(eg. height, header) +We pass a BlockInfo struct with all framework-defined +information down the Decorator/Handler stack. +For custom info that is only to be consumed within a particular +module, or timeouts, etc, you can make use of +context.Context. Please do not store info in there that is required +for other code to work, rather optional context to enhance functionality */ package weave diff --git a/blockinfo_test.go b/blockinfo_test.go new file mode 100644 index 00000000..961a13b9 --- /dev/null +++ b/blockinfo_test.go @@ -0,0 +1,65 @@ +package weave_test + +import ( + "os" + "testing" + "time" + + "github.com/iov-one/weave" + "github.com/iov-one/weave/errors" + "github.com/stretchr/testify/assert" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" +) + +func TestBlockInfo(t *testing.T) { + blocktime, err := time.Parse(time.RFC3339, "2019-03-15T14:56:00Z") + assert.Nil(t, err) + unixtime := weave.AsUnixTime(blocktime) + h := abci.Header{ + Height: 123, + Time: blocktime, + } + + newLogger := log.NewTMLogger(os.Stdout) + + cases := map[string]struct { + chainID string + logger log.Logger + err *errors.Error + expectLogger log.Logger + }{ + "default logger": { + chainID: "test-chain", + expectLogger: weave.DefaultLogger, + }, + "custom logger": { + chainID: "test-chain", + logger: newLogger, + expectLogger: newLogger, + }, + "bad chain id": { + chainID: "invalid;;chars", + err: errors.ErrInput, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + bi, err := weave.NewBlockInfo(h, weave.CommitInfo{}, tc.chainID, tc.logger) + if tc.err != nil { + if !tc.err.Is(err) { + t.Fatalf("Unexpected error: %+v", err) + } + return + } + + assert.Nil(t, err) + assert.Equal(t, tc.expectLogger, bi.Logger()) + assert.Equal(t, int64(123), bi.Height()) + assert.Equal(t, blocktime, bi.BlockTime()) + assert.Equal(t, unixtime, bi.UnixTime()) + }) + } + +} diff --git a/context_test.go b/context_test.go deleted file mode 100644 index 609ac1f5..00000000 --- a/context_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package weave_test - -import ( - "context" - "os" - "testing" - - "github.com/iov-one/weave" - "github.com/stretchr/testify/assert" - "github.com/tendermint/tendermint/libs/log" -) - -func TestContext(t *testing.T) { - bg := context.Background() - - // try logger with default - newLogger := log.NewTMLogger(os.Stdout) - ctx := weave.WithLogger(bg, newLogger) - assert.Equal(t, weave.DefaultLogger, weave.GetLogger(bg)) - assert.Equal(t, newLogger, weave.GetLogger(ctx)) - - // test height - uninitialized - val, ok := weave.GetHeight(ctx) - assert.Equal(t, int64(0), val) - assert.False(t, ok) - // set - ctx = weave.WithHeight(ctx, 7) - val, ok = weave.GetHeight(ctx) - assert.Equal(t, int64(7), val) - assert.True(t, ok) - // no reset - assert.Panics(t, func() { weave.WithHeight(ctx, 9) }) - - // changing the info, should modify the logger, but not the height - ctx2 := weave.WithLogInfo(ctx, "foo", "bar") - assert.NotEqual(t, weave.GetLogger(ctx), weave.GetLogger(ctx2)) - val, _ = weave.GetHeight(ctx) - assert.Equal(t, int64(7), val) - - // chain id MUST be set exactly once - assert.Panics(t, func() { weave.GetChainID(ctx) }) - ctx2 = weave.WithChainID(ctx, "my-chain") - assert.Equal(t, "my-chain", weave.GetChainID(ctx2)) - // don't try a second time - assert.Panics(t, func() { weave.WithChainID(ctx2, "my-chain") }) - - // TODO: test header context! -} - -func TestChainID(t *testing.T) { - cases := []struct { - chainID string - valid bool - }{ - {"", false}, - {"foo", false}, - {"special", true}, - {"wish-YOU-88", true}, - {"invalid;;chars", false}, - {"this-chain-id-is-way-too-long", false}, - } - - for _, tc := range cases { - assert.Equal(t, tc.valid, weave.IsValidChainID(tc.chainID), tc.chainID) - } -} diff --git a/time_test.go b/time_test.go index 6f6e68c7..7f85c3b1 100644 --- a/time_test.go +++ b/time_test.go @@ -1,13 +1,13 @@ package weave import ( - "context" "encoding/json" "testing" "time" "github.com/iov-one/weave/errors" "github.com/stretchr/testify/assert" + abci "github.com/tendermint/tendermint/abci/types" ) func TestUnixTimeUnmarshal(t *testing.T) { @@ -127,34 +127,31 @@ func TestUnixTimeAdd(t *testing.T) { } } +func mockBlockInfo(t time.Time) (BlockInfo, error) { + return NewBlockInfo(abci.Header{Height: 456, Time: t}, CommitInfo{}, "test-chain-2", nil) +} + func TestIsExpired(t *testing.T) { - now := AsUnixTime(time.Now()) - ctx := WithBlockTime(context.Background(), now.Time()) + tnow := time.Now() + now := AsUnixTime(tnow) + bi, err := mockBlockInfo(tnow) + assert.Nil(t, err) future := now.Add(5 * time.Minute) - if IsExpired(ctx, future) { + if bi.IsExpired(future) { t.Error("future is expired") } past := now.Add(-5 * time.Minute) - if !IsExpired(ctx, past) { + if !bi.IsExpired(past) { t.Error("past is not expired") } - if !IsExpired(ctx, now) { + if !bi.IsExpired(now) { t.Fatal("when expiration time is equal to now it is expected to be expired") } } -func TestIsExpiredRequiresBlockTime(t *testing.T) { - now := AsUnixTime(time.Now()) - assert.Panics(t, func() { - // Calling isExpected with a context without a block height - // attached is expected to panic. - IsExpired(context.Background(), now) - }) -} - func TestUnixDurationJSONUnmarshal(t *testing.T) { cases := map[string]struct { raw string @@ -203,34 +200,21 @@ func TestUnixDurationJSONUnmarshal(t *testing.T) { func TestInThePast(t *testing.T) { now := time.Now() - ctx := WithBlockTime(context.Background(), now) + bi, err := mockBlockInfo(now) + assert.Nil(t, err) + assert.Equal(t, AsUnixTime(now), bi.UnixTime()) - assert.Equal(t, false, InThePast(ctx, now)) - assert.Equal(t, false, InThePast(ctx, now.Add(time.Second))) - assert.Equal(t, true, InThePast(ctx, now.Add(-time.Second))) -} - -func TestInThePastRequiresBlockTime(t *testing.T) { - assert.Panics(t, func() { - // Calling isExpected with a context without a block height - // attached is expected to panic. - InThePast(context.Background(), time.Now()) - }) + assert.Equal(t, false, bi.InThePast(now)) + assert.Equal(t, false, bi.InThePast(now.Add(time.Second))) + assert.Equal(t, true, bi.InThePast(now.Add(-time.Second))) } func TestInTheFuture(t *testing.T) { now := time.Now() - ctx := WithBlockTime(context.Background(), now) - - assert.Equal(t, false, InTheFuture(ctx, now)) - assert.Equal(t, true, InTheFuture(ctx, now.Add(time.Second))) - assert.Equal(t, false, InTheFuture(ctx, now.Add(-time.Second))) -} + bi, err := mockBlockInfo(now) + assert.Nil(t, err) -func TestInTheFutureRequiresBlockTime(t *testing.T) { - assert.Panics(t, func() { - // Calling isExpected with a context without a block height - // attached is expected to panic. - InTheFuture(context.Background(), time.Now()) - }) + assert.Equal(t, false, bi.InTheFuture(now)) + assert.Equal(t, true, bi.InTheFuture(now.Add(time.Second))) + assert.Equal(t, false, bi.InTheFuture(now.Add(-time.Second))) } From 6eb94c8194686ffed20e8bbb65689c2c8e09161a Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:04:24 +0200 Subject: [PATCH 03/17] Use context.Context rather than weave.Context --- app/chain.go | 5 +-- app/chain_test.go | 4 +-- app/router.go | 9 ++--- app/store.go | 9 ++--- cmd/bnsd/x/username/handlers.go | 19 +++++----- gconf/handler.go | 7 ++-- migration/handler.go | 16 +++++---- weavetest/auth.go | 10 +++--- weavetest/decorators.go | 14 +++++--- weavetest/handlers.go | 6 ++-- x/aswap/handler.go | 19 +++++----- x/aswap/handler_test.go | 20 +++++------ x/auth.go | 22 ++++++------ x/auth_test.go | 2 +- x/batch/decorator.go | 5 +-- x/batch/decorator_test.go | 4 +-- x/cash/dynamicfee.go | 9 ++--- x/cash/handler.go | 6 ++-- x/cash/staticfee.go | 7 ++-- x/currency/handler.go | 7 ++-- x/distribution/handler.go | 19 +++++----- x/distribution/handler_test.go | 2 +- x/escrow/handler.go | 25 ++++++------- x/escrow/handler_test.go | 2 +- x/gov/context.go | 10 +++--- x/gov/handler.go | 43 +++++++++++----------- x/gov/handler_test.go | 58 +++++++++++++++--------------- x/gov/helpers_test.go | 4 +-- x/gov/interfaces.go | 5 +-- x/msgfee/antispam_fee_decorator.go | 5 +-- x/msgfee/fee_decorator.go | 6 ++-- x/multisig/context.go | 6 ++-- x/multisig/context_test.go | 2 +- x/multisig/decorator.go | 8 +++-- x/multisig/decorator_test.go | 4 +-- x/multisig/handlers.go | 13 +++---- x/paychan/handler.go | 17 ++++----- x/paychan/handler_test.go | 2 +- x/sigs/context.go | 6 ++-- x/sigs/decorator.go | 6 ++-- x/sigs/decorator_test.go | 4 +-- x/sigs/handler.go | 7 ++-- x/utils/action_tagger.go | 6 ++-- x/utils/key_tagger.go | 5 +-- x/utils/key_tagger_test.go | 4 +-- x/utils/logging.go | 7 ++-- x/utils/recover.go | 6 ++-- x/utils/recover_test.go | 4 +-- x/utils/savepoint.go | 6 ++-- x/utils/savepoint_test.go | 4 +-- x/validators/handler.go | 7 ++-- 51 files changed, 274 insertions(+), 229 deletions(-) diff --git a/app/chain.go b/app/chain.go index 38e9829d..247f195b 100644 --- a/app/chain.go +++ b/app/chain.go @@ -1,6 +1,7 @@ package app import ( + "context" "reflect" "github.com/iov-one/weave" @@ -76,11 +77,11 @@ type step struct { var _ weave.Handler = step{} // Check passes the handler into the decorator, implements Handler -func (s step) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (s step) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { return s.d.Check(ctx, store, tx, s.next) } // Deliver passes the handler into the decorator, implements Handler -func (s step) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (s step) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { return s.d.Deliver(ctx, store, tx, s.next) } diff --git a/app/chain_test.go b/app/chain_test.go index 91e821a8..c7b80044 100644 --- a/app/chain_test.go +++ b/app/chain_test.go @@ -67,14 +67,14 @@ type panicAtHeightDecorator int64 var _ weave.Decorator = panicAtHeightDecorator(0) -func (ph panicAtHeightDecorator) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (ph panicAtHeightDecorator) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { if val, _ := weave.GetHeight(ctx); val >= int64(ph) { panic("too high") } return next.Check(ctx, db, tx) } -func (ph panicAtHeightDecorator) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (ph panicAtHeightDecorator) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { if val, _ := weave.GetHeight(ctx); val >= int64(ph) { panic("too high") } diff --git a/app/router.go b/app/router.go index 028344ff..2bdb4d51 100644 --- a/app/router.go +++ b/app/router.go @@ -1,6 +1,7 @@ package app import ( + "context" "fmt" "regexp" @@ -57,7 +58,7 @@ func (r *Router) handler(m weave.Msg) weave.Handler { } // Check dispatches to the proper handler based on path -func (r *Router) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (r *Router) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, errors.Wrap(err, "cannot load msg") @@ -67,7 +68,7 @@ func (r *Router) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*we } // Deliver dispatches to the proper handler based on path -func (r *Router) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (r *Router) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, errors.Wrap(err, "cannot load msg") @@ -80,10 +81,10 @@ func (r *Router) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (* // provided. type notFoundHandler string -func (path notFoundHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (path notFoundHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { return nil, errors.Wrapf(errors.ErrNotFound, "no handler for message path %q", path) } -func (path notFoundHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (path notFoundHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { return nil, errors.Wrapf(errors.ErrNotFound, "no handler for message path %q", path) } diff --git a/app/store.go b/app/store.go index 86db6ed8..69ca7dad 100644 --- a/app/store.go +++ b/app/store.go @@ -1,6 +1,7 @@ package app import ( + "context" "encoding/json" "fmt" "strings" @@ -47,11 +48,11 @@ type StoreApp struct { // baseContext contains context info that is valid for // lifetime of this app (eg. chainID) - baseContext weave.Context + baseContext context.Context // blockContext contains context info that is valid for the // current block (eg. height, header), reset on BeginBlock - blockContext weave.Context + blockContext context.Context } // NewStoreApp initializes this app into a ready state with some defaults @@ -59,7 +60,7 @@ type StoreApp struct { // panics if unable to properly load the state from the given store // TODO: is this correct? nothing else to do really.... func NewStoreApp(name string, store weave.CommitKVStore, - queryRouter weave.QueryRouter, baseContext weave.Context) *StoreApp { + queryRouter weave.QueryRouter, baseContext context.Context) *StoreApp { s := &StoreApp{ name: name, // note: panics if trouble initializing from store @@ -150,7 +151,7 @@ func (s *StoreApp) Logger() log.Logger { } // BlockContext returns the block context for public use -func (s *StoreApp) BlockContext() weave.Context { +func (s *StoreApp) BlockContext() context.Context { return s.blockContext } diff --git a/cmd/bnsd/x/username/handlers.go b/cmd/bnsd/x/username/handlers.go index 505ad402..26f2c012 100644 --- a/cmd/bnsd/x/username/handlers.go +++ b/cmd/bnsd/x/username/handlers.go @@ -1,6 +1,7 @@ package username import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -28,14 +29,14 @@ type registerTokenHandler struct { bucket orm.ModelBucket } -func (h *registerTokenHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *registerTokenHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: registerTokenCost}, nil } -func (h *registerTokenHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *registerTokenHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -57,7 +58,7 @@ func (h *registerTokenHandler) Deliver(ctx weave.Context, db weave.KVStore, tx w return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *registerTokenHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*RegisterTokenMsg, error) { +func (h *registerTokenHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*RegisterTokenMsg, error) { var msg RegisterTokenMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -79,14 +80,14 @@ type transferTokenHandler struct { bucket orm.ModelBucket } -func (h *transferTokenHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *transferTokenHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: transferTokenCost}, nil } -func (h *transferTokenHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *transferTokenHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, token, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -99,7 +100,7 @@ func (h *transferTokenHandler) Deliver(ctx weave.Context, db weave.KVStore, tx w return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *transferTokenHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*TransferTokenMsg, *Token, error) { +func (h *transferTokenHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferTokenMsg, *Token, error) { var msg TransferTokenMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -122,14 +123,14 @@ type changeTokenTargetsHandler struct { bucket orm.ModelBucket } -func (h *changeTokenTargetsHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *changeTokenTargetsHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: changeTokenTargetCost}, nil } -func (h *changeTokenTargetsHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *changeTokenTargetsHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, token, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -142,7 +143,7 @@ func (h *changeTokenTargetsHandler) Deliver(ctx weave.Context, db weave.KVStore, return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *changeTokenTargetsHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*ChangeTokenTargetsMsg, *Token, error) { +func (h *changeTokenTargetsHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ChangeTokenTargetsMsg, *Token, error) { var msg ChangeTokenTargetsMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/gconf/handler.go b/gconf/handler.go index 902498be..d8d0cbc7 100644 --- a/gconf/handler.go +++ b/gconf/handler.go @@ -1,6 +1,7 @@ package gconf import ( + "context" "reflect" "github.com/iov-one/weave" @@ -34,21 +35,21 @@ func NewUpdateConfigurationHandler(pkg string, config OwnedConfig, auth x.Authen } } -func (h UpdateConfigurationHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateConfigurationHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if err := h.applyTx(ctx, store, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h UpdateConfigurationHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateConfigurationHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { if err := h.applyTx(ctx, store, tx); err != nil { return nil, err } return &weave.DeliverResult{}, nil } -func (h UpdateConfigurationHandler) applyTx(ctx weave.Context, store weave.KVStore, tx weave.Tx) error { +func (h UpdateConfigurationHandler) applyTx(ctx context.Context, store weave.KVStore, tx weave.Tx) error { if err := Load(store, h.pkg, h.config); err != nil { return errors.Wrap(err, "load message") } diff --git a/migration/handler.go b/migration/handler.go index a63b7ca6..09304584 100644 --- a/migration/handler.go +++ b/migration/handler.go @@ -1,6 +1,8 @@ package migration import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/x" @@ -51,14 +53,14 @@ type schemaMigratingHandler struct { migrations *register } -func (h *schemaMigratingHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *schemaMigratingHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if err := h.migrate(db, tx); err != nil { return nil, errors.Wrap(err, "migration") } return h.handler.Check(ctx, db, tx) } -func (h *schemaMigratingHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *schemaMigratingHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { if err := h.migrate(db, tx); err != nil { return nil, errors.Wrap(err, "migration") } @@ -101,14 +103,14 @@ type upgradeSchemaHandler struct { auth x.Authenticator } -func (h *upgradeSchemaHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *upgradeSchemaHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h *upgradeSchemaHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *upgradeSchemaHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -132,7 +134,7 @@ func (h *upgradeSchemaHandler) Deliver(ctx weave.Context, db weave.KVStore, tx w return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h *upgradeSchemaHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*UpgradeSchemaMsg, error) { +func (h *upgradeSchemaHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpgradeSchemaMsg, error) { var msg UpgradeSchemaMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -180,7 +182,7 @@ type schemaRoutingHandler []weave.Handler var _ weave.Handler = (schemaRoutingHandler)(nil) -func (h schemaRoutingHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h schemaRoutingHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { handler, err := h.selectHandler(tx) if err != nil { return nil, err @@ -188,7 +190,7 @@ func (h schemaRoutingHandler) Check(ctx weave.Context, db weave.KVStore, tx weav return handler.Check(ctx, db, tx) } -func (h schemaRoutingHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h schemaRoutingHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { handler, err := h.selectHandler(tx) if err != nil { return nil, err diff --git a/weavetest/auth.go b/weavetest/auth.go index 512a43c7..0d2755e5 100644 --- a/weavetest/auth.go +++ b/weavetest/auth.go @@ -25,14 +25,14 @@ type Auth struct { Signers []weave.Condition } -func (a *Auth) GetConditions(weave.Context) []weave.Condition { +func (a *Auth) GetConditions(context.Context) []weave.Condition { if a.Signer != nil { return append(a.Signers, a.Signer) } return a.Signers } -func (a *Auth) HasAddress(ctx weave.Context, addr weave.Address) bool { +func (a *Auth) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.Signers { if addr.Equals(s.Address()) { return true @@ -53,11 +53,11 @@ type CtxAuth struct { Key string } -func (a *CtxAuth) SetConditions(ctx weave.Context, permissions ...weave.Condition) weave.Context { +func (a *CtxAuth) SetConditions(ctx context.Context, permissions ...weave.Condition) context.Context { return context.WithValue(ctx, a.Key, permissions) } -func (a *CtxAuth) GetConditions(ctx weave.Context) []weave.Condition { +func (a *CtxAuth) GetConditions(ctx context.Context) []weave.Condition { val := ctx.Value(a.Key) if val == nil { return nil @@ -69,7 +69,7 @@ func (a *CtxAuth) GetConditions(ctx weave.Context) []weave.Condition { return conds } -func (a *CtxAuth) HasAddress(ctx weave.Context, addr weave.Address) bool { +func (a *CtxAuth) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true diff --git a/weavetest/decorators.go b/weavetest/decorators.go index e529f9c2..46504cf7 100644 --- a/weavetest/decorators.go +++ b/weavetest/decorators.go @@ -1,6 +1,10 @@ package weavetest -import "github.com/iov-one/weave" +import ( + "context" + + "github.com/iov-one/weave" +) // Decorator is a mock implementation of the weave.Decorator interface. // @@ -23,7 +27,7 @@ type Decorator struct { var _ weave.Decorator = (*Decorator)(nil) -func (d *Decorator) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d *Decorator) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { d.checkCall++ if d.CheckErr != nil { @@ -32,7 +36,7 @@ func (d *Decorator) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx, next return next.Check(ctx, db, tx) } -func (d *Decorator) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d *Decorator) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { d.deliverCall++ if d.DeliverErr != nil { @@ -64,10 +68,10 @@ type decoratedHandler struct { var _ weave.Handler = (*decoratedHandler)(nil) -func (d *decoratedHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (d *decoratedHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { return d.dc.Check(ctx, db, tx, d.hn) } -func (d *decoratedHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (d *decoratedHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { return d.dc.Deliver(ctx, db, tx, d.hn) } diff --git a/weavetest/handlers.go b/weavetest/handlers.go index 8cfba5aa..f11f24ab 100644 --- a/weavetest/handlers.go +++ b/weavetest/handlers.go @@ -1,6 +1,8 @@ package weavetest import ( + "context" + "github.com/iov-one/weave" ) @@ -24,7 +26,7 @@ type Handler struct { var _ weave.Handler = (*Handler)(nil) -func (h *Handler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *Handler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { h.checkCall++ if h.CheckErr != nil { return nil, h.CheckErr @@ -34,7 +36,7 @@ func (h *Handler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weav return &res, nil } -func (h *Handler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *Handler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { h.deliverCall++ if h.DeliverErr != nil { return nil, h.DeliverErr diff --git a/x/aswap/handler.go b/x/aswap/handler.go index 90805a5f..c1f6270f 100644 --- a/x/aswap/handler.go +++ b/x/aswap/handler.go @@ -2,6 +2,7 @@ package aswap import ( "bytes" + "context" "crypto/sha256" "github.com/iov-one/weave" @@ -46,7 +47,7 @@ type CreateSwapHandler struct { var _ weave.Handler = CreateSwapHandler{} // Check does the validation and sets the cost of the transaction -func (h CreateSwapHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -59,7 +60,7 @@ func (h CreateSwapHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.T } // Deliver moves the tokens from sender to the swap account if all conditions are met. -func (h CreateSwapHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { @@ -93,7 +94,7 @@ func (h CreateSwapHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave } // validate does all common pre-processing between Check and Deliver. -func (h CreateSwapHandler) validate(ctx weave.Context, +func (h CreateSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { @@ -119,7 +120,7 @@ var _ weave.Handler = ReleaseSwapHandler{} // Check just verifies it is properly formed and returns // the cost of executing it -func (h ReleaseSwapHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReleaseSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -130,7 +131,7 @@ func (h ReleaseSwapHandler) Check(ctx weave.Context, db weave.KVStore, tx weave. // Deliver moves the tokens from swap account to the receiver if // all preconditions are met. When the swap account is empty it is deleted. -func (h ReleaseSwapHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReleaseSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { swapID, swap, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -157,7 +158,7 @@ func (h ReleaseSwapHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weav } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseSwapHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { +func (h ReleaseSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -192,7 +193,7 @@ var _ weave.Handler = ReturnSwapHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h ReturnSwapHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReturnSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -203,7 +204,7 @@ func (h ReturnSwapHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.T // Deliver moves all the tokens from the swap to the defined sender if // all preconditions are met. The swap is deleted afterwards. -func (h ReturnSwapHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReturnSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, swap, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -228,7 +229,7 @@ func (h ReturnSwapHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave } // validate does all common pre-processing between Check and Deliver. -func (h ReturnSwapHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { +func (h ReturnSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { var msg ReturnSwapMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/aswap/handler_test.go b/x/aswap/handler_test.go index 31159843..2e1f15ff 100644 --- a/x/aswap/handler_test.go +++ b/x/aswap/handler_test.go @@ -46,7 +46,7 @@ func TestCreateHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx weave.Context, db weave.KVStore) weave.Context + setup func(ctx context.Context, db weave.KVStore) context.Context check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -54,7 +54,7 @@ func TestCreateHandler(t *testing.T) { mutator func(db *aswap.CreateMsg) }{ "Happy Path": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { setBalance(t, db, alice.Address(), initialCoins) return authenticator.SetConditions(ctx, alice) }, @@ -73,7 +73,7 @@ func TestCreateHandler(t *testing.T) { }, }, "happy path, timeout can be in the past": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { setBalance(t, db, alice.Address(), initialCoins) return authenticator.SetConditions(ctx, alice) }, @@ -89,14 +89,14 @@ func TestCreateHandler(t *testing.T) { }, }, "Invalid Auth": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { return authenticator.SetConditions(ctx, pete) }, wantDeliverErr: errors.ErrUnauthorized, wantCheckErr: errors.ErrUnauthorized, }, "Empty account": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { return authenticator.SetConditions(ctx, alice) }, wantDeliverErr: errors.ErrEmpty, @@ -159,7 +159,7 @@ func TestReleaseHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx weave.Context, db weave.KVStore) weave.Context + setup func(ctx context.Context, db weave.KVStore) context.Context check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -203,7 +203,7 @@ func TestReleaseHandler(t *testing.T) { }, }, "Expired": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { return weave.WithBlockTime(ctx, time.Now().Add(10*time.Hour)) }, wantDeliverErr: errors.ErrState, @@ -271,7 +271,7 @@ func TestReturnHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx weave.Context, db weave.KVStore) weave.Context + setup func(ctx context.Context, db weave.KVStore) context.Context check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -279,7 +279,7 @@ func TestReturnHandler(t *testing.T) { mutator func(db *aswap.ReturnSwapMsg) }{ "Happy Path, includes no auth check": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { return weave.WithBlockTime(ctx, blockNow.Add(2*time.Hour)) }, wantDeliverErr: nil, @@ -310,7 +310,7 @@ func TestReturnHandler(t *testing.T) { }, }, "Not Expired": { - setup: func(ctx weave.Context, db weave.KVStore) weave.Context { + setup: func(ctx context.Context, db weave.KVStore) context.Context { return weave.WithBlockTime(ctx, blockNow) }, wantDeliverErr: errors.ErrState, diff --git a/x/auth.go b/x/auth.go index 8fdd8e6e..2593608e 100644 --- a/x/auth.go +++ b/x/auth.go @@ -1,6 +1,8 @@ package x import ( + "context" + "github.com/iov-one/weave" ) @@ -11,9 +13,9 @@ import ( type Authenticator interface { // GetConditions reveals all Conditions fulfilled, // you may want GetAddresses helper - GetConditions(weave.Context) []weave.Condition + GetConditions(context.Context) []weave.Condition // HasAddress checks if any condition matches this address - HasAddress(weave.Context, weave.Address) bool + HasAddress(context.Context, weave.Address) bool } // MultiAuth chains together many Authenticators into one @@ -29,7 +31,7 @@ func ChainAuth(impls ...Authenticator) MultiAuth { } // GetConditions combines all Conditions from all Authenticators -func (m MultiAuth) GetConditions(ctx weave.Context) []weave.Condition { +func (m MultiAuth) GetConditions(ctx context.Context) []weave.Condition { var res []weave.Condition for _, impl := range m.impls { add := impl.GetConditions(ctx) @@ -42,7 +44,7 @@ func (m MultiAuth) GetConditions(ctx weave.Context) []weave.Condition { } // HasAddress returns true iff any Authenticator support this -func (m MultiAuth) HasAddress(ctx weave.Context, addr weave.Address) bool { +func (m MultiAuth) HasAddress(ctx context.Context, addr weave.Address) bool { for _, impl := range m.impls { if impl.HasAddress(ctx, addr) { return true @@ -52,7 +54,7 @@ func (m MultiAuth) HasAddress(ctx weave.Context, addr weave.Address) bool { } // GetAddresses wraps the GetConditions method of any Authenticator -func GetAddresses(ctx weave.Context, auth Authenticator) []weave.Address { +func GetAddresses(ctx context.Context, auth Authenticator) []weave.Address { perms := auth.GetConditions(ctx) addrs := make([]weave.Address, len(perms)) for i, p := range perms { @@ -62,7 +64,7 @@ func GetAddresses(ctx weave.Context, auth Authenticator) []weave.Address { } // MainSigner returns the first permission if any, otherwise nil -func MainSigner(ctx weave.Context, auth Authenticator) weave.Condition { +func MainSigner(ctx context.Context, auth Authenticator) weave.Condition { signers := auth.GetConditions(ctx) if len(signers) == 0 { return nil @@ -72,7 +74,7 @@ func MainSigner(ctx weave.Context, auth Authenticator) weave.Condition { // HasAllAddresses returns true if all elements in required are // also in context. -func HasAllAddresses(ctx weave.Context, auth Authenticator, required []weave.Address) bool { +func HasAllAddresses(ctx context.Context, auth Authenticator, required []weave.Address) bool { for _, r := range required { if !auth.HasAddress(ctx, r) { return false @@ -83,7 +85,7 @@ func HasAllAddresses(ctx weave.Context, auth Authenticator, required []weave.Add // HasNAddresses returns true if at least n elements in requested are // also in context. -func HasNAddresses(ctx weave.Context, auth Authenticator, required []weave.Address, n int) bool { +func HasNAddresses(ctx context.Context, auth Authenticator, required []weave.Address, n int) bool { // Special case: is this an error??? if n <= 0 { return true @@ -102,14 +104,14 @@ func HasNAddresses(ctx weave.Context, auth Authenticator, required []weave.Addre // HasAllConditions returns true if all elements in required are // also in context. -func HasAllConditions(ctx weave.Context, auth Authenticator, required []weave.Condition) bool { +func HasAllConditions(ctx context.Context, auth Authenticator, required []weave.Condition) bool { return HasNConditions(ctx, auth, required, len(required)) } // HasNConditions returns true if at least n elements in requested are // also in context. // Useful for threshold conditions (1 of 3, 3 of 5, etc...) -func HasNConditions(ctx weave.Context, auth Authenticator, requested []weave.Condition, n int) bool { +func HasNConditions(ctx context.Context, auth Authenticator, requested []weave.Condition, n int) bool { // Special case: is this an error??? if n <= 0 { return true diff --git a/x/auth_test.go b/x/auth_test.go index b22776d8..d939360e 100644 --- a/x/auth_test.go +++ b/x/auth_test.go @@ -18,7 +18,7 @@ func TestAuth(t *testing.T) { ctx2 := &weavetest.CtxAuth{Key: "bar"} cases := map[string]struct { - ctx weave.Context + ctx context.Context auth Authenticator mainSigner weave.Condition wantInCtx weave.Condition diff --git a/x/batch/decorator.go b/x/batch/decorator.go index 494ac2d0..8df755ee 100644 --- a/x/batch/decorator.go +++ b/x/batch/decorator.go @@ -6,6 +6,7 @@ transaction package batch import ( + "context" "strings" "github.com/iov-one/weave" @@ -42,7 +43,7 @@ func (tx *BatchTx) GetMsg() (weave.Msg, error) { // Check iterates through messages in a batch transaction and passes them // down the stack -func (d Decorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, err @@ -105,7 +106,7 @@ func (*Decorator) combineChecks(checks []*weave.CheckResult) (*weave.CheckResult // Deliver iterates through messages in a batch transaction and passes them // down the stack -func (d Decorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, err diff --git a/x/batch/decorator_test.go b/x/batch/decorator_test.go index 1662d1ea..ac4500c4 100644 --- a/x/batch/decorator_test.go +++ b/x/batch/decorator_test.go @@ -49,12 +49,12 @@ func (m *mockHelper) GetMsg() (weave.Msg, error) { return args.Get(0).(weave.Msg), args.Error(1) } -func (m *mockHelper) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (m *mockHelper) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { args := m.Called(ctx, store, tx) return args.Get(0).(*weave.CheckResult), args.Error(1) } -func (m *mockHelper) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (m *mockHelper) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { args := m.Called(ctx, store, tx) return args.Get(0).(*weave.DeliverResult), args.Error(1) } diff --git a/x/cash/dynamicfee.go b/x/cash/dynamicfee.go index b1e042c3..6f4ceff9 100644 --- a/x/cash/dynamicfee.go +++ b/x/cash/dynamicfee.go @@ -34,6 +34,7 @@ address is configured via gconf package. package cash import ( + "context" "github.com/iov-one/weave" coin "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -57,7 +58,7 @@ func NewDynamicFeeDecorator(auth x.Authenticator, ctrl Controller) DynamicFeeDec } // Check verifies and deducts fees before calling down the stack -func (d DynamicFeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (cres *weave.CheckResult, cerr error) { +func (d DynamicFeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (cres *weave.CheckResult, cerr error) { fee, payer, cache, err := d.prepare(ctx, store, tx) if err != nil { return nil, errors.Wrap(err, "cannot prepare") @@ -97,7 +98,7 @@ func (d DynamicFeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx we } // Deliver verifies and deducts fees before calling down the stack -func (d DynamicFeeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (dres *weave.DeliverResult, derr error) { +func (d DynamicFeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (dres *weave.DeliverResult, derr error) { fee, payer, cache, err := d.prepare(ctx, store, tx) if err != nil { return nil, errors.Wrap(err, "cannot prepare") @@ -157,7 +158,7 @@ func (d DynamicFeeDecorator) chargeMinimalFee(store weave.KVStore, src weave.Add // prepare is all shared setup between Check and Deliver. It computes the fee // for the transaction, ensures that the payer is authenticated and prepares // the database transaction. -func (d DynamicFeeDecorator) prepare(ctx weave.Context, store weave.KVStore, tx weave.Tx) (fee coin.Coin, payer weave.Address, cache weave.KVCacheWrap, err error) { +func (d DynamicFeeDecorator) prepare(ctx context.Context, store weave.KVStore, tx weave.Tx) (fee coin.Coin, payer weave.Address, cache weave.KVCacheWrap, err error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return fee, payer, cache, errors.Wrap(err, "cannot extract fee") @@ -185,7 +186,7 @@ func (d DynamicFeeDecorator) prepare(ctx weave.Context, store weave.KVStore, tx } // this returns the fee info to deduct and the error if incorrectly set -func (d DynamicFeeDecorator) extractFee(ctx weave.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { +func (d DynamicFeeDecorator) extractFee(ctx context.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { var finfo *FeeInfo ftx, ok := tx.(FeeTx) if ok { diff --git a/x/cash/handler.go b/x/cash/handler.go index 36755a40..7e1a846e 100644 --- a/x/cash/handler.go +++ b/x/cash/handler.go @@ -1,6 +1,8 @@ package cash import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/gconf" @@ -40,7 +42,7 @@ func NewSendHandler(auth x.Authenticator, control Controller) SendHandler { // Check just verifies it is properly formed and returns // the cost of executing it -func (h SendHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h SendHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { var msg SendMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -59,7 +61,7 @@ func (h SendHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) // Deliver moves the tokens from sender to receiver if // all preconditions are met -func (h SendHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h SendHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { var msg SendMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/cash/staticfee.go b/x/cash/staticfee.go index c13067c6..cdd9f968 100644 --- a/x/cash/staticfee.go +++ b/x/cash/staticfee.go @@ -16,6 +16,7 @@ It uses auth to verify the sender. package cash import ( + "context" "github.com/iov-one/weave" coin "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -40,7 +41,7 @@ func NewFeeDecorator(auth x.Authenticator, ctrl CoinMover) FeeDecorator { } // Check verifies and deducts fees before calling down the stack -func (d FeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return nil, err @@ -74,7 +75,7 @@ func (d FeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, } // Deliver verifies and deducts fees before calling down the stack -func (d FeeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d FeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return nil, err @@ -100,7 +101,7 @@ func (d FeeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.T return next.Deliver(ctx, store, tx) } -func (d FeeDecorator) extractFee(ctx weave.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { +func (d FeeDecorator) extractFee(ctx context.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { var finfo *FeeInfo ftx, ok := tx.(FeeTx) if ok { diff --git a/x/currency/handler.go b/x/currency/handler.go index 964023b1..3d4167ae 100644 --- a/x/currency/handler.go +++ b/x/currency/handler.go @@ -1,6 +1,7 @@ package currency import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -33,14 +34,14 @@ type createTokenInfoHandler struct { issuer weave.Address } -func (h *createTokenInfoHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *createTokenInfoHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: newTokenInfoCost}, nil } -func (h *createTokenInfoHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *createTokenInfoHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -49,7 +50,7 @@ func (h *createTokenInfoHandler) Deliver(ctx weave.Context, db weave.KVStore, tx return &weave.DeliverResult{}, h.bucket.Save(db, obj) } -func (h *createTokenInfoHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createTokenInfoHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/distribution/handler.go b/x/distribution/handler.go index 6d701dce..09b53685 100644 --- a/x/distribution/handler.go +++ b/x/distribution/handler.go @@ -1,6 +1,7 @@ package distribution import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -55,14 +56,14 @@ type createRevenueHandler struct { ctrl CashController } -func (h *createRevenueHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *createRevenueHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: newRevenueCost}, nil } -func (h *createRevenueHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *createRevenueHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -79,7 +80,7 @@ func (h *createRevenueHandler) Deliver(ctx weave.Context, db weave.KVStore, tx w return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h *createRevenueHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createRevenueHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -93,7 +94,7 @@ type distributeHandler struct { ctrl CashController } -func (h *distributeHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *distributeHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -109,7 +110,7 @@ func (h *distributeHandler) Check(ctx weave.Context, db weave.KVStore, tx weave. return &res, nil } -func (h *distributeHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *distributeHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -130,7 +131,7 @@ func (h *distributeHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weav return &weave.DeliverResult{}, nil } -func (h *distributeHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*DistributeMsg, error) { +func (h *distributeHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DistributeMsg, error) { var msg DistributeMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -147,7 +148,7 @@ type resetRevenueHandler struct { ctrl CashController } -func (h *resetRevenueHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *resetRevenueHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -166,7 +167,7 @@ func (h *resetRevenueHandler) Check(ctx weave.Context, db weave.KVStore, tx weav return &res, nil } -func (h *resetRevenueHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *resetRevenueHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -196,7 +197,7 @@ func (h *resetRevenueHandler) Deliver(ctx weave.Context, db weave.KVStore, tx we return &weave.DeliverResult{}, nil } -func (h *resetRevenueHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*ResetMsg, error) { +func (h *resetRevenueHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ResetMsg, error) { var msg ResetMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/distribution/handler_test.go b/x/distribution/handler_test.go index f42b6682..ab24afcb 100644 --- a/x/distribution/handler_test.go +++ b/x/distribution/handler_test.go @@ -410,7 +410,7 @@ func (a *action) tx() weave.Tx { return &weavetest.Tx{Msg: a.msg} } -func (a *action) ctx() weave.Context { +func (a *action) ctx() context.Context { ctx := weave.WithHeight(context.Background(), a.blocksize) ctx = weave.WithChainID(ctx, "testchain-123") auth := &weavetest.CtxAuth{Key: "auth"} diff --git a/x/escrow/handler.go b/x/escrow/handler.go index 12577a9b..3028c8b5 100644 --- a/x/escrow/handler.go +++ b/x/escrow/handler.go @@ -1,6 +1,7 @@ package escrow import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -46,7 +47,7 @@ var _ weave.Handler = CreateEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h CreateEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -60,7 +61,7 @@ func (h CreateEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave // Deliver moves the tokens from sender to the escrow account if // all preconditions are met. -func (h CreateEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -100,7 +101,7 @@ func (h CreateEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx wea } // validate does all common pre-processing between Check and Deliver. -func (h CreateEscrowHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -131,7 +132,7 @@ var _ weave.Handler = ReleaseEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it -func (h ReleaseEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReleaseEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -142,7 +143,7 @@ func (h ReleaseEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weav // Deliver moves the tokens from escrow account to the receiver if // all preconditions are met. When the escrow account is empty it is deleted. -func (h ReleaseEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReleaseEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, escrow, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -180,7 +181,7 @@ func (h ReleaseEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx we } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseEscrowHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { +func (h ReleaseEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -214,7 +215,7 @@ var _ weave.Handler = ReturnEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h ReturnEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReturnEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -225,7 +226,7 @@ func (h ReturnEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave // Deliver moves all the tokens from the escrow to the defined sender if // all preconditions are met. The escrow is deleted afterwards. -func (h ReturnEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReturnEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { key, escrow, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -249,7 +250,7 @@ func (h ReturnEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx wea } // validate does all common pre-processing between Check and Deliver. -func (h ReturnEscrowHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { +func (h ReturnEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { var msg ReturnMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -277,7 +278,7 @@ var _ weave.Handler = UpdateEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h UpdateEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -288,7 +289,7 @@ func (h UpdateEscrowHandler) Check(ctx weave.Context, db weave.KVStore, tx weave // Deliver updates the any of the sender, recipient or arbiter if // all preconditions are met. No coins are moved. -func (h UpdateEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, escrow, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -315,7 +316,7 @@ func (h UpdateEscrowHandler) Deliver(ctx weave.Context, db weave.KVStore, tx wea } // validate does all common pre-processing between Check and Deliver. -func (h UpdateEscrowHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { +func (h UpdateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { var msg UpdatePartiesMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/escrow/handler_test.go b/x/escrow/handler_test.go index 72c707c1..4368aa1d 100644 --- a/x/escrow/handler_test.go +++ b/x/escrow/handler_test.go @@ -694,7 +694,7 @@ func (a action) tx() weave.Tx { return &weavetest.Tx{Msg: a.msg} } -func (a action) ctx() weave.Context { +func (a action) ctx() context.Context { ctx := context.Background() if !a.blockTime.IsZero() { ctx = weave.WithBlockTime(ctx, a.blockTime) diff --git a/x/gov/context.go b/x/gov/context.go index 3cf60538..a0e77766 100644 --- a/x/gov/context.go +++ b/x/gov/context.go @@ -20,7 +20,7 @@ type proposalWrapper struct { proposalID []byte } -func withElectionSuccess(ctx weave.Context, ruleID []byte) weave.Context { +func withElectionSuccess(ctx context.Context, ruleID []byte) context.Context { val, _ := ctx.Value(contextKeyGov).([]weave.Condition) return context.WithValue(ctx, contextKeyGov, append(val, ElectionCondition(ruleID))) } @@ -37,14 +37,14 @@ type Authenticate struct { var _ x.Authenticator = Authenticate{} // GetConditions returns permissions previously set on this context. -func (a Authenticate) GetConditions(ctx weave.Context) []weave.Condition { +func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { // (val, ok) form to return nil instead of panic if unset val, _ := ctx.Value(contextKeyGov).([]weave.Condition) return val } // HasAddress returns true iff this address is in GetConditions. -func (a Authenticate) HasAddress(ctx weave.Context, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true @@ -53,12 +53,12 @@ func (a Authenticate) HasAddress(ctx weave.Context, addr weave.Address) bool { return false } -func withProposal(ctx weave.Context, proposal *Proposal, proposalID []byte) weave.Context { +func withProposal(ctx context.Context, proposal *Proposal, proposalID []byte) context.Context { return context.WithValue(ctx, contextKeyProposal, proposalWrapper{proposal: proposal, proposalID: proposalID}) } // CtxProposal reads the the proposal and it's id from the context -func CtxProposal(ctx weave.Context) (*Proposal, []byte) { +func CtxProposal(ctx context.Context) (*Proposal, []byte) { val, _ := ctx.Value(contextKeyProposal).(proposalWrapper) return val.proposal, val.proposalID } diff --git a/x/gov/handler.go b/x/gov/handler.go index 8cf79dde..1e3df0f6 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -1,6 +1,7 @@ package gov import ( + "context" "fmt" "github.com/iov-one/weave" @@ -66,7 +67,7 @@ func newVoteHandler(auth x.Authenticator) *VoteHandler { } } -func (h VoteHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h VoteHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -74,7 +75,7 @@ func (h VoteHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*w } -func (h VoteHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h VoteHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { voteMsg, proposal, vote, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -102,7 +103,7 @@ func (h VoteHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) ( return &weave.DeliverResult{}, nil } -func (h VoteHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { +func (h VoteHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { var msg VoteMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -170,7 +171,7 @@ func newTallyHandler(auth x.Authenticator, decoder OptionDecoder, executor Execu } } -func (h TallyHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h TallyHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -178,7 +179,7 @@ func (h TallyHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (* } -func (h TallyHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (resOut *weave.DeliverResult, errOut error) { +func (h TallyHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (resOut *weave.DeliverResult, errOut error) { msg, proposal, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -245,7 +246,7 @@ func (h TallyHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) return res, nil } -func (h TallyHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { +func (h TallyHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { var msg TallyMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -285,7 +286,7 @@ func newCreateProposalHandler(auth x.Authenticator, decoder OptionDecoder) *Crea } } -func (h CreateProposalHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateProposalHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -293,7 +294,7 @@ func (h CreateProposalHandler) Check(ctx weave.Context, db weave.KVStore, tx wea } -func (h CreateProposalHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateProposalHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, rule, electorate, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -328,7 +329,7 @@ func (h CreateProposalHandler) Deliver(ctx weave.Context, db weave.KVStore, tx w return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h CreateProposalHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { +func (h CreateProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { var msg CreateProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -406,7 +407,7 @@ func newDeleteProposalHandler(auth x.Authenticator) *DeleteProposalHandler { } } -func (h DeleteProposalHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { +func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { var msg DeleteProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -429,14 +430,14 @@ func (h DeleteProposalHandler) validate(ctx weave.Context, db weave.KVStore, tx return &msg, prop, nil } -func (h DeleteProposalHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h DeleteProposalHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: deleteProposalCost}, nil } -func (h DeleteProposalHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h DeleteProposalHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, prop, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -465,7 +466,7 @@ func newUpdateElectorateHandler(auth x.Authenticator) *UpdateElectorateHandler { } } -func (h UpdateElectorateHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateElectorateHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -473,7 +474,7 @@ func (h UpdateElectorateHandler) Check(ctx weave.Context, db weave.KVStore, tx w return &weave.CheckResult{GasAllocated: updateElectorateCost}, nil } -func (h UpdateElectorateHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateElectorateHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, elect, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -489,7 +490,7 @@ func (h UpdateElectorateHandler) Deliver(ctx weave.Context, db weave.KVStore, tx return &weave.DeliverResult{Data: msg.ElectorateID}, nil } -func (h UpdateElectorateHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectorateMsg, *Electorate, error) { +func (h UpdateElectorateHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectorateMsg, *Electorate, error) { var msg UpdateElectorateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -527,7 +528,7 @@ func newUpdateElectionRuleHandler(auth x.Authenticator) *UpdateElectionRuleHandl } } -func (h UpdateElectionRuleHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateElectionRuleHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -535,7 +536,7 @@ func (h UpdateElectionRuleHandler) Check(ctx weave.Context, db weave.KVStore, tx return &weave.CheckResult{GasAllocated: updateElectionRuleCost}, nil } -func (h UpdateElectionRuleHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateElectionRuleHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, rule, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -548,7 +549,7 @@ func (h UpdateElectionRuleHandler) Deliver(ctx weave.Context, db weave.KVStore, return &weave.DeliverResult{Data: msg.ElectionRuleID}, nil } -func (h UpdateElectionRuleHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectionRuleMsg, *ElectionRule, error) { +func (h UpdateElectionRuleHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectionRuleMsg, *ElectionRule, error) { var msg UpdateElectionRuleMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -579,7 +580,7 @@ func newCreateTextResolutionHandler(auth x.Authenticator) *createTextResolutionH } } -func (h createTextResolutionHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h createTextResolutionHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -587,7 +588,7 @@ func (h createTextResolutionHandler) Check(ctx weave.Context, db weave.KVStore, return &weave.CheckResult{GasAllocated: textResolutionCost}, nil } -func (h createTextResolutionHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h createTextResolutionHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -612,7 +613,7 @@ func (h createTextResolutionHandler) Deliver(ctx weave.Context, db weave.KVStore return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h createTextResolutionHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateTextResolutionMsg, error) { +func (h createTextResolutionHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateTextResolutionMsg, error) { var msg CreateTextResolutionMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/gov/handler_test.go b/x/gov/handler_test.go index 94a8e404..0a551337 100644 --- a/x/gov/handler_test.go +++ b/x/gov/handler_test.go @@ -403,7 +403,7 @@ func TestDeleteProposal(t *testing.T) { proposalID := weavetest.SequenceID(1) nonExistentProposalID := weavetest.SequenceID(2) specs := map[string]struct { - Mods func(weave.Context, *Proposal) // modifies test fixtures before storing + Mods func(context.Context, *Proposal) // modifies test fixtures before storing ProposalDeleted bool Msg DeleteProposalMsg SignedBy weave.Condition @@ -414,7 +414,7 @@ func TestDeleteProposal(t *testing.T) { Msg: DeleteProposalMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID}, SignedBy: hAliceCond, ProposalDeleted: true, - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(1 * time.Hour)) proposal.VotingEndTime = weave.AsUnixTime(time.Now().Add(2 * time.Hour)) }, @@ -430,7 +430,7 @@ func TestDeleteProposal(t *testing.T) { SignedBy: hBobbyCond, WantCheckErr: errors.ErrUnauthorized, WantDeliverErr: errors.ErrUnauthorized, - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(1 * time.Hour)) proposal.VotingEndTime = weave.AsUnixTime(time.Now().Add(2 * time.Hour)) }, @@ -438,7 +438,7 @@ func TestDeleteProposal(t *testing.T) { "Voting has started": { Msg: DeleteProposalMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID}, SignedBy: hAliceCond, - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(-1 * time.Hour)) proposal.SubmissionTime = weave.AsUnixTime(time.Now().Add(-2 * time.Hour)) }, @@ -498,7 +498,7 @@ func TestCreateResolution(t *testing.T) { proposal := Proposal{ElectorateRef: orm.VersionedIDRef{ID: ID, Version: 1}} specs := map[string]struct { - ctx weave.Context + ctx context.Context Msg CreateTextResolutionMsg WantCheckErr *errors.Error WantDeliverErr *errors.Error @@ -570,8 +570,8 @@ func TestVote(t *testing.T) { nonElectorCond := weavetest.NewCondition() nonElector := nonElectorCond.Address() specs := map[string]struct { - Init func(ctx weave.Context, db store.KVStore) // executed before test fixtures - Mods func(weave.Context, *Proposal) // modifies test fixtures before storing + Init func(ctx context.Context, db store.KVStore) // executed before test fixtures + Mods func(context.Context, *Proposal) // modifies test fixtures before storing Msg VoteMsg SignedBy weave.Condition WantCheckErr *errors.Error @@ -610,7 +610,7 @@ func TestVote(t *testing.T) { ExpVotedBy: hAlice, }, "Can change vote": { - Init: func(ctx weave.Context, db store.KVStore) { + Init: func(ctx context.Context, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -621,7 +621,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VoteState.TotalYes = 10 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_No, Voter: hBobby}, @@ -630,7 +630,7 @@ func TestVote(t *testing.T) { ExpVotedBy: hBobby, }, "Can resubmit vote": { - Init: func(ctx weave.Context, db store.KVStore) { + Init: func(ctx context.Context, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -641,7 +641,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VoteState.TotalYes = 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -668,7 +668,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrUnauthorized, }, "Vote before start date": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingStartTime = unixBlockTime(t, ctx) + 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -677,7 +677,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on start date": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingStartTime = unixBlockTime(t, ctx) }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -686,7 +686,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on end date": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingEndTime = unixBlockTime(t, ctx) }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -695,7 +695,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote after end date": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.VotingEndTime = unixBlockTime(t, ctx) - 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -704,7 +704,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on withdrawn proposal must fail": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.Status = Proposal_Withdrawn }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -713,7 +713,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on closed proposal must fail": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { proposal.Status = Proposal_Closed }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -722,7 +722,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Sanity check on count vote": { - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { // not a valid setup proposal.VoteState.TotalYes = math.MaxUint64 proposal.VoteState.TotalElectorateWeight = math.MaxUint64 @@ -732,7 +732,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrHuman, }, "Sanity check on undo count vote": { - Init: func(ctx weave.Context, db store.KVStore) { + Init: func(ctx context.Context, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -743,7 +743,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx weave.Context, proposal *Proposal) { + Mods: func(ctx context.Context, proposal *Proposal) { // not a valid setup proposal.VoteState.TotalYes = 0 proposal.VoteState.TotalElectorateWeight = math.MaxUint64 @@ -817,7 +817,7 @@ func TestTally(t *testing.T) { yes, no, abstain uint64 } specs := map[string]struct { - Mods func(weave.Context, *Proposal) + Mods func(context.Context, *Proposal) Src tallySetup WantCheckErr *errors.Error WantDeliverErr *errors.Error @@ -1089,7 +1089,7 @@ func TestTally(t *testing.T) { t.Fatalf("unexpected error: %+v", err) } }, - Mods: func(ctx weave.Context, p *Proposal) { + Mods: func(ctx context.Context, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 10}) p.VotingEndTime = unixBlockTime(t, ctx) - 1 }, @@ -1142,7 +1142,7 @@ func TestTally(t *testing.T) { t.Fatalf("unexpected error: %+v", err) } }, - Mods: func(ctx weave.Context, p *Proposal) { + Mods: func(ctx context.Context, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 0}) p.VotingEndTime = unixBlockTime(t, ctx) - 1 }, @@ -1158,7 +1158,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal accepted: execution error:", }, "Does not update an electorate when rejected": { - Mods: func(ctx weave.Context, p *Proposal) { + Mods: func(ctx context.Context, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 10}) p.VotingEndTime = unixBlockTime(t, ctx) - 1 }, @@ -1189,7 +1189,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on second tally": { - Mods: func(_ weave.Context, p *Proposal) { + Mods: func(_ context.Context, p *Proposal) { p.Status = Proposal_Closed }, Src: tallySetup{ @@ -1203,7 +1203,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on tally before end date": { - Mods: func(ctx weave.Context, p *Proposal) { + Mods: func(ctx context.Context, p *Proposal) { p.VotingEndTime = unixBlockTime(t, ctx) + 1 }, Src: tallySetup{ @@ -1216,7 +1216,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on tally at end date": { - Mods: func(ctx weave.Context, p *Proposal) { + Mods: func(ctx context.Context, p *Proposal) { p.VotingEndTime = unixBlockTime(t, ctx) }, Src: tallySetup{ @@ -1229,7 +1229,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on withdrawn proposal": { - Mods: func(ctx weave.Context, p *Proposal) { + Mods: func(ctx context.Context, p *Proposal) { p.Status = Proposal_Withdrawn }, Src: tallySetup{ @@ -1255,7 +1255,7 @@ func TestTally(t *testing.T) { // given ctx := weave.WithBlockTime(context.Background(), time.Now().Round(time.Second)) - setupForTally := func(_ weave.Context, p *Proposal) { + setupForTally := func(_ context.Context, p *Proposal) { p.VoteState = NewTallyResult(spec.Src.quorum, spec.Src.threshold, spec.Src.totalWeightElectorate) p.VoteState.TotalYes = spec.Src.yes p.VoteState.TotalNo = spec.Src.no diff --git a/x/gov/helpers_test.go b/x/gov/helpers_test.go index 345ce125..ff0b4eaf 100644 --- a/x/gov/helpers_test.go +++ b/x/gov/helpers_test.go @@ -12,9 +12,9 @@ import ( ) // ctxAwareMutator is a call back interface to modify the passed proposal for test setup -type ctxAwareMutator func(weave.Context, *Proposal) +type ctxAwareMutator func(context.Context, *Proposal) -func withTextProposal(t *testing.T, db store.KVStore, ctx weave.Context, mods ...ctxAwareMutator) *ProposalBucket { +func withTextProposal(t *testing.T, db store.KVStore, ctx context.Context, mods ...ctxAwareMutator) *ProposalBucket { t.Helper() // setup electorate withElectorate(t, db) diff --git a/x/gov/interfaces.go b/x/gov/interfaces.go index 568a8412..a19312f8 100644 --- a/x/gov/interfaces.go +++ b/x/gov/interfaces.go @@ -1,6 +1,7 @@ package gov import ( + "context" weave "github.com/iov-one/weave" ) @@ -8,13 +9,13 @@ import ( type OptionDecoder func(raw []byte) (weave.Msg, error) // Executor will do something with the message once it is approved. -type Executor func(ctx weave.Context, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) +type Executor func(ctx context.Context, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) // HandlerAsExecutor wraps the msg in a fake Tx to satisfy the Handler interface // Since a Router and Decorators also expose this interface, we can wrap any stack // that does not care about the extra Tx info besides Msg. func HandlerAsExecutor(h weave.Handler) Executor { - return func(ctx weave.Context, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) { + return func(ctx context.Context, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) { tx := &fakeTx{msg: msg} return h.Deliver(ctx, store, tx) } diff --git a/x/msgfee/antispam_fee_decorator.go b/x/msgfee/antispam_fee_decorator.go index 14426646..18ee9c1c 100644 --- a/x/msgfee/antispam_fee_decorator.go +++ b/x/msgfee/antispam_fee_decorator.go @@ -1,6 +1,7 @@ package msgfee import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -28,7 +29,7 @@ func NewAntispamFeeDecorator(fee coin.Coin) *AntispamFeeDecorator { return &AntispamFeeDecorator{fee: fee} } -func (d *AntispamFeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d *AntispamFeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { res, err := next.Check(ctx, store, tx) if d == nil { // Since NewAntispamFeeDecorator can return nil, let's be graceful here return res, err @@ -49,6 +50,6 @@ func (d *AntispamFeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx return res, nil } -func (d *AntispamFeeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d *AntispamFeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { return next.Deliver(ctx, store, tx) } diff --git a/x/msgfee/fee_decorator.go b/x/msgfee/fee_decorator.go index f67a228b..d0b712ab 100644 --- a/x/msgfee/fee_decorator.go +++ b/x/msgfee/fee_decorator.go @@ -1,6 +1,8 @@ package msgfee import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -26,7 +28,7 @@ func NewFeeDecorator() *FeeDecorator { } } -func (d *FeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d *FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { res, err := next.Check(ctx, store, tx) if err != nil { return nil, err @@ -46,7 +48,7 @@ func (d *FeeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx return res, nil } -func (d *FeeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d *FeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { res, err := next.Deliver(ctx, store, tx) if err != nil { return nil, err diff --git a/x/multisig/context.go b/x/multisig/context.go index dcf5da81..5246ec6b 100644 --- a/x/multisig/context.go +++ b/x/multisig/context.go @@ -15,7 +15,7 @@ const ( // withMultisig is a private method, as only this module // can add a multisig signer -func withMultisig(ctx weave.Context, id []byte) weave.Context { +func withMultisig(ctx context.Context, id []byte) context.Context { val, _ := ctx.Value(contextKeyMultisig).([]weave.Condition) if val == nil { return context.WithValue(ctx, contextKeyMultisig, []weave.Condition{MultiSigCondition(id)}) @@ -36,7 +36,7 @@ type Authenticate struct { var _ x.Authenticator = Authenticate{} // GetConditions returns permissions previously set on this context -func (a Authenticate) GetConditions(ctx weave.Context) []weave.Condition { +func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { // (val, ok) form to return nil instead of panic if unset val, _ := ctx.Value(contextKeyMultisig).([]weave.Condition) if val == nil { @@ -46,7 +46,7 @@ func (a Authenticate) GetConditions(ctx weave.Context) []weave.Condition { } // HasAddress returns true iff this address is in GetConditions -func (a Authenticate) HasAddress(ctx weave.Context, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true diff --git a/x/multisig/context_test.go b/x/multisig/context_test.go index 8a9b99d0..06006d3d 100644 --- a/x/multisig/context_test.go +++ b/x/multisig/context_test.go @@ -21,7 +21,7 @@ func TestContext(t *testing.T) { bg := context.Background() cases := map[string]struct { - ctx weave.Context + ctx context.Context wantPerms []weave.Condition wantAddr []weave.Address wantNoAddr []weave.Address diff --git a/x/multisig/decorator.go b/x/multisig/decorator.go index fcb8b26c..ea49422c 100644 --- a/x/multisig/decorator.go +++ b/x/multisig/decorator.go @@ -1,6 +1,8 @@ package multisig import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/x" @@ -24,7 +26,7 @@ func NewDecorator(auth x.Authenticator) Decorator { } // Check enforce multisig contract before calling down the stack -func (d Decorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { newCtx, cost, err := d.authMultisig(ctx, store, tx) if err != nil { return nil, err @@ -39,7 +41,7 @@ func (d Decorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, ne } // Deliver enforces multisig contract before calling down the stack -func (d Decorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { newCtx, _, err := d.authMultisig(ctx, store, tx) if err != nil { return nil, err @@ -48,7 +50,7 @@ func (d Decorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, return next.Deliver(newCtx, store, tx) } -func (d Decorator) authMultisig(ctx weave.Context, store weave.KVStore, tx weave.Tx) (weave.Context, int64, error) { +func (d Decorator) authMultisig(ctx context.Context, store weave.KVStore, tx weave.Tx) (context.Context, int64, error) { multisigContract, ok := tx.(MultiSigTx) if !ok { return ctx, 0, nil diff --git a/x/multisig/decorator_test.go b/x/multisig/decorator_test.go index 3a7da04f..8c7141f3 100644 --- a/x/multisig/decorator_test.go +++ b/x/multisig/decorator_test.go @@ -149,12 +149,12 @@ type MultisigCheckHandler struct { var _ weave.Handler = (*MultisigCheckHandler)(nil) -func (s *MultisigCheckHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (s *MultisigCheckHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { s.Perms = Authenticate{}.GetConditions(ctx) return &weave.CheckResult{}, nil } -func (s *MultisigCheckHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (s *MultisigCheckHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { s.Perms = Authenticate{}.GetConditions(ctx) return &weave.DeliverResult{}, nil } diff --git a/x/multisig/handlers.go b/x/multisig/handlers.go index 24971b68..c8ad26e7 100644 --- a/x/multisig/handlers.go +++ b/x/multisig/handlers.go @@ -1,6 +1,7 @@ package multisig import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -29,7 +30,7 @@ type CreateMsgHandler struct { var _ weave.Handler = CreateMsgHandler{} -func (h CreateMsgHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateMsgHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -38,7 +39,7 @@ func (h CreateMsgHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx return &weave.CheckResult{GasAllocated: creationCost}, nil } -func (h CreateMsgHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateMsgHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -62,7 +63,7 @@ func (h CreateMsgHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave. } // validate does all common pre-processing between Check and Deliver. -func (h CreateMsgHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateMsgHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { // Retrieve tx main signer in this context. sender := x.MainSigner(ctx, h.auth) if sender == nil { @@ -84,7 +85,7 @@ type UpdateMsgHandler struct { var _ weave.Handler = CreateMsgHandler{} -func (h UpdateMsgHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateMsgHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -92,7 +93,7 @@ func (h UpdateMsgHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx return &weave.CheckResult{GasAllocated: updateCost}, nil } -func (h UpdateMsgHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateMsgHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -114,7 +115,7 @@ func (h UpdateMsgHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave. return &weave.DeliverResult{}, nil } -func (h UpdateMsgHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*UpdateMsg, error) { +func (h UpdateMsgHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateMsg, error) { var msg UpdateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/paychan/handler.go b/x/paychan/handler.go index bcb6e453..8edbab87 100644 --- a/x/paychan/handler.go +++ b/x/paychan/handler.go @@ -1,6 +1,7 @@ package paychan import ( + "context" "github.com/iov-one/weave" coin "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -41,7 +42,7 @@ type createPaymentChannelHandler struct { var _ weave.Handler = (*createPaymentChannelHandler)(nil) -func (h *createPaymentChannelHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *createPaymentChannelHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -49,7 +50,7 @@ func (h *createPaymentChannelHandler) Check(ctx weave.Context, db weave.KVStore, return &weave.CheckResult{GasAllocated: createPaymentChannelCost}, nil } -func (h *createPaymentChannelHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -63,7 +64,7 @@ func (h *createPaymentChannelHandler) validate(ctx weave.Context, db weave.KVSto return &msg, nil } -func (h *createPaymentChannelHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *createPaymentChannelHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -100,14 +101,14 @@ type transferPaymentChannelHandler struct { var _ weave.Handler = (*transferPaymentChannelHandler)(nil) -func (h *transferPaymentChannelHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *transferPaymentChannelHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: transferPaymentChannelCost}, nil } -func (h *transferPaymentChannelHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { +func (h *transferPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { var msg TransferMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -147,7 +148,7 @@ func (h *transferPaymentChannelHandler) validate(ctx weave.Context, db weave.KVS return &msg, nil } -func (h *transferPaymentChannelHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *transferPaymentChannelHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -204,7 +205,7 @@ type closePaymentChannelHandler struct { var _ weave.Handler = (*closePaymentChannelHandler)(nil) -func (h *closePaymentChannelHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *closePaymentChannelHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { var msg CloseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -212,7 +213,7 @@ func (h *closePaymentChannelHandler) Check(ctx weave.Context, db weave.KVStore, return &weave.CheckResult{}, nil } -func (h *closePaymentChannelHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *closePaymentChannelHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { var msg CloseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/paychan/handler_test.go b/x/paychan/handler_test.go index 8c0170f1..e9f4da73 100644 --- a/x/paychan/handler_test.go +++ b/x/paychan/handler_test.go @@ -561,7 +561,7 @@ func (a *action) tx() weave.Tx { return &weavetest.Tx{Msg: a.msg} } -func (a *action) ctx() weave.Context { +func (a *action) ctx() context.Context { ctx := weave.WithHeight(context.Background(), a.blocksize) ctx = weave.WithChainID(ctx, "testchain-123") if !a.blockTime.IsZero() { diff --git a/x/sigs/context.go b/x/sigs/context.go index a6ce8317..6be4a9a3 100644 --- a/x/sigs/context.go +++ b/x/sigs/context.go @@ -18,7 +18,7 @@ const ( // withSigners is a private method, as only this module // can add a signer -func withSigners(ctx weave.Context, signers []weave.Condition) weave.Context { +func withSigners(ctx context.Context, signers []weave.Condition) context.Context { return context.WithValue(ctx, contextKeySigners, signers) } @@ -30,7 +30,7 @@ var _ x.Authenticator = Authenticate{} // GetConditions returns who signed the current Context. // May be empty -func (a Authenticate) GetConditions(ctx weave.Context) []weave.Condition { +func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { // (val, ok) form to return nil instead of panic if unset val, _ := ctx.Value(contextKeySigners).([]weave.Condition) // if we were paranoid about our own code, we would deep-copy @@ -40,7 +40,7 @@ func (a Authenticate) GetConditions(ctx weave.Context) []weave.Condition { // HasAddress returns true if the given address // had signed in the current Context. -func (a Authenticate) HasAddress(ctx weave.Context, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { signers := a.GetConditions(ctx) for _, s := range signers { if addr.Equals(s.Address()) { diff --git a/x/sigs/decorator.go b/x/sigs/decorator.go index f2fcaf2b..0fb78cb6 100644 --- a/x/sigs/decorator.go +++ b/x/sigs/decorator.go @@ -6,6 +6,8 @@ and maintain nonces for replay protection. package sigs import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" ) @@ -47,7 +49,7 @@ func (d Decorator) AllowMissingSigs() Decorator { } // Check verifies signatures before calling down the stack. -func (d Decorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { stx, ok := tx.(SignedTx) if !ok { return next.Check(ctx, store, tx) @@ -76,7 +78,7 @@ func (d Decorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, ne } // Deliver verifies signatures before calling down the stack. -func (d Decorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { stx, ok := tx.(SignedTx) if !ok { return next.Deliver(ctx, store, tx) diff --git a/x/sigs/decorator_test.go b/x/sigs/decorator_test.go index 5a77ff85..7e882e9c 100644 --- a/x/sigs/decorator_test.go +++ b/x/sigs/decorator_test.go @@ -95,12 +95,12 @@ type SigCheckHandler struct { var _ weave.Handler = (*SigCheckHandler)(nil) -func (s *SigCheckHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (s *SigCheckHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { s.Signers = Authenticate{}.GetConditions(ctx) return &weave.CheckResult{}, nil } -func (s *SigCheckHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (s *SigCheckHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { s.Signers = Authenticate{}.GetConditions(ctx) return &weave.DeliverResult{}, nil } diff --git a/x/sigs/handler.go b/x/sigs/handler.go index c9b8852e..cf79c032 100644 --- a/x/sigs/handler.go +++ b/x/sigs/handler.go @@ -1,6 +1,7 @@ package sigs import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -21,14 +22,14 @@ type bumpSequenceHandler struct { b Bucket } -func (h *bumpSequenceHandler) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *bumpSequenceHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h *bumpSequenceHandler) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *bumpSequenceHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { user, msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -50,7 +51,7 @@ func (h *bumpSequenceHandler) Deliver(ctx weave.Context, db weave.KVStore, tx we return &weave.DeliverResult{}, nil } -func (h *bumpSequenceHandler) validate(ctx weave.Context, db weave.KVStore, tx weave.Tx) (*UserData, *BumpSequenceMsg, error) { +func (h *bumpSequenceHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UserData, *BumpSequenceMsg, error) { var msg BumpSequenceMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/utils/action_tagger.go b/x/utils/action_tagger.go index 95bec8e0..6a8c23ec 100644 --- a/x/utils/action_tagger.go +++ b/x/utils/action_tagger.go @@ -1,6 +1,8 @@ package utils import ( + "context" + "github.com/iov-one/weave" "github.com/tendermint/tendermint/libs/common" ) @@ -28,12 +30,12 @@ func NewActionTagger() ActionTagger { } // Check just passes the request along -func (ActionTagger) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (ActionTagger) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { return next.Check(ctx, db, tx) } // Deliver appends a tag on the result if there is a success. -func (ActionTagger) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (ActionTagger) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { // if we error in reporting, let's do so early before dispatching msg, err := tx.GetMsg() if err != nil { diff --git a/x/utils/key_tagger.go b/x/utils/key_tagger.go index cc5773ab..4e8545ac 100644 --- a/x/utils/key_tagger.go +++ b/x/utils/key_tagger.go @@ -1,6 +1,7 @@ package utils import ( + "context" "encoding/hex" "strings" @@ -30,13 +31,13 @@ func NewKeyTagger() KeyTagger { } // Check does nothing -func (KeyTagger) Check(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (KeyTagger) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { return next.Check(ctx, db, tx) } // Deliver passes in a recording KVStore into the child and // uses that to calculate tags to add to DeliverResult -func (KeyTagger) Deliver(ctx weave.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (KeyTagger) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { record := store.NewRecordingStore(db) res, err := next.Deliver(ctx, record, tx) if err != nil { diff --git a/x/utils/key_tagger_test.go b/x/utils/key_tagger_test.go index 18373065..099ef2c3 100644 --- a/x/utils/key_tagger_test.go +++ b/x/utils/key_tagger_test.go @@ -149,7 +149,7 @@ type writeDecorator struct { var _ weave.Decorator = writeDecorator{} -func (d writeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d writeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { if !d.after { store.Set(d.key, d.value) } @@ -160,7 +160,7 @@ func (d writeDecorator) Check(ctx weave.Context, store weave.KVStore, tx weave.T return res, err } -func (d writeDecorator) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d writeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { if !d.after { store.Set(d.key, d.value) } diff --git a/x/utils/logging.go b/x/utils/logging.go index 6d7f85ac..922c40ef 100644 --- a/x/utils/logging.go +++ b/x/utils/logging.go @@ -1,6 +1,7 @@ package utils import ( + "context" "time" "github.com/iov-one/weave" @@ -17,7 +18,7 @@ func NewLogging() Logging { } // Check logs error -> info, success -> debug -func (r Logging) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (r Logging) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { start := time.Now() res, err := next.Check(ctx, store, tx) var resLog string @@ -29,7 +30,7 @@ func (r Logging) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next } // Deliver logs error -> error, success -> info -func (r Logging) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (r Logging) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { start := time.Now() res, err := next.Deliver(ctx, store, tx) var resLog string @@ -41,7 +42,7 @@ func (r Logging) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, ne } // logDuration writes information about the time and result to the logger -func logDuration(ctx weave.Context, start time.Time, msg string, err error, lowPrio bool) { +func logDuration(ctx context.Context, start time.Time, msg string, err error, lowPrio bool) { delta := time.Now().Sub(start) logger := weave.GetLogger(ctx).With("duration", delta/time.Microsecond) diff --git a/x/utils/recover.go b/x/utils/recover.go index 7653c2d6..6462156b 100644 --- a/x/utils/recover.go +++ b/x/utils/recover.go @@ -1,6 +1,8 @@ package utils import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" ) @@ -17,13 +19,13 @@ func NewRecovery() Recovery { } // Check turns panics into normal errors -func (r Recovery) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (_ *weave.CheckResult, err error) { +func (r Recovery) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (_ *weave.CheckResult, err error) { defer errors.Recover(&err) return next.Check(ctx, store, tx) } // Deliver turns panics into normal errors -func (r Recovery) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (_ *weave.DeliverResult, err error) { +func (r Recovery) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (_ *weave.DeliverResult, err error) { defer errors.Recover(&err) return next.Deliver(ctx, store, tx) } diff --git a/x/utils/recover_test.go b/x/utils/recover_test.go index 33eaf6d7..163fc4d5 100644 --- a/x/utils/recover_test.go +++ b/x/utils/recover_test.go @@ -33,10 +33,10 @@ type panicHandler struct{} var _ weave.Handler = panicHandler{} -func (p panicHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (p panicHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { panic("check panic") } -func (p panicHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (p panicHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { panic("deliver panic") } diff --git a/x/utils/savepoint.go b/x/utils/savepoint.go index d9efbc94..89c02e8a 100644 --- a/x/utils/savepoint.go +++ b/x/utils/savepoint.go @@ -1,6 +1,8 @@ package utils import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" ) @@ -37,7 +39,7 @@ func (s Savepoint) OnDeliver() Savepoint { } // Check will optionally set a checkpoint -func (s Savepoint) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (s Savepoint) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { if !s.onCheck { return next.Check(ctx, store, tx) } @@ -59,7 +61,7 @@ func (s Savepoint) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx, ne } // Deliver will optionally set a checkpoint -func (s Savepoint) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (s Savepoint) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { if !s.onDeliver { return next.Deliver(ctx, store, tx) } diff --git a/x/utils/savepoint_test.go b/x/utils/savepoint_test.go index 0cff0168..e3c61651 100644 --- a/x/utils/savepoint_test.go +++ b/x/utils/savepoint_test.go @@ -131,12 +131,12 @@ type writeHandler struct { err error } -func (h writeHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h writeHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { store.Set(h.key, h.value) return &weave.CheckResult{}, h.err } -func (h writeHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h writeHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { store.Set(h.key, h.value) return &weave.DeliverResult{}, h.err } diff --git a/x/validators/handler.go b/x/validators/handler.go index d12b5f83..f115726d 100644 --- a/x/validators/handler.go +++ b/x/validators/handler.go @@ -1,6 +1,7 @@ package validators import ( + "context" "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -29,14 +30,14 @@ type updateHandler struct { var _ weave.Handler = (*updateHandler)(nil) -func (h updateHandler) Check(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h updateHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, store, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h updateHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h updateHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { diff, updates, err := h.validate(ctx, store, tx) if err != nil { return nil, err @@ -50,7 +51,7 @@ func (h updateHandler) Deliver(ctx weave.Context, store weave.KVStore, tx weave. } // Validate returns an update diff, ValidatorUpdates to store for bookkeeping and an error. -func (h updateHandler) validate(ctx weave.Context, store weave.KVStore, tx weave.Tx) ([]weave.ValidatorUpdate, +func (h updateHandler) validate(ctx context.Context, store weave.KVStore, tx weave.Tx) ([]weave.ValidatorUpdate, weave.ValidatorUpdates, error) { var msg ApplyDiffMsg var resUpdates weave.ValidatorUpdates From 4b715a9a4c0fc9bc12820dc3c14b3ec7880f04f6 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:25:10 +0200 Subject: [PATCH 04/17] Add BlockInfo to Handler/Decorator interface --- app/base.go | 4 +- app/chain.go | 8 ++-- app/chain_test.go | 16 +++---- app/router.go | 12 ++--- client/client.go | 14 +++--- client/wrapper.go | 12 ++--- cmd/bnsd/x/username/handlers.go | 18 ++++---- docs/design/transactionhandling.rst | 4 +- gconf/handler.go | 6 +-- gconf/handler_test.go | 4 +- handler.go | 10 ++--- migration/handler.go | 22 ++++----- tmtest/tmtest.go | 2 +- weavetest/auth.go | 6 +-- weavetest/decorators.go | 16 +++---- weavetest/handlers.go | 4 +- x/aswap/handler.go | 18 ++++---- x/aswap/handler_test.go | 32 ++++++------- x/auth.go | 14 +++--- x/batch/decorator.go | 12 ++--- x/batch/decorator_test.go | 4 +- x/cash/dynamicfee.go | 12 ++--- x/cash/handler.go | 4 +- x/cash/staticfee.go | 14 +++--- x/currency/handler.go | 6 +-- x/distribution/handler.go | 18 ++++---- x/escrow/handler.go | 24 +++++----- x/gov/context.go | 6 +-- x/gov/handler.go | 42 ++++++++--------- x/gov/handler_test.go | 70 ++++++++++++++--------------- x/gov/helpers_test.go | 2 +- x/gov/interfaces.go | 7 +-- x/msgfee/antispam_fee_decorator.go | 8 ++-- x/msgfee/fee_decorator.go | 8 ++-- x/multisig/context.go | 4 +- x/multisig/decorator.go | 6 +-- x/multisig/decorator_test.go | 8 ++-- x/multisig/handlers.go | 12 ++--- x/multisig/handlers_test.go | 8 ++-- x/paychan/handler.go | 16 +++---- x/sigs/context.go | 4 +- x/sigs/decorator.go | 12 ++--- x/sigs/decorator_test.go | 10 ++--- x/sigs/handler.go | 6 +-- x/sigs/handler_test.go | 4 +- x/utils/action_tagger.go | 8 ++-- x/utils/action_tagger_test.go | 2 +- x/utils/key_tagger.go | 8 ++-- x/utils/key_tagger_test.go | 12 ++--- x/utils/logging.go | 10 ++--- x/utils/recover.go | 8 ++-- x/utils/recover_test.go | 12 ++--- x/utils/savepoint.go | 16 +++---- x/utils/savepoint_test.go | 8 ++-- x/validators/handler.go | 6 +-- x/validators/handler_test.go | 4 +- 56 files changed, 322 insertions(+), 321 deletions(-) diff --git a/app/base.go b/app/base.go index 098d628e..dc1a53cd 100644 --- a/app/base.go +++ b/app/base.go @@ -43,7 +43,7 @@ func (b BaseApp) DeliverTx(txBytes []byte) abci.ResponseDeliverTx { "call", "deliver_tx", "path", weave.GetPath(tx)) - res, err := b.handler.Deliver(ctx, b.DeliverStore(), tx) + res, err := b.handler.Deliver(ctx, info, b.DeliverStore(), tx) if err == nil { b.AddValChange(res.Diff) } @@ -61,7 +61,7 @@ func (b BaseApp) CheckTx(txBytes []byte) abci.ResponseCheckTx { "call", "check_tx", "path", weave.GetPath(tx)) - res, err := b.handler.Check(ctx, b.CheckStore(), tx) + res, err := b.handler.Check(ctx, info, b.CheckStore(), tx) return weave.CheckOrError(res, err, b.debug) } diff --git a/app/chain.go b/app/chain.go index 247f195b..a0853192 100644 --- a/app/chain.go +++ b/app/chain.go @@ -77,11 +77,11 @@ type step struct { var _ weave.Handler = step{} // Check passes the handler into the decorator, implements Handler -func (s step) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - return s.d.Check(ctx, store, tx, s.next) +func (s step) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { + return s.d.Check(ctx, info, store, tx, s.next) } // Deliver passes the handler into the decorator, implements Handler -func (s step) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - return s.d.Deliver(ctx, store, tx, s.next) +func (s step) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { + return s.d.Deliver(ctx, info, store, tx, s.next) } diff --git a/app/chain_test.go b/app/chain_test.go index c7b80044..7a18d592 100644 --- a/app/chain_test.go +++ b/app/chain_test.go @@ -31,14 +31,14 @@ func TestChain(t *testing.T) { // This height must not panic. ctx := weave.WithHeight(context.Background(), panicHeight-2) - _, err := stack.Check(ctx, nil, nil) + _, err := stack.Check(ctx, info, nil, nil) assert.NoError(t, err) assert.Equal(t, 1, c1.CheckCallCount()) assert.Equal(t, 1, c2.CheckCallCount()) assert.Equal(t, 1, c3.CheckCallCount()) assert.Equal(t, 1, h.CheckCallCount()) - _, err = stack.Deliver(ctx, nil, nil) + _, err = stack.Deliver(ctx, info, nil, nil) assert.NoError(t, err) assert.Equal(t, 1, c1.DeliverCallCount()) assert.Equal(t, 1, c2.DeliverCallCount()) @@ -48,14 +48,14 @@ func TestChain(t *testing.T) { // Trigger a panic. ctx = weave.WithHeight(context.Background(), panicHeight+2) - _, err = stack.Check(ctx, nil, nil) + _, err = stack.Check(ctx, info, nil, nil) assert.Error(t, err) assert.Equal(t, 2, c1.CheckCallCount()) assert.Equal(t, 2, c2.CheckCallCount()) assert.Equal(t, 1, c3.CheckCallCount()) assert.Equal(t, 1, h.CheckCallCount()) - _, err = stack.Deliver(ctx, nil, nil) + _, err = stack.Deliver(ctx, info, nil, nil) assert.Error(t, err) assert.Equal(t, 2, c1.DeliverCallCount()) assert.Equal(t, 2, c2.DeliverCallCount()) @@ -67,18 +67,18 @@ type panicAtHeightDecorator int64 var _ weave.Decorator = panicAtHeightDecorator(0) -func (ph panicAtHeightDecorator) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (ph panicAtHeightDecorator) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { if val, _ := weave.GetHeight(ctx); val >= int64(ph) { panic("too high") } - return next.Check(ctx, db, tx) + return next.Check(ctx, info, db, tx) } -func (ph panicAtHeightDecorator) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (ph panicAtHeightDecorator) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { if val, _ := weave.GetHeight(ctx); val >= int64(ph) { panic("too high") } - return next.Deliver(ctx, db, tx) + return next.Deliver(ctx, info, db, tx) } func TestChainNilDecorator(t *testing.T) { diff --git a/app/router.go b/app/router.go index 2bdb4d51..68ea5fba 100644 --- a/app/router.go +++ b/app/router.go @@ -58,33 +58,33 @@ func (r *Router) handler(m weave.Msg) weave.Handler { } // Check dispatches to the proper handler based on path -func (r *Router) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (r *Router) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, errors.Wrap(err, "cannot load msg") } h := r.handler(msg) - return h.Check(ctx, store, tx) + return h.Check(ctx, info, store, tx) } // Deliver dispatches to the proper handler based on path -func (r *Router) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (r *Router) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, errors.Wrap(err, "cannot load msg") } h := r.handler(msg) - return h.Deliver(ctx, store, tx) + return h.Deliver(ctx, info, store, tx) } // notFoundHandler always returns ErrNotFound error regardless of the arguments // provided. type notFoundHandler string -func (path notFoundHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (path notFoundHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { return nil, errors.Wrapf(errors.ErrNotFound, "no handler for message path %q", path) } -func (path notFoundHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (path notFoundHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { return nil, errors.Wrapf(errors.ErrNotFound, "no handler for message path %q", path) } diff --git a/client/client.go b/client/client.go index 33d22375..b93de524 100644 --- a/client/client.go +++ b/client/client.go @@ -59,7 +59,7 @@ func (c *Client) Status(ctx context.Context) (*Status, error) { // Header returns the block header at the given height. // Returns an error if no header exists yet for that height -func (c *Client) Header(ctx context.Context, height int64) (*Header, error) { +func (c *Client) Header(ctx context.Context, info weave.BlockInfo, height int64) (*Header, error) { // TODO: add context timeout here info, err := c.conn.BlockchainInfo(height, height) if err != nil { @@ -74,7 +74,7 @@ func (c *Client) Header(ctx context.Context, height int64) (*Header, error) { // SubmitTx will submit the tx to the mempool and then return with success or error // You will need to use WatchTx (easily parallelizable) to get the result. // CommitTx and CommitTxs provide helpers for common use cases -func (c *Client) SubmitTx(ctx context.Context, tx weave.Tx) (TransactionID, error) { +func (c *Client) SubmitTx(ctx context.Context, info weave.BlockInfo, tx weave.Tx) (TransactionID, error) { bz, err := tx.Marshal() if err != nil { return nil, errors.Wrapf(errors.ErrMsg, "marshaling: %s", err.Error()) @@ -110,7 +110,7 @@ func (c *Client) Query(query RequestQuery) ResponseQuery { } // GetTxByID will return 0 or 1 results (nil or result value) -func (c *Client) GetTxByID(ctx context.Context, id TransactionID) (*CommitResult, error) { +func (c *Client) GetTxByID(ctx context.Context, info weave.BlockInfo, id TransactionID) (*CommitResult, error) { // TODO: add context timeout here tx, err := c.conn.Tx(id, false) // FIXME: use proofs sometime if err != nil { @@ -122,7 +122,7 @@ func (c *Client) GetTxByID(ctx context.Context, id TransactionID) (*CommitResult // SearchTx will search for all committed transactions that match a query, // returning them as one large array. // It returns an error if the subscription request failed. -func (c *Client) SearchTx(ctx context.Context, query TxQuery) ([]*CommitResult, error) { +func (c *Client) SearchTx(ctx context.Context, info weave.BlockInfo, query TxQuery) ([]*CommitResult, error) { // TODO: return actual transaction content as well? not just ID and Result // TODO: add context timeout here // FIXME: use proofs sometime @@ -141,7 +141,7 @@ func (c *Client) SearchTx(ctx context.Context, query TxQuery) ([]*CommitResult, // SubscribeHeaders will fills the channel with all new headers // Stops when the context is cancelled -func (c *Client) SubscribeHeaders(ctx context.Context, results chan<- Header, options ...Option) error { +func (c *Client) SubscribeHeaders(ctx context.Context, info weave.BlockInfo, results chan<- Header, options ...Option) error { data, err := c.subscribe(ctx, QueryForHeader(), options...) if err != nil { return err @@ -171,7 +171,7 @@ func (c *Client) SubscribeHeaders(ctx context.Context, results chan<- Header, op // SubscribeTx will subscribe to all transactions that match a query, writing them to the // results channel as they arrive. It returns an error if the subscription request failed. // Once subscriptions start, the continue until the context is closed (or network error) -func (c *Client) SubscribeTx(ctx context.Context, query TxQuery, results chan<- CommitResult, options ...Option) error { +func (c *Client) SubscribeTx(ctx context.Context, info weave.BlockInfo, query TxQuery, results chan<- CommitResult, options ...Option) error { q := fmt.Sprintf("%s='%s' AND %s", tmtypes.EventTypeKey, tmtypes.EventTx, query) data, err := c.subscribe(ctx, q, options...) @@ -202,7 +202,7 @@ func (c *Client) SubscribeTx(ctx context.Context, query TxQuery, results chan<- } // subscribe should be used internally, it wraps conn.Subscribe and uses ctx.Done() to trigger Unsubscription -func (c *Client) subscribe(ctx context.Context, query string, options ...Option) (<-chan ctypes.ResultEvent, error) { +func (c *Client) subscribe(ctx context.Context, info weave.BlockInfo, query string, options ...Option) (<-chan ctypes.ResultEvent, error) { var outCapacity []int for _, option := range options { switch o := option.(type) { diff --git a/client/wrapper.go b/client/wrapper.go index cd723db5..2e04eda0 100644 --- a/client/wrapper.go +++ b/client/wrapper.go @@ -11,7 +11,7 @@ import ( // SubscribeTxByID will block until there is a result, then return it // You must cancel the context to avoid blocking forever in some cases -func (c *Client) SubscribeTxByID(ctx context.Context, id TransactionID) (*CommitResult, error) { +func (c *Client) SubscribeTxByID(ctx context.Context, info weave.BlockInfo, id TransactionID) (*CommitResult, error) { txs := make(chan CommitResult, 1) err := c.SubscribeTx(ctx, QueryTxByID(id), txs) if err != nil { @@ -29,7 +29,7 @@ func (c *Client) SubscribeTxByID(ctx context.Context, id TransactionID) (*Commit // WatchTx will block until this transaction makes it into a block // It will return immediately if the id was included in a block prior to the query, to avoid timing issues // You can use context.Context to pass in a timeout -func (c *Client) WatchTx(ctx context.Context, id TransactionID) (*CommitResult, error) { +func (c *Client) WatchTx(ctx context.Context, info weave.BlockInfo, id TransactionID) (*CommitResult, error) { // TODO: combine subscribe tx and search tx (two other functions to be writen) subctx, cancel := context.WithCancel(ctx) defer cancel() @@ -57,7 +57,7 @@ func (c *Client) WatchTx(ctx context.Context, id TransactionID) (*CommitResult, } // CommitTx will block on both Check and Deliver, returning when it is in a block -func (c *Client) CommitTx(ctx context.Context, tx weave.Tx) (*CommitResult, error) { +func (c *Client) CommitTx(ctx context.Context, info weave.BlockInfo, tx weave.Tx) (*CommitResult, error) { // This can be combined from other primitives check, err := c.SubmitTx(ctx, tx) if err != nil { @@ -72,7 +72,7 @@ func (c *Client) CommitTx(ctx context.Context, tx weave.Tx) (*CommitResult, erro } // WatchTxs will watch a list of transactions in parallel -func (c *Client) WatchTxs(ctx context.Context, ids []TransactionID) ([]*CommitResult, error) { +func (c *Client) WatchTxs(ctx context.Context, info weave.BlockInfo, ids []TransactionID) ([]*CommitResult, error) { var mutex sync.Mutex // FIXME: make this multierror when that exists var gotErr error @@ -110,7 +110,7 @@ func (c *Client) WatchTxs(ctx context.Context, ids []TransactionID) ([]*CommitRe // // If any tx fails in mempool or network, this returns an error // TODO: improve error handling (maybe we need to use CommitResultOrError type?) -func (c *Client) CommitTxs(ctx context.Context, txs []weave.Tx) ([]*CommitResult, error) { +func (c *Client) CommitTxs(ctx context.Context, info weave.BlockInfo, txs []weave.Tx) ([]*CommitResult, error) { // first submit them all (some may error), this should be in order var err error ids := make([]TransactionID, len(txs)) @@ -155,7 +155,7 @@ func (c *Client) WaitForNextBlock(ctx context.Context) (*Header, error) { // WaitForHeight subscribes to headers and returns as soon as a header arrives // equal to or greater than the given height. If the requested height is in the past, // it will still wait for the next block to arrive -func (c *Client) WaitForHeight(ctx context.Context, height int64) (*Header, error) { +func (c *Client) WaitForHeight(ctx context.Context, info weave.BlockInfo, height int64) (*Header, error) { // ensure we close subscription at function return cctx, cancel := context.WithCancel(ctx) defer cancel() diff --git a/cmd/bnsd/x/username/handlers.go b/cmd/bnsd/x/username/handlers.go index 26f2c012..94da9307 100644 --- a/cmd/bnsd/x/username/handlers.go +++ b/cmd/bnsd/x/username/handlers.go @@ -29,14 +29,14 @@ type registerTokenHandler struct { bucket orm.ModelBucket } -func (h *registerTokenHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *registerTokenHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: registerTokenCost}, nil } -func (h *registerTokenHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *registerTokenHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -58,7 +58,7 @@ func (h *registerTokenHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *registerTokenHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*RegisterTokenMsg, error) { +func (h *registerTokenHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*RegisterTokenMsg, error) { var msg RegisterTokenMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -80,14 +80,14 @@ type transferTokenHandler struct { bucket orm.ModelBucket } -func (h *transferTokenHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *transferTokenHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: transferTokenCost}, nil } -func (h *transferTokenHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *transferTokenHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, token, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -100,7 +100,7 @@ func (h *transferTokenHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *transferTokenHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferTokenMsg, *Token, error) { +func (h *transferTokenHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TransferTokenMsg, *Token, error) { var msg TransferTokenMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -123,14 +123,14 @@ type changeTokenTargetsHandler struct { bucket orm.ModelBucket } -func (h *changeTokenTargetsHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *changeTokenTargetsHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: changeTokenTargetCost}, nil } -func (h *changeTokenTargetsHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *changeTokenTargetsHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, token, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -143,7 +143,7 @@ func (h *changeTokenTargetsHandler) Deliver(ctx context.Context, db weave.KVStor return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *changeTokenTargetsHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ChangeTokenTargetsMsg, *Token, error) { +func (h *changeTokenTargetsHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ChangeTokenTargetsMsg, *Token, error) { var msg ChangeTokenTargetsMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/docs/design/transactionhandling.rst b/docs/design/transactionhandling.rst index 76c2731c..126ce15b 100644 --- a/docs/design/transactionhandling.rst +++ b/docs/design/transactionhandling.rst @@ -57,8 +57,8 @@ is registered with it. .. code-block:: go type Handler interface { - Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) - Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) + Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) + Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) } The ``Handler`` is provided with the key-value store diff --git a/gconf/handler.go b/gconf/handler.go index d8d0cbc7..9bab8b41 100644 --- a/gconf/handler.go +++ b/gconf/handler.go @@ -35,21 +35,21 @@ func NewUpdateConfigurationHandler(pkg string, config OwnedConfig, auth x.Authen } } -func (h UpdateConfigurationHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateConfigurationHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if err := h.applyTx(ctx, store, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h UpdateConfigurationHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateConfigurationHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { if err := h.applyTx(ctx, store, tx); err != nil { return nil, err } return &weave.DeliverResult{}, nil } -func (h UpdateConfigurationHandler) applyTx(ctx context.Context, store weave.KVStore, tx weave.Tx) error { +func (h UpdateConfigurationHandler) applyTx(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) error { if err := Load(store, h.pkg, h.config); err != nil { return errors.Wrap(err, "load message") } diff --git a/gconf/handler_test.go b/gconf/handler_test.go index a5bcd9d8..531d1ff8 100644 --- a/gconf/handler_test.go +++ b/gconf/handler_test.go @@ -133,12 +133,12 @@ func TestUpdateConfigurationHandler(t *testing.T) { tx := &weavetest.Tx{Msg: tc.Msg} cache := db.CacheWrap() - if _, err := handler.Check(ctx, cache, tx); !tc.WantCheckErr.Is(err) { + if _, err := handler.Check(ctx, info, cache, tx); !tc.WantCheckErr.Is(err) { t.Fatal(err) } cache.Discard() - if _, err := handler.Deliver(ctx, db, tx); !tc.WantDeliverErr.Is(err) { + if _, err := handler.Deliver(ctx, info, db, tx); !tc.WantDeliverErr.Is(err) { t.Fatal(err) } diff --git a/handler.go b/handler.go index 75877323..400069ba 100644 --- a/handler.go +++ b/handler.go @@ -18,27 +18,27 @@ type Handler interface { // It is its own interface to allow better type controls in the next // arguments in Decorator type Checker interface { - Check(ctx context.Context, store KVStore, tx Tx) (*CheckResult, error) + Check(ctx context.Context, info BlockInfo, store KVStore, tx Tx) (*CheckResult, error) } // Deliverer is a subset of Handler to execute a transaction. // It is its own interface to allow better type controls in the next // arguments in Decorator type Deliverer interface { - Deliver(ctx context.Context, store KVStore, tx Tx) (*DeliverResult, error) + Deliver(ctx context.Context, info BlockInfo, store KVStore, tx Tx) (*DeliverResult, error) } // Decorator wraps a Handler to provide common functionality // like authentication, or fee-handling, to many Handlers type Decorator interface { - Check(ctx context.Context, store KVStore, tx Tx, next Checker) (*CheckResult, error) - Deliver(ctx context.Context, store KVStore, tx Tx, next Deliverer) (*DeliverResult, error) + Check(ctx context.Context, info BlockInfo, store KVStore, tx Tx, next Checker) (*CheckResult, error) + Deliver(ctx context.Context, info BlockInfo, store KVStore, tx Tx, next Deliverer) (*DeliverResult, error) } // Ticker is a method that is called the beginning of every block, // which can be used to perform periodic or delayed tasks type Ticker interface { - Tick(ctx context.Context, store KVStore) (TickResult, error) + Tick(ctx context.Context, info BlockInfo, store KVStore) (TickResult, error) } // Registry is an interface to register your handler, diff --git a/migration/handler.go b/migration/handler.go index 09304584..4cba94fb 100644 --- a/migration/handler.go +++ b/migration/handler.go @@ -53,18 +53,18 @@ type schemaMigratingHandler struct { migrations *register } -func (h *schemaMigratingHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *schemaMigratingHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if err := h.migrate(db, tx); err != nil { return nil, errors.Wrap(err, "migration") } - return h.handler.Check(ctx, db, tx) + return h.handler.Check(ctx, info, db, tx) } -func (h *schemaMigratingHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *schemaMigratingHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { if err := h.migrate(db, tx); err != nil { return nil, errors.Wrap(err, "migration") } - return h.handler.Deliver(ctx, db, tx) + return h.handler.Deliver(ctx, info, db, tx) } func (h *schemaMigratingHandler) migrate(db weave.ReadOnlyKVStore, tx weave.Tx) error { @@ -103,14 +103,14 @@ type upgradeSchemaHandler struct { auth x.Authenticator } -func (h *upgradeSchemaHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *upgradeSchemaHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h *upgradeSchemaHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *upgradeSchemaHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -134,7 +134,7 @@ func (h *upgradeSchemaHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h *upgradeSchemaHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpgradeSchemaMsg, error) { +func (h *upgradeSchemaHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpgradeSchemaMsg, error) { var msg UpgradeSchemaMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -182,20 +182,20 @@ type schemaRoutingHandler []weave.Handler var _ weave.Handler = (schemaRoutingHandler)(nil) -func (h schemaRoutingHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h schemaRoutingHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { handler, err := h.selectHandler(tx) if err != nil { return nil, err } - return handler.Check(ctx, db, tx) + return handler.Check(ctx, info, db, tx) } -func (h schemaRoutingHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h schemaRoutingHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { handler, err := h.selectHandler(tx) if err != nil { return nil, err } - return handler.Deliver(ctx, db, tx) + return handler.Deliver(ctx, info, db, tx) } // selectHandler returns the best fitting handler to process given transaction, diff --git a/tmtest/tmtest.go b/tmtest/tmtest.go index 1ced0a7c..ca956cd8 100644 --- a/tmtest/tmtest.go +++ b/tmtest/tmtest.go @@ -18,7 +18,7 @@ import ( // // Set FORCE_TM_TEST=1 environment variable to fail the test if the binary is // not available. This might be desired when running tests by CI. -func RunTendermint(ctx context.Context, t *testing.T, home string) (cleanup func()) { +func RunTendermint(ctx context.Context, info weave.BlockInfo, t *testing.T, home string) (cleanup func()) { t.Helper() tmpath, err := exec.LookPath("tendermint") diff --git a/weavetest/auth.go b/weavetest/auth.go index 0d2755e5..5490ad79 100644 --- a/weavetest/auth.go +++ b/weavetest/auth.go @@ -32,7 +32,7 @@ func (a *Auth) GetConditions(context.Context) []weave.Condition { return a.Signers } -func (a *Auth) HasAddress(ctx context.Context, addr weave.Address) bool { +func (a *Auth) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { for _, s := range a.Signers { if addr.Equals(s.Address()) { return true @@ -53,7 +53,7 @@ type CtxAuth struct { Key string } -func (a *CtxAuth) SetConditions(ctx context.Context, permissions ...weave.Condition) context.Context { +func (a *CtxAuth) SetConditions(ctx context.Context, info weave.BlockInfo, permissions ...weave.Condition) context.Context { return context.WithValue(ctx, a.Key, permissions) } @@ -69,7 +69,7 @@ func (a *CtxAuth) GetConditions(ctx context.Context) []weave.Condition { return conds } -func (a *CtxAuth) HasAddress(ctx context.Context, addr weave.Address) bool { +func (a *CtxAuth) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true diff --git a/weavetest/decorators.go b/weavetest/decorators.go index 46504cf7..57ebb0f9 100644 --- a/weavetest/decorators.go +++ b/weavetest/decorators.go @@ -27,22 +27,22 @@ type Decorator struct { var _ weave.Decorator = (*Decorator)(nil) -func (d *Decorator) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d *Decorator) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { d.checkCall++ if d.CheckErr != nil { return &weave.CheckResult{}, d.CheckErr } - return next.Check(ctx, db, tx) + return next.Check(ctx, info, db, tx) } -func (d *Decorator) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d *Decorator) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { d.deliverCall++ if d.DeliverErr != nil { return &weave.DeliverResult{}, d.DeliverErr } - return next.Deliver(ctx, db, tx) + return next.Deliver(ctx, info, db, tx) } func (d *Decorator) CheckCallCount() int { @@ -68,10 +68,10 @@ type decoratedHandler struct { var _ weave.Handler = (*decoratedHandler)(nil) -func (d *decoratedHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - return d.dc.Check(ctx, db, tx, d.hn) +func (d *decoratedHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { + return d.dc.Check(ctx, info, db, tx, d.hn) } -func (d *decoratedHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - return d.dc.Deliver(ctx, db, tx, d.hn) +func (d *decoratedHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { + return d.dc.Deliver(ctx, info, db, tx, d.hn) } diff --git a/weavetest/handlers.go b/weavetest/handlers.go index f11f24ab..2bfcf391 100644 --- a/weavetest/handlers.go +++ b/weavetest/handlers.go @@ -26,7 +26,7 @@ type Handler struct { var _ weave.Handler = (*Handler)(nil) -func (h *Handler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *Handler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { h.checkCall++ if h.CheckErr != nil { return nil, h.CheckErr @@ -36,7 +36,7 @@ func (h *Handler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*we return &res, nil } -func (h *Handler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *Handler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { h.deliverCall++ if h.DeliverErr != nil { return nil, h.DeliverErr diff --git a/x/aswap/handler.go b/x/aswap/handler.go index c1f6270f..b1d55137 100644 --- a/x/aswap/handler.go +++ b/x/aswap/handler.go @@ -47,7 +47,7 @@ type CreateSwapHandler struct { var _ weave.Handler = CreateSwapHandler{} // Check does the validation and sets the cost of the transaction -func (h CreateSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -60,7 +60,7 @@ func (h CreateSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave } // Deliver moves the tokens from sender to the swap account if all conditions are met. -func (h CreateSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { @@ -94,7 +94,7 @@ func (h CreateSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx wea } // validate does all common pre-processing between Check and Deliver. -func (h CreateSwapHandler) validate(ctx context.Context, +func (h CreateSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { @@ -120,7 +120,7 @@ var _ weave.Handler = ReleaseSwapHandler{} // Check just verifies it is properly formed and returns // the cost of executing it -func (h ReleaseSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReleaseSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -131,7 +131,7 @@ func (h ReleaseSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weav // Deliver moves the tokens from swap account to the receiver if // all preconditions are met. When the swap account is empty it is deleted. -func (h ReleaseSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReleaseSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { swapID, swap, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -158,7 +158,7 @@ func (h ReleaseSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx we } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { +func (h ReleaseSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -193,7 +193,7 @@ var _ weave.Handler = ReturnSwapHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h ReturnSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReturnSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -204,7 +204,7 @@ func (h ReturnSwapHandler) Check(ctx context.Context, db weave.KVStore, tx weave // Deliver moves all the tokens from the swap to the defined sender if // all preconditions are met. The swap is deleted afterwards. -func (h ReturnSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReturnSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, swap, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -229,7 +229,7 @@ func (h ReturnSwapHandler) Deliver(ctx context.Context, db weave.KVStore, tx wea } // validate does all common pre-processing between Check and Deliver. -func (h ReturnSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { +func (h ReturnSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { var msg ReturnSwapMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/aswap/handler_test.go b/x/aswap/handler_test.go index 2e1f15ff..cf42901c 100644 --- a/x/aswap/handler_test.go +++ b/x/aswap/handler_test.go @@ -46,7 +46,7 @@ func TestCreateHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx context.Context, db weave.KVStore) context.Context + setup func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -54,7 +54,7 @@ func TestCreateHandler(t *testing.T) { mutator func(db *aswap.CreateMsg) }{ "Happy Path": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { setBalance(t, db, alice.Address(), initialCoins) return authenticator.SetConditions(ctx, alice) }, @@ -73,7 +73,7 @@ func TestCreateHandler(t *testing.T) { }, }, "happy path, timeout can be in the past": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { setBalance(t, db, alice.Address(), initialCoins) return authenticator.SetConditions(ctx, alice) }, @@ -89,14 +89,14 @@ func TestCreateHandler(t *testing.T) { }, }, "Invalid Auth": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { return authenticator.SetConditions(ctx, pete) }, wantDeliverErr: errors.ErrUnauthorized, wantCheckErr: errors.ErrUnauthorized, }, "Empty account": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { return authenticator.SetConditions(ctx, alice) }, wantDeliverErr: errors.ErrEmpty, @@ -128,13 +128,13 @@ func TestCreateHandler(t *testing.T) { cache := db.CacheWrap() tx := &weavetest.Tx{Msg: createMsg} - if _, err := r.Check(ctx, cache, tx); !spec.wantCheckErr.Is(err) { + if _, err := r.Check(ctx, info, cache, tx); !spec.wantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.wantCheckErr, err) } cache.Discard() - res, err := r.Deliver(ctx, cache, tx) + res, err := r.Deliver(ctx, info, cache, tx) if !spec.wantDeliverErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.wantDeliverErr, err) } @@ -159,7 +159,7 @@ func TestReleaseHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx context.Context, db weave.KVStore) context.Context + setup func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -203,7 +203,7 @@ func TestReleaseHandler(t *testing.T) { }, }, "Expired": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { return weave.WithBlockTime(ctx, time.Now().Add(10*time.Hour)) }, wantDeliverErr: errors.ErrState, @@ -248,13 +248,13 @@ func TestReleaseHandler(t *testing.T) { cache := db.CacheWrap() tx = &weavetest.Tx{Msg: releaseMsg} - if _, err := r.Check(ctx, cache, tx); !spec.wantCheckErr.Is(err) { + if _, err := r.Check(ctx, info, cache, tx); !spec.wantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.wantCheckErr, err) } cache.Discard() - if _, err := r.Deliver(ctx, cache, tx); !spec.wantDeliverErr.Is(err) { + if _, err := r.Deliver(ctx, info, cache, tx); !spec.wantDeliverErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.wantDeliverErr, err) } if spec.check != nil { @@ -271,7 +271,7 @@ func TestReturnHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx context.Context, db weave.KVStore) context.Context + setup func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -279,7 +279,7 @@ func TestReturnHandler(t *testing.T) { mutator func(db *aswap.ReturnSwapMsg) }{ "Happy Path, includes no auth check": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { return weave.WithBlockTime(ctx, blockNow.Add(2*time.Hour)) }, wantDeliverErr: nil, @@ -310,7 +310,7 @@ func TestReturnHandler(t *testing.T) { }, }, "Not Expired": { - setup: func(ctx context.Context, db weave.KVStore) context.Context { + setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { return weave.WithBlockTime(ctx, blockNow) }, wantDeliverErr: errors.ErrState, @@ -354,13 +354,13 @@ func TestReturnHandler(t *testing.T) { cache := db.CacheWrap() tx = &weavetest.Tx{Msg: returnMsg} - if _, err := r.Check(ctx, cache, tx); !spec.wantCheckErr.Is(err) { + if _, err := r.Check(ctx, info, cache, tx); !spec.wantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.wantCheckErr, err) } cache.Discard() - if _, err := r.Deliver(ctx, cache, tx); !spec.wantDeliverErr.Is(err) { + if _, err := r.Deliver(ctx, info, cache, tx); !spec.wantDeliverErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.wantDeliverErr, err) } if spec.check != nil { diff --git a/x/auth.go b/x/auth.go index 2593608e..6483d406 100644 --- a/x/auth.go +++ b/x/auth.go @@ -44,7 +44,7 @@ func (m MultiAuth) GetConditions(ctx context.Context) []weave.Condition { } // HasAddress returns true iff any Authenticator support this -func (m MultiAuth) HasAddress(ctx context.Context, addr weave.Address) bool { +func (m MultiAuth) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { for _, impl := range m.impls { if impl.HasAddress(ctx, addr) { return true @@ -54,7 +54,7 @@ func (m MultiAuth) HasAddress(ctx context.Context, addr weave.Address) bool { } // GetAddresses wraps the GetConditions method of any Authenticator -func GetAddresses(ctx context.Context, auth Authenticator) []weave.Address { +func GetAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator) []weave.Address { perms := auth.GetConditions(ctx) addrs := make([]weave.Address, len(perms)) for i, p := range perms { @@ -64,7 +64,7 @@ func GetAddresses(ctx context.Context, auth Authenticator) []weave.Address { } // MainSigner returns the first permission if any, otherwise nil -func MainSigner(ctx context.Context, auth Authenticator) weave.Condition { +func MainSigner(ctx context.Context, info weave.BlockInfo, auth Authenticator) weave.Condition { signers := auth.GetConditions(ctx) if len(signers) == 0 { return nil @@ -74,7 +74,7 @@ func MainSigner(ctx context.Context, auth Authenticator) weave.Condition { // HasAllAddresses returns true if all elements in required are // also in context. -func HasAllAddresses(ctx context.Context, auth Authenticator, required []weave.Address) bool { +func HasAllAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator, required []weave.Address) bool { for _, r := range required { if !auth.HasAddress(ctx, r) { return false @@ -85,7 +85,7 @@ func HasAllAddresses(ctx context.Context, auth Authenticator, required []weave.A // HasNAddresses returns true if at least n elements in requested are // also in context. -func HasNAddresses(ctx context.Context, auth Authenticator, required []weave.Address, n int) bool { +func HasNAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator, required []weave.Address, n int) bool { // Special case: is this an error??? if n <= 0 { return true @@ -104,14 +104,14 @@ func HasNAddresses(ctx context.Context, auth Authenticator, required []weave.Add // HasAllConditions returns true if all elements in required are // also in context. -func HasAllConditions(ctx context.Context, auth Authenticator, required []weave.Condition) bool { +func HasAllConditions(ctx context.Context, info weave.BlockInfo, auth Authenticator, required []weave.Condition) bool { return HasNConditions(ctx, auth, required, len(required)) } // HasNConditions returns true if at least n elements in requested are // also in context. // Useful for threshold conditions (1 of 3, 3 of 5, etc...) -func HasNConditions(ctx context.Context, auth Authenticator, requested []weave.Condition, n int) bool { +func HasNConditions(ctx context.Context, info weave.BlockInfo, auth Authenticator, requested []weave.Condition, n int) bool { // Special case: is this an error??? if n <= 0 { return true diff --git a/x/batch/decorator.go b/x/batch/decorator.go index 8df755ee..fd61a3ad 100644 --- a/x/batch/decorator.go +++ b/x/batch/decorator.go @@ -43,7 +43,7 @@ func (tx *BatchTx) GetMsg() (weave.Msg, error) { // Check iterates through messages in a batch transaction and passes them // down the stack -func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d Decorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, err @@ -51,7 +51,7 @@ func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, batchMsg, ok := msg.(Msg) if !ok { - return next.Check(ctx, store, tx) + return next.Check(ctx, info, store, tx) } if err = batchMsg.Validate(); err != nil { @@ -62,7 +62,7 @@ func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, checks := make([]*weave.CheckResult, len(msgList)) for i, msg := range msgList { - checks[i], err = next.Check(ctx, store, &BatchTx{Tx: tx, msg: msg}) + checks[i], err = next.Check(ctx, info, store, &BatchTx{Tx: tx, msg: msg}) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func (*Decorator) combineChecks(checks []*weave.CheckResult) (*weave.CheckResult // Deliver iterates through messages in a batch transaction and passes them // down the stack -func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d Decorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { msg, err := tx.GetMsg() if err != nil { return nil, err @@ -114,7 +114,7 @@ func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx batchMsg, ok := msg.(Msg) if !ok { - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } if err = batchMsg.Validate(); err != nil { @@ -125,7 +125,7 @@ func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx delivers := make([]*weave.DeliverResult, len(msgList)) for i, msg := range msgList { - delivers[i], err = next.Deliver(ctx, store, &BatchTx{Tx: tx, msg: msg}) + delivers[i], err = next.Deliver(ctx, info, store, &BatchTx{Tx: tx, msg: msg}) if err != nil { return nil, err } diff --git a/x/batch/decorator_test.go b/x/batch/decorator_test.go index ac4500c4..38116d59 100644 --- a/x/batch/decorator_test.go +++ b/x/batch/decorator_test.go @@ -49,12 +49,12 @@ func (m *mockHelper) GetMsg() (weave.Msg, error) { return args.Get(0).(weave.Msg), args.Error(1) } -func (m *mockHelper) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (m *mockHelper) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { args := m.Called(ctx, store, tx) return args.Get(0).(*weave.CheckResult), args.Error(1) } -func (m *mockHelper) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (m *mockHelper) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { args := m.Called(ctx, store, tx) return args.Get(0).(*weave.DeliverResult), args.Error(1) } diff --git a/x/cash/dynamicfee.go b/x/cash/dynamicfee.go index 6f4ceff9..b3456520 100644 --- a/x/cash/dynamicfee.go +++ b/x/cash/dynamicfee.go @@ -58,7 +58,7 @@ func NewDynamicFeeDecorator(auth x.Authenticator, ctrl Controller) DynamicFeeDec } // Check verifies and deducts fees before calling down the stack -func (d DynamicFeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (cres *weave.CheckResult, cerr error) { +func (d DynamicFeeDecorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (cres *weave.CheckResult, cerr error) { fee, payer, cache, err := d.prepare(ctx, store, tx) if err != nil { return nil, errors.Wrap(err, "cannot prepare") @@ -86,7 +86,7 @@ func (d DynamicFeeDecorator) Check(ctx context.Context, store weave.KVStore, tx if err := d.chargeFee(cache, payer, fee); err != nil { return nil, errors.Wrap(err, "cannot charge fee") } - cres, err = next.Check(ctx, cache, tx) + cres, err = next.Check(ctx, info, cache, tx) if err != nil { return nil, err } @@ -98,7 +98,7 @@ func (d DynamicFeeDecorator) Check(ctx context.Context, store weave.KVStore, tx } // Deliver verifies and deducts fees before calling down the stack -func (d DynamicFeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (dres *weave.DeliverResult, derr error) { +func (d DynamicFeeDecorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (dres *weave.DeliverResult, derr error) { fee, payer, cache, err := d.prepare(ctx, store, tx) if err != nil { return nil, errors.Wrap(err, "cannot prepare") @@ -124,7 +124,7 @@ func (d DynamicFeeDecorator) Deliver(ctx context.Context, store weave.KVStore, t if err := d.chargeFee(cache, payer, fee); err != nil { return nil, errors.Wrap(err, "cannot charge fee") } - res, err := next.Deliver(ctx, cache, tx) + res, err := next.Deliver(ctx, info, cache, tx) if err != nil { return res, err } @@ -158,7 +158,7 @@ func (d DynamicFeeDecorator) chargeMinimalFee(store weave.KVStore, src weave.Add // prepare is all shared setup between Check and Deliver. It computes the fee // for the transaction, ensures that the payer is authenticated and prepares // the database transaction. -func (d DynamicFeeDecorator) prepare(ctx context.Context, store weave.KVStore, tx weave.Tx) (fee coin.Coin, payer weave.Address, cache weave.KVCacheWrap, err error) { +func (d DynamicFeeDecorator) prepare(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (fee coin.Coin, payer weave.Address, cache weave.KVCacheWrap, err error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return fee, payer, cache, errors.Wrap(err, "cannot extract fee") @@ -186,7 +186,7 @@ func (d DynamicFeeDecorator) prepare(ctx context.Context, store weave.KVStore, t } // this returns the fee info to deduct and the error if incorrectly set -func (d DynamicFeeDecorator) extractFee(ctx context.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { +func (d DynamicFeeDecorator) extractFee(ctx context.Context, info weave.BlockInfo, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { var finfo *FeeInfo ftx, ok := tx.(FeeTx) if ok { diff --git a/x/cash/handler.go b/x/cash/handler.go index 7e1a846e..c79fff03 100644 --- a/x/cash/handler.go +++ b/x/cash/handler.go @@ -42,7 +42,7 @@ func NewSendHandler(auth x.Authenticator, control Controller) SendHandler { // Check just verifies it is properly formed and returns // the cost of executing it -func (h SendHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h SendHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { var msg SendMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -61,7 +61,7 @@ func (h SendHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx // Deliver moves the tokens from sender to receiver if // all preconditions are met -func (h SendHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h SendHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { var msg SendMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/cash/staticfee.go b/x/cash/staticfee.go index cdd9f968..05c0e725 100644 --- a/x/cash/staticfee.go +++ b/x/cash/staticfee.go @@ -41,7 +41,7 @@ func NewFeeDecorator(auth x.Authenticator, ctrl CoinMover) FeeDecorator { } // Check verifies and deducts fees before calling down the stack -func (d FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d FeeDecorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return nil, err @@ -50,7 +50,7 @@ func (d FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.T // if nothing returned, but no error, just move along fee := finfo.GetFees() if coin.IsEmpty(fee) { - return next.Check(ctx, store, tx) + return next.Check(ctx, info, store, tx) } // verify we have access to the money @@ -66,7 +66,7 @@ func (d FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.T // now update the importance... paid := toPayment(*fee) - res, err := next.Check(ctx, store, tx) + res, err := next.Check(ctx, info, store, tx) if err != nil { return nil, err } @@ -75,7 +75,7 @@ func (d FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.T } // Deliver verifies and deducts fees before calling down the stack -func (d FeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d FeeDecorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return nil, err @@ -84,7 +84,7 @@ func (d FeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave // if nothing returned, but no error, just move along fee := finfo.GetFees() if coin.IsEmpty(fee) { - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } // verify we have access to the money @@ -98,10 +98,10 @@ func (d FeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave return nil, err } - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } -func (d FeeDecorator) extractFee(ctx context.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { +func (d FeeDecorator) extractFee(ctx context.Context, info weave.BlockInfo, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { var finfo *FeeInfo ftx, ok := tx.(FeeTx) if ok { diff --git a/x/currency/handler.go b/x/currency/handler.go index 3d4167ae..0367c319 100644 --- a/x/currency/handler.go +++ b/x/currency/handler.go @@ -34,14 +34,14 @@ type createTokenInfoHandler struct { issuer weave.Address } -func (h *createTokenInfoHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *createTokenInfoHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: newTokenInfoCost}, nil } -func (h *createTokenInfoHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *createTokenInfoHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -50,7 +50,7 @@ func (h *createTokenInfoHandler) Deliver(ctx context.Context, db weave.KVStore, return &weave.DeliverResult{}, h.bucket.Save(db, obj) } -func (h *createTokenInfoHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createTokenInfoHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/distribution/handler.go b/x/distribution/handler.go index 09b53685..077ddc25 100644 --- a/x/distribution/handler.go +++ b/x/distribution/handler.go @@ -56,14 +56,14 @@ type createRevenueHandler struct { ctrl CashController } -func (h *createRevenueHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *createRevenueHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: newRevenueCost}, nil } -func (h *createRevenueHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *createRevenueHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -80,7 +80,7 @@ func (h *createRevenueHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h *createRevenueHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createRevenueHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -94,7 +94,7 @@ type distributeHandler struct { ctrl CashController } -func (h *distributeHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *distributeHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -110,7 +110,7 @@ func (h *distributeHandler) Check(ctx context.Context, db weave.KVStore, tx weav return &res, nil } -func (h *distributeHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *distributeHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -131,7 +131,7 @@ func (h *distributeHandler) Deliver(ctx context.Context, db weave.KVStore, tx we return &weave.DeliverResult{}, nil } -func (h *distributeHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DistributeMsg, error) { +func (h *distributeHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*DistributeMsg, error) { var msg DistributeMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -148,7 +148,7 @@ type resetRevenueHandler struct { ctrl CashController } -func (h *resetRevenueHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *resetRevenueHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -167,7 +167,7 @@ func (h *resetRevenueHandler) Check(ctx context.Context, db weave.KVStore, tx we return &res, nil } -func (h *resetRevenueHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *resetRevenueHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -197,7 +197,7 @@ func (h *resetRevenueHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{}, nil } -func (h *resetRevenueHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ResetMsg, error) { +func (h *resetRevenueHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ResetMsg, error) { var msg ResetMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/escrow/handler.go b/x/escrow/handler.go index 3028c8b5..01155823 100644 --- a/x/escrow/handler.go +++ b/x/escrow/handler.go @@ -47,7 +47,7 @@ var _ weave.Handler = CreateEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h CreateEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -61,7 +61,7 @@ func (h CreateEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx wea // Deliver moves the tokens from sender to the escrow account if // all preconditions are met. -func (h CreateEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -101,7 +101,7 @@ func (h CreateEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx w } // validate does all common pre-processing between Check and Deliver. -func (h CreateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -132,7 +132,7 @@ var _ weave.Handler = ReleaseEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it -func (h ReleaseEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReleaseEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -143,7 +143,7 @@ func (h ReleaseEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx we // Deliver moves the tokens from escrow account to the receiver if // all preconditions are met. When the escrow account is empty it is deleted. -func (h ReleaseEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReleaseEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, escrow, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -181,7 +181,7 @@ func (h ReleaseEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { +func (h ReleaseEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -215,7 +215,7 @@ var _ weave.Handler = ReturnEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h ReturnEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h ReturnEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -226,7 +226,7 @@ func (h ReturnEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx wea // Deliver moves all the tokens from the escrow to the defined sender if // all preconditions are met. The escrow is deleted afterwards. -func (h ReturnEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h ReturnEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { key, escrow, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -250,7 +250,7 @@ func (h ReturnEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx w } // validate does all common pre-processing between Check and Deliver. -func (h ReturnEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { +func (h ReturnEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { var msg ReturnMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -278,7 +278,7 @@ var _ weave.Handler = UpdateEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. -func (h UpdateEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -289,7 +289,7 @@ func (h UpdateEscrowHandler) Check(ctx context.Context, db weave.KVStore, tx wea // Deliver updates the any of the sender, recipient or arbiter if // all preconditions are met. No coins are moved. -func (h UpdateEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, escrow, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -316,7 +316,7 @@ func (h UpdateEscrowHandler) Deliver(ctx context.Context, db weave.KVStore, tx w } // validate does all common pre-processing between Check and Deliver. -func (h UpdateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { +func (h UpdateEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { var msg UpdatePartiesMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/gov/context.go b/x/gov/context.go index a0e77766..56985b3c 100644 --- a/x/gov/context.go +++ b/x/gov/context.go @@ -20,7 +20,7 @@ type proposalWrapper struct { proposalID []byte } -func withElectionSuccess(ctx context.Context, ruleID []byte) context.Context { +func withElectionSuccess(ctx context.Context, info weave.BlockInfo, ruleID []byte) context.Context { val, _ := ctx.Value(contextKeyGov).([]weave.Condition) return context.WithValue(ctx, contextKeyGov, append(val, ElectionCondition(ruleID))) } @@ -44,7 +44,7 @@ func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { } // HasAddress returns true iff this address is in GetConditions. -func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true @@ -53,7 +53,7 @@ func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { return false } -func withProposal(ctx context.Context, proposal *Proposal, proposalID []byte) context.Context { +func withProposal(ctx context.Context, info weave.BlockInfo, proposal *Proposal, proposalID []byte) context.Context { return context.WithValue(ctx, contextKeyProposal, proposalWrapper{proposal: proposal, proposalID: proposalID}) } diff --git a/x/gov/handler.go b/x/gov/handler.go index 1e3df0f6..660199e0 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -67,7 +67,7 @@ func newVoteHandler(auth x.Authenticator) *VoteHandler { } } -func (h VoteHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h VoteHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -75,7 +75,7 @@ func (h VoteHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) ( } -func (h VoteHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h VoteHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { voteMsg, proposal, vote, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -103,7 +103,7 @@ func (h VoteHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) return &weave.DeliverResult{}, nil } -func (h VoteHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { +func (h VoteHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { var msg VoteMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -171,7 +171,7 @@ func newTallyHandler(auth x.Authenticator, decoder OptionDecoder, executor Execu } } -func (h TallyHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h TallyHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -179,7 +179,7 @@ func (h TallyHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) } -func (h TallyHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (resOut *weave.DeliverResult, errOut error) { +func (h TallyHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (resOut *weave.DeliverResult, errOut error) { msg, proposal, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -246,7 +246,7 @@ func (h TallyHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx return res, nil } -func (h TallyHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { +func (h TallyHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { var msg TallyMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -286,7 +286,7 @@ func newCreateProposalHandler(auth x.Authenticator, decoder OptionDecoder) *Crea } } -func (h CreateProposalHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateProposalHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -294,7 +294,7 @@ func (h CreateProposalHandler) Check(ctx context.Context, db weave.KVStore, tx w } -func (h CreateProposalHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, rule, electorate, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -329,7 +329,7 @@ func (h CreateProposalHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h CreateProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { +func (h CreateProposalHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { var msg CreateProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -407,7 +407,7 @@ func newDeleteProposalHandler(auth x.Authenticator) *DeleteProposalHandler { } } -func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { +func (h DeleteProposalHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { var msg DeleteProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -430,14 +430,14 @@ func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, t return &msg, prop, nil } -func (h DeleteProposalHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h DeleteProposalHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: deleteProposalCost}, nil } -func (h DeleteProposalHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h DeleteProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, prop, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -466,7 +466,7 @@ func newUpdateElectorateHandler(auth x.Authenticator) *UpdateElectorateHandler { } } -func (h UpdateElectorateHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateElectorateHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -474,7 +474,7 @@ func (h UpdateElectorateHandler) Check(ctx context.Context, db weave.KVStore, tx return &weave.CheckResult{GasAllocated: updateElectorateCost}, nil } -func (h UpdateElectorateHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateElectorateHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, elect, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -490,7 +490,7 @@ func (h UpdateElectorateHandler) Deliver(ctx context.Context, db weave.KVStore, return &weave.DeliverResult{Data: msg.ElectorateID}, nil } -func (h UpdateElectorateHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectorateMsg, *Electorate, error) { +func (h UpdateElectorateHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdateElectorateMsg, *Electorate, error) { var msg UpdateElectorateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -528,7 +528,7 @@ func newUpdateElectionRuleHandler(auth x.Authenticator) *UpdateElectionRuleHandl } } -func (h UpdateElectionRuleHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateElectionRuleHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -536,7 +536,7 @@ func (h UpdateElectionRuleHandler) Check(ctx context.Context, db weave.KVStore, return &weave.CheckResult{GasAllocated: updateElectionRuleCost}, nil } -func (h UpdateElectionRuleHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateElectionRuleHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, rule, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -549,7 +549,7 @@ func (h UpdateElectionRuleHandler) Deliver(ctx context.Context, db weave.KVStore return &weave.DeliverResult{Data: msg.ElectionRuleID}, nil } -func (h UpdateElectionRuleHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectionRuleMsg, *ElectionRule, error) { +func (h UpdateElectionRuleHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdateElectionRuleMsg, *ElectionRule, error) { var msg UpdateElectionRuleMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -580,7 +580,7 @@ func newCreateTextResolutionHandler(auth x.Authenticator) *createTextResolutionH } } -func (h createTextResolutionHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h createTextResolutionHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -588,7 +588,7 @@ func (h createTextResolutionHandler) Check(ctx context.Context, db weave.KVStore return &weave.CheckResult{GasAllocated: textResolutionCost}, nil } -func (h createTextResolutionHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h createTextResolutionHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -613,7 +613,7 @@ func (h createTextResolutionHandler) Deliver(ctx context.Context, db weave.KVSto return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h createTextResolutionHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateTextResolutionMsg, error) { +func (h createTextResolutionHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateTextResolutionMsg, error) { var msg CreateTextResolutionMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/gov/handler_test.go b/x/gov/handler_test.go index 0a551337..83c0f983 100644 --- a/x/gov/handler_test.go +++ b/x/gov/handler_test.go @@ -370,14 +370,14 @@ func TestCreateTextProposal(t *testing.T) { // when check is called tx := &weavetest.Tx{Msg: &spec.Msg} - if _, err := rt.Check(ctx, cache, tx); !spec.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver is called - res, err := rt.Deliver(ctx, db, tx) + res, err := rt.Deliver(ctx, info, db, tx) if !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantCheckErr, err) } @@ -414,7 +414,7 @@ func TestDeleteProposal(t *testing.T) { Msg: DeleteProposalMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID}, SignedBy: hAliceCond, ProposalDeleted: true, - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(1 * time.Hour)) proposal.VotingEndTime = weave.AsUnixTime(time.Now().Add(2 * time.Hour)) }, @@ -430,7 +430,7 @@ func TestDeleteProposal(t *testing.T) { SignedBy: hBobbyCond, WantCheckErr: errors.ErrUnauthorized, WantDeliverErr: errors.ErrUnauthorized, - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(1 * time.Hour)) proposal.VotingEndTime = weave.AsUnixTime(time.Now().Add(2 * time.Hour)) }, @@ -438,7 +438,7 @@ func TestDeleteProposal(t *testing.T) { "Voting has started": { Msg: DeleteProposalMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID}, SignedBy: hAliceCond, - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(-1 * time.Hour)) proposal.SubmissionTime = weave.AsUnixTime(time.Now().Add(-2 * time.Hour)) }, @@ -465,13 +465,13 @@ func TestDeleteProposal(t *testing.T) { // when check tx := &weavetest.Tx{Msg: &spec.Msg} - if _, err := rt.Check(ctx, cache, tx); !spec.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver - if _, err := rt.Deliver(ctx, db, tx); !spec.WantDeliverErr.Is(err) { + if _, err := rt.Deliver(ctx, info, db, tx); !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantCheckErr, err) } @@ -570,7 +570,7 @@ func TestVote(t *testing.T) { nonElectorCond := weavetest.NewCondition() nonElector := nonElectorCond.Address() specs := map[string]struct { - Init func(ctx context.Context, db store.KVStore) // executed before test fixtures + Init func(ctx context.Context, info weave.BlockInfo, db store.KVStore) // executed before test fixtures Mods func(context.Context, *Proposal) // modifies test fixtures before storing Msg VoteMsg SignedBy weave.Condition @@ -610,7 +610,7 @@ func TestVote(t *testing.T) { ExpVotedBy: hAlice, }, "Can change vote": { - Init: func(ctx context.Context, db store.KVStore) { + Init: func(ctx context.Context, info weave.BlockInfo, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -621,7 +621,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VoteState.TotalYes = 10 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_No, Voter: hBobby}, @@ -630,7 +630,7 @@ func TestVote(t *testing.T) { ExpVotedBy: hBobby, }, "Can resubmit vote": { - Init: func(ctx context.Context, db store.KVStore) { + Init: func(ctx context.Context, info weave.BlockInfo, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -641,7 +641,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VoteState.TotalYes = 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -668,7 +668,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrUnauthorized, }, "Vote before start date": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingStartTime = unixBlockTime(t, ctx) + 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -677,7 +677,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on start date": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingStartTime = unixBlockTime(t, ctx) }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -686,7 +686,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on end date": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingEndTime = unixBlockTime(t, ctx) }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -695,7 +695,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote after end date": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.VotingEndTime = unixBlockTime(t, ctx) - 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -704,7 +704,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on withdrawn proposal must fail": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.Status = Proposal_Withdrawn }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -713,7 +713,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on closed proposal must fail": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { proposal.Status = Proposal_Closed }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -722,7 +722,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Sanity check on count vote": { - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { // not a valid setup proposal.VoteState.TotalYes = math.MaxUint64 proposal.VoteState.TotalElectorateWeight = math.MaxUint64 @@ -732,7 +732,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrHuman, }, "Sanity check on undo count vote": { - Init: func(ctx context.Context, db store.KVStore) { + Init: func(ctx context.Context, info weave.BlockInfo, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -743,7 +743,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx context.Context, proposal *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { // not a valid setup proposal.VoteState.TotalYes = 0 proposal.VoteState.TotalElectorateWeight = math.MaxUint64 @@ -775,13 +775,13 @@ func TestVote(t *testing.T) { // when check tx := &weavetest.Tx{Msg: &spec.Msg} - if _, err := rt.Check(ctx, cache, tx); !spec.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver - if _, err := rt.Deliver(ctx, db, tx); !spec.WantDeliverErr.Is(err) { + if _, err := rt.Deliver(ctx, info, db, tx); !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantCheckErr, err) } @@ -1089,7 +1089,7 @@ func TestTally(t *testing.T) { t.Fatalf("unexpected error: %+v", err) } }, - Mods: func(ctx context.Context, p *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 10}) p.VotingEndTime = unixBlockTime(t, ctx) - 1 }, @@ -1142,7 +1142,7 @@ func TestTally(t *testing.T) { t.Fatalf("unexpected error: %+v", err) } }, - Mods: func(ctx context.Context, p *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 0}) p.VotingEndTime = unixBlockTime(t, ctx) - 1 }, @@ -1158,7 +1158,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal accepted: execution error:", }, "Does not update an electorate when rejected": { - Mods: func(ctx context.Context, p *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 10}) p.VotingEndTime = unixBlockTime(t, ctx) - 1 }, @@ -1203,7 +1203,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on tally before end date": { - Mods: func(ctx context.Context, p *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { p.VotingEndTime = unixBlockTime(t, ctx) + 1 }, Src: tallySetup{ @@ -1216,7 +1216,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on tally at end date": { - Mods: func(ctx context.Context, p *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { p.VotingEndTime = unixBlockTime(t, ctx) }, Src: tallySetup{ @@ -1229,7 +1229,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on withdrawn proposal": { - Mods: func(ctx context.Context, p *Proposal) { + Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { p.Status = Proposal_Withdrawn }, Src: tallySetup{ @@ -1270,14 +1270,14 @@ func TestTally(t *testing.T) { // when check is called tx := &weavetest.Tx{Msg: &TallyMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: weavetest.SequenceID(1)}} - if _, err := rt.Check(ctx, cache, tx); !spec.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver is called - dres, err := rt.Deliver(ctx, db, tx) + dres, err := rt.Deliver(ctx, info, db, tx) if !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantDeliverErr, err) } @@ -1440,14 +1440,14 @@ func TestUpdateElectorate(t *testing.T) { ctx := context.Background() // when check is called tx := &weavetest.Tx{Msg: &spec.Msg} - if _, err := rt.Check(ctx, cache, tx); !spec.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver is called - res, err := rt.Deliver(ctx, db, tx) + res, err := rt.Deliver(ctx, info, db, tx) if !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantCheckErr, err) } @@ -1575,14 +1575,14 @@ func TestUpdateElectionRules(t *testing.T) { ctx := context.Background() // when check is called tx := &weavetest.Tx{Msg: &spec.Msg} - if _, err := rt.Check(ctx, cache, tx); !spec.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver is called - res, err := rt.Deliver(ctx, db, tx) + res, err := rt.Deliver(ctx, info, db, tx) if !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantCheckErr, err) } diff --git a/x/gov/helpers_test.go b/x/gov/helpers_test.go index ff0b4eaf..c4e5e163 100644 --- a/x/gov/helpers_test.go +++ b/x/gov/helpers_test.go @@ -14,7 +14,7 @@ import ( // ctxAwareMutator is a call back interface to modify the passed proposal for test setup type ctxAwareMutator func(context.Context, *Proposal) -func withTextProposal(t *testing.T, db store.KVStore, ctx context.Context, mods ...ctxAwareMutator) *ProposalBucket { +func withTextProposal(t *testing.T, db store.KVStore, ctx context.Context, info weave.BlockInfo, mods ...ctxAwareMutator) *ProposalBucket { t.Helper() // setup electorate withElectorate(t, db) diff --git a/x/gov/interfaces.go b/x/gov/interfaces.go index a19312f8..0d63f6fb 100644 --- a/x/gov/interfaces.go +++ b/x/gov/interfaces.go @@ -2,6 +2,7 @@ package gov import ( "context" + weave "github.com/iov-one/weave" ) @@ -9,15 +10,15 @@ import ( type OptionDecoder func(raw []byte) (weave.Msg, error) // Executor will do something with the message once it is approved. -type Executor func(ctx context.Context, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) +type Executor func(ctx context.Context, info weave.BlockInfo, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) // HandlerAsExecutor wraps the msg in a fake Tx to satisfy the Handler interface // Since a Router and Decorators also expose this interface, we can wrap any stack // that does not care about the extra Tx info besides Msg. func HandlerAsExecutor(h weave.Handler) Executor { - return func(ctx context.Context, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) { + return func(ctx context.Context, info weave.BlockInfo, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) { tx := &fakeTx{msg: msg} - return h.Deliver(ctx, store, tx) + return h.Deliver(ctx, info, info, store, tx) } } diff --git a/x/msgfee/antispam_fee_decorator.go b/x/msgfee/antispam_fee_decorator.go index 18ee9c1c..0cca7c89 100644 --- a/x/msgfee/antispam_fee_decorator.go +++ b/x/msgfee/antispam_fee_decorator.go @@ -29,8 +29,8 @@ func NewAntispamFeeDecorator(fee coin.Coin) *AntispamFeeDecorator { return &AntispamFeeDecorator{fee: fee} } -func (d *AntispamFeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { - res, err := next.Check(ctx, store, tx) +func (d *AntispamFeeDecorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { + res, err := next.Check(ctx, info, store, tx) if d == nil { // Since NewAntispamFeeDecorator can return nil, let's be graceful here return res, err } @@ -50,6 +50,6 @@ func (d *AntispamFeeDecorator) Check(ctx context.Context, store weave.KVStore, t return res, nil } -func (d *AntispamFeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { - return next.Deliver(ctx, store, tx) +func (d *AntispamFeeDecorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { + return next.Deliver(ctx, info, store, tx) } diff --git a/x/msgfee/fee_decorator.go b/x/msgfee/fee_decorator.go index d0b712ab..f90d10cc 100644 --- a/x/msgfee/fee_decorator.go +++ b/x/msgfee/fee_decorator.go @@ -28,8 +28,8 @@ func NewFeeDecorator() *FeeDecorator { } } -func (d *FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { - res, err := next.Check(ctx, store, tx) +func (d *FeeDecorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { + res, err := next.Check(ctx, info, store, tx) if err != nil { return nil, err } @@ -48,8 +48,8 @@ func (d *FeeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave. return res, nil } -func (d *FeeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { - res, err := next.Deliver(ctx, store, tx) +func (d *FeeDecorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { + res, err := next.Deliver(ctx, info, store, tx) if err != nil { return nil, err } diff --git a/x/multisig/context.go b/x/multisig/context.go index 5246ec6b..054d5292 100644 --- a/x/multisig/context.go +++ b/x/multisig/context.go @@ -15,7 +15,7 @@ const ( // withMultisig is a private method, as only this module // can add a multisig signer -func withMultisig(ctx context.Context, id []byte) context.Context { +func withMultisig(ctx context.Context, info weave.BlockInfo, id []byte) context.Context { val, _ := ctx.Value(contextKeyMultisig).([]weave.Condition) if val == nil { return context.WithValue(ctx, contextKeyMultisig, []weave.Condition{MultiSigCondition(id)}) @@ -46,7 +46,7 @@ func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { } // HasAddress returns true iff this address is in GetConditions -func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true diff --git a/x/multisig/decorator.go b/x/multisig/decorator.go index ea49422c..f3521355 100644 --- a/x/multisig/decorator.go +++ b/x/multisig/decorator.go @@ -26,7 +26,7 @@ func NewDecorator(auth x.Authenticator) Decorator { } // Check enforce multisig contract before calling down the stack -func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d Decorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { newCtx, cost, err := d.authMultisig(ctx, store, tx) if err != nil { return nil, err @@ -41,7 +41,7 @@ func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, } // Deliver enforces multisig contract before calling down the stack -func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d Decorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { newCtx, _, err := d.authMultisig(ctx, store, tx) if err != nil { return nil, err @@ -50,7 +50,7 @@ func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx return next.Deliver(newCtx, store, tx) } -func (d Decorator) authMultisig(ctx context.Context, store weave.KVStore, tx weave.Tx) (context.Context, int64, error) { +func (d Decorator) authMultisig(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (context.Context, int64, error) { multisigContract, ok := tx.(MultiSigTx) if !ok { return ctx, 0, nil diff --git a/x/multisig/decorator_test.go b/x/multisig/decorator_test.go index 8c7141f3..c784bc0e 100644 --- a/x/multisig/decorator_test.go +++ b/x/multisig/decorator_test.go @@ -126,7 +126,7 @@ func TestDecorator(t *testing.T) { var hn MultisigCheckHandler stack := weavetest.Decorate(&hn, d) - cres, err := stack.Check(ctx, db, tc.tx) + cres, err := stack.Check(ctx, info, db, tc.tx) if !tc.wantErr.Is(err) { t.Fatalf("unexpected error: %+v", err) } @@ -134,7 +134,7 @@ func TestDecorator(t *testing.T) { t.Errorf("want %d gas payment, got %d", tc.wantGas, cres.GasPayment) } - if _, err := stack.Deliver(ctx, db, tc.tx); !tc.wantErr.Is(err) { + if _, err := stack.Deliver(ctx, info, db, tc.tx); !tc.wantErr.Is(err) { t.Fatalf("unexpected error: %+v", err) } }) @@ -149,12 +149,12 @@ type MultisigCheckHandler struct { var _ weave.Handler = (*MultisigCheckHandler)(nil) -func (s *MultisigCheckHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (s *MultisigCheckHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { s.Perms = Authenticate{}.GetConditions(ctx) return &weave.CheckResult{}, nil } -func (s *MultisigCheckHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (s *MultisigCheckHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { s.Perms = Authenticate{}.GetConditions(ctx) return &weave.DeliverResult{}, nil } diff --git a/x/multisig/handlers.go b/x/multisig/handlers.go index c8ad26e7..0d4ca09f 100644 --- a/x/multisig/handlers.go +++ b/x/multisig/handlers.go @@ -30,7 +30,7 @@ type CreateMsgHandler struct { var _ weave.Handler = CreateMsgHandler{} -func (h CreateMsgHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h CreateMsgHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -39,7 +39,7 @@ func (h CreateMsgHandler) Check(ctx context.Context, db weave.KVStore, tx weave. return &weave.CheckResult{GasAllocated: creationCost}, nil } -func (h CreateMsgHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h CreateMsgHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -63,7 +63,7 @@ func (h CreateMsgHandler) Deliver(ctx context.Context, db weave.KVStore, tx weav } // validate does all common pre-processing between Check and Deliver. -func (h CreateMsgHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateMsgHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { // Retrieve tx main signer in this context. sender := x.MainSigner(ctx, h.auth) if sender == nil { @@ -85,7 +85,7 @@ type UpdateMsgHandler struct { var _ weave.Handler = CreateMsgHandler{} -func (h UpdateMsgHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h UpdateMsgHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { _, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -93,7 +93,7 @@ func (h UpdateMsgHandler) Check(ctx context.Context, db weave.KVStore, tx weave. return &weave.CheckResult{GasAllocated: updateCost}, nil } -func (h UpdateMsgHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h UpdateMsgHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -115,7 +115,7 @@ func (h UpdateMsgHandler) Deliver(ctx context.Context, db weave.KVStore, tx weav return &weave.DeliverResult{}, nil } -func (h UpdateMsgHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateMsg, error) { +func (h UpdateMsgHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdateMsg, error) { var msg UpdateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/multisig/handlers_test.go b/x/multisig/handlers_test.go index 762fa4a9..5784ffa8 100644 --- a/x/multisig/handlers_test.go +++ b/x/multisig/handlers_test.go @@ -97,7 +97,7 @@ func TestCreateContractHandler(t *testing.T) { tx := &weavetest.Tx{Msg: tc.Msg} cache := db.CacheWrap() - if _, err := rt.Check(ctx, cache, tx); !tc.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !tc.WantCheckErr.Is(err) { t.Logf("want: %+v", tc.WantCheckErr) t.Logf(" got: %+v", err) t.Fatalf("check (%T)", tc.Msg) @@ -108,7 +108,7 @@ func TestCreateContractHandler(t *testing.T) { return } - if _, err := rt.Deliver(ctx, db, tx); !tc.WantDeliverErr.Is(err) { + if _, err := rt.Deliver(ctx, info, db, tx); !tc.WantDeliverErr.Is(err) { t.Logf("want: %+v", tc.WantDeliverErr) t.Logf(" got: %+v", err) t.Fatalf("delivery (%T)", tc.Msg) @@ -277,7 +277,7 @@ func TestUpdateContractHandler(t *testing.T) { assert.Nil(t, err) cache := db.CacheWrap() - if _, err := rt.Check(ctx, cache, tx); !tc.WantCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !tc.WantCheckErr.Is(err) { t.Logf("want: %+v", tc.WantCheckErr) t.Logf(" got: %+v", err) t.Fatalf("check (%T)", tc.Msg) @@ -288,7 +288,7 @@ func TestUpdateContractHandler(t *testing.T) { return } - if _, err := rt.Deliver(ctx, db, tx); !tc.WantDeliverErr.Is(err) { + if _, err := rt.Deliver(ctx, info, db, tx); !tc.WantDeliverErr.Is(err) { t.Logf("want: %+v", tc.WantDeliverErr) t.Logf(" got: %+v", err) t.Fatalf("delivery (%T)", tc.Msg) diff --git a/x/paychan/handler.go b/x/paychan/handler.go index 8edbab87..428e47ec 100644 --- a/x/paychan/handler.go +++ b/x/paychan/handler.go @@ -42,7 +42,7 @@ type createPaymentChannelHandler struct { var _ weave.Handler = (*createPaymentChannelHandler)(nil) -func (h *createPaymentChannelHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *createPaymentChannelHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } @@ -50,7 +50,7 @@ func (h *createPaymentChannelHandler) Check(ctx context.Context, db weave.KVStor return &weave.CheckResult{GasAllocated: createPaymentChannelCost}, nil } -func (h *createPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createPaymentChannelHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -64,7 +64,7 @@ func (h *createPaymentChannelHandler) validate(ctx context.Context, db weave.KVS return &msg, nil } -func (h *createPaymentChannelHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *createPaymentChannelHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -101,14 +101,14 @@ type transferPaymentChannelHandler struct { var _ weave.Handler = (*transferPaymentChannelHandler)(nil) -func (h *transferPaymentChannelHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *transferPaymentChannelHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: transferPaymentChannelCost}, nil } -func (h *transferPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { +func (h *transferPaymentChannelHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { var msg TransferMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -148,7 +148,7 @@ func (h *transferPaymentChannelHandler) validate(ctx context.Context, db weave.K return &msg, nil } -func (h *transferPaymentChannelHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *transferPaymentChannelHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -205,7 +205,7 @@ type closePaymentChannelHandler struct { var _ weave.Handler = (*closePaymentChannelHandler)(nil) -func (h *closePaymentChannelHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *closePaymentChannelHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { var msg CloseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -213,7 +213,7 @@ func (h *closePaymentChannelHandler) Check(ctx context.Context, db weave.KVStore return &weave.CheckResult{}, nil } -func (h *closePaymentChannelHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *closePaymentChannelHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { var msg CloseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/sigs/context.go b/x/sigs/context.go index 6be4a9a3..47231946 100644 --- a/x/sigs/context.go +++ b/x/sigs/context.go @@ -18,7 +18,7 @@ const ( // withSigners is a private method, as only this module // can add a signer -func withSigners(ctx context.Context, signers []weave.Condition) context.Context { +func withSigners(ctx context.Context, info weave.BlockInfo, signers []weave.Condition) context.Context { return context.WithValue(ctx, contextKeySigners, signers) } @@ -40,7 +40,7 @@ func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { // HasAddress returns true if the given address // had signed in the current Context. -func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { signers := a.GetConditions(ctx) for _, s := range signers { if addr.Equals(s.Address()) { diff --git a/x/sigs/decorator.go b/x/sigs/decorator.go index 0fb78cb6..eb604577 100644 --- a/x/sigs/decorator.go +++ b/x/sigs/decorator.go @@ -49,10 +49,10 @@ func (d Decorator) AllowMissingSigs() Decorator { } // Check verifies signatures before calling down the stack. -func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d Decorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { stx, ok := tx.(SignedTx) if !ok { - return next.Check(ctx, store, tx) + return next.Check(ctx, info, store, tx) } chainID := weave.GetChainID(ctx) @@ -66,7 +66,7 @@ func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, ctx = withSigners(ctx, signers) - res, err := next.Check(ctx, store, tx) + res, err := next.Check(ctx, info, store, tx) if err != nil { return nil, err } @@ -78,10 +78,10 @@ func (d Decorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, } // Deliver verifies signatures before calling down the stack. -func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d Decorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { stx, ok := tx.(SignedTx) if !ok { - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } chainID := weave.GetChainID(ctx) @@ -94,5 +94,5 @@ func (d Decorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx } ctx = withSigners(ctx, signers) - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } diff --git a/x/sigs/decorator_test.go b/x/sigs/decorator_test.go index 7e882e9c..af985ec8 100644 --- a/x/sigs/decorator_test.go +++ b/x/sigs/decorator_test.go @@ -39,14 +39,14 @@ func TestDecorator(t *testing.T) { { name: "check", fn: func(dec weave.Decorator, my weave.Tx) error { - _, err := dec.Check(ctx, checkKv, my, signers) + _, err := dec.Check(ctx, info, checkKv, my, signers) return err }, }, { name: "deliver", fn: func(dec weave.Decorator, my weave.Tx) error { - _, err := dec.Deliver(ctx, kv, my, signers) + _, err := dec.Deliver(ctx, info, kv, my, signers) return err }, }, @@ -95,12 +95,12 @@ type SigCheckHandler struct { var _ weave.Handler = (*SigCheckHandler)(nil) -func (s *SigCheckHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (s *SigCheckHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { s.Signers = Authenticate{}.GetConditions(ctx) return &weave.CheckResult{}, nil } -func (s *SigCheckHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (s *SigCheckHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { s.Signers = Authenticate{}.GetConditions(ctx) return &weave.DeliverResult{}, nil } @@ -124,7 +124,7 @@ func TestGasPaymentPerSigner(t *testing.T) { tx.Signatures = []*StdSignature{sig} } - res, err := d.Check(ctx, db, tx, &h) + res, err := d.Check(ctx, info, db, tx, &h) if err != nil { t.Fatalf("cannot check: %s", err) } diff --git a/x/sigs/handler.go b/x/sigs/handler.go index cf79c032..5f0305ef 100644 --- a/x/sigs/handler.go +++ b/x/sigs/handler.go @@ -22,14 +22,14 @@ type bumpSequenceHandler struct { b Bucket } -func (h *bumpSequenceHandler) Check(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h *bumpSequenceHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, db, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h *bumpSequenceHandler) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h *bumpSequenceHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { user, msg, err := h.validate(ctx, db, tx) if err != nil { return nil, err @@ -51,7 +51,7 @@ func (h *bumpSequenceHandler) Deliver(ctx context.Context, db weave.KVStore, tx return &weave.DeliverResult{}, nil } -func (h *bumpSequenceHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UserData, *BumpSequenceMsg, error) { +func (h *bumpSequenceHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UserData, *BumpSequenceMsg, error) { var msg BumpSequenceMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/sigs/handler_test.go b/x/sigs/handler_test.go index ecd21229..54cf6126 100644 --- a/x/sigs/handler_test.go +++ b/x/sigs/handler_test.go @@ -145,12 +145,12 @@ func TestBumpSequence(t *testing.T) { tx := weavetest.Tx{Msg: &tc.Msg} cache := db.CacheWrap() - if _, err := handler.Check(ctx, cache, &tx); !tc.WantCheckErr.Is(err) { + if _, err := handler.Check(ctx, info, cache, &tx); !tc.WantCheckErr.Is(err) { t.Fatalf("unexpected check error: %+v", err) } cache.Discard() - if _, err := handler.Deliver(ctx, db, &tx); !tc.WantDeliverErr.Is(err) { + if _, err := handler.Deliver(ctx, info, db, &tx); !tc.WantDeliverErr.Is(err) { t.Fatalf("unexpected check error: %+v", err) } if tc.WantDeliverErr != nil { diff --git a/x/utils/action_tagger.go b/x/utils/action_tagger.go index 6a8c23ec..2188c05d 100644 --- a/x/utils/action_tagger.go +++ b/x/utils/action_tagger.go @@ -30,19 +30,19 @@ func NewActionTagger() ActionTagger { } // Check just passes the request along -func (ActionTagger) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { - return next.Check(ctx, db, tx) +func (ActionTagger) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { + return next.Check(ctx, info, db, tx) } // Deliver appends a tag on the result if there is a success. -func (ActionTagger) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (ActionTagger) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { // if we error in reporting, let's do so early before dispatching msg, err := tx.GetMsg() if err != nil { return nil, err } - res, err := next.Deliver(ctx, db, tx) + res, err := next.Deliver(ctx, info, db, tx) if err != nil { return nil, err } diff --git a/x/utils/action_tagger_test.go b/x/utils/action_tagger_test.go index e529a9c5..662ca77a 100644 --- a/x/utils/action_tagger_test.go +++ b/x/utils/action_tagger_test.go @@ -80,7 +80,7 @@ func TestActionTagger(t *testing.T) { store := store.MemStore() // we get tagged on success - res, err := tc.stack.Deliver(ctx, store, tc.tx) + res, err := tc.stack.Deliver(ctx, info, store, tc.tx) if tc.err != nil { if !tc.err.Is(err) { t.Fatalf("Unexpected error type returned: %v", err) diff --git a/x/utils/key_tagger.go b/x/utils/key_tagger.go index 4e8545ac..50283308 100644 --- a/x/utils/key_tagger.go +++ b/x/utils/key_tagger.go @@ -31,15 +31,15 @@ func NewKeyTagger() KeyTagger { } // Check does nothing -func (KeyTagger) Check(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { - return next.Check(ctx, db, tx) +func (KeyTagger) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { + return next.Check(ctx, info, db, tx) } // Deliver passes in a recording KVStore into the child and // uses that to calculate tags to add to DeliverResult -func (KeyTagger) Deliver(ctx context.Context, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (KeyTagger) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { record := store.NewRecordingStore(db) - res, err := next.Deliver(ctx, record, tx) + res, err := next.Deliver(ctx, info, record, tx) if err != nil { return nil, err } diff --git a/x/utils/key_tagger_test.go b/x/utils/key_tagger_test.go index 099ef2c3..39a3e062 100644 --- a/x/utils/key_tagger_test.go +++ b/x/utils/key_tagger_test.go @@ -108,7 +108,7 @@ func TestKeyTagger(t *testing.T) { // try check - no op check := db.CacheWrap() - _, err := tagger.Check(ctx, check, nil, tc.handler) + _, err := tagger.Check(ctx, info, check, nil, tc.handler) if tc.isError { if err == nil { t.Fatalf("Expected error") @@ -118,7 +118,7 @@ func TestKeyTagger(t *testing.T) { } // try deliver - records tags and sets values on success - res, err := tagger.Deliver(ctx, db, nil, tc.handler) + res, err := tagger.Deliver(ctx, info, db, nil, tc.handler) if tc.isError { if err == nil { t.Fatalf("Expected error") @@ -149,22 +149,22 @@ type writeDecorator struct { var _ weave.Decorator = writeDecorator{} -func (d writeDecorator) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (d writeDecorator) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { if !d.after { store.Set(d.key, d.value) } - res, err := next.Check(ctx, store, tx) + res, err := next.Check(ctx, info, store, tx) if d.after && err == nil { store.Set(d.key, d.value) } return res, err } -func (d writeDecorator) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (d writeDecorator) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { if !d.after { store.Set(d.key, d.value) } - res, err := next.Deliver(ctx, store, tx) + res, err := next.Deliver(ctx, info, store, tx) if d.after && err == nil { store.Set(d.key, d.value) } diff --git a/x/utils/logging.go b/x/utils/logging.go index 922c40ef..49a88354 100644 --- a/x/utils/logging.go +++ b/x/utils/logging.go @@ -18,9 +18,9 @@ func NewLogging() Logging { } // Check logs error -> info, success -> debug -func (r Logging) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (r Logging) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { start := time.Now() - res, err := next.Check(ctx, store, tx) + res, err := next.Check(ctx, info, store, tx) var resLog string if err == nil { resLog = res.Log @@ -30,9 +30,9 @@ func (r Logging) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, ne } // Deliver logs error -> error, success -> info -func (r Logging) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (r Logging) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { start := time.Now() - res, err := next.Deliver(ctx, store, tx) + res, err := next.Deliver(ctx, info, store, tx) var resLog string if err == nil { resLog = res.Log @@ -42,7 +42,7 @@ func (r Logging) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, } // logDuration writes information about the time and result to the logger -func logDuration(ctx context.Context, start time.Time, msg string, err error, lowPrio bool) { +func logDuration(ctx context.Context, info weave.BlockInfo, start time.Time, msg string, err error, lowPrio bool) { delta := time.Now().Sub(start) logger := weave.GetLogger(ctx).With("duration", delta/time.Microsecond) diff --git a/x/utils/recover.go b/x/utils/recover.go index 6462156b..983fb1dc 100644 --- a/x/utils/recover.go +++ b/x/utils/recover.go @@ -19,13 +19,13 @@ func NewRecovery() Recovery { } // Check turns panics into normal errors -func (r Recovery) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (_ *weave.CheckResult, err error) { +func (r Recovery) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (_ *weave.CheckResult, err error) { defer errors.Recover(&err) - return next.Check(ctx, store, tx) + return next.Check(ctx, info, store, tx) } // Deliver turns panics into normal errors -func (r Recovery) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (_ *weave.DeliverResult, err error) { +func (r Recovery) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (_ *weave.DeliverResult, err error) { defer errors.Recover(&err) - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } diff --git a/x/utils/recover_test.go b/x/utils/recover_test.go index 163fc4d5..44c248ba 100644 --- a/x/utils/recover_test.go +++ b/x/utils/recover_test.go @@ -18,14 +18,14 @@ func TestRecovery(t *testing.T) { store := store.MemStore() // Panic handler panics. Test the test tool. - assert.Panics(t, func() { h.Check(ctx, store, nil) }) - assert.Panics(t, func() { h.Deliver(ctx, store, nil) }) + assert.Panics(t, func() { h.Check(ctx, info, store, nil) }) + assert.Panics(t, func() { h.Deliver(ctx, info, store, nil) }) // Recovery wrapped handler returns an error. - _, err := r.Check(ctx, store, nil, h) + _, err := r.Check(ctx, info, store, nil, h) assert.True(t, errors.ErrPanic.Is(err)) - _, err = r.Deliver(ctx, store, nil, h) + _, err = r.Deliver(ctx, info, store, nil, h) assert.True(t, errors.ErrPanic.Is(err)) } @@ -33,10 +33,10 @@ type panicHandler struct{} var _ weave.Handler = panicHandler{} -func (p panicHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (p panicHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { panic("check panic") } -func (p panicHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (p panicHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { panic("deliver panic") } diff --git a/x/utils/savepoint.go b/x/utils/savepoint.go index 89c02e8a..54fcc053 100644 --- a/x/utils/savepoint.go +++ b/x/utils/savepoint.go @@ -39,18 +39,18 @@ func (s Savepoint) OnDeliver() Savepoint { } // Check will optionally set a checkpoint -func (s Savepoint) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { +func (s Savepoint) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { if !s.onCheck { - return next.Check(ctx, store, tx) + return next.Check(ctx, info, store, tx) } cstore, ok := store.(weave.CacheableKVStore) if !ok { - return next.Check(ctx, store, tx) + return next.Check(ctx, info, store, tx) } cache := cstore.CacheWrap() - if res, err := next.Check(ctx, cache, tx); err != nil { + if res, err := next.Check(ctx, info, cache, tx); err != nil { cache.Discard() return nil, err } else if werr := cache.Write(); werr != nil { @@ -61,18 +61,18 @@ func (s Savepoint) Check(ctx context.Context, store weave.KVStore, tx weave.Tx, } // Deliver will optionally set a checkpoint -func (s Savepoint) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { +func (s Savepoint) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { if !s.onDeliver { - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } cstore, ok := store.(weave.CacheableKVStore) if !ok { - return next.Deliver(ctx, store, tx) + return next.Deliver(ctx, info, store, tx) } cache := cstore.CacheWrap() - if res, err := next.Deliver(ctx, cache, tx); err != nil { + if res, err := next.Deliver(ctx, info, cache, tx); err != nil { cache.Discard() return nil, err } else if werr := cache.Write(); werr != nil { diff --git a/x/utils/savepoint_test.go b/x/utils/savepoint_test.go index e3c61651..d729e7cf 100644 --- a/x/utils/savepoint_test.go +++ b/x/utils/savepoint_test.go @@ -93,9 +93,9 @@ func TestSavepoint(t *testing.T) { var err error if tc.check { - _, err = tc.save.Check(ctx, kv, nil, tc.handler) + _, err = tc.save.Check(ctx, info, kv, nil, tc.handler) } else { - _, err = tc.save.Deliver(ctx, kv, nil, tc.handler) + _, err = tc.save.Deliver(ctx, info, kv, nil, tc.handler) } if tc.isError { @@ -131,12 +131,12 @@ type writeHandler struct { err error } -func (h writeHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h writeHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { store.Set(h.key, h.value) return &weave.CheckResult{}, h.err } -func (h writeHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h writeHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { store.Set(h.key, h.value) return &weave.DeliverResult{}, h.err } diff --git a/x/validators/handler.go b/x/validators/handler.go index f115726d..b8905801 100644 --- a/x/validators/handler.go +++ b/x/validators/handler.go @@ -30,14 +30,14 @@ type updateHandler struct { var _ weave.Handler = (*updateHandler)(nil) -func (h updateHandler) Check(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { +func (h updateHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { if _, _, err := h.validate(ctx, store, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } -func (h updateHandler) Deliver(ctx context.Context, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { +func (h updateHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { diff, updates, err := h.validate(ctx, store, tx) if err != nil { return nil, err @@ -51,7 +51,7 @@ func (h updateHandler) Deliver(ctx context.Context, store weave.KVStore, tx weav } // Validate returns an update diff, ValidatorUpdates to store for bookkeeping and an error. -func (h updateHandler) validate(ctx context.Context, store weave.KVStore, tx weave.Tx) ([]weave.ValidatorUpdate, +func (h updateHandler) validate(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) ([]weave.ValidatorUpdate, weave.ValidatorUpdates, error) { var msg ApplyDiffMsg var resUpdates weave.ValidatorUpdates diff --git a/x/validators/handler_test.go b/x/validators/handler_test.go index 309a4bf5..6cb37dd3 100644 --- a/x/validators/handler_test.go +++ b/x/validators/handler_test.go @@ -153,13 +153,13 @@ func TestHandler(t *testing.T) { ValidatorUpdates: spec.Src, }} // when check is called - if _, err := rt.Check(ctx, cache, tx); !spec.ExpCheckErr.Is(err) { + if _, err := rt.Check(ctx, info, cache, tx); !spec.ExpCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.ExpCheckErr, err) } cache.Discard() // and when deliver is called - res, err := rt.Deliver(ctx, db, tx) + res, err := rt.Deliver(ctx, info, db, tx) if !spec.ExpDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.ExpCheckErr, err) } From 3220fa3e789ecfb0a5ff43b27be31b2d643ff78a Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:36:50 +0200 Subject: [PATCH 05/17] Fix up all Handlers after search-replace --- cmd/bnsd/x/username/handlers.go | 7 ++++--- gconf/handler.go | 4 ++-- migration/handler.go | 2 +- x/aswap/handler.go | 6 +++--- x/auth.go | 14 +++++++------- x/cash/dynamicfee.go | 5 +++-- x/cash/staticfee.go | 3 ++- x/currency/handler.go | 3 ++- x/distribution/handler.go | 7 ++++--- x/escrow/handler.go | 9 +++++---- x/gov/context.go | 6 +++--- x/gov/handler.go | 16 ++++++++-------- x/gov/interfaces.go | 2 +- x/multisig/context.go | 6 +++--- x/multisig/decorator.go | 6 +++--- x/multisig/handlers.go | 5 +++-- x/paychan/handler.go | 5 +++-- x/sigs/context.go | 4 ++-- x/sigs/decorator.go | 4 ++-- x/sigs/handler.go | 3 ++- x/utils/logging.go | 8 ++++---- x/validators/handler.go | 3 ++- 22 files changed, 69 insertions(+), 59 deletions(-) diff --git a/cmd/bnsd/x/username/handlers.go b/cmd/bnsd/x/username/handlers.go index 94da9307..dc26e05e 100644 --- a/cmd/bnsd/x/username/handlers.go +++ b/cmd/bnsd/x/username/handlers.go @@ -2,6 +2,7 @@ package username import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -58,7 +59,7 @@ func (h *registerTokenHandler) Deliver(ctx context.Context, info weave.BlockInfo return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *registerTokenHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*RegisterTokenMsg, error) { +func (h *registerTokenHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*RegisterTokenMsg, error) { var msg RegisterTokenMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -100,7 +101,7 @@ func (h *transferTokenHandler) Deliver(ctx context.Context, info weave.BlockInfo return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *transferTokenHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TransferTokenMsg, *Token, error) { +func (h *transferTokenHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferTokenMsg, *Token, error) { var msg TransferTokenMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -143,7 +144,7 @@ func (h *changeTokenTargetsHandler) Deliver(ctx context.Context, info weave.Bloc return &weave.DeliverResult{Data: msg.Username.Bytes()}, nil } -func (h *changeTokenTargetsHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ChangeTokenTargetsMsg, *Token, error) { +func (h *changeTokenTargetsHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ChangeTokenTargetsMsg, *Token, error) { var msg ChangeTokenTargetsMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/gconf/handler.go b/gconf/handler.go index 9bab8b41..da67ccd7 100644 --- a/gconf/handler.go +++ b/gconf/handler.go @@ -36,14 +36,14 @@ func NewUpdateConfigurationHandler(pkg string, config OwnedConfig, auth x.Authen } func (h UpdateConfigurationHandler) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if err := h.applyTx(ctx, store, tx); err != nil { + if err := h.applyTx(ctx, info, store, tx); err != nil { return nil, err } return &weave.CheckResult{}, nil } func (h UpdateConfigurationHandler) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - if err := h.applyTx(ctx, store, tx); err != nil { + if err := h.applyTx(ctx, info, store, tx); err != nil { return nil, err } return &weave.DeliverResult{}, nil diff --git a/migration/handler.go b/migration/handler.go index 4cba94fb..9f9e3a2b 100644 --- a/migration/handler.go +++ b/migration/handler.go @@ -134,7 +134,7 @@ func (h *upgradeSchemaHandler) Deliver(ctx context.Context, info weave.BlockInfo return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h *upgradeSchemaHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpgradeSchemaMsg, error) { +func (h *upgradeSchemaHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpgradeSchemaMsg, error) { var msg UpgradeSchemaMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/aswap/handler.go b/x/aswap/handler.go index b1d55137..3d78eb8b 100644 --- a/x/aswap/handler.go +++ b/x/aswap/handler.go @@ -94,7 +94,7 @@ func (h CreateSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db } // validate does all common pre-processing between Check and Deliver. -func (h CreateSwapHandler) validate(ctx context.Context, info weave.BlockInfo, +func (h CreateSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { @@ -158,7 +158,7 @@ func (h ReleaseSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, d } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { +func (h ReleaseSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -229,7 +229,7 @@ func (h ReturnSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db } // validate does all common pre-processing between Check and Deliver. -func (h ReturnSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { +func (h ReturnSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { var msg ReturnSwapMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/auth.go b/x/auth.go index 6483d406..2593608e 100644 --- a/x/auth.go +++ b/x/auth.go @@ -44,7 +44,7 @@ func (m MultiAuth) GetConditions(ctx context.Context) []weave.Condition { } // HasAddress returns true iff any Authenticator support this -func (m MultiAuth) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { +func (m MultiAuth) HasAddress(ctx context.Context, addr weave.Address) bool { for _, impl := range m.impls { if impl.HasAddress(ctx, addr) { return true @@ -54,7 +54,7 @@ func (m MultiAuth) HasAddress(ctx context.Context, info weave.BlockInfo, addr we } // GetAddresses wraps the GetConditions method of any Authenticator -func GetAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator) []weave.Address { +func GetAddresses(ctx context.Context, auth Authenticator) []weave.Address { perms := auth.GetConditions(ctx) addrs := make([]weave.Address, len(perms)) for i, p := range perms { @@ -64,7 +64,7 @@ func GetAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator) } // MainSigner returns the first permission if any, otherwise nil -func MainSigner(ctx context.Context, info weave.BlockInfo, auth Authenticator) weave.Condition { +func MainSigner(ctx context.Context, auth Authenticator) weave.Condition { signers := auth.GetConditions(ctx) if len(signers) == 0 { return nil @@ -74,7 +74,7 @@ func MainSigner(ctx context.Context, info weave.BlockInfo, auth Authenticator) w // HasAllAddresses returns true if all elements in required are // also in context. -func HasAllAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator, required []weave.Address) bool { +func HasAllAddresses(ctx context.Context, auth Authenticator, required []weave.Address) bool { for _, r := range required { if !auth.HasAddress(ctx, r) { return false @@ -85,7 +85,7 @@ func HasAllAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticat // HasNAddresses returns true if at least n elements in requested are // also in context. -func HasNAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator, required []weave.Address, n int) bool { +func HasNAddresses(ctx context.Context, auth Authenticator, required []weave.Address, n int) bool { // Special case: is this an error??? if n <= 0 { return true @@ -104,14 +104,14 @@ func HasNAddresses(ctx context.Context, info weave.BlockInfo, auth Authenticator // HasAllConditions returns true if all elements in required are // also in context. -func HasAllConditions(ctx context.Context, info weave.BlockInfo, auth Authenticator, required []weave.Condition) bool { +func HasAllConditions(ctx context.Context, auth Authenticator, required []weave.Condition) bool { return HasNConditions(ctx, auth, required, len(required)) } // HasNConditions returns true if at least n elements in requested are // also in context. // Useful for threshold conditions (1 of 3, 3 of 5, etc...) -func HasNConditions(ctx context.Context, info weave.BlockInfo, auth Authenticator, requested []weave.Condition, n int) bool { +func HasNConditions(ctx context.Context, auth Authenticator, requested []weave.Condition, n int) bool { // Special case: is this an error??? if n <= 0 { return true diff --git a/x/cash/dynamicfee.go b/x/cash/dynamicfee.go index b3456520..803b0628 100644 --- a/x/cash/dynamicfee.go +++ b/x/cash/dynamicfee.go @@ -35,6 +35,7 @@ package cash import ( "context" + "github.com/iov-one/weave" coin "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -158,7 +159,7 @@ func (d DynamicFeeDecorator) chargeMinimalFee(store weave.KVStore, src weave.Add // prepare is all shared setup between Check and Deliver. It computes the fee // for the transaction, ensures that the payer is authenticated and prepares // the database transaction. -func (d DynamicFeeDecorator) prepare(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (fee coin.Coin, payer weave.Address, cache weave.KVCacheWrap, err error) { +func (d DynamicFeeDecorator) prepare(ctx context.Context, store weave.KVStore, tx weave.Tx) (fee coin.Coin, payer weave.Address, cache weave.KVCacheWrap, err error) { finfo, err := d.extractFee(ctx, tx, store) if err != nil { return fee, payer, cache, errors.Wrap(err, "cannot extract fee") @@ -186,7 +187,7 @@ func (d DynamicFeeDecorator) prepare(ctx context.Context, info weave.BlockInfo, } // this returns the fee info to deduct and the error if incorrectly set -func (d DynamicFeeDecorator) extractFee(ctx context.Context, info weave.BlockInfo, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { +func (d DynamicFeeDecorator) extractFee(ctx context.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { var finfo *FeeInfo ftx, ok := tx.(FeeTx) if ok { diff --git a/x/cash/staticfee.go b/x/cash/staticfee.go index 05c0e725..888623f2 100644 --- a/x/cash/staticfee.go +++ b/x/cash/staticfee.go @@ -17,6 +17,7 @@ package cash import ( "context" + "github.com/iov-one/weave" coin "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -101,7 +102,7 @@ func (d FeeDecorator) Deliver(ctx context.Context, info weave.BlockInfo, store w return next.Deliver(ctx, info, store, tx) } -func (d FeeDecorator) extractFee(ctx context.Context, info weave.BlockInfo, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { +func (d FeeDecorator) extractFee(ctx context.Context, tx weave.Tx, store weave.KVStore) (*FeeInfo, error) { var finfo *FeeInfo ftx, ok := tx.(FeeTx) if ok { diff --git a/x/currency/handler.go b/x/currency/handler.go index 0367c319..17b40e22 100644 --- a/x/currency/handler.go +++ b/x/currency/handler.go @@ -2,6 +2,7 @@ package currency import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -50,7 +51,7 @@ func (h *createTokenInfoHandler) Deliver(ctx context.Context, info weave.BlockIn return &weave.DeliverResult{}, h.bucket.Save(db, obj) } -func (h *createTokenInfoHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createTokenInfoHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/distribution/handler.go b/x/distribution/handler.go index 077ddc25..2e8b66ef 100644 --- a/x/distribution/handler.go +++ b/x/distribution/handler.go @@ -2,6 +2,7 @@ package distribution import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -80,7 +81,7 @@ func (h *createRevenueHandler) Deliver(ctx context.Context, info weave.BlockInfo return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h *createRevenueHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createRevenueHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -131,7 +132,7 @@ func (h *distributeHandler) Deliver(ctx context.Context, info weave.BlockInfo, d return &weave.DeliverResult{}, nil } -func (h *distributeHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*DistributeMsg, error) { +func (h *distributeHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DistributeMsg, error) { var msg DistributeMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -197,7 +198,7 @@ func (h *resetRevenueHandler) Deliver(ctx context.Context, info weave.BlockInfo, return &weave.DeliverResult{}, nil } -func (h *resetRevenueHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ResetMsg, error) { +func (h *resetRevenueHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ResetMsg, error) { var msg ResetMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/escrow/handler.go b/x/escrow/handler.go index 01155823..938ad2bb 100644 --- a/x/escrow/handler.go +++ b/x/escrow/handler.go @@ -2,6 +2,7 @@ package escrow import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -101,7 +102,7 @@ func (h CreateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h CreateEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -181,7 +182,7 @@ func (h ReleaseEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { +func (h ReleaseEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -250,7 +251,7 @@ func (h ReturnEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h ReturnEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { +func (h ReturnEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { var msg ReturnMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -316,7 +317,7 @@ func (h UpdateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h UpdateEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { +func (h UpdateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { var msg UpdatePartiesMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/gov/context.go b/x/gov/context.go index 56985b3c..a0e77766 100644 --- a/x/gov/context.go +++ b/x/gov/context.go @@ -20,7 +20,7 @@ type proposalWrapper struct { proposalID []byte } -func withElectionSuccess(ctx context.Context, info weave.BlockInfo, ruleID []byte) context.Context { +func withElectionSuccess(ctx context.Context, ruleID []byte) context.Context { val, _ := ctx.Value(contextKeyGov).([]weave.Condition) return context.WithValue(ctx, contextKeyGov, append(val, ElectionCondition(ruleID))) } @@ -44,7 +44,7 @@ func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { } // HasAddress returns true iff this address is in GetConditions. -func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true @@ -53,7 +53,7 @@ func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr return false } -func withProposal(ctx context.Context, info weave.BlockInfo, proposal *Proposal, proposalID []byte) context.Context { +func withProposal(ctx context.Context, proposal *Proposal, proposalID []byte) context.Context { return context.WithValue(ctx, contextKeyProposal, proposalWrapper{proposal: proposal, proposalID: proposalID}) } diff --git a/x/gov/handler.go b/x/gov/handler.go index 660199e0..612f64f4 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -103,7 +103,7 @@ func (h VoteHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave return &weave.DeliverResult{}, nil } -func (h VoteHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { +func (h VoteHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { var msg VoteMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -228,7 +228,7 @@ func (h TallyHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weav } subDB := cstore.CacheWrap() - res, err := h.executor(voteCtx, subDB, opts) + res, err := h.executor(voteCtx, info, subDB, opts) if err != nil { subDB.Discard() log := fmt.Sprintf("Proposal accepted: execution error: %v", err) @@ -246,7 +246,7 @@ func (h TallyHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weav return res, nil } -func (h TallyHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { +func (h TallyHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { var msg TallyMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -329,7 +329,7 @@ func (h CreateProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h CreateProposalHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { +func (h CreateProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { var msg CreateProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -407,7 +407,7 @@ func newDeleteProposalHandler(auth x.Authenticator) *DeleteProposalHandler { } } -func (h DeleteProposalHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { +func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { var msg DeleteProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -490,7 +490,7 @@ func (h UpdateElectorateHandler) Deliver(ctx context.Context, info weave.BlockIn return &weave.DeliverResult{Data: msg.ElectorateID}, nil } -func (h UpdateElectorateHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdateElectorateMsg, *Electorate, error) { +func (h UpdateElectorateHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectorateMsg, *Electorate, error) { var msg UpdateElectorateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -549,7 +549,7 @@ func (h UpdateElectionRuleHandler) Deliver(ctx context.Context, info weave.Block return &weave.DeliverResult{Data: msg.ElectionRuleID}, nil } -func (h UpdateElectionRuleHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdateElectionRuleMsg, *ElectionRule, error) { +func (h UpdateElectionRuleHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateElectionRuleMsg, *ElectionRule, error) { var msg UpdateElectionRuleMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -613,7 +613,7 @@ func (h createTextResolutionHandler) Deliver(ctx context.Context, info weave.Blo return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h createTextResolutionHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateTextResolutionMsg, error) { +func (h createTextResolutionHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateTextResolutionMsg, error) { var msg CreateTextResolutionMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/gov/interfaces.go b/x/gov/interfaces.go index 0d63f6fb..ae49d33b 100644 --- a/x/gov/interfaces.go +++ b/x/gov/interfaces.go @@ -18,7 +18,7 @@ type Executor func(ctx context.Context, info weave.BlockInfo, store weave.KVStor func HandlerAsExecutor(h weave.Handler) Executor { return func(ctx context.Context, info weave.BlockInfo, store weave.KVStore, msg weave.Msg) (*weave.DeliverResult, error) { tx := &fakeTx{msg: msg} - return h.Deliver(ctx, info, info, store, tx) + return h.Deliver(ctx, info, store, tx) } } diff --git a/x/multisig/context.go b/x/multisig/context.go index 054d5292..74673937 100644 --- a/x/multisig/context.go +++ b/x/multisig/context.go @@ -15,7 +15,7 @@ const ( // withMultisig is a private method, as only this module // can add a multisig signer -func withMultisig(ctx context.Context, info weave.BlockInfo, id []byte) context.Context { +func withMultisig(ctx context.Context, id []byte) context.Context { val, _ := ctx.Value(contextKeyMultisig).([]weave.Condition) if val == nil { return context.WithValue(ctx, contextKeyMultisig, []weave.Condition{MultiSigCondition(id)}) @@ -45,8 +45,8 @@ func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { return val } -// HasAddress returns true iff this address is in GetConditions -func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { +// HasAddress returns true iff this address is in Ggo etConditions +func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true diff --git a/x/multisig/decorator.go b/x/multisig/decorator.go index f3521355..adb25198 100644 --- a/x/multisig/decorator.go +++ b/x/multisig/decorator.go @@ -32,7 +32,7 @@ func (d Decorator) Check(ctx context.Context, info weave.BlockInfo, store weave. return nil, err } - res, err := next.Check(newCtx, store, tx) + res, err := next.Check(newCtx, info, store, tx) if err != nil { return nil, err } @@ -47,10 +47,10 @@ func (d Decorator) Deliver(ctx context.Context, info weave.BlockInfo, store weav return nil, err } - return next.Deliver(newCtx, store, tx) + return next.Deliver(newCtx, info, store, tx) } -func (d Decorator) authMultisig(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (context.Context, int64, error) { +func (d Decorator) authMultisig(ctx context.Context, store weave.KVStore, tx weave.Tx) (context.Context, int64, error) { multisigContract, ok := tx.(MultiSigTx) if !ok { return ctx, 0, nil diff --git a/x/multisig/handlers.go b/x/multisig/handlers.go index 0d4ca09f..03888c50 100644 --- a/x/multisig/handlers.go +++ b/x/multisig/handlers.go @@ -2,6 +2,7 @@ package multisig import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -63,7 +64,7 @@ func (h CreateMsgHandler) Deliver(ctx context.Context, info weave.BlockInfo, db } // validate does all common pre-processing between Check and Deliver. -func (h CreateMsgHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateMsgHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { // Retrieve tx main signer in this context. sender := x.MainSigner(ctx, h.auth) if sender == nil { @@ -115,7 +116,7 @@ func (h UpdateMsgHandler) Deliver(ctx context.Context, info weave.BlockInfo, db return &weave.DeliverResult{}, nil } -func (h UpdateMsgHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdateMsg, error) { +func (h UpdateMsgHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdateMsg, error) { var msg UpdateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/paychan/handler.go b/x/paychan/handler.go index 428e47ec..fcf6ef78 100644 --- a/x/paychan/handler.go +++ b/x/paychan/handler.go @@ -2,6 +2,7 @@ package paychan import ( "context" + "github.com/iov-one/weave" coin "github.com/iov-one/weave/coin" "github.com/iov-one/weave/errors" @@ -50,7 +51,7 @@ func (h *createPaymentChannelHandler) Check(ctx context.Context, info weave.Bloc return &weave.CheckResult{GasAllocated: createPaymentChannelCost}, nil } -func (h *createPaymentChannelHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") @@ -108,7 +109,7 @@ func (h *transferPaymentChannelHandler) Check(ctx context.Context, info weave.Bl return &weave.CheckResult{GasAllocated: transferPaymentChannelCost}, nil } -func (h *transferPaymentChannelHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { +func (h *transferPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { var msg TransferMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") diff --git a/x/sigs/context.go b/x/sigs/context.go index 47231946..6be4a9a3 100644 --- a/x/sigs/context.go +++ b/x/sigs/context.go @@ -18,7 +18,7 @@ const ( // withSigners is a private method, as only this module // can add a signer -func withSigners(ctx context.Context, info weave.BlockInfo, signers []weave.Condition) context.Context { +func withSigners(ctx context.Context, signers []weave.Condition) context.Context { return context.WithValue(ctx, contextKeySigners, signers) } @@ -40,7 +40,7 @@ func (a Authenticate) GetConditions(ctx context.Context) []weave.Condition { // HasAddress returns true if the given address // had signed in the current Context. -func (a Authenticate) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { +func (a Authenticate) HasAddress(ctx context.Context, addr weave.Address) bool { signers := a.GetConditions(ctx) for _, s := range signers { if addr.Equals(s.Address()) { diff --git a/x/sigs/decorator.go b/x/sigs/decorator.go index eb604577..b323e9f4 100644 --- a/x/sigs/decorator.go +++ b/x/sigs/decorator.go @@ -55,7 +55,7 @@ func (d Decorator) Check(ctx context.Context, info weave.BlockInfo, store weave. return next.Check(ctx, info, store, tx) } - chainID := weave.GetChainID(ctx) + chainID := info.ChainID() signers, err := VerifyTxSignatures(store, stx, chainID) if err != nil { return nil, errors.Wrap(err, "cannot verify signatures") @@ -84,7 +84,7 @@ func (d Decorator) Deliver(ctx context.Context, info weave.BlockInfo, store weav return next.Deliver(ctx, info, store, tx) } - chainID := weave.GetChainID(ctx) + chainID := info.ChainID() signers, err := VerifyTxSignatures(store, stx, chainID) if err != nil { return nil, errors.Wrap(err, "cannot verify signatures") diff --git a/x/sigs/handler.go b/x/sigs/handler.go index 5f0305ef..610b0d41 100644 --- a/x/sigs/handler.go +++ b/x/sigs/handler.go @@ -2,6 +2,7 @@ package sigs import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -51,7 +52,7 @@ func (h *bumpSequenceHandler) Deliver(ctx context.Context, info weave.BlockInfo, return &weave.DeliverResult{}, nil } -func (h *bumpSequenceHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UserData, *BumpSequenceMsg, error) { +func (h *bumpSequenceHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UserData, *BumpSequenceMsg, error) { var msg BumpSequenceMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") diff --git a/x/utils/logging.go b/x/utils/logging.go index 49a88354..c43daad4 100644 --- a/x/utils/logging.go +++ b/x/utils/logging.go @@ -25,7 +25,7 @@ func (r Logging) Check(ctx context.Context, info weave.BlockInfo, store weave.KV if err == nil { resLog = res.Log } - logDuration(ctx, start, resLog, err, true) + logDuration(info, start, resLog, err, true) return res, err } @@ -37,14 +37,14 @@ func (r Logging) Deliver(ctx context.Context, info weave.BlockInfo, store weave. if err == nil { resLog = res.Log } - logDuration(ctx, start, resLog, err, false) + logDuration(info, start, resLog, err, false) return res, err } // logDuration writes information about the time and result to the logger -func logDuration(ctx context.Context, info weave.BlockInfo, start time.Time, msg string, err error, lowPrio bool) { +func logDuration(info weave.BlockInfo, start time.Time, msg string, err error, lowPrio bool) { delta := time.Now().Sub(start) - logger := weave.GetLogger(ctx).With("duration", delta/time.Microsecond) + logger := info.Logger().With("duration", delta/time.Microsecond) if err != nil { logger = logger.With("err", err) diff --git a/x/validators/handler.go b/x/validators/handler.go index b8905801..eaacdfda 100644 --- a/x/validators/handler.go +++ b/x/validators/handler.go @@ -2,6 +2,7 @@ package validators import ( "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/migration" @@ -51,7 +52,7 @@ func (h updateHandler) Deliver(ctx context.Context, info weave.BlockInfo, store } // Validate returns an update diff, ValidatorUpdates to store for bookkeeping and an error. -func (h updateHandler) validate(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) ([]weave.ValidatorUpdate, +func (h updateHandler) validate(ctx context.Context, store weave.KVStore, tx weave.Tx) ([]weave.ValidatorUpdate, weave.ValidatorUpdates, error) { var msg ApplyDiffMsg var resUpdates weave.ValidatorUpdates From fa3973dfb2f1349206d3381d4588c36789ecf430 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:45:16 +0200 Subject: [PATCH 06/17] Fix app compilation errors --- app/base.go | 14 ++++++++------ app/store.go | 46 ++++++++++++++++------------------------------ 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/app/base.go b/app/base.go index dc1a53cd..94f0126c 100644 --- a/app/base.go +++ b/app/base.go @@ -1,6 +1,8 @@ package app import ( + "context" + "github.com/iov-one/weave" "github.com/iov-one/weave/errors" abci "github.com/tendermint/tendermint/abci/types" @@ -39,11 +41,11 @@ func (b BaseApp) DeliverTx(txBytes []byte) abci.ResponseDeliverTx { } // ignore error here, allow it to be logged - ctx := weave.WithLogInfo(b.BlockContext(), + info := b.BlockInfo().WithLogInfo( "call", "deliver_tx", "path", weave.GetPath(tx)) - res, err := b.handler.Deliver(ctx, info, b.DeliverStore(), tx) + res, err := b.handler.Deliver(context.Background(), info, b.DeliverStore(), tx) if err == nil { b.AddValChange(res.Diff) } @@ -57,11 +59,11 @@ func (b BaseApp) CheckTx(txBytes []byte) abci.ResponseCheckTx { return weave.CheckTxError(err, b.debug) } - ctx := weave.WithLogInfo(b.BlockContext(), + info := b.BlockInfo().WithLogInfo( "call", "check_tx", "path", weave.GetPath(tx)) - res, err := b.handler.Check(ctx, info, b.CheckStore(), tx) + res, err := b.handler.Check(context.Background(), info, b.CheckStore(), tx) return weave.CheckOrError(res, err, b.debug) } @@ -76,8 +78,8 @@ func (b BaseApp) BeginBlock(req abci.RequestBeginBlock) ( if b.ticker != nil { // start := time.Now() // Add info to the logger - ctx := weave.WithLogInfo(b.BlockContext(), "call", "begin_block") - res, err := b.ticker.Tick(ctx, b.DeliverStore()) + info := b.BlockInfo().WithLogInfo("call", "begin_block") + res, err := b.ticker.Tick(context.Background(), info, b.DeliverStore()) // logDuration(ctx, start, "Ticker", err, false) if err != nil { panic(err) diff --git a/app/store.go b/app/store.go index 69ca7dad..01d64e0c 100644 --- a/app/store.go +++ b/app/store.go @@ -1,7 +1,6 @@ package app import ( - "context" "encoding/json" "fmt" "strings" @@ -46,42 +45,35 @@ type StoreApp struct { // cached validator changes from DeliverTx pending weave.ValidatorUpdates - // baseContext contains context info that is valid for - // lifetime of this app (eg. chainID) - baseContext context.Context - - // blockContext contains context info that is valid for the + // blockInfo contains context info that is valid for the // current block (eg. height, header), reset on BeginBlock - blockContext context.Context + blockInfo weave.BlockInfo } // NewStoreApp initializes this app into a ready state with some defaults // // panics if unable to properly load the state from the given store // TODO: is this correct? nothing else to do really.... -func NewStoreApp(name string, store weave.CommitKVStore, - queryRouter weave.QueryRouter, baseContext context.Context) *StoreApp { +func NewStoreApp(name string, store weave.CommitKVStore, queryRouter weave.QueryRouter) *StoreApp { s := &StoreApp{ name: name, // note: panics if trouble initializing from store store: NewCommitStore(store), queryRouter: queryRouter, - baseContext: baseContext, + logger: log.NewNopLogger(), } - s = s.WithLogger(log.NewNopLogger()) - // load the chainID from the db s.chainID = mustLoadChainID(s.DeliverStore()) - if s.chainID != "" { - s.baseContext = weave.WithChainID(s.baseContext, s.chainID) - } // get the most recent height info, err := s.store.CommitInfo() if err != nil { panic(err) } - s.blockContext = weave.WithHeight(s.baseContext, info.Version) + s.blockInfo, err = weave.NewBlockInfo(abci.Header{Height: info.Version}, weave.CommitInfo{}, s.chainID, s.logger) + if err != nil { + panic(err) + } return s } @@ -121,7 +113,7 @@ func (s *StoreApp) parseAppState(data []byte, params weave.GenesisParams, chainI return init.FromGenesis(appState, params, s.DeliverStore()) } -// store chainID and update context +// store chainID func (s *StoreApp) storeChainID(chainID string) error { // set the chainID s.chainID = chainID @@ -129,9 +121,6 @@ func (s *StoreApp) storeChainID(chainID string) error { if err != nil { return err } - // and update the context - s.baseContext = weave.WithChainID(s.baseContext, s.chainID) - return nil } @@ -140,7 +129,6 @@ func (s *StoreApp) storeChainID(chainID string) error { // // also sets baseContext logger func (s *StoreApp) WithLogger(logger log.Logger) *StoreApp { - s.baseContext = weave.WithLogger(s.baseContext, logger) s.logger = logger return s } @@ -151,8 +139,8 @@ func (s *StoreApp) Logger() log.Logger { } // BlockContext returns the block context for public use -func (s *StoreApp) BlockContext() context.Context { - return s.blockContext +func (s *StoreApp) BlockInfo() weave.BlockInfo { + return s.blockInfo } // DeliverStore returns the current DeliverTx cache for methods @@ -325,17 +313,15 @@ func (s *StoreApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitCh // Sets up blockContext // TODO: investigate response tags as of 0.11 abci func (s *StoreApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeginBlock) { - // set the begin block context - ctx := weave.WithHeader(s.baseContext, req.Header) - ctx = weave.WithHeight(ctx, req.Header.GetHeight()) - ctx = weave.WithCommitInfo(ctx, req.LastCommitInfo) - + var err error + s.blockInfo, err = weave.NewBlockInfo(req.Header, req.LastCommitInfo, s.chainID, s.logger) + if err != nil { + panic(err) + } now := req.Header.GetTime() if now.IsZero() { panic("current time not found in the block header") } - ctx = weave.WithBlockTime(ctx, now) - s.blockContext = ctx return res } From 179fbd6883fcc3ab6ab7276a89ec383e4a6b2754 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:46:59 +0200 Subject: [PATCH 07/17] Fix more auto-rename issues --- client/client.go | 14 +++++++------- client/wrapper.go | 12 ++++++------ tmtest/tmtest.go | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/client/client.go b/client/client.go index b93de524..33d22375 100644 --- a/client/client.go +++ b/client/client.go @@ -59,7 +59,7 @@ func (c *Client) Status(ctx context.Context) (*Status, error) { // Header returns the block header at the given height. // Returns an error if no header exists yet for that height -func (c *Client) Header(ctx context.Context, info weave.BlockInfo, height int64) (*Header, error) { +func (c *Client) Header(ctx context.Context, height int64) (*Header, error) { // TODO: add context timeout here info, err := c.conn.BlockchainInfo(height, height) if err != nil { @@ -74,7 +74,7 @@ func (c *Client) Header(ctx context.Context, info weave.BlockInfo, height int64) // SubmitTx will submit the tx to the mempool and then return with success or error // You will need to use WatchTx (easily parallelizable) to get the result. // CommitTx and CommitTxs provide helpers for common use cases -func (c *Client) SubmitTx(ctx context.Context, info weave.BlockInfo, tx weave.Tx) (TransactionID, error) { +func (c *Client) SubmitTx(ctx context.Context, tx weave.Tx) (TransactionID, error) { bz, err := tx.Marshal() if err != nil { return nil, errors.Wrapf(errors.ErrMsg, "marshaling: %s", err.Error()) @@ -110,7 +110,7 @@ func (c *Client) Query(query RequestQuery) ResponseQuery { } // GetTxByID will return 0 or 1 results (nil or result value) -func (c *Client) GetTxByID(ctx context.Context, info weave.BlockInfo, id TransactionID) (*CommitResult, error) { +func (c *Client) GetTxByID(ctx context.Context, id TransactionID) (*CommitResult, error) { // TODO: add context timeout here tx, err := c.conn.Tx(id, false) // FIXME: use proofs sometime if err != nil { @@ -122,7 +122,7 @@ func (c *Client) GetTxByID(ctx context.Context, info weave.BlockInfo, id Transac // SearchTx will search for all committed transactions that match a query, // returning them as one large array. // It returns an error if the subscription request failed. -func (c *Client) SearchTx(ctx context.Context, info weave.BlockInfo, query TxQuery) ([]*CommitResult, error) { +func (c *Client) SearchTx(ctx context.Context, query TxQuery) ([]*CommitResult, error) { // TODO: return actual transaction content as well? not just ID and Result // TODO: add context timeout here // FIXME: use proofs sometime @@ -141,7 +141,7 @@ func (c *Client) SearchTx(ctx context.Context, info weave.BlockInfo, query TxQue // SubscribeHeaders will fills the channel with all new headers // Stops when the context is cancelled -func (c *Client) SubscribeHeaders(ctx context.Context, info weave.BlockInfo, results chan<- Header, options ...Option) error { +func (c *Client) SubscribeHeaders(ctx context.Context, results chan<- Header, options ...Option) error { data, err := c.subscribe(ctx, QueryForHeader(), options...) if err != nil { return err @@ -171,7 +171,7 @@ func (c *Client) SubscribeHeaders(ctx context.Context, info weave.BlockInfo, res // SubscribeTx will subscribe to all transactions that match a query, writing them to the // results channel as they arrive. It returns an error if the subscription request failed. // Once subscriptions start, the continue until the context is closed (or network error) -func (c *Client) SubscribeTx(ctx context.Context, info weave.BlockInfo, query TxQuery, results chan<- CommitResult, options ...Option) error { +func (c *Client) SubscribeTx(ctx context.Context, query TxQuery, results chan<- CommitResult, options ...Option) error { q := fmt.Sprintf("%s='%s' AND %s", tmtypes.EventTypeKey, tmtypes.EventTx, query) data, err := c.subscribe(ctx, q, options...) @@ -202,7 +202,7 @@ func (c *Client) SubscribeTx(ctx context.Context, info weave.BlockInfo, query Tx } // subscribe should be used internally, it wraps conn.Subscribe and uses ctx.Done() to trigger Unsubscription -func (c *Client) subscribe(ctx context.Context, info weave.BlockInfo, query string, options ...Option) (<-chan ctypes.ResultEvent, error) { +func (c *Client) subscribe(ctx context.Context, query string, options ...Option) (<-chan ctypes.ResultEvent, error) { var outCapacity []int for _, option := range options { switch o := option.(type) { diff --git a/client/wrapper.go b/client/wrapper.go index 2e04eda0..cd723db5 100644 --- a/client/wrapper.go +++ b/client/wrapper.go @@ -11,7 +11,7 @@ import ( // SubscribeTxByID will block until there is a result, then return it // You must cancel the context to avoid blocking forever in some cases -func (c *Client) SubscribeTxByID(ctx context.Context, info weave.BlockInfo, id TransactionID) (*CommitResult, error) { +func (c *Client) SubscribeTxByID(ctx context.Context, id TransactionID) (*CommitResult, error) { txs := make(chan CommitResult, 1) err := c.SubscribeTx(ctx, QueryTxByID(id), txs) if err != nil { @@ -29,7 +29,7 @@ func (c *Client) SubscribeTxByID(ctx context.Context, info weave.BlockInfo, id T // WatchTx will block until this transaction makes it into a block // It will return immediately if the id was included in a block prior to the query, to avoid timing issues // You can use context.Context to pass in a timeout -func (c *Client) WatchTx(ctx context.Context, info weave.BlockInfo, id TransactionID) (*CommitResult, error) { +func (c *Client) WatchTx(ctx context.Context, id TransactionID) (*CommitResult, error) { // TODO: combine subscribe tx and search tx (two other functions to be writen) subctx, cancel := context.WithCancel(ctx) defer cancel() @@ -57,7 +57,7 @@ func (c *Client) WatchTx(ctx context.Context, info weave.BlockInfo, id Transacti } // CommitTx will block on both Check and Deliver, returning when it is in a block -func (c *Client) CommitTx(ctx context.Context, info weave.BlockInfo, tx weave.Tx) (*CommitResult, error) { +func (c *Client) CommitTx(ctx context.Context, tx weave.Tx) (*CommitResult, error) { // This can be combined from other primitives check, err := c.SubmitTx(ctx, tx) if err != nil { @@ -72,7 +72,7 @@ func (c *Client) CommitTx(ctx context.Context, info weave.BlockInfo, tx weave.Tx } // WatchTxs will watch a list of transactions in parallel -func (c *Client) WatchTxs(ctx context.Context, info weave.BlockInfo, ids []TransactionID) ([]*CommitResult, error) { +func (c *Client) WatchTxs(ctx context.Context, ids []TransactionID) ([]*CommitResult, error) { var mutex sync.Mutex // FIXME: make this multierror when that exists var gotErr error @@ -110,7 +110,7 @@ func (c *Client) WatchTxs(ctx context.Context, info weave.BlockInfo, ids []Trans // // If any tx fails in mempool or network, this returns an error // TODO: improve error handling (maybe we need to use CommitResultOrError type?) -func (c *Client) CommitTxs(ctx context.Context, info weave.BlockInfo, txs []weave.Tx) ([]*CommitResult, error) { +func (c *Client) CommitTxs(ctx context.Context, txs []weave.Tx) ([]*CommitResult, error) { // first submit them all (some may error), this should be in order var err error ids := make([]TransactionID, len(txs)) @@ -155,7 +155,7 @@ func (c *Client) WaitForNextBlock(ctx context.Context) (*Header, error) { // WaitForHeight subscribes to headers and returns as soon as a header arrives // equal to or greater than the given height. If the requested height is in the past, // it will still wait for the next block to arrive -func (c *Client) WaitForHeight(ctx context.Context, info weave.BlockInfo, height int64) (*Header, error) { +func (c *Client) WaitForHeight(ctx context.Context, height int64) (*Header, error) { // ensure we close subscription at function return cctx, cancel := context.WithCancel(ctx) defer cancel() diff --git a/tmtest/tmtest.go b/tmtest/tmtest.go index ca956cd8..1ced0a7c 100644 --- a/tmtest/tmtest.go +++ b/tmtest/tmtest.go @@ -18,7 +18,7 @@ import ( // // Set FORCE_TM_TEST=1 environment variable to fail the test if the binary is // not available. This might be desired when running tests by CI. -func RunTendermint(ctx context.Context, info weave.BlockInfo, t *testing.T, home string) (cleanup func()) { +func RunTendermint(ctx context.Context, t *testing.T, home string) (cleanup func()) { t.Helper() tmpath, err := exec.LookPath("tendermint") From 08a94a5126e68ef6b4210cb673843f7c89e2ee53 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:56:18 +0200 Subject: [PATCH 08/17] Fixed all remaining compiler errors --- cmd/bnsd/app/app.go | 4 +--- cmd/bnsd/app/init.go | 4 +--- x/aswap/handler.go | 16 ++++++++-------- x/escrow/handler.go | 32 ++++++++++++++++---------------- x/gov/handler.go | 42 +++++++++++++++++++----------------------- x/paychan/handler.go | 18 +++++++++--------- 6 files changed, 54 insertions(+), 62 deletions(-) diff --git a/cmd/bnsd/app/app.go b/cmd/bnsd/app/app.go index c69d4243..198d1e03 100644 --- a/cmd/bnsd/app/app.go +++ b/cmd/bnsd/app/app.go @@ -5,7 +5,6 @@ to construct the bnsd app. package bnsd import ( - "context" "fmt" "path/filepath" "strings" @@ -124,12 +123,11 @@ func Stack(issuer weave.Address, minFee coin.Coin) weave.Handler { func Application(name string, h weave.Handler, tx weave.TxDecoder, dbPath string, options *server.Options) (app.BaseApp, error) { - ctx := context.Background() kv, err := CommitKVStore(dbPath) if err != nil { return app.BaseApp{}, err } - store := app.NewStoreApp(name, kv, QueryRouter(options.MinFee), ctx) + store := app.NewStoreApp(name, kv, QueryRouter(options.MinFee)) base := app.NewBaseApp(store, tx, h, nil, options.Debug) return base, nil } diff --git a/cmd/bnsd/app/init.go b/cmd/bnsd/app/init.go index 647b4fec..b592c8e3 100644 --- a/cmd/bnsd/app/init.go +++ b/cmd/bnsd/app/init.go @@ -1,7 +1,6 @@ package bnsd import ( - "context" "encoding/hex" "encoding/json" "fmt" @@ -112,8 +111,7 @@ func DecorateApp(application app.BaseApp, logger log.Logger) app.BaseApp { func InlineApp(kv weave.CommitKVStore, logger log.Logger, debug bool) abci.Application { minFee := coin.Coin{} stack := Stack(nil, minFee) - ctx := context.Background() - store := app.NewStoreApp("bnsd", kv, QueryRouter(minFee), ctx) + store := app.NewStoreApp("bnsd", kv, QueryRouter(minFee)) base := app.NewBaseApp(store, TxDecoder, stack, nil, debug) return DecorateApp(base, logger) } diff --git a/x/aswap/handler.go b/x/aswap/handler.go index 3d78eb8b..dbb760ce 100644 --- a/x/aswap/handler.go +++ b/x/aswap/handler.go @@ -121,7 +121,7 @@ var _ weave.Handler = ReleaseSwapHandler{} // Check just verifies it is properly formed and returns // the cost of executing it func (h ReleaseSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - _, _, err := h.validate(ctx, db, tx) + _, _, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -132,7 +132,7 @@ func (h ReleaseSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db // Deliver moves the tokens from swap account to the receiver if // all preconditions are met. When the swap account is empty it is deleted. func (h ReleaseSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - swapID, swap, err := h.validate(ctx, db, tx) + swapID, swap, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -158,7 +158,7 @@ func (h ReleaseSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, d } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { +func (h ReleaseSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) ([]byte, *Swap, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -175,7 +175,7 @@ func (h ReleaseSwapHandler) validate(ctx context.Context, db weave.KVStore, tx w return nil, nil, errors.Wrap(errors.ErrUnauthorized, "invalid preimageHash") } - if weave.IsExpired(ctx, swap.Timeout) { + if info.IsExpired(swap.Timeout) { return nil, nil, errors.Wrap(errors.ErrState, "swap is expired") } @@ -194,7 +194,7 @@ var _ weave.Handler = ReturnSwapHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. func (h ReturnSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - _, _, err := h.validate(ctx, db, tx) + _, _, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -205,7 +205,7 @@ func (h ReturnSwapHandler) Check(ctx context.Context, info weave.BlockInfo, db w // Deliver moves all the tokens from the swap to the defined sender if // all preconditions are met. The swap is deleted afterwards. func (h ReturnSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, swap, err := h.validate(ctx, db, tx) + msg, swap, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -229,7 +229,7 @@ func (h ReturnSwapHandler) Deliver(ctx context.Context, info weave.BlockInfo, db } // validate does all common pre-processing between Check and Deliver. -func (h ReturnSwapHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { +func (h ReturnSwapHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ReturnSwapMsg, *Swap, error) { var msg ReturnSwapMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -240,7 +240,7 @@ func (h ReturnSwapHandler) validate(ctx context.Context, db weave.KVStore, tx we return nil, nil, err } - if !weave.IsExpired(ctx, swap.Timeout) { + if !info.IsExpired(swap.Timeout) { return nil, nil, errors.Wrapf(errors.ErrState, "swap not expired %v", swap.Timeout) } diff --git a/x/escrow/handler.go b/x/escrow/handler.go index 938ad2bb..e940a79d 100644 --- a/x/escrow/handler.go +++ b/x/escrow/handler.go @@ -49,7 +49,7 @@ var _ weave.Handler = CreateEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. func (h CreateEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - _, err := h.validate(ctx, db, tx) + _, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -63,7 +63,7 @@ func (h CreateEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db // Deliver moves the tokens from sender to the escrow account if // all preconditions are met. func (h CreateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, err := h.validate(ctx, db, tx) + msg, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -102,13 +102,13 @@ func (h CreateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h CreateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h CreateEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") } - if weave.IsExpired(ctx, msg.Timeout) { + if info.IsExpired(msg.Timeout) { return nil, errors.Wrap(errors.ErrInput, "timeout in the past") } @@ -134,7 +134,7 @@ var _ weave.Handler = ReleaseEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it func (h ReleaseEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - _, _, err := h.validate(ctx, db, tx) + _, _, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -145,7 +145,7 @@ func (h ReleaseEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, d // Deliver moves the tokens from escrow account to the receiver if // all preconditions are met. When the escrow account is empty it is deleted. func (h ReleaseEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, escrow, err := h.validate(ctx, db, tx) + msg, escrow, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -182,7 +182,7 @@ func (h ReleaseEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h ReleaseEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { +func (h ReleaseEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*ReleaseMsg, *Escrow, error) { var msg ReleaseMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -197,7 +197,7 @@ func (h ReleaseEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx return nil, nil, errors.ErrUnauthorized } - if weave.IsExpired(ctx, escrow.Timeout) { + if info.IsExpired(escrow.Timeout) { err := errors.Wrapf(errors.ErrExpired, "escrow expired %v", escrow.Timeout) return nil, nil, err } @@ -217,7 +217,7 @@ var _ weave.Handler = ReturnEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. func (h ReturnEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - _, _, err := h.validate(ctx, db, tx) + _, _, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -228,7 +228,7 @@ func (h ReturnEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db // Deliver moves all the tokens from the escrow to the defined sender if // all preconditions are met. The escrow is deleted afterwards. func (h ReturnEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - key, escrow, err := h.validate(ctx, db, tx) + key, escrow, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -251,7 +251,7 @@ func (h ReturnEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h ReturnEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { +func (h ReturnEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) ([]byte, *Escrow, error) { var msg ReturnMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -262,7 +262,7 @@ func (h ReturnEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx return nil, nil, err } - if !weave.IsExpired(ctx, escrow.Timeout) { + if !info.IsExpired(escrow.Timeout) { return nil, nil, errors.Wrapf(errors.ErrState, "escrow not expired %v", escrow.Timeout) } @@ -280,7 +280,7 @@ var _ weave.Handler = UpdateEscrowHandler{} // Check just verifies it is properly formed and returns // the cost of executing it. func (h UpdateEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - _, _, err := h.validate(ctx, db, tx) + _, _, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -291,7 +291,7 @@ func (h UpdateEscrowHandler) Check(ctx context.Context, info weave.BlockInfo, db // Deliver updates the any of the sender, recipient or arbiter if // all preconditions are met. No coins are moved. func (h UpdateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, escrow, err := h.validate(ctx, db, tx) + msg, escrow, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -317,7 +317,7 @@ func (h UpdateEscrowHandler) Deliver(ctx context.Context, info weave.BlockInfo, } // validate does all common pre-processing between Check and Deliver. -func (h UpdateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { +func (h UpdateEscrowHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*UpdatePartiesMsg, *Escrow, error) { var msg UpdatePartiesMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -328,7 +328,7 @@ func (h UpdateEscrowHandler) validate(ctx context.Context, db weave.KVStore, tx return nil, nil, err } - if weave.IsExpired(ctx, escrow.Timeout) { + if info.IsExpired(escrow.Timeout) { return nil, nil, errors.Wrapf(errors.ErrExpired, "escrow expired %v", escrow.Timeout) } diff --git a/x/gov/handler.go b/x/gov/handler.go index 612f64f4..0a8b06d4 100644 --- a/x/gov/handler.go +++ b/x/gov/handler.go @@ -68,7 +68,7 @@ func newVoteHandler(auth x.Authenticator) *VoteHandler { } func (h VoteHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if _, _, _, err := h.validate(ctx, db, tx); err != nil { + if _, _, _, err := h.validate(ctx, info, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: voteCost}, nil @@ -76,7 +76,7 @@ func (h VoteHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.K } func (h VoteHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - voteMsg, proposal, vote, err := h.validate(ctx, db, tx) + voteMsg, proposal, vote, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func (h VoteHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave return &weave.DeliverResult{}, nil } -func (h VoteHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { +func (h VoteHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*VoteMsg, *Proposal, *Vote, error) { var msg VoteMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") @@ -116,10 +116,10 @@ func (h VoteHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx if proposal.Status != Proposal_Submitted { return nil, nil, nil, errors.Wrap(errors.ErrState, "not in voting period") } - if !weave.InThePast(ctx, proposal.VotingStartTime.Time()) { + if !info.InThePast(proposal.VotingStartTime.Time()) { return nil, nil, nil, errors.Wrap(errors.ErrState, "vote before proposal start time") } - if !weave.InTheFuture(ctx, proposal.VotingEndTime.Time()) { + if !info.InTheFuture(proposal.VotingEndTime.Time()) { return nil, nil, nil, errors.Wrap(errors.ErrState, "vote after proposal end time") } @@ -172,7 +172,7 @@ func newTallyHandler(auth x.Authenticator, decoder OptionDecoder, executor Execu } func (h TallyHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if _, _, err := h.validate(ctx, db, tx); err != nil { + if _, _, err := h.validate(ctx, info, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: tallyCost}, nil @@ -180,7 +180,7 @@ func (h TallyHandler) Check(ctx context.Context, info weave.BlockInfo, db weave. } func (h TallyHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (resOut *weave.DeliverResult, errOut error) { - msg, proposal, err := h.validate(ctx, db, tx) + msg, proposal, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -246,7 +246,7 @@ func (h TallyHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weav return res, nil } -func (h TallyHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { +func (h TallyHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TallyMsg, *Proposal, error) { var msg TallyMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -262,7 +262,7 @@ func (h TallyHandler) validate(ctx context.Context, db weave.KVStore, tx weave.T if common.Status != Proposal_Submitted { return nil, nil, errors.Wrapf(errors.ErrState, "unexpected status: %s", common.Status.String()) } - if !weave.InThePast(ctx, common.VotingEndTime.Time()) { + if !info.InThePast(common.VotingEndTime.Time()) { return nil, nil, errors.Wrap(errors.ErrState, "tally before proposal end time: block time") } return &msg, proposal, nil @@ -287,7 +287,7 @@ func newCreateProposalHandler(auth x.Authenticator, decoder OptionDecoder) *Crea } func (h CreateProposalHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if _, _, _, err := h.validate(ctx, db, tx); err != nil { + if _, _, _, err := h.validate(ctx, info, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: proposalCost}, nil @@ -295,14 +295,10 @@ func (h CreateProposalHandler) Check(ctx context.Context, info weave.BlockInfo, } func (h CreateProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, rule, electorate, err := h.validate(ctx, db, tx) + msg, rule, electorate, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } - blockTime, err := weave.BlockTime(ctx) - if err != nil { - return nil, errors.Wrap(err, "block time") - } proposal := &Proposal{ Metadata: &weave.Metadata{Schema: 1}, @@ -313,7 +309,7 @@ func (h CreateProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo ElectorateRef: orm.VersionedIDRef{ID: rule.ElectorateID, Version: electorate.Version}, VotingStartTime: msg.StartTime, VotingEndTime: msg.StartTime.Add(rule.VotingPeriod.Duration()), - SubmissionTime: weave.AsUnixTime(blockTime), + SubmissionTime: info.UnixTime(), Author: msg.Author, VoteState: NewTallyResult(rule.Quorum, rule.Threshold, electorate.TotalElectorateWeight), Status: Proposal_Submitted, @@ -329,16 +325,16 @@ func (h CreateProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo return &weave.DeliverResult{Data: obj.Key()}, nil } -func (h CreateProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { +func (h CreateProposalHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateProposalMsg, *ElectionRule, *Electorate, error) { var msg CreateProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, nil, errors.Wrap(err, "load msg") } - if !weave.InTheFuture(ctx, msg.StartTime.Time()) { + if !info.InTheFuture(msg.StartTime.Time()) { return nil, nil, nil, errors.Wrap(errors.ErrInput, "start time must be in the future") } - if weave.InTheFuture(ctx, msg.StartTime.Time().Add(-maxFutureStart)) { + if info.InTheFuture(msg.StartTime.Time().Add(-maxFutureStart)) { return nil, nil, nil, errors.Wrapf(errors.ErrInput, "start time cam not be more than %s h in the future", maxFutureStart) } @@ -407,7 +403,7 @@ func newDeleteProposalHandler(auth x.Authenticator) *DeleteProposalHandler { } } -func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { +func (h DeleteProposalHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*DeleteProposalMsg, *Proposal, error) { var msg DeleteProposalMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, nil, errors.Wrap(err, "load msg") @@ -421,7 +417,7 @@ func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, t return nil, nil, errors.Wrap(errors.ErrState, "this proposal is already withdrawn") } - if weave.InThePast(ctx, prop.VotingStartTime.Time()) { + if info.InThePast(prop.VotingStartTime.Time()) { return nil, nil, errors.Wrap(errors.ErrImmutable, "voting has already started") } if !h.auth.HasAddress(ctx, prop.Author) { @@ -431,14 +427,14 @@ func (h DeleteProposalHandler) validate(ctx context.Context, db weave.KVStore, t } func (h DeleteProposalHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if _, _, err := h.validate(ctx, db, tx); err != nil { + if _, _, err := h.validate(ctx, info, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: deleteProposalCost}, nil } func (h DeleteProposalHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, prop, err := h.validate(ctx, db, tx) + msg, prop, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } diff --git a/x/paychan/handler.go b/x/paychan/handler.go index fcf6ef78..2059df6e 100644 --- a/x/paychan/handler.go +++ b/x/paychan/handler.go @@ -44,19 +44,19 @@ type createPaymentChannelHandler struct { var _ weave.Handler = (*createPaymentChannelHandler)(nil) func (h *createPaymentChannelHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if _, err := h.validate(ctx, db, tx); err != nil { + if _, err := h.validate(ctx, info, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: createPaymentChannelCost}, nil } -func (h *createPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { +func (h *createPaymentChannelHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*CreateMsg, error) { var msg CreateMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") } - if weave.IsExpired(ctx, msg.Timeout) { + if info.IsExpired(msg.Timeout) { return nil, errors.Wrapf(errors.ErrExpired, "timeout in the past") } if !h.auth.HasAddress(ctx, msg.Src) { @@ -66,7 +66,7 @@ func (h *createPaymentChannelHandler) validate(ctx context.Context, db weave.KVS } func (h *createPaymentChannelHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, err := h.validate(ctx, db, tx) + msg, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -103,18 +103,18 @@ type transferPaymentChannelHandler struct { var _ weave.Handler = (*transferPaymentChannelHandler)(nil) func (h *transferPaymentChannelHandler) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - if _, err := h.validate(ctx, db, tx); err != nil { + if _, err := h.validate(ctx, info, db, tx); err != nil { return nil, err } return &weave.CheckResult{GasAllocated: transferPaymentChannelCost}, nil } -func (h *transferPaymentChannelHandler) validate(ctx context.Context, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { +func (h *transferPaymentChannelHandler) validate(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*TransferMsg, error) { var msg TransferMsg if err := weave.LoadMsg(tx, &msg); err != nil { return nil, errors.Wrap(err, "load msg") } - if weave.GetChainID(ctx) != msg.Payment.ChainID { + if info.ChainID() != msg.Payment.ChainID { return nil, errors.Wrap(errors.ErrMsg, "invalid chain ID") } @@ -150,7 +150,7 @@ func (h *transferPaymentChannelHandler) validate(ctx context.Context, db weave.K } func (h *transferPaymentChannelHandler) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - msg, err := h.validate(ctx, db, tx) + msg, err := h.validate(ctx, info, db, tx) if err != nil { return nil, err } @@ -231,7 +231,7 @@ func (h *closePaymentChannelHandler) Deliver(ctx context.Context, info weave.Blo return nil, err } - if !weave.IsExpired(ctx, pc.Timeout) { + if !info.IsExpired(pc.Timeout) { // If timeout was not reached, only the recipient is allowed to // close the channel. if !h.auth.HasAddress(ctx, pc.Recipient) { From cf2606d73da3ff4edf0529c5949ad5ea810d9472 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 19:59:27 +0200 Subject: [PATCH 09/17] Fixed weavetest tests --- weavetest/auth.go | 6 +++--- weavetest/decorators_test.go | 20 ++++++++++---------- weavetest/handlers_test.go | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/weavetest/auth.go b/weavetest/auth.go index 5490ad79..0d2755e5 100644 --- a/weavetest/auth.go +++ b/weavetest/auth.go @@ -32,7 +32,7 @@ func (a *Auth) GetConditions(context.Context) []weave.Condition { return a.Signers } -func (a *Auth) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { +func (a *Auth) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.Signers { if addr.Equals(s.Address()) { return true @@ -53,7 +53,7 @@ type CtxAuth struct { Key string } -func (a *CtxAuth) SetConditions(ctx context.Context, info weave.BlockInfo, permissions ...weave.Condition) context.Context { +func (a *CtxAuth) SetConditions(ctx context.Context, permissions ...weave.Condition) context.Context { return context.WithValue(ctx, a.Key, permissions) } @@ -69,7 +69,7 @@ func (a *CtxAuth) GetConditions(ctx context.Context) []weave.Condition { return conds } -func (a *CtxAuth) HasAddress(ctx context.Context, info weave.BlockInfo, addr weave.Address) bool { +func (a *CtxAuth) HasAddress(ctx context.Context, addr weave.Address) bool { for _, s := range a.GetConditions(ctx) { if addr.Equals(s.Address()) { return true diff --git a/weavetest/decorators_test.go b/weavetest/decorators_test.go index e7f92301..27fc2bd2 100644 --- a/weavetest/decorators_test.go +++ b/weavetest/decorators_test.go @@ -13,10 +13,10 @@ func TestSuccessfulDecorator(t *testing.T) { h Handler ) - _, _ = d.Check(nil, nil, nil, &h) + _, _ = d.Check(nil, weave.BlockInfo{}, nil, nil, &h) assertHCounts(t, &h, 1, 0) - _, _ = d.Deliver(nil, nil, nil, &h) + _, _ = d.Deliver(nil, weave.BlockInfo{}, nil, nil, &h) assertHCounts(t, &h, 1, 1) } @@ -30,12 +30,12 @@ func TestDecoratorWithError(t *testing.T) { // Otherwise using nil would panic. var handler weave.Handler = nil - _, err := d.Check(nil, nil, nil, handler) + _, err := d.Check(nil, weave.BlockInfo{}, nil, nil, handler) if want := errors.ErrUnauthorized; !want.Is(err) { t.Errorf("want %q, got %q", want, err) } - _, err = d.Deliver(nil, nil, nil, handler) + _, err = d.Deliver(nil, weave.BlockInfo{}, nil, nil, handler) if want := errors.ErrNotFound; !want.Is(err) { t.Errorf("want %q, got %q", want, err) } @@ -47,26 +47,26 @@ func TestDecoratorCallCount(t *testing.T) { assertDCounts(t, &d, 0, 0) - d.Check(nil, nil, nil, &Handler{}) + d.Check(nil, weave.BlockInfo{}, nil, nil, &Handler{}) assertDCounts(t, &d, 1, 0) - d.Check(nil, nil, nil, &Handler{}) + d.Check(nil, weave.BlockInfo{}, nil, nil, &Handler{}) assertDCounts(t, &d, 2, 0) - d.Deliver(nil, nil, nil, &Handler{}) + d.Deliver(nil, weave.BlockInfo{}, nil, nil, &Handler{}) assertDCounts(t, &d, 2, 1) - d.Deliver(nil, nil, nil, &Handler{}) + d.Deliver(nil, weave.BlockInfo{}, nil, nil, &Handler{}) assertDCounts(t, &d, 2, 2) // Failing counter must increment as well. d.CheckErr = errors.ErrNotFound d.DeliverErr = errors.ErrNotFound - d.Check(nil, nil, nil, &Handler{}) + d.Check(nil, weave.BlockInfo{}, nil, nil, &Handler{}) assertDCounts(t, &d, 3, 2) - d.Deliver(nil, nil, nil, &Handler{}) + d.Deliver(nil, weave.BlockInfo{}, nil, nil, &Handler{}) assertDCounts(t, &d, 3, 3) } diff --git a/weavetest/handlers_test.go b/weavetest/handlers_test.go index 5e721fd5..5a05502e 100644 --- a/weavetest/handlers_test.go +++ b/weavetest/handlers_test.go @@ -14,12 +14,12 @@ func TestHandlerWithError(t *testing.T) { DeliverErr: errors.ErrNotFound, } - _, err := h.Check(nil, nil, nil) + _, err := h.Check(nil, weave.BlockInfo{}, nil, nil) if want := errors.ErrUnauthorized; !want.Is(err) { t.Errorf("want %q, got %q", want, err) } - _, err = h.Deliver(nil, nil, nil) + _, err = h.Deliver(nil, weave.BlockInfo{}, nil, nil) if want := errors.ErrNotFound; !want.Is(err) { t.Errorf("want %q, got %q", want, err) } @@ -31,26 +31,26 @@ func TestHandlerCallCount(t *testing.T) { assertHCounts(t, &h, 0, 0) - h.Check(nil, nil, nil) + h.Check(nil, weave.BlockInfo{}, nil, nil) assertHCounts(t, &h, 1, 0) - h.Check(nil, nil, nil) + h.Check(nil, weave.BlockInfo{}, nil, nil) assertHCounts(t, &h, 2, 0) - h.Deliver(nil, nil, nil) + h.Deliver(nil, weave.BlockInfo{}, nil, nil) assertHCounts(t, &h, 2, 1) - h.Deliver(nil, nil, nil) + h.Deliver(nil, weave.BlockInfo{}, nil, nil) assertHCounts(t, &h, 2, 2) // Failing counter must increment as well. h.CheckErr = errors.ErrNotFound h.DeliverErr = errors.ErrNotFound - h.Check(nil, nil, nil) + h.Check(nil, weave.BlockInfo{}, nil, nil) assertHCounts(t, &h, 3, 2) - h.Deliver(nil, nil, nil) + h.Deliver(nil, weave.BlockInfo{}, nil, nil) assertHCounts(t, &h, 3, 3) } @@ -82,11 +82,11 @@ func TestHandlerResult(t *testing.T) { DeliverResult: wantDres, } - gotCres, _ := h.Check(nil, nil, nil) + gotCres, _ := h.Check(nil, weave.BlockInfo{}, nil, nil) if !reflect.DeepEqual(&wantCres, gotCres) { t.Fatalf("got check result: %+v", gotCres) } - gotDres, _ := h.Deliver(nil, nil, nil) + gotDres, _ := h.Deliver(nil, weave.BlockInfo{}, nil, nil) if !reflect.DeepEqual(&wantDres, gotDres) { t.Fatalf("got deliver result: %+v", gotDres) } From d8b50a93b95e669ec85e2ba23d2edd243d44cb8d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 20:07:00 +0200 Subject: [PATCH 10/17] Update gconf and migrations tests --- gconf/handler_test.go | 5 ++--- migration/handler_test.go | 16 +++++++++------- weavetest/tx.go | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/gconf/handler_test.go b/gconf/handler_test.go index 531d1ff8..8faa93a1 100644 --- a/gconf/handler_test.go +++ b/gconf/handler_test.go @@ -126,9 +126,8 @@ func TestUpdateConfigurationHandler(t *testing.T) { auth := &weavetest.CtxAuth{Key: "auth"} handler := NewUpdateConfigurationHandler("mypkg", &c, auth) - ctx := weave.WithHeight(context.Background(), 999) - ctx = weave.WithChainID(ctx, "mychain-123") - ctx = auth.SetConditions(ctx, tc.MsgConditions...) + info := weavetest.BlockInfo("mychain-123", 999) + ctx := auth.SetConditions(context.Background(), tc.MsgConditions...) tx := &weavetest.Tx{Msg: tc.Msg} diff --git a/migration/handler_test.go b/migration/handler_test.go index abfdece4..869a37fc 100644 --- a/migration/handler_test.go +++ b/migration/handler_test.go @@ -42,11 +42,12 @@ func TestSchemaMigratingHandler(t *testing.T) { Metadata: &weave.Metadata{Schema: 1}, Content: "foo", } - _, err = handler.Check(nil, db, &weavetest.Tx{Msg: msg1}) + info := weavetest.BlockInfo("mychain-123", 999) + _, err = handler.Check(nil, info, db, &weavetest.Tx{Msg: msg1}) assert.Nil(t, err) assert.Equal(t, msg1.Metadata.Schema, uint32(1)) assert.Equal(t, msg1.Content, "foo") - _, err = handler.Deliver(nil, db, &weavetest.Tx{Msg: msg1}) + _, err = handler.Deliver(nil, info, db, &weavetest.Tx{Msg: msg1}) assert.Nil(t, err) assert.Equal(t, msg1.Metadata.Schema, uint32(1)) assert.Equal(t, msg1.Content, "foo") @@ -55,11 +56,11 @@ func TestSchemaMigratingHandler(t *testing.T) { // the schema as well. ensureSchemaVersion(t, db, thisPkgName, 2) - _, err = handler.Check(nil, db, &weavetest.Tx{Msg: msg1}) + _, err = handler.Check(nil, info, db, &weavetest.Tx{Msg: msg1}) assert.Nil(t, err) assert.Equal(t, msg1.Metadata.Schema, uint32(2)) assert.Equal(t, msg1.Content, "foo m2") - _, err = handler.Deliver(nil, db, &weavetest.Tx{Msg: msg1}) + _, err = handler.Deliver(nil, info, db, &weavetest.Tx{Msg: msg1}) assert.Nil(t, err) assert.Equal(t, msg1.Metadata.Schema, uint32(2)) assert.Equal(t, msg1.Content, "foo m2") @@ -69,11 +70,11 @@ func TestSchemaMigratingHandler(t *testing.T) { Metadata: &weave.Metadata{Schema: 2}, Content: "bar", } - _, err = handler.Check(nil, db, &weavetest.Tx{Msg: msg2}) + _, err = handler.Check(nil, info, db, &weavetest.Tx{Msg: msg2}) assert.Nil(t, err) assert.Equal(t, msg2.Metadata.Schema, uint32(2)) assert.Equal(t, msg2.Content, "bar") - _, err = handler.Deliver(nil, db, &weavetest.Tx{Msg: msg2}) + _, err = handler.Deliver(nil, info, db, &weavetest.Tx{Msg: msg2}) assert.Nil(t, err) assert.Equal(t, msg2.Metadata.Schema, uint32(2)) assert.Equal(t, msg2.Content, "bar") @@ -217,7 +218,8 @@ func TestSchemaRoutingHandler(t *testing.T) { for testName, tc := range cases { t.Run(testName, func(t *testing.T) { - _, err := tc.Handler.Deliver(nil, nil, tc.Tx) + info := weavetest.BlockInfo("test-chain", 5) + _, err := tc.Handler.Deliver(nil, info, nil, tc.Tx) if !tc.WantErr.Is(err) { t.Fatalf("unexpected error result: %s", err) } diff --git a/weavetest/tx.go b/weavetest/tx.go index 3d2da09c..362ce762 100644 --- a/weavetest/tx.go +++ b/weavetest/tx.go @@ -2,8 +2,10 @@ package weavetest import ( "encoding/binary" + "time" "github.com/iov-one/weave" + abci "github.com/tendermint/tendermint/abci/types" ) // Tx represents a weave transaction. @@ -69,3 +71,17 @@ func SequenceID(n uint64) []byte { binary.BigEndian.PutUint64(b, n) return b } + +// BlockInfo sets up a BlockInfo struct with a chainID and a height. +// Uses given time if provided, otherwise time.Now() +func BlockInfo(chainID string, height int64, ts ...time.Time) weave.BlockInfo { + t := time.Now() + if len(ts) > 0 { + t = ts[0] + } + res, err := weave.NewBlockInfo(abci.Header{Height: height, Time: t}, weave.CommitInfo{}, chainID, nil) + if err != nil { + panic(err) + } + return res +} From 7e0122eb5fe5ad81cf8a7e9dfea0106f796477c1 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 20:19:05 +0200 Subject: [PATCH 11/17] Fix aswap tests, cleaner weavetest helpers --- gconf/handler_test.go | 2 +- migration/handler_test.go | 4 ++-- weavetest/tx.go | 9 +++++++-- x/aswap/handler_test.go | 39 ++++++++++++++++++++------------------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/gconf/handler_test.go b/gconf/handler_test.go index 8faa93a1..a024180c 100644 --- a/gconf/handler_test.go +++ b/gconf/handler_test.go @@ -126,7 +126,7 @@ func TestUpdateConfigurationHandler(t *testing.T) { auth := &weavetest.CtxAuth{Key: "auth"} handler := NewUpdateConfigurationHandler("mypkg", &c, auth) - info := weavetest.BlockInfo("mychain-123", 999) + info := weavetest.BlockInfo(999) ctx := auth.SetConditions(context.Background(), tc.MsgConditions...) tx := &weavetest.Tx{Msg: tc.Msg} diff --git a/migration/handler_test.go b/migration/handler_test.go index 869a37fc..f9e48908 100644 --- a/migration/handler_test.go +++ b/migration/handler_test.go @@ -42,7 +42,7 @@ func TestSchemaMigratingHandler(t *testing.T) { Metadata: &weave.Metadata{Schema: 1}, Content: "foo", } - info := weavetest.BlockInfo("mychain-123", 999) + info := weavetest.BlockInfo(999) _, err = handler.Check(nil, info, db, &weavetest.Tx{Msg: msg1}) assert.Nil(t, err) assert.Equal(t, msg1.Metadata.Schema, uint32(1)) @@ -218,7 +218,7 @@ func TestSchemaRoutingHandler(t *testing.T) { for testName, tc := range cases { t.Run(testName, func(t *testing.T) { - info := weavetest.BlockInfo("test-chain", 5) + info := weavetest.BlockInfo(5) _, err := tc.Handler.Deliver(nil, info, nil, tc.Tx) if !tc.WantErr.Is(err) { t.Fatalf("unexpected error result: %s", err) diff --git a/weavetest/tx.go b/weavetest/tx.go index 362ce762..0a7f23b7 100644 --- a/weavetest/tx.go +++ b/weavetest/tx.go @@ -72,9 +72,14 @@ func SequenceID(n uint64) []byte { return b } -// BlockInfo sets up a BlockInfo struct with a chainID and a height. +// BlockInfo returns a simple BlockInfo struct given just a height and optional time +func BlockInfo(height int64, ts ...time.Time) weave.BlockInfo { + return BlockInfoWithChain("test-chain", height, ts...) +} + +// BlockInfoWithChain sets up a BlockInfo struct with a chainID and a height. // Uses given time if provided, otherwise time.Now() -func BlockInfo(chainID string, height int64, ts ...time.Time) weave.BlockInfo { +func BlockInfoWithChain(chainID string, height int64, ts ...time.Time) weave.BlockInfo { t := time.Now() if len(ts) > 0 { t = ts[0] diff --git a/x/aswap/handler_test.go b/x/aswap/handler_test.go index cf42901c..002af1b5 100644 --- a/x/aswap/handler_test.go +++ b/x/aswap/handler_test.go @@ -117,10 +117,10 @@ func TestCreateHandler(t *testing.T) { db := store.MemStore() migration.MustInitPkg(db, "aswap", "cash") - ctx := weave.WithHeight(context.Background(), 500) - ctx = weave.WithBlockTime(ctx, blockNow) + ctx := context.Background() + info := weavetest.BlockInfo(500, blockNow) if spec.setup != nil { - ctx = spec.setup(ctx, db) + ctx = spec.setup(ctx, info, db) } if spec.mutator != nil { spec.mutator(createMsg) @@ -159,7 +159,7 @@ func TestReleaseHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context + setup func(info weave.BlockInfo, db weave.KVStore) weave.BlockInfo check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -203,8 +203,8 @@ func TestReleaseHandler(t *testing.T) { }, }, "Expired": { - setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { - return weave.WithBlockTime(ctx, time.Now().Add(10*time.Hour)) + setup: func(info weave.BlockInfo, db weave.KVStore) weave.BlockInfo { + return weavetest.BlockInfo(info.Height(), time.Now().Add(10*time.Hour)) }, wantDeliverErr: errors.ErrState, wantCheckErr: errors.ErrState, @@ -224,13 +224,13 @@ func TestReleaseHandler(t *testing.T) { db := store.MemStore() migration.MustInitPkg(db, "aswap", "cash") - ctx := weave.WithHeight(context.Background(), 500) - ctx = weave.WithBlockTime(ctx, blockNow) + ctx := context.Background() + info := weavetest.BlockInfo(500, blockNow) // setup a swap createCtx := authenticator.SetConditions(ctx, alice) setBalance(t, db, alice.Address(), initialCoins) tx := &weavetest.Tx{Msg: createMsg} - _, err = r.Deliver(createCtx, db, tx) + _, err = r.Deliver(createCtx, info, db, tx) assert.Nil(t, err) releaseMsg := &aswap.ReleaseMsg{ @@ -240,7 +240,7 @@ func TestReleaseHandler(t *testing.T) { } if spec.setup != nil { - ctx = spec.setup(ctx, db) + info = spec.setup(info, db) } if spec.mutator != nil { spec.mutator(releaseMsg) @@ -271,7 +271,7 @@ func TestReturnHandler(t *testing.T) { assert.Nil(t, err) cases := map[string]struct { - setup func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context + setup func(info weave.BlockInfo, db weave.KVStore) weave.BlockInfo check func(t *testing.T, db weave.KVStore) wantCheckErr *errors.Error wantDeliverErr *errors.Error @@ -279,8 +279,8 @@ func TestReturnHandler(t *testing.T) { mutator func(db *aswap.ReturnSwapMsg) }{ "Happy Path, includes no auth check": { - setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { - return weave.WithBlockTime(ctx, blockNow.Add(2*time.Hour)) + setup: func(info weave.BlockInfo, db weave.KVStore) weave.BlockInfo { + return weavetest.BlockInfo(info.Height(), blockNow.Add(2*time.Hour)) }, wantDeliverErr: nil, wantCheckErr: nil, @@ -310,8 +310,8 @@ func TestReturnHandler(t *testing.T) { }, }, "Not Expired": { - setup: func(ctx context.Context, info weave.BlockInfo, db weave.KVStore) context.Context { - return weave.WithBlockTime(ctx, blockNow) + setup: func(info weave.BlockInfo, db weave.KVStore) weave.BlockInfo { + return weavetest.BlockInfo(info.Height(), blockNow) }, wantDeliverErr: errors.ErrState, wantCheckErr: errors.ErrState, @@ -331,13 +331,14 @@ func TestReturnHandler(t *testing.T) { db := store.MemStore() migration.MustInitPkg(db, "aswap", "cash") - ctx := weave.WithHeight(context.Background(), 500) - ctx = weave.WithBlockTime(ctx, blockNow) + ctx := context.Background() + info := weavetest.BlockInfo(500, blockNow) + // setup a swap createCtx := authenticator.SetConditions(ctx, alice) setBalance(t, db, alice.Address(), initialCoins) tx := &weavetest.Tx{Msg: createMsg} - _, err = r.Deliver(createCtx, db, tx) + _, err = r.Deliver(createCtx, info, db, tx) assert.Nil(t, err) returnMsg := &aswap.ReturnSwapMsg{ @@ -346,7 +347,7 @@ func TestReturnHandler(t *testing.T) { } if spec.setup != nil { - ctx = spec.setup(ctx, db) + info = spec.setup(info, db) } if spec.mutator != nil { spec.mutator(returnMsg) From e8f5507b614befb8b32b5110f054d7a10a6956fc Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 20:23:10 +0200 Subject: [PATCH 12/17] Fix batch tests --- x/batch/decorator_test.go | 57 +++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/x/batch/decorator_test.go b/x/batch/decorator_test.go index 38116d59..6c2b898a 100644 --- a/x/batch/decorator_test.go +++ b/x/batch/decorator_test.go @@ -1,12 +1,14 @@ package batch_test import ( + "context" "errors" "strings" "testing" "github.com/iov-one/weave" "github.com/iov-one/weave/coin" + "github.com/iov-one/weave/weavetest" "github.com/iov-one/weave/x/batch" . "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/mock" @@ -50,12 +52,12 @@ func (m *mockHelper) GetMsg() (weave.Msg, error) { } func (m *mockHelper) Check(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.CheckResult, error) { - args := m.Called(ctx, store, tx) + args := m.Called(ctx, info, store, tx) return args.Get(0).(*weave.CheckResult), args.Error(1) } func (m *mockHelper) Deliver(ctx context.Context, info weave.BlockInfo, store weave.KVStore, tx weave.Tx) (*weave.DeliverResult, error) { - args := m.Called(ctx, store, tx) + args := m.Called(ctx, info, store, tx) return args.Get(0).(*weave.DeliverResult), args.Error(1) } @@ -107,7 +109,8 @@ func TestDecorator(t *testing.T) { msg.On("MsgList").Return(make([]weave.Msg, num), nil).Times(2) helper.On("GetMsg").Return(msg, nil).Times(2) - helper.On("Check", nil, nil, mock.Anything).Return(&weave.CheckResult{ + info := weavetest.BlockInfo(33) + helper.On("Check", nil, info, nil, mock.Anything).Return(&weave.CheckResult{ Data: make([]byte, 1), Log: logVal, GasAllocated: gas, @@ -115,7 +118,7 @@ func TestDecorator(t *testing.T) { RequiredFee: fee, }, nil).Times(int(num)) - checkRes, err := decorator.Check(nil, nil, helper, helper) + checkRes, err := decorator.Check(nil, info, nil, helper, helper) So(err, ShouldBeNil) data, _ := mockData(num, dataContent).Marshal() So(checkRes, ShouldResemble, &weave.CheckResult{ @@ -126,7 +129,7 @@ func TestDecorator(t *testing.T) { RequiredFee: combinedFee, }) - helper.On("Deliver", nil, nil, mock.Anything).Return(&weave.DeliverResult{ + helper.On("Deliver", nil, info, nil, mock.Anything).Return(&weave.DeliverResult{ Data: make([]byte, 1), Log: logVal, GasUsed: gas, @@ -135,7 +138,7 @@ func TestDecorator(t *testing.T) { RequiredFee: fee, }, nil).Times(int(num)) - deliverRes, err := decorator.Deliver(nil, nil, helper, helper) + deliverRes, err := decorator.Deliver(nil, info, nil, helper, helper) So(err, ShouldBeNil) So(deliverRes, ShouldResemble, &weave.DeliverResult{ Data: data, @@ -149,6 +152,8 @@ func TestDecorator(t *testing.T) { msg.AssertExpectations(t) }) + info := weavetest.BlockInfo(72) + Convey("Combine required fees with none", func() { // 4 elements, 1 and 3 with fees, 2 and 4 without num := int64(4) @@ -175,12 +180,12 @@ func TestDecorator(t *testing.T) { } } // fee, zero, fee2, zero - helper.On("Deliver", nil, nil, mock.Anything).Return(makeRes(fee), nil).Times(1) - helper.On("Deliver", nil, nil, mock.Anything).Return(makeRes(zero), nil).Times(1) - helper.On("Deliver", nil, nil, mock.Anything).Return(makeRes(fee2), nil).Times(1) - helper.On("Deliver", nil, nil, mock.Anything).Return(makeRes(zero), nil).Times(1) + helper.On("Deliver", nil, info, nil, mock.Anything).Return(makeRes(fee), nil).Times(1) + helper.On("Deliver", nil, info, nil, mock.Anything).Return(makeRes(zero), nil).Times(1) + helper.On("Deliver", nil, info, nil, mock.Anything).Return(makeRes(fee2), nil).Times(1) + helper.On("Deliver", nil, info, nil, mock.Anything).Return(makeRes(zero), nil).Times(1) - deliverRes, err := decorator.Deliver(nil, nil, helper, helper) + deliverRes, err := decorator.Deliver(nil, info, nil, helper, helper) So(err, ShouldBeNil) data, _ := mockData(num, dataContent).Marshal() So(deliverRes, ShouldResemble, &weave.DeliverResult{ @@ -195,12 +200,12 @@ func TestDecorator(t *testing.T) { Convey("Wrong tx type", func() { helper.On("GetMsg").Return(wrongWeaveMsg{}, nil).Times(2) - helper.On("Deliver", nil, nil, mock.Anything).Return(&weave.DeliverResult{}, nil).Times(1) - helper.On("Check", nil, nil, mock.Anything).Return(&weave.CheckResult{}, nil).Times(1) + helper.On("Deliver", nil, info, nil, mock.Anything).Return(&weave.DeliverResult{}, nil).Times(1) + helper.On("Check", nil, info, nil, mock.Anything).Return(&weave.CheckResult{}, nil).Times(1) - _, err := decorator.Check(nil, nil, helper, helper) + _, err := decorator.Check(nil, info, nil, helper, helper) So(err, ShouldBeNil) - _, err = decorator.Deliver(nil, nil, helper, helper) + _, err = decorator.Deliver(nil, info, nil, helper, helper) So(err, ShouldBeNil) helper.AssertExpectations(t) }) @@ -210,9 +215,9 @@ func TestDecorator(t *testing.T) { expectedErr := errors.New("asd") helper.On("GetMsg").Return(msg, expectedErr).Times(2) - _, err := decorator.Check(nil, nil, helper, helper) + _, err := decorator.Check(nil, info, nil, helper, helper) So(err, ShouldEqual, expectedErr) - _, err = decorator.Deliver(nil, nil, helper, helper) + _, err = decorator.Deliver(nil, info, nil, helper, helper) So(err, ShouldEqual, expectedErr) helper.AssertExpectations(t) }) @@ -226,14 +231,14 @@ func TestDecorator(t *testing.T) { helper.On("GetMsg").Return(msg, nil).Times(2) // two different returns, with different fees - helper.On("Check", nil, nil, mock.Anything).Return(&weave.CheckResult{ + helper.On("Check", nil, info, nil, mock.Anything).Return(&weave.CheckResult{ Data: make([]byte, 2), Log: logVal, GasAllocated: gas, GasPayment: gas, RequiredFee: coin.Coin{Whole: 1, Ticker: "IOV"}, }, nil).Times(1) - helper.On("Check", nil, nil, mock.Anything).Return(&weave.CheckResult{ + helper.On("Check", nil, info, nil, mock.Anything).Return(&weave.CheckResult{ Data: make([]byte, 1), Log: logVal, GasAllocated: gas, @@ -241,7 +246,7 @@ func TestDecorator(t *testing.T) { RequiredFee: coin.Coin{Whole: 1, Ticker: "LSK"}, }, nil).Times(1) - _, err := decorator.Check(nil, nil, helper, helper) + _, err := decorator.Check(nil, info, nil, helper, helper) So(err, ShouldNotBeNil) }) @@ -250,9 +255,9 @@ func TestDecorator(t *testing.T) { helper.On("GetMsg").Return(msg, nil).Times(2) msg.On("Validate").Return(expectedErr).Times(2) - _, err := decorator.Check(nil, nil, helper, helper) + _, err := decorator.Check(nil, info, nil, helper, helper) So(err, ShouldEqual, expectedErr) - _, err = decorator.Deliver(nil, nil, helper, helper) + _, err = decorator.Deliver(nil, info, nil, helper, helper) So(err, ShouldEqual, expectedErr) helper.AssertExpectations(t) msg.AssertExpectations(t) @@ -263,12 +268,12 @@ func TestDecorator(t *testing.T) { helper.On("GetMsg").Return(msg, nil).Times(2) msg.On("Validate").Return(nil).Times(2) msg.On("MsgList").Return(make([]weave.Msg, 4), nil).Times(2) - helper.On("Deliver", nil, nil, mock.Anything).Return((*weave.DeliverResult)(nil), expectedErr).Times(1) - helper.On("Check", nil, nil, mock.Anything).Return((*weave.CheckResult)(nil), expectedErr).Times(1) + helper.On("Deliver", nil, info, nil, mock.Anything).Return((*weave.DeliverResult)(nil), expectedErr).Times(1) + helper.On("Check", nil, info, nil, mock.Anything).Return((*weave.CheckResult)(nil), expectedErr).Times(1) - _, err := decorator.Check(nil, nil, helper, helper) + _, err := decorator.Check(nil, info, nil, helper, helper) So(err, ShouldEqual, expectedErr) - _, err = decorator.Deliver(nil, nil, helper, helper) + _, err = decorator.Deliver(nil, info, nil, helper, helper) So(err, ShouldEqual, expectedErr) helper.AssertExpectations(t) msg.AssertExpectations(t) From 1cd27cb73dbc99ab496ca2a39ab52a150a8b109c Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 20:32:19 +0200 Subject: [PATCH 13/17] Fix cash, dist, escrow, utils tests --- x/cash/configuration_test.go | 2 +- x/cash/dynamicfee_test.go | 10 ++++++---- x/cash/handler_test.go | 5 +++-- x/cash/staticfee_test.go | 5 +++-- x/distribution/handler_test.go | 13 +++++++------ x/escrow/handler_test.go | 18 +++++++++--------- x/utils/action_tagger_test.go | 2 +- x/utils/key_tagger_test.go | 1 + x/utils/recover_test.go | 2 ++ x/utils/savepoint_test.go | 6 ++++-- 10 files changed, 37 insertions(+), 27 deletions(-) diff --git a/x/cash/configuration_test.go b/x/cash/configuration_test.go index 102dc264..8000b57b 100644 --- a/x/cash/configuration_test.go +++ b/x/cash/configuration_test.go @@ -83,7 +83,7 @@ func TestConfigurationHandler(t *testing.T) { assert.Equal(t, tc.init, load) // call deliver - _, err = h.Deliver(nil, kv, &weavetest.Tx{Msg: &tc.update}) + _, err = h.Deliver(nil, weavetest.BlockInfo(7), kv, &weavetest.Tx{Msg: &tc.update}) assert.Nil(t, err) // should update stored config diff --git a/x/cash/dynamicfee_test.go b/x/cash/dynamicfee_test.go index 8287a21b..4deb15d7 100644 --- a/x/cash/dynamicfee_test.go +++ b/x/cash/dynamicfee_test.go @@ -50,11 +50,12 @@ func TestCacheWriteFail(t *testing.T) { decorator := NewDynamicFeeDecorator(auth, ctrl) - if _, err := decorator.Check(context.TODO(), db, tx, handler); !myerr.Is(err) { + info := weavetest.BlockInfo(7) + if _, err := decorator.Check(context.TODO(), info, db, tx, handler); !myerr.Is(err) { t.Fatalf("unexpected check result error: %+v", err) } - if _, err := decorator.Deliver(context.TODO(), db, tx, handler); !myerr.Is(err) { + if _, err := decorator.Deliver(context.TODO(), info, db, tx, handler); !myerr.Is(err) { t.Fatalf("unexpected deliver result error: %+v", err) } } @@ -276,7 +277,8 @@ func TestDynamicFeeDecorator(t *testing.T) { cache := db.CacheWrap() - cRes, err := h.Check(nil, cache, tx, tc.handler) + info := weavetest.BlockInfo(7) + cRes, err := h.Check(nil, info, cache, tx, tc.handler) if !tc.wantCheckErr.Is(err) { t.Fatalf("got check error: %v", err) } @@ -295,7 +297,7 @@ func TestDynamicFeeDecorator(t *testing.T) { cache.Discard() - if _, err = h.Deliver(nil, cache, tx, tc.handler); !tc.wantDeliverErr.Is(err) { + if _, err = h.Deliver(nil, info, cache, tx, tc.handler); !tc.wantDeliverErr.Is(err) { t.Fatalf("got deliver error: %v", err) } diff --git a/x/cash/handler_test.go b/x/cash/handler_test.go index 7964c0f0..92f81e29 100644 --- a/x/cash/handler_test.go +++ b/x/cash/handler_test.go @@ -99,10 +99,11 @@ func TestSend(t *testing.T) { tx := &weavetest.Tx{Msg: tc.msg} - if _, err := h.Check(nil, kv, tx); !tc.wantCheckErr.Is(err) { + info := weavetest.BlockInfo(7) + if _, err := h.Check(nil, info, kv, tx); !tc.wantCheckErr.Is(err) { t.Fatalf("unexpected check error: %+v", err) } - if _, err := h.Deliver(nil, kv, tx); !tc.wantDeliverErr.Is(err) { + if _, err := h.Deliver(nil, info, kv, tx); !tc.wantDeliverErr.Is(err) { t.Fatalf("unexpected deliver error: %+v", err) } }) diff --git a/x/cash/staticfee_test.go b/x/cash/staticfee_test.go index fd06f180..72dc8162 100644 --- a/x/cash/staticfee_test.go +++ b/x/cash/staticfee_test.go @@ -176,9 +176,10 @@ func TestFees(t *testing.T) { tx := &feeTx{tc.fee} - _, err := h.Check(nil, kv, tx, &weavetest.Handler{}) + info := weavetest.BlockInfo(7) + _, err := h.Check(nil, info, kv, tx, &weavetest.Handler{}) assert.True(t, tc.expect(err), "%+v", err) - _, err = h.Deliver(nil, kv, tx, &weavetest.Handler{}) + _, err = h.Deliver(nil, info, kv, tx, &weavetest.Handler{}) assert.True(t, tc.expect(err), "%+v", err) }) } diff --git a/x/distribution/handler_test.go b/x/distribution/handler_test.go index ab24afcb..072295e0 100644 --- a/x/distribution/handler_test.go +++ b/x/distribution/handler_test.go @@ -358,7 +358,8 @@ func TestHandlers(t *testing.T) { for i, a := range tc.actions { cache := db.CacheWrap() - if _, err := rt.Check(a.ctx(), cache, a.tx()); !a.wantCheckErr.Is(err) { + ctx, info := a.ctx() + if _, err := rt.Check(ctx, info, cache, a.tx()); !a.wantCheckErr.Is(err) { t.Logf("want: %+v", a.wantCheckErr) t.Logf(" got: %+v", err) t.Fatalf("action %d check (%T)", i, a.msg) @@ -369,7 +370,7 @@ func TestHandlers(t *testing.T) { continue } - if _, err := rt.Deliver(a.ctx(), db, a.tx()); !a.wantDeliverErr.Is(err) { + if _, err := rt.Deliver(ctx, info, db, a.tx()); !a.wantDeliverErr.Is(err) { t.Logf("want: %+v", a.wantDeliverErr) t.Logf(" got: %+v", err) t.Fatalf("action %d delivery (%T)", i, a.msg) @@ -410,11 +411,11 @@ func (a *action) tx() weave.Tx { return &weavetest.Tx{Msg: a.msg} } -func (a *action) ctx() context.Context { - ctx := weave.WithHeight(context.Background(), a.blocksize) - ctx = weave.WithChainID(ctx, "testchain-123") +func (a *action) ctx() (context.Context, weave.BlockInfo) { + info := weavetest.BlockInfoWithChain("testchain-123", a.blocksize) + ctx := context.Background() auth := &weavetest.CtxAuth{Key: "auth"} - return auth.SetConditions(ctx, a.conditions...) + return auth.SetConditions(ctx, a.conditions...), info } func TestFindGdc(t *testing.T) { diff --git a/x/escrow/handler_test.go b/x/escrow/handler_test.go index 4368aa1d..7149e4ae 100644 --- a/x/escrow/handler_test.go +++ b/x/escrow/handler_test.go @@ -625,16 +625,18 @@ func TestHandler(t *testing.T) { for j, p := range tc.prep { // try check cache := db.CacheWrap() - _, err = router.Check(p.ctx(), cache, p.tx()) + ctx, info := p.ctx() + _, err = router.Check(ctx, info, cache, p.tx()) require.NoError(t, err, "%d", j) cache.Discard() // then perform - _, err = router.Deliver(p.ctx(), db, p.tx()) + _, err = router.Deliver(ctx, info, db, p.tx()) require.NoError(t, err, "%d", j) } - _, err = router.Deliver(tc.do.ctx(), db, tc.do.tx()) + ctx, info := tc.do.ctx() + _, err = router.Deliver(ctx, info, db, tc.do.tx()) if tc.isError { require.Error(t, err) } else { @@ -694,14 +696,12 @@ func (a action) tx() weave.Tx { return &weavetest.Tx{Msg: a.msg} } -func (a action) ctx() context.Context { - ctx := context.Background() +func (a action) ctx() (context.Context, weave.BlockInfo) { + ctx := authenticator().SetConditions(context.Background(), a.perms...) if !a.blockTime.IsZero() { - ctx = weave.WithBlockTime(ctx, a.blockTime) - } else { - ctx = weave.WithBlockTime(ctx, blockNow) + return ctx, weavetest.BlockInfo(77, a.blockTime) } - return authenticator().SetConditions(ctx, a.perms...) + return ctx, weavetest.BlockInfo(77, blockNow) } // authenticator returns a default for all tests... diff --git a/x/utils/action_tagger_test.go b/x/utils/action_tagger_test.go index 662ca77a..004a1fa5 100644 --- a/x/utils/action_tagger_test.go +++ b/x/utils/action_tagger_test.go @@ -80,7 +80,7 @@ func TestActionTagger(t *testing.T) { store := store.MemStore() // we get tagged on success - res, err := tc.stack.Deliver(ctx, info, store, tc.tx) + res, err := tc.stack.Deliver(ctx, weavetest.BlockInfo(44), store, tc.tx) if tc.err != nil { if !tc.err.Is(err) { t.Fatalf("Unexpected error type returned: %v", err) diff --git a/x/utils/key_tagger_test.go b/x/utils/key_tagger_test.go index 39a3e062..b96a3cfb 100644 --- a/x/utils/key_tagger_test.go +++ b/x/utils/key_tagger_test.go @@ -108,6 +108,7 @@ func TestKeyTagger(t *testing.T) { // try check - no op check := db.CacheWrap() + info := weavetest.BlockInfo(22) _, err := tagger.Check(ctx, info, check, nil, tc.handler) if tc.isError { if err == nil { diff --git a/x/utils/recover_test.go b/x/utils/recover_test.go index 44c248ba..bd5e7a74 100644 --- a/x/utils/recover_test.go +++ b/x/utils/recover_test.go @@ -7,6 +7,7 @@ import ( "github.com/iov-one/weave" "github.com/iov-one/weave/errors" "github.com/iov-one/weave/store" + "github.com/iov-one/weave/weavetest" "github.com/stretchr/testify/assert" ) @@ -18,6 +19,7 @@ func TestRecovery(t *testing.T) { store := store.MemStore() // Panic handler panics. Test the test tool. + info := weavetest.BlockInfo(22) assert.Panics(t, func() { h.Check(ctx, info, store, nil) }) assert.Panics(t, func() { h.Deliver(ctx, info, store, nil) }) diff --git a/x/utils/savepoint_test.go b/x/utils/savepoint_test.go index d729e7cf..5979d908 100644 --- a/x/utils/savepoint_test.go +++ b/x/utils/savepoint_test.go @@ -92,6 +92,7 @@ func TestSavepoint(t *testing.T) { kv.Set(ok, ov) var err error + info := weavetest.BlockInfo(22) if tc.check { _, err = tc.save.Check(ctx, info, kv, nil, tc.handler) } else { @@ -158,11 +159,12 @@ func TestCacheWriteFail(t *testing.T) { decorator := NewSavepoint().OnCheck().OnDeliver() - if _, err := decorator.Check(context.TODO(), db, tx, handler); !myerr.Is(err) { + info := weavetest.BlockInfo(64) + if _, err := decorator.Check(context.TODO(), info, db, tx, handler); !myerr.Is(err) { t.Fatalf("unexpected check result error: %+v", err) } - if _, err := decorator.Deliver(context.TODO(), db, tx, handler); !myerr.Is(err) { + if _, err := decorator.Deliver(context.TODO(), info, db, tx, handler); !myerr.Is(err) { t.Fatalf("unexpected deliver result error: %+v", err) } } From d14aa6a38b050f591a5b2c1d84634e1e8df3f88f Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 20:39:47 +0200 Subject: [PATCH 14/17] Fix all x/... tests except gov --- x/currency/handler_test.go | 5 +++-- x/msgfee/antispam_fee_decorator_test.go | 5 +++-- x/msgfee/fee_decorator_test.go | 5 +++-- x/multisig/decorator_test.go | 3 ++- x/multisig/handlers_test.go | 2 ++ x/paychan/handler_test.go | 19 +++++++++---------- x/sigs/decorator_test.go | 5 +++-- x/sigs/handler_test.go | 4 ++-- x/validators/handler_test.go | 1 + 9 files changed, 28 insertions(+), 21 deletions(-) diff --git a/x/currency/handler_test.go b/x/currency/handler_test.go index 66a232bd..7638d0ae 100644 --- a/x/currency/handler_test.go +++ b/x/currency/handler_test.go @@ -95,13 +95,14 @@ func TestNewTokenInfoHandler(t *testing.T) { auth := &weavetest.Auth{Signers: tc.signers} h := newCreateTokenInfoHandler(auth, tc.issuer) tx := &weavetest.Tx{Msg: tc.msg} - _, err := h.Check(nil, db, tx) + info := weavetest.BlockInfo(8) + _, err := h.Check(nil, info, db, tx) if err != nil { if !tc.wantCheckErr.Is(err) { t.Fatalf("check error: want %v, got %+v", tc.wantCheckErr, err) } } - _, err = h.Deliver(nil, db, tx) + _, err = h.Deliver(nil, info, db, tx) if err != nil { if !tc.wantDeliverErr.Is(err) { t.Fatalf("deliver error: want %v, got %+v", tc.wantDeliverErr, err) diff --git a/x/msgfee/antispam_fee_decorator_test.go b/x/msgfee/antispam_fee_decorator_test.go index 5a830127..806e5ce8 100644 --- a/x/msgfee/antispam_fee_decorator_test.go +++ b/x/msgfee/antispam_fee_decorator_test.go @@ -106,7 +106,8 @@ func TestNewAntispamFeeDecorator(t *testing.T) { tc.Handler.CheckErr = tc.CheckErr } - cres, err := decorator.Check(nil, nil, tc.Tx, tc.Handler) + info := weavetest.BlockInfo(8) + cres, err := decorator.Check(nil, info, nil, tc.Tx, tc.Handler) if !tc.WantCheckErr.Is(err) { t.Fatalf("check returned an unexpected error: %v", err) } @@ -114,7 +115,7 @@ func TestNewAntispamFeeDecorator(t *testing.T) { t.Fatalf("unexpected check fee: %v", cres.RequiredFee) } - if _, err := decorator.Deliver(nil, nil, tc.Tx, tc.Handler); !tc.WantDeliverErr.Is(err) { + if _, err := decorator.Deliver(nil, info, nil, tc.Tx, tc.Handler); !tc.WantDeliverErr.Is(err) { t.Fatalf("deliver returned an unexpected error: %v", err) } diff --git a/x/msgfee/fee_decorator_test.go b/x/msgfee/fee_decorator_test.go index d46d2ab1..39339b71 100644 --- a/x/msgfee/fee_decorator_test.go +++ b/x/msgfee/fee_decorator_test.go @@ -121,7 +121,8 @@ func TestFeeDecorator(t *testing.T) { } } - cres, err := decorator.Check(nil, db, tc.Tx, tc.Handler) + info := weavetest.BlockInfo(8) + cres, err := decorator.Check(nil, info, db, tc.Tx, tc.Handler) if !tc.WantCheckErr.Is(err) { t.Fatalf("check returned an unexpected error: %v", err) } @@ -129,7 +130,7 @@ func TestFeeDecorator(t *testing.T) { t.Fatalf("unexpected check fee: %v", cres.RequiredFee) } - dres, err := decorator.Deliver(nil, db, tc.Tx, tc.Handler) + dres, err := decorator.Deliver(nil, info, db, tc.Tx, tc.Handler) if !tc.WantDeliverErr.Is(err) { t.Fatalf("deliver returned an unexpected error: %v", err) } diff --git a/x/multisig/decorator_test.go b/x/multisig/decorator_test.go index c784bc0e..ef5a4bde 100644 --- a/x/multisig/decorator_test.go +++ b/x/multisig/decorator_test.go @@ -118,9 +118,10 @@ func TestDecorator(t *testing.T) { for testName, tc := range cases { t.Run(testName, func(t *testing.T) { ctx := context.Background() - ctx = weave.WithHeight(ctx, 100) auth := &weavetest.CtxAuth{Key: "authKey"} ctx = auth.SetConditions(ctx, tc.signers...) + info := weavetest.BlockInfo(100) + d := NewDecorator(x.ChainAuth(auth, Authenticate{})) var hn MultisigCheckHandler diff --git a/x/multisig/handlers_test.go b/x/multisig/handlers_test.go index 5784ffa8..a48a5b9b 100644 --- a/x/multisig/handlers_test.go +++ b/x/multisig/handlers_test.go @@ -94,6 +94,7 @@ func TestCreateContractHandler(t *testing.T) { db := store.MemStore() migration.MustInitPkg(db, "multisig") ctx := context.Background() + info := weavetest.BlockInfo(100) tx := &weavetest.Tx{Msg: tc.Msg} cache := db.CacheWrap() @@ -259,6 +260,7 @@ func TestUpdateContractHandler(t *testing.T) { ctx := context.Background() ctx = auth.SetConditions(ctx, tc.Conditions...) + info := weavetest.BlockInfo(100) tx := &weavetest.Tx{Msg: tc.Msg} b := NewContractBucket() diff --git a/x/paychan/handler_test.go b/x/paychan/handler_test.go index e9f4da73..93cc7eca 100644 --- a/x/paychan/handler_test.go +++ b/x/paychan/handler_test.go @@ -517,7 +517,8 @@ func TestPaymentChannelHandlers(t *testing.T) { for i, a := range tc.actions { cache := db.CacheWrap() - if _, err := rt.Check(a.ctx(), cache, a.tx()); !a.wantCheckErr.Is(err) { + ctx, info := a.ctx() + if _, err := rt.Check(ctx, info, cache, a.tx()); !a.wantCheckErr.Is(err) { t.Logf("want: %+v", a.wantCheckErr) t.Logf(" got: %+v", err) t.Fatalf("action %d check (%T)", i, a.msg) @@ -528,7 +529,7 @@ func TestPaymentChannelHandlers(t *testing.T) { continue } - if _, err := rt.Deliver(a.ctx(), db, a.tx()); !a.wantDeliverErr.Is(err) { + if _, err := rt.Deliver(ctx, info, db, a.tx()); !a.wantDeliverErr.Is(err) { t.Logf("want: %+v", a.wantDeliverErr) t.Logf(" got: %+v", err) t.Fatalf("action %d delivery (%T)", i, a.msg) @@ -561,16 +562,14 @@ func (a *action) tx() weave.Tx { return &weavetest.Tx{Msg: a.msg} } -func (a *action) ctx() context.Context { - ctx := weave.WithHeight(context.Background(), a.blocksize) - ctx = weave.WithChainID(ctx, "testchain-123") +func (a *action) ctx() (context.Context, weave.BlockInfo) { + auth := &weavetest.CtxAuth{Key: "auth"} + ctx := auth.SetConditions(context.Background(), a.conditions...) + if !a.blockTime.IsZero() { - ctx = weave.WithBlockTime(ctx, a.blockTime) - } else { - ctx = weave.WithBlockTime(ctx, now) + return ctx, weavetest.BlockInfoWithChain("testchain-123", a.blocksize, a.blockTime) } - auth := &weavetest.CtxAuth{Key: "auth"} - return auth.SetConditions(ctx, a.conditions...) + return ctx, weavetest.BlockInfoWithChain("testchain-123", a.blocksize, now) } // querycheck is a declaration of a query result. For given path and data diff --git a/x/sigs/decorator_test.go b/x/sigs/decorator_test.go index af985ec8..3bfedb33 100644 --- a/x/sigs/decorator_test.go +++ b/x/sigs/decorator_test.go @@ -19,7 +19,8 @@ func TestDecorator(t *testing.T) { signers := new(SigCheckHandler) d := NewDecorator() chainID := "deco-rate" - ctx := weave.WithChainID(context.Background(), chainID) + ctx := context.Background() + info := weavetest.BlockInfoWithChain(chainID, 8) priv := weavetest.NewKey() perms := []weave.Condition{priv.PublicKey().Condition()} @@ -112,7 +113,7 @@ func TestGasPaymentPerSigner(t *testing.T) { ) ctx := context.Background() - ctx = weave.WithChainID(ctx, "mychain") + info := weavetest.BlockInfoWithChain("mychain", 8) db := store.MemStore() migration.MustInitPkg(db, "sigs") diff --git a/x/sigs/handler_test.go b/x/sigs/handler_test.go index 54cf6126..e53b76a9 100644 --- a/x/sigs/handler_test.go +++ b/x/sigs/handler_test.go @@ -145,12 +145,12 @@ func TestBumpSequence(t *testing.T) { tx := weavetest.Tx{Msg: &tc.Msg} cache := db.CacheWrap() - if _, err := handler.Check(ctx, info, cache, &tx); !tc.WantCheckErr.Is(err) { + if _, err := handler.Check(ctx, weavetest.BlockInfo(7), cache, &tx); !tc.WantCheckErr.Is(err) { t.Fatalf("unexpected check error: %+v", err) } cache.Discard() - if _, err := handler.Deliver(ctx, info, db, &tx); !tc.WantDeliverErr.Is(err) { + if _, err := handler.Deliver(ctx, weavetest.BlockInfo(7), db, &tx); !tc.WantDeliverErr.Is(err) { t.Fatalf("unexpected check error: %+v", err) } if tc.WantDeliverErr != nil { diff --git a/x/validators/handler_test.go b/x/validators/handler_test.go index 6cb37dd3..39ca1ca0 100644 --- a/x/validators/handler_test.go +++ b/x/validators/handler_test.go @@ -153,6 +153,7 @@ func TestHandler(t *testing.T) { ValidatorUpdates: spec.Src, }} // when check is called + info := weavetest.BlockInfo(1) if _, err := rt.Check(ctx, info, cache, tx); !spec.ExpCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.ExpCheckErr, err) } From 6f130ab8df77ec309b70498da6cf574584ec2a91 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 21:03:34 +0200 Subject: [PATCH 15/17] Fixed gov tests --- x/gov/handler_test.go | 129 +++++++++++++++++++++--------------------- x/gov/helpers_test.go | 14 ++--- 2 files changed, 71 insertions(+), 72 deletions(-) diff --git a/x/gov/handler_test.go b/x/gov/handler_test.go index 83c0f983..d716c7ab 100644 --- a/x/gov/handler_test.go +++ b/x/gov/handler_test.go @@ -357,7 +357,8 @@ func TestCreateTextProposal(t *testing.T) { migration.MustInitPkg(db, packageName) // given - ctx := weave.WithBlockTime(context.Background(), now.Time()) + ctx := context.Background() + info := weavetest.BlockInfo(100, now.Time()) if spec.Init != nil { spec.Init(t, db) } @@ -403,7 +404,7 @@ func TestDeleteProposal(t *testing.T) { proposalID := weavetest.SequenceID(1) nonExistentProposalID := weavetest.SequenceID(2) specs := map[string]struct { - Mods func(context.Context, *Proposal) // modifies test fixtures before storing + Mods func(weave.BlockInfo, *Proposal) // modifies test fixtures before storing ProposalDeleted bool Msg DeleteProposalMsg SignedBy weave.Condition @@ -414,9 +415,9 @@ func TestDeleteProposal(t *testing.T) { Msg: DeleteProposalMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID}, SignedBy: hAliceCond, ProposalDeleted: true, - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(1 * time.Hour)) - proposal.VotingEndTime = weave.AsUnixTime(time.Now().Add(2 * time.Hour)) + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingStartTime = info.UnixTime().Add(1 * time.Hour) + proposal.VotingEndTime = info.UnixTime().Add(2 * time.Hour) }, }, "Proposal does not exist": { @@ -430,17 +431,17 @@ func TestDeleteProposal(t *testing.T) { SignedBy: hBobbyCond, WantCheckErr: errors.ErrUnauthorized, WantDeliverErr: errors.ErrUnauthorized, - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(1 * time.Hour)) - proposal.VotingEndTime = weave.AsUnixTime(time.Now().Add(2 * time.Hour)) + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingStartTime = info.UnixTime().Add(1 * time.Hour) + proposal.VotingEndTime = info.UnixTime().Add(2 * time.Hour) }, }, "Voting has started": { Msg: DeleteProposalMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID}, SignedBy: hAliceCond, - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingStartTime = weave.AsUnixTime(time.Now().Add(-1 * time.Hour)) - proposal.SubmissionTime = weave.AsUnixTime(time.Now().Add(-2 * time.Hour)) + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingStartTime = info.UnixTime().Add(-1 * time.Hour) + proposal.SubmissionTime = info.UnixTime().Add(-2 * time.Hour) }, WantCheckErr: errors.ErrImmutable, WantDeliverErr: errors.ErrImmutable, @@ -459,8 +460,9 @@ func TestDeleteProposal(t *testing.T) { RegisterRoutes(rt, auth, decodeProposalOptions, nil) // given - ctx := weave.WithBlockTime(context.Background(), time.Now().Round(time.Second)) - pBucket := withTextProposal(t, db, ctx, spec.Mods) + ctx := context.Background() + info := weavetest.BlockInfo(87, time.Now().Round(time.Second)) + pBucket := withTextProposal(t, db, info, spec.Mods) cache := db.CacheWrap() // when check @@ -537,13 +539,14 @@ func TestCreateResolution(t *testing.T) { // when check tx := &weavetest.Tx{Msg: &spec.Msg} - if _, err := rt.Check(spec.ctx, cache, tx); !spec.WantCheckErr.Is(err) { + info := weavetest.BlockInfo(7) + if _, err := rt.Check(spec.ctx, info, db, tx); !spec.WantCheckErr.Is(err) { t.Fatalf("check expected: %+v but got %+v", spec.WantCheckErr, err) } cache.Discard() // and when deliver - if _, err := rt.Deliver(spec.ctx, db, tx); !spec.WantDeliverErr.Is(err) { + if _, err := rt.Deliver(spec.ctx, info, db, tx); !spec.WantDeliverErr.Is(err) { t.Fatalf("deliver expected: %+v but got %+v", spec.WantCheckErr, err) } @@ -570,8 +573,8 @@ func TestVote(t *testing.T) { nonElectorCond := weavetest.NewCondition() nonElector := nonElectorCond.Address() specs := map[string]struct { - Init func(ctx context.Context, info weave.BlockInfo, db store.KVStore) // executed before test fixtures - Mods func(context.Context, *Proposal) // modifies test fixtures before storing + Init func(info weave.BlockInfo, db store.KVStore) // executed before test fixtures + Mods func(weave.BlockInfo, *Proposal) // modifies test fixtures before storing Msg VoteMsg SignedBy weave.Condition WantCheckErr *errors.Error @@ -610,7 +613,7 @@ func TestVote(t *testing.T) { ExpVotedBy: hAlice, }, "Can change vote": { - Init: func(ctx context.Context, info weave.BlockInfo, db store.KVStore) { + Init: func(info weave.BlockInfo, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -621,7 +624,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { + Mods: func(info weave.BlockInfo, proposal *Proposal) { proposal.VoteState.TotalYes = 10 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_No, Voter: hBobby}, @@ -630,7 +633,7 @@ func TestVote(t *testing.T) { ExpVotedBy: hBobby, }, "Can resubmit vote": { - Init: func(ctx context.Context, info weave.BlockInfo, db store.KVStore) { + Init: func(info weave.BlockInfo, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -641,7 +644,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { + Mods: func(info weave.BlockInfo, proposal *Proposal) { proposal.VoteState.TotalYes = 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -668,8 +671,8 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrUnauthorized, }, "Vote before start date": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingStartTime = unixBlockTime(t, ctx) + 1 + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingStartTime = info.UnixTime() + 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, SignedBy: hAliceCond, @@ -677,8 +680,8 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on start date": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingStartTime = unixBlockTime(t, ctx) + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingStartTime = info.UnixTime() }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, SignedBy: hAliceCond, @@ -686,8 +689,8 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on end date": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingEndTime = unixBlockTime(t, ctx) + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingEndTime = info.UnixTime() }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, SignedBy: hAliceCond, @@ -695,8 +698,8 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote after end date": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { - proposal.VotingEndTime = unixBlockTime(t, ctx) - 1 + Mods: func(info weave.BlockInfo, proposal *Proposal) { + proposal.VotingEndTime = info.UnixTime() - 1 }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, SignedBy: hAliceCond, @@ -704,7 +707,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on withdrawn proposal must fail": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { + Mods: func(info weave.BlockInfo, proposal *Proposal) { proposal.Status = Proposal_Withdrawn }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -713,7 +716,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Vote on closed proposal must fail": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { + Mods: func(info weave.BlockInfo, proposal *Proposal) { proposal.Status = Proposal_Closed }, Msg: VoteMsg{Metadata: &weave.Metadata{Schema: 1}, ProposalID: proposalID, Selected: VoteOption_Yes, Voter: hAlice}, @@ -722,7 +725,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrState, }, "Sanity check on count vote": { - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { + Mods: func(info weave.BlockInfo, proposal *Proposal) { // not a valid setup proposal.VoteState.TotalYes = math.MaxUint64 proposal.VoteState.TotalElectorateWeight = math.MaxUint64 @@ -732,7 +735,7 @@ func TestVote(t *testing.T) { WantDeliverErr: errors.ErrHuman, }, "Sanity check on undo count vote": { - Init: func(ctx context.Context, info weave.BlockInfo, db store.KVStore) { + Init: func(info weave.BlockInfo, db store.KVStore) { vBucket := NewVoteBucket() obj := vBucket.Build(db, proposalID, Vote{ @@ -743,7 +746,7 @@ func TestVote(t *testing.T) { ) vBucket.Save(db, obj) }, - Mods: func(ctx context.Context, info weave.BlockInfo, proposal *Proposal) { + Mods: func(info weave.BlockInfo, proposal *Proposal) { // not a valid setup proposal.VoteState.TotalYes = 0 proposal.VoteState.TotalElectorateWeight = math.MaxUint64 @@ -766,11 +769,12 @@ func TestVote(t *testing.T) { RegisterRoutes(rt, auth, decodeProposalOptions, nil) // given - ctx := weave.WithBlockTime(context.Background(), time.Now().Round(time.Second)) + ctx := context.Background() + info := weavetest.BlockInfo(55, time.Now().Round(time.Second)) if spec.Init != nil { - spec.Init(ctx, db) + spec.Init(info, db) } - pBucket := withTextProposal(t, db, ctx, spec.Mods) + pBucket := withTextProposal(t, db, info, spec.Mods) cache := db.CacheWrap() // when check @@ -817,7 +821,7 @@ func TestTally(t *testing.T) { yes, no, abstain uint64 } specs := map[string]struct { - Mods func(context.Context, *Proposal) + Mods func(weave.BlockInfo, *Proposal) Src tallySetup WantCheckErr *errors.Error WantDeliverErr *errors.Error @@ -1089,9 +1093,9 @@ func TestTally(t *testing.T) { t.Fatalf("unexpected error: %+v", err) } }, - Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { + Mods: func(info weave.BlockInfo, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 10}) - p.VotingEndTime = unixBlockTime(t, ctx) - 1 + p.VotingEndTime = info.UnixTime() - 1 }, Src: tallySetup{ yes: 10, @@ -1142,9 +1146,9 @@ func TestTally(t *testing.T) { t.Fatalf("unexpected error: %+v", err) } }, - Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { + Mods: func(info weave.BlockInfo, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 0}) - p.VotingEndTime = unixBlockTime(t, ctx) - 1 + p.VotingEndTime = info.UnixTime() - 1 }, Src: tallySetup{ yes: 10, @@ -1158,9 +1162,9 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal accepted: execution error:", }, "Does not update an electorate when rejected": { - Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { + Mods: func(info weave.BlockInfo, p *Proposal) { p.RawOption = genElectorateOptions(t, Elector{hAlice, 10}) - p.VotingEndTime = unixBlockTime(t, ctx) - 1 + p.VotingEndTime = info.UnixTime() - 1 }, Src: tallySetup{ yes: 1, @@ -1189,7 +1193,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on second tally": { - Mods: func(_ context.Context, p *Proposal) { + Mods: func(_ weave.BlockInfo, p *Proposal) { p.Status = Proposal_Closed }, Src: tallySetup{ @@ -1203,8 +1207,8 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on tally before end date": { - Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { - p.VotingEndTime = unixBlockTime(t, ctx) + 1 + Mods: func(info weave.BlockInfo, p *Proposal) { + p.VotingEndTime = info.UnixTime() + 1 }, Src: tallySetup{ threshold: Fraction{Numerator: 1, Denominator: 2}, @@ -1216,8 +1220,8 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on tally at end date": { - Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { - p.VotingEndTime = unixBlockTime(t, ctx) + Mods: func(info weave.BlockInfo, p *Proposal) { + p.VotingEndTime = info.UnixTime() }, Src: tallySetup{ threshold: Fraction{Numerator: 1, Denominator: 2}, @@ -1229,7 +1233,7 @@ func TestTally(t *testing.T) { WantDeliverLog: "Proposal not accepted", }, "Fails on withdrawn proposal": { - Mods: func(ctx context.Context, info weave.BlockInfo, p *Proposal) { + Mods: func(info weave.BlockInfo, p *Proposal) { p.Status = Proposal_Withdrawn }, Src: tallySetup{ @@ -1254,15 +1258,16 @@ func TestTally(t *testing.T) { migration.MustInitPkg(db, packageName) // given - ctx := weave.WithBlockTime(context.Background(), time.Now().Round(time.Second)) - setupForTally := func(_ context.Context, p *Proposal) { + ctx := context.Background() + info := weavetest.BlockInfo(8, time.Now().Round(time.Second)) + setupForTally := func(info weave.BlockInfo, p *Proposal) { p.VoteState = NewTallyResult(spec.Src.quorum, spec.Src.threshold, spec.Src.totalWeightElectorate) p.VoteState.TotalYes = spec.Src.yes p.VoteState.TotalNo = spec.Src.no p.VoteState.TotalAbstain = spec.Src.abstain - p.VotingEndTime = unixBlockTime(t, ctx) - 1 + p.VotingEndTime = info.UnixTime() - 1 } - pBucket := withTextProposal(t, db, ctx, append([]ctxAwareMutator{setupForTally}, spec.Mods)...) + pBucket := withTextProposal(t, db, info, append([]blockAwareMutator{setupForTally}, spec.Mods)...) if spec.Init != nil { spec.Init(t, db) } @@ -1320,8 +1325,8 @@ func TestUpdateElectorate(t *testing.T) { WantCheckErr *errors.Error WantDeliverErr *errors.Error ExpModel *Electorate - WithProposal bool // enables the usage of mods to create a proposal - Mods ctxAwareMutator // modifies TextProposal test fixtures before storing + WithProposal bool // enables the usage of mods to create a proposal + Mods blockAwareMutator // modifies TextProposal test fixtures before storing }{ "All good with update by owner": { Msg: UpdateElectorateMsg{ @@ -1421,6 +1426,7 @@ func TestUpdateElectorate(t *testing.T) { }, } bucket := NewElectorateBucket() + info := weavetest.BlockInfo(5) for msg, spec := range specs { t.Run(msg, func(t *testing.T) { auth := &weavetest.Auth{ @@ -1433,7 +1439,7 @@ func TestUpdateElectorate(t *testing.T) { withElectorate(t, db) if spec.WithProposal { - withTextProposal(t, db, nil, spec.Mods) + withTextProposal(t, db, weavetest.BlockInfo(7), spec.Mods) } cache := db.CacheWrap() @@ -1559,6 +1565,7 @@ func TestUpdateElectionRules(t *testing.T) { }, } bucket := NewElectionRulesBucket() + info := weavetest.BlockInfo(5) for msg, spec := range specs { t.Run(msg, func(t *testing.T) { auth := &weavetest.Auth{ @@ -1600,11 +1607,3 @@ func TestUpdateElectionRules(t *testing.T) { }) } } - -func unixBlockTime(t testing.TB, ctx context.Context) weave.UnixTime { - now, err := weave.BlockTime(ctx) - if err != nil { - t.Fatal(err) - } - return weave.AsUnixTime(now) -} diff --git a/x/gov/helpers_test.go b/x/gov/helpers_test.go index c4e5e163..f506e200 100644 --- a/x/gov/helpers_test.go +++ b/x/gov/helpers_test.go @@ -11,10 +11,10 @@ import ( "github.com/iov-one/weave/weavetest/assert" ) -// ctxAwareMutator is a call back interface to modify the passed proposal for test setup -type ctxAwareMutator func(context.Context, *Proposal) +// blockAwareMutator is a call back interface to modify the passed proposal for test setup +type blockAwareMutator func(weave.BlockInfo, *Proposal) -func withTextProposal(t *testing.T, db store.KVStore, ctx context.Context, info weave.BlockInfo, mods ...ctxAwareMutator) *ProposalBucket { +func withTextProposal(t *testing.T, db store.KVStore, info weave.BlockInfo, mods ...blockAwareMutator) *ProposalBucket { t.Helper() // setup electorate withElectorate(t, db) @@ -22,18 +22,18 @@ func withTextProposal(t *testing.T, db store.KVStore, ctx context.Context, info withElectionRule(t, db) // adapter to call fixture mutator with context - ctxMods := make([]func(*Proposal), len(mods)) + blockMods := make([]func(*Proposal), len(mods)) for i := 0; i < len(mods); i++ { j := i - ctxMods[j] = func(p *Proposal) { + blockMods[j] = func(p *Proposal) { if mods[j] == nil { return } - mods[j](ctx, p) + mods[j](info, p) } } pBucket := NewProposalBucket() - proposal := proposalFixture(t, hAlice, ctxMods...) + proposal := proposalFixture(t, hAlice, blockMods...) if _, err := pBucket.Create(db, &proposal); err != nil { t.Fatalf("unexpected error: %+v", err) From 84c2ff1b6ccfceda9e30bdf397e217f81d4d57ee Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 21:20:59 +0200 Subject: [PATCH 16/17] Fix app startup logic (no chain id) --- app/chain_test.go | 9 +++++---- app/router_test.go | 10 ++++++---- app/store.go | 16 +++++++++++----- app/store_test.go | 3 +-- cmd/bnsd/x/username/handlers_test.go | 15 +++++++++------ 5 files changed, 32 insertions(+), 21 deletions(-) diff --git a/app/chain_test.go b/app/chain_test.go index 7a18d592..e4b8ba32 100644 --- a/app/chain_test.go +++ b/app/chain_test.go @@ -29,7 +29,8 @@ func TestChain(t *testing.T) { ).WithHandler(h) // This height must not panic. - ctx := weave.WithHeight(context.Background(), panicHeight-2) + ctx := context.Background() + info := weavetest.BlockInfo(panicHeight - 2) _, err := stack.Check(ctx, info, nil, nil) assert.NoError(t, err) @@ -46,7 +47,7 @@ func TestChain(t *testing.T) { assert.Equal(t, 1, h.DeliverCallCount()) // Trigger a panic. - ctx = weave.WithHeight(context.Background(), panicHeight+2) + info = weavetest.BlockInfo(panicHeight + 2) _, err = stack.Check(ctx, info, nil, nil) assert.Error(t, err) @@ -68,14 +69,14 @@ type panicAtHeightDecorator int64 var _ weave.Decorator = panicAtHeightDecorator(0) func (ph panicAtHeightDecorator) Check(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Checker) (*weave.CheckResult, error) { - if val, _ := weave.GetHeight(ctx); val >= int64(ph) { + if info.Height() >= int64(ph) { panic("too high") } return next.Check(ctx, info, db, tx) } func (ph panicAtHeightDecorator) Deliver(ctx context.Context, info weave.BlockInfo, db weave.KVStore, tx weave.Tx, next weave.Deliverer) (*weave.DeliverResult, error) { - if val, _ := weave.GetHeight(ctx); val >= int64(ph) { + if info.Height() >= int64(ph) { panic("too high") } return next.Deliver(ctx, info, db, tx) diff --git a/app/router_test.go b/app/router_test.go index 46083040..3d12b8fb 100644 --- a/app/router_test.go +++ b/app/router_test.go @@ -18,11 +18,12 @@ func TestRouterSuccess(t *testing.T) { ) r.Handle(msg, handler) + info := weavetest.BlockInfo(44) - if _, err := r.Check(context.TODO(), nil, &weavetest.Tx{Msg: msg}); err != nil { + if _, err := r.Check(context.TODO(), info, nil, &weavetest.Tx{Msg: msg}); err != nil { t.Fatalf("check failed: %s", err) } - if _, err := r.Deliver(context.TODO(), nil, &weavetest.Tx{Msg: msg}); err != nil { + if _, err := r.Deliver(context.TODO(), info, nil, &weavetest.Tx{Msg: msg}); err != nil { t.Fatalf("delivery failed: %s", err) } assert.Equal(t, 2, handler.CallCount()) @@ -32,11 +33,12 @@ func TestRouterNoHandler(t *testing.T) { r := NewRouter() tx := &weavetest.Tx{Msg: &weavetest.Msg{RoutePath: "test/secret"}} + info := weavetest.BlockInfo(44) - if _, err := r.Check(context.TODO(), nil, tx); !errors.ErrNotFound.Is(err) { + if _, err := r.Check(context.TODO(), info, nil, tx); !errors.ErrNotFound.Is(err) { t.Fatalf("expected not found error, got %s", err) } - if _, err := r.Deliver(context.TODO(), nil, tx); !errors.ErrNotFound.Is(err) { + if _, err := r.Deliver(context.TODO(), info, nil, tx); !errors.ErrNotFound.Is(err) { t.Fatalf("expected not found error, got %s", err) } } diff --git a/app/store.go b/app/store.go index 01d64e0c..78f65aa9 100644 --- a/app/store.go +++ b/app/store.go @@ -70,9 +70,12 @@ func NewStoreApp(name string, store weave.CommitKVStore, queryRouter weave.Query if err != nil { panic(err) } - s.blockInfo, err = weave.NewBlockInfo(abci.Header{Height: info.Version}, weave.CommitInfo{}, s.chainID, s.logger) - if err != nil { - panic(err) + // on first startup, we have no chainID to add, we will set blockInfo in Init + if s.chainID != "" { + s.blockInfo, err = weave.NewBlockInfo(abci.Header{Height: info.Version}, weave.CommitInfo{}, s.chainID, s.logger) + if s.chainID != "" && err != nil { + panic(err) + } } return s } @@ -296,8 +299,6 @@ func (s *StoreApp) Commit() (res abci.ResponseCommit) { } // InitChain implements ABCI -// Note: in tendermint 0.17, the genesis file is passed -// in here, we should use this to trigger reading the genesis now // TODO: investigate validators and consensusParams in response func (s *StoreApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitChain) { err := s.parseAppState(req.AppStateBytes, weave.FromInitChain(req), req.ChainId, s.initializer) @@ -305,6 +306,11 @@ func (s *StoreApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitCh // Read comment on type header panic(err) } + s.blockInfo, err = weave.NewBlockInfo(abci.Header{Height: 0}, weave.CommitInfo{}, s.chainID, s.logger) + if err != nil { + // Read comment on type header + panic(err) + } return abci.ResponseInitChain{} } diff --git a/app/store_test.go b/app/store_test.go index 04b8fc1a..b3d685c7 100644 --- a/app/store_test.go +++ b/app/store_test.go @@ -1,7 +1,6 @@ package app import ( - "context" "testing" "github.com/iov-one/weave" @@ -19,7 +18,7 @@ func TestAddValChange(t *testing.T) { Type: "test", Data: []byte("someKey2"), } - app := NewStoreApp("dummy", iavl.MockCommitStore(), weave.NewQueryRouter(), context.Background()) + app := NewStoreApp("dummy", iavl.MockCommitStore(), weave.NewQueryRouter()) t.Run("Diff is equal to output with one update", func(t *testing.T) { diff := []weave.ValidatorUpdate{ diff --git a/cmd/bnsd/x/username/handlers_test.go b/cmd/bnsd/x/username/handlers_test.go index 3c91d317..a65245cd 100644 --- a/cmd/bnsd/x/username/handlers_test.go +++ b/cmd/bnsd/x/username/handlers_test.go @@ -83,6 +83,7 @@ func TestRegisterTokenHandler(t *testing.T) { for testName, tc := range cases { t.Run(testName, func(t *testing.T) { db := store.MemStore() + info := weavetest.BlockInfo(65) migration.MustInitPkg(db, "username") b := NewTokenBucket() @@ -101,11 +102,11 @@ func TestRegisterTokenHandler(t *testing.T) { } cache := db.CacheWrap() - if _, err := h.Check(context.TODO(), cache, tc.Tx); !tc.WantCheckErr.Is(err) { + if _, err := h.Check(context.TODO(), info, cache, tc.Tx); !tc.WantCheckErr.Is(err) { t.Fatalf("unexpected check error: %s", err) } cache.Discard() - if _, err := h.Deliver(context.TODO(), db, tc.Tx); !tc.WantDeliverErr.Is(err) { + if _, err := h.Deliver(context.TODO(), info, db, tc.Tx); !tc.WantDeliverErr.Is(err) { t.Fatalf("unexpected deliver error: %s", err) } }) @@ -172,6 +173,7 @@ func TestChangeTokenOwnerHandler(t *testing.T) { for testName, tc := range cases { t.Run(testName, func(t *testing.T) { db := store.MemStore() + info := weavetest.BlockInfo(42) migration.MustInitPkg(db, "username") b := NewTokenBucket() @@ -190,11 +192,11 @@ func TestChangeTokenOwnerHandler(t *testing.T) { } cache := db.CacheWrap() - if _, err := h.Check(context.TODO(), cache, tc.Tx); !tc.WantCheckErr.Is(err) { + if _, err := h.Check(context.TODO(), info, cache, tc.Tx); !tc.WantCheckErr.Is(err) { t.Fatalf("unexpected check error: %s", err) } cache.Discard() - if _, err := h.Deliver(context.TODO(), db, tc.Tx); !tc.WantDeliverErr.Is(err) { + if _, err := h.Deliver(context.TODO(), info, db, tc.Tx); !tc.WantDeliverErr.Is(err) { t.Fatalf("unexpected deliver error: %s", err) } }) @@ -281,6 +283,7 @@ func TestChangeTokenTargetHandler(t *testing.T) { for testName, tc := range cases { t.Run(testName, func(t *testing.T) { db := store.MemStore() + info := weavetest.BlockInfo(5) migration.MustInitPkg(db, "username") b := NewTokenBucket() @@ -299,11 +302,11 @@ func TestChangeTokenTargetHandler(t *testing.T) { } cache := db.CacheWrap() - if _, err := h.Check(context.TODO(), cache, tc.Tx); !tc.WantCheckErr.Is(err) { + if _, err := h.Check(context.TODO(), info, cache, tc.Tx); !tc.WantCheckErr.Is(err) { t.Fatalf("unexpected check error: %s", err) } cache.Discard() - if _, err := h.Deliver(context.TODO(), db, tc.Tx); !tc.WantDeliverErr.Is(err) { + if _, err := h.Deliver(context.TODO(), info, db, tc.Tx); !tc.WantDeliverErr.Is(err) { t.Fatalf("unexpected deliver error: %s", err) } }) From b43cd6b0a665959a86c10282e477916c9a50012d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 27 Jun 2019 21:21:07 +0200 Subject: [PATCH 17/17] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5371edee..62c79e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Breaking changes - Unify all message paths to follow pattern `/` - `app.Router` interface was changed. Handler registration requires a message and not message path. +- Pull out BlockInfo from weave.Context, add additional argument to all Handlers + and Decorators: `Deliver(context.Context, weave.BlockInfo, weave.KVStore, weave.Tx)` ## 0.17.0