Skip to content

Commit

Permalink
Support for Groestlcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Jun 23, 2018
1 parent 139fa4b commit 0572d77
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 35 deletions.
15 changes: 15 additions & 0 deletions NBXplorer.Client/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NBitcoin;
using NBitcoin.RPC;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
Expand Down Expand Up @@ -113,6 +114,20 @@ public static async Task CloseSocket(this WebSocket socket, WebSocketCloseStatus
finally { socket.Dispose(); }
}

public static async Task<uint256[]> EnsureGenerateAsync(this RPCClient client, int blockCount)
{
uint256[] blockIds = new uint256[blockCount];
int generated = 0;
while(generated < blockCount)
{
foreach(var id in await client.GenerateAsync(blockCount - generated).ConfigureAwait(false))
{
blockIds[generated++] = id;
}
}
return blockIds;
}

public static string GetNotificationMessageTypeName(Type type)
{
_NameByType.TryGetValue(type, out string name);
Expand Down
6 changes: 3 additions & 3 deletions NBXplorer.Client/NBXplorer.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Company>Digital Garage</Company>
<Version>1.0.2.10</Version>
<Version>1.0.2.11</Version>
<Copyright>Copyright © Digital Garage 2017</Copyright>
<Description>Client API for the minimalist HD Wallet Tracker NBXplorer</Description>
<PackageIconUrl>https://aois.blob.core.windows.net/public/Bitcoin.png</PackageIconUrl>
Expand All @@ -16,8 +16,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NBitcoin" Version="4.1.1.10" />
<PackageReference Include="NBitcoin.Altcoins" Version="1.0.1.8" />
<PackageReference Include="NBitcoin" Version="4.1.1.17" />
<PackageReference Include="NBitcoin.Altcoins" Version="1.0.1.10" />
<PackageReference Include="NicolasDorier.StandardConfiguration" Version="1.0.0.14" />
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.2" />
</ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions NBXplorer.Client/NBXplorerNetworkProvider.Groestlcoin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NBitcoin;
using System;
using System.Collections.Generic;
using System.Text;

namespace NBXplorer
{
public partial class NBXplorerNetworkProvider
{
private void InitGroestlcoin(NetworkType networkType)
{
Add(new NBXplorerNetwork(NBitcoin.Altcoins.Groestlcoin.Instance, networkType)
{
MinRPCVersion = 2160000
});
}
}
}
1 change: 1 addition & 0 deletions NBXplorer.Client/NBXplorerNetworkProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public NBXplorerNetworkProvider(NetworkType networkType)
InitLitecoin(networkType);
InitDogecoin(networkType);
InitBCash(networkType);
InitGroestlcoin(networkType);
InitBGold(networkType);
InitDash(networkType);
InitPolis(networkType);
Expand Down
7 changes: 7 additions & 0 deletions NBXplorer.Tests/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Linq;
using NBXplorer.DerivationStrategy;
using NBitcoin.RPC;

namespace NBXplorer.Tests
{
Expand All @@ -14,5 +15,11 @@ public static KeyPathInformation GetKeyInformation(this Repository repo, Script
{
return repo.GetKeyInformations(new Script[] { script })[script].SingleOrDefault();
}

public static uint256[] EnsureGenerate(this RPCClient client, int blockCount)
{
return client.EnsureGenerateAsync(blockCount).GetAwaiter().GetResult();
}
}

}
2 changes: 1 addition & 1 deletion NBXplorer.Tests/NBXplorer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NBitcoin.TestFramework" Version="1.6.6" />
<PackageReference Include="NBitcoin.TestFramework" Version="1.6.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
Expand Down
12 changes: 8 additions & 4 deletions NBXplorer.Tests/ServerTester.Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ private void SetEnvironment()
//nodeDownloadData = NodeDownloadData.Dash.v0_12_2;
//Network = NBitcoin.Altcoins.Dash.Instance.Regtest;
//RPCSupportSegwit = false;

//CryptoCode = "Polis";
//nodeDownloadData = NodeDownloadData.Polis.v1_3_0;
//Network = NBitcoin.Altcoins.Polis.Instance.Regtest;
//RPCSupportSegwit = false;

//CryptoCode = "BTG";
//nodeDownloadData = NodeDownloadData.BGold.v0_15_0;
//Network = NBitcoin.Altcoins.BGold.Instance.Regtest;
//RPCSupportSegwit = false;

//CryptoCode = "MONA";
//nodeDownloadData = NodeDownloadData.Monacoin.v0_15_1;
//Network = NBitcoin.Altcoins.Monacoin.Instance.Regtest;
Expand All @@ -55,12 +55,16 @@ private void SetEnvironment()
//nodeDownloadData = NodeDownloadData.Ufo.v0_16_0;
//Network = NBitcoin.Altcoins.Ufo.Instance.Regtest;
//RPCSupportSegwit = false;

//CryptoCode = "VIA";
//nodeDownloadData = NodeDownloadData.Viacoin.v0_15_1;
//Network = NBitcoin.Altcoins.Viacoin.Instance.Regtest;
//RPCSupportSegwit = false;

//CryptoCode = "GRS";
//nodeDownloadData = NodeDownloadData.Groestlcoin.v2_16_0;
//Network = NBitcoin.Altcoins.Groestlcoin.Instance.Regtest;

CryptoCode = "BTC";
nodeDownloadData = NodeDownloadData.Bitcoin.v0_16_0;
Network = NBitcoin.Network.RegTest;
Expand Down
2 changes: 1 addition & 1 deletion NBXplorer.Tests/ServerTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ServerTester(string directory)
User1.Sync(Explorer, true);
Explorer.CreateRPCClient().Generate(1);
Explorer.Sync(User2, true);
User2.CreateRPCClient().Generate(101);
User2.CreateRPCClient().EnsureGenerate(Network.Consensus.CoinbaseMaturity + 1);
User1.Sync(User2, true);

var datadir = Path.Combine(directory, "explorer");
Expand Down
27 changes: 14 additions & 13 deletions NBXplorer.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void CanTrack4()
utxoBob = tester.Client.GetUTXOs(bobPubKey, utxoBob);
Assert.NotNull(utxoAlice.Unconfirmed.KnownBookmark);

tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice);
utxoBob = tester.Client.GetUTXOs(bobPubKey, utxoBob);
Expand All @@ -423,7 +423,7 @@ public void CanTrack4()
utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice, false);
Assert.NotNull(utxoAlice.Unconfirmed.KnownBookmark);

tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

utxoAlice = tester.Client.GetUTXOs(alicePubKey, utxoAlice);
utxoBob = tester.Client.GetUTXOs(bobPubKey, utxoBob);
Expand Down Expand Up @@ -463,7 +463,7 @@ public void CanTrack3()
events.NextEvent(Timeout);
var utxo = tester.Client.GetUTXOs(pubkey, null);

tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

var prev = utxo;
utxo = tester.Client.GetUTXOs(pubkey, prev);
Expand Down Expand Up @@ -518,7 +518,7 @@ public void CanTrackSeveralTransactions()
break;
}

tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

utxo = tester.Client.GetUTXOs(pubkey, utxo);
Assert.Single(utxo.Confirmed.UTXOs);
Expand Down Expand Up @@ -719,7 +719,7 @@ public void CanGetTransactionsOfDerivation()
result = tester.Client.GetTransactions(pubkey, result, false);
Assert.False(result.HasChanges());

tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);
result = tester.Client.GetTransactions(pubkey, result);
Assert.True(result.HasChanges());
Assert.Null(result.UnconfirmedTransactions.KnownBookmark);
Expand Down Expand Up @@ -765,7 +765,7 @@ public void CanTrack5()
// We receive money
var fundingTx = tester.SendToAddress(tester.AddressOf(key, "0/0"), Money.Coins(1.0m));
utxo = tester.Client.GetUTXOs(pubkey, utxo);
tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);
utxo = tester.Client.GetUTXOs(pubkey, utxo);
Assert.Single(utxo.Confirmed.UTXOs);

Expand Down Expand Up @@ -801,7 +801,7 @@ public void CanTrack()
var gettingUTXO = tester.Client.GetUTXOsAsync(pubkey, utxo);
var txId = tester.SendToAddress(tester.AddressOf(key, "0/0"), Money.Coins(1.0m));
utxo = gettingUTXO.GetAwaiter().GetResult();
Assert.Equal(103, utxo.CurrentHeight);
Assert.Equal(tester.Network.Consensus.CoinbaseMaturity + 3, utxo.CurrentHeight);

Assert.NotNull(utxo.Confirmed.KnownBookmark);
Assert.Single(utxo.Unconfirmed.UTXOs);
Expand All @@ -818,7 +818,7 @@ public void CanTrack()
Assert.Equal(utxo.Unconfirmed.UTXOs[0].Outpoint.Hash, tx.Transaction.GetHash());
Assert.Equal(unconfTimestamp, tx.Timestamp);

tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);
var prevUtxo = utxo;
utxo = tester.Client.GetUTXOs(pubkey, prevUtxo);
Assert.Null(utxo.Unconfirmed.KnownBookmark);
Expand Down Expand Up @@ -867,7 +867,7 @@ public void CanTrack()
Assert.Equal(1, tx.Confirmations);
Assert.NotNull(tx.BlockId);
Assert.Equal(utxo.Confirmed.UTXOs[0].Outpoint.Hash, tx.Transaction.GetHash());
tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

utxo = tester.Client.GetUTXOs(pubkey, utxo);
Assert.Single(utxo.Confirmed.UTXOs);
Expand All @@ -884,7 +884,7 @@ public void CanTrack()
Assert.Single(utxo.Unconfirmed.UTXOs);
Assert.Empty(utxo.Confirmed.UTXOs);
Assert.Equal(new KeyPath("0/2"), utxo.Unconfirmed.UTXOs[0].KeyPath);
tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

utxo = tester.Client.GetUTXOs(pubkey, utxo);
Assert.Single(utxo.Confirmed.UTXOs);
Expand All @@ -911,7 +911,7 @@ public void CanTrack()

utxo = tester.Client.GetUTXOs(pubkey, utxo, false);
Assert.False(utxo.HasChanges);
tester.RPC.Generate(1);
tester.RPC.EnsureGenerate(1);

utxo = tester.Client.GetUTXOs(pubkey, before01Spend, utxo.Unconfirmed.Bookmark);
Assert.True(utxo.Unconfirmed.HasChanges);
Expand Down Expand Up @@ -953,10 +953,11 @@ public void CanTopologicalSort()
[Fact]
public void CanTopologicalSortTx()
{
#pragma warning disable CS0618 // Type or member is obsolete
var tx1 = new Transaction() { Outputs = { new TxOut(Money.Zero, new Key()) } };
var tx2 = new Transaction() { Inputs = { new TxIn(new OutPoint(tx1, 0)) } };
var tx3 = new Transaction() { Inputs = { new TxIn(new OutPoint(tx2, 0)) } };

#pragma warning restore CS0618 // Type or member is obsolete
var arr = new[] { tx2, tx1, tx3 };
var expected = new[] { tx1, tx2, tx3 };
var actual = arr.TopologicalSort().ToArray();
Expand All @@ -969,7 +970,7 @@ public void CanBroadcast()
using(var tester = ServerTester.Create())
{
tester.Client.WaitServerStarted();
var tx = new Transaction();
var tx = tester.Network.Consensus.ConsensusFactory.CreateTransaction();
tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
var funded = tester.User1.CreateRPCClient().FundRawTransaction(tx);
var signed = tester.User1.CreateRPCClient().SignRawTransaction(funded.Transaction);
Expand Down
6 changes: 3 additions & 3 deletions NBXplorer/BitcoinDWaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,10 @@ private void LoadChainFromCache()

private async Task<bool> WarmupBlockchain()
{
if(await _RPC.GetBlockCountAsync() < 100)
if(await _RPC.GetBlockCountAsync() < _Network.NBitcoinNetwork.Consensus.CoinbaseMaturity)
{
Logs.Configuration.LogInformation($"{_Network.CryptoCode}: Less than 100 blocks, mining some block for regtest");
await _RPC.GenerateAsync(101);
Logs.Configuration.LogInformation($"{_Network.CryptoCode}: Less than {_Network.NBitcoinNetwork.Consensus.CoinbaseMaturity} blocks, mining some block for regtest");
await _RPC.EnsureGenerateAsync(_Network.NBitcoinNetwork.Consensus.CoinbaseMaturity + 1);
return true;
}
else
Expand Down
6 changes: 4 additions & 2 deletions NBXplorer/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,12 @@ private async Task<bool> WaitingTransaction(DerivationStrategyBase extPubKey)
[ModelBinder(BinderType = typeof(DestinationModelBinder))]
DerivationStrategyBase extPubKey)
{
var tx = new Transaction();
var network = GetNetwork(cryptoCode);

var tx = network.NBitcoinNetwork.Consensus.ConsensusFactory.CreateTransaction();
var stream = new BitcoinStream(Request.Body, false);
tx.ReadWrite(stream);
var network = GetNetwork(cryptoCode);

var waiter = this.Waiters.GetWaiter(network);
if(!waiter.RPCAvailable)
throw RPCUnavailable();
Expand Down
2 changes: 1 addition & 1 deletion NBXplorer/NBXplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Version>1.0.2.8</Version>
<Version>1.0.2.9</Version>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\NBXplorer.xml</DocumentationFile>
<NoWarn>1701;1702;1705;1591;CS1591</NoWarn>
<LangVersion>7.3</LangVersion>
Expand Down
17 changes: 10 additions & 7 deletions NBXplorer/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,11 @@ public TimeStampedTransaction()
{

}
public TimeStampedTransaction(byte[] hex)
public TimeStampedTransaction(Network network, byte[] hex)
{
this.ReadWrite(hex);
var stream = new BitcoinStream(hex);
stream.ConsensusFactory = network.Consensus.ConsensusFactory;
this.ReadWrite(stream);
}

public TimeStampedTransaction(Transaction tx, ulong timestamp)
Expand Down Expand Up @@ -675,7 +677,7 @@ public List<SavedTransaction> SaveTransactions(DateTimeOffset now, NBitcoin.Tran
var key = blockHash == null ? "0" : blockHash.ToString();
var value = timestamped.ToBytes();
tx.Insert($"{_Suffix}tx-" + btx.GetHash().ToString(), key, value);
result.Add(ToSavedTransaction(key, value));
result.Add(ToSavedTransaction(Network.NBitcoinNetwork, key, value));
}
tx.Commit();
});
Expand Down Expand Up @@ -707,19 +709,19 @@ public SavedTransaction[] GetSavedTransactions(uint256 txid)
{
foreach(var row in tx.SelectForward<string, byte[]>($"{_Suffix}tx-" + txid.ToString()))
{
SavedTransaction t = ToSavedTransaction(row.Key, row.Value);
SavedTransaction t = ToSavedTransaction(Network.NBitcoinNetwork, row.Key, row.Value);
saved.Add(t);
}
});
return saved.ToArray();
}

private static SavedTransaction ToSavedTransaction(string key, byte[] value)
private static SavedTransaction ToSavedTransaction(Network network, string key, byte[] value)
{
SavedTransaction t = new SavedTransaction();
if(key.Length != 1)
t.BlockHash = new uint256(key);
var timeStamped = new TimeStampedTransaction(value);
var timeStamped = new TimeStampedTransaction(network, value);
t.Transaction = timeStamped.Transaction;
t.Timestamp = NBitcoin.Utils.UnixTimeToDateTime(timeStamped.TimeStamp);
t.Transaction.PrecomputeHash(true, false);
Expand Down Expand Up @@ -839,7 +841,7 @@ public TrackedTransaction[] GetTransactions(DerivationStrategyBase pubkey)
continue;
MemoryStream ms = new MemoryStream(row.Value);
BitcoinStream bs = new BitcoinStream(ms, false);
bs.ConsensusFactory = Network.NBitcoinNetwork.Consensus.ConsensusFactory;
TransactionMatchData data = new TransactionMatchData();
bs.ReadWrite(ref data);
data.Transaction.PrecomputeHash(true, true);
Expand Down Expand Up @@ -1144,6 +1146,7 @@ public void SaveMatches(DateTimeOffset now, MatchedTransaction[] transactions)
var ticksCount = now.UtcTicks;
var ms = new MemoryStream();
BitcoinStream bs = new BitcoinStream(ms, true);
bs.ConsensusFactory = Network.NBitcoinNetwork.Consensus.ConsensusFactory;
TransactionMatchData data = new TransactionMatchData()
{
Transaction = value.Match.Transaction,
Expand Down

0 comments on commit 0572d77

Please sign in to comment.