Difference between revisions of "Mining - Extra Nonce"

From Veriblock Wiki
Jump to: navigation, search
(Created page with "See: Technical_Articles __TOC__ === Introduction === VeriBlock provides a 64-bit integer extra nonce field that is factored into the computation of the merkle root, and...")
 
 
Line 9: Line 9:
  
 
<pre>
 
<pre>
{
+
{
"command": "MINING_JOB",
+
  "command":"MINING_JOB",
"request_id": {
+
  "request_id":{
"type": "REQUEST_ID",
+
      "type":"REQUEST_ID",
"data": 1919582661
+
      "data":1107123451
},
+
  },
"job_id": {
+
  "job_id":{
"type": "JOB_ID",
+
      "type":"JOB_ID",
"data": 2555
+
      "data":224
},
+
  },
"block_index": {
+
  "block_index":{
"type": "BLOCK_INDEX",
+
      "type":"BLOCK_INDEX",
"data": 326661
+
      "data":1010
},
+
  },
"block_version": {
+
  "block_version":{
"type": "BLOCK_VERSION",
+
      "type":"BLOCK_VERSION",
"data": 1
+
      "data":2
},
+
  },
"previous_block_hash": {
+
  "previous_block_hash":{
"type": "BLOCK_HASH",
+
      "type":"BLOCK_HASH",
"data": "0000000000036C36D081B6E1A3A1F632F6D580642E0EF43D"
+
      "data":"0000000000009AD1F05D1C1A6B79DF3E3B59711579D3BB37"
},
+
  },
"second_previous_block_hash": {
+
  "second_previous_block_hash":{
"type": "BLOCK_HASH",
+
      "type":"BLOCK_HASH",
"data": "000000000001785388664F04161BF23CC968F26CD868D5F6"
+
      "data":"000000000000C09BE6D14004A7D8F2DD91B99CF316246920"
},
+
  },
"third_previous_block_hash": {
+
  "third_previous_block_hash":{
"type": "BLOCK_HASH",
+
      "type":"BLOCK_HASH",
"data": "00000000000348E63152B5F9F71F86D69CA2117FB6BAEDB2"
+
      "data":"000000000001917F9EE65F9B3BD9DB54CB81C3383034C4AA"
},
+
  },
"pool_address": {
+
  "pool_address":{
"type": "ADDRESS",
+
      "type":"ADDRESS",
"data": "V7GwB117zoE58Htr57gDVb6Wg8GM7E"
+
      "data":"VCTHaNxhxGPCuwG1eUKHg5iLZ68aG9"
},
+
  },
"merkle_root": {
+
  "merkle_root":{
"type": "TOP_LEVEL_MERKLE_ROOT",
+
      "type":"TOP_LEVEL_MERKLE_ROOT",
"data": "D108A340DB59542A3887F9E238BE367A050BD0686B8D40FE"
+
      "data":"78F71FD9F518B5B43738477D84A3421ABDABFE2EDC503165"
},
+
  },
"timestamp": {
+
  "timestamp":{
"type": "TIMESTAMP",
+
      "type":"TIMESTAMP",
"data": 1547055606
+
      "data":1554374816
},
+
  },
"difficulty": {
+
  "difficulty":{
"type": "DIFFICULTY",
+
      "type":"DIFFICULTY",
"data": 103841256
+
      "data":117480792
},
+
  },
"mining_target": {
+
  "mining_target":{
"type": "TARGET",
+
      "type":"TARGET",
"data": "000000ffffffffffffffffffffffffffffffffffffffffff"
+
      "data":"00000000ffffffffffffffffffffffffffffffffffffffff"
},
+
  },
"ledger_hash": {
+
  "ledger_hash":{
"type": "LEDGER_HASH",
+
      "type":"LEDGER_HASH",
"data": "CFA51D122F7AEE447EF3479604EEBC67F31E5C970495ED0E"
+
      "data":"A6FD19B41E1F97F887C87BA5D785432A2061EA23F3ACD055"
},
+
  },
"coinbase_txid": {
+
  "coinbase_txid":{
"type": "TRANSACTION_ID",
+
      "type":"TRANSACTION_ID",
"data": "619E8E714BD4D8653B1D8E7B0A5DD97D41031E97AD19B1F8301BD1338F5D848F"
+
      "data":"E07B589BA4903404FA8A6E228A988826AE510105A45213E3E3A21E41BECD2334"
},
+
  },
"pop_datastore_hash": {
+
  "pop_datastore_hash":{
"type": "POP_DATASTORE_HASH",
+
      "type":"POP_DATASTORE_HASH",
"data": "F5A5FD42D16A20302798EF6ED309979B43003D2320D9F0E8EA9831A92759FB4B"
+
      "data":"F5A5FD42D16A20302798EF6ED309979B43003D2320D9F0E8EA9831A92759FB4B"
},
+
  },
"miner_comment": {
+
  "miner_comment":{
"type": "MINER_COMMENT",
+
      "type":"MINER_COMMENT",
"data": ""
+
      "data":""
},
+
  },
"pop_transaction_merkle_root": {
+
  "pop_transaction_merkle_root":{
"type": "INTERMEDIATE_LEVEL_MERKLE_ROOT",
+
      "type":"INTERMEDIATE_LEVEL_MERKLE_ROOT",
"data": "E46ABBD3921267FC9070FBB32C554F3740BC6398D4B7E5EE21BD240A686FF379"
+
      "data":"0000000000000000000000000000000000000000000000000000000000000000"
},
+
  },
"normal_transaction_merkle_root": {
+
  "normal_transaction_merkle_root":{
"type": "INTERMEDIATE_LEVEL_MERKLE_ROOT",
+
      "type":"INTERMEDIATE_LEVEL_MERKLE_ROOT",
"data": "2F22E087E3441CCD5E8857ECD66D5B5B5E528E82B815187A39129066991D637F"
+
      "data":"0000000000000000000000000000000000000000000000000000000000000000"
},
+
  },
"extra_nonce_start": {
+
  "intermediate_metapackage_hash":{ 
"type": "EXTRA_NONCE",
+
      "type":"INTERMEDIATE_METAPACKAGE_HASH",
"data": 0
+
      "data":"219819F77F0E0DB759594E61F3952C168231D6FEE0C6C30AFC933A120914BECE"
},
+
  },
"extra_nonce_end": {
+
  "extra_nonce_start":{
"type": "EXTRA_NONCE",
+
      "type":"EXTRA_NONCE",
"data": 999999
+
      "data":0
}
+
  },
 +
  "extra_nonce_end":{
 +
      "type":"EXTRA_NONCE",
 +
      "data":999999
 +
  }
 
}
 
}
 
</pre>
 
</pre>
  
In this message, we're able to identify the merkle root that will be used as a component of the block hash computation. The value in the above message is: D108A340DB59542A3887F9E238BE367A050BD0686B8D40FE. The last 8 properties in the above message represent raw ingredients that go into the computation of the merkle root.
+
 
 +
In this message, we're able to identify the merkle root that will be used as a component of the block hash computation. The value in the above message is: 78F71FD9F518B5B43738477D84A3421ABDABFE2EDC503165. The last 12 properties in the above message represent raw ingredients that go into the computation of the merkle root.
  
 
=== VeriBlock Merkle Tree ===
 
=== VeriBlock Merkle Tree ===
Before delving into the computation, it's important to understand a few things about the VeriBlock merkle tree. It is somewhat unique in that it is essentially composed of three sub-trees. The three sub-trees represent the "Block Content Metapackage", the PoP Transactions merkle tree and the "normal" transactions merkle tree. The PoP transaction merkle tree and normal transaction merkle tree are each typical transaction merkle trees. If we were to represent this in a hierarchical format, it would look like this:
+
Before delving into the computation, it's important to understand a few things about the VeriBlock merkle tree. It is somewhat unique in that it is essentially composed of three sub-trees. The three sub-trees represent the "Block Content Metapackage", the PoP Transactions Merkle tree and the "normal" transactions Merkle tree. The PoP transaction Merkle tree and normal transaction Merkle tree are each typical transaction Merkle trees. If we were to represent this in a hierarchical format, it would look like this:
  
 
<pre>
 
<pre>
Line 110: Line 115:
 
Referring back to our sample UCP Mining Job message, we see that we are given the merkle roots for each of the transaction trees in the properties "pop_transaction_merkle_root" and "normal_transaction_merkle_root". Now, to calculate the metapackage merkle root:
 
Referring back to our sample UCP Mining Job message, we see that we are given the merkle roots for each of the transaction trees in the properties "pop_transaction_merkle_root" and "normal_transaction_merkle_root". Now, to calculate the metapackage merkle root:
  
# There are ALWAYS four leaf nodes in the metapackage.  
+
# The intermediate hash of the intermediate_metapackage_hash is provided.  
## Coinbase TxId ("coinbase_txid") - Represented as a hex string in the message, should be converted to byte[]
+
# Concatenate the intermediate_metapackage_hash and the extra nonce (64-bit integer), represented as a byte[]
## PoP Datastore Hash ("pop_datastore_hash") - Represented as a hex string in the message, should be converted to byte[]
 
## Miner Comment ("miner_comment") - Represented as a UTF-8 string in the message, should be converted to byte[]
 
## Ledger Hash ("ledger_hash") - Represented as a hex string in the message, should be converted to byte[]
 
# Each leaf node in raw byte[] format is Sha256 hashed and built into an intermediate merkle root
 
# Concatenate the resulting intermediate merkle root and the extra nonce (64-bit integer), represented as a byte[]
 
 
# Sha256 hash the concatenated value
 
# Sha256 hash the concatenated value
  
 
=== Merkle Root ===
 
=== Merkle Root ===
  
Having produced the metapackage hash above, it can be joined with the transaction root hash to produce the merkle root.  The Mining Job message is initially computed with the "extra_nonce_start" value used in the above metapackage computation. The final merkle root is trimmed to the first 24 bytes.
+
Having produced the complete metapackage hash above, it can be joined with the transaction root hash to produce the top-level Merkle root.  The Mining Job message is initially computed with the "extra_nonce_start" value used in the above metapackage computation. The final merkle root is trimmed to the first 24 bytes.
  
 
=== Sample Java Test ===
 
=== Sample Java Test ===
Below is a sample test, written in Java, that computes the merkle root from the properties supplied in the Mining Job message above. Please note that the "Crypto" and "Utility" methods are just simple helpers to make the code more readable.
+
Below is a sample program, written in Java, that computes the top-level Merkle root from the properties supplied in the Mining Job message above.
 
<pre>
 
<pre>
    @Test
+
import javax.xml.bind.DatatypeConverter;
    public void calculateMerkleRoot() {
+
import java.nio.charset.StandardCharsets;
        String popTxMerkleRoot = "E46ABBD3921267FC9070FBB32C554F3740BC6398D4B7E5EE21BD240A686FF379";
+
import java.security.MessageDigest;
        String normalTxMerkleRoot = "2F22E087E3441CCD5E8857ECD66D5B5B5E528E82B815187A39129066991D637F";
+
import java.security.NoSuchAlgorithmException;
  
        String ledgerHash = "CFA51D122F7AEE447EF3479604EEBC67F31E5C970495ED0E";
+
public class ExtraNonceDemo {
         String coinbaseTxId = "619E8E714BD4D8653B1D8E7B0A5DD97D41031E97AD19B1F8301BD1338F5D848F";
+
    public static void main(String[] args) {
         String popDatastoreHash = "F5A5FD42D16A20302798EF6ED309979B43003D2320D9F0E8EA9831A92759FB4B";
+
         String popTxMerkleRoot = "0000000000000000000000000000000000000000000000000000000000000000";
         String minerComment = "";
+
         String normalTxMerkleRoot = "0000000000000000000000000000000000000000000000000000000000000000";
 +
 
 +
         String intermediateMetapackageHash = "219819F77F0E0DB759594E61F3952C168231D6FEE0C6C30AFC933A120914BECE";
  
 
         long extraNonce = 0L;
 
         long extraNonce = 0L;
  
         Crypto c = new Crypto();
+
         byte[] intermediateMetapackageHashBytes = DatatypeConverter.parseHexBinary(intermediateMetapackageHash);
  
        byte[] hashCoinbaseTxId = c.SHA256ReturnBytes(Utility.hexToBytes(coinbaseTxId));
 
        byte[] hashPoPDatastoreHash = c.SHA256ReturnBytes(Utility.hexToBytes(popDatastoreHash));
 
        byte[] hashMinerComment = c.SHA256ReturnBytes(minerComment.getBytes());
 
        byte[] hashLedgerHash = c.SHA256ReturnBytes(Utility.hexToBytes(ledgerHash));
 
 
        // Calculate interim merkle root for block content metapackage. Needs doing 1 time
 
        byte[] leftParent = c.SHA256ReturnBytes(Utility.concat(hashCoinbaseTxId, hashPoPDatastoreHash));
 
        byte[] rightParent = c.SHA256ReturnBytes(Utility.concat(hashMinerComment, hashLedgerHash));
 
        byte[] interimRoot = c.SHA256ReturnBytes(Utility.concat(leftParent, rightParent));
 
  
 
         // Calculate the block content metapackage with the extra nonce
 
         // Calculate the block content metapackage with the extra nonce
         byte[] blockContentMerkleRootWithExtraNonce = c.SHA256ReturnBytes(Utility.concat(interimRoot, Utility.longToByteArray(extraNonce)));
+
         byte[] blockContentMerkleRootWithExtraNonce = SHA256ReturnBytes(concat(intermediateMetapackageHashBytes, longToByteArray(extraNonce)));
  
         byte[] txMerkleRoot = c.SHA256ReturnBytes(Utility.concat(Utility.hexToBytes(popTxMerkleRoot), Utility.hexToBytes(normalTxMerkleRoot)));
+
         byte[] txMerkleRoot = SHA256ReturnBytes(concat(DatatypeConverter.parseHexBinary(popTxMerkleRoot), DatatypeConverter.parseHexBinary(normalTxMerkleRoot)));
  
         byte[] merkleRoot = c.SHA256ReturnBytes(Utility.concat(blockContentMerkleRootWithExtraNonce, txMerkleRoot));
+
         byte[] merkleRoot = SHA256ReturnBytes(concat(blockContentMerkleRootWithExtraNonce, txMerkleRoot));
 
         byte[] trimmedContentMerkleRoot = new byte[24];
 
         byte[] trimmedContentMerkleRoot = new byte[24];
 
         System.arraycopy(merkleRoot, 0, trimmedContentMerkleRoot, 0, trimmedContentMerkleRoot.length);
 
         System.arraycopy(merkleRoot, 0, trimmedContentMerkleRoot, 0, trimmedContentMerkleRoot.length);
  
         Assert.assertEquals("D108A340DB59542A3887F9E238BE367A050BD0686B8D40FE", Utility.bytesToHex(trimmedContentMerkleRoot));
+
         System.out.println(DatatypeConverter.printHexBinary(trimmedContentMerkleRoot));
 +
    }
 +
 
 +
    public static byte[] longToByteArray(long input) {
 +
        return new byte[]{
 +
                (byte) ((input & 0xFF00000000000000l) >> 56),
 +
                (byte) ((input & 0x00FF000000000000l) >> 48),
 +
                (byte) ((input & 0x0000FF0000000000l) >> 40),
 +
                (byte) ((input & 0x000000FF00000000l) >> 32),
 +
                (byte) ((input & 0x00000000FF000000l) >> 24),
 +
                (byte) ((input & 0x0000000000FF0000l) >> 16),
 +
                (byte) ((input & 0x000000000000FF00l) >> 8),
 +
                (byte) ((input & 0x00000000000000FFl)),
 +
        };
 +
    }
 +
 
 +
    public static byte[] concat(byte[] first, byte[] second) {
 +
        byte[] result = new byte[first.length + second.length];
 +
        System.arraycopy(first, 0, result, 0, first.length);
 +
        System.arraycopy(second, 0, result, first.length, second.length);
 +
        return result;
 +
    }
 +
 
 +
    public static byte[] SHA256ReturnBytes(String input) {
 +
        return SHA256ReturnBytes(input.getBytes(StandardCharsets.UTF_8));
 
     }
 
     }
</pre>
 
  
=== Extra Nonce ===
+
    public static byte[] SHA256ReturnBytes(byte[] input) {
Each subscribed UCP mining client is assigned an extra nonce range, represented above by the properties "extra_nonce_start" and "extra_nonce_end". Supplying values outside of your assigned range will result in a rejection of the submission.
+
        return _sha256.digest(input);
 +
    }
  
To aid in third-party implementations, below are the calculated merkle roots for 10 different extra nonce values in the range specified above.
 
  
Extra Nonce: '''112012''' -> D8FF2E84224B7363DBFC4D90048B5BE7089B6F61BD2E25A7
 
  
Extra Nonce: '''247465''' -> 1D1FA061D6651B4CFCA5DFD29E9DFA50DF24F291CDA91B09
+
    private static MessageDigest _sha256;
  
Extra Nonce: '''452910''' -> F557864EE74FB7CCBBC78E02F815F870D43F21CAD0A3CD2C
+
    static {
 +
        try {
 +
            _sha256 = MessageDigest.getInstance("SHA-256");
 +
        } catch (
 +
                NoSuchAlgorithmException e) {
 +
            e.printStackTrace();
 +
        }
 +
    }
 +
}
 +
</pre>
  
Extra Nonce: '''462112''' -> E35E0EA8045C8FE7065CA4F1346F87824BA02AB97DC17D0A
+
=== Extra Nonce ===
 +
Each subscribed UCP mining client is assigned an extra nonce range, represented above by the properties "extra_nonce_start" and "extra_nonce_end". Supplying values outside of your assigned range will result in a rejection of the submission.
  
Extra Nonce: '''421824''' -> 87FBE908EB218BF921897E8837F9949988943C2617A789EC
+
To aid in third-party implementations, below are the calculated merkle roots for 5 different extra nonce values in the range specified above.
  
Extra Nonce: '''204383''' -> CB167431CE24C52CF67508787BCD053212B0A52BF19EA428
+
Extra Nonce: '''552''' -> A4D09298321FE8C5893F45A07633EB80E044612F22ACC7BB
  
Extra Nonce: '''623725''' -> F9BBB077EFA29253B64A818D1E38DAD4C948E7A1F1839E74
+
Extra Nonce: '''983''' -> C349240F588FBBBBCD3BC9A1DD99DC5FDB4E17A498AC956C
  
Extra Nonce: '''563624''' -> 907DFEF38E2DA95ED44E4CBF9E100C550FAB5098C9CB6174
+
Extra Nonce: '''10478''' -> CBA028C21DCDFAF3CF2FACF3192A6DB5C4647103F33C3F08
  
Extra Nonce: '''558308''' -> EF1BD48534F6F6FF6A9C871087A6700FD850E4233BBE6398
+
Extra Nonce: '''55231''' -> 38E87C83FE132D5235D5364AB82103CBC5EBCA2CA567F0AB
  
Extra Nonce: '''855216''' -> A7795794A9F7C03FB43430BEE71E13B00B6A8B9349156E11
+
Extra Nonce: '''887166''' -> 025D310D312997A50A46A654FAE78EE0CDF9F55190A52CDA

Latest revision as of 11:04, 4 April 2019

See: Technical_Articles

Introduction

VeriBlock provides a 64-bit integer extra nonce field that is factored into the computation of the merkle root, and hence the block hash. This field should be used as opposed to common alternatives, such as timestamp manipulation. The aim of this article is to provide an explanation of the VeriBlock merkle root and the extra nonce field's role.

Let's take an example UCP Mining Job message to use throughout this document.

{  
   "command":"MINING_JOB",
   "request_id":{  
      "type":"REQUEST_ID",
      "data":1107123451
   },
   "job_id":{  
      "type":"JOB_ID",
      "data":224
   },
   "block_index":{  
      "type":"BLOCK_INDEX",
      "data":1010
   },
   "block_version":{  
      "type":"BLOCK_VERSION",
      "data":2
   },
   "previous_block_hash":{  
      "type":"BLOCK_HASH",
      "data":"0000000000009AD1F05D1C1A6B79DF3E3B59711579D3BB37"
   },
   "second_previous_block_hash":{  
      "type":"BLOCK_HASH",
      "data":"000000000000C09BE6D14004A7D8F2DD91B99CF316246920"
   },
   "third_previous_block_hash":{  
      "type":"BLOCK_HASH",
      "data":"000000000001917F9EE65F9B3BD9DB54CB81C3383034C4AA"
   },
   "pool_address":{  
      "type":"ADDRESS",
      "data":"VCTHaNxhxGPCuwG1eUKHg5iLZ68aG9"
   },
   "merkle_root":{  
      "type":"TOP_LEVEL_MERKLE_ROOT",
      "data":"78F71FD9F518B5B43738477D84A3421ABDABFE2EDC503165"
   },
   "timestamp":{  
      "type":"TIMESTAMP",
      "data":1554374816
   },
   "difficulty":{  
      "type":"DIFFICULTY",
      "data":117480792
   },
   "mining_target":{  
      "type":"TARGET",
      "data":"00000000ffffffffffffffffffffffffffffffffffffffff"
   },
   "ledger_hash":{  
      "type":"LEDGER_HASH",
      "data":"A6FD19B41E1F97F887C87BA5D785432A2061EA23F3ACD055"
   },
   "coinbase_txid":{  
      "type":"TRANSACTION_ID",
      "data":"E07B589BA4903404FA8A6E228A988826AE510105A45213E3E3A21E41BECD2334"
   },
   "pop_datastore_hash":{  
      "type":"POP_DATASTORE_HASH",
      "data":"F5A5FD42D16A20302798EF6ED309979B43003D2320D9F0E8EA9831A92759FB4B"
   },
   "miner_comment":{  
      "type":"MINER_COMMENT",
      "data":""
   },
   "pop_transaction_merkle_root":{  
      "type":"INTERMEDIATE_LEVEL_MERKLE_ROOT",
      "data":"0000000000000000000000000000000000000000000000000000000000000000"
   },
   "normal_transaction_merkle_root":{  
      "type":"INTERMEDIATE_LEVEL_MERKLE_ROOT",
      "data":"0000000000000000000000000000000000000000000000000000000000000000"
   },
   "intermediate_metapackage_hash":{  
      "type":"INTERMEDIATE_METAPACKAGE_HASH",
      "data":"219819F77F0E0DB759594E61F3952C168231D6FEE0C6C30AFC933A120914BECE"
   },
   "extra_nonce_start":{  
      "type":"EXTRA_NONCE",
      "data":0
   },
   "extra_nonce_end":{  
      "type":"EXTRA_NONCE",
      "data":999999
   }
}


In this message, we're able to identify the merkle root that will be used as a component of the block hash computation. The value in the above message is: 78F71FD9F518B5B43738477D84A3421ABDABFE2EDC503165. The last 12 properties in the above message represent raw ingredients that go into the computation of the merkle root.

VeriBlock Merkle Tree

Before delving into the computation, it's important to understand a few things about the VeriBlock merkle tree. It is somewhat unique in that it is essentially composed of three sub-trees. The three sub-trees represent the "Block Content Metapackage", the PoP Transactions Merkle tree and the "normal" transactions Merkle tree. The PoP transaction Merkle tree and normal transaction Merkle tree are each typical transaction Merkle trees. If we were to represent this in a hierarchical format, it would look like this:

                   [MERKLE_ROOT]
               /                   \
[METAPACKAGE_ROOT]               [TRANSACTION_ROOT]
                               /                    \
            [POP_TRANSACTION_ROOT]              [NORMAL_TRANSACTION_ROOT]

Block Content Metapackage

Referring back to our sample UCP Mining Job message, we see that we are given the merkle roots for each of the transaction trees in the properties "pop_transaction_merkle_root" and "normal_transaction_merkle_root". Now, to calculate the metapackage merkle root:

  1. The intermediate hash of the intermediate_metapackage_hash is provided.
  2. Concatenate the intermediate_metapackage_hash and the extra nonce (64-bit integer), represented as a byte[]
  3. Sha256 hash the concatenated value

Merkle Root

Having produced the complete metapackage hash above, it can be joined with the transaction root hash to produce the top-level Merkle root. The Mining Job message is initially computed with the "extra_nonce_start" value used in the above metapackage computation. The final merkle root is trimmed to the first 24 bytes.

Sample Java Test

Below is a sample program, written in Java, that computes the top-level Merkle root from the properties supplied in the Mining Job message above.

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ExtraNonceDemo {
    public static void main(String[] args) {
        String popTxMerkleRoot = "0000000000000000000000000000000000000000000000000000000000000000";
        String normalTxMerkleRoot = "0000000000000000000000000000000000000000000000000000000000000000";

        String intermediateMetapackageHash = "219819F77F0E0DB759594E61F3952C168231D6FEE0C6C30AFC933A120914BECE";

        long extraNonce = 0L;

        byte[] intermediateMetapackageHashBytes = DatatypeConverter.parseHexBinary(intermediateMetapackageHash);


        // Calculate the block content metapackage with the extra nonce
        byte[] blockContentMerkleRootWithExtraNonce = SHA256ReturnBytes(concat(intermediateMetapackageHashBytes, longToByteArray(extraNonce)));

        byte[] txMerkleRoot = SHA256ReturnBytes(concat(DatatypeConverter.parseHexBinary(popTxMerkleRoot), DatatypeConverter.parseHexBinary(normalTxMerkleRoot)));

        byte[] merkleRoot = SHA256ReturnBytes(concat(blockContentMerkleRootWithExtraNonce, txMerkleRoot));
        byte[] trimmedContentMerkleRoot = new byte[24];
        System.arraycopy(merkleRoot, 0, trimmedContentMerkleRoot, 0, trimmedContentMerkleRoot.length);

        System.out.println(DatatypeConverter.printHexBinary(trimmedContentMerkleRoot));
    }

    public static byte[] longToByteArray(long input) {
        return new byte[]{
                (byte) ((input & 0xFF00000000000000l) >> 56),
                (byte) ((input & 0x00FF000000000000l) >> 48),
                (byte) ((input & 0x0000FF0000000000l) >> 40),
                (byte) ((input & 0x000000FF00000000l) >> 32),
                (byte) ((input & 0x00000000FF000000l) >> 24),
                (byte) ((input & 0x0000000000FF0000l) >> 16),
                (byte) ((input & 0x000000000000FF00l) >> 8),
                (byte) ((input & 0x00000000000000FFl)),
        };
    }

    public static byte[] concat(byte[] first, byte[] second) {
        byte[] result = new byte[first.length + second.length];
        System.arraycopy(first, 0, result, 0, first.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    public static byte[] SHA256ReturnBytes(String input) {
        return SHA256ReturnBytes(input.getBytes(StandardCharsets.UTF_8));
    }

    public static byte[] SHA256ReturnBytes(byte[] input) {
        return _sha256.digest(input);
    }



    private static MessageDigest _sha256;

    static {
        try {
            _sha256 = MessageDigest.getInstance("SHA-256");
        } catch (
                NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}

Extra Nonce

Each subscribed UCP mining client is assigned an extra nonce range, represented above by the properties "extra_nonce_start" and "extra_nonce_end". Supplying values outside of your assigned range will result in a rejection of the submission.

To aid in third-party implementations, below are the calculated merkle roots for 5 different extra nonce values in the range specified above.

Extra Nonce: 552 -> A4D09298321FE8C5893F45A07633EB80E044612F22ACC7BB

Extra Nonce: 983 -> C349240F588FBBBBCD3BC9A1DD99DC5FDB4E17A498AC956C

Extra Nonce: 10478 -> CBA028C21DCDFAF3CF2FACF3192A6DB5C4647103F33C3F08

Extra Nonce: 55231 -> 38E87C83FE132D5235D5364AB82103CBC5EBCA2CA567F0AB

Extra Nonce: 887166 -> 025D310D312997A50A46A654FAE78EE0CDF9F55190A52CDA