Skip to content

Commit

Permalink
allow contract updates with specific parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Jun 10, 2022
1 parent 11db24c commit bb21268
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
84 changes: 84 additions & 0 deletions runtime/contract_update_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,90 @@ func TestRuntimeContractUpdateConformanceChanges(t *testing.T) {
err := testDeployAndUpdate(t, contractValidationEnabled, "Test", oldCode, newCode)
require.NoError(t, err)
})

t.Run("missing comma in parameter list of old contract", func(t *testing.T) {

t.Parallel()

address := common.MustBytesToAddress([]byte{0x42})

const contractName = "Test"

const oldCode = `
pub contract Test {
pub fun test(a: Int b: Int) {}
}
`

const newCode = `
pub contract Test {
pub fun test(a: Int, b: Int) {}
}
`

rt := newTestInterpreterRuntime(
WithContractUpdateValidationEnabled(contractValidationEnabled),
)

accountCodes := map[common.LocationID][]byte{
common.AddressLocation{
Address: address,
Name: contractName,
}.ID(): []byte(oldCode),
}

var events []cadence.Event
runtimeInterface := &testRuntimeInterface{
getCode: func(location Location) (bytes []byte, err error) {
return accountCodes[location.ID()], nil
},
storage: newTestLedger(nil, nil),
getSigningAccounts: func() ([]Address, error) {
return []Address{address}, nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getAccountContractCode: func(address Address, name string) (code []byte, err error) {
location := common.AddressLocation{
Address: address,
Name: name,
}
return accountCodes[location.ID()], nil
},
updateAccountContractCode: func(address Address, name string, code []byte) error {
location := common.AddressLocation{
Address: address,
Name: name,
}
accountCodes[location.ID()] = code
return nil
},
removeAccountContractCode: func(address Address, name string) error {
location := common.AddressLocation{
Address: address,
Name: name,
}
delete(accountCodes, location.ID())
return nil
},
emitEvent: func(event cadence.Event) error {
events = append(events, event)
return nil
},
}

nextTransactionLocation := newTransactionLocationGenerator()

err := rt.ExecuteTransaction(
Script{
Source: []byte(newContractUpdateTransaction(contractName, newCode)),
},
Context{
Interface: runtimeInterface,
Location: nextTransactionLocation(),
},
)
require.NoError(t, err)
})
}

func TestRuntimeContractUpdateProgramCaching(t *testing.T) {
Expand Down
27 changes: 26 additions & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2709,7 +2709,10 @@ func (r *interpreterRuntime) newAuthAccountContractsChangeFunction(
handleContractUpdateError(err)

oldProgram, err := parser2.ParseProgram(string(oldCode), inter)
handleContractUpdateError(err)

if !ignoreUpdatedProgramParserError(err) {
handleContractUpdateError(err)
}

validator := NewContractUpdateValidator(
context.Location,
Expand Down Expand Up @@ -2784,6 +2787,28 @@ func (r *interpreterRuntime) newAuthAccountContractsChangeFunction(
)
}

// ignoreUpdatedProgramParserError determines if the parsing error
// for a program that is being updated can be ignored.
func ignoreUpdatedProgramParserError(err error) bool {
parserError, ok := err.(parser2.Error)
if !ok {
return false
}

// Are all parse errors ones that can be ignored?
for _, parseError := range parserError.Errors {
// Missing commas in parameter lists were reported starting
// with https://github.com/onflow/cadence/pull/1073.
// Allow existing contracts with such an error to be updated
_, ok := parseError.(*parser2.MissingCommaInParameterListError)
if !ok {
return false
}
}

return true
}

type updateAccountContractCodeOptions struct {
createContract bool
}
Expand Down

0 comments on commit bb21268

Please sign in to comment.