Skip to content

Commit

Permalink
Sync with the latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Feb 10, 2021
1 parent e9ad098 commit d09944b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions runtime/contract_update_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/parser2"
)

type ContractUpdateValidator struct {
Expand All @@ -32,28 +33,24 @@ type ContractUpdateValidator struct {
entryPointName string
currentDeclName string
currentFieldName string
visitedDecls map[string]bool
visited map[string]bool
}

// ContractUpdateValidator should implement ast.TypeEqualityChecker
var _ ast.TypeEqualityChecker = &ContractUpdateValidator{}

func NewContractUpdateValidator(
runtime *interpreterRuntime,
context Context,
existingCode []byte,
newProgram *ast.Program) *ContractUpdateValidator {
func NewContractUpdateValidator(existingCode []byte, newProgram *ast.Program) *ContractUpdateValidator {

oldProgram, err := runtime.parse(existingCode, context)
oldProgram, err := parser2.ParseProgram(string(existingCode))
if err != nil {
// Not a user error. Hence panic
panic(err)
}

oldCompDecls := oldProgram.CompositeDeclarations()
oldInterfaceDecls := oldProgram.InterfaceDeclarations()
var oldDecl ast.Declaration

var oldDecl ast.Declaration
if len(oldCompDecls) == 1 {
oldDecl = oldCompDecls[0]
} else if len(oldInterfaceDecls) == 1 {
Expand All @@ -64,8 +61,8 @@ func NewContractUpdateValidator(

newCompDecls := newProgram.CompositeDeclarations()
newInterfaceDecls := newProgram.InterfaceDeclarations()
var newDecl ast.Declaration

var newDecl ast.Declaration
if len(newCompDecls) == 1 {
newDecl = newCompDecls[0]
} else if len(newInterfaceDecls) == 1 {
Expand All @@ -77,7 +74,7 @@ func NewContractUpdateValidator(
return &ContractUpdateValidator{
oldDeclaration: oldDecl,
newDeclaration: newDecl,
visitedDecls: map[string]bool{}}
visited: map[string]bool{}}
}

func (validator *ContractUpdateValidator) Validate() error {
Expand All @@ -102,13 +99,13 @@ func (validator *ContractUpdateValidator) checkDeclarationUpdatability(
validator.currentDeclName = parentDeclName
}()

// If the same same decl is already visited, then do not check again.
// If the same decl is already visited, then do not check again.
// This also avoids getting stuck on circular dependencies between composite decls.
if validator.visitedDecls[validator.currentDeclName] {
if validator.visited[validator.currentDeclName] {
return nil
}

validator.visitedDecls[validator.currentDeclName] = true
validator.visited[validator.currentDeclName] = true

oldFields := getDeclarationMembers(oldDeclaration).Fields()
newFields := getDeclarationMembers(newDeclaration).Fields()
Expand Down Expand Up @@ -145,7 +142,10 @@ func (validator *ContractUpdateValidator) checkDeclarationUpdatability(
return nil
}

func (validator *ContractUpdateValidator) visitField(newField *ast.FieldDeclaration, oldFiledTypes map[string]ast.Type) error {
func (validator *ContractUpdateValidator) visitField(
newField *ast.FieldDeclaration,
oldFiledTypes map[string]ast.Type) error {

prevFieldName := validator.currentFieldName
validator.currentFieldName = newField.Identifier.Identifier
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ func (r *interpreterRuntime) newAuthAccountContractsChangeFunction(
}

if isUpdate {
validator := NewContractUpdateValidator(r, context, existingCode, checker.Program)
validator := NewContractUpdateValidator(existingCode, program.Program)
err := validator.Validate()
if err != nil {
panic(&InvalidContractDeploymentError{
Expand Down

0 comments on commit d09944b

Please sign in to comment.