LCOV - code coverage report
Current view: top level - funk - fd_funk_base.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 85 87 97.7 %
Date: 2025-08-05 05:04:49 Functions: 35 2190 1.6 %

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

Generated by: LCOV version 1.14