-
Notifications
You must be signed in to change notification settings - Fork 7
/
storage.h
68 lines (57 loc) · 1.85 KB
/
storage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef __STORAGE_H
#define __STORAGE_H
#include <stddef.h>
#include <stdint.h>
#include <bebi.h>
#include <hostio.h>
/**
* Storage.h creates tools that help access storage from a c-sdk contract
*
* These user is still required to understand solidity storage and use accordingly
* See: https://docs.soliditylang.org/en/v0.8.20/internals/layout_in_storage.html
*
* requires: bebi.h (string.h)
* c-file: storage.c
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* storage_load / store load or store a value from storage accordingly.
*
* The value of the first "storage" pointer is not used.
* generated headers provide:
* a const storage pointer when working for a view-only function
* a non const pointer for a mutating function
* no pointer (so don't call storage_load) for a pure function
*
*/
inline void storage_load(const void* storage, const uint8_t *key, uint8_t *dest) {
storage_load_bytes32(key, dest);
}
/**
* see documentation for storage_load
*/
inline void storage_store(void *storage, const uint8_t *key, const uint8_t *value) {
storage_store_bytes32(key, value);
}
/**
* calculate slot for a map with base slot "storage" to put "key"
* If key requires padding it must be applied before calling this function
*/
void map_slot(bebi32 const storage, uint8_t const *key, size_t key_len, bebi32 slot_out);
/**
* calculate slot and offset for an array with base slot "slot"
* notice tht short byte-arrays and strings are not stored in base but in
* the "size" slot - see solidity spec
*/
int array_slot_offset(bebi32 const base, size_t val_size, uint64_t index, bebi32 slot_out, size_t *offset_out);
/**
* calculate the base slot for a dynamic array with storage-slot "storage"
* (this is just native keccak of storage into base_out)
*/
void dynamic_array_base_slot(bebi32 const storage, bebi32 base_out);
#ifdef __cplusplus
}
#endif
#endif