Skip to content

Commit 59458b4

Browse files
author
marcus-0x
authored
XRC HardFork - X11, DigiShield POW algo (#402)
* Added hardfork support for XRC - X11, Digishield * Refactoring, added condition for default difficult on XRC main net * Set correct main net X11 fork block time * Added back old XRC Main network checkpoints
1 parent 25d68dc commit 59458b4

12 files changed

Lines changed: 385 additions & 181 deletions

src/Networks/Blockcore.Networks.XRC/Blockcore.Networks.XRC.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,4 @@
3131
<ItemGroup>
3232
<Folder Include="Rules\" />
3333
</ItemGroup>
34-
35-
<ItemGroup>
36-
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
37-
</ItemGroup>
3834
</Project>

src/Networks/Blockcore.Networks.XRC/Consensus/XRCBlockHeader.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ public class XRCBlockHeader : PosBlockHeader
1010
{
1111
private const int X13_HASH_MINERUNCOMPATIBLE = 1;
1212
private const int X13_HASH_MINERCOMPATIBLE = 2;
13-
private const uint XRC_X13_HASH_HARDFORK = 1541879606;
13+
14+
public XRCConsensusProtocol Consensus { get; set; }
15+
16+
public XRCBlockHeader(XRCConsensusProtocol consensus)
17+
{
18+
this.Consensus = consensus;
19+
}
1420

1521
public override uint256 GetHash()
1622
{
@@ -43,8 +49,13 @@ public override uint256 GetPoWHash()
4349
using (var ms = new MemoryStream())
4450
{
4551
this.ReadWriteHashingStream(new BitcoinStream(ms, true));
52+
53+
if (this.Time > this.Consensus.PowDigiShieldX11Time)
54+
{
55+
return XRCHashX11.Instance.Hash(this.ToBytes());
56+
}
4657
//block HardFork - height: 1648, time - 1541879606, hash - a75312cab7cf2a6ee89ab33bcb0ab9f96676fbc965041d50b889d9469eff6cdb
47-
if (this.Time > XRC_X13_HASH_HARDFORK)
58+
else if (this.Time > this.Consensus.PowLimit2Time)
4859
{
4960
return XRCHashX13.Instance.Hash(this.ToBytes(), X13_HASH_MINERCOMPATIBLE);
5061
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Blockcore.Networks.XRC.Consensus
8+
{
9+
public class XRCCoinType
10+
{
11+
public enum CoinTypes
12+
{
13+
/// <summary>
14+
/// XRhodium Main Network
15+
/// </summary>
16+
XRCMain = 10291,
17+
18+
/// <summary>
19+
/// Testnet
20+
/// </summary>
21+
XRCTest = 1,
22+
23+
/// <summary>
24+
/// RegTest
25+
/// </summary>
26+
XRCReg = 1
27+
}
28+
}
29+
}

src/Networks/Blockcore.Networks.XRC/Consensus/XRCConsensus.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public class XRCConsensus : IConsensus
4848

4949
public Target PowLimit { get; }
5050
public Target PowLimit2 { get; }
51-
public int PowLimit2Height { get; }
52-
public uint PowLimit2Time { get; }
5351

5452
public TimeSpan TargetTimespan { get; }
5553

@@ -131,8 +129,6 @@ public XRCConsensus(
131129
bool powNoRetargeting,
132130
Target powLimit,
133131
Target powLimit2,
134-
int powLimit2Height,
135-
uint powLimit2Time,
136132
uint256 minimumChainWork,
137133
bool isProofOfStake,
138134
int lastPowBlock,
@@ -158,8 +154,6 @@ public XRCConsensus(
158154
this.BIP34Hash = bip34Hash;
159155
this.PowLimit = powLimit;
160156
this.PowLimit2 = powLimit2;
161-
this.PowLimit2Height = powLimit2Height;
162-
this.PowLimit2Time = powLimit2Time;
163157
this.TargetTimespan = targetTimespan;
164158
this.TargetSpacing = targetSpacing;
165159
this.PowAllowMinDifficultyBlocks = powAllowMinDifficultyBlocks;

src/Networks/Blockcore.Networks.XRC/Consensus/XRCConsensusFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public XRCConsensusFactory() : base()
1414

1515
public override BlockHeader CreateBlockHeader()
1616
{
17-
return new XRCBlockHeader();
17+
return new XRCBlockHeader((XRCConsensusProtocol)this.Protocol);
1818
}
1919
}
2020
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Blockcore.Consensus;
2+
3+
namespace Blockcore.Networks.XRC.Consensus
4+
{
5+
public class XRCConsensusProtocol : ConsensusProtocol
6+
{
7+
public int PowLimit2Height { get; set; }
8+
public uint PowLimit2Time { get; set; }
9+
public int PowDigiShieldX11Height { get; set; }
10+
public uint PowDigiShieldX11Time { get; set; }
11+
}
12+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Blockcore.Consensus;
4+
using Blockcore.Features.Consensus.Rules.CommonRules;
5+
using Blockcore.Features.Consensus.Rules.UtxosetRules;
6+
using Blockcore.Features.MemoryPool.Rules;
7+
using Blockcore.Networks.XRC.Rules;
8+
using Blockcore.Consensus.BlockInfo;
9+
using Blockcore.Consensus.ScriptInfo;
10+
using Blockcore.Consensus.TransactionInfo;
11+
using NBitcoin;
12+
using NBitcoin.DataEncoders;
13+
14+
namespace Blockcore.Networks.XRC.Consensus
15+
{
16+
public class XRCNetwork : Network
17+
{
18+
public Block CreateXRCGenesisBlock(XRCConsensusFactory consensusFactory, uint nTime, uint nNonce, uint nBits, int nVersion, string pubKey)
19+
{
20+
string message = "Release the Kraken!!! Zeus";
21+
return CreateXRCGenesisBlock(consensusFactory, message, nTime, nNonce, nBits, nVersion, pubKey);
22+
}
23+
24+
private Block CreateXRCGenesisBlock(XRCConsensusFactory consensusFactory, string message, uint nTime, uint nNonce, uint nBits, int nVersion, string pubKey)
25+
{
26+
//nTime = 1512043200 => Thursday, November 30, 2017 12:00:00 PM (born XRC)
27+
//nTime = 1527811200 => Friday, Jun 1, 2017 12:00:00 PM (born TestXRC)
28+
//nBits = 0x1d00ffff (it is exactly 0x1b = 27 bytes long) => 0x00ffff0000000000000000000000000000000000000000000000000000 => 1
29+
//nNonce = XTimes to trying to find a genesis block
30+
Transaction txNew = consensusFactory.CreateTransaction();
31+
txNew.Version = 2;
32+
if (txNew is IPosTransactionWithTime posTx)
33+
posTx.Time = nTime;
34+
txNew.AddInput(new TxIn()
35+
{
36+
ScriptSig = new Script(Op.GetPushOp(nBits), new Op()
37+
{
38+
Code = (OpcodeType)0x1,
39+
PushData = new[] { (byte)4 }
40+
}, Op.GetPushOp(Encoders.ASCII.DecodeData(message)))
41+
});
42+
txNew.AddOutput(new TxOut()
43+
{
44+
Value = Money.Zero,
45+
ScriptPubKey = Script.FromBytesUnsafe(Encoders.Hex.DecodeData(pubKey))
46+
});
47+
48+
Block genesis = consensusFactory.CreateBlock();
49+
genesis.Header.BlockTime = Utils.UnixTimeToDateTime(nTime);
50+
genesis.Header.Bits = nBits;
51+
genesis.Header.Nonce = nNonce;
52+
genesis.Header.Version = nVersion;
53+
genesis.Transactions.Add(txNew);
54+
genesis.Header.HashPrevBlock = uint256.Zero;
55+
genesis.UpdateMerkleRoot();
56+
return genesis;
57+
}
58+
59+
protected void RegisterRules(IConsensus consensus)
60+
{
61+
consensus.ConsensusRules
62+
.Register<HeaderTimeChecksRule>()
63+
.Register<XRCCheckDifficultyPowRule>()
64+
.Register<XRCHeaderVersionRule>();
65+
66+
consensus.ConsensusRules
67+
.Register<BlockMerkleRootRule>();
68+
69+
consensus.ConsensusRules
70+
.Register<SetActivationDeploymentsPartialValidationRule>()
71+
72+
.Register<TransactionLocktimeActivationRule>()
73+
.Register<CoinbaseHeightActivationRule>()
74+
.Register<WitnessCommitmentsRule>()
75+
.Register<BlockSizeRule>()
76+
77+
.Register<EnsureCoinbaseRule>()
78+
.Register<CheckPowTransactionRule>()
79+
.Register<CheckSigOpsRule>();
80+
81+
consensus.ConsensusRules
82+
.Register<SetActivationDeploymentsFullValidationRule>()
83+
84+
// rules that require the store to be loaded (coinview)
85+
.Register<FetchUtxosetRule>()
86+
.Register<TransactionDuplicationActivationRule>()
87+
.Register<CheckPowUtxosetPowRule>()
88+
.Register<PushUtxosetRule>()
89+
.Register<FlushUtxosetRule>();
90+
}
91+
92+
protected void RegisterMempoolRules(IConsensus consensus)
93+
{
94+
consensus.MempoolRules = new List<Type>()
95+
{
96+
typeof(CheckConflictsMempoolRule),
97+
typeof(CheckCoinViewMempoolRule),
98+
typeof(CreateMempoolEntryMempoolRule),
99+
typeof(CheckSigOpsMempoolRule),
100+
typeof(CheckFeeMempoolRule),
101+
typeof(CheckRateLimitMempoolRule),
102+
typeof(CheckAncestorsMempoolRule),
103+
typeof(CheckReplacementMempoolRule),
104+
typeof(CheckAllInputsMempoolRule),
105+
typeof(CheckTxOutDustRule)
106+
};
107+
}
108+
}
109+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading;
6+
using HashLib;
7+
using NBitcoin;
8+
9+
namespace Blockcore.Networks.XRC.Crypto
10+
{
11+
internal class XRCHashX11
12+
{
13+
private readonly List<IHash> hashers;
14+
15+
private readonly object hashLock;
16+
17+
private static readonly Lazy<XRCHashX11> SingletonInstance = new Lazy<XRCHashX11>(LazyThreadSafetyMode.PublicationOnly);
18+
19+
public XRCHashX11()
20+
{
21+
this.hashers = new List<IHash>
22+
{
23+
HashFactory.Crypto.SHA3.CreateBlake512(),
24+
HashFactory.Crypto.SHA3.CreateBlueMidnightWish512(),
25+
HashFactory.Crypto.SHA3.CreateGroestl512(),
26+
HashFactory.Crypto.SHA3.CreateSkein512_Custom(),
27+
HashFactory.Crypto.SHA3.CreateJH512(),
28+
HashFactory.Crypto.SHA3.CreateKeccak512(),
29+
HashFactory.Crypto.SHA3.CreateLuffa512(),
30+
HashFactory.Crypto.SHA3.CreateCubeHash512(),
31+
HashFactory.Crypto.SHA3.CreateSHAvite3_512_Custom(),
32+
HashFactory.Crypto.SHA3.CreateSIMD512(),
33+
HashFactory.Crypto.SHA3.CreateEcho512(),
34+
};
35+
36+
this.hashLock = new object();
37+
this.Multiplier = 1;
38+
}
39+
40+
public uint Multiplier { get; private set; }
41+
42+
/// <summary>
43+
/// using the instance method is not thread safe.
44+
/// to calling the hashing method in a multi threaded environment use the create() method
45+
/// </summary>
46+
public static XRCHashX11 Instance => SingletonInstance.Value;
47+
48+
public static XRCHashX11 Create()
49+
{
50+
return new XRCHashX11();
51+
}
52+
53+
public uint256 Hash(byte[] input)
54+
{
55+
var buffer = input;
56+
57+
lock (this.hashLock)
58+
{
59+
List<IHash> hashers = this.hashers;
60+
61+
foreach (IHash hasher in hashers)
62+
{
63+
buffer = hasher.ComputeBytes(buffer).GetBytes();
64+
}
65+
}
66+
67+
return new uint256(buffer.Take(32).ToArray());
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)