Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XRC HardFork - X11, DigiShield POW algo #402

Merged
merged 4 commits into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@
<ItemGroup>
<Folder Include="Rules\" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.1.1" />
</ItemGroup>
</Project>
15 changes: 13 additions & 2 deletions src/Networks/Blockcore.Networks.XRC/Consensus/XRCBlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public class XRCBlockHeader : PosBlockHeader
{
private const int X13_HASH_MINERUNCOMPATIBLE = 1;
private const int X13_HASH_MINERCOMPATIBLE = 2;
private const uint XRC_X13_HASH_HARDFORK = 1541879606;

public XRCConsensusProtocol Consensus { get; set; }

public XRCBlockHeader(XRCConsensusProtocol consensus)
{
this.Consensus = consensus;
}

public override uint256 GetHash()
{
Expand Down Expand Up @@ -43,8 +49,13 @@ public override uint256 GetPoWHash()
using (var ms = new MemoryStream())
{
this.ReadWriteHashingStream(new BitcoinStream(ms, true));

if (this.Time > this.Consensus.PowDigiShieldX11Time)
{
return XRCHashX11.Instance.Hash(this.ToBytes());
}
//block HardFork - height: 1648, time - 1541879606, hash - a75312cab7cf2a6ee89ab33bcb0ab9f96676fbc965041d50b889d9469eff6cdb
if (this.Time > XRC_X13_HASH_HARDFORK)
else if (this.Time > this.Consensus.PowLimit2Time)
{
return XRCHashX13.Instance.Hash(this.ToBytes(), X13_HASH_MINERCOMPATIBLE);
}
Expand Down
29 changes: 29 additions & 0 deletions src/Networks/Blockcore.Networks.XRC/Consensus/XRCCoinType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blockcore.Networks.XRC.Consensus
{
public class XRCCoinType
{
public enum CoinTypes
{
/// <summary>
/// XRhodium Main Network
/// </summary>
XRCMain = 10291,

/// <summary>
/// Testnet
/// </summary>
XRCTest = 1,

/// <summary>
/// RegTest
/// </summary>
XRCReg = 1
}
}
}
6 changes: 0 additions & 6 deletions src/Networks/Blockcore.Networks.XRC/Consensus/XRCConsensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public class XRCConsensus : IConsensus

public Target PowLimit { get; }
public Target PowLimit2 { get; }
public int PowLimit2Height { get; }
public uint PowLimit2Time { get; }

public TimeSpan TargetTimespan { get; }

Expand Down Expand Up @@ -131,8 +129,6 @@ public XRCConsensus(
bool powNoRetargeting,
Target powLimit,
Target powLimit2,
int powLimit2Height,
uint powLimit2Time,
uint256 minimumChainWork,
bool isProofOfStake,
int lastPowBlock,
Expand All @@ -158,8 +154,6 @@ public XRCConsensus(
this.BIP34Hash = bip34Hash;
this.PowLimit = powLimit;
this.PowLimit2 = powLimit2;
this.PowLimit2Height = powLimit2Height;
this.PowLimit2Time = powLimit2Time;
this.TargetTimespan = targetTimespan;
this.TargetSpacing = targetSpacing;
this.PowAllowMinDifficultyBlocks = powAllowMinDifficultyBlocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public XRCConsensusFactory() : base()

public override BlockHeader CreateBlockHeader()
{
return new XRCBlockHeader();
return new XRCBlockHeader((XRCConsensusProtocol)this.Protocol);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Blockcore.Consensus;

namespace Blockcore.Networks.XRC.Consensus
{
public class XRCConsensusProtocol : ConsensusProtocol
{
public int PowLimit2Height { get; set; }
public uint PowLimit2Time { get; set; }
public int PowDigiShieldX11Height { get; set; }
public uint PowDigiShieldX11Time { get; set; }
}
}
109 changes: 109 additions & 0 deletions src/Networks/Blockcore.Networks.XRC/Consensus/XRCNetwork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using Blockcore.Consensus;
using Blockcore.Features.Consensus.Rules.CommonRules;
using Blockcore.Features.Consensus.Rules.UtxosetRules;
using Blockcore.Features.MemoryPool.Rules;
using Blockcore.Networks.XRC.Rules;
using Blockcore.Consensus.BlockInfo;
using Blockcore.Consensus.ScriptInfo;
using Blockcore.Consensus.TransactionInfo;
using NBitcoin;
using NBitcoin.DataEncoders;

namespace Blockcore.Networks.XRC.Consensus
{
public class XRCNetwork : Network
{
public Block CreateXRCGenesisBlock(XRCConsensusFactory consensusFactory, uint nTime, uint nNonce, uint nBits, int nVersion, string pubKey)
{
string message = "Release the Kraken!!! Zeus";
return CreateXRCGenesisBlock(consensusFactory, message, nTime, nNonce, nBits, nVersion, pubKey);
}

private Block CreateXRCGenesisBlock(XRCConsensusFactory consensusFactory, string message, uint nTime, uint nNonce, uint nBits, int nVersion, string pubKey)
{
//nTime = 1512043200 => Thursday, November 30, 2017 12:00:00 PM (born XRC)
//nTime = 1527811200 => Friday, Jun 1, 2017 12:00:00 PM (born TestXRC)
//nBits = 0x1d00ffff (it is exactly 0x1b = 27 bytes long) => 0x00ffff0000000000000000000000000000000000000000000000000000 => 1
//nNonce = XTimes to trying to find a genesis block
Transaction txNew = consensusFactory.CreateTransaction();
txNew.Version = 2;
if (txNew is IPosTransactionWithTime posTx)
posTx.Time = nTime;
txNew.AddInput(new TxIn()
{
ScriptSig = new Script(Op.GetPushOp(nBits), new Op()
{
Code = (OpcodeType)0x1,
PushData = new[] { (byte)4 }
}, Op.GetPushOp(Encoders.ASCII.DecodeData(message)))
});
txNew.AddOutput(new TxOut()
{
Value = Money.Zero,
ScriptPubKey = Script.FromBytesUnsafe(Encoders.Hex.DecodeData(pubKey))
});

Block genesis = consensusFactory.CreateBlock();
genesis.Header.BlockTime = Utils.UnixTimeToDateTime(nTime);
genesis.Header.Bits = nBits;
genesis.Header.Nonce = nNonce;
genesis.Header.Version = nVersion;
genesis.Transactions.Add(txNew);
genesis.Header.HashPrevBlock = uint256.Zero;
genesis.UpdateMerkleRoot();
return genesis;
}

protected void RegisterRules(IConsensus consensus)
{
consensus.ConsensusRules
.Register<HeaderTimeChecksRule>()
.Register<XRCCheckDifficultyPowRule>()
.Register<XRCHeaderVersionRule>();

consensus.ConsensusRules
.Register<BlockMerkleRootRule>();

consensus.ConsensusRules
.Register<SetActivationDeploymentsPartialValidationRule>()

.Register<TransactionLocktimeActivationRule>()
.Register<CoinbaseHeightActivationRule>()
.Register<WitnessCommitmentsRule>()
.Register<BlockSizeRule>()

.Register<EnsureCoinbaseRule>()
.Register<CheckPowTransactionRule>()
.Register<CheckSigOpsRule>();

consensus.ConsensusRules
.Register<SetActivationDeploymentsFullValidationRule>()

// rules that require the store to be loaded (coinview)
.Register<FetchUtxosetRule>()
.Register<TransactionDuplicationActivationRule>()
.Register<CheckPowUtxosetPowRule>()
.Register<PushUtxosetRule>()
.Register<FlushUtxosetRule>();
}

protected void RegisterMempoolRules(IConsensus consensus)
{
consensus.MempoolRules = new List<Type>()
{
typeof(CheckConflictsMempoolRule),
typeof(CheckCoinViewMempoolRule),
typeof(CreateMempoolEntryMempoolRule),
typeof(CheckSigOpsMempoolRule),
typeof(CheckFeeMempoolRule),
typeof(CheckRateLimitMempoolRule),
typeof(CheckAncestorsMempoolRule),
typeof(CheckReplacementMempoolRule),
typeof(CheckAllInputsMempoolRule),
typeof(CheckTxOutDustRule)
};
}
}
}
70 changes: 70 additions & 0 deletions src/Networks/Blockcore.Networks.XRC/Crypto/XRCHashX11.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using HashLib;
using NBitcoin;

namespace Blockcore.Networks.XRC.Crypto
{
internal class XRCHashX11
{
private readonly List<IHash> hashers;

private readonly object hashLock;

private static readonly Lazy<XRCHashX11> SingletonInstance = new Lazy<XRCHashX11>(LazyThreadSafetyMode.PublicationOnly);

public XRCHashX11()
{
this.hashers = new List<IHash>
{
HashFactory.Crypto.SHA3.CreateBlake512(),
HashFactory.Crypto.SHA3.CreateBlueMidnightWish512(),
HashFactory.Crypto.SHA3.CreateGroestl512(),
HashFactory.Crypto.SHA3.CreateSkein512_Custom(),
HashFactory.Crypto.SHA3.CreateJH512(),
HashFactory.Crypto.SHA3.CreateKeccak512(),
HashFactory.Crypto.SHA3.CreateLuffa512(),
HashFactory.Crypto.SHA3.CreateCubeHash512(),
HashFactory.Crypto.SHA3.CreateSHAvite3_512_Custom(),
HashFactory.Crypto.SHA3.CreateSIMD512(),
HashFactory.Crypto.SHA3.CreateEcho512(),
};

this.hashLock = new object();
this.Multiplier = 1;
}

public uint Multiplier { get; private set; }

/// <summary>
/// using the instance method is not thread safe.
/// to calling the hashing method in a multi threaded environment use the create() method
/// </summary>
public static XRCHashX11 Instance => SingletonInstance.Value;

public static XRCHashX11 Create()
{
return new XRCHashX11();
}

public uint256 Hash(byte[] input)
{
var buffer = input;

lock (this.hashLock)
{
List<IHash> hashers = this.hashers;

foreach (IHash hasher in hashers)
{
buffer = hasher.ComputeBytes(buffer).GetBytes();
}
}

return new uint256(buffer.Take(32).ToArray());
}
}
}
Loading