From f04c2e4773f5b12fc9c1d075e5643652af32d3a1 Mon Sep 17 00:00:00 2001 From: Matthew Mitchell Date: Fri, 18 Aug 2023 15:51:23 +0100 Subject: [PATCH] Ensure size limit applies equally to witness tranasctions --- coinlib/test/tx/transaction_test.dart | 36 ++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/coinlib/test/tx/transaction_test.dart b/coinlib/test/tx/transaction_test.dart index 5616c9c..e49b76e 100644 --- a/coinlib/test/tx/transaction_test.dart +++ b/coinlib/test/tx/transaction_test.dart @@ -76,7 +76,25 @@ void main() { test("tx too large", () { - final nonScriptSize = 4 + 1 + 32 + 4 + 5 + 4 + 1 + 4; + final nonScriptSize + = 4 // Version + + 1 // nIn + + 36 // Prevout + + 5 // Script varint + + 4 // Sequence + + 1 // nOut + + 4; // Locktime + + final witnessNonScriptSize + = 4 // Version + + 2 // Marker and flag + + 1 // nIn + + 36 // Prevout + + 1 // Empty script + + 4 // Sequence + + 1 // nOut + + 6 // Witness varints + + 4; // Locktime Uint8List dataOfSize(int size) { @@ -107,14 +125,30 @@ void main() { outputs: [], ); + Transaction witnessTxOfSize(int size) => Transaction( + inputs: [ + WitnessInput( + prevOut: examplePrevOut, + witness: [Uint8List(size-witnessNonScriptSize)], + ), + ], + outputs: [], + ); + + expect(Transaction.fromBytes(dataOfSize(1000000)).size, 1000000); expect(txOfSize(1000000).size, 1000000); + expect(witnessTxOfSize(1000000).size, 1000000); expect( () => Transaction.fromBytes(dataOfSize(1000001)), throwsA(isA()), ); expect(() => txOfSize(1000001), throwsA(isA())); + expect( + () => witnessTxOfSize(1000001), + throwsA(isA()), + ); });