forked from MetacoSA/NBitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qtum.cs
279 lines (254 loc) · 9.79 KB
/
Qtum.cs
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
using NBitcoin;
using NBitcoin.DataEncoders;
using NBitcoin.Protocol;
using NBitcoin.RPC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace NBitcoin.Altcoins
{
// Reference: https://github.com/qtumproject/qtum/blob/f925e854a84165f8302aa2772cc90faf8a98cb61/src/chainparams.cpp
public class Qtum : NetworkSetBase
{
public static Qtum Instance { get; } = new Qtum();
public override string CryptoCode => "QTUM";
private Qtum()
{
}
public class QtumConsensusFactory : ConsensusFactory
{
private QtumConsensusFactory()
{
}
public static QtumConsensusFactory Instance { get; } = new QtumConsensusFactory();
public override BlockHeader CreateBlockHeader()
{
return new QtumBlockHeader();
}
public override Block CreateBlock()
{
return new QtumBlock(new QtumBlockHeader());
}
}
#pragma warning disable CS0618 // Type or member is obsolete
public class AuxPow : IBitcoinSerializable
{
uint256 hashStateRoot = uint256.Zero;
public uint256 HashStateRoot
{
get
{
return hashStateRoot;
}
set
{
hashStateRoot = value;
}
}
uint256 hashUtxoRoot = uint256.Zero;
public uint256 HashUtxoRoot
{
get
{
return hashUtxoRoot;
}
set
{
hashUtxoRoot = value;
}
}
OutPoint prevoutStake = new OutPoint(uint256.Zero, uint.MaxValue);
public OutPoint PrevoutStake
{
get
{
return prevoutStake;
}
set
{
prevoutStake = value;
}
}
byte[] blockSignature = null;
public byte[] BlockSignature
{
get
{
return blockSignature;
}
set
{
blockSignature = value;
}
}
public void ReadWrite(BitcoinStream stream)
{
stream.ReadWrite(ref hashStateRoot);
stream.ReadWrite(ref hashUtxoRoot);
stream.ReadWrite(ref prevoutStake);
stream.ReadWriteAsVarString(ref blockSignature);
}
}
public class QtumBlock : Block
{
public QtumBlock(QtumBlockHeader header) : base(header)
{
}
public override ConsensusFactory GetConsensusFactory()
{
return QtumConsensusFactory.Instance;
}
}
public class QtumBlockHeader : BlockHeader
{
AuxPow auxPow = new AuxPow();
public AuxPow AuxPow
{
get
{
return auxPow;
}
set
{
auxPow = value;
}
}
public override void ReadWrite(BitcoinStream stream)
{
base.ReadWrite(stream);
stream.ReadWrite(ref auxPow);
}
}
#pragma warning restore CS0618 // Type or member is obsolete
//Format visual studio
//{({.*?}), (.*?)}
//Tuple.Create(new byte[]$1, $2)
//static Tuple<byte[], int>[] pnSeed6_main = null;
//static Tuple<byte[], int>[] pnSeed6_test = null;
protected override NetworkBuilder CreateMainnet()
{
var builder = new NetworkBuilder();
builder.SetConsensus(new Consensus()
{
SubsidyHalvingInterval = 985500,
MajorityEnforceBlockUpgrade = 1916,
MajorityRejectBlockOutdated = 1916,
MajorityWindow = 1916,
PowLimit = new Target(new uint256("0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")),
PowTargetTimespan = TimeSpan.FromSeconds(4000),
PowTargetSpacing = TimeSpan.FromSeconds(2 * 64),
PowAllowMinDifficultyBlocks = false,
// Reference: https://github.com/qtumproject/qtum/blob/f925e854a84165f8302aa2772cc90faf8a98cb61/src/consensus/consensus.h#L27
CoinbaseMaturity = 500,
PowNoRetargeting = false,
ConsensusFactory = QtumConsensusFactory.Instance,
LitecoinWorkCalculation = false,
SupportSegwit = true
})
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { 58 })
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { 50 })
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { 128 })
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { 0x04, 0x88, 0xB2, 0x1E })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { 0x04, 0x88, 0xAD, 0xE4 })
.SetBech32(Bech32Type.WITNESS_PUBKEY_ADDRESS, Encoders.Bech32("qc"))
.SetBech32(Bech32Type.WITNESS_SCRIPT_ADDRESS, Encoders.Bech32("qc"))
.SetMagic(0xd3a6cff1)
.SetPort(3888)
.SetRPCPort(3889)
.SetMaxP2PVersion(70021)
.SetName("qtum-main")
.AddAlias("qtum-mainnet")
.AddDNSSeeds(new[]{
new DNSSeedData("qtum3.dynu.net", "seed.qtum3.dynu.net"),
})
.AddSeeds(new NetworkAddress[0])
.SetGenesis("0100000000000000000000000000000000000000000000000000000000000000000000006db905142382324db417761891f2d2f355ea92f27ab0fc35e59e90b50e0534edf5d2af59ffff001ff9787a00e965ffd002cd6ad0e2dc402b8044de833e06b23127ea8c3d80aec9141077149556e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4210000000000000000000000000000000000000000000000000000000000000000ffffffff000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff420004bf91221d0104395365702030322c203230313720426974636f696e20627265616b732024352c30303020696e206c6174657374207072696365206672656e7a79ffffffff0100f2052a010000004341040d61d8653448c98731ee5fffd303c15e71ec2057b77f11ab3601979728cdaff2d68afbba14e4fa0bc44f2072b0b23ef63717f8cdfbe58dcd33f32b6afe98741aac00000000");
return builder;
}
protected override NetworkBuilder CreateTestnet()
{
var builder = new NetworkBuilder();
builder.SetConsensus(new Consensus()
{
SubsidyHalvingInterval = 985500,
MajorityEnforceBlockUpgrade = 1512,
MajorityRejectBlockOutdated = 1512,
MajorityWindow = 1512,
PowLimit = new Target(new uint256("0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")),
PowTargetTimespan = TimeSpan.FromSeconds(4000),
PowTargetSpacing = TimeSpan.FromSeconds(2 * 64),
PowAllowMinDifficultyBlocks = false,
CoinbaseMaturity = 500,
PowNoRetargeting = false,
ConsensusFactory = QtumConsensusFactory.Instance,
LitecoinWorkCalculation = false,
SupportSegwit = true
})
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { 120 })
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { 110 })
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { 239 })
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { 0x04, 0x35, 0x87, 0xCF })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { 0x04, 0x35, 0x83, 0x94 })
.SetBech32(Bech32Type.WITNESS_PUBKEY_ADDRESS, Encoders.Bech32("tq"))
.SetBech32(Bech32Type.WITNESS_SCRIPT_ADDRESS, Encoders.Bech32("tq"))
.SetMagic(0x0615220d)
.SetPort(13888)
.SetRPCPort(13889)
.SetMaxP2PVersion(70021)
.SetName("qtum-test")
.AddAlias("qtum-testnet")
.AddDNSSeeds(new[]{
new DNSSeedData("qtum4.dynu.net", "seed.qtum4.dynu.net"),
})
.AddSeeds(new NetworkAddress[0])
// Incorrect, using mainnet for now
.SetGenesis("0100000000000000000000000000000000000000000000000000000000000000000000006db905142382324db417761891f2d2f355ea92f27ab0fc35e59e90b50e0534edf5d2af59ffff001ff9787a00e965ffd002cd6ad0e2dc402b8044de833e06b23127ea8c3d80aec9141077149556e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4210000000000000000000000000000000000000000000000000000000000000000ffffffff000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff420004bf91221d0104395365702030322c203230313720426974636f696e20627265616b732024352c30303020696e206c6174657374207072696365206672656e7a79ffffffff0100f2052a010000004341040d61d8653448c98731ee5fffd303c15e71ec2057b77f11ab3601979728cdaff2d68afbba14e4fa0bc44f2072b0b23ef63717f8cdfbe58dcd33f32b6afe98741aac00000000");
return builder;
}
protected override NetworkBuilder CreateRegtest()
{
var builder = new NetworkBuilder();
builder.SetConsensus(new Consensus()
{
SubsidyHalvingInterval = 150,
MajorityEnforceBlockUpgrade = 108,
MajorityRejectBlockOutdated = 108,
MajorityWindow = 108,
PowLimit = new Target(new uint256("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")),
PowTargetTimespan = TimeSpan.FromSeconds(4000),
PowTargetSpacing = TimeSpan.FromSeconds(2 * 64),
PowAllowMinDifficultyBlocks = true,
CoinbaseMaturity = 500,
PowNoRetargeting = false,
ConsensusFactory = QtumConsensusFactory.Instance,
LitecoinWorkCalculation = false,
SupportSegwit = true
})
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { 120 })
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { 110 })
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { 239 })
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { 0x04, 0x35, 0x87, 0xCF })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { 0x04, 0x35, 0x83, 0x94 })
.SetBech32(Bech32Type.WITNESS_PUBKEY_ADDRESS, Encoders.Bech32("qcrt"))
.SetBech32(Bech32Type.WITNESS_SCRIPT_ADDRESS, Encoders.Bech32("qcrt"))
.SetMagic(0xe1c6ddfd)
.SetPort(23888)
.SetRPCPort(13889)
.SetMaxP2PVersion(70021)
.SetName("qtum-reg")
.AddAlias("qtum-regtest")
.AddSeeds(new NetworkAddress[0])
// Incorrect, using mainnet for now
.SetGenesis("0100000000000000000000000000000000000000000000000000000000000000000000006db905142382324db417761891f2d2f355ea92f27ab0fc35e59e90b50e0534edf5d2af59ffff7f2011000000e965ffd002cd6ad0e2dc402b8044de833e06b23127ea8c3d80aec9141077149556e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b4210000000000000000000000000000000000000000000000000000000000000000ffffffff000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff420004bf91221d0104395365702030322c203230313720426974636f696e20627265616b732024352c30303020696e206c6174657374207072696365206672656e7a79ffffffff0100f2052a010000004341040d61d8653448c98731ee5fffd303c15e71ec2057b77f11ab3601979728cdaff2d68afbba14e4fa0bc44f2072b0b23ef63717f8cdfbe58dcd33f32b6afe98741aac00000000");
return builder;
}
protected override void PostInit()
{
// Reference: https://github.com/qtumproject/qtum/blob/342d769cf60ccfc46c0669507dcd154988d87d4f/src/util/system.cpp#L701
RegisterDefaultCookiePath("Qtum");
}
}
}