[#174] subnet: Add UserAllowed method

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-11-23 16:19:58 +03:00 committed by LeL
parent a7a272ad08
commit 9e2842b4ad
2 changed files with 45 additions and 0 deletions

View file

@ -442,6 +442,32 @@ func RemoveUser(subnetID []byte, groupID []byte, userID []byte) {
deleteKeyFromList(ctx, userID, stKey) deleteKeyFromList(ctx, userID, stKey)
} }
const groupIDSize = 4
// UserAllowed returns bool that indicates if node is included in the
// specified subnet or not.
func UserAllowed(subnetID []byte, user []byte) bool {
ctx := storage.GetContext()
stKey := append([]byte{ownerPrefix}, subnetID...)
if storage.Get(ctx, stKey) == nil {
panic("userAllowed: " + ErrSubNotExist)
}
stKey[0] = userPrefix
prefixLen := len(stKey) + groupIDSize
iter := storage.Find(ctx, stKey, storage.KeysOnly)
for iterator.Next(iter) {
key := iterator.Value(iter).([]byte)
if common.BytesEqual(user, key[prefixLen:]) {
return true
}
}
return false
}
// Version returns version of the contract. // Version returns version of the contract.
func Version() int { func Version() int {
return common.Version return common.Version

View file

@ -285,6 +285,25 @@ func TestSubnet_RemoveUser(t *testing.T) {
cAdm.InvokeFail(t, method+errSeparator+subnet.ErrUserNotExist, method, id, groupId, user) cAdm.InvokeFail(t, method+errSeparator+subnet.ErrUserNotExist, method, id, groupId, user)
} }
func TestSubnet_UserAllowed(t *testing.T) {
e := newSubnetInvoker(t)
id, owner := createSubnet(t, e)
groupId := randomBytes(4)
user := randomBytes(27)
const method = "userAllowed"
cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrSubNotExist, method, []byte{0, 0, 0, 0}, user)
cOwn.Invoke(t, stackitem.NewBool(false), method, id, user)
cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user)
cOwn.Invoke(t, stackitem.NewBool(true), method, id, user)
}
func createSubnet(t *testing.T, e *neotest.ContractInvoker) (id []byte, owner neotest.Signer) { func createSubnet(t *testing.T, e *neotest.ContractInvoker) (id []byte, owner neotest.Signer) {
var ( var (
ok bool ok bool