After some overconfidence in IP67 dipping it into the pool (after a screen repair- which probably reduced the effectiveness of the seals) I encountered the same boot loop issue on my iPhone X.
I tried to put it into DFU mode and restore and got the 4031 error every time.
After disconnecting my earpiece speaker/sensor assembly as pointed out above, I didn’t observe any corrosion and it was still unable to restore out of DFU mode (still error 4031) with the flex disconnected.
After some troubleshooting through disconnecting the camera and charging flexes I was still unable to restore out of the 4031 error.
Then it sprung to mind that it could be the charging assembly at fault but I was stuck in DFU mode (no untethered boot). I used the ‘PhoneRescue’ app (can use any equivalent app) to exit the device out of DFU/ramdisk mode.
I then disconnected only the charging assembly flex and voila! It booted with no issues. Now all I need to do is replace the charging assembly.
The iPhone X bootloop issue may also be due to the charging flex as well as the earpiece assembly -just make sure you’re out of DFU mode.