diff --git a/silkaj/tx.py b/silkaj/tx.py index edbe3b1674e1511d2d7b4e771578bb7e8dcfb869..061f58357eee78e6d877f32c8d414b07c30becae 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -44,6 +44,17 @@ from duniterpy.documents.transaction import OutputSource, Unlock, SIGParameter MAX_COMMENT_LENGTH = 255 +# max size for tx doc is 100 lines. Formula for accepted field numbers is : (2 * IU + 2 * IS + O) <= ( MAX_LINES - FIX_LINES) +# with IU = inputs/unlocks ; IS = Issuers/Signatures ; O = Outouts. +MAX_LINES_IN_TX_DOC = 100 +# 2 lines are necessary, and we block 1 more for the comment +FIX_LINES = 3 +# assuming there is only 1 issuer and 2 outputs, max inputs is 46 +MAX_INPUTS_PER_TX = 46 +# assuming there is 1 issuer and 1 input, max outputs is 93. +MAX_OUTPUTS = 93 + + @command("tx", help="Send transaction") @option( "amounts", @@ -339,6 +350,17 @@ async def handle_intermediaries_transactions( break +def max_inputs_number(outputs_number, issuers_number): + """ + returns the maximum number of inputs. + This function does not take care of backchange line. + formula is IU <= (MAX_LINES - FIX_LINES - O - 2*IS)/2 + """ + return int( + (MAX_LINES_IN_TX_DOC - FIX_LINES - (2 * issuers_number) - outputs_number) / 2 + ) + + async def generate_and_send_transaction( key, issuers, diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index bcab87b3ec67a5842d7a19125ccf0004c9c0746f..5ab9d2ea36e183ae05e4f3dbd2e7f8b6be31a8fa 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -1471,3 +1471,21 @@ def test_generate_output(listoutput, unitbase, rest, recipient_address, expected assert len(expected) == len(listoutput) for e, o in zip(expected, listoutput): assert e == o + + +# test max_inputs_number +@pytest.mark.parametrize( + "outputs_number, issuers_number, expected", + [ + (1, 1, 47), + (2, 1, 46), + (93, 1, 1), + ], +) +def test_max_inputs_number(outputs_number, issuers_number, expected): + """ + returns the maximum number of inputs. + This function does not take care of backchange line. + formula is IU <= (MAX_LINES - FIX_LINES - O - 2*IS)/2 + """ + assert tx.max_inputs_number(outputs_number, issuers_number) == expected