Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly meter the creation of entitlement static types #2723

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,12 @@ func importAuthorization(memoryGauge common.MemoryGauge, auth cadence.Authorizat
case cadence.EntitlementMapAuthorization:
return interpreter.NewEntitlementMapAuthorization(memoryGauge, auth.TypeID)
case cadence.EntitlementSetAuthorization:
return interpreter.NewEntitlementSetAuthorization(memoryGauge, auth.Entitlements, sema.EntitlementSetKind(auth.Kind))
return interpreter.NewEntitlementSetAuthorization(
memoryGauge,
func() []common.TypeID { return auth.Entitlements },
len(auth.Entitlements),
sema.EntitlementSetKind(auth.Kind),
)
}
panic(fmt.Sprintf("cannot import authorization of type %T", auth))
}
Expand Down
13 changes: 11 additions & 2 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,12 @@ func TestImportRuntimeType(t *testing.T) {
Type: cadence.IntType{},
},
expected: interpreter.ReferenceStaticType{
Authorization: interpreter.NewEntitlementSetAuthorization(nil, []common.TypeID{"E", "F"}, sema.Conjunction),
Authorization: interpreter.NewEntitlementSetAuthorization(
nil,
func() []common.TypeID { return []common.TypeID{"E", "F"} },
2,
sema.Conjunction,
),
ReferencedType: interpreter.PrimitiveStaticTypeInt,
},
},
Expand All @@ -1226,7 +1231,11 @@ func TestImportRuntimeType(t *testing.T) {
Type: cadence.IntType{},
},
expected: interpreter.ReferenceStaticType{
Authorization: interpreter.NewEntitlementSetAuthorization(nil, []common.TypeID{"E", "F"}, sema.Disjunction),
Authorization: interpreter.NewEntitlementSetAuthorization(
nil,
func() []common.TypeID { return []common.TypeID{"E", "F"} },
2,
sema.Disjunction),
ReferencedType: interpreter.PrimitiveStaticTypeInt,
},
},
Expand Down
36 changes: 26 additions & 10 deletions runtime/interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1546,18 +1546,34 @@ func (d TypeDecoder) decodeStaticAuthorization() (Authorization, error) {
}
return nil, err
}
var entitlements []common.TypeID
if entitlementsSize > 0 {
entitlements = make([]common.TypeID, entitlementsSize)
for i := 0; i < int(entitlementsSize); i++ {
typeID, err := d.decoder.DecodeString()
if err != nil {
return nil, err

var setCreationErr error

entitlementSet := NewEntitlementSetAuthorization(
d.memoryGauge,
func() (entitlements []common.TypeID) {
if entitlementsSize > 0 {
entitlements = make([]common.TypeID, entitlementsSize)
for i := 0; i < int(entitlementsSize); i++ {
typeID, err := d.decoder.DecodeString()
if err != nil {
setCreationErr = err
return nil
}
entitlements[i] = common.TypeID(typeID)
}
}
entitlements[i] = common.TypeID(typeID)
}
return
},
int(entitlementsSize),
sema.EntitlementSetKind(setKind),
)

if setCreationErr != nil {
return nil, setCreationErr
}
return NewEntitlementSetAuthorization(d.memoryGauge, entitlements, sema.EntitlementSetKind(setKind)), nil

return entitlementSet, nil
}
return nil, errors.NewUnexpectedError("invalid static authorization encoding tag: %d", number)
}
Expand Down
45 changes: 26 additions & 19 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3481,34 +3481,41 @@ func referenceTypeFunction(invocation Invocation) Value {
}

var authorization Authorization = UnauthorizedAccess
var entitlements []common.TypeID = make([]common.TypeID, 0, entitlementValues.Count())
errInIteration := false
entitlementsCount := entitlementValues.Count()

entitlementValues.Iterate(invocation.Interpreter, func(element Value) (resume bool) {
entitlementString, isString := element.(*StringValue)
if !isString {
errInIteration = true
return false
}
if entitlementsCount > 0 {
authorization = NewEntitlementSetAuthorization(
invocation.Interpreter,
func() []common.TypeID {
entitlements := make([]common.TypeID, 0, entitlementsCount)
entitlementValues.Iterate(invocation.Interpreter, func(element Value) (resume bool) {
entitlementString, isString := element.(*StringValue)
if !isString {
errInIteration = true
return false
}

_, err := lookupEntitlement(invocation.Interpreter, entitlementString.Str)
if err != nil {
errInIteration = true
return false
}
entitlements = append(entitlements, common.TypeID(entitlementString.Str))
_, err := lookupEntitlement(invocation.Interpreter, entitlementString.Str)
if err != nil {
errInIteration = true
return false
}
entitlements = append(entitlements, common.TypeID(entitlementString.Str))

return true
})
return true
})
return entitlements
},
entitlementsCount,
sema.Conjunction,
)
}

if errInIteration {
return Nil
}

if len(entitlements) > 0 {
authorization = NewEntitlementSetAuthorization(invocation.Interpreter, entitlements, sema.Conjunction)
}

return NewSomeValueNonCopying(
invocation.Interpreter,
NewTypeValue(
Expand Down
24 changes: 21 additions & 3 deletions runtime/interpreter/statictype.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,21 @@ var _ Authorization = EntitlementSetAuthorization{}

func NewEntitlementSetAuthorization(
memoryGauge common.MemoryGauge,
entitlementList []common.TypeID,
entitlementListConstructor func() []common.TypeID,
entitlementListSize int,
kind sema.EntitlementSetKind,
) EntitlementSetAuthorization {
common.UseMemory(memoryGauge, common.MemoryUsage{
Kind: common.MemoryKindEntitlementSetStaticAccess,
Amount: uint64(len(entitlementList)),
Amount: uint64(entitlementListSize),
})

entitlementList := entitlementListConstructor()
if len(entitlementList) > entitlementListSize {
// it should not be possible to reach this point unless something is implemented wrong
panic(errors.NewUnreachableError())
}

entitlements := orderedmap.New[sema.TypeIDOrderedSet](len(entitlementList))
for _, entitlement := range entitlementList {
entitlements.Set(entitlement, struct{}{})
Expand Down Expand Up @@ -831,7 +838,18 @@ func ConvertSemaAccesstoStaticAuthorization(
typeId := key.ID()
entitlements = append(entitlements, typeId)
})
return NewEntitlementSetAuthorization(memoryGauge, entitlements, access.SetKind)
return NewEntitlementSetAuthorization(
memoryGauge,
func() (entitlements []common.TypeID) {
access.Entitlements.Foreach(func(key *sema.EntitlementType, _ struct{}) {
typeId := key.ID()
entitlements = append(entitlements, typeId)
})
return
},
access.Entitlements.Len(),
access.SetKind,
)

case sema.EntitlementMapAccess:
typeId := access.Type.ID()
Expand Down
Loading