Skip to content

Commit 7d4d6c7

Browse files
committedJun 8, 2017
EVM-C: Fix constness, extend examples
1 parent 7e9bd50 commit 7d4d6c7

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed
 

‎examples/capi.c

+35-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ struct evm_uint160be address(struct evm_env* env)
1717
return ret;
1818
}
1919

20+
static void print_address(const struct evm_uint160be* address)
21+
{
22+
int i = 0;
23+
for (i = 0; i < sizeof(address->bytes); ++i)
24+
printf("%x", address->bytes[i] & 0xff);
25+
}
26+
2027
static void query(union evm_variant* result,
2128
struct evm_env* env,
2229
enum evm_query_key key,
@@ -43,11 +50,24 @@ static void query(union evm_variant* result,
4350
}
4451

4552
static void set_storage(struct evm_env* env,
46-
const struct evm_uint160be* addr,
53+
const struct evm_uint160be* address,
4754
const struct evm_uint256be* key,
4855
const struct evm_uint256be* value)
4956
{
50-
printf("EVM-C: SSTORE");
57+
printf("EVM-C: SSTORE @");
58+
print_address(address);
59+
printf("\n");
60+
}
61+
62+
static void selfdestruct(struct evm_env* env,
63+
const struct evm_uint160be* address,
64+
const struct evm_uint160be* beneficiary)
65+
{
66+
printf("EVM-C: SELFDESTRUCT ");
67+
print_address(address);
68+
printf(" -> ");
69+
print_address(beneficiary);
70+
printf("\n");
5171
}
5272

5373
static void call(struct evm_result* result,
@@ -69,15 +89,26 @@ static void get_block_hash(struct evm_uint256be* result, struct evm_env* env,
6989

7090
}
7191

92+
/// EVM log callback.
93+
///
94+
/// @note The `evm_log` name is used to avoid conflict with `log()` C function.
95+
static void evm_log(struct evm_env* env, const struct evm_uint160be* address,
96+
const uint8_t* data, size_t data_size,
97+
const struct evm_uint256be topics[], size_t topics_count)
98+
{
99+
printf("EVM-C: LOG%d\n", (int)topics_count);
100+
}
101+
72102
/// Example how the API is supposed to be used.
73103
int main(int argc, char *argv[]) {
74104
struct evm_factory factory = examplevm_get_factory();
75105
if (factory.abi_version != EVM_ABI_VERSION)
76106
return 1; // Incompatible ABI version.
77107

78-
struct evm_instance* jit = factory.create(query, set_storage, NULL, call,
108+
struct evm_instance* jit = factory.create(query, set_storage, selfdestruct,
109+
call,
79110
get_tx_context, get_block_hash,
80-
NULL);
111+
evm_log);
81112

82113
uint8_t const code[] = "Place some EVM bytecode here";
83114
const size_t code_size = sizeof(code);

‎examples/examplevm.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ struct examplevm
99
struct evm_instance instance;
1010
evm_query_state_fn query_fn;
1111
evm_set_storage_fn set_storage_fn;
12+
evm_selfdestruct_fn selfdestruct_fn;
1213
evm_call_fn call_fn;
1314
evm_get_tx_context_fn get_tx_context_fn;
1415
evm_get_block_hash_fn get_block_hash_fn;
16+
evm_log_fn log_fn;
1517

1618
int example_option;
1719
};
@@ -115,7 +117,7 @@ static struct evm_result execute(struct evm_instance* instance,
115117
}
116118

117119
static struct evm_instance* evm_create(evm_query_state_fn query_fn,
118-
evm_set_storage_fn update_fn,
120+
evm_set_storage_fn set_storage_fn,
119121
evm_selfdestruct_fn selfdestruct_fn,
120122
evm_call_fn call_fn,
121123
evm_get_tx_context_fn get_tx_context_fn,
@@ -128,10 +130,12 @@ static struct evm_instance* evm_create(evm_query_state_fn query_fn,
128130
interface->execute = execute;
129131
interface->set_option = evm_set_option;
130132
vm->query_fn = query_fn;
131-
vm->set_storage_fn = update_fn;
133+
vm->set_storage_fn = set_storage_fn;
134+
vm->selfdestruct_fn = selfdestruct_fn;
132135
vm->call_fn = call_fn;
133136
vm->get_tx_context_fn = get_tx_context_fn;
134137
vm->get_block_hash_fn = get_block_hash_fn;
138+
vm->log_fn = log_fn;
135139
return interface;
136140
}
137141

‎include/evm.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ typedef void (*evm_set_storage_fn)(struct evm_env* env,
282282
/// the host application.
283283
/// @param address The address of the contract to be selfdestructed.
284284
/// @param beneficiary The address where the remaining ETH is going to be
285-
/// transfer.
285+
/// transferred.
286286
typedef void (*evm_selfdestruct_fn)(struct evm_env* env,
287287
const struct evm_uint160be* address,
288288
const struct evm_uint160be* beneficiary);
@@ -291,19 +291,20 @@ typedef void (*evm_selfdestruct_fn)(struct evm_env* env,
291291
///
292292
/// This callback function is used by an EVM to inform about a LOG that happened
293293
/// during an EVM bytecode execution.
294-
/// @param env The pointer to execution environment managed by the host
295-
/// application.
296-
/// @param address The address of the contract that generated the log.
297-
/// @param data The pointer to unindexed data attached to the log.
298-
/// @param data_size The length of the data.
299-
/// @param topics The pointer to the array of topics attached to the log.
300-
/// @param topics_num The number of the topics. Valid values 0 - 4.
294+
/// @param env The pointer to execution environment managed by
295+
/// the host application.
296+
/// @param address The address of the contract that generated the log.
297+
/// @param data The pointer to unindexed data attached to the log.
298+
/// @param data_size The length of the data.
299+
/// @param topics The pointer to the array of topics attached to the log.
300+
/// @param topics_count The number of the topics. Valid values are between
301+
/// 0 and 4 inclusively.
301302
typedef void (*evm_log_fn)(struct evm_env* env,
302303
const struct evm_uint160be* address,
303304
const uint8_t* data,
304305
size_t data_size,
305-
struct evm_uint256be topics[],
306-
size_t topics_num);
306+
const struct evm_uint256be topics[],
307+
size_t topics_count);
307308

308309
/// Pointer to the callback function supporting EVM calls.
309310
///
@@ -331,7 +332,7 @@ struct evm_instance; ///< Forward declaration.
331332
/// @param get_block_hash_fn Pointer to get block hash function. Nonnull.
332333
/// @return Pointer to the created EVM instance.
333334
typedef struct evm_instance* (*evm_create_fn)(evm_query_state_fn query_fn,
334-
evm_set_storage_fn update_fn,
335+
evm_set_storage_fn set_storage_fn,
335336
evm_selfdestruct_fn selfdestruct_fn,
336337
evm_call_fn call_fn,
337338
evm_get_tx_context_fn get_tx_context_fn,

0 commit comments

Comments
 (0)
Please sign in to comment.