Skip to content

Commit

Permalink
Merge pull request #3306 from onflow/bastian/update-atree-register-in…
Browse files Browse the repository at this point in the history
…lining-v1.0-3
  • Loading branch information
turbolent authored May 2, 2024
2 parents 09bb39a + 153d4a3 commit 4ef6541
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/kr/pretty v0.3.1
github.com/leanovate/gopter v0.2.9
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/onflow/atree v0.6.1-0.20240429171214-e4400b25dfa9
github.com/onflow/atree v0.8.0-rc.1
github.com/rivo/uniseg v0.4.4
github.com/schollz/progressbar/v3 v3.13.1
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvr
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/onflow/atree v0.6.1-0.20240429171214-e4400b25dfa9 h1:lS/47Nt8qRIEC+V8QPobTAWnudK982u/wJboucugndg=
github.com/onflow/atree v0.6.1-0.20240429171214-e4400b25dfa9/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A=
github.com/onflow/atree v0.8.0-rc.1 h1:sTxguTcS0qq8vv0EcJri0OO2be8/GCZDARGm7Nt0XRg=
github.com/onflow/atree v0.8.0-rc.1/go.mod h1:7YNAyCd5JENq+NzH+fR1ABUZVzbSq9dkt0+5fZH3L2A=
github.com/onflow/crypto v0.25.0 h1:BeWbLsh3ZD13Ej+Uky6kg1PL1ZIVBDVX+2MVBNwqddg=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/cadence-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onflow/cadence-parser",
"version": "1.0.0-preview.23",
"version": "1.0.0-preview.25",
"description": "The Cadence parser",
"homepage": "https://github.com/onflow/cadence",
"repository": {
Expand Down
3 changes: 3 additions & 0 deletions runtime/common/orderedmap/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (om *OrderedMap[K, V]) Delete(key K) (oldValue V, present bool) {

// Len returns the length of the ordered map.
func (om *OrderedMap[K, V]) Len() int {
if om == nil {
return 0
}
return len(om.pairs)
}

Expand Down
40 changes: 38 additions & 2 deletions runtime/sema/entitlementset.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ func disjunctionKey(disjunction *EntitlementOrderedSet) string {
type DisjunctionOrderedSet = orderedmap.OrderedMap[string, *EntitlementOrderedSet]

// EntitlementSet is a set (conjunction) of entitlements and entitlement disjunctions.
// e.g. {entitlements: A, B; disjunctions: (C | D), (E | F)}
// e.g. {entitlements: A, B; disjunctions: (C ∨ D), (E ∨ F)}
// This is distinct from an EntitlementSetAccess in Cadence, which is a program-level
// (and possibly non-minimal) approximation of this abstract set
type EntitlementSet struct {
// Entitlements is a set of entitlements
Entitlements *EntitlementOrderedSet
// Disjunctions is a set of entitlement disjunctions, keyed by disjunctionKey
Disjunctions *DisjunctionOrderedSet
// Minimized tracks whether the set is minimized or not
minimized bool
}

// Add adds an entitlement to the set.
Expand All @@ -67,6 +71,8 @@ func (s *EntitlementSet) Add(entitlementType *EntitlementType) {
s.Entitlements = orderedmap.New[EntitlementOrderedSet](1)
}
s.Entitlements.Set(entitlementType, struct{}{})

s.minimized = false
}

// AddDisjunction adds an entitlement disjunction to the set.
Expand All @@ -92,6 +98,8 @@ func (s *EntitlementSet) AddDisjunction(disjunction *EntitlementOrderedSet) {
s.Disjunctions = orderedmap.New[DisjunctionOrderedSet](1)
}
s.Disjunctions.Set(key, disjunction)

s.minimized = false
}

// Merge merges the other entitlement set into this set.
Expand All @@ -116,12 +124,16 @@ func (s *EntitlementSet) Merge(other *EntitlementSet) {
s.AddDisjunction(disjunction)
})
}

s.minimized = false
}

// Minimize minimizes the entitlement set.
// It removes disjunctions that contain entitlements
// which are also in the entitlement set
func (s *EntitlementSet) Minimize() {
defer func() { s.minimized = true }()

// If there are no entitlements or no disjunctions,
// there is nothing to minimize
if s.Entitlements == nil || s.Disjunctions == nil {
Expand All @@ -141,14 +153,38 @@ func (s *EntitlementSet) Minimize() {
}
}

// Returns whether this entitlement set is minimally representable in Cadence.
//
// If true, this set can be exactly represented as a non-nested logical formula: i.e. either a single conjunction or a single disjunction
// If false, this set cannot be represented without nesting connective operators, and thus must be over-approximated when being
// represented in Cadence.
// As Cadence does not support nesting disjunctions and conjunctions in the same entitlement set, this function returns false
// when s.Entitlements and s.Disjunctions are both non-empty, or when s.Disjunctions has more than one element
func (s *EntitlementSet) IsMinimallyRepresentable() bool {
if s == nil {
return true
}

if !s.minimized {
s.Minimize()
}

return s.Disjunctions.Len() == 0 || (s.Entitlements.Len() == 0 && s.Disjunctions.Len() == 1)
}

// Access returns the access represented by the entitlement set.
// The set is minimized before the access is computed.
// Note that this function may over-approximate the permissions
// required to represent this set of entitlements as an access modifier that Cadence can use,
// e.g. `(A ∨ B) ∧ C` cannot be represented in Cadence and will the produce an over-approximation of `(A, B, C)`
func (s *EntitlementSet) Access() Access {
if s == nil {
return UnauthorizedAccess
}

s.Minimize()
if !s.minimized {
s.Minimize()
}

var entitlements *EntitlementOrderedSet
if s.Entitlements != nil && s.Entitlements.Len() > 0 {
Expand Down
Loading

0 comments on commit 4ef6541

Please sign in to comment.