What is the limitation?
If you try to read/write to the same location in nested transactions, you get the following error:
RuntimeError: Nested transactions must currently read/modify mutually exclusive state from their parent transactions
Opening ticket to track use cases.
Use Case & Workarounds
I have a chess app.
- Have a secondary index (
LocPieceIndex that maps location -> piece) that I want to update
Piece.MovePiece moves the chess pieces, and when capturing a piece, calls Piece.Remove to remove the captured piece.
- Ideally, each individual
Piece method would do the required update.
- This results in both
Piece.Remove trying to remove the piece at the location, and Piece.MovePiece then adding the new piece to that location.
- Workaround: b/c
Piece.Remove's update is temporary and immediately overwritten, made Piece.MovePiece for the update. Introduced Piece.RemoveNoIndexUpdate to indicate that the caller is responsible for updating the secondary index.
- Fine b/c capturing a chess piece necessarily has an associated piece moved in.
- When castling the king (move performed on the king) need to check rook hasn't moved and then also move the rook
- B/c Rook is a different piece from King, requires that it get updated in a child method.
- Again, would ideally have the Index update in the
Piece method that actually makes the move
- Workaround: have the parent transaction be responsible for updating the index.
- Alternate workaround: make a special method for castling rook move which first checks if it has moved then moves it; making it so the parent txn doesn't need to read before writing.
What is the limitation?
If you try to read/write to the same location in nested transactions, you get the following error:
RuntimeError: Nested transactions must currently read/modify mutually exclusive state from their parent transactionsOpening ticket to track use cases.
Use Case & Workarounds
I have a chess app.
LocPieceIndexthat maps location -> piece) that I want to updatePiece.MovePiecemoves the chess pieces, and when capturing a piece, callsPiece.Removeto remove the captured piece.Piecemethod would do the required update.Piece.Removetrying to remove the piece at the location, andPiece.MovePiecethen adding the new piece to that location.Piece.Remove's update is temporary and immediately overwritten, madePiece.MovePiecefor the update. IntroducedPiece.RemoveNoIndexUpdateto indicate that the caller is responsible for updating the secondary index.Piecemethod that actually makes the move