Skip to content

Commit

Permalink
Merge pull request #119 from hyperledger/feature/smart-contract
Browse files Browse the repository at this point in the history
Feature/smart contract
  • Loading branch information
motxx committed Jan 13, 2017
2 parents 9b5afd0 + ea83964 commit cdae96d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
37 changes: 31 additions & 6 deletions core/infra/smart_contract/jvm/java_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ namespace smart_contract {
return nullptr;
}

std::cout << "-Djava.class.path=" + std::string(getenv("IROHA_HOME")) +
"/smart_contract/" + contractName <<"/ "<< contractName.c_str() << std::endl;
std::string java_command = "-Djava.class.path=" + std::string(getenv("IROHA_HOME")) + "/smart_contract/" + contractName + "/";

std::cout << java_command.c_str() << " " << contractName.c_str() << std::endl;

JavaVMOption options[3];
options[0].optionString = const_cast<char*>(("-Djava.class.path=" + std::string(getenv("IROHA_HOME")) + "/smart_contract").c_str());
options[0].optionString = const_cast<char*>( java_command.c_str() );
options[1].optionString = const_cast<char*>("-Djava.security.manager");
options[2].optionString = const_cast<char*>("-Djava.security.policy=policy.txt");

Expand All @@ -60,7 +61,7 @@ namespace smart_contract {
return nullptr;
}

jclass cls = env->FindClass("SampleCurrency/SampleCurrency");// (contractName+"/"+contractName).c_str());
jclass cls = env->FindClass( (contractName).c_str() );// (contractName+"/"+contractName).c_str());
if (cls == nullptr) {
std::cout << "could not found class : " << contractName << std::endl;
return nullptr;
Expand All @@ -84,23 +85,47 @@ namespace smart_contract {
);
}



void execFunction(
const std::unique_ptr<JavaContext> &context,
std::string functionName,
std::unordered_map<std::string, std::string> params
) {
jobject jmap = JavaMakeMap( context->env, params );

jmethodID mid = context->env->GetStaticMethodID(context->jClass, functionName.c_str(), "(Ljava/util/Map;)V");
jmethodID mid = context->env->GetStaticMethodID(context->jClass, functionName.c_str(), "(Ljava/util/HashMap;)V");
if (mid == nullptr) {
std::cout << "could not get method : " << functionName << std::endl;
return;
}
context->env->CallVoidMethod(context->jObject, mid);

context->env->CallVoidMethod(context->jObject, mid, jmap );

auto res = context->jvm->DestroyJavaVM();
if (res) {
std::cout << "could not destroy JavaVM : " << res << std::endl;
}
}


JNIEXPORT jobject JNICALL JavaMakeMap(JNIEnv *env, std::unordered_map<std::string,std::string> mMap) {
env->PushLocalFrame(256); // fix for local references
jclass hashMapClass= env->FindClass( "java/util/HashMap" );
jmethodID hashMapInit = env->GetMethodID( hashMapClass, "<init>", "(I)V");
jobject hashMapObj = env->NewObject( hashMapClass, hashMapInit, mMap.size());
jmethodID hashMapOut = env->GetMethodID( hashMapClass, "put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

for (auto it : mMap)
{
env->CallObjectMethod( hashMapObj, hashMapOut,
env->NewStringUTF(it.first.c_str()),
env->NewStringUTF(it.second.c_str()));
}

env->PopLocalFrame(hashMapObj);
return hashMapObj;
}

};
1 change: 1 addition & 0 deletions core/infra/smart_contract/jvm/java_virtual_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace smart_contract {
std::unordered_map<std::string, std::string> params
);

JNIEXPORT jobject JNICALL JavaMakeMap(JNIEnv *env, std::unordered_map<std::string,std::string> mMap);
}

#endif //IROHA_JAVA_VIRTUAL_MACHINE_HPP_HPP
14 changes: 12 additions & 2 deletions smart_contract/SampleCurrency/SampleCurrency.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
limitations under the License.
*/

import java.util.Map;
import java.util.HashMap;

// no package declaration
public class SampleCurrency {

public static void remit(Map<String,String> params){
public static void put( String param ) {
System.out.println("Hello in JAVA! in add");
System.out.println("vvvvvvvv param vvvvvvvv");
System.out.println( param );
}

public static void remit(HashMap<String,String> params){
System.out.println("Hello in JAVA! in contract");
System.out.println("vvvvvvvv params vvvvvvvv");
for( String key : params.keySet() ) {
System.out.println( key + " : " + params.get( key ) );
}
}

public static void main(String[] argv){
Expand Down
2 changes: 2 additions & 0 deletions test/smart_contract/java_vm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ TEST(SmartContract, createVM){
std::string contractName = "SampleCurrency";
std::string functionName = "remit";
std::unordered_map<std::string, std::string> params;
params["A"] = "Ant";
params["B"] = "Bright";
SmartContract smartContract = SmartContract();
smartContract.initializeVM(contractName);
smartContract.invokeFunction(
Expand Down

0 comments on commit cdae96d

Please sign in to comment.