minotari_node 5.4.0-pre.0

The tari full base node implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright 2025. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use hyper::{Response, StatusCode, body::Bytes};
use log::{debug, info, trace, warn};
use serde_json::{Value, json};
use tari_common_types::{
    tari_address::TariAddress,
    types::{
        BlockHash,
        CompressedCommitment,
        CompressedPublicKey,
        CompressedSignature,
        UncompressedCommitment,
        UncompressedPublicKey,
    },
};
use tari_core::{
    base_node::{LocalNodeCommsInterface, StateMachineHandle},
    consensus::BaseNodeConsensusManager,
    validation::tari_rx_vm_key_height,
};
use tari_transaction_components::{
    generate_coinbase_with_wallet_output,
    key_manager::{KeyManager, TariKeyId, TransactionKeyManagerInterface, TxoStage},
    tari_proof_of_work::PowAlgorithm,
    transaction_components::{
        CoinBaseExtra,
        KernelBuilder,
        RangeProofType,
        TransactionKernel,
        TransactionKernelVersion,
        memo_field::{MemoField, TxType},
    },
};
use tari_utilities::ByteArray;

use super::{
    block_template_storage::BlockTemplateStorage,
    error::XmrigProxyError,
    service::{ProxyBody, json_response},
};

const LOG_TARGET: &str = "minotari::base_node::xmrig_proxy";

/// The byte offset in the 76-byte mining blob where the extra nonce starts.
/// XMRig writes a per-thread extra nonce here so mining threads don't duplicate work.
/// This corresponds to the high 4 bytes of the u64 nonce field.
pub const TARI_BLOB_RESERVED_OFFSET: u32 = 35;

/// The total size of the Tari mining blob in bytes.
const TARI_MINING_BLOB_SIZE: usize = 76;
/// The pow_algo byte value for RandomXT (= 2).
const POW_ALGO_RANDOMXT: u8 = 2;

#[derive(Clone)]
pub struct InnerService {
    pub node_service: LocalNodeCommsInterface,
    pub consensus_rules: BaseNodeConsensusManager,
    /// State machine handle, available for future sync-status checks.
    #[allow(dead_code)]
    pub state_machine: StateMachineHandle,
    pub block_templates: BlockTemplateStorage,
    pub wallet_payment_address: TariAddress,
    pub coinbase_extra: Vec<u8>,
    pub range_proof_type: RangeProofType,
}

#[derive(Clone, Copy, Debug)]
struct ChainTip {
    height: u64,
    top_hash: BlockHash,
}

impl InnerService {
    /// Handle a JSON-RPC request from the miner.
    pub async fn handle(&self, body: Bytes) -> Result<Response<ProxyBody>, XmrigProxyError> {
        let json: Value = serde_json::from_slice(&body)?;
        let method = json.get("method").and_then(Value::as_str).unwrap_or("");
        trace!(target: LOG_TARGET, "Received method: {method}");
        match method {
            "getblocktemplate" => self.handle_get_block_template(&json).await,
            "submitblock" => self.handle_submit_block(&json).await,
            "getblockcount" | "get_height" => self.handle_get_height(&json).await,
            "getheight" => self.handle_get_height_hash().await,
            "getinfo" => self.handle_get_info().await,
            _ => {
                debug!(target: LOG_TARGET, "Unknown method: {method}");
                json_response(
                    StatusCode::OK,
                    &json_rpc_error(
                        json.get("id").map(|v| v.as_i64()).unwrap_or_default(),
                        -32601,
                        "Method not found",
                    ),
                )
            },
        }
    }

    /// Fetch the current chain tip height and block hash from the node.
    async fn get_chain_tip(&self) -> Result<ChainTip, XmrigProxyError> {
        let mut handler = self.node_service.clone();
        let meta = handler.get_metadata().await?;
        Ok(ChainTip {
            height: meta.best_block_height(),
            top_hash: *meta.best_block_hash(),
        })
    }

    /// Handle GET /get_height, /getinfo, /getheight requests (some mining software uses these).
    pub async fn handle_get(&self, path: &str) -> Result<Response<ProxyBody>, XmrigProxyError> {
        match path {
            "/get_height" | "/getblockcount" => self.handle_get_height(&json!({})).await,
            "/getheight" => self.handle_get_height_hash().await,
            "/getinfo" | "/get_info" => self.handle_get_info().await,
            _ => json_response(StatusCode::NOT_FOUND, &json!({"error": "Not found"})),
        }
    }

    async fn handle_get_height(&self, req: &Value) -> Result<Response<ProxyBody>, XmrigProxyError> {
        let tip = self.get_chain_tip().await?;
        json_response(
            StatusCode::OK,
            &json_rpc_success(
                req["id"].get("id").map(|v| v.as_i64()).unwrap_or_default(),
                json!({ "count": tip.height, "status": "OK" }),
            ),
        )
    }

    async fn handle_get_height_hash(&self) -> Result<Response<ProxyBody>, XmrigProxyError> {
        let tip = self.get_chain_tip().await?;
        json_response(
            StatusCode::OK,
            &json!({
                "height": tip.height,
                "hash": format!("{}", tip.top_hash),
                "status": "OK",
            }),
        )
    }

    async fn handle_get_info(&self) -> Result<Response<ProxyBody>, XmrigProxyError> {
        let tip = self.get_chain_tip().await?;
        json_response(
            StatusCode::OK,
            &json!({
                "top_block_hash": format!("{}", tip.top_hash),
                "height": tip.height,
                "status": "OK",
            }),
        )
    }

    #[allow(clippy::too_many_lines)]
    async fn handle_get_block_template(&self, req: &Value) -> Result<Response<ProxyBody>, XmrigProxyError> {
        let mut handler = self.node_service.clone();

        // Get chain metadata to determine block height for weight/coinbase calculations
        let meta = handler.get_metadata().await?;
        let next_height = meta.best_block_height().saturating_add(1);

        let constants = self.consensus_rules.consensus_constants(next_height);
        let asking_weight = constants.max_block_transaction_weight();

        // Get a new RandomXT block template from the local node
        let mut new_template = handler
            .get_new_block_template(PowAlgorithm::RandomXT, asking_weight)
            .await
            .map_err(|e| {
                warn!(target: LOG_TARGET, "Failed to get block template: {e}");
                e
            })?;

        let height = new_template.header.height;
        // Capture target_difficulty from the template before it's consumed by get_new_block
        let target_difficulty = new_template.target_difficulty.as_u64();

        // Calculate the coinbase reward for this block
        let reward = self
            .consensus_rules
            .calculate_coinbase_and_fees(height, new_template.body.kernels())
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?
            .as_u64();

        // Validate coinbase count
        let max_coinbases = self
            .consensus_rules
            .consensus_constants(height)
            .max_block_coinbase_count();
        if 1 > max_coinbases {
            return Err(XmrigProxyError::InternalError(
                "No coinbases allowed by consensus".to_string(),
            ));
        }

        // Generate the coinbase output and kernel for our wallet address
        let coinbase_extra = CoinBaseExtra::try_from(self.coinbase_extra.clone())
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;
        let key_manager = KeyManager::new_random().map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;
        let script_key_id = TariKeyId::default();

        let (_, coinbase_output, coinbase_kernel, wallet_output) = generate_coinbase_with_wallet_output(
            0.into(),
            reward.into(),
            height,
            &coinbase_extra,
            &key_manager,
            &script_key_id,
            &self.wallet_payment_address,
            false, // stealth_payment
            constants,
            self.range_proof_type,
            MemoField::new_open(vec![], TxType::Coinbase).expect("empty user-data should always be valid"),
        )
        .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;

        new_template.body.add_output(coinbase_output);

        // Build the kernel signature
        let new_nonce = key_manager
            .get_random_key(None, None)
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;
        let total_nonce: UncompressedPublicKey = new_nonce
            .pub_key
            .to_public_key()
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;
        let total_excess: UncompressedCommitment = coinbase_kernel
            .excess
            .to_commitment()
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;
        let kernel_message = TransactionKernel::build_kernel_signature_message(
            TransactionKernelVersion::get_current_version(),
            coinbase_kernel.fee,
            coinbase_kernel.lock_height,
            &coinbase_kernel.features,
            &None,
        );
        let kernel_signature = key_manager
            .get_partial_txo_kernel_signature(
                wallet_output.commitment_mask_key_id(),
                &new_nonce.key_id,
                &CompressedPublicKey::new_from_pk(total_nonce),
                &CompressedPublicKey::new_from_pk(total_excess.as_public_key().clone()),
                TransactionKernelVersion::get_current_version(),
                &kernel_message,
                &coinbase_kernel.features,
                TxoStage::Output,
            )
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?
            .to_schnorr_signature()
            .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?;

        let kernel_new = KernelBuilder::new()
            .with_fee(0.into())
            .with_features(coinbase_kernel.features)
            .with_lock_height(coinbase_kernel.lock_height)
            .with_excess(&CompressedCommitment::from_commitment(
                coinbase_kernel
                    .excess
                    .to_commitment()
                    .map_err(|e| XmrigProxyError::InternalError(e.to_string()))?,
            ))
            .with_signature(CompressedSignature::new_from_schnorr(kernel_signature))
            .build()
            .unwrap();

        new_template.body.add_kernel(kernel_new);
        new_template.body.sort();

        // Ask the node to finalize the block (fills in MMR roots etc.)
        let new_block = handler.get_new_block(new_template).await.map_err(|e| {
            warn!(target: LOG_TARGET, "Failed to get new block: {e}");
            e
        })?;

        let block_height = new_block.header.height;
        let prev_hash = new_block.header.prev_hash.to_vec();

        // Compute the RandomXT mining hash
        let mining_hash = match new_block.header.pow.pow_algo {
            PowAlgorithm::RandomXT => new_block.header.mining_hash().to_vec(),
            algo => {
                return Err(XmrigProxyError::InternalError(format!(
                    "Expected RandomXT block template, got {algo:?}"
                )));
            },
        };

        if mining_hash.len() != 32 {
            return Err(XmrigProxyError::MissingData(format!(
                "mining_hash has wrong length: {}",
                mining_hash.len()
            )));
        }

        // Get the RandomX VM key (seed hash for XMRig) from the block at tari_rx_vm_key_height
        let vm_key_height = tari_rx_vm_key_height(block_height);
        let vm_key = *handler
            .get_header(vm_key_height)
            .await?
            .ok_or_else(|| XmrigProxyError::MissingData(format!("block header at height {vm_key_height} not found")))?
            .hash();

        // Build the 76-byte XMRig-compatible mining blob
        let blob = build_tari_mining_blob(&mining_hash, 0u64, POW_ALGO_RANDOMXT);
        let blob_hex = hex::encode(&blob);
        let seed_hex = hex::encode(vm_key);
        let prev_hash_hex = hex::encode(&prev_hash);

        let target_difficulty_val = target_difficulty;
        let expected_reward = reward;

        // Store the block template keyed by the 32-byte mining hash
        let mining_hash_key: [u8; 32] = mining_hash
            .as_slice()
            .try_into()
            .map_err(|_| XmrigProxyError::MissingData("mining hash not 32 bytes".to_string()))?;
        self.block_templates.store(mining_hash_key, new_block).await;

        debug!(
            target: LOG_TARGET,
            "Returning block template for height #{block_height}, seed={seed_hex}"
        );

        json_response(
            StatusCode::OK,
            &json_rpc_success(
                req["id"].as_i64(),
                json!({
                    "blocktemplate_blob": blob_hex,
                    "blockhashing_blob": blob_hex,
                    "seed_hash": seed_hex,
                    "difficulty": target_difficulty_val,
                    "height": block_height,
                    "prev_hash": prev_hash_hex,
                    "reserved_offset": TARI_BLOB_RESERVED_OFFSET,
                    "expected_reward": expected_reward,
                    "status": "OK",
                    "untrusted": false,
                }),
            ),
        )
    }

    async fn handle_submit_block(&self, req: &Value) -> Result<Response<ProxyBody>, XmrigProxyError> {
        let params = match req["params"].as_array() {
            Some(p) => p,
            None => {
                return json_response(
                    StatusCode::OK,
                    &json_rpc_error(req["id"].as_i64(), -32602, "params must be an array"),
                );
            },
        };

        let blob_hex = match params.first().and_then(Value::as_str) {
            Some(s) => s,
            None => {
                return json_response(
                    StatusCode::OK,
                    &json_rpc_error(req["id"].as_i64(), -32602, "params[0] must be a hex string"),
                );
            },
        };

        let blob = hex::decode(blob_hex).map_err(|e| XmrigProxyError::InvalidRequest(e.to_string()))?;

        if blob.len() != TARI_MINING_BLOB_SIZE {
            return json_response(
                StatusCode::OK,
                &json_rpc_error(
                    req["id"].as_i64(),
                    -32602,
                    &format!(
                        "submitted blob has wrong length: {} (expected {TARI_MINING_BLOB_SIZE})",
                        blob.len()
                    ),
                ),
            );
        }

        // Extract the 32-byte mining hash from bytes 3..35
        let mining_hash: [u8; 32] = blob
            .get(3..35)
            .ok_or(XmrigProxyError::InvalidRequest("bad mining hash slice".to_string()))?
            .try_into()
            .map_err(|_| XmrigProxyError::InvalidRequest("bad mining hash slice".to_string()))?;

        // Extract the 8-byte nonce from bytes 35..43 (big-endian u64)
        let nonce_bytes: [u8; 8] = blob
            .get(35..43)
            .ok_or(XmrigProxyError::InvalidRequest("bad mining hash slice".to_string()))?
            .try_into()
            .map_err(|_| XmrigProxyError::InvalidRequest("bad nonce slice".to_string()))?;
        let nonce = u64::from_be_bytes(nonce_bytes);

        // Look up the stored block template
        let mut block = match self.block_templates.take(&mining_hash).await {
            Some(b) => b,
            None => {
                let hash_hex = hex::encode(mining_hash);
                warn!(
                    target: LOG_TARGET,
                    "No block template found for mining hash {hash_hex} - possible duplicate submission"
                );
                return json_response(
                    StatusCode::OK,
                    &json_rpc_error(req["id"].as_i64(), -1, "Block template not found or already submitted"),
                );
            },
        };

        // Update the nonce in the block header
        block.header.nonce = nonce;

        let block_height = block.header.height;
        info!(target: LOG_TARGET, "Submitting block #{block_height} with nonce={nonce} to base node");

        // Submit to the base node via LocalNodeCommsInterface
        let mut handler = self.node_service.clone();
        match handler.submit_block(block).await {
            Ok(block_hash) => {
                let block_hash_hex = hex::encode(block_hash);
                info!(target: LOG_TARGET, "Block #{block_height} accepted, hash={block_hash_hex}");
                json_response(
                    StatusCode::OK,
                    &json_rpc_success(
                        req["id"].as_i64(),
                        json!({
                            "status": "OK",
                            "untrusted": false,
                        }),
                    ),
                )
            },
            Err(e) => {
                warn!(target: LOG_TARGET, "Block #{block_height} rejected: {e}");
                json_response(
                    StatusCode::OK,
                    &json_rpc_error(req["id"].as_i64(), -5, &format!("Block rejected: {e}")),
                )
            },
        }
    }
}

/// Build a 76-byte XMRig-compatible mining blob for Tari RandomXT.
///
/// Format:
/// ```text
/// | 3 bytes (zeros) | 32 bytes (mining_hash) | 8 bytes (nonce big-endian) | 33 bytes (pow_algo + zeros) |
/// ```
///
/// - Bytes 35..39: high nonce bytes (XMRig writes per-thread extra nonce here, at `reserved_offset`)
/// - Bytes 39..43: low nonce bytes (XMRig iterates this 4-byte field at the standard Monero nonce offset)
pub fn build_tari_mining_blob(mining_hash: &[u8], nonce: u64, pow_algo: u8) -> Vec<u8> {
    let mut blob = vec![0u8; 3];
    blob.extend_from_slice(mining_hash);
    blob.extend_from_slice(&nonce.to_be_bytes());
    blob.push(pow_algo);
    blob.extend_from_slice(&[0u8; 32]);
    blob
}

fn json_rpc_success(id: Option<i64>, result: Value) -> Value {
    json!({
        "id": id.unwrap_or(-1),
        "jsonrpc": "2.0",
        "result": result,
    })
}

fn json_rpc_error(id: Option<i64>, code: i32, message: &str) -> Value {
    json!({
        "id": id.unwrap_or(-1),
        "jsonrpc": "2.0",
        "error": {
            "code": code,
            "message": message,
        },
    })
}