[#174] subnet: Make len(subnetID) constant and fix its value

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-11-24 13:03:02 +03:00 committed by LeL
parent c4ff617525
commit c0636ff0ee
2 changed files with 30 additions and 17 deletions

View file

@ -37,7 +37,9 @@ const (
ErrAccessDenied = "access denied" ErrAccessDenied = "access denied"
errCheckWitnessFailed = "owner witness check failed" errCheckWitnessFailed = "owner witness check failed"
)
const (
nodeAdminPrefix = 'a' nodeAdminPrefix = 'a'
infoPrefix = 'i' infoPrefix = 'i'
clientAdminPrefix = 'm' clientAdminPrefix = 'm'
@ -49,6 +51,8 @@ const (
const ( const (
userIDSize = 27 userIDSize = 27
subnetIDSize = 5
groupIDSize = 5
) )
// _deploy function sets up initial list of inner ring public keys. // _deploy function sets up initial list of inner ring public keys.
@ -78,7 +82,8 @@ func Update(script []byte, manifest []byte, data interface{}) {
// Put creates new subnet with the specified owner and info. // Put creates new subnet with the specified owner and info.
func Put(id []byte, ownerKey interop.PublicKey, info []byte) { func Put(id []byte, ownerKey interop.PublicKey, info []byte) {
if len(id) != 4 { // V2 format check
if len(id) != subnetIDSize {
panic("put: " + ErrInvalidSubnetID) panic("put: " + ErrInvalidSubnetID)
} }
if len(ownerKey) != interop.PublicKeyCompressedLen { if len(ownerKey) != interop.PublicKeyCompressedLen {
@ -442,8 +447,6 @@ 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 // UserAllowed returns bool that indicates if node is included in the
// specified subnet or not. // specified subnet or not.
func UserAllowed(subnetID []byte, user []byte) bool { func UserAllowed(subnetID []byte, user []byte) bool {

View file

@ -45,7 +45,7 @@ func TestSubnet_Put(t *testing.T) {
pub, ok := vm.ParseSignatureContract(acc.Script()) pub, ok := vm.ParseSignatureContract(acc.Script())
require.True(t, ok) require.True(t, ok)
id := make([]byte, 4) id := make([]byte, 5)
binary.LittleEndian.PutUint32(id, 123) binary.LittleEndian.PutUint32(id, 123)
info := randomBytes(10) info := randomBytes(10)
@ -70,7 +70,8 @@ func TestSubnet_Delete(t *testing.T) {
e.InvokeFail(t, "witness check failed", "delete", id) e.InvokeFail(t, "witness check failed", "delete", id)
cAcc := e.WithSigners(owner) cAcc := e.WithSigners(owner)
cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", []byte{1, 1, 1, 1}) cAcc.InvokeFail(t, subnet.ErrInvalidSubnetID, "delete", []byte{1, 1, 1, 1})
cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", []byte{1, 1, 1, 1, 1})
cAcc.Invoke(t, stackitem.Null{}, "delete", id) cAcc.Invoke(t, stackitem.Null{}, "delete", id)
cAcc.InvokeFail(t, subnet.ErrNotExist, "get", id) cAcc.InvokeFail(t, subnet.ErrNotExist, "get", id)
cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", id) cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", id)
@ -87,8 +88,9 @@ func TestSubnet_AddNodeAdmin(t *testing.T) {
const method = "addNodeAdmin" const method = "addNodeAdmin"
e.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, admPub)
e.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, admPub[1:]) e.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, admPub[1:])
e.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, admPub) e.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, admPub)
cAdm := e.WithSigners(adm) cAdm := e.WithSigners(adm)
cAdm.InvokeFail(t, method+errSeparator+"owner witness check failed", method, id, admPub) cAdm.InvokeFail(t, method+errSeparator+"owner witness check failed", method, id, admPub)
@ -110,8 +112,9 @@ func TestSubnet_RemoveNodeAdmin(t *testing.T) {
const method = "removeNodeAdmin" const method = "removeNodeAdmin"
e.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, admPub)
e.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, admPub[1:]) e.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, admPub[1:])
e.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, admPub) e.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, admPub)
cAdm := e.WithSigners(adm) cAdm := e.WithSigners(adm)
cAdm.InvokeFail(t, method+errSeparator+"owner witness check failed", method, id, admPub) cAdm.InvokeFail(t, method+errSeparator+"owner witness check failed", method, id, admPub)
@ -136,8 +139,9 @@ func TestSubnet_AddNode(t *testing.T) {
const method = "addNode" const method = "addNode"
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidNode, method, id, nodePub[1:]) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidNode, method, id, nodePub[1:])
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, nodePub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub) cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
cOwn.InvokeFail(t, method+errSeparator+"node has already been added", method, id, nodePub) cOwn.InvokeFail(t, method+errSeparator+"node has already been added", method, id, nodePub)
@ -159,8 +163,9 @@ func TestSubnet_RemoveNode(t *testing.T) {
const method = "removeNode" const method = "removeNode"
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidNode, method, id, nodePub[1:]) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidNode, method, id, nodePub[1:])
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, nodePub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNodeNotExist, method, id, nodePub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNodeNotExist, method, id, nodePub)
cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub) cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub)
@ -184,8 +189,9 @@ func TestSubnet_NodeAllowed(t *testing.T) {
const method = "nodeAllowed" const method = "nodeAllowed"
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidNode, method, id, nodePub[1:]) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidNode, method, id, nodePub[1:])
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, nodePub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
cOwn.Invoke(t, stackitem.NewBool(false), method, id, nodePub) cOwn.Invoke(t, stackitem.NewBool(false), method, id, nodePub)
cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub) cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub)
@ -206,8 +212,9 @@ func TestSubnet_AddClientAdmin(t *testing.T) {
groupId := randomBytes(8) groupId := randomBytes(8)
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, admPub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:]) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:])
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, groupId, admPub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub) cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
cOwn.InvokeFail(t, method+errSeparator+"client admin has already been added", method, id, groupId, admPub) cOwn.InvokeFail(t, method+errSeparator+"client admin has already been added", method, id, groupId, admPub)
} }
@ -226,8 +233,9 @@ func TestSubnet_RemoveClientAdmin(t *testing.T) {
groupId := randomBytes(8) groupId := randomBytes(8)
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, admPub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:]) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:])
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, groupId, admPub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, admPub)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrClientAdmNotExist, method, id, groupId, admPub) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrClientAdmNotExist, method, id, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub) cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub) cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
@ -249,7 +257,8 @@ func TestSubnet_AddUser(t *testing.T) {
const method = "addUser" const method = "addUser"
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrSubNotExist, method, []byte{0, 0, 0, 0}, groupId, user) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, user)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, user)
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub) cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
@ -273,7 +282,8 @@ func TestSubnet_RemoveUser(t *testing.T) {
const method = "removeUser" const method = "removeUser"
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrSubNotExist, method, []byte{0, 0, 0, 0}, groupId, user) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, user)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, user)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrUserNotExist, method, id, groupId, user) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrUserNotExist, method, id, groupId, user)
cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user) cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user)
@ -290,14 +300,14 @@ func TestSubnet_UserAllowed(t *testing.T) {
id, owner := createSubnet(t, e) id, owner := createSubnet(t, e)
groupId := randomBytes(4) groupId := randomBytes(5)
user := randomBytes(27) user := randomBytes(27)
const method = "userAllowed" const method = "userAllowed"
cOwn := e.WithSigners(owner) cOwn := e.WithSigners(owner)
cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0}, user) cOwn.InvokeFail(t, method+errSeparator+subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, user)
cOwn.Invoke(t, stackitem.NewBool(false), method, id, user) cOwn.Invoke(t, stackitem.NewBool(false), method, id, user)
cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user) cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user)
@ -314,7 +324,7 @@ func createSubnet(t *testing.T, e *neotest.ContractInvoker) (id []byte, owner ne
pub, ok = vm.ParseSignatureContract(owner.Script()) pub, ok = vm.ParseSignatureContract(owner.Script())
require.True(t, ok) require.True(t, ok)
id = make([]byte, 4) id = make([]byte, 5)
binary.LittleEndian.PutUint32(id, 123) binary.LittleEndian.PutUint32(id, 123)
info := randomBytes(10) info := randomBytes(10)