Skip to content

Commit

Permalink
add nil check & test
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Apr 17, 2024
1 parent 739c595 commit 97c69f7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
10 changes: 5 additions & 5 deletions internal/migrate/staging_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ func (v *stagingValidator) resolveLocation(
}

func (v *stagingValidator) resolveAccountAccess(checker *sema.Checker, memberLocation common.Location) bool {
if checker == nil {
return false
}

checkerLocation, ok := checker.Location.(common.StringLocation)
if !ok {
return false
Expand All @@ -403,11 +407,7 @@ func (v *stagingValidator) resolveAccountAccess(checker *sema.Checker, memberLoc

// If the source code of the update is being checked, we should check account access based on the
// targeted network location of the contract & not the source code location
if checkerLocation == v.sourceCodeLocation && memberAddressLocation.Address == v.targetLocation.Address {
return true
}

return false
return checkerLocation == v.sourceCodeLocation && memberAddressLocation.Address == v.targetLocation.Address
}

func (v *stagingValidator) resolveAddressContractNames(address common.Address) ([]string, error) {
Expand Down
61 changes: 61 additions & 0 deletions internal/migrate/staging_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,65 @@ func Test_StagingValidator(t *testing.T) {
err := validator.ValidateContractUpdate(location, sourceCodeLocation, []byte(newContract))
require.NoError(t, err)
})

t.Run("resolves account access correctly", func(t *testing.T) {
location := common.NewAddressLocation(nil, common.Address{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, "Test")
sourceCodeLocation := common.StringLocation("./Test.cdc")
oldContract := `
import ImpContract from 0x01
pub contract Test {
pub fun test() {}
}`
newContract := `
import ImpContract from 0x01
access(all) contract Test {
access(all) fun test() {}
init() {
ImpContract.test()
}
}`
impContract := `
access(all) contract ImpContract {
access(account) fun test() {}
init() {}
}`
mockScriptResultString, err := cadence.NewString(impContract)
require.NoError(t, err)

mockAccount := &flow.Account{
Address: flow.HexToAddress("01"),
Balance: 1000,
Keys: nil,
Contracts: map[string][]byte{
"Test": []byte(oldContract),
},
}

// setup mocks
require.NoError(t, rw.WriteFile(sourceCodeLocation.String(), []byte(newContract), 0o644))
srv.GetAccount.Run(func(args mock.Arguments) {
require.Equal(t, flow.HexToAddress("01"), args.Get(1).(flow.Address))
}).Return(mockAccount, nil)
srv.Network.Return(config.Network{
Name: "testnet",
}, nil)
srv.ExecuteScript.Run(func(args mock.Arguments) {
script := args.Get(1).(flowkit.Script)

assert.Equal(t, templates.GenerateGetStagedContractCodeScript(MigrationContractStagingAddress("testnet")), script.Code)

assert.Equal(t, 2, len(script.Args))
actualContractAddressArg, actualContractNameArg := script.Args[0], script.Args[1]

contractName, _ := cadence.NewString("ImpContract")
contractAddr := cadence.NewAddress(flow.HexToAddress("01"))
assert.Equal(t, contractName, actualContractNameArg)
assert.Equal(t, contractAddr, actualContractAddressArg)
}).Return(cadence.NewOptional(mockScriptResultString), nil)

// validate
validator := newStagingValidator(srv.Mock, state)
err = validator.ValidateContractUpdate(location, sourceCodeLocation, []byte(newContract))
require.NoError(t, err)
})
}

0 comments on commit 97c69f7

Please sign in to comment.