Is there a way to tell (probabilistically, maybe) by looking at an on-chain transaction that it is related to Lightning (is [likely to be] a channel opening or closure)?
A lightning funding transaction is a P2WSH transaction, which appears on chain as paying to some random-looking script hash, but the
redeemScript which can unlock and spend the money is not revealed on chain until the transaction gets spent (which is when the channel gets closed in Lightning's case).
You can recreate the
redeemScript if you know the funding public keys used for it, which are broadcast over Lightning's gossip network as part of a
channel_announcement message. If you take the two funding public keys, where the first key is the lexicographically lesser of the two, the
redeemScript is simply:
OP_2 <pubKey1> <pubKey2> OP_2 OP_CHECKMULTISIG
By hashing this and comparing it to the hash the unspent P2WSH transaction spends to, you can verify that the transaction is a Lightning channel.
But without knowledge of those funding public keys (private channels or non-lightning txs), you cannot know the content of the
redeemScript until the transaction is spent.
This question seems very similar to Extract lightning network funding transactions with python bitcoin blockchain parser lib in which the answer was given that you cannot really scan the blockchain for lightning channels.
However there is more to add:
The situation becomes worse with 2p ecdsa or scriptless scripts via which you can theoretically open a channel and close it via P2PKH transaction. The same holds true when Bitcoin gets its soft fork for schnorr signatures. In that way those tx (or smart contacts) become indistinguishable from regular Bitcoin txs.
But : you can look at the gossip protocol of the lightning network for open channels. Assume you have a full history of open and closed channels (which would not include private channels) you could probably run some machine learning algorithms / classifier to create a probabilistic model. Assuming most channels are public you should have a relatively good training / test set. However as long as channels are not closed all tx are P2WSH tx which are not insistinguishable as mentioned by the above linked thread. So a machine learning model would only make sense (if at all) to detect former / closed channels.