From eff4423ac6d6436c5890224a88e822f8c2941f6d Mon Sep 17 00:00:00 2001 From: Jeremy Bokobza Date: Wed, 13 Jun 2018 11:48:19 +0100 Subject: [PATCH] Networkz (#22) * using Apex throughout solution * removed unused assert method in Networks * remove left behind * remove extra space --- .../Chain_With_NetworkExtension_Shall.cs | 8 +-- .../Stratis.FederatedPeg.Tests.csproj | 1 - src/Stratis.Sidechains.Networks/ApexMain.cs | 48 ++++++++++++++++++ .../ApexNetwork.cs | 49 ++++++++++++++++++ .../ApexRegTest.cs | 50 +++++++++++++++++++ src/Stratis.Sidechains.Networks/ApexTest.cs | 44 ++++++++++++++++ .../Stratis.Sidechains.Networks.csproj | 11 ++++ 7 files changed, 206 insertions(+), 5 deletions(-) create mode 100644 src/Stratis.Sidechains.Networks/ApexMain.cs create mode 100644 src/Stratis.Sidechains.Networks/ApexNetwork.cs create mode 100644 src/Stratis.Sidechains.Networks/ApexRegTest.cs create mode 100644 src/Stratis.Sidechains.Networks/ApexTest.cs create mode 100644 src/Stratis.Sidechains.Networks/Stratis.Sidechains.Networks.csproj diff --git a/src/Stratis.FederatedPeg.Tests/Chain_With_NetworkExtension_Shall.cs b/src/Stratis.FederatedPeg.Tests/Chain_With_NetworkExtension_Shall.cs index ee202655b98..9997c30925a 100644 --- a/src/Stratis.FederatedPeg.Tests/Chain_With_NetworkExtension_Shall.cs +++ b/src/Stratis.FederatedPeg.Tests/Chain_With_NetworkExtension_Shall.cs @@ -1,6 +1,6 @@ using FluentAssertions; using NBitcoin; -using Stratis.Sidechains.Features.BlockchainGeneration; +using Stratis.Sidechains.Networks; using Xunit; namespace Stratis.FederatedPeg.Tests @@ -34,19 +34,19 @@ public void correctly_identify_mainchain() public void correctly_identify_sidechain() { //reg test - var network = SidechainNetwork.SidechainRegTest; + var network = ApexNetwork.RegTest; var chain = network.ToChain(); chain.Should().Be(Chain.Sidechain); chain.Should().NotBe(Chain.Mainchain); //testnet - network = SidechainNetwork.SidechainTest; + network = ApexNetwork.Test; chain = network.ToChain(); chain.Should().Be(Chain.Sidechain); chain.Should().NotBe(Chain.Mainchain); //mainnet - network = SidechainNetwork.SidechainMain; + network = ApexNetwork.Main; chain = network.ToChain(); chain.Should().Be(Chain.Sidechain); chain.Should().NotBe(Chain.Mainchain); diff --git a/src/Stratis.FederatedPeg.Tests/Stratis.FederatedPeg.Tests.csproj b/src/Stratis.FederatedPeg.Tests/Stratis.FederatedPeg.Tests.csproj index 1628f146729..335b3a689bd 100644 --- a/src/Stratis.FederatedPeg.Tests/Stratis.FederatedPeg.Tests.csproj +++ b/src/Stratis.FederatedPeg.Tests/Stratis.FederatedPeg.Tests.csproj @@ -25,7 +25,6 @@ - diff --git a/src/Stratis.Sidechains.Networks/ApexMain.cs b/src/Stratis.Sidechains.Networks/ApexMain.cs new file mode 100644 index 00000000000..f2b45e0e156 --- /dev/null +++ b/src/Stratis.Sidechains.Networks/ApexMain.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using NBitcoin; +using NBitcoin.Networks; +using NBitcoin.Protocol; + +namespace Stratis.Sidechains.Networks +{ + public class ApexMain : StratisMain + { + public ApexMain() + { + this.Name = ApexNetwork.MainNetworkName; + this.RootFolderName = ApexNetwork.ChainName.ToLowerInvariant(); + this.DefaultConfigFilename = $"{ApexNetwork.ChainName.ToLowerInvariant()}.conf"; + this.DefaultPort = 36000; + this.RPCPort = 36100; + this.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS] = new byte[] { 23 }; // A + this.Base58Prefixes[(int)Base58Type.SCRIPT_ADDRESS] = new byte[] { 83 }; // a + this.Magic = 0x522357A; + this.CoinTicker = "APX"; + + this.Consensus.CoinType = 3000; + this.Consensus.DefaultAssumeValid = null; + this.Consensus.CoinbaseMaturity = 50; + this.Consensus.PremineReward = Money.Coins(20000000); + this.Consensus.ProofOfWorkReward = Money.Zero; + this.Consensus.ProofOfStakeReward = Money.Zero; + this.Consensus.MaxReorgLength = 0; + this.Consensus.MaxMoney = Money.Coins(20000000); + + this.Checkpoints = new Dictionary(); + this.DNSSeeds = new List(); + this.SeedNodes = new List(); + + // Create the genesis block. + this.GenesisTime = 1528217223; + this.GenesisNonce = 58285; + this.GenesisBits = this.Consensus.PowLimit.ToCompact(); + this.GenesisVersion = 1; + this.GenesisReward = Money.Coins(50m); + + this.Genesis = ApexNetwork.CreateGenesisBlock(this.Consensus.ConsensusFactory, this.GenesisTime, this.GenesisNonce, this.Consensus.PowLimit, this.GenesisVersion, this.GenesisReward); + this.Consensus.HashGenesisBlock = this.Genesis.GetHash(); + Assert(this.Consensus.HashGenesisBlock.ToString() == "000009a6434326a4851f0e95285351839c287182bd2b62ca8765ce30007605e1"); + Assert(this.Genesis.Header.HashMerkleRoot == uint256.Parse("070b7da316f93439d05240db30a9ca4f6019d550e0b6af9a8ac1b075726c9403")); + } + } +} \ No newline at end of file diff --git a/src/Stratis.Sidechains.Networks/ApexNetwork.cs b/src/Stratis.Sidechains.Networks/ApexNetwork.cs new file mode 100644 index 00000000000..c78ae842740 --- /dev/null +++ b/src/Stratis.Sidechains.Networks/ApexNetwork.cs @@ -0,0 +1,49 @@ +using NBitcoin; +using NBitcoin.DataEncoders; + +namespace Stratis.Sidechains.Networks +{ + public class ApexNetwork + { + public const string ChainName = "Apex"; + public const string MainNetworkName = ChainName + "Main"; + public const string TestNetworkName = ChainName + "Test"; + public const string RegTestNetworkName = ChainName + "RegTest"; + + public static Network Main => Network.GetNetwork(MainNetworkName) ?? Network.Register(new ApexMain()); + + public static Network Test => Network.GetNetwork(TestNetworkName) ?? Network.Register(new ApexTest()); + + public static Network RegTest => Network.GetNetwork(RegTestNetworkName) ?? Network.Register(new ApexRegTest()); + + public static Block CreateGenesisBlock(ConsensusFactory consensusFactory, uint nTime, uint nNonce, uint nBits, int nVersion, Money genesisReward) + { + string pszTimestamp = "https://www.coindesk.com/apple-co-founder-backs-dorsey-bitcoin-become-webs-currency/"; + + Transaction txNew = consensusFactory.CreateTransaction(); + txNew.Version = 1; + txNew.Time = nTime; + txNew.AddInput(new TxIn() + { + ScriptSig = new Script(Op.GetPushOp(0), new Op() + { + Code = (OpcodeType)0x1, + PushData = new[] { (byte)42 } + }, Op.GetPushOp(Encoders.ASCII.DecodeData(pszTimestamp))) + }); + txNew.AddOutput(new TxOut() + { + Value = genesisReward, + }); + 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; + } + } +} diff --git a/src/Stratis.Sidechains.Networks/ApexRegTest.cs b/src/Stratis.Sidechains.Networks/ApexRegTest.cs new file mode 100644 index 00000000000..845b329a78f --- /dev/null +++ b/src/Stratis.Sidechains.Networks/ApexRegTest.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using NBitcoin; +using NBitcoin.Protocol; + +namespace Stratis.Sidechains.Networks +{ + public class ApexRegTest : ApexMain + { + public ApexRegTest() + { + this.Name = ApexNetwork.RegTestNetworkName; + this.RootFolderName = ApexNetwork.ChainName.ToLowerInvariant(); + this.DefaultConfigFilename = $"{ApexNetwork.ChainName.ToLowerInvariant()}.conf"; + this.DefaultPort = 36002; + this.RPCPort = 36102; + this.Magic = 0x522357C; + this.MinTxFee = 0; + this.FallbackFee = 0; + this.MinRelayTxFee = 0; + this.CoinTicker = "TAPX"; + + this.Consensus.CoinType = 3001; + this.Consensus.PowAllowMinDifficultyBlocks = true; + this.Consensus.PowNoRetargeting = true; + this.Consensus.PowLimit = new Target(new uint256("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); + this.Consensus.DefaultAssumeValid = null; // turn off assumevalid for regtest. + this.Consensus.CoinbaseMaturity = 10; + this.Consensus.MaxMoney = Money.Coins(20000000); + + this.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS] = new byte[] { 75 }; // X + this.Base58Prefixes[(int)Base58Type.SCRIPT_ADDRESS] = new byte[] { 137 }; // x + + this.Checkpoints = new Dictionary(); + this.DNSSeeds = new List(); + this.SeedNodes = new List(); + + // Create the genesis block. + this.GenesisTime = 1528217336; + this.GenesisNonce = 2; + this.GenesisBits = this.Consensus.PowLimit.ToCompact(); + this.GenesisVersion = 1; + this.GenesisReward = Money.Coins(50m); + + this.Genesis = ApexNetwork.CreateGenesisBlock(this.Consensus.ConsensusFactory, this.GenesisTime, this.GenesisNonce, this.Consensus.PowLimit, this.GenesisVersion, this.GenesisReward); + this.Consensus.HashGenesisBlock = this.Genesis.GetHash(); + Assert(this.Consensus.HashGenesisBlock.ToString() == "0688f8440feed473792edc56e365bb7ab20ae2d4e2010cfb8af4ccadaa53e611"); + Assert(this.Genesis.Header.HashMerkleRoot == uint256.Parse("a835d145c6011d3731b0ab5a8f2108f635a5197ccd2e69b74cac1dfa9007db4b")); + } + } +} \ No newline at end of file diff --git a/src/Stratis.Sidechains.Networks/ApexTest.cs b/src/Stratis.Sidechains.Networks/ApexTest.cs new file mode 100644 index 00000000000..5e24f2ab262 --- /dev/null +++ b/src/Stratis.Sidechains.Networks/ApexTest.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using NBitcoin; +using NBitcoin.Protocol; + +namespace Stratis.Sidechains.Networks +{ + public class ApexTest : ApexMain + { + public ApexTest() + { + this.Name = ApexNetwork.TestNetworkName; + this.RootFolderName = ApexNetwork.ChainName.ToLowerInvariant(); + this.DefaultConfigFilename = $"{ApexNetwork.ChainName.ToLowerInvariant()}.conf"; + this.DefaultPort = 36001; + this.RPCPort = 36101; + this.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS] = new byte[] { 55 }; // P + this.Base58Prefixes[(int)Base58Type.SCRIPT_ADDRESS] = new byte[] { 117 }; // p + this.Magic = 0x522357B; + this.CoinTicker = "TAPX"; + + this.Consensus.CoinType = 3001; + this.Consensus.DefaultAssumeValid = null; + this.Consensus.PowLimit = new Target(new uint256("0000ffff00000000000000000000000000000000000000000000000000000000")); + this.Consensus.CoinbaseMaturity = 10; + this.Consensus.MaxMoney = Money.Coins(20000000); + + this.Checkpoints = new Dictionary(); + this.DNSSeeds = new List(); + this.SeedNodes = new List(); + + // Create the genesis block. + this.GenesisTime = 1528217189; + this.GenesisNonce = 9027; + this.GenesisBits = this.Consensus.PowLimit.ToCompact(); + this.GenesisVersion = 1; + this.GenesisReward = Money.Coins(50m); + + this.Genesis = ApexNetwork.CreateGenesisBlock(this.Consensus.ConsensusFactory, this.GenesisTime, this.GenesisNonce, this.Consensus.PowLimit, this.GenesisVersion, this.GenesisReward); + this.Consensus.HashGenesisBlock = this.Genesis.GetHash(); + Assert(this.Consensus.HashGenesisBlock.ToString() == "0000e451752bac9f67cb5d63fd32442ba42d42b1b2ea28131d91e1e3f29f523b"); + Assert(this.Genesis.Header.HashMerkleRoot == uint256.Parse("7326e99b152ce27951b0e9633c2663e8ddfd4006752d1721d579a22046e2d8fb")); + } + } +} \ No newline at end of file diff --git a/src/Stratis.Sidechains.Networks/Stratis.Sidechains.Networks.csproj b/src/Stratis.Sidechains.Networks/Stratis.Sidechains.Networks.csproj new file mode 100644 index 00000000000..249318204a6 --- /dev/null +++ b/src/Stratis.Sidechains.Networks/Stratis.Sidechains.Networks.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + +