/* | |
* Copyright (c) Robert Bosch GmbH. All rights reserved. | |
*/ | |
package org.eclipse.blockchain.core; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import org.eclipse.core.resources.IFile; | |
import org.eclipse.core.resources.IProject; | |
import org.eclipse.core.resources.IProjectDescription; | |
import org.eclipse.core.resources.IResource; | |
import org.eclipse.core.resources.IResourceVisitor; | |
import org.eclipse.core.runtime.CoreException; | |
/** | |
* @author ADG5COB | |
*/ | |
public class EthereumProject { | |
private final String projectLocation; | |
private IProjectDescription projectDescription = null; | |
private IProject project = null; | |
private IFile firstMatchingSolFile = null; | |
private final List<String> projectNatures = new ArrayList<>(); | |
private final Set<IResource> compiledSolidityFiles = new HashSet<>(); | |
/** | |
* Environment based deployment model. Currently there are 2 types 1. EnvironmentType.GETH_CLIENT 2. | |
* EnvironmentType.EMBEDDED_EVM | |
*/ | |
private final Map<String, Map<IResource, DeploymentModel>> environmentBasedDeployedModel = new HashMap<>(); | |
/** | |
* @param projectLocation - The project location | |
*/ | |
public EthereumProject(final String projectLocation) { | |
this.projectLocation = projectLocation; | |
} | |
void addCompiledSolidityFiles(final IResource solidityFile) { | |
this.compiledSolidityFiles.add(solidityFile); | |
} | |
/** | |
* @return - | |
*/ | |
public Set<IResource> getCompiledSolidityFiles() { | |
return this.compiledSolidityFiles; | |
} | |
/** | |
* @param project - The IProject for this project | |
*/ | |
public void setProject(final IProject project) { | |
this.project = project; | |
} | |
/** | |
* @return - The IProject for this project | |
*/ | |
public IProject getProject() { | |
return this.project; | |
} | |
/** | |
* @return - Project Location | |
*/ | |
public String getProjectLocation() { | |
return this.projectLocation; | |
} | |
/** | |
* @param projectDescription - Project description | |
*/ | |
public void setProjectDescription(final IProjectDescription projectDescription) { | |
this.projectDescription = projectDescription; | |
} | |
/** | |
* @return - Project description | |
*/ | |
public IProjectDescription getProjectDescription() { | |
return this.projectDescription; | |
} | |
void addDefaultNatures() { | |
setProjectNatures(EthereumProjectNature.ETHEREUM_NATURE); | |
} | |
/** | |
* The ethereum project name | |
* | |
* @return - project name | |
*/ | |
public String getProjectName() { | |
return this.project != null ? this.project.getName() : ""; | |
} | |
/** | |
* | |
*/ | |
public void setProjectBuilders() {} | |
/** | |
* @param nature - | |
*/ | |
public void setProjectNatures(final String nature) { | |
this.projectNatures.add(nature); | |
} | |
/** | |
* @return - | |
*/ | |
public String[] getNatureIds() { | |
return this.projectNatures.toArray(new String[this.projectNatures.size()]); | |
} | |
/** | |
* @return - | |
* @throws CoreException - | |
*/ | |
IFile getSolFile() throws CoreException { | |
if (this.project != null) { | |
this.project.accept(new IResourceVisitor() { | |
@Override | |
public boolean visit(final IResource resource) throws CoreException { | |
if ((resource instanceof IFile) && (resource.getFileExtension() != null) && | |
resource.getFileExtension().equals("sol")) { | |
EthereumProject.this.firstMatchingSolFile = (IFile) resource; | |
return false; | |
} | |
return true; | |
} | |
}); | |
} | |
return this.firstMatchingSolFile; | |
} | |
/** | |
* Creates a deployment model for the deployed Smart contract | |
* | |
* @param contractAddress - The blockchain contract address | |
* @param scFile - smart contract file | |
* @param lastUsedAccount - the account that was used for interacting/deploying the smart contract | |
*/ | |
public void createDeploymentModel(final String contractAddress, final IResource scFile, final String lastUsedAccount, | |
final String environment) { | |
DeploymentModel dm = new DeploymentModel(contractAddress, scFile, lastUsedAccount); | |
Map<IResource, DeploymentModel> depModelMap = | |
this.environmentBasedDeployedModel.computeIfAbsent(environment, m -> new HashMap<>()); | |
depModelMap.put(scFile, dm); | |
} | |
/** | |
* @param scFile - | |
* @return | |
*/ | |
public String getLastUsedAccount(final IResource scFile, final String environment) { | |
Map<IResource, DeploymentModel> map = this.environmentBasedDeployedModel.get(environment); | |
if ((map != null) && map.containsKey(scFile)) { | |
return map.get(scFile).getLastUsedAccount(); | |
} | |
return ""; | |
} | |
/** | |
* @param scFile | |
* @param accountDetails | |
*/ | |
public void updateLastUsedAccount(final IResource scFile, final String accountDetails, final String environment) { | |
Map<IResource, DeploymentModel> map = this.environmentBasedDeployedModel.get(environment); | |
if ((map != null) && map.containsKey(scFile)) { | |
map.get(scFile).setLastUsedAccount(accountDetails); | |
} | |
} | |
/** | |
* @param scFile | |
* @return | |
*/ | |
public String getContractAddress(final IResource scFile, final String environment) { | |
Map<IResource, DeploymentModel> map = this.environmentBasedDeployedModel.get(environment); | |
if ((map != null) && map.containsKey(scFile)) { | |
return map.get(scFile).getContractAddress(); | |
} | |
return ""; | |
} | |
void removeDeploymentModelForResource(final IResource resource, final String environment) { | |
Map<IResource, DeploymentModel> map = this.environmentBasedDeployedModel.get(environment); | |
if ((map != null) && map.containsKey(resource)) { | |
map.remove(resource); | |
} | |
} | |
/** | |
* @param scFile | |
* @return | |
*/ | |
public boolean isSCDeployed(final IResource scFile, final String environment) { | |
Map<IResource, DeploymentModel> map = this.environmentBasedDeployedModel.get(environment); | |
return (map != null) && map.containsKey(scFile); | |
} | |
} | |
/** | |
* When a smart contract is deployed into the blockchain a corresponding DeploymentModel has to be created which will | |
* maintain the details of the deployed smart contract | |
* | |
* @author ADG5COB | |
*/ | |
class DeploymentModel { | |
private final String contractAddress; | |
private final IResource scFile; | |
private String lastUsedAccount; | |
DeploymentModel(final String contractAddress, final IResource scFile, final String lastUsedAccount) { | |
this.contractAddress = contractAddress; | |
this.scFile = scFile; | |
this.lastUsedAccount = lastUsedAccount; | |
} | |
String getContractAddress() { | |
return this.contractAddress; | |
} | |
IResource getScFile() { | |
return this.scFile; | |
} | |
String getLastUsedAccount() { | |
return this.lastUsedAccount; | |
} | |
void setLastUsedAccount(final String lastUsedAccount) { | |
this.lastUsedAccount = lastUsedAccount; | |
} | |
} |