Skip to content

Commit c8b5ea9

Browse files
authored
sort role func in auth contract (#1081)
1 parent 799c934 commit c8b5ea9

4 files changed

Lines changed: 42 additions & 50 deletions

File tree

smartcontract/service/native/auth/auth.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package auth
2121
import (
2222
"bytes"
2323
"fmt"
24-
"strings"
2524
"time"
2625

2726
"github.com/ontio/ontology/account"
@@ -191,13 +190,12 @@ func AssignFuncsToRole(native *native.NativeService) ([]byte, error) {
191190
if err != nil {
192191
return nil, fmt.Errorf("[assignFuncsToRole] getRoleFunc failed: %v", err)
193192
}
194-
if funcs != nil {
195-
funcNames := append(funcs.funcNames, param.FuncNames...)
196-
funcs.funcNames = stringSliceUniq(funcNames)
197-
} else {
193+
if funcs == nil {
198194
funcs = new(roleFuncs)
199-
funcs.funcNames = stringSliceUniq(param.FuncNames)
200195
}
196+
197+
funcs.AppendFuncs(param.FuncNames)
198+
201199
err = putRoleFunc(native, param.ContractAddr, param.Role, funcs)
202200
if err != nil {
203201
return nil, fmt.Errorf("[assignFuncsToRole] putRoleFunc failed: %v", err)
@@ -589,10 +587,8 @@ func verifyToken(native *native.NativeService, contractAddr common.Address, call
589587
if funcs == nil || token.expireTime < native.Time {
590588
continue
591589
}
592-
for _, f := range funcs.funcNames {
593-
if strings.Compare(fn, f) == 0 {
594-
return true, nil
595-
}
590+
if funcs.ContainsFunc(fn) {
591+
return true, nil
596592
}
597593
}
598594
}
@@ -610,10 +606,8 @@ func verifyToken(native *native.NativeService, contractAddr common.Address, call
610606
if funcs == nil || s.expireTime < native.Time {
611607
continue
612608
}
613-
for _, f := range funcs.funcNames {
614-
if strings.Compare(fn, f) == 0 {
615-
return true, nil
616-
}
609+
if funcs.ContainsFunc(fn) {
610+
return true, nil
617611
}
618612
}
619613
}

smartcontract/service/native/auth/state.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package auth
2020

2121
import (
2222
"io"
23+
"strings"
2324

2425
"github.com/ontio/ontology/common/serialization"
2526
)
@@ -31,10 +32,26 @@ type roleFuncs struct {
3132
funcNames []string
3233
}
3334

35+
func (this *roleFuncs) AppendFuncs(fns []string) {
36+
funcNames := append(this.funcNames, fns...)
37+
this.funcNames = StringsDedupAndSort(funcNames)
38+
}
39+
40+
func (this *roleFuncs) ContainsFunc(fn string) bool {
41+
for _, f := range this.funcNames {
42+
if strings.Compare(fn, f) == 0 {
43+
return true
44+
}
45+
}
46+
47+
return false
48+
}
49+
3450
func (this *roleFuncs) Serialize(w io.Writer) error {
3551
if err := serialization.WriteUint32(w, uint32(len(this.funcNames))); err != nil {
3652
return err
3753
}
54+
this.funcNames = StringsDedupAndSort(this.funcNames)
3855
for _, fn := range this.funcNames {
3956
if err := serialization.WriteString(w, fn); err != nil {
4057
return err
@@ -49,14 +66,17 @@ func (this *roleFuncs) Deserialize(rd io.Reader) error {
4966
if err != nil {
5067
return err
5168
}
52-
this.funcNames = make([]string, 0)
69+
funcNames := make([]string, 0)
5370
for i := uint32(0); i < fnLen; i++ {
5471
fn, err := serialization.ReadString(rd)
5572
if err != nil {
5673
return err
5774
}
58-
this.funcNames = append(this.funcNames, fn)
75+
funcNames = append(funcNames, fn)
5976
}
77+
78+
this.funcNames = StringsDedupAndSort(funcNames)
79+
6080
return nil
6181
}
6282

smartcontract/service/native/auth/utils.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"bytes"
2323
"fmt"
2424
"io"
25+
"sort"
2526

2627
"github.com/ontio/ontology/common"
2728
"github.com/ontio/ontology/common/serialization"
@@ -181,8 +182,8 @@ func putDelegateStatus(native *native.NativeService, contractAddr common.Address
181182
return nil
182183
}
183184

184-
//remote duplicates in the slice of string
185-
func stringSliceUniq(s []string) []string {
185+
//remove duplicates in the slice of string and sorts the slice in increasing order.
186+
func StringsDedupAndSort(s []string) []string {
186187
smap := make(map[string]int)
187188
for i, str := range s {
188189
if str == "" {
@@ -192,10 +193,11 @@ func stringSliceUniq(s []string) []string {
192193
}
193194
ret := make([]string, len(smap))
194195
i := 0
195-
for str, _ := range smap {
196+
for str := range smap {
196197
ret[i] = str
197198
i++
198199
}
200+
sort.Strings(ret)
199201
return ret
200202
}
201203

smartcontract/service/native/auth/utils_test.go

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,13 @@
1717
*/
1818
package auth
1919

20-
import "testing"
20+
import (
21+
"github.com/magiconair/properties/assert"
22+
"testing"
23+
)
2124

22-
//{"a", "b"} == {"b", "a"}
23-
func testEq(a, b []string) bool {
24-
if a == nil && b == nil {
25-
return true
26-
}
27-
if a == nil || b == nil {
28-
return false
29-
}
30-
31-
if len(a) != len(b) {
32-
return false
33-
}
34-
Map := make(map[string]bool)
35-
for i := range a {
36-
Map[a[i]] = true
37-
}
38-
for _, s := range b {
39-
_, ok := Map[s]
40-
if !ok {
41-
return false
42-
}
43-
}
44-
return true
45-
}
4625
func TestStringSliceUniq(t *testing.T) {
47-
s := []string{"foo", "foo1", "foo2", "foo", "foo1", "foo2", "foo3"}
48-
ret := stringSliceUniq(s)
49-
t.Log(ret)
50-
if !testEq(ret, []string{"foo", "foo1", "foo2", "foo3"}) {
51-
t.Fatalf("failed")
52-
}
26+
s := []string{"foo3", "foo", "foo1", "foo2", "foo", "foo1", "foo2", "foo3"}
27+
ret := StringsDedupAndSort(s)
28+
assert.Equal(t, ret, []string{"foo", "foo1", "foo2", "foo3"})
5329
}

0 commit comments

Comments
 (0)