blob: b6ff26932cf0063aa60760950504894d02b56dfa [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2020 RBEI and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v. 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Adhith Gopal - Initial API and Implementation
*******************************************************************************/
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.blockchain.core.interfaces.IEthereumProject1;
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;
/**
* The EthereumProject is a smart contract project representation
*/
public class EthereumProject1 implements IEthereumProject1 {
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 List<String> projectBuilders = 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 EthereumProject1(final String projectLocation) {
this.projectLocation = projectLocation;
}
/**
*
* {@inheritDoc}
*/
@Override
public void addCompiledSolidityFiles(final IResource solidityFile) {
this.compiledSolidityFiles.add(solidityFile);
}
/**
*
* {@inheritDoc}
*/
@Override
public Set<IResource> getCompiledSolidityFiles() {
return this.compiledSolidityFiles;
}
/**
*
* {@inheritDoc}
*/
@Override
public void setProject(final IProject project) {
this.project = project;
}
/**
*
* {@inheritDoc}
*/
@Override
public IProject getProject() {
return this.project;
}
/**
*
* {@inheritDoc}
*/
@Override
public String getProjectLocation() {
return this.projectLocation;
}
/**
*
* {@inheritDoc}
*/
@Override
public void setProjectDescription(final IProjectDescription projectDescription) {
this.projectDescription = projectDescription;
}
/**
*
* {@inheritDoc}
*/
@Override
public IProjectDescription getProjectDescription() {
return this.projectDescription;
}
/**
*
* {@inheritDoc}
*/
@Override
public void addDefaultNatures() {
setProjectNatures(EthereumNature.ETHEREUM_NATURE);
}
/**
* {@inheritDoc}
*/
@Override
public void addDefaultBuilders() {
setProjectBuilders(EthereumBuilder.ETHEREUM_BUILDER);
}
/**
*
* {@inheritDoc}
*/
@Override
public String getProjectName() {
return this.project != null ? this.project.getName() : "";
}
/**
*
* {@inheritDoc}
*/
@Override
public void setProjectBuilders(final String builderId) {
this.projectBuilders.add(builderId);
}
/**
*
* {@inheritDoc}
*/
@Override
public void setProjectNatures(final String nature) {
this.projectNatures.add(nature);
}
/**
*
* {@inheritDoc}
*/
@Override
public String[] getNatureIds() {
return this.projectNatures.toArray(new String[this.projectNatures.size()]);
}
/**
* {@inheritDoc}
*/
@Override
public String[] getBuilderIds() {
return this.projectBuilders.toArray(new String[this.projectBuilders.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")) {
EthereumProject1.this.firstMatchingSolFile = (IFile) resource;
return false;
}
return true;
}
});
}
return this.firstMatchingSolFile;
}
/**
*
* {@inheritDoc}
*/
@Override
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);
}
/**
*
* {@inheritDoc}
*/
@Override
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 "";
}
/**
*
* {@inheritDoc}
*/
@Override
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);
}
}
/**
*
* {@inheritDoc}
*/
@Override
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 "";
}
/**
*
* {@inheritDoc}
*/
@Override
public 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);
}
}
/**
*
* {@inheritDoc}
*/
@Override
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;
}
}