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

Sync Stable Cadence branch with master #2701

Merged
merged 27 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a93d153
Follow links for validation during storage iteration
SupunS Jul 21, 2023
5cf6009
Temporarily remove deprecations for existing capability API
m-Peter Jul 21, 2023
ae4c521
Handle wrapped errors returned from FVM
SupunS Jul 21, 2023
a32ac46
Add test for error mis-clasification
SupunS Jul 21, 2023
abb7402
Wrap host env errors with external errors
SupunS Jul 26, 2023
fe2ba63
Update go.cap
SupunS Jul 27, 2023
5ea8284
Merge pull request #2683 from onflow/supun/handle-external-errors
SupunS Aug 1, 2023
e5694e7
Pass down the location range
SupunS Aug 1, 2023
b127194
Merge branch 'master' of https://github.com/onflow/cadence into supun…
SupunS Aug 1, 2023
7655a90
Merge pull request #2676 from onflow/supun/fix-storage-iteration
SupunS Aug 1, 2023
13d2513
Fix error on reference creation with invalid type
SupunS Aug 2, 2023
c8833d2
Merge pull request #2687 from onflow/supun/fix-type-error
SupunS Aug 2, 2023
62d6ec8
Merge pull request #2673 from m-Peter/temporarily-remove-existing-cap…
turbolent Aug 2, 2023
48e73aa
remove unused code
turbolent Aug 3, 2023
c373e38
provide location ranges
turbolent Aug 3, 2023
937ff73
ensure errors with location, position, and suggested fixes, can produ…
turbolent Aug 3, 2023
d278372
fix error types
turbolent Aug 3, 2023
e010922
Merge pull request #2692 from onflow/bastian/cleanup
turbolent Aug 3, 2023
440fb31
fix run for push
turbolent Aug 3, 2023
8d83423
update to Go 1.20
turbolent Aug 3, 2023
f8d763e
improve random value generation, use local random generator
turbolent Aug 3, 2023
9962b85
v0.40.0
turbolent Aug 3, 2023
d0f1acc
Merge pull request #2694 from onflow/bastian/go-1.20
turbolent Aug 3, 2023
96403f2
Merge pull request #2695 from onflow/release/v0.40.0
turbolent Aug 3, 2023
06d81ef
Merge branch 'master' into bastian/sync-stable-cadence-5
turbolent Aug 8, 2023
ca4d44b
Update storage iteration value check + tests
SupunS Aug 8, 2023
8cce6bb
Merge pull request #2702 from onflow/supun/update-storage-iter
turbolent Aug 8, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
- 'v**'

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
- 'v**'

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
- 'v**'

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compatibility-check-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ on:
type: string

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ inputs.base-branch || github.run_id }}-${{ inputs.chain }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/compatibility-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ on:
- 'v**'

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ inputs.base || github.run_id }}
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
echo "branch=`(echo "${{ github.event.pull_request.head.sha }}")`" >> $GITHUB_OUTPUT
echo "base=`(echo "${{ github.base_ref }}")`" >> $GITHUB_OUTPUT
else
echo "repo=`(echo "${{ inputs.repo || 'onflow/cadence' }}")`" >> $GITHUB_OUTPUT
echo "repo=`(echo "${{ inputs.repo || github.repository }}")`" >> $GITHUB_OUTPUT
echo "branch=`(echo "${{ inputs.branch }}")`" >> $GITHUB_OUTPUT
echo "base=`(echo "${{ inputs.base }}")`" >> $GITHUB_OUTPUT
fi
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/downstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
- 'v**'

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
Expand All @@ -40,7 +40,7 @@ jobs:
run: sh crypto_setup.sh

- name: Update Cadence
run: go mod edit -replace github.com/onflow/cadence=github.com/${{ github.event.pull_request.head.repo.full_name }}@${{ github.event.pull_request.head.sha }}
run: go mod edit -replace github.com/onflow/cadence=github.com/${{ github.event.pull_request.head.repo.full_name || github.repository }}@${{ github.event.pull_request.head.sha || github.sha }}

- name: Tidy up
run: go mod tidy
Expand All @@ -65,7 +65,7 @@ jobs:
cache: true

- name: Update Cadence
run: go mod edit -replace github.com/onflow/cadence=github.com/${{ github.event.pull_request.head.repo.full_name }}@${{ github.event.pull_request.head.sha }}
run: go mod edit -replace github.com/onflow/cadence=github.com/${{ github.event.pull_request.head.repo.full_name || github.repository }}@${{ github.event.pull_request.head.sha || github.sha }}

- name: Tidy up
run: go mod tidy
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ on:
default: 'master'

env:
GO_VERSION: '1.19.2'
GO_VERSION: '1.20'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
Expand Down
1 change: 1 addition & 0 deletions go.cap
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ github.com/texttheater/golang-levenshtein/levenshtein (file)
github.com/zeebo/blake3/internal/consts (file)
golang.org/x/sys/unix (runtime, syscall)
golang.org/x/xerrors (runtime)
github.com/onflow/cadence/runtime/interpreter (runtime)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/onflow/cadence

go 1.18
go 1.20

require (
github.com/bits-and-blooms/bitset v1.2.2
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/cadence-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onflow/cadence-parser",
"version": "0.39.14",
"version": "0.40.0",
"description": "The Cadence parser",
"homepage": "https://github.com/onflow/cadence",
"repository": {
Expand Down
20 changes: 19 additions & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@
errors.WrapPanic(func() {
res, err = e.runtimeInterface.ResolveLocation(identifiers, location)
})

if err != nil {
err = interpreter.WrappedExternalError(err)
}

Check warning on line 430 in runtime/environment.go

View check run for this annotation

Codecov / codecov/patch

runtime/environment.go#L429-L430

Added lines #L429 - L430 were not covered by tests
return
}
}
Expand Down Expand Up @@ -553,6 +557,11 @@
if panicErr != nil {
return nil, panicErr
}

if err != nil {
err = interpreter.WrappedExternalError(err)
}

return
})
})
Expand All @@ -570,6 +579,11 @@
code, err = e.runtimeInterface.GetCode(location)
})
}

if err != nil {
err = interpreter.WrappedExternalError(err)
}

Check warning on line 585 in runtime/environment.go

View check run for this annotation

Codecov / codecov/patch

runtime/environment.go#L584-L585

Added lines #L584 - L585 were not covered by tests

return
}

Expand Down Expand Up @@ -738,6 +752,10 @@
errors.WrapPanic(func() {
uuid, err = e.runtimeInterface.GenerateUUID()
})

if err != nil {
err = interpreter.WrappedExternalError(err)
}

Check warning on line 758 in runtime/environment.go

View check run for this annotation

Codecov / codecov/patch

runtime/environment.go#L757-L758

Added lines #L757 - L758 were not covered by tests
return
}
}
Expand Down Expand Up @@ -914,7 +932,7 @@
err = e.runtimeInterface.MeterComputation(compKind, intensity)
})
if err != nil {
panic(err)
panic(interpreter.WrappedExternalError(err))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@
err = emitEvent(exportedEvent)
})
if err != nil {
panic(err)
panic(interpreter.WrappedExternalError(err))

Check warning on line 84 in runtime/events.go

View check run for this annotation

Codecov / codecov/patch

runtime/events.go#L84

Added line #L84 was not covered by tests
}
}
20 changes: 20 additions & 0 deletions runtime/interpreter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package interpreter

import (
"fmt"
"runtime"
"strings"

"github.com/onflow/cadence/runtime/ast"
Expand Down Expand Up @@ -959,3 +960,22 @@ func (RecursiveTransferError) IsUserError() {}
func (RecursiveTransferError) Error() string {
return "recursive transfer of value"
}

func WrappedExternalError(err error) error {
switch err := err.(type) {
case
// If the error is a go-runtime error, don't wrap.
// These are crashers.
runtime.Error,

// If the error is already a cadence error, then avoid redundant wrapping.
errors.InternalError,
errors.UserError,
errors.ExternalError,
Error:
return err

default:
return errors.NewExternalError(err)
}
}
99 changes: 79 additions & 20 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,20 +513,8 @@

func (interpreter *Interpreter) RecoverErrors(onError func(error)) {
if r := recover(); r != nil {
var err error

// Recover all errors, because interpreter can be directly invoked by FVM.
switch r := r.(type) {
case Error,
errors.ExternalError,
errors.InternalError,
errors.UserError:
err = r.(error)
case error:
err = errors.NewUnexpectedErrorFromCause(r)
default:
err = errors.NewUnexpectedError("%s", r)
}
err := asCadenceError(r)

// if the error is not yet an interpreter error, wrap it
if _, ok := err.(Error); !ok {
Expand Down Expand Up @@ -556,6 +544,31 @@
}
}

func asCadenceError(r any) error {
err, isError := r.(error)
if !isError {
return errors.NewUnexpectedError("%s", r)
}

Check warning on line 551 in runtime/interpreter/interpreter.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter.go#L550-L551

Added lines #L550 - L551 were not covered by tests

rootError := err

for {
switch typedError := err.(type) {
case Error,
errors.ExternalError,
errors.InternalError,
errors.UserError:
return typedError
case xerrors.Wrapper:
err = typedError.Unwrap()
case error:
return errors.NewUnexpectedErrorFromCause(rootError)
default:
return errors.NewUnexpectedErrorFromCause(rootError)

Check warning on line 567 in runtime/interpreter/interpreter.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter.go#L566-L567

Added lines #L566 - L567 were not covered by tests
}
}
}

func (interpreter *Interpreter) CallStack() []Invocation {
return interpreter.SharedState.callStack.Invocations[:]
}
Expand Down Expand Up @@ -4024,12 +4037,18 @@
}()

for key, value := storageIterator.Next(); key != nil && value != nil; key, value = storageIterator.Next() {

staticType := value.StaticType(inter)

// Perform a forced type loading to see if the underlying type is not broken.
// Perform a forced value de-referencing to see if the associated type is not broken.
// If broken, skip this value from the iteration.
typeError := inter.checkTypeLoading(staticType)
if typeError != nil {
valueError := inter.checkValue(
value,
staticType,
invocation.LocationRange,
)

if valueError != nil {
continue
}

Expand Down Expand Up @@ -4079,14 +4098,19 @@
)
}

func (interpreter *Interpreter) checkTypeLoading(staticType StaticType) (typeError error) {
func (interpreter *Interpreter) checkValue(
value Value,
staticType StaticType,
locationRange LocationRange,
) (valueError error) {

defer func() {
if r := recover(); r != nil {
rootError := r
for {
switch err := r.(type) {
case errors.UserError, errors.ExternalError:
typeError = err.(error)
valueError = err.(error)
return
case xerrors.Wrapper:
r = err.Unwrap()
Expand All @@ -4097,8 +4121,43 @@
}
}()

// Here it is only interested in whether the type can be properly loaded.
_, typeError = interpreter.ConvertStaticToSemaType(staticType)
// Here, the value at the path could be either:
// 1) The actual stored value (storage path)
// 2) A capability to the value at the storage (private/public paths)

if idCapability, ok := value.(*IDCapabilityValue); ok {
// If, the value is a capability, try to load the value at the capability target.
// However, borrow type is not statically known.
// So take the borrow type from the value itself

// Capability values always have a `CapabilityStaticType` static type.
borrowType := staticType.(CapabilityStaticType).BorrowType

var borrowSemaType sema.Type
borrowSemaType, valueError = interpreter.ConvertStaticToSemaType(borrowType)
if valueError != nil {
return valueError
}

referenceType, ok := borrowSemaType.(*sema.ReferenceType)
if !ok {
panic(errors.NewUnreachableError())

Check warning on line 4144 in runtime/interpreter/interpreter.go

View check run for this annotation

Codecov / codecov/patch

runtime/interpreter/interpreter.go#L4144

Added line #L4144 was not covered by tests
}

_ = interpreter.SharedState.Config.IDCapabilityCheckHandler(
interpreter,
locationRange,
idCapability.Address,
idCapability.ID,
referenceType,
referenceType,
)

} else {
// For all other values, trying to load the type is sufficient.
// Here it is only interested in whether the type can be properly loaded.
_, valueError = interpreter.ConvertStaticToSemaType(staticType)
}

return
}
Expand Down
8 changes: 0 additions & 8 deletions runtime/interpreter/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ import (
"github.com/onflow/cadence/runtime/common"
)

// LocationPosition defines a position in the source of the import tree.
// The Location defines the script within the import tree, the Position
// defines the row/colum within the source of that script.
type LocationPosition struct {
Location common.Location
Position ast.Position
}

// LocationRange defines a range in the source of the import tree.
// The Position defines the script within the import tree, the Range
// defines the start/end position within the source of that script.
Expand Down
Loading
Loading