Skip to content

feat(statebag): add private keys that only replicate to the owner - #4391

Open
BigSmoKe07 wants to merge 3 commits into
citizenfx:masterfrom
BigSmoKe07:feat/statebag-private-keys
Open

BigSmoKe07 wants to merge 3 commits into
citizenfx:masterfrom
BigSmoKe07:feat/statebag-private-keys

Conversation

@BigSmoKe07

@BigSmoKe07 BigSmoKe07 commented Nov 10, 2026 •

Copy link
Copy Markdown

Goal of this PR

Let the server keep a state bag key private to the owner of the state bag.

Today every replicated key goes to every player the state bag replicates to, so per-player values such as a balance or a permission level can be read by any client that has that player in scope. The only way around it is to keep such data out of state bags and sync it with events.

How is this PR achieving the goal

-- server
SetStateBagKeyPrivate(('player:%s'):format(source), 'balance', true)
Player(source).state.balance = 5000
peers 1 (owner) and 2 have player 1 in scope

public key     peer 1 <- balance     peer 2 <- balance
private key    peer 1 <- balance     peer 2 <- nothing
  1. StateBag::SetKeyPrivate(key, isPrivate) marks a key that is only sent to the owning peer.
  2. Enforced in SendKeyValueToAllTargets and SendAll, so owner writes and peers entering scope later are covered.
  3. SetOwningPeer sends private keys to the new owner and nil to the previous one.
  4. A mode change on a key with a value sends nil (private) or the value (public) to non-owners.
  5. Sends now hold the state bag's data lock (shared), which also guards the owning peer.
  6. New server native SET_STATE_BAG_KEY_PRIVATE, with docs and a test case.

Nothing changes on the wire and keys stay public unless a script marks them, so existing resources and clients behave as before.

This PR applies to the following area(s)

FiveM, RedM, Server, Natives

Successfully tested on

Game builds: b3889

Platforms: Windows

Component check: State Bag private keys test case passes under ASan; a concurrent stress run is clean under TSan.

Checklist

  • Code compiles and has been tested successfully.
  • Code explains itself well and/or is documented.
  • My commit message explains what the changes do and what they are for.
  • No extra compilation warnings are added by these changes.

Fixes issues

Related: https://forum.cfx.re/t/5002514

Signed-off-by: Guru Saran <gurusarank@icloud.com>
@github-actions github-actions Bot added RedM Issues/PRs related to RedM invalid Requires changes before it's considered valid and can be (re)triaged labels Nov 10, 2026
@xalva98

xalva98 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

I think this is nice to have, but not really necessary - events can already do everything here. Also, why are you still iterating over all peers (O(n)) when the whole point of this PR is to replicate private keys to only one peer (the owner)?

Could just send it directly to the owner, the info should be available

Signed-off-by: Guru Saran <gurusarank@icloud.com>
Signed-off-by: Guru Saran <gurusarank@icloud.com>
@BigSmoKe07

BigSmoKe07 commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

Events work, but each resource then rebuilds what state bags already do. Use cases from the forum thread:

  • personal player data without events and exports per resource
  • state only the entity owner needs to run code
  • other clients never get the value
  • entity state follows owner changes

Loop removed: private keys go straight to the owner. Also fixed: a reused owner ID no longer gets them.

@ook3D

ook3D commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Should be banned for spam at this point

@Entytaiment25

Copy link
Copy Markdown

@ook3D he is road to 2 pages full of PRs atp

@github-actions github-actions Bot added triage Needs a preliminary assessment to determine the urgency and required action invalid Requires changes before it's considered valid and can be (re)triaged and removed invalid Requires changes before it's considered valid and can be (re)triaged triage Needs a preliminary assessment to determine the urgency and required action labels Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

invalid Requires changes before it's considered valid and can be (re)triaged RedM Issues/PRs related to RedM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants