Line data Source code
1 : #ifndef HEADER_fd_src_funk_fd_funk_base_h
2 : #define HEADER_fd_src_funk_fd_funk_base_h
3 :
4 : /* Funk terminology / concepts:
5 :
6 : - A funk instance stores records.
7 :
8 : - A record is a key-value pair.
9 :
10 : - keys are a fixed length fd_funk_rec_key_t.
11 :
12 : - values are variable size arbitrary binary data with an upper bound
13 : to the size.
14 :
15 : - Records are indexed by key.
16 :
17 : - A funk transaction describes changes to the funk records.
18 :
19 : - A transactions has a globally unique identifier and a parent
20 : transaction.
21 :
22 : - Transactions with children cannot be modified.
23 :
24 : - The chain of transactions through a transaction's ancestors
25 : (its parent, grandparent, great-grandparent, ...) provides a
26 : history of the funk all the way back the "root" transaction.
27 :
28 : - A transaction can be either in preparation or published.
29 :
30 : - The ancestors of a published transaction cannot be modified.
31 :
32 : - In preparation transactions can be cancelled.
33 :
34 : - Cancelling a transaction will discard all funk record updates for
35 : that transaction and any descendant transactions.
36 :
37 : - Published transactions cannot be cancelled.
38 :
39 : - Critically, competing/parallel transaction histories are allowed.
40 :
41 : - A user can update all funk records for the most recently
42 : published transactions (if it is not frozen) or all transactions
43 : in preparation (if they are not frozen). */
44 :
45 : #include "../util/fd_util.h"
46 :
47 : #if FD_HAS_X86
48 : #include <immintrin.h>
49 : #endif
50 :
51 : /* FD_FUNK_SUCCESS is used by various APIs to indicate the operation
52 : successfully completed. This will be 0. FD_FUNK_ERR_* gives a
53 : number of error codes used by fd_funk APIs. These will be negative
54 : integers. */
55 :
56 1278510 : #define FD_FUNK_SUCCESS (0) /* Success */
57 3 : #define FD_FUNK_ERR_INVAL (-1) /* Failed due to obviously invalid inputs */
58 3 : #define FD_FUNK_ERR_XID (-2) /* Failed due to transaction id issue (e.g. xid present/absent when it should be absent/present) */
59 3 : #define FD_FUNK_ERR_KEY (-3) /* Failed due to record key issue (e.g. key present/absent when it should be absent/present) */
60 3 : #define FD_FUNK_ERR_FROZEN (-4) /* Failed due to frozen issue (e.g. attempt to change records in a frozen transaction) */
61 3 : #define FD_FUNK_ERR_TXN (-5) /* Failed due to transaction map issue (e.g. funk txn_max too small) */
62 3 : #define FD_FUNK_ERR_REC (-6) /* Failed due to record map issue (e.g. funk rec_max too small) */
63 3 : #define FD_FUNK_ERR_MEM (-7) /* Failed due to wksp issue (e.g. wksp too small) */
64 0 : #define FD_FUNK_ERR_SYS (-8) /* Failed system call (e.g. a file write) */
65 :
66 : /* FD_FUNK_REC_KEY_{ALIGN,FOOTPRINT} describe the alignment and
67 : footprint of a fd_funk_rec_key_t. ALIGN is a positive integer power
68 : of 2. FOOTPRINT is a multiple of ALIGN. These are provided to
69 : facilitate compile time declarations. */
70 :
71 : #define FD_FUNK_REC_KEY_ALIGN (8UL)
72 : #define FD_FUNK_REC_KEY_FOOTPRINT (32UL)
73 :
74 : /* A fd_funk_rec_key_t identifies a funk record. Compact binary keys
75 : are encouraged but a cstr can be used so long as it has
76 : strlen(cstr)<FD_FUNK_REC_KEY_FOOTPRINT and the characters c[i] for i
77 : in [strlen(cstr),FD_FUNK_REC_KEY_FOOTPRINT) zero. (Also, if encoding
78 : a cstr in a key, recommend using first byte to encode the strlen for
79 : accelerating cstr operations further but this is up to the user.) */
80 :
81 : union __attribute__((aligned(FD_FUNK_REC_KEY_ALIGN))) fd_funk_rec_key {
82 : uchar uc[ FD_FUNK_REC_KEY_FOOTPRINT ];
83 : uint ui[ 8 ];
84 : ulong ul[ 4 ];
85 : };
86 :
87 : typedef union fd_funk_rec_key fd_funk_rec_key_t;
88 :
89 : /* FD_FUNK_TXN_XID_{ALIGN,FOOTPRINT} describe the alignment and
90 : footprint of a fd_funk_txn_xid_t. ALIGN is a positive integer power
91 : of 2. FOOTPRINT is a multiple of ALIGN. These are provided to
92 : facilitate compile time declarations. */
93 :
94 : #define FD_FUNK_TXN_XID_ALIGN (16UL)
95 : #define FD_FUNK_TXN_XID_FOOTPRINT (16UL)
96 :
97 : /* A fd_funk_txn_xid_t identifies a funk transaction currently in
98 : preparation. Compact binary identifiers are encouraged but a cstr
99 : can be used so long as it has
100 : strlen(cstr)<FD_FUNK_TXN_XID_FOOTPRINT and characters c[i] for i
101 : in [strlen(cstr),FD_FUNK_TXN_KEY_FOOTPRINT) zero. (Also, if
102 : encoding a cstr in a transaction id, recommend using first byte to
103 : encode the strlen for accelerating cstr operations even further but
104 : this is more up to the application.) */
105 :
106 : union __attribute__((aligned(FD_FUNK_TXN_XID_ALIGN))) fd_funk_txn_xid {
107 : uchar uc[ FD_FUNK_TXN_XID_FOOTPRINT ];
108 : ulong ul[ FD_FUNK_TXN_XID_FOOTPRINT / sizeof(ulong) ];
109 : #if FD_HAS_INT128
110 : uint128 uf[1];
111 : #endif
112 : #if FD_HAS_X86
113 : __m128i xmm[1];
114 : #endif
115 : };
116 :
117 : typedef union fd_funk_txn_xid fd_funk_txn_xid_t;
118 :
119 : /* FD_FUNK_XID_KEY_PAIR_{ALIGN,FOOTPRINT} describe the alignment and
120 : footprint of a fd_funk_xid_key_pair_t. ALIGN is a positive integer
121 : power of 2. FOOTPRINT is a multiple of ALIGN. These are provided to
122 : facilitate compile time declarations. */
123 :
124 : #define FD_FUNK_XID_KEY_PAIR_ALIGN (16UL)
125 : #define FD_FUNK_XID_KEY_PAIR_FOOTPRINT (48UL)
126 :
127 : /* A fd_funk_xid_key_pair_t identifies a funk record. It is just
128 : xid and key packed into the same structure. */
129 :
130 : struct fd_funk_xid_key_pair {
131 : fd_funk_txn_xid_t xid[1];
132 : fd_funk_rec_key_t key[1];
133 : };
134 :
135 : typedef struct fd_funk_xid_key_pair fd_funk_xid_key_pair_t;
136 :
137 : /* A fd_funk_shmem_t is the top part of a funk object in shared memory. */
138 :
139 : struct fd_funk_shmem_private;
140 : typedef struct fd_funk_shmem_private fd_funk_shmem_t;
141 :
142 : /* A fd_funk_t * is local join handle to a funk instance */
143 :
144 : struct fd_funk_private;
145 : typedef struct fd_funk_private fd_funk_t;
146 :
147 : FD_PROTOTYPES_BEGIN
148 :
149 : /* fd_funk_rec_key_hash provides a family of hashes that hash the key
150 : pointed to by k to a uniform quasi-random 64-bit integer. seed
151 : selects the particular hash function to use and can be an arbitrary
152 : 64-bit value. Returns the hash. The hash functions are high quality
153 : but not cryptographically secure. Assumes k is in the caller's
154 : address space and valid. */
155 :
156 : #if FD_HAS_INT128
157 :
158 : /* If the target supports uint128, fd_funk_rec_key_hash is seeded
159 : xxHash3 with 64-bit output size. (open source BSD licensed) */
160 :
161 : static inline ulong
162 29246454 : fd_xxh3_mul128_fold64( ulong lhs, ulong rhs ) {
163 29246454 : uint128 product = (uint128)lhs * (uint128)rhs;
164 29246454 : return (ulong)product ^ (ulong)( product>>64 );
165 29246454 : }
166 :
167 : static inline ulong
168 : fd_xxh3_mix16b( ulong i0, ulong i1,
169 : ulong s0, ulong s1,
170 29246454 : ulong seed ) {
171 29246454 : return fd_xxh3_mul128_fold64( i0 ^ (s0 + seed), i1 ^ (s1 - seed) );
172 29246454 : }
173 :
174 : FD_FN_PURE static inline ulong
175 : fd_funk_rec_key_hash1( uchar const key[ 32 ],
176 14623227 : ulong seed ) {
177 14623227 : ulong k0 = FD_LOAD( ulong, key+ 0 );
178 14623227 : ulong k1 = FD_LOAD( ulong, key+ 8 );
179 14623227 : ulong k2 = FD_LOAD( ulong, key+16 );
180 14623227 : ulong k3 = FD_LOAD( ulong, key+24 );
181 14623227 : ulong acc = 32 * 0x9E3779B185EBCA87ULL;
182 14623227 : acc += fd_xxh3_mix16b( k0, k1, 0xbe4ba423396cfeb8UL, 0x1cad21f72c81017cUL, seed );
183 14623227 : acc += fd_xxh3_mix16b( k2, k3, 0xdb979083e96dd4deUL, 0x1f67b3b7a4a44072UL, seed );
184 14623227 : acc = acc ^ (acc >> 37);
185 14623227 : acc *= 0x165667919E3779F9ULL;
186 14623227 : acc = acc ^ (acc >> 32);
187 14623227 : return acc;
188 14623227 : }
189 :
190 : FD_FN_PURE static inline ulong
191 : fd_funk_rec_key_hash( fd_funk_rec_key_t const * k,
192 14621073 : ulong seed ) {
193 14621073 : return fd_funk_rec_key_hash1( k->uc, seed );
194 14621073 : }
195 :
196 : #else
197 :
198 : /* If the target does not support xxHash3, fallback to the 'old' funk
199 : key hash function.
200 :
201 : FIXME This version is vulnerable to HashDoS */
202 :
203 : FD_FN_PURE static inline ulong
204 : fd_funk_rec_key_hash1( uchar const key[ 32 ],
205 : ulong seed ) {
206 : /* tons of ILP */
207 : return (fd_ulong_hash( seed ^ (1UL<<0) ^ FD_LOAD( ulong, key+ 0 ) ) ^
208 : fd_ulong_hash( seed ^ (1UL<<1) ^ FD_LOAD( ulong, key+ 8 ) ) ) ^
209 : (fd_ulong_hash( seed ^ (1UL<<2) ^ FD_LOAD( ulong, key+16 ) ) ^
210 : fd_ulong_hash( seed ^ (1UL<<3) ^ FD_LOAD( ulong, key+24 ) ) );
211 : }
212 :
213 : FD_FN_PURE static inline ulong
214 : fd_funk_rec_key_hash( fd_funk_rec_key_t const * k,
215 : ulong seed ) {
216 : return fd_funk_rec_key_hash1( k->uc, seed );
217 : }
218 :
219 : #endif /* FD_HAS_INT128 */
220 :
221 : /* fd_funk_rec_key_eq returns 1 if keys pointed to by ka and kb are
222 : equal and 0 otherwise. Assumes ka and kb are in the caller's address
223 : space and valid. */
224 :
225 : FD_FN_UNUSED FD_FN_PURE static int /* Workaround -Winline */
226 : fd_funk_rec_key_eq( fd_funk_rec_key_t const * ka,
227 53042904 : fd_funk_rec_key_t const * kb ) {
228 53042904 : ulong const * a = ka->ul;
229 53042904 : ulong const * b = kb->ul;
230 53042904 : return !( ((a[0]^b[0]) | (a[1]^b[1])) | ((a[2]^b[2]) | (a[3]^b[3])) ) ;
231 53042904 : }
232 :
233 : /* fd_funk_rec_key_copy copies the key pointed to by ks into the key
234 : pointed to by kd and returns kd. Assumes kd and ks are in the
235 : caller's address space and valid. */
236 :
237 : static inline fd_funk_rec_key_t *
238 : fd_funk_rec_key_copy( fd_funk_rec_key_t * kd,
239 10876464 : fd_funk_rec_key_t const * ks ) {
240 10876464 : ulong * d = kd->ul;
241 10876464 : ulong const * s = ks->ul;
242 10876464 : d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; d[3] = s[3];
243 10876464 : return kd;
244 10876464 : }
245 :
246 : /* fd_funk_txn_xid_hash provides a family of hashes that hash the xid
247 : pointed to by x to a uniform quasi-random 64-bit integer. seed
248 : selects the particular hash function to use and can be an arbitrary
249 : 64-bit value. Returns the hash. The hash functions are high quality
250 : but not cryptographically secure. Assumes x is in the caller's
251 : address space and valid. */
252 :
253 : FD_FN_UNUSED FD_FN_PURE static ulong /* Work around -Winline */
254 : fd_funk_txn_xid_hash( fd_funk_txn_xid_t const * x,
255 9541020 : ulong seed ) {
256 9541020 : return ( fd_ulong_hash( seed ^ (1UL<<0) ^ x->ul[0] ) ^ fd_ulong_hash( seed ^ (1UL<<1) ^ x->ul[1] ) ); /* tons of ILP */
257 9541020 : }
258 :
259 : /* fd_funk_txn_xid_eq returns 1 if transaction id pointed to by xa and
260 : xb are equal and 0 otherwise. Assumes xa and xb are in the caller's
261 : address space and valid. */
262 :
263 : FD_FN_PURE static inline int
264 : fd_funk_txn_xid_eq( fd_funk_txn_xid_t const * xa,
265 116292258 : fd_funk_txn_xid_t const * xb ) {
266 116292258 : ulong const * a = xa->ul;
267 116292258 : ulong const * b = xb->ul;
268 116292258 : return !( (a[0]^b[0]) | (a[1]^b[1]) );
269 116292258 : }
270 :
271 : /* fd_funk_txn_xid_copy copies the transaction id pointed to by xs into
272 : the transaction id pointed to by xd and returns xd. Assumes xd and
273 : xs are in the caller's address space and valid. */
274 :
275 : static inline fd_funk_txn_xid_t *
276 : fd_funk_txn_xid_copy( fd_funk_txn_xid_t * xd,
277 14333958 : fd_funk_txn_xid_t const * xs ) {
278 14333958 : ulong * d = xd->ul;
279 14333958 : ulong const * s = xs->ul;
280 14333958 : d[0] = s[0]; d[1] = s[1];
281 14333958 : return xd;
282 14333958 : }
283 :
284 : static inline fd_funk_txn_xid_t *
285 : fd_funk_txn_xid_st_atomic( fd_funk_txn_xid_t * xd,
286 818082 : fd_funk_txn_xid_t const * xs ) {
287 818082 : # if FD_HAS_X86
288 818082 : FD_VOLATILE( xd->xmm[0] ) = xs->xmm[0];
289 : # elif FD_HAS_INT128
290 : FD_VOLATILE( xd->uf[0] ) = xs->uf[0];
291 : # else
292 : fd_funk_txn_xid_copy( xd, xs );
293 : # endif
294 818082 : return xd;
295 818082 : }
296 :
297 : static inline fd_funk_txn_xid_t *
298 : fd_funk_txn_xid_ld_atomic( fd_funk_txn_xid_t * xd,
299 45 : fd_funk_txn_xid_t const * xs ) {
300 45 : # if FD_HAS_X86
301 45 : xd->xmm[0] = FD_VOLATILE_CONST( xs->xmm[0] );
302 : # elif FD_HAS_INT128
303 : xd->uf[0] = FD_VOLATILE_CONST( xs->uf[0] );
304 : # else
305 : fd_funk_txn_xid_copy( xd, xs );
306 : # endif
307 45 : return xd;
308 45 : }
309 :
310 : /* fd_funk_txn_xid_eq_root returns 1 if transaction id pointed to by x
311 : is the root transaction. Assumes x is in the caller's address space
312 : and valid. */
313 :
314 : FD_FN_PURE static inline int
315 27624426 : fd_funk_txn_xid_eq_root( fd_funk_txn_xid_t const * x ) {
316 27624426 : ulong const * a = x->ul;
317 27624426 : return ((a[0] == ULONG_MAX) & (a[1] == ULONG_MAX));
318 27624426 : }
319 :
320 : /* fd_funk_txn_xid_set_root sets transaction id pointed to by x to the
321 : root transaction and returns x. Assumes x is in the caller's address
322 : space and valid. */
323 :
324 : static inline fd_funk_txn_xid_t *
325 593808 : fd_funk_txn_xid_set_root( fd_funk_txn_xid_t * x ) {
326 593808 : ulong * a = x->ul;
327 593808 : a[0] = ULONG_MAX; a[1] = ULONG_MAX;
328 593808 : return x;
329 593808 : }
330 :
331 : /* fd_funk_xid_key_pair_hash produces a 64-bit hash case for a
332 : xid_key_pair. Assumes p is in the caller's address space and valid. */
333 :
334 : FD_FN_PURE static inline ulong
335 : fd_funk_xid_key_pair_hash( fd_funk_xid_key_pair_t const * p,
336 8621073 : ulong seed ) {
337 : /* We ignore the xid part of the key because we need all the instances
338 : of a given record key to appear in the same hash
339 : chain. fd_funk_rec_query_global depends on this. */
340 8621073 : return fd_funk_rec_key_hash( p->key, seed );
341 8621073 : }
342 :
343 : /* fd_funk_xid_key_pair_eq returns 1 if (xid,key) pair pointed to by pa
344 : and pb are equal and 0 otherwise. Assumes pa and pb are in the
345 : caller's address space and valid. */
346 :
347 : FD_FN_UNUSED FD_FN_PURE static int /* Work around -Winline */
348 : fd_funk_xid_key_pair_eq( fd_funk_xid_key_pair_t const * pa,
349 29041749 : fd_funk_xid_key_pair_t const * pb ) {
350 29041749 : return fd_funk_txn_xid_eq( pa->xid, pb->xid ) & fd_funk_rec_key_eq( pa->key, pb->key );
351 29041749 : }
352 :
353 : /* fd_funk_xid_key_pair_copy copies the (xid,key) pair pointed to by ps
354 : into the (xid,key) pair to by pd and returns pd. Assumes pd and ps
355 : are in the caller's address space and valid. */
356 :
357 : static inline fd_funk_xid_key_pair_t *
358 : fd_funk_xid_key_pair_copy( fd_funk_xid_key_pair_t * pd,
359 3000000 : fd_funk_xid_key_pair_t const * ps ) {
360 3000000 : fd_funk_txn_xid_copy( pd->xid, ps->xid );
361 3000000 : fd_funk_rec_key_copy( pd->key, ps->key );
362 3000000 : return pd;
363 3000000 : }
364 :
365 : /* fd_funk_xid_key_pair_init set the (xid,key) pair p to pair formed
366 : from the transaction id pointed to by x and the record key pointed to
367 : by k. Assumes p, x and k are in the caller's address space and
368 : valid. */
369 :
370 : static inline fd_funk_xid_key_pair_t *
371 : fd_funk_xid_key_pair_init( fd_funk_xid_key_pair_t * p,
372 : fd_funk_txn_xid_t const * x,
373 3000003 : fd_funk_rec_key_t const * k ) {
374 3000003 : fd_funk_txn_xid_copy( p->xid, x );
375 3000003 : fd_funk_rec_key_copy( p->key, k );
376 3000003 : return p;
377 3000003 : }
378 :
379 : /* fd_funk_strerror converts an FD_FUNK_SUCCESS / FD_FUNK_ERR_* code
380 : into a human readable cstr. The lifetime of the returned pointer is
381 : infinite. The returned pointer is always to a non-NULL cstr. */
382 :
383 : FD_FN_CONST char const *
384 : fd_funk_strerror( int err );
385 :
386 : /* TODO: Consider renaming transaction/txn to update (too much typing)?
387 : upd (probably too similar to UDP) node? block? blk? state? commit?
388 : ... to reduce naming collisions with terminology in use elsewhere?
389 :
390 : TODO: Consider fine tuning {REC,TXN}_{ALIGN,FOOTPRINT} to balance
391 : application use cases with in memory packing with AVX / CPU cache
392 : friendly accelerability. Likewise, virtually everything in here can
393 : be AVX accelerated if desired. E.g. 8 uint hash in parallel then an
394 : 8 wide xor lane reduction tree in hash?
395 :
396 : TODO: Consider letting the user provide alternatives for record and
397 : transaction hashes at compile time (e.g. ids in blockchain apps are
398 : often already crypto secure hashes in which case x->ul[0] ^ seed is
399 : just as good theoretically and faster practically). */
400 :
401 : FD_PROTOTYPES_END
402 :
403 : #endif /* HEADER_fd_src_funk_fd_funk_base_h */
|