Skip to content

Commit

Permalink
sort role func in auth contract (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy committed Sep 26, 2019
1 parent 1970d6a commit 2377759
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
22 changes: 8 additions & 14 deletions smartcontract/service/native/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package auth
import (
"bytes"
"fmt"
"strings"
"time"

"github.com/ontio/ontology/account"
Expand Down Expand Up @@ -191,13 +190,12 @@ func AssignFuncsToRole(native *native.NativeService) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("[assignFuncsToRole] getRoleFunc failed: %v", err)
}
if funcs != nil {
funcNames := append(funcs.funcNames, param.FuncNames...)
funcs.funcNames = stringSliceUniq(funcNames)
} else {
if funcs == nil {
funcs = new(roleFuncs)
funcs.funcNames = stringSliceUniq(param.FuncNames)
}

funcs.AppendFuncs(param.FuncNames)

err = putRoleFunc(native, param.ContractAddr, param.Role, funcs)
if err != nil {
return nil, fmt.Errorf("[assignFuncsToRole] putRoleFunc failed: %v", err)
Expand Down Expand Up @@ -589,10 +587,8 @@ func verifyToken(native *native.NativeService, contractAddr common.Address, call
if funcs == nil || token.expireTime < native.Time {
continue
}
for _, f := range funcs.funcNames {
if strings.Compare(fn, f) == 0 {
return true, nil
}
if funcs.ContainsFunc(fn) {
return true, nil
}
}
}
Expand All @@ -610,10 +606,8 @@ func verifyToken(native *native.NativeService, contractAddr common.Address, call
if funcs == nil || s.expireTime < native.Time {
continue
}
for _, f := range funcs.funcNames {
if strings.Compare(fn, f) == 0 {
return true, nil
}
if funcs.ContainsFunc(fn) {
return true, nil
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions smartcontract/service/native/auth/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package auth

import (
"io"
"strings"

"github.com/ontio/ontology/common/serialization"
)
Expand All @@ -31,10 +32,26 @@ type roleFuncs struct {
funcNames []string
}

func (this *roleFuncs) AppendFuncs(fns []string) {
funcNames := append(this.funcNames, fns...)
this.funcNames = StringsDedupAndSort(funcNames)
}

func (this *roleFuncs) ContainsFunc(fn string) bool {
for _, f := range this.funcNames {
if strings.Compare(fn, f) == 0 {
return true
}
}

return false
}

func (this *roleFuncs) Serialize(w io.Writer) error {
if err := serialization.WriteUint32(w, uint32(len(this.funcNames))); err != nil {
return err
}
this.funcNames = StringsDedupAndSort(this.funcNames)
for _, fn := range this.funcNames {
if err := serialization.WriteString(w, fn); err != nil {
return err
Expand All @@ -49,14 +66,17 @@ func (this *roleFuncs) Deserialize(rd io.Reader) error {
if err != nil {
return err
}
this.funcNames = make([]string, 0)
funcNames := make([]string, 0)
for i := uint32(0); i < fnLen; i++ {
fn, err := serialization.ReadString(rd)
if err != nil {
return err
}
this.funcNames = append(this.funcNames, fn)
funcNames = append(funcNames, fn)
}

this.funcNames = StringsDedupAndSort(funcNames)

return nil
}

Expand Down
8 changes: 5 additions & 3 deletions smartcontract/service/native/auth/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"fmt"
"io"
"sort"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/common/serialization"
Expand Down Expand Up @@ -181,8 +182,8 @@ func putDelegateStatus(native *native.NativeService, contractAddr common.Address
return nil
}

//remote duplicates in the slice of string
func stringSliceUniq(s []string) []string {
//remove duplicates in the slice of string and sorts the slice in increasing order.
func StringsDedupAndSort(s []string) []string {
smap := make(map[string]int)
for i, str := range s {
if str == "" {
Expand All @@ -192,10 +193,11 @@ func stringSliceUniq(s []string) []string {
}
ret := make([]string, len(smap))
i := 0
for str, _ := range smap {
for str := range smap {
ret[i] = str
i++
}
sort.Strings(ret)
return ret
}

Expand Down
38 changes: 7 additions & 31 deletions smartcontract/service/native/auth/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,13 @@
*/
package auth

import "testing"
import (
"github.com/magiconair/properties/assert"
"testing"
)

//{"a", "b"} == {"b", "a"}
func testEq(a, b []string) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}

if len(a) != len(b) {
return false
}
Map := make(map[string]bool)
for i := range a {
Map[a[i]] = true
}
for _, s := range b {
_, ok := Map[s]
if !ok {
return false
}
}
return true
}
func TestStringSliceUniq(t *testing.T) {
s := []string{"foo", "foo1", "foo2", "foo", "foo1", "foo2", "foo3"}
ret := stringSliceUniq(s)
t.Log(ret)
if !testEq(ret, []string{"foo", "foo1", "foo2", "foo3"}) {
t.Fatalf("failed")
}
s := []string{"foo3", "foo", "foo1", "foo2", "foo", "foo1", "foo2", "foo3"}
ret := StringsDedupAndSort(s)
assert.Equal(t, ret, []string{"foo", "foo1", "foo2", "foo3"})
}

0 comments on commit 2377759

Please sign in to comment.