-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
135 lines (111 loc) · 4.79 KB
/
Program.cs
File metadata and controls
135 lines (111 loc) · 4.79 KB
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
using System.IO;
using BitcoinKernel;
using BitcoinKernel.Primitives;
using BitcoinKernel.Chain;
using BitcoinKernel.Interop.Enums;
namespace BlockProcessing;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Bitcoin Kernel Block Processing Example");
Console.WriteLine("=====================================");
var dataDir = Path.Combine(Path.GetTempPath(), $"bitcoinkernel_{Guid.NewGuid()}");
var blocksDir = Path.Combine(dataDir, "blocks");
try
{
using var logging = new LoggingConnection((category, message, level) =>
{
if (level <= 2)
Console.WriteLine($"[{category}] {message}");
});
using var chainParams = new ChainParameters(ChainType.MAINNET);
using var contextOptions = new KernelContextOptions().SetChainParams(chainParams);
using var context = new KernelContext(contextOptions);
using var options = new ChainstateManagerOptions(context, dataDir, blocksDir);
using var chainstate = new ChainstateManager(context, chainParams, options);
Console.WriteLine("Created kernel for mainnet");
Console.WriteLine("Chainstate initialized");
byte[] sampleBlockData;
try
{
sampleBlockData = CreateSampleBlock();
Console.WriteLine("Created sample block data");
}
catch (Exception ex)
{
Console.WriteLine($"Block creation failed: {ex.Message}");
Console.WriteLine("This is expected for simplified block data.");
return;
}
DisplayBlockInfo(sampleBlockData);
Console.WriteLine("\nProcessing block...");
try
{
using var block = Block.FromBytes(sampleBlockData);
bool isNew = chainstate.ProcessBlock(block);
if (isNew)
{
var activeChain = chainstate.GetActiveChain();
Console.WriteLine($"Block processed! Chain height: {activeChain.Height}");
var tip = activeChain.GetTip();
Console.WriteLine($" - Tip: {BitConverter.ToString(tip.GetBlockHash()).Replace("-", "")}");
}
else
{
Console.WriteLine("Block processing failed - expected for invalid block data");
}
}
catch (Exception ex)
{
Console.WriteLine($"Block processing error: {ex.Message}");
Console.WriteLine("This is expected for simplified/invalid block data.");
}
Console.WriteLine("\nBlock processing example completed!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
}
}
private static byte[] CreateSampleBlock()
{
byte[] blockData = new byte[80];
// Version: 1 (little endian)
BitConverter.GetBytes(1).CopyTo(blockData, 0);
// Previous block hash: all zeros (indices 4-35)
// Merkle root: all zeros (indices 36-67)
// Timestamp: current Unix timestamp
uint timestamp = (uint)(DateTimeOffset.UtcNow.ToUnixTimeSeconds());
BitConverter.GetBytes(timestamp).CopyTo(blockData, 68);
// Bits: 0x1d00ffff (Bitcoin mainnet difficulty)
BitConverter.GetBytes(0x1d00ffffu).CopyTo(blockData, 72);
// Nonce: 0 (indices 76-79)
return blockData;
}
private static void DisplayBlockInfo(byte[] blockData)
{
Console.WriteLine("\nBlock Information:");
Console.WriteLine("-----------------");
try
{
Console.WriteLine($"Block Size: {blockData.Length} bytes");
Console.WriteLine($"Block Data (first 32 bytes): {BitConverter.ToString(blockData.Take(32).ToArray()).Replace("-", " ")}");
uint version = BitConverter.ToUInt32(blockData, 0);
Console.WriteLine($"Version: {version}");
uint timestamp = BitConverter.ToUInt32(blockData, 68);
DateTime blockTime = DateTimeOffset.FromUnixTimeSeconds(timestamp).DateTime;
Console.WriteLine($"Timestamp: {timestamp} ({blockTime:yyyy-MM-dd HH:mm:ss UTC})");
uint bits = BitConverter.ToUInt32(blockData, 72);
Console.WriteLine($"Bits: 0x{bits:X8}");
uint nonce = BitConverter.ToUInt32(blockData, 76);
Console.WriteLine($"Nonce: {nonce}");
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing block info: {ex.Message}");
}
}
}