From 42b3b54e3c01047ece16fc9e29e833dfd707f701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20J=C3=A4mthagen?= Date: Mon, 9 Jan 2017 14:58:58 +0100 Subject: [PATCH] lnwallet: allow maximum state size to be used + tests Add tests to assert maximum state can be used. Also test that more than one input in the commitment transaction will fail and that having state number larger than maxStateHint will fail. --- lnwallet/script_utils.go | 2 +- lnwallet/script_utils_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index 8aea1d6c..2065411b 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -784,7 +784,7 @@ func SetStateNumHint(commitTx *wire.MsgTx, stateNum uint64, // With the current schema we are only able able to encode state num // hints up to 2^48. Therefore if the passed height is greater than our // state hint ceiling, then exit early. - if stateNum >= maxStateHint { + if stateNum > maxStateHint { return fmt.Errorf("unable to encode state, %v is greater "+ "state num that max of %v", stateNum, maxStateHint) } diff --git a/lnwallet/script_utils_test.go b/lnwallet/script_utils_test.go index cc402916..62f2da7e 100644 --- a/lnwallet/script_utils_test.go +++ b/lnwallet/script_utils_test.go @@ -583,5 +583,27 @@ func TestCommitTxStateHint(t *testing.T) { t.Fatalf("state number mismatched, expected %v, got %v", stateNum, extractedStateNum) } + + //Test from maximum allowed state + stateNum = uint64(maxStateHint - i) + err = SetStateNumHint(commitTx, stateNum, obsfucator) + if err != nil { + t.Fatalf("unable to set state num %v: %v", i, err) + } + + extractedStateNum = GetStateNumHint(commitTx, obsfucator) + if extractedStateNum != stateNum { + t.Fatalf("state number mismatched, expected %v, got %v", + stateNum, extractedStateNum) + } + } + + if err := SetStateNumHint(commitTx, maxStateHint+1, obsfucator); err == nil { + t.Fatalf("state number should not exceed %X", maxStateHint) + } + + commitTx.AddTxIn(&wire.TxIn{}) + if err := SetStateNumHint(commitTx, 0, obsfucator); err == nil { + t.Fatalf("more than one input in commit transaction should not be valid") } }