Skip to content

Commit 9d87d1e

Browse files
hemetacoHesquibetNicolasDorier
authored
backport to 7.x (#1271)
* backport to 7.x * Fix tests (#1270) * add RemoveSignatures method --------- Co-authored-by: Hugo ESQUIBET <hesquibet@ripple.com> Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
1 parent c53b096 commit 9d87d1e

19 files changed

+90
-66
lines changed

NBitcoin.Altcoins/Bitcoinplus.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,7 @@ private void DeserializeTxn(BitcoinStream stream, bool witSupported)
338338
stream.ReadWrite(ref vinTemp);
339339
vinTemp.Transaction = this;
340340

341-
var hasNoDummy = (nVersionTemp & NoDummyInput) != 0 && vinTemp.Count == 0;
342-
if (witSupported && hasNoDummy)
343-
nVersionTemp = nVersionTemp & ~NoDummyInput;
344-
345-
if (vinTemp.Count == 0 && witSupported && !hasNoDummy)
341+
if (vinTemp.Count == 0 && witSupported)
346342
{
347343
/* We read a dummy or an empty vin. */
348344
stream.ReadWrite(ref flags);
@@ -392,8 +388,7 @@ private void DeserializeTxn(BitcoinStream stream, bool witSupported)
392388
private void SerializeTxn(BitcoinStream stream, bool witSupported)
393389
{
394390
byte flags = 0;
395-
var version = (witSupported && (this.Inputs.Count == 0 && this.Outputs.Count > 0)) ? this.Version | NoDummyInput : this.Version;
396-
stream.ReadWrite(ref version);
391+
stream.ReadWrite(ref nVersion);
397392

398393
// POS Timestamp
399394
var time = this.Time;

NBitcoin.Altcoins/Litecoin.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,7 @@ public override ConsensusFactory GetConsensusFactory()
161161
public override void ReadWrite(BitcoinStream stream)
162162
{
163163
var witSupported = (((uint)stream.TransactionOptions & (uint)TransactionOptions.Witness) != 0) &&
164-
stream.ProtocolCapabilities.SupportWitness;
165-
166-
//var mwebSupported = false; //when mweb is supported in nbitcoin this is to be fixed
164+
stream.ProtocolCapabilities.SupportWitness;
167165

168166
byte flags = 0;
169167
if (!stream.Serializing)
@@ -172,11 +170,8 @@ public override void ReadWrite(BitcoinStream stream)
172170
/* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
173171
stream.ReadWrite(ref vin);
174172
vin.Transaction = this;
175-
var hasNoDummy = (nVersion & NoDummyInput) != 0 && vin.Count == 0;
176-
if (witSupported && hasNoDummy)
177-
nVersion = nVersion & ~NoDummyInput;
178173

179-
if (vin.Count == 0 && witSupported && !hasNoDummy)
174+
if (vin.Count == 0 && witSupported)
180175
{
181176
/* We read a dummy or an empty vin. */
182177
stream.ReadWrite(ref flags);
@@ -224,8 +219,10 @@ public override void ReadWrite(BitcoinStream stream)
224219
}
225220
else
226221
{
227-
var version = (witSupported && (vin.Count == 0 && vout.Count > 0)) ? nVersion | NoDummyInput : nVersion;
228-
stream.ReadWrite(ref version);
222+
if (Inputs.Count == 0 && !stream.AllowNoInputs)
223+
throw new InvalidOperationException("The transaction must have at least one input");
224+
225+
stream.ReadWrite(ref nVersion);
229226

230227
if (witSupported)
231228
{
@@ -238,8 +235,8 @@ public override void ReadWrite(BitcoinStream stream)
238235
if (flags != 0)
239236
{
240237
/* Use extended format in case witnesses are to be serialized. */
241-
TxInList vinDummy = new TxInList();
242-
stream.ReadWrite(ref vinDummy);
238+
byte marker = 0;
239+
stream.ReadWrite(ref marker);
243240
stream.ReadWrite(ref flags);
244241
}
245242
stream.ReadWrite(ref vin);

NBitcoin.Altcoins/NBitcoin.Altcoins.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<TargetFrameworks Condition="'$(TargetFrameworkOverride)' != ''">$(TargetFrameworkOverride)</TargetFrameworks>
1818
<NoWarn>1591;1573;1572;1584;1570;3021</NoWarn>
1919
<GenerateDocumentationFile>true</GenerateDocumentationFile>
20-
<Version>3.0.28</Version>
20+
<Version>3.0.28.1</Version>
2121
</PropertyGroup>
2222
<ItemGroup>
2323
<ProjectReference Include="..\NBitcoin\NBitcoin.csproj" />

NBitcoin.Altcoins/Stratis.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,8 @@ private void DeserializeTxn(BitcoinStream stream, bool witSupported)
360360
// Try to read the vin. In case the dummy is there, this will be read as an empty vector.
361361
stream.ReadWrite(ref vinTemp);
362362
vinTemp.Transaction = this;
363-
var hasNoDummy = (nVersionTemp & NoDummyInput) != 0 && vinTemp.Count == 0;
364-
if (witSupported && hasNoDummy)
365-
nVersionTemp = nVersionTemp & ~NoDummyInput;
366363

367-
if (vinTemp.Count == 0 && witSupported && !hasNoDummy)
364+
if (vinTemp.Count == 0 && witSupported)
368365
{
369366
// We read a dummy or an empty vin.
370367
stream.ReadWrite(ref flags);
@@ -414,8 +411,7 @@ private void DeserializeTxn(BitcoinStream stream, bool witSupported)
414411
private void SerializeTxn(BitcoinStream stream, bool witSupported)
415412
{
416413
byte flags = 0;
417-
var version = (witSupported && (this.Inputs.Count == 0 && this.Outputs.Count > 0)) ? this.Version | NoDummyInput : this.Version;
418-
stream.ReadWrite(ref version);
414+
stream.ReadWrite(ref nVersion);
419415

420416
// POS Timestamp
421417
var time = this.Time;

NBitcoin.TestFramework/NBitcoin.TestFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<VersionPrefix>3.0.28</VersionPrefix>
4+
<VersionPrefix>3.0.28.1</VersionPrefix>
55
<LangVersion>9.0</LangVersion>
66
<TargetFrameworks>netstandard1.6;net472;netstandard2.0</TargetFrameworks>
77
<TargetFrameworks Condition="'$(BuildingInsideVisualStudio)' == 'true'">netstandard2.1</TargetFrameworks>

NBitcoin.Tests/ChainTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ public ChainedBlock AppendBlock(ChainedBlock previous, params ConcurrentChain[]
608608
var nonce = RandomUtils.GetUInt32();
609609
foreach (var chain in chains)
610610
{
611-
var block = TestUtils.CreateFakeBlock(Network.Main.CreateTransaction());
611+
var tx = Network.Main.CreateTransaction();
612+
tx.Inputs.Add();
613+
var block = TestUtils.CreateFakeBlock(tx);
612614
block.Header.HashPrevBlock = previous == null ? chain.Tip.HashBlock : previous.HashBlock;
613615
block.Header.Nonce = nonce;
614616
if (!chain.TrySetTip(block.Header, out last))

NBitcoin.Tests/ColoredCoinsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void CanColorizeSpecScenario()
151151
Assert.True(destroyed[0].Id == a2.Id);
152152

153153
var prior = Network.Main.CreateTransaction();
154+
prior.Inputs.Add();
154155
prior.Outputs.Add(new TxOut(dust, a1.ScriptPubKey));
155156
prior.Outputs.Add(new TxOut(dust, a2.ScriptPubKey));
156157
prior.Outputs.Add(new TxOut(dust, h.ScriptPubKey));

NBitcoin.Tests/Generators/PSBTGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ from psbt in SanePSBT(network)
3838
/// <param name="network"></param>
3939
/// <returns></returns>
4040
public static Gen<PSBT> SanePSBT(Network network) =>
41-
from inputN in Gen.Choose(0, 8)
41+
from inputN in Gen.Choose(1, 8)
4242
from scripts in Gen.ListOf(inputN, ScriptGenerator.RandomScriptSig())
4343
from txOuts in Gen.Sequence(scripts.Select(sc => OutputFromRedeem(sc)))
4444
from prevN in Gen.Choose(0, 5)

NBitcoin.Tests/PSBTTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Linq;
99
using static NBitcoin.Tests.Comparer;
1010
using Xunit.Abstractions;
11+
using System.Net.Http;
12+
using System.Threading.Tasks;
1113

1214
namespace NBitcoin.Tests
1315
{
@@ -52,6 +54,7 @@ public static void ShouldCalculateBalanceOfHDKey()
5254
var bob = bobMaster.Derive(new KeyPath("4/5/6"));
5355

5456
var funding = network.CreateTransaction();
57+
funding.Inputs.Add();
5558
funding.Outputs.Add(Money.Coins(1.0m), alice);
5659
funding.Outputs.Add(Money.Coins(1.5m), bob);
5760

@@ -153,6 +156,15 @@ public static void ShouldParseValidDataDeterministically()
153156
Assert.Equal(psbt, psbt2, ComparerInstance);
154157
}
155158
}
159+
160+
[Fact]
161+
[Trait("UnitTest", "UnitTest")]
162+
public void FixVersionParsingBug()
163+
{
164+
var tx = Transaction.Parse("39a75f190001010000000000000000000000000000000000000000000000000000000000000000ffffffff4903d7ae0d04970a256861627a637862fabe6d6d86846e235af48afb776d0a32cb278930b7dd601fb0e05011789ef233a3e0e7d1010000000000000012b299f4e40000000000ffffffffffffffff02ec12bb1200000000160014b6f3cfc20084e3b9f0d12b0e6f9da8fcbcf5a2d90000000000000000266a24aa21a9edc8a9c6157fb538507480083f8f5144e2f73d1bd9b6b5fabde17bd08ff524b7100120000000000000000000000000000000000000000000000000000000000000000000000000", Network.Main);
165+
Assert.Equal("889d4fed1ec4a775d02082b5ff727f48d93013469db709d8d435e15b173118f3", tx.GetHash().ToString());
166+
}
167+
156168
[Fact]
157169
[Trait("UnitTest", "UnitTest")]
158170
public void AddCoinsShouldNotRemoveInfoFromPSBT()
@@ -594,8 +606,9 @@ public void CanRebaseKeypathInPSBT()
594606
var accountExtKey = masterExtkey.Derive(new KeyPath("0'/0'/0'"));
595607
var accountRootedKeyPath = new KeyPath("0'/0'/0'").ToRootedKeyPath(masterExtkey);
596608
uint hardenedFlag = 0x80000000U;
597-
retry:
609+
retry:
598610
Transaction funding = masterExtkey.Network.CreateTransaction();
611+
funding.Inputs.Add();
599612
funding.Outputs.Add(Money.Coins(2.0m), accountExtKey.Derive(0 | hardenedFlag).ScriptPubKey);
600613
funding.Outputs.Add(Money.Coins(2.0m), accountExtKey.Derive(1 | hardenedFlag).ScriptPubKey);
601614

NBitcoin.Tests/ProtocolTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,9 @@ public void CanConnectMultipleTimeToServer()
11021102
public void CanRoundtripCmpctBlock()
11031103
{
11041104
Block block = Network.Main.Consensus.ConsensusFactory.CreateBlock();
1105-
block.Transactions.Add(Network.Main.Consensus.ConsensusFactory.CreateTransaction());
1105+
var tx = Network.Main.Consensus.ConsensusFactory.CreateTransaction();
1106+
tx.Inputs.Add();
1107+
block.Transactions.Add(tx);
11061108
var cmpct = new CmpctBlockPayload(block);
11071109
cmpct.Clone();
11081110
}

0 commit comments

Comments
 (0)