Is there any format change in block #498557
From what I can tell, by testing your script, is that it is failing on Segwit transactions. This would make sense, if you haven't accounted for it, because it changes what is used to produce the tx hash. This was introduced with BIP141. Currently, not all transactions are required to be in this format, so you will have to account for both. The txid still uses the entire transaction structure, this is why you might notice bitcoin-core rpc calls will return a different txid than the tx hash (wtxid) for segwit transactions.


  Definition of txid remains unchanged: the double SHA256 of the
  traditional serialization format:
  
  [nVersion][txins][txouts][nLockTime]
  
  A new wtxid is defined: the double SHA256 of the new serialization
  with witness data:
  
  [nVersion][marker][flag][txins][txouts][witness][nLockTime]


see BIP141

What you'll need to do is look for the segwit flag 0x01 in the serialized transaction. It's an optional 2-byte array right after the first version bytes, see https://en.bitcoin.it/wiki/Transaction