Test create-role and fix CheckTx
This commit is contained in:
parent
bb61b9fca3
commit
7e7f124bc9
|
@ -28,8 +28,13 @@ func (Handler) Name() string {
|
||||||
|
|
||||||
// CheckTx verifies if the transaction is properly formated
|
// CheckTx verifies if the transaction is properly formated
|
||||||
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
|
func (h Handler) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx) (res basecoin.Result, err error) {
|
||||||
_, err = checkTx(ctx, tx)
|
var cr CreateRoleTx
|
||||||
return res, err
|
cr, err = checkTx(ctx, tx)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = checkNoRole(store, cr.Role)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeliverTx tries to create a new role.
|
// DeliverTx tries to create a new role.
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package roles_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tendermint/basecoin"
|
||||||
|
"github.com/tendermint/basecoin/modules/roles"
|
||||||
|
"github.com/tendermint/basecoin/stack"
|
||||||
|
"github.com/tendermint/basecoin/state"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateRole(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
a := basecoin.Actor{App: "foo", Address: []byte("bar")}
|
||||||
|
b := basecoin.Actor{ChainID: "eth", App: "foo", Address: []byte("bar")}
|
||||||
|
c := basecoin.Actor{App: "foo", Address: []byte("baz")}
|
||||||
|
d := basecoin.Actor{App: "si-ly", Address: []byte("bar")}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
valid bool
|
||||||
|
role string
|
||||||
|
min uint32
|
||||||
|
sigs []basecoin.Actor
|
||||||
|
}{
|
||||||
|
{true, "awesome", 1, []basecoin.Actor{a}},
|
||||||
|
{true, "cool", 2, []basecoin.Actor{b, c, d}},
|
||||||
|
{false, "oops", 3, []basecoin.Actor{a, d}}, // too many
|
||||||
|
{false, "ugh", 0, []basecoin.Actor{a, d}}, // too few
|
||||||
|
{false, "phew", 1, []basecoin.Actor{}}, // none
|
||||||
|
{false, "cool", 1, []basecoin.Actor{c, d}}, // duplicate of existing one
|
||||||
|
}
|
||||||
|
|
||||||
|
h := roles.NewHandler()
|
||||||
|
ctx := stack.MockContext("role-chain", 123)
|
||||||
|
store := state.NewMemKVStore()
|
||||||
|
for i, tc := range cases {
|
||||||
|
tx := roles.NewCreateRoleTx([]byte(tc.role), tc.min, tc.sigs)
|
||||||
|
_, err := h.CheckTx(ctx, store, tx)
|
||||||
|
_, err2 := h.DeliverTx(ctx, store, tx)
|
||||||
|
if tc.valid {
|
||||||
|
assert.Nil(err, "%d/%s: %+v", i, tc.role, err)
|
||||||
|
assert.Nil(err2, "%d/%s: %+v", i, tc.role, err2)
|
||||||
|
} else {
|
||||||
|
assert.NotNil(err, "%d/%s", i, tc.role)
|
||||||
|
assert.NotNil(err2, "%d/%s", i, tc.role)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -68,11 +68,18 @@ func loadRole(store state.KVStore, key []byte) (role Role, err error) {
|
||||||
return role, nil
|
return role, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// we only have create here, no update, since we don't allow update yet
|
func checkNoRole(store state.KVStore, key []byte) error {
|
||||||
func createRole(store state.KVStore, key []byte, role Role) error {
|
|
||||||
if _, err := loadRole(store, key); !IsNoRoleErr(err) {
|
if _, err := loadRole(store, key); !IsNoRoleErr(err) {
|
||||||
return ErrRoleExists()
|
return ErrRoleExists()
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// we only have create here, no update, since we don't allow update yet
|
||||||
|
func createRole(store state.KVStore, key []byte, role Role) error {
|
||||||
|
if err := checkNoRole(store, key); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
bin := wire.BinaryBytes(role)
|
bin := wire.BinaryBytes(role)
|
||||||
store.Set(key, bin)
|
store.Set(key, bin)
|
||||||
return nil // real stores can return error...
|
return nil // real stores can return error...
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"github.com/tendermint/basecoin/errors"
|
"github.com/tendermint/basecoin/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
// MaxMembers it the maximum number of members in a Role. Used to avoid
|
// MaxMembers it the maximum number of members in a Role. Used to avoid
|
||||||
// extremely large roles.
|
// extremely large roles.
|
||||||
// Value is arbitrary, please adjust as needed
|
// Value is arbitrary, please adjust as needed
|
||||||
|
|
Loading…
Reference in New Issue