Skip to content

Commit

Permalink
Merge pull request #2701 from onflow/bastian/sync-stable-cadence-5
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Aug 8, 2023
2 parents 9bdde37 + 8cce6bb commit 07f5125
Show file tree
Hide file tree
Showing 39 changed files with 3,530 additions and 1,728 deletions.
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 @@ func (e *interpreterEnvironment) newLocationHandler() sema.LocationHandlerFunc {
errors.WrapPanic(func() {
res, err = e.runtimeInterface.ResolveLocation(identifiers, location)
})

if err != nil {
err = interpreter.WrappedExternalError(err)
}
return
}
}
Expand Down Expand Up @@ -553,6 +557,11 @@ func (e *interpreterEnvironment) getProgram(
if panicErr != nil {
return nil, panicErr
}

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

return
})
})
Expand All @@ -570,6 +579,11 @@ func (e *interpreterEnvironment) getCode(location common.Location) (code []byte,
code, err = e.runtimeInterface.GetCode(location)
})
}

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

return
}

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

if err != nil {
err = interpreter.WrappedExternalError(err)
}
return
}
}
Expand Down Expand Up @@ -914,7 +932,7 @@ func (e *interpreterEnvironment) newOnMeterComputation() interpreter.OnMeterComp
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 @@ func emitEventFields(
err = emitEvent(exportedEvent)
})
if err != nil {
panic(err)
panic(interpreter.WrappedExternalError(err))
}
}
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) InvokeTransaction(index int, arguments ...Value)

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 (interpreter *Interpreter) RecoverErrors(onError func(error)) {
}
}

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

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)
}
}
}

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

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) newStorageIterationFunction(
)
}

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 @@ func (interpreter *Interpreter) checkTypeLoading(staticType StaticType) (typeErr
}
}()

// 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())
}

_ = 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

0 comments on commit 07f5125

Please sign in to comment.