StateKeys reading from state #501
-
Context: I’m using hypersdk to create a vm that plays checkers. I’m storing the game related info (players, turn, and status) in a value associated to a BoardID. The structure of the action MovePiece (in charge of moving the pieces) consists of the BoardId and the positions for piece to move. Problem: When implementing the StateKeys of MovePiece action I need to specify the balance key of the actor's opponent, to decrease the piece balance in case the movement ends in capture. For getting the opponent address, I’m trying to retrieve the players from the Board state, but this function requires a context.Context and a chain.Database parameters, that can’t be passed as a StateKeys parameters because this will end up in *MovePiece not implementing chain.Action interface properly. I know I can make that “actor's opponent” element part of my action struct, but since this is an already stored data, I would like to just retrieve it from the state, instead of providing it and validate. Is that possible? If so, whats the best practice to do so? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Can't wait to play ❤️
Great question! In short, no you cannot "just retrieve it from the state." Much of the HyperSDK's execution performance comes from knowing the strict set of keys that will be accessed before executing the transaction. This allows the HyperSDK to prefetch the keys you will access and to parallelize the execution of transactions that don't touch the same keys (the ones that do must be processed serially). Somewhat unrelated, the HyperSDK also requires state keys to specify the max size that can be read from a key to prevent unbounded reads. If we allow arbitrary keys to be fetched during execution, we may need to wait for a database read (~1ms) during the latency-sensitive execution loop and/or conflict with transactions processing similar state (which would cause concurrent, non-deterministic modification of state -> potentially corrupting the state at the end of execution). HyperSDK Programs will provide a "tracer" that will pre-execute transactions and return all the state keys touched back to the client during transaction construction, however, the same functionality is not planned for fully specified actions (which should be able to efficiently enumerate what they touch without a user providing an "access list," which would greatly increase the tx size). To reiterate, this approach still requires state keys to be included in the action (similar to how the opponent balance key must be added) but it is just computed generically before the action is submitted because the VM likely won't be able to derive state keys across all possible programs just from function arguments.
An example of doing something like this is the hypersdk/examples/tokenvm/actions/fill_order.go Lines 25 to 43 in 4b3f1ea |
Beta Was this translation helpful? Give feedback.
Can't wait to play ❤️
Great question! In short, no you cannot "just retrieve it from the state."
Much of the HyperSDK's execution perfo…