Skip to content

Commit

Permalink
Add message if conflicting contract on dependency install
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed May 2, 2024
1 parent 3b93072 commit 0c72153
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions internal/dependencymanager/dependencyinstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
type categorizedLogs struct {
fileSystemActions []string
stateUpdates []string
issues []string
}

func (cl *categorizedLogs) LogAll(logger output.Logger) {
Expand All @@ -59,19 +60,27 @@ func (cl *categorizedLogs) LogAll(logger output.Logger) {
if len(cl.fileSystemActions) > 0 {
logger.Info(util.MessageWithEmojiPrefix("🗃️", "File System Actions:"))
for _, msg := range cl.fileSystemActions {
logger.Info(util.MessageWithEmojiPrefix("✅", msg))
logger.Info(msg)
}
logger.Info("") // Add a line break after the section
}

if len(cl.stateUpdates) > 0 {
logger.Info(util.MessageWithEmojiPrefix("💾", "State Updates:"))
for _, msg := range cl.stateUpdates {
logger.Info(util.MessageWithEmojiPrefix("✅", msg))
logger.Info(msg)
}
logger.Info("") // Add a line break after the section
}

if len(cl.issues) > 0 {
logger.Info(util.MessageWithEmojiPrefix("⚠️", "Issues:"))
for _, msg := range cl.issues {
logger.Info(msg)
}
logger.Info("")
}

if len(cl.fileSystemActions) == 0 && len(cl.stateUpdates) == 0 {
logger.Info(util.MessageWithEmojiPrefix("👍", "Zero changes were made. Everything looks good."))
}
Expand Down Expand Up @@ -280,7 +289,8 @@ func (di *DependencyInstaller) handleFileSystem(contractAddr, contractName, cont
return fmt.Errorf("failed to create contract file: %w", err)
}

di.logs.fileSystemActions = append(di.logs.fileSystemActions, fmt.Sprintf("%s from %s on %s installed", contractName, contractAddr, networkName))
msg := util.MessageWithEmojiPrefix("✅️", fmt.Sprintf("Contract %s from %s on %s installed", contractName, contractAddr, networkName))
di.logs.fileSystemActions = append(di.logs.fileSystemActions, msg)
}

return nil
Expand All @@ -297,6 +307,20 @@ func isCoreContract(contractName string) bool {
return false
}

// checkForContractConflicts checks if a contract with the same name already exists in the state and adds a warning
func (di *DependencyInstaller) checkForContractConflicts(contractName string) error {
_, err := di.State.Contracts().ByName(contractName)
if err != nil {
return nil
} else {
if !isCoreContract(contractName) {
msg := util.MessageWithEmojiPrefix("❌", fmt.Sprintf("Contract named %s already exists in flow.json", contractName))
di.logs.issues = append(di.logs.issues, msg)
}
return nil
}
}

func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, assignedName, contractName string, program *project.Program) error {
hash := sha256.New()
hash.Write(program.CodeWithUnprocessedImports())
Expand Down Expand Up @@ -324,7 +348,14 @@ func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, as
}
}

err := di.handleFileSystem(contractAddr, contractName, contractData, networkName)
//// This needs to happen before dependency state is updated
err := di.checkForContractConflicts(assignedName)
if err != nil {
di.Logger.Error(fmt.Sprintf("Error checking for contract conflicts: %v", err))
return err
}

err = di.handleFileSystem(contractAddr, contractName, contractData, networkName)
if err != nil {
return fmt.Errorf("error handling file system: %w", err)
}
Expand All @@ -343,7 +374,8 @@ func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, as
return err
}

di.logs.stateUpdates = append(di.logs.stateUpdates, fmt.Sprintf("%s added to emulator deployments", contractName))
msg := util.MessageWithEmojiPrefix("✅", fmt.Sprintf("%s added to emulator deployments", contractName))
di.logs.stateUpdates = append(di.logs.stateUpdates, msg)
}

// If the contract is not a core contract and the user does not want to skip aliasing, then prompt for an alias
Expand All @@ -354,7 +386,8 @@ func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, as
return err
}

di.logs.stateUpdates = append(di.logs.stateUpdates, fmt.Sprintf("Alias added for %s on %s", contractName, networkName))
msg := util.MessageWithEmojiPrefix("✅", fmt.Sprintf("Alias added for %s on %s", contractName, networkName))
di.logs.stateUpdates = append(di.logs.stateUpdates, msg)
}

return nil
Expand Down Expand Up @@ -424,7 +457,8 @@ func (di *DependencyInstaller) updateDependencyState(networkName, contractAddres
di.State.Contracts().AddDependencyAsContract(dep, networkName)

if isNewDep {
di.logs.stateUpdates = append(di.logs.stateUpdates, fmt.Sprintf("%s added to flow.json", dep.Name))
msg := util.MessageWithEmojiPrefix("✅", fmt.Sprintf("%s added to flow.json", dep.Name))
di.logs.stateUpdates = append(di.logs.stateUpdates, msg)
}

return nil
Expand Down

0 comments on commit 0c72153

Please sign in to comment.