Skip to content

Commit

Permalink
Enable type removal during contract updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Oct 30, 2024
1 parent 1399a6d commit 95f7dbd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 177 deletions.
2 changes: 0 additions & 2 deletions interpreter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ type Config struct {
CapabilityBorrowHandler CapabilityBorrowHandlerFunc
// LegacyContractUpgradeEnabled specifies whether to fall back to the old parser when attempting a contract upgrade
LegacyContractUpgradeEnabled bool
// ContractUpdateTypeRemovalEnabled specifies if type removal is enabled in contract updates
ContractUpdateTypeRemovalEnabled bool
// ValidateAccountCapabilitiesGetHandler is used to handle when a capability of an account is got.
ValidateAccountCapabilitiesGetHandler ValidateAccountCapabilitiesGetHandlerFunc
// ValidateAccountCapabilitiesPublishHandler is used to handle when a capability of an account is got.
Expand Down
2 changes: 0 additions & 2 deletions runtime/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ type Config struct {
CoverageReport *CoverageReport
// LegacyContractUpgradeEnabled enabled specifies whether to use the old parser when parsing an old contract
LegacyContractUpgradeEnabled bool
// ContractUpdateTypeRemovalEnabled specifies if type removal is enabled in contract updates
ContractUpdateTypeRemovalEnabled bool
}
135 changes: 27 additions & 108 deletions runtime/contract_update_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,31 +177,14 @@ func testWithValidatorsAndTypeRemovalEnabled(
withC1Upgrade := withC1Upgrade
name := name

for _, withTypeRemovalEnabled := range []bool{true, false} {
withTypeRemovalEnabled := withTypeRemovalEnabled
name := name

switch {
case withC1Upgrade && withTypeRemovalEnabled:
name = fmt.Sprintf("%s (with C1 validator and type removal enabled)", name)

case withC1Upgrade:
name = fmt.Sprintf("%s (with C1 validator)", name)

case withTypeRemovalEnabled:
name = fmt.Sprintf("%s (with type removal enabled)", name)
}

t.Run(name, func(t *testing.T) {
t.Parallel()
t.Run(name, func(t *testing.T) {
t.Parallel()

config := DefaultTestInterpreterConfig
config.LegacyContractUpgradeEnabled = withC1Upgrade
config.ContractUpdateTypeRemovalEnabled = withTypeRemovalEnabled
config := DefaultTestInterpreterConfig
config.LegacyContractUpgradeEnabled = withC1Upgrade

testFunc(t, config)
})
}
testFunc(t, config)
})
}
}

Expand Down Expand Up @@ -3230,13 +3213,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.TypeRemovalPragmaRemovalError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.TypeRemovalPragmaRemovalError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3262,13 +3240,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.TypeRemovalPragmaRemovalError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.TypeRemovalPragmaRemovalError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand Down Expand Up @@ -3313,13 +3286,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3342,13 +3310,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3368,14 +3331,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {

var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3395,13 +3352,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.InvalidTypeRemovalPragmaError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3422,13 +3374,7 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
require.NoError(t, err)
} else {
var expectedErr *stdlib.MissingDeclarationError
require.ErrorAs(t, err, &expectedErr)
}
require.NoError(t, err)
},
)

Expand All @@ -3451,13 +3397,7 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
require.NoError(t, err)
} else {
var expectedErr *stdlib.MissingDeclarationError
require.ErrorAs(t, err, &expectedErr)
}
require.NoError(t, err)
},
)

Expand Down Expand Up @@ -3526,13 +3466,7 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
require.NoError(t, err)
} else {
var expectedErr *stdlib.MissingDeclarationError
require.ErrorAs(t, err, &expectedErr)
}
require.NoError(t, err)
},
)

Expand Down Expand Up @@ -3574,13 +3508,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.UseOfRemovedTypeError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.UseOfRemovedTypeError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3602,13 +3531,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.UseOfRemovedTypeError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.UseOfRemovedTypeError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand All @@ -3630,13 +3554,8 @@ func TestTypeRemovalPragmaUpdates(t *testing.T) {
`

err := testDeployAndUpdate(t, "Test", oldCode, newCode, config)

if config.ContractUpdateTypeRemovalEnabled {
var expectedErr *stdlib.UseOfRemovedTypeError
require.ErrorAs(t, err, &expectedErr)
} else {
require.NoError(t, err)
}
var expectedErr *stdlib.UseOfRemovedTypeError
require.ErrorAs(t, err, &expectedErr)
},
)

Expand Down
1 change: 0 additions & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ func (e *interpreterEnvironment) newInterpreterConfig() *interpreter.Config {
CapabilityBorrowHandler: e.newCapabilityBorrowHandler(),
CapabilityCheckHandler: e.newCapabilityCheckHandler(),
LegacyContractUpgradeEnabled: e.config.LegacyContractUpgradeEnabled,
ContractUpdateTypeRemovalEnabled: e.config.ContractUpdateTypeRemovalEnabled,
ValidateAccountCapabilitiesGetHandler: e.newValidateAccountCapabilitiesGetHandler(),
ValidateAccountCapabilitiesPublishHandler: e.newValidateAccountCapabilitiesPublishHandler(),
}
Expand Down
3 changes: 0 additions & 3 deletions stdlib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,6 @@ func changeAccountContracts(

memoryGauge := invocation.Interpreter.SharedState.Config.MemoryGauge
legacyUpgradeEnabled := invocation.Interpreter.SharedState.Config.LegacyContractUpgradeEnabled
contractUpdateTypeRemovalEnabled := invocation.Interpreter.SharedState.Config.ContractUpdateTypeRemovalEnabled

var oldProgram *ast.Program

Expand Down Expand Up @@ -1731,8 +1730,6 @@ func changeAccountContracts(
)
}

validator = validator.WithTypeRemovalEnabled(contractUpdateTypeRemovalEnabled)

err = validator.Validate()
handleContractUpdateError(err, newCode)
}
Expand Down
11 changes: 0 additions & 11 deletions stdlib/cadence_v0.42_to_v1_contract_upgrade_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,13 @@ func (validator *CadenceV042ToV1ContractUpdateValidator) Location() common.Locat
return validator.underlyingUpdateValidator.location
}

func (validator *CadenceV042ToV1ContractUpdateValidator) isTypeRemovalEnabled() bool {
return validator.underlyingUpdateValidator.isTypeRemovalEnabled()
}

func (validator *CadenceV042ToV1ContractUpdateValidator) WithUserDefinedTypeChangeChecker(
typeChangeCheckFunc func(oldTypeID common.TypeID, newTypeID common.TypeID) (checked, valid bool),
) *CadenceV042ToV1ContractUpdateValidator {
validator.checkUserDefinedType = typeChangeCheckFunc
return validator
}

func (validator *CadenceV042ToV1ContractUpdateValidator) WithTypeRemovalEnabled(
enabled bool,
) UpdateValidator {
validator.underlyingUpdateValidator.WithTypeRemovalEnabled(enabled)
return validator
}

func (validator *CadenceV042ToV1ContractUpdateValidator) getCurrentDeclaration() ast.Declaration {
return validator.underlyingUpdateValidator.getCurrentDeclaration()
}
Expand Down
Loading

0 comments on commit 95f7dbd

Please sign in to comment.