forked from joemphilips/DotNetLightning
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TxOut.LexicographicCompare method & test
Ordering txouts by their byte representation doesn't give a valid lexicographic ordering due to (a) the value being represented in little-endian (BIP69 requires that txouts with smaller values appear first) and (b) F#'s comparision function on byte arrays always ordering smaller arrays first (eg. [| 1 |] < [| 0; 0 |] < [| 1; 1 |]), rather than ordering them in dictionary order (ie. [| 0; 0 |] < [| 1 |] < [| 1; 1 |]). This commit adds a TxOut.LexicographicCompare method which correctly implements BIP69's txout lexicographic ordering spec. This is then used for ordering outputs in the closing transaction. Backported from upstream PR: joemphilips#138
- Loading branch information
Showing
4 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/DotNetLightning.Core.Tests/TxOutLexicographicCompareTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module TxOutLexicographicCompareTests | ||
|
||
open NBitcoin | ||
open Expecto | ||
open DotNetLightning.Utils | ||
|
||
[<Tests>] | ||
let tests = | ||
let test (amount0: int) | ||
(amount1: int) | ||
(script0: array<byte>) | ||
(script1: array<byte>) | ||
(expected: int) = | ||
let txOut0 = TxOut(Money amount0, Script script0) | ||
let txOut1 = TxOut(Money amount1, Script script1) | ||
let result = TxOut.LexicographicCompare txOut0 txOut1 | ||
Expect.equal | ||
result | ||
expected | ||
(sprintf | ||
"unexpected comparison result: \ | ||
TxOut.LexicographicCompare(TxOut(%i, %A), TxOut(%i, %A))" | ||
amount0 | ||
script0 | ||
amount1 | ||
script1 | ||
) | ||
|
||
testList "TxOutLexicographicCompare tests" [ | ||
testCase "smaller amount, shorter pubkey, lower bytes values" <| fun _ -> | ||
test 1 2 [| 0uy |] [| 1uy; 1uy |] -1 | ||
testCase "smaller amount, shorter pubkey, equal bytes values" <| fun _ -> | ||
test 1 2 [| 0uy |] [| 0uy; 0uy |] -1 | ||
testCase "smaller amount, shorter pubkey, greater bytes values" <| fun _ -> | ||
test 1 2 [| 1uy |] [| 0uy; 0uy |] -1 | ||
testCase "smaller amount, equal lengths, lower bytes values" <| fun _ -> | ||
test 1 2 [| 0uy |] [| 1uy |] -1 | ||
testCase "smaller amount, equal lengths, equal bytes values" <| fun _ -> | ||
test 1 2 [| 0uy |] [| 0uy |] -1 | ||
testCase "smaller amount, equal lengths, greater bytes values" <| fun _ -> | ||
test 1 2 [| 1uy |] [| 0uy |] -1 | ||
testCase "smaller amount, longer pubkey, lower bytes values" <| fun _ -> | ||
test 1 2 [| 0uy; 0uy |] [| 1uy |] -1 | ||
testCase "smaller amount, longer pubkey, equal bytes values" <| fun _ -> | ||
test 1 2 [| 0uy; 0uy |] [| 0uy |] -1 | ||
testCase "smaller amount, longer pubkey, greater bytes values" <| fun _ -> | ||
test 1 2 [| 1uy; 1uy |] [| 0uy |] -1 | ||
|
||
testCase "same amount, shorter pubkey, lower bytes values" <| fun _ -> | ||
test 1 1 [| 0uy |] [| 1uy; 1uy |] -1 | ||
testCase "same amount, shorter pubkey, equal bytes values" <| fun _ -> | ||
test 1 1 [| 0uy |] [| 0uy; 0uy |] -1 | ||
testCase "same amount, shorter pubkey, greater bytes values" <| fun _ -> | ||
test 1 1 [| 1uy |] [| 0uy; 0uy |] 1 | ||
testCase "same amount, equal lengths, lower bytes values" <| fun _ -> | ||
test 1 1 [| 0uy |] [| 1uy |] -1 | ||
testCase "same amount, equal lengths, equal bytes values" <| fun _ -> | ||
test 1 1 [| 0uy |] [| 0uy |] 0 | ||
testCase "same amount, equal lengths, greater bytes values" <| fun _ -> | ||
test 1 1 [| 1uy |] [| 0uy |] 1 | ||
testCase "same amount, longer pubkey, lower bytes values" <| fun _ -> | ||
test 1 1 [| 0uy; 0uy |] [| 1uy |] -1 | ||
testCase "same amount, longer pubkey, equal bytes values" <| fun _ -> | ||
test 1 1 [| 0uy; 0uy |] [| 0uy |] 1 | ||
testCase "same amount, longer pubkey, greater bytes values" <| fun _ -> | ||
test 1 1 [| 1uy; 1uy |] [| 0uy |] 1 | ||
|
||
testCase "greater amount, shorter pubkey, lower bytes values" <| fun _ -> | ||
test 2 1 [| 0uy |] [| 1uy; 1uy |] 1 | ||
testCase "greater amount, shorter pubkey, equal bytes values" <| fun _ -> | ||
test 2 1 [| 0uy |] [| 0uy; 0uy |] 1 | ||
testCase "greater amount, shorter pubkey, greater bytes values" <| fun _ -> | ||
test 2 1 [| 1uy |] [| 0uy; 0uy |] 1 | ||
testCase "greater amount, equal lengths, lower bytes values" <| fun _ -> | ||
test 2 1 [| 0uy |] [| 1uy |] 1 | ||
testCase "greater amount, equal lengths, equal bytes values" <| fun _ -> | ||
test 2 1 [| 0uy |] [| 0uy |] 1 | ||
testCase "greater amount, equal lengths, greater bytes values" <| fun _ -> | ||
test 2 1 [| 1uy |] [| 0uy |] 1 | ||
testCase "greater amount, longer pubkey, lower bytes values" <| fun _ -> | ||
test 2 1 [| 0uy; 0uy |] [| 1uy |] 1 | ||
testCase "greater amount, longer pubkey, equal bytes values" <| fun _ -> | ||
test 2 1 [| 0uy; 0uy |] [| 0uy |] 1 | ||
testCase "greater amount, longer pubkey, greater bytes values" <| fun _ -> | ||
test 2 1 [| 1uy; 1uy |] [| 0uy |] 1 | ||
] |