Skip to content

Commit 8110d2a

Browse files
committed
abi: proper encoding/decoding technique
1 parent 04ed480 commit 8110d2a

File tree

5 files changed

+682
-518
lines changed

5 files changed

+682
-518
lines changed

include/ethc/abi.h

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,59 @@ extern "C" {
1111
#include <tommath.h>
1212

1313
#define ETH_ABI_WORD_SIZE 32
14-
#define ETH_ABI_DYNAMIC_TYPE_POOL_SIZE 64
15-
#define ETH_ABI_DYNAMIC_TYPE_WORDS 64
16-
#define ETH_ABI_DYNAMIC_TYPES 64
1714
#define ETH_ABI_FUNCTION_SELECTOR_SIZE 4
15+
#define ETH_ABI_FRAME_MAX_HEAD_WORDS 64
16+
#define ETH_ABI_FRAME_MAX_TAIL_WORDS 128
17+
#define ETH_ABI_FRAME_MAX_PATCHES 64
18+
#define ETH_ABI_FRAME_MAX_STACK_SIZE 64
19+
#define ETH_ABI_FRAME_MAX_POOL_SIZE 64
1820

1921
int s_mp_get_bit(const mp_int *a, int b);
2022

21-
// ABI mode (encoding or decoding)
23+
// ABI mode enum (encoding or decoding)
2224
enum eth_abi_mode {
2325
ETH_ABI_ENCODE = 0,
2426
ETH_ABI_DECODE = 1
2527
};
2628

27-
struct ethc_abi_dynamic_type_stack {
28-
// stack elements
29-
struct ethc_abi_dynamic_type *stack[ETH_ABI_DYNAMIC_TYPES];
30-
// stack head
31-
size_t head;
29+
// patch struct where dynamic data offets stored
30+
struct ethc_abi_patch {
31+
size_t head_offset;
32+
size_t tail_offset;
3233
};
3334

34-
struct ethc_abi_dynamic_type {
35-
// absolute location of the type (for backpatching)
36-
size_t absloc;
37-
// location of this type in parent type
38-
size_t ploc;
39-
// pointer to the parent type
40-
struct ethc_abi_dynamic_type *ptype;
41-
// pointer to the raw buffer
42-
uint8_t rawbuf[ETH_ABI_DYNAMIC_TYPE_WORDS * ETH_ABI_WORD_SIZE];
43-
// raw buffer offset
44-
size_t offset;
45-
// size of the type
46-
size_t size;
35+
struct ethc_abi_frame {
36+
// buffer for storing static elements
37+
size_t head_offset;
38+
uint8_t head[ETH_ABI_WORD_SIZE * ETH_ABI_FRAME_MAX_HEAD_WORDS];
39+
40+
// buffer for storing dynamic and nested data
41+
size_t tail_offset;
42+
uint8_t tail[ETH_ABI_WORD_SIZE * ETH_ABI_FRAME_MAX_TAIL_WORDS];
43+
44+
// buffer for dynamic data patches
45+
size_t patch_offset;
46+
struct ethc_abi_patch patches[ETH_ABI_FRAME_MAX_PATCHES];
47+
48+
// size of the tail, used on decode
49+
size_t tail_size;
4750
};
4851

4952
struct eth_abi {
5053
// ABI mode
5154
enum eth_abi_mode m;
52-
// dynamic type pool
53-
struct ethc_abi_dynamic_type typepool[ETH_ABI_DYNAMIC_TYPE_POOL_SIZE];
54-
// dynamic type pool head
55-
size_t head;
56-
// stack for nested dynamic types
57-
struct ethc_abi_dynamic_type_stack stack;
58-
// list of dynamic types
59-
struct ethc_abi_dynamic_type *types[ETH_ABI_DYNAMIC_TYPES];
60-
// length of list of dynamic types
61-
size_t typelen;
55+
56+
// dynamic data frame pool
57+
struct ethc_abi_frame pool[ETH_ABI_FRAME_MAX_POOL_SIZE];
58+
size_t pool_head;
59+
60+
// frame stack for nested elements
61+
struct ethc_abi_frame *stack[ETH_ABI_FRAME_MAX_STACK_SIZE];
62+
int stack_top;
63+
64+
// function selector
65+
uint8_t func[4];
66+
int func_set;
6267
};
6368

6469
/*!
@@ -167,7 +172,7 @@ ETHC_EXPORT ETH_OP eth_abi_mpint(struct eth_abi *abi, mp_int *mpint);
167172
* @param[inout] addr Ponter to address to read/write the data from/to.
168173
* @return `1` on success, `-1` otherwise.
169174
*/
170-
ETHC_EXPORT ETH_OP eth_abi_address(struct eth_abi *abi, char **addr);
175+
ETHC_EXPORT ETH_OP eth_abi_address(struct eth_abi *abi, char *addr);
171176

172177
/*!
173178
* @brief Encodes/decodes 8 byte array.
@@ -277,7 +282,6 @@ ETHC_EXPORT ETH_OP eth_abi_array(struct eth_abi *abi, size_t *len);
277282
*/
278283
ETHC_EXPORT ETH_OP eth_abi_array_end(struct eth_abi *abi);
279284

280-
281285
#ifdef __cplusplus
282286
}
283287
#endif

0 commit comments

Comments
 (0)