Skip to content

Commit

Permalink
Networkz (#22)
Browse files Browse the repository at this point in the history
* using Apex throughout solution

* removed unused assert method in Networks

* remove left behind

* remove extra space
  • Loading branch information
bokobza authored and monsieurleberre committed Jun 13, 2018
1 parent 15d0847 commit eff4423
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FluentAssertions;
using NBitcoin;
using Stratis.Sidechains.Features.BlockchainGeneration;
using Stratis.Sidechains.Networks;
using Xunit;

namespace Stratis.FederatedPeg.Tests
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

<ItemGroup>
<ProjectReference Include="..\Stratis.FederatedPeg\Stratis.FederatedPeg.csproj" />
<ProjectReference Include="..\Stratis.Sidechains.Features.BlockchainGeneration\Stratis.Sidechains.Features.BlockchainGeneration.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions src/Stratis.Sidechains.Networks/ApexMain.cs
Original file line number Diff line number Diff line change
@@ -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<int, CheckpointInfo>();
this.DNSSeeds = new List<DNSSeedData>();
this.SeedNodes = new List<NetworkAddress>();

// 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"));
}
}
}
49 changes: 49 additions & 0 deletions src/Stratis.Sidechains.Networks/ApexNetwork.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
50 changes: 50 additions & 0 deletions src/Stratis.Sidechains.Networks/ApexRegTest.cs
Original file line number Diff line number Diff line change
@@ -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<int, CheckpointInfo>();
this.DNSSeeds = new List<DNSSeedData>();
this.SeedNodes = new List<NetworkAddress>();

// 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"));
}
}
}
44 changes: 44 additions & 0 deletions src/Stratis.Sidechains.Networks/ApexTest.cs
Original file line number Diff line number Diff line change
@@ -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<int, CheckpointInfo>();
this.DNSSeeds = new List<DNSSeedData>();
this.SeedNodes = new List<NetworkAddress>();

// 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"));
}
}
}
11 changes: 11 additions & 0 deletions src/Stratis.Sidechains.Networks/Stratis.Sidechains.Networks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NStratis" Version="4.0.0.60" />
</ItemGroup>

</Project>

0 comments on commit eff4423

Please sign in to comment.