Move ChainID into context

This commit is contained in:
Ethan Frey
2017-07-03 17:32:01 +02:00
parent ef0ab758ed
commit 159574db89
16 changed files with 170 additions and 124 deletions
+5 -7
View File
@@ -12,9 +12,7 @@ const (
)
// Chain enforces that this tx was bound to the named chain
type Chain struct {
ChainID string
}
type Chain struct{}
func (_ Chain) Name() string {
return NameRecovery
@@ -23,7 +21,7 @@ func (_ Chain) Name() string {
var _ Middleware = Chain{}
func (c Chain) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
stx, err := c.checkChain(tx)
stx, err := c.checkChain(ctx.ChainID(), tx)
if err != nil {
return res, err
}
@@ -31,7 +29,7 @@ func (c Chain) CheckTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx
}
func (c Chain) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
stx, err := c.checkChain(tx)
stx, err := c.checkChain(ctx.ChainID(), tx)
if err != nil {
return res, err
}
@@ -39,12 +37,12 @@ func (c Chain) DeliverTx(ctx basecoin.Context, store types.KVStore, tx basecoin.
}
// checkChain makes sure the tx is a txs.Chain and
func (c Chain) checkChain(tx basecoin.Tx) (basecoin.Tx, error) {
func (c Chain) checkChain(chainID string, tx basecoin.Tx) (basecoin.Tx, error) {
ctx, ok := tx.Unwrap().(*txs.Chain)
if !ok {
return tx, errors.ErrNoChain()
}
if ctx.ChainID != c.ChainID {
if ctx.ChainID != chainID {
return tx, errors.ErrWrongChain(ctx.ChainID)
}
return ctx.Tx, nil
+2 -2
View File
@@ -30,12 +30,12 @@ func TestChain(t *testing.T) {
}
// generic args here...
ctx := NewContext(log.NewNopLogger())
ctx := NewContext(chainID, log.NewNopLogger())
store := types.NewMemKVStore()
// build the stack
ok := OKHandler{msg}
app := New(Chain{chainID}).Use(ok)
app := New(Chain{}).Use(ok)
for idx, tc := range cases {
i := strconv.Itoa(idx)
+10 -1
View File
@@ -17,20 +17,26 @@ type nonce int64
type secureContext struct {
id nonce
chain string
app string
perms []basecoin.Actor
log.Logger
}
func NewContext(logger log.Logger) basecoin.Context {
func NewContext(chain string, logger log.Logger) basecoin.Context {
return secureContext{
id: nonce(rand.Int63()),
chain: chain,
Logger: logger,
}
}
var _ basecoin.Context = secureContext{}
func (c secureContext) ChainID() string {
return c.chain
}
// WithPermissions will panic if they try to set permission without the proper app
func (c secureContext) WithPermissions(perms ...basecoin.Actor) basecoin.Context {
// the guard makes sure you only set permissions for the app you are inside
@@ -44,6 +50,7 @@ func (c secureContext) WithPermissions(perms ...basecoin.Actor) basecoin.Context
return secureContext{
id: c.id,
chain: c.chain,
app: c.app,
perms: append(c.perms, perms...),
Logger: c.Logger,
@@ -73,6 +80,7 @@ func (c secureContext) IsParent(other basecoin.Context) bool {
func (c secureContext) Reset() basecoin.Context {
return secureContext{
id: c.id,
chain: c.chain,
app: c.app,
perms: nil,
Logger: c.Logger,
@@ -88,6 +96,7 @@ func withApp(ctx basecoin.Context, app string) basecoin.Context {
}
return secureContext{
id: sc.id,
chain: sc.chain,
app: app,
perms: sc.perms,
Logger: sc.Logger,
+3 -3
View File
@@ -15,7 +15,7 @@ import (
func TestOK(t *testing.T) {
assert := assert.New(t)
ctx := NewContext(log.NewNopLogger())
ctx := NewContext("test-chain", log.NewNopLogger())
store := types.NewMemKVStore()
data := "this looks okay"
tx := basecoin.Tx{}
@@ -33,7 +33,7 @@ func TestOK(t *testing.T) {
func TestFail(t *testing.T) {
assert := assert.New(t)
ctx := NewContext(log.NewNopLogger())
ctx := NewContext("test-chain", log.NewNopLogger())
store := types.NewMemKVStore()
msg := "big problem"
tx := basecoin.Tx{}
@@ -53,7 +53,7 @@ func TestFail(t *testing.T) {
func TestPanic(t *testing.T) {
assert := assert.New(t)
ctx := NewContext(log.NewNopLogger())
ctx := NewContext("test-chain", log.NewNopLogger())
store := types.NewMemKVStore()
msg := "system crash!"
tx := basecoin.Tx{}
+2 -2
View File
@@ -57,12 +57,12 @@ func New(middlewares ...Middleware) *Stack {
// NewDefault sets up the common middlewares before your custom stack.
//
// This is logger, recovery, signature, and chain
func NewDefault(chainID string, middlewares ...Middleware) *Stack {
func NewDefault(middlewares ...Middleware) *Stack {
mids := []Middleware{
Logger{},
Recovery{},
Signatures{},
Chain{chainID},
Chain{},
}
mids = append(mids, middlewares...)
return New(mids...)
+1 -1
View File
@@ -17,7 +17,7 @@ func TestPermissionSandbox(t *testing.T) {
require := require.New(t)
// generic args
ctx := NewContext(log.NewNopLogger())
ctx := NewContext("test-chain", log.NewNopLogger())
store := types.NewMemKVStore()
raw := txs.NewRaw([]byte{1, 2, 3, 4})
rawBytes, err := data.ToWire(raw)
+7 -1
View File
@@ -10,17 +10,23 @@ import (
type mockContext struct {
perms []basecoin.Actor
chain string
log.Logger
}
func MockContext() basecoin.Context {
func MockContext(chain string) basecoin.Context {
return mockContext{
chain: chain,
Logger: log.NewNopLogger(),
}
}
var _ basecoin.Context = mockContext{}
func (c mockContext) ChainID() string {
return c.chain
}
// WithPermissions will panic if they try to set permission without the proper app
func (c mockContext) WithPermissions(perms ...basecoin.Actor) basecoin.Context {
return mockContext{
+1 -1
View File
@@ -15,7 +15,7 @@ func TestRecovery(t *testing.T) {
assert := assert.New(t)
// generic args here...
ctx := NewContext(log.NewNopLogger())
ctx := NewContext("test-chain", log.NewNopLogger())
store := types.NewMemKVStore()
tx := basecoin.Tx{}
+1 -1
View File
@@ -17,7 +17,7 @@ func TestSignatureChecks(t *testing.T) {
assert := assert.New(t)
// generic args
ctx := NewContext(log.NewNopLogger())
ctx := NewContext("test-chain", log.NewNopLogger())
store := types.NewMemKVStore()
raw := txs.NewRaw([]byte{1, 2, 3, 4})