Skip to content

Commit 45418fc

Browse files
NicolasDorierHesquibet
authored andcommitted
Fix tests (#1270)
1 parent 2615555 commit 45418fc

File tree

14 files changed

+52
-35
lines changed

14 files changed

+52
-35
lines changed

NBitcoin.Altcoins/Litecoin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ public override void ReadWrite(BitcoinStream stream)
219219
}
220220
else
221221
{
222+
if (Inputs.Count == 0 && !stream.AllowNoInputs)
223+
throw new InvalidOperationException("The transaction must have at least one input");
224+
222225
stream.ReadWrite(ref nVersion);
223226

224227
if (witSupported)

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static void ShouldCalculateBalanceOfHDKey()
5454
var bob = bobMaster.Derive(new KeyPath("4/5/6"));
5555

5656
var funding = network.CreateTransaction();
57+
funding.Inputs.Add();
5758
funding.Outputs.Add(Money.Coins(1.0m), alice);
5859
funding.Outputs.Add(Money.Coins(1.5m), bob);
5960

@@ -605,8 +606,9 @@ public void CanRebaseKeypathInPSBT()
605606
var accountExtKey = masterExtkey.Derive(new KeyPath("0'/0'/0'"));
606607
var accountRootedKeyPath = new KeyPath("0'/0'/0'").ToRootedKeyPath(masterExtkey);
607608
uint hardenedFlag = 0x80000000U;
608-
retry:
609+
retry:
609610
Transaction funding = masterExtkey.Network.CreateTransaction();
611+
funding.Inputs.Add();
610612
funding.Outputs.Add(Money.Coins(2.0m), accountExtKey.Derive(0 | hardenedFlag).ScriptPubKey);
611613
funding.Outputs.Add(Money.Coins(2.0m), accountExtKey.Derive(1 | hardenedFlag).ScriptPubKey);
612614

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
}

NBitcoin.Tests/TestUtils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public static Block CreateFakeBlock(Transaction tx)
4747

4848
public static Block CreateFakeBlock()
4949
{
50-
var block = TestUtils.CreateFakeBlock(Network.Main.CreateTransaction());
50+
var tx = Network.Main.CreateTransaction();
51+
tx.Inputs.Add();
52+
var block = TestUtils.CreateFakeBlock(tx);
5153
block.Header.HashPrevBlock = new uint256(RandomUtils.GetBytes(32));
5254
block.Header.Nonce = RandomUtils.GetUInt32();
5355
return block;

NBitcoin.Tests/script_tests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public void BIP65_tests()
149149
private void BIP65_testsCore(LockTime target, LockTime now, bool expectedResult)
150150
{
151151
Transaction tx = Network.CreateTransaction();
152+
tx.Inputs.Add();
152153
tx.Outputs.Add(new TxOut()
153154
{
154155
ScriptPubKey = new Script(Op.GetPushOp(target.Value), OpcodeType.OP_CHECKLOCKTIMEVERIFY)
@@ -736,6 +737,7 @@ public void script_CHECKMULTISIG12()
736737
);
737738

738739
Transaction txFrom12 = Network.CreateTransaction();
740+
txFrom12.Inputs.Add();
739741
txFrom12.Outputs.Add(new TxOut());
740742
txFrom12.Outputs[0].ScriptPubKey = scriptPubKey12;
741743

@@ -780,6 +782,7 @@ public void script_CHECKMULTISIG23()
780782

781783

782784
var txFrom23 = Network.CreateTransaction();
785+
txFrom23.Inputs.Add();
783786
txFrom23.Outputs.Add(new TxOut());
784787
txFrom23.Outputs[0].ScriptPubKey = scriptPubKey23;
785788

NBitcoin.Tests/transaction_tests.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ public void CanBuildShuffleColoredTransaction()
987987
var repo = new NoSqlColoredTransactionRepository(new NoSqlTransactionRepository(), new InMemoryNoSqlRepository());
988988

989989
var init = Network.CreateTransaction();
990+
init.Inputs.Add();
990991
init.Outputs.Add("1.0", gold.PubKey);
991992
init.Outputs.Add("1.0", silver.PubKey);
992993
init.Outputs.Add("1.0", satoshi.PubKey);
@@ -1278,6 +1279,7 @@ public void CanBuildColoredTransaction()
12781279
var repo = new NoSqlColoredTransactionRepository();
12791280

12801281
var init = Network.CreateTransaction();
1282+
init.Inputs.Add();
12811283
init.Outputs.Add("1.0", gold.PubKey);
12821284
init.Outputs.Add("1.0", silver.PubKey);
12831285
init.Outputs.Add("1.0", satoshi.PubKey);
@@ -1374,6 +1376,7 @@ public void CanBuildColoredTransaction()
13741376

13751377
//Gold receive 2.5 BTC
13761378
tx = txBuilder.Network.Consensus.ConsensusFactory.CreateTransaction();
1379+
tx.Inputs.Add();
13771380
tx.Outputs.Add("2.5", gold.PubKey);
13781381
repo.Transactions.Put(tx.GetHash(), tx);
13791382

@@ -1761,6 +1764,7 @@ public void CanEstimateFees()
17611764
builder.SendEstimatedFees(rate);
17621765
signed = builder.BuildTransaction(true);
17631766
Assert.True(builder.Verify(signed, estimatedFees));
1767+
Assert.Equal(1174, builder.EstimateSize(signed));
17641768
}
17651769

17661770
private Coin RandomCoin(Money amount, IDestination dest, bool p2sh)
@@ -1919,23 +1923,6 @@ void BitcoinStreamCoverageCore<TItem>(TItem[] input, BitcoinStreamCoverageCoreDe
19191923
AssertEx.CollectionEquals(before, input);
19201924
}
19211925

1922-
[Fact]
1923-
[Trait("UnitTest", "UnitTest")]
1924-
public void CanSerializeInvalidTransactionsBackAndForth()
1925-
{
1926-
Transaction before = Network.CreateTransaction();
1927-
var versionBefore = before.Version;
1928-
before.Outputs.Add(new TxOut());
1929-
Transaction after = AssertClone(before);
1930-
Assert.Equal(before.Version, after.Version);
1931-
Assert.Equal(versionBefore, after.Version);
1932-
Assert.True(after.Outputs.Count == 1);
1933-
1934-
before = Network.CreateTransaction();
1935-
after = AssertClone(before);
1936-
Assert.Equal(before.Version, versionBefore);
1937-
}
1938-
19391926
private Transaction AssertClone(Transaction before)
19401927
{
19411928
Transaction after = before.Clone();
@@ -2095,6 +2082,7 @@ public void CanFilterUneconomicalCoins()
20952082
var bob = new Key();
20962083
//P2SH(P2WSH)
20972084
var previousTx = Network.CreateTransaction();
2085+
previousTx.Inputs.Add();
20982086
previousTx.Outputs.Add(new TxOut(Money.Coins(1.0m), alice.PubKey.ScriptPubKey.WitHash.ScriptPubKey.Hash));
20992087
var previousCoin = previousTx.Outputs.AsCoins().First();
21002088

@@ -2643,6 +2631,7 @@ public void CanBuildTransactionWithDustPrevention()
26432631
var bob = new Key();
26442632
var alice = new Key();
26452633
var tx = Network.CreateTransaction();
2634+
tx.Inputs.Add();
26462635
tx.Outputs.Add(Money.Coins(1.0m), bob);
26472636
var coins = tx.Outputs.AsCoins().ToArray();
26482637

@@ -2837,6 +2826,7 @@ public void CanMutateSignature()
28372826
public void CanUseLockTime()
28382827
{
28392828
var tx = Network.CreateTransaction();
2829+
tx.Inputs.Add();
28402830
tx.LockTime = new LockTime(4);
28412831
var clone = tx.Clone();
28422832
Assert.Equal(tx.LockTime, clone.LockTime);
@@ -3067,6 +3057,7 @@ public void witnessHasPushSizeLimit()
30673057
{
30683058
var bob = new Key().GetWif(Network.RegTest);
30693059
Transaction tx = Network.CreateTransaction();
3060+
tx.Inputs.Add();
30703061
tx.Outputs.Add(new TxOut(Money.Coins(1.0m), bob.PubKey.ScriptPubKey.WitHash));
30713062
ScriptCoin coin = new ScriptCoin(tx.Outputs.AsCoins().First(), bob.PubKey.ScriptPubKey);
30723063

NBitcoin/BitcoinStream.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ public void CopyParameters(BitcoinStream from)
592592
IsBigEndian = from.IsBigEndian;
593593
MaxArraySize = from.MaxArraySize;
594594
Type = from.Type;
595+
AllowNoInputs = from.AllowNoInputs;
595596
}
596597

597598
public SerializationType Type
@@ -630,6 +631,13 @@ public System.Threading.CancellationToken ReadCancellationToken
630631
set;
631632
}
632633

634+
/// <summary>
635+
/// Allows serialization of transactions with no inputs.
636+
/// Such transactions are not valid for deserialization, but may still be useful,
637+
/// for example, when computing a transaction hash or estimating size.
638+
/// </summary>
639+
public bool AllowNoInputs { get; set; }
640+
633641
public void ReadWriteAsVarInt(ref uint val)
634642
{
635643
if (Serializing)

0 commit comments

Comments
 (0)