Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Aug 20, 2024
1 parent 7f68e06 commit f712fde
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
54 changes: 30 additions & 24 deletions migrations/capcons/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package capcons

import (
"cmp"
"fmt"
"strings"
"sync"
Expand All @@ -41,7 +42,8 @@ type Path struct {
}

type AccountCapabilities struct {
Capabilities []AccountCapability
capabilities []AccountCapability
sortOnce sync.Once
}

func (c *AccountCapabilities) Record(
Expand All @@ -50,8 +52,8 @@ func (c *AccountCapabilities) Record(
storageKey interpreter.StorageKey,
storageMapKey interpreter.StorageMapKey,
) {
c.Capabilities = append(
c.Capabilities,
c.capabilities = append(
c.capabilities,
AccountCapability{
TargetPath: path,
BorrowType: borrowType,
Expand All @@ -63,34 +65,38 @@ func (c *AccountCapabilities) Record(
)
}

func (c *AccountCapabilities) ForEach(
// ForEachSorted will first sort the capabilities list,
// and iterates through the sorted list.
func (c *AccountCapabilities) ForEachSorted(
f func(AccountCapability) bool,
) {
slices.SortFunc(
c.Capabilities,
func(a, b AccountCapability) int {
pathA := a.TargetPath
pathB := b.TargetPath

if pathA.Domain == pathB.Domain {
return strings.Compare(pathA.Identifier, pathB.Identifier)
}

if pathA.Domain < pathB.Domain {
return -1
}

return +1
},
)

for _, accountCapability := range c.Capabilities {
c.sort()
for _, accountCapability := range c.capabilities {
if !f(accountCapability) {
return
}
}
}

func (c *AccountCapabilities) sort() {
c.sortOnce.Do(
func() {
slices.SortFunc(
c.capabilities,
func(a, b AccountCapability) int {
pathA := a.TargetPath
pathB := b.TargetPath

return cmp.Or(
cmp.Compare(pathA.Domain, pathB.Domain),
strings.Compare(pathA.Identifier, pathB.Identifier),
)
},
)
},
)
}

type AccountsCapabilities struct {
// accountCapabilities maps common.Address to *AccountCapabilities
accountCapabilities sync.Map
Expand Down Expand Up @@ -129,7 +135,7 @@ func (m *AccountsCapabilities) ForEach(

accountCapabilities := rawAccountCapabilities.(*AccountCapabilities)

accountCapabilities.ForEach(f)
accountCapabilities.ForEachSorted(f)
}

func (m *AccountsCapabilities) Get(address common.Address) *AccountCapabilities {
Expand Down
2 changes: 1 addition & 1 deletion migrations/capcons/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestCapabilitiesIteration(t *testing.T) {

var paths []interpreter.PathValue

caps.ForEach(func(capability AccountCapability) bool {
caps.ForEachSorted(func(capability AccountCapability) bool {
paths = append(paths, capability.TargetPath)
return true
})
Expand Down
2 changes: 1 addition & 1 deletion migrations/capcons/storagecapmigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func IssueAccountCapabilities(
false,
)

capabilities.ForEach(func(capability AccountCapability) bool {
capabilities.ForEachSorted(func(capability AccountCapability) bool {

addressPath := interpreter.AddressPath{
Address: address,
Expand Down

0 comments on commit f712fde

Please sign in to comment.