node-zwave-js icon indicating copy to clipboard operation
node-zwave-js copied to clipboard

fix: reproduce S2 collisions in tests and allow decryption with previous SPAN if both parties transmit at the same time

Open AlCalzone opened this issue 3 years ago • 0 comments
trafficstars

This PR adds support for using S2 in mock-based tests, and reproduces the S2 collision for the case where both nodes send at the same time. The specifications don't describe what to do in this situation, and this happens more often that we'd like. So as a solution, we now remember the nonce used to encrypt the last command we sent. When an S2-encrypted command is received, we first attempt decryption with that nonce under the following circumstances:

  1. We've recently (<500 ms) sent an S2-encrypted command to the source node and remembered the nonce
  2. Decryption with that nonce hasn't been attempted yet
  3. The sequence number of the received command is the expected one directly after the last sequence number from the source node

Whether decryption succeeds or not, the nonce then gets deleted to avoid using it multiple times. Any decryption attempt under any other circumstances will delete the nonce too.

By doing so, Z-Wave JS will accept the command the node sent, but since it was using the same nonce as the node, the node will still not understand the command Z-Wave JS sent and attempt re-synchronization. This way, only one parallel resync flow will happen. Since we recently started using Supervision for most SET-type commands, we are now able to resend the last command the node didn't understand in most situations and (almost) no command should get lost this way.

fixes part of #3917 fixes: https://github.com/zwave-js/node-zwave-js/issues/4879

AlCalzone avatar Aug 09 '22 19:08 AlCalzone