@@ -63,5 +63,56 @@ BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
6363 BOOST_CHECK_EQUAL (fee, check_tx (fee + 123 ));
6464}
6565
66+ static void TestFillInputToWeight (int64_t additional_weight, std::vector<int64_t > expected_stack_sizes)
67+ {
68+ static const int64_t EMPTY_INPUT_WEIGHT = GetTransactionInputWeight (CTxIn ());
69+
70+ CTxIn input;
71+ int64_t target_weight = EMPTY_INPUT_WEIGHT + additional_weight;
72+ BOOST_CHECK (FillInputToWeight (input, target_weight));
73+ BOOST_CHECK_EQUAL (GetTransactionInputWeight (input), target_weight);
74+ BOOST_CHECK_EQUAL (input.scriptWitness .stack .size (), expected_stack_sizes.size ());
75+ for (unsigned int i = 0 ; i < expected_stack_sizes.size (); ++i) {
76+ BOOST_CHECK_EQUAL (input.scriptWitness .stack [i].size (), expected_stack_sizes[i]);
77+ }
78+ }
79+
80+ BOOST_FIXTURE_TEST_CASE (FillInputToWeightTest, BasicTestingSetup)
81+ {
82+ {
83+ // Less than or equal minimum of 165 should not add any witness data
84+ CTxIn input;
85+ BOOST_CHECK (!FillInputToWeight (input, -1 ));
86+ BOOST_CHECK_EQUAL (GetTransactionInputWeight (input), 165 );
87+ BOOST_CHECK_EQUAL (input.scriptWitness .stack .size (), 0 );
88+ BOOST_CHECK (!FillInputToWeight (input, 0 ));
89+ BOOST_CHECK_EQUAL (GetTransactionInputWeight (input), 165 );
90+ BOOST_CHECK_EQUAL (input.scriptWitness .stack .size (), 0 );
91+ BOOST_CHECK (!FillInputToWeight (input, 164 ));
92+ BOOST_CHECK_EQUAL (GetTransactionInputWeight (input), 165 );
93+ BOOST_CHECK_EQUAL (input.scriptWitness .stack .size (), 0 );
94+ BOOST_CHECK (FillInputToWeight (input, 165 ));
95+ BOOST_CHECK_EQUAL (GetTransactionInputWeight (input), 165 );
96+ BOOST_CHECK_EQUAL (input.scriptWitness .stack .size (), 0 );
97+ }
98+
99+ // Make sure we can add at least one weight
100+ TestFillInputToWeight (1 , {0 });
101+
102+ // 1 byte compact size uint boundary
103+ TestFillInputToWeight (252 , {251 });
104+ TestFillInputToWeight (253 , {83 , 168 });
105+ TestFillInputToWeight (262 , {86 , 174 });
106+ TestFillInputToWeight (263 , {260 });
107+
108+ // 3 byte compact size uint boundary
109+ TestFillInputToWeight (65535 , {65532 });
110+ TestFillInputToWeight (65536 , {21842 , 43688 });
111+ TestFillInputToWeight (65545 , {21845 , 43694 });
112+ TestFillInputToWeight (65546 , {65541 });
113+
114+ // Note: We don't test the next boundary because of memory allocation constraints.
115+ }
116+
66117BOOST_AUTO_TEST_SUITE_END ()
67118} // namespace wallet
0 commit comments