user126986: Based on what I read, there is this script to build the Merkle root
import hashlib     
def dblsha(data):      
    return hashlib.sha256(hashlib.sha256(data).digest()).digest()     
 
txnlist = [coinbase] + [binascii.a2b_hex(a['data']) for a in template['transactions']]     
merklehashes = [dblsha(t) for t in txnlist]     
while len(merklehashes) > 1:     
     if len(merklehashes) % 2:     
        merklehashes.append(merklehashes[-1])      
    merklehashes = [dblsha(merklehashes[i] + merklehashes[i + 1]) for i in range(0, len(merklehashes), 2)]      
merkleroot = merklehashes[0]        
   

I wanted to mine an empty block (a block with only a coinbase transaction in it) , can I modify the script so that txnlist only includes coinbase and ignore the other transactions? or it is  considered a hard fork?
txnlist = [coinbase]       
 

Or this isn't the correct way to mine empty block?
I currently only have a shallow understanding of Bitcoin, that's why I ask.

user13866: Yes.
A block is valid even with no transactions other than the coinbase transaction. There have been many such blocks. This is especially true for early blocks of the blockchain.


Close to 19% of mined Bitcoin blocks were empty - Steven Zheng.
October 17, 2018
A total of 71 empty blocks were mined in the first five months of 2020, accounting for 0.3% of the total blocks produced

 (source: Cointelegraph)

126986: <p>Based on what <a href="https://en.bitcoin.it/wiki/Getblocktemplate" rel="nofollow noreferrer">I read</a>, there is this script to build the Merkle root</p>
<pre><code>import hashlib     
def dblsha(data):      
    return hashlib.sha256(hashlib.sha256(data).digest()).digest()     
 
txnlist = [coinbase] + [binascii.a2b_hex(a['data']) for a in template['transactions']]     
merklehashes = [dblsha(t) for t in txnlist]     
while len(merklehashes) &gt; 1:     
     if len(merklehashes) % 2:     
        merklehashes.append(merklehashes[-1])      
    merklehashes = [dblsha(merklehashes[i] + merklehashes[i + 1]) for i in range(0, len(merklehashes), 2)]      
merkleroot = merklehashes[0]        
   
</code></pre>
<p>I wanted to mine an empty block (a block with only a coinbase transaction in it) , can I modify the script so that <code>txnlist</code> only includes coinbase and ignore the other transactions? or it is  considered a hard fork?</p>
<pre><code>txnlist = [coinbase]       
 
</code></pre>
<p>Or this isn't the correct way to mine empty block?</p>
<p>I currently only have a shallow understanding of Bitcoin, that's why I ask.</p>
 13866: <p>Yes.</p>
<p>A block is valid even with no transactions other than the coinbase transaction. There have been many such blocks. This is especially true for early blocks of the blockchain.</p>
<hr />
<ul>
<li><a href="https://www.theblockcrypto.com/linked/1964/close-to-19-of-mined-bitcoin-blocks-were-empty" rel="nofollow noreferrer">Close to 19% of mined Bitcoin blocks were empty</a> - Steven Zheng.
October 17, 2018</li>
<li><a href="https://www.theblockcrypto.com/post/67928/bitcoin-miners-are-mining-fewer-empty-blocks-in-2020-and-it-may-not-all-because-of-chance" rel="nofollow noreferrer">A total of 71 empty blocks were mined in the first five months of 2020, accounting for 0.3% of the total blocks produced</a></li>
</ul>
<p><a href="https://i.stack.imgur.com/XqAL6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XqAL6.png" alt="Average number of BTC transactions per block" /></a> (<a href="https://cointelegraph.com/news/the-current-state-of-the-bitcoin-network-and-its-biggest-block" rel="nofollow noreferrer">source: Cointelegraph</a>)</p>
