Skip to content

Commit

Permalink
Switch from sync.Once to a boolean flag
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Aug 20, 2024
1 parent f712fde commit 2d6e436
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
33 changes: 19 additions & 14 deletions migrations/capcons/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Path struct {

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

func (c *AccountCapabilities) Record(
Expand All @@ -63,6 +63,9 @@ func (c *AccountCapabilities) Record(
},
},
)

// Reset the sorted flag, if new entries are added.
c.sorted = false
}

// ForEachSorted will first sort the capabilities list,
Expand All @@ -79,22 +82,24 @@ func (c *AccountCapabilities) ForEachSorted(
}

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),
)
},
if c.sorted {
return
}

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),
)
},
)

c.sorted = true
}

type AccountsCapabilities struct {
Expand Down
34 changes: 34 additions & 0 deletions migrations/capcons/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
Expand Down Expand Up @@ -67,17 +68,50 @@ func TestCapabilitiesIteration(t *testing.T) {
nil,
)

require.False(t, caps.sorted)

var paths []interpreter.PathValue
caps.ForEachSorted(func(capability AccountCapability) bool {
paths = append(paths, capability.TargetPath)
return true
})

require.True(t, caps.sorted)

assert.Equal(
t,
[]interpreter.PathValue{
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "a"),
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "b"),
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "c"),
interpreter.NewUnmeteredPathValue(common.PathDomainPublic, "a"),
interpreter.NewUnmeteredPathValue(common.PathDomainPublic, "b"),
},
paths,
)

caps.Record(
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "aa"),
nil,
interpreter.StorageKey{},
nil,
)

require.False(t, caps.sorted)

paths = make([]interpreter.PathValue, 0)
caps.ForEachSorted(func(capability AccountCapability) bool {
paths = append(paths, capability.TargetPath)
return true
})

require.True(t, caps.sorted)

assert.Equal(
t,
[]interpreter.PathValue{
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "a"),
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "aa"),
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "b"),
interpreter.NewUnmeteredPathValue(common.PathDomainStorage, "c"),
interpreter.NewUnmeteredPathValue(common.PathDomainPublic, "a"),
Expand Down

0 comments on commit 2d6e436

Please sign in to comment.