Skip to content

Commit 1ee093a

Browse files
committed
make interface condition deduplication feature flag version-based, add runtime tests
1 parent 7ac903a commit 1ee093a

File tree

4 files changed

+103
-16
lines changed

4 files changed

+103
-16
lines changed

interpreter/interface_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ func TestInterpretInterfaceFunctionConditionsInheritance(t *testing.T) {
11111111

11121112
t.Parallel()
11131113

1114-
testWithConditionsDeduplication := func(conditionsDeduplicationEnabled bool) []string {
1114+
testWithConditionsDeduplication := func(t *testing.T, conditionsDeduplicationEnabled bool) []string {
11151115

11161116
var logs []string
11171117

@@ -1164,7 +1164,7 @@ func TestInterpretInterfaceFunctionConditionsInheritance(t *testing.T) {
11641164
struct Test: Foo {
11651165
}
11661166
1167-
access(all) view fun printMessage(_ msg: String): Bool {
1167+
view fun printMessage(_ msg: String): Bool {
11681168
log(msg)
11691169
return true
11701170
}
@@ -1202,29 +1202,29 @@ func TestInterpretInterfaceFunctionConditionsInheritance(t *testing.T) {
12021202
t.Run("enabled", func(t *testing.T) {
12031203
t.Parallel()
12041204

1205-
logs := testWithConditionsDeduplication(true)
1205+
logs := testWithConditionsDeduplication(t, true)
12061206
require.Equal(
12071207
t,
12081208
[]string{
1209-
"\"invoked Foo.test() pre-condition\"",
1210-
"\"invoked Foo.test()\"",
1211-
"\"invoked Foo.test() post-condition\"",
1209+
`"invoked Foo.test() pre-condition"`,
1210+
`"invoked Foo.test()"`,
1211+
`"invoked Foo.test() post-condition"`,
12121212
}, logs,
12131213
)
12141214
})
12151215

12161216
t.Run("disabled", func(t *testing.T) {
12171217
t.Parallel()
12181218

1219-
logs := testWithConditionsDeduplication(false)
1219+
logs := testWithConditionsDeduplication(t, false)
12201220
require.Equal(
12211221
t,
12221222
[]string{
1223-
"\"invoked Foo.test() pre-condition\"",
1224-
"\"invoked Foo.test() pre-condition\"",
1225-
"\"invoked Foo.test()\"",
1226-
"\"invoked Foo.test() post-condition\"",
1227-
"\"invoked Foo.test() post-condition\"",
1223+
`"invoked Foo.test() pre-condition"`,
1224+
`"invoked Foo.test() pre-condition"`,
1225+
`"invoked Foo.test()"`,
1226+
`"invoked Foo.test() post-condition"`,
1227+
`"invoked Foo.test() post-condition"`,
12281228
}, logs,
12291229
)
12301230
})

runtime/config.go

-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,4 @@ type Config struct {
3939
LegacyContractUpgradeEnabled bool
4040
// StorageFormatV2Enabled specifies whether storage format V2 is enabled
4141
StorageFormatV2Enabled bool
42-
// FunctionConditionsDeduplicationEnabled determines whether to skip running function conditions explicitly
43-
// if they are already included as part of the inherited default function.
44-
FunctionConditionsDeduplicationEnabled bool
4542
}

runtime/environment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ func (e *interpreterEnvironment) newInterpreterConfig() *interpreter.Config {
199199
LegacyContractUpgradeEnabled: e.config.LegacyContractUpgradeEnabled,
200200
ValidateAccountCapabilitiesGetHandler: e.newValidateAccountCapabilitiesGetHandler(),
201201
ValidateAccountCapabilitiesPublishHandler: e.newValidateAccountCapabilitiesPublishHandler(),
202-
FunctionConditionsDeduplicationEnabled: e.config.FunctionConditionsDeduplicationEnabled,
203202
}
204203
}
205204

@@ -1447,4 +1446,5 @@ func (e *interpreterEnvironment) configureVersionedFeatures() {
14471446
fixesEnabled := semver.Compare(minimumRequiredVersion, FixesEnabledVersion) >= 0
14481447
e.InterpreterConfig.ExportFixesEnabled = fixesEnabled
14491448
e.InterpreterConfig.FunctionScopingFixEnabled = fixesEnabled
1449+
e.InterpreterConfig.FunctionConditionsDeduplicationEnabled = fixesEnabled
14501450
}

runtime/runtime_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -12392,3 +12392,93 @@ func TestRuntimeClosureScopingInnerFunction(t *testing.T) {
1239212392
require.Equal(t, cadence.NewInt(2), actual)
1239312393
})
1239412394
}
12395+
12396+
func TestRuntimeInterfaceConditionDeduplication(t *testing.T) {
12397+
t.Parallel()
12398+
12399+
testWithConditionsDeduplication := func(t *testing.T, conditionsDeduplicationEnabled bool) []string {
12400+
12401+
script := `
12402+
access(all) event Log(message: String)
12403+
12404+
access(all) struct interface Foo {
12405+
12406+
access(all) fun test() {
12407+
pre {
12408+
emit Log(message: "invoked Foo.test() pre-condition")
12409+
}
12410+
post {
12411+
emit Log(message: "invoked Foo.test() post-condition")
12412+
}
12413+
emit Log(message: "invoked Foo.test()")
12414+
}
12415+
}
12416+
12417+
access(all) struct Test: Foo {
12418+
}
12419+
12420+
access(all) fun main() {
12421+
Test().test()
12422+
}
12423+
`
12424+
12425+
rt := NewTestInterpreterRuntime()
12426+
12427+
var events []string
12428+
12429+
_, err := rt.ExecuteScript(
12430+
Script{
12431+
Source: []byte(script),
12432+
},
12433+
Context{
12434+
Interface: &TestRuntimeInterface{
12435+
OnMinimumRequiredVersion: func() (string, error) {
12436+
if conditionsDeduplicationEnabled {
12437+
return FixesEnabledVersion, nil
12438+
} else {
12439+
return "v0.0.0", nil
12440+
}
12441+
},
12442+
OnEmitEvent: func(event cadence.Event) error {
12443+
events = append(events, event.FieldsMappedByName()["message"].String())
12444+
return nil
12445+
},
12446+
},
12447+
Location: common.ScriptLocation{},
12448+
},
12449+
)
12450+
require.NoError(t, err)
12451+
12452+
return events
12453+
}
12454+
12455+
t.Run("enabled", func(t *testing.T) {
12456+
t.Parallel()
12457+
12458+
logs := testWithConditionsDeduplication(t, true)
12459+
require.Equal(
12460+
t,
12461+
[]string{
12462+
`"invoked Foo.test() pre-condition"`,
12463+
`"invoked Foo.test()"`,
12464+
`"invoked Foo.test() post-condition"`,
12465+
}, logs,
12466+
)
12467+
})
12468+
12469+
t.Run("disabled", func(t *testing.T) {
12470+
t.Parallel()
12471+
12472+
logs := testWithConditionsDeduplication(t, false)
12473+
require.Equal(
12474+
t,
12475+
[]string{
12476+
`"invoked Foo.test() pre-condition"`,
12477+
`"invoked Foo.test() pre-condition"`,
12478+
`"invoked Foo.test()"`,
12479+
`"invoked Foo.test() post-condition"`,
12480+
`"invoked Foo.test() post-condition"`,
12481+
}, logs,
12482+
)
12483+
})
12484+
}

0 commit comments

Comments
 (0)