Skip to content

Mining Modes

Mining modes determine when transactions are included in blocks and confirmed. By choosing the right mining mode, you can accurately simulate real blockchain networks or optimize for specific testing scenarios.

Available Mining Modes

Auto Mining

Mines a block after each transaction

⏱️ Interval Mining

Mines blocks at fixed time intervals

🔨 Manual Mining

Only mines when explicitly requested

Gas-Based Mining

Mines when gas threshold is reached

Auto Mining
const node = createTevmNode({
  miningConfig: {

    type: "auto", 
  }, 
});
 
// Send a transaction - it will be mined immediately
const txHash = await node.sendTransaction({
  // transaction details
});
 
// Transaction is already confirmed in a block
const receipt = await node.getTransactionReceipt({ hash: txHash });
console.log("Block number:", receipt.blockNumber);

Automatically mines a new block after each transaction.

Changing Mining Modes

// Switch to interval mining
await node.setMiningConfig({

  type: "interval", 
  interval: 5000, // 5 seconds 
}); 
 
// Switch to manual mining
await node.setMiningConfig({

  type: "manual", 
}); 

Event Handlers

Tevm provides event handlers for mining operations, allowing you to monitor blocks, receipts, and logs in real-time:

import { createMemoryClient } from "tevm";
 
const client = createMemoryClient();
 
// Mine with event handlers
const result = await client.mine({
  blockCount: 2,
  // Monitor each block as it's mined
  onBlock: (block, next) => {
    console.log(`Block #${block.header.number} mined:`, {
      hash: block.hash().toString("hex"),
      timestamp: block.header.timestamp,
      gasUsed: block.header.gasUsed,
    });
    next?.(); // Must call next to continue processing
  },
  // Monitor transaction receipts
  onReceipt: (receipt, blockHash, next) => {
    console.log(`Receipt for tx ${receipt.transactionHash}:`, {
      blockHash,
      gasUsed: receipt.gasUsed,
    });
    next?.();
  },
  // Monitor logs from transactions
  onLog: (log, receipt, next) => {
    console.log(`Log from ${log.address}:`, {
      topics: log.topics,
      data: log.data,
    });
    next?.();
  },
});

Best Practices

Choose the Right Mode for Your Use Case

💻 Development

Use auto mining for the fastest feedback loop during development

🧪 Testing

Use manual mining for deterministic tests with precise control

🔄 Simulation

Use interval mining to simulate real-world network conditions

📊 Load Testing

Use gas mining to test application behavior under congestion

Consider Performance Implications

Testing Best Practices

// For time-sensitive logic testing
const timeNode = createTevmNode({
  miningConfig: { type: "interval", interval: 10000 },
});
 
// For deterministic test cases
const deterministicNode = createTevmNode({
  miningConfig: { type: "manual" },
});
 
// For gas-sensitive contract testing
const gasNode = createTevmNode({
  miningConfig: { type: "gas", gasLimit: 8000000 },
});

Example: Testing Different Mining Modes

Comparative Example
import { createTevmNode } from 'tevm'
 
// Auto mining for quick tests
const autoNode = createTevmNode({ 
  miningConfig: { type: 'auto' } 
}) 
 
// Interval mining for realistic scenarios
const intervalNode = createTevmNode({ 
  miningConfig: { 
    type: 'interval', 
    interval: 12000
  } 
})
 
// Manual mining for controlled tests
const manualNode = createTevmNode({ 
  miningConfig: { type: 'manual' } 
}) 
 
// Test transaction processing
await autoNode.sendTransaction({...}) // Mines immediately 
await intervalNode.sendTransaction({...}) // Mines after interval 
await manualNode.sendTransaction({...}) // Stays pending until manual mine 
await manualNode.mine() // Now the transaction is mined 

Related Resources

Transaction Pool
Understanding pending transaction handling and mempool behavior

Performance Optimization

Tuning mining modes for better performance

Block Management
Low-level block control and manipulation