Skip to content

Commit

Permalink
Merge pull request stratisproject#16 from stratisproject/segwit
Browse files Browse the repository at this point in the history
Segwit
  • Loading branch information
dangershony authored Nov 16, 2019
2 parents 25c4f82 + 15822b5 commit dd91484
Show file tree
Hide file tree
Showing 296 changed files with 11,713 additions and 1,806 deletions.
6 changes: 3 additions & 3 deletions Scripts/LaunchSidechainMasternode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ function Check-TimeDifference
{
Write-Host "Checking UTC Time Difference" -ForegroundColor Cyan
$timeDifSamples = @([int16]::MaxValue,[int16]::MaxValue,[int16]::MaxValue)
$timeDifSamples[0] = New-TimeSpan -Start (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") -End ( Invoke-WebRequest http://worldtimeapi.org/api/timezone/UTC | ConvertFrom-Json | Select-Object -ExpandProperty utc_datetime ) | Select-Object -ExpandProperty TotalSeconds
$timeDifSamples[1] = New-TimeSpan -Start (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") -End ( Invoke-WebRequest http://worldtimeapi.org/api/timezone/UTC | ConvertFrom-Json | Select-Object -ExpandProperty utc_datetime ) | Select-Object -ExpandProperty TotalSeconds
$timeDifSamples[2] = New-TimeSpan -Start (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") -End ( Invoke-WebRequest http://worldtimeapi.org/api/timezone/UTC | ConvertFrom-Json | Select-Object -ExpandProperty utc_datetime ) | Select-Object -ExpandProperty TotalSeconds
$timeDifSamples[0] = New-TimeSpan -Start (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") -End ( Invoke-WebRequest http://worldtimeapi.org/api/timezone/Etc/GMT | ConvertFrom-Json | Select-Object -ExpandProperty utc_datetime ) | Select-Object -ExpandProperty TotalSeconds
$timeDifSamples[1] = New-TimeSpan -Start (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") -End ( Invoke-WebRequest http://worldtimeapi.org/api/timezone/Etc/GMT | ConvertFrom-Json | Select-Object -ExpandProperty utc_datetime ) | Select-Object -ExpandProperty TotalSeconds
$timeDifSamples[2] = New-TimeSpan -Start (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") -End ( Invoke-WebRequest http://worldtimeapi.org/api/timezone/Etc/GMT | ConvertFrom-Json | Select-Object -ExpandProperty utc_datetime ) | Select-Object -ExpandProperty TotalSeconds

$timeDif = Get-Median -numberSeries $timeDifSamples

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions src/FederationSetup/FederationSetup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeFrameworkVersion>2.1.1</RuntimeFrameworkVersion>
<StartupObject>FederationSetup.Program</StartupObject>
<RuntimeIdentifiers>win10-x64;</RuntimeIdentifiers>
<DebugType>Full</DebugType>
Expand Down
6 changes: 3 additions & 3 deletions src/NBitcoin.Tests/NBitcoin.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
15 changes: 6 additions & 9 deletions src/NBitcoin.Tests/NetworkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ public void StratisMainIsInitializedCorrectly()
Assert.Equal("STRAT", network.CoinTicker);

Assert.Equal(2, network.Bech32Encoders.Length);
Assert.Null(network.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS]);
Assert.Null(network.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS]);
Assert.Equal(new Bech32Encoder("strat").ToString(), network.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS].ToString());
Assert.Equal(new Bech32Encoder("strat").ToString(), network.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS].ToString());

Assert.Equal(12, network.Base58Prefixes.Length);
Assert.Equal(new byte[] { (63) }, network.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS]);
Expand Down Expand Up @@ -377,7 +377,6 @@ public void StratisMainIsInitializedCorrectly()
Assert.False(network.Consensus.PowNoRetargeting);
Assert.Equal(1916, network.Consensus.RuleChangeActivationThreshold);
Assert.Equal(2016, network.Consensus.MinerConfirmationWindow);
Assert.Null(network.Consensus.BIP9Deployments[StratisBIP9Deployments.TestDummy]);
Assert.Equal(12500, network.Consensus.LastPOWBlock);
Assert.True(network.Consensus.IsProofOfStake);
Assert.Equal(105, network.Consensus.CoinType);
Expand Down Expand Up @@ -421,8 +420,8 @@ public void StratisTestnetIsInitializedCorrectly()
Assert.Equal("TSTRAT", network.CoinTicker);

Assert.Equal(2, network.Bech32Encoders.Length);
Assert.Null(network.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS]);
Assert.Null(network.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS]);
Assert.Equal(new Bech32Encoder("tstrat").ToString(), network.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS].ToString());
Assert.Equal(new Bech32Encoder("tstrat").ToString(), network.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS].ToString());

Assert.Equal(12, network.Base58Prefixes.Length);
Assert.Equal(new byte[] { (65) }, network.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS]);
Expand Down Expand Up @@ -454,7 +453,6 @@ public void StratisTestnetIsInitializedCorrectly()
Assert.False(network.Consensus.PowNoRetargeting);
Assert.Equal(1916, network.Consensus.RuleChangeActivationThreshold);
Assert.Equal(2016, network.Consensus.MinerConfirmationWindow);
Assert.Null(network.Consensus.BIP9Deployments[StratisBIP9Deployments.TestDummy]);
Assert.Equal(12500, network.Consensus.LastPOWBlock);
Assert.True(network.Consensus.IsProofOfStake);
Assert.Equal(105, network.Consensus.CoinType);
Expand Down Expand Up @@ -498,8 +496,8 @@ public void StratisRegTestIsInitializedCorrectly()
Assert.Equal("TSTRAT", network.CoinTicker);

Assert.Equal(2, network.Bech32Encoders.Length);
Assert.Null(network.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS]);
Assert.Null(network.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS]);
Assert.Equal(new Bech32Encoder("tstrat").ToString(), network.Bech32Encoders[(int)Bech32Type.WITNESS_PUBKEY_ADDRESS].ToString());
Assert.Equal(new Bech32Encoder("tstrat").ToString(), network.Bech32Encoders[(int)Bech32Type.WITNESS_SCRIPT_ADDRESS].ToString());

Assert.Equal(12, network.Base58Prefixes.Length);
Assert.Equal(new byte[] { (65) }, network.Base58Prefixes[(int)Base58Type.PUBKEY_ADDRESS]);
Expand Down Expand Up @@ -531,7 +529,6 @@ public void StratisRegTestIsInitializedCorrectly()
Assert.True(network.Consensus.PowNoRetargeting);
Assert.Equal(1916, network.Consensus.RuleChangeActivationThreshold);
Assert.Equal(2016, network.Consensus.MinerConfirmationWindow);
Assert.Null(network.Consensus.BIP9Deployments[StratisBIP9Deployments.TestDummy]);
Assert.Equal(12500, network.Consensus.LastPOWBlock);
Assert.True(network.Consensus.IsProofOfStake);
Assert.Equal(105, network.Consensus.CoinType);
Expand Down
3 changes: 3 additions & 0 deletions src/NBitcoin/Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public class Consensus : IConsensus
/// <inheritdoc />
public bool IsProofOfStake { get; }

/// <inheritdoc />
public bool PosEmptyCoinbase { get; set; }

/// <inheritdoc />
public uint256 DefaultAssumeValid { get; }

Expand Down
11 changes: 7 additions & 4 deletions src/NBitcoin/ConsensusOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ public ConsensusOptions(
int maxStandardVersion,
int maxStandardTxWeight,
int maxBlockSigopsCost,
int maxStandardTxSigopsCost)
int maxStandardTxSigopsCost,
int witnessScaleFactor)
{
this.MaxBlockBaseSize = maxBlockBaseSize;

// Having witnessScale = 1 and setting the weight to be the same value as the base size
// will result in all checks comparing size in bytes.
this.MaxBlockWeight = maxBlockBaseSize;
this.MaxBlockSerializedSize = maxBlockBaseSize;
this.WitnessScaleFactor = 1;
this.WitnessScaleFactor = witnessScaleFactor;

this.MaxStandardVersion = maxStandardVersion;
this.MaxStandardTxWeight = maxStandardTxWeight;
Expand Down Expand Up @@ -179,8 +180,9 @@ public PosConsensusOptions(
int maxStandardVersion,
int maxStandardTxWeight,
int maxBlockSigopsCost,
int maxStandardTxSigopsCost
) : base(maxBlockBaseSize, maxStandardVersion, maxStandardTxWeight, maxBlockSigopsCost, maxStandardTxSigopsCost)
int maxStandardTxSigopsCost,
int witnessScaleFactor
) : base(maxBlockBaseSize, maxStandardVersion, maxStandardTxWeight, maxBlockSigopsCost, maxStandardTxSigopsCost, witnessScaleFactor)
{
}

Expand All @@ -191,6 +193,7 @@ int maxStandardTxSigopsCost
/// <param name="network">The network.</param>
public virtual int GetStakeMinConfirmations(int height, Network network)
{
// TODO: Is there supposed to be a defined activation height for regtest?
if (network.NetworkType == NetworkType.Testnet || network.NetworkType == NetworkType.Regtest)
return height < CoinstakeMinConfirmationActivationHeightTestnet ? 10 : 20;

Expand Down
10 changes: 10 additions & 0 deletions src/NBitcoin/IConsensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public interface IConsensus
/// <summary>PoW blocks are not accepted after block with height <see cref="Consensus.LastPOWBlock"/>.</summary>
int LastPOWBlock { get; set; }

/// <summary>
/// This flag will restrict the coinbase in a POS network to be empty.
/// For legacy POS the coinbase is required to be empty.
/// </summary>
/// <remarks>
/// Some implementations will put extra data in the coinbase (for example the witness commitment)
/// To allow such data to be in the coinbase we use this flag, a POS network that already has that limitation will use the coinbase input instead.
/// </remarks>
bool PosEmptyCoinbase { get; set; }

/// <summary>
/// An indicator whether this is a Proof Of Stake network.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/NBitcoin/Policy/StandardTransactionPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public TransactionPolicyError[] Check(Transaction transaction, ICoin[] spentCoin
{
if (this.ScriptVerify != null)
{
if(!input.VerifyScript(this.network, coin.TxOut.ScriptPubKey, coin.TxOut.Value, this.ScriptVerify.Value, out ScriptError error))
if (!input.VerifyScript(this.network, coin.TxOut.ScriptPubKey, coin.TxOut.Value, this.ScriptVerify.Value, out ScriptError error))
{
errors.Add(new ScriptPolicyError(input, error, this.ScriptVerify.Value, coin.TxOut.ScriptPubKey));
}
Expand Down Expand Up @@ -180,11 +180,11 @@ protected virtual void CheckMinRelayTxFee(Transaction transaction, List<Transact
}
}

protected static bool IsOpReturn(byte[] bytes)
public static bool IsOpReturn(byte[] bytes)
{
return bytes.Length > 0 && bytes[0] == (byte)OpcodeType.OP_RETURN;
}

public StandardTransactionPolicy Clone()
{
return new StandardTransactionPolicy(this.network)
Expand Down
7 changes: 6 additions & 1 deletion src/NBitcoin/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ public enum ScriptVerify : uint
| Witness
| DiscourageUpgradableWitnessProgram
| NullFail
| MinimalIf
| MinimalIf,

/// <summary>
/// For convenience, standard but not mandatory verify flags
/// </summary>
StandardNotMandatory = Standard & ~Mandatory
}

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/NBitcoin/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ public WitScript WitScript
}
}

public bool WitScriptEmpty
{
get
{
return this.WitScript == WitScript.Empty || this.WitScript == null;
}
}

#region IBitcoinSerializable Members

public void ReadWrite(BitcoinStream stream)
Expand Down Expand Up @@ -1893,7 +1901,7 @@ public bool HasWitness
{
get
{
return this.Inputs.Any(i => i.WitScript != WitScript.Empty && i.WitScript != null);
return this.Inputs.Any(i => i.WitScriptEmpty == false);
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/NBitcoin/TransactionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,36 @@ public IEnumerable<ICoin> Select(IEnumerable<ICoin> coins, IMoney target)
}
}

return result;
if (minTotal != null)
{
total = minTotal;
}
break;
}
}
}

if (total.CompareTo(target) == -1)
return null;

// optimize the set of used coins, removing unnecessary small inputs
List<ICoin> sortedUsedCoins = result.OrderBy(c => c.Amount).ToList();
IMoney excess = total.Sub(target);
for (int i = 0; i < sortedUsedCoins.Count; i++)
{
// if the smallest coin in the set is smaller or equal to the difference between the excess of
// the total and its amount, we can safely remove it
ICoin coin = sortedUsedCoins[i];
if (coin.Amount.CompareTo(excess) <= 0)
{
result.Remove(coin);
excess = excess.Sub(coin.Amount);
}

if (excess.CompareTo(zero) <= 0)
break;
}

return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Stratis.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static void Main(string[] args)
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

// for debug benchmark, adds "new DebugInProcessConfig()"
// BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, new DebugInProcessConfig());
//BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, new DebugInProcessConfig());
}
}
}
3 changes: 2 additions & 1 deletion src/Stratis.Benchmark/Stratis.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeFrameworkVersion>2.1.1</RuntimeFrameworkVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
83 changes: 83 additions & 0 deletions src/Stratis.Bitcoin.Features.Api/ConfigureSwaggerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.IO;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Stratis.Bitcoin.Features.Api
{
/// <summary>
/// Configures the Swagger generation options.
/// </summary>
/// <remarks>This allows API versioning to define a Swagger document per API version after the
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container.
/// Adapted from https://github.com/microsoft/aspnet-api-versioning/blob/master/samples/aspnetcore/SwaggerSample/ConfigureSwaggerOptions.cs.
/// </remarks>
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
{
private const string ApiXmlFilename = "Stratis.Bitcoin.Api.xml";
private const string WalletXmlFilename = "Stratis.Bitcoin.LightWallet.xml";

private readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider)
{
this.provider = provider;
}

/// <inheritdoc />
public void Configure(SwaggerGenOptions options)
{
// Add a swagger document for each discovered API version
foreach (ApiVersionDescription description in this.provider.ApiVersionDescriptions)
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
}

//Set the comments path for the swagger json and ui.
string basePath = PlatformServices.Default.Application.ApplicationBasePath;
string apiXmlPath = Path.Combine(basePath, ApiXmlFilename);
string walletXmlPath = Path.Combine(basePath, WalletXmlFilename);

if (File.Exists(apiXmlPath))
{
options.IncludeXmlComments(apiXmlPath);
}

if (File.Exists(walletXmlPath))
{
options.IncludeXmlComments(walletXmlPath);
}

options.DescribeAllEnumsAsStrings();
}

static Info CreateInfoForApiVersion(ApiVersionDescription description)
{
var info = new Info()
{
Title = "Stratis Node API",
Version = description.ApiVersion.ToString(),
Description = "Access to the Stratis Node's core features."
};

if (info.Version.Contains("dev"))
{
info.Description += " This version of the API is in development and subject to change. Use an earlier version for production applications.";
}

if (description.IsDeprecated)
{
info.Description += " This API version has been deprecated.";
}

return info;
}
}
}
Loading

0 comments on commit dd91484

Please sign in to comment.