Skip to content

Commit

Permalink
Merge pull request #550 from ethereum/java-host-context
Browse files Browse the repository at this point in the history
java: remove the extra wrapping of HostContext and pass it directly
  • Loading branch information
axic authored Oct 14, 2020
2 parents 6365871 + 1e9bcc9 commit fe40029
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 114 deletions.
6 changes: 3 additions & 3 deletions bindings/java/c/evmc-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_destroy(JNIEnv* jenv,
JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
jclass jcls,
jobject jevm,
jint jcontext_index,
jobject jcontext,
jint jrev,
jobject jmsg,
jobject jcode,
Expand All @@ -92,14 +92,14 @@ JNIEXPORT void JNICALL Java_org_ethereum_evmc_EvmcVm_execute(JNIEnv* jenv,
assert(msg != NULL);
size_t code_size;
const uint8_t* code = GetDirectBuffer(jenv, jcode, &code_size);
struct evmc_host_context context = {jcontext_index};
struct evmc_vm* evm = (struct evmc_vm*)(*jenv)->GetDirectBufferAddress(jenv, jevm);
assert(evm != NULL);
const struct evmc_host_interface* host = evmc_java_get_host_interface();
struct evmc_result* result =
(struct evmc_result*)(*jenv)->GetDirectBufferAddress(jenv, jresult);
assert(result != NULL);
*result = evmc_execute(evm, host, &context, (enum evmc_revision)jrev, msg, code, code_size);
*result = evmc_execute(evm, host, (struct evmc_host_context*)jcontext, (enum evmc_revision)jrev,
msg, code, code_size);
}

JNIEXPORT jint JNICALL Java_org_ethereum_evmc_EvmcVm_get_1capabilities(JNIEnv* jenv,
Expand Down
54 changes: 29 additions & 25 deletions bindings/java/c/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void CopyFromByteBuffer(JNIEnv* jenv, jobject src, void* dst, size_t size
static bool account_exists_fn(struct evmc_host_context* context, const evmc_address* address)
{
const char java_method_name[] = "account_exists";
const char java_method_signature[] = "(I[B)Z";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B)Z";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -70,7 +70,7 @@ static bool account_exists_fn(struct evmc_host_context* context, const evmc_addr

// call java method
jboolean jresult =
(*jenv)->CallStaticBooleanMethod(jenv, host_class, method, context->index, jaddress);
(*jenv)->CallStaticBooleanMethod(jenv, host_class, method, (jobject)context, jaddress);
return jresult != 0;
}

Expand All @@ -79,7 +79,8 @@ static evmc_bytes32 get_storage_fn(struct evmc_host_context* context,
const evmc_bytes32* key)
{
const char java_method_name[] = "get_storage";
const char java_method_signature[] = "(I[B[B)Ljava/nio/ByteBuffer;";
const char java_method_signature[] =
"(Lorg/ethereum/evmc/HostContext;[B[B)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -99,7 +100,7 @@ static evmc_bytes32 get_storage_fn(struct evmc_host_context* context,

// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress, jkey);
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context, jaddress, jkey);
assert(jresult != NULL);

evmc_bytes32 result;
Expand All @@ -113,7 +114,7 @@ static enum evmc_storage_status set_storage_fn(struct evmc_host_context* context
const evmc_bytes32* value)
{
const char java_method_name[] = "set_storage";
const char java_method_signature[] = "(I[B[B[B)I";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B[B[B)I";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -133,15 +134,15 @@ static enum evmc_storage_status set_storage_fn(struct evmc_host_context* context
jbyteArray jval = CopyDataToJava(jenv, value, sizeof(struct evmc_bytes32));

// call java method
jint jresult = (*jenv)->CallStaticIntMethod(jenv, host_class, method, context->index, jaddress,
jkey, jval);
jint jresult = (*jenv)->CallStaticIntMethod(jenv, host_class, method, (jobject)context,
jaddress, jkey, jval);
return (enum evmc_storage_status)jresult;
}

static evmc_uint256be get_balance_fn(struct evmc_host_context* context, const evmc_address* address)
{
const char java_method_name[] = "get_balance";
const char java_method_signature[] = "(I[B)Ljava/nio/ByteBuffer;";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -160,7 +161,7 @@ static evmc_uint256be get_balance_fn(struct evmc_host_context* context, const ev

// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress);
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context, jaddress);
assert(jresult != NULL);

evmc_uint256be result;
Expand All @@ -174,7 +175,7 @@ static evmc_uint256be get_balance_fn(struct evmc_host_context* context, const ev
static size_t get_code_size_fn(struct evmc_host_context* context, const evmc_address* address)
{
const char java_method_name[] = "get_code_size";
const char java_method_signature[] = "(I[B)I";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B)I";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -192,14 +193,15 @@ static size_t get_code_size_fn(struct evmc_host_context* context, const evmc_add
jbyteArray jaddress = CopyDataToJava(jenv, address, sizeof(struct evmc_address));

// call java method
jint jresult = (*jenv)->CallStaticIntMethod(jenv, host_class, method, context->index, jaddress);
jint jresult =
(*jenv)->CallStaticIntMethod(jenv, host_class, method, (jobject)context, jaddress);
return (size_t)jresult;
}

static evmc_bytes32 get_code_hash_fn(struct evmc_host_context* context, const evmc_address* address)
{
const char java_method_name[] = "get_code_hash";
const char java_method_signature[] = "(I[B)Ljava/nio/ByteBuffer;";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -218,7 +220,7 @@ static evmc_bytes32 get_code_hash_fn(struct evmc_host_context* context, const ev

// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress);
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context, jaddress);
assert(jresult != NULL);

evmc_bytes32 result;
Expand All @@ -241,7 +243,7 @@ static size_t copy_code_fn(struct evmc_host_context* context,
size_t buffer_size)
{
const char java_method_name[] = "copy_code";
const char java_method_signature[] = "(I[B)Ljava/nio/ByteBuffer;";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -260,7 +262,7 @@ static size_t copy_code_fn(struct evmc_host_context* context,

// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jaddress);
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context, jaddress);
assert(jresult != NULL);

// copy jresult back to buffer_data
Expand All @@ -285,7 +287,7 @@ static void selfdestruct_fn(struct evmc_host_context* context,
const evmc_address* beneficiary)
{
const char java_method_name[] = "selfdestruct";
const char java_method_signature[] = "(I[B[B)V";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B[B)V";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -304,13 +306,15 @@ static void selfdestruct_fn(struct evmc_host_context* context,
jbyteArray jbeneficiary = CopyDataToJava(jenv, beneficiary, sizeof(struct evmc_address));

// call java method
(*jenv)->CallStaticIntMethod(jenv, host_class, method, context->index, jaddress, jbeneficiary);
(*jenv)->CallStaticIntMethod(jenv, host_class, method, (jobject)context, jaddress,
jbeneficiary);
}

static struct evmc_result call_fn(struct evmc_host_context* context, const struct evmc_message* msg)
{
const char java_method_name[] = "call";
const char java_method_signature[] = "(ILjava/nio/ByteBuffer;)Ljava/nio/ByteBuffer;";
const char java_method_signature[] =
"(Lorg/ethereum/evmc/HostContext;Ljava/nio/ByteBuffer;)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -330,7 +334,7 @@ static struct evmc_result call_fn(struct evmc_host_context* context, const struc

// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, jmsg);
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context, jmsg);
assert(jresult != NULL);

struct evmc_result result;
Expand All @@ -341,7 +345,7 @@ static struct evmc_result call_fn(struct evmc_host_context* context, const struc
static struct evmc_tx_context get_tx_context_fn(struct evmc_host_context* context)
{
const char java_method_name[] = "get_tx_context";
const char java_method_signature[] = "(I)Ljava/nio/ByteBuffer;";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -356,7 +360,7 @@ static struct evmc_tx_context get_tx_context_fn(struct evmc_host_context* contex
assert(method != NULL);

// call java method
jobject jresult = (*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index);
jobject jresult = (*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context);
assert(jresult != NULL);

struct evmc_tx_context result;
Expand All @@ -367,7 +371,7 @@ static struct evmc_tx_context get_tx_context_fn(struct evmc_host_context* contex
static evmc_bytes32 get_block_hash_fn(struct evmc_host_context* context, int64_t number)
{
char java_method_name[] = "get_code_hash";
char java_method_signature[] = "(IJ)Ljava/nio/ByteBuffer;";
char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;J)Ljava/nio/ByteBuffer;";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand All @@ -383,7 +387,7 @@ static evmc_bytes32 get_block_hash_fn(struct evmc_host_context* context, int64_t

// call java method
jobject jresult =
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, context->index, (jlong)number);
(*jenv)->CallStaticObjectMethod(jenv, host_class, method, (jobject)context, (jlong)number);
assert(jresult != NULL);

evmc_bytes32 result;
Expand All @@ -399,7 +403,7 @@ static void emit_log_fn(struct evmc_host_context* context,
size_t topics_count)
{
const char java_method_name[] = "emit_log";
const char java_method_signature[] = "(I[B[BI[[BI)V";
const char java_method_signature[] = "(Lorg/ethereum/evmc/HostContext;[B[BI[[BI)V";

assert(context != NULL);
JNIEnv* jenv = attach();
Expand Down Expand Up @@ -428,7 +432,7 @@ static void emit_log_fn(struct evmc_host_context* context,
}

// call java method
(*jenv)->CallStaticIntMethod(jenv, host_class, method, context->index, jaddress, jdata,
(*jenv)->CallStaticIntMethod(jenv, host_class, method, (jobject)context, jaddress, jdata,
data_size, jtopics, topics_count);
}

Expand Down
5 changes: 0 additions & 5 deletions bindings/java/c/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
extern "C" {
#endif

struct evmc_host_context
{
int index;
};

int evmc_java_set_jvm(JNIEnv*);
const struct evmc_host_interface* evmc_java_get_host_interface();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package org.ethereum.evmc;

import org.ethereum.evmc.EvmcLoaderException;
import static org.ethereum.evmc.Host.addContext;
import static org.ethereum.evmc.Host.removeContext;

import java.nio.ByteBuffer;
import java.util.Objects;
Expand Down Expand Up @@ -103,7 +101,7 @@ public String version() {
*/
private static native void execute(
ByteBuffer nativeVm,
int context_index,
HostContext context,
int rev,
ByteBuffer msg,
ByteBuffer code,
Expand All @@ -116,11 +114,9 @@ private static native void execute(
*/
public synchronized ByteBuffer execute(
HostContext context, int rev, ByteBuffer msg, ByteBuffer code) {
int context_index = addContext(context);
int resultSize = get_result_size();
ByteBuffer result = ByteBuffer.allocateDirect(resultSize);
execute(nativeVm, context_index, rev, msg, code, result);
removeContext(context_index);
execute(nativeVm, context, rev, msg, code, result);
return result;
}

Expand Down
Loading

0 comments on commit fe40029

Please sign in to comment.