diwakergupta / stacks-blockchain-tob-audit

GNU General Public License v3.0
0 stars 0 forks source link

Sorting comparison functions could be simplified #32

Open bradlarsen opened 3 years ago

bradlarsen commented 3 years ago

While auditing the code for other issues related to leader election and proof-of-transfer, we came across several locations where a complicated custom comparison function is used to sort. For example:

https://github.com/trailofbits/x-audit-blockstack-core/blob/6acfe2f61910db35f66d4f5b09d211f7fdf42fd3/src/burnchains/burnchain.rs#L195

In this case, since vtxindex() returns a u32, which has an Ord implementation, the sort operation could be simplified to the following:

accepted_ops.sort_by(|ref a, ref b| a.vtxindex().cmp(&b.vtxindex())); 

This is shorter and eliminates the possibility of a runtime panic from calling unwrap().

Other locations where this applies:

https://github.com/trailofbits/x-audit-blockstack-core/blob/6acfe2f61910db35f66d4f5b09d211f7fdf42fd3/src/burnchains/burnchain.rs#L616

https://github.com/trailofbits/x-audit-blockstack-core/blob/6acfe2f61910db35f66d4f5b09d211f7fdf42fd3/src/burnchains/burnchain.rs#L1659

https://github.com/trailofbits/x-audit-blockstack-core/blob/9774bd330fd0c46032352c86be803cffa7457cbe/src/main.rs#L393

https://github.com/trailofbits/x-audit-blockstack-core/blob/110c17a90bec3194662bd6e2d711b7ced0d8e0a1/src/burnchains/db.rs#L50

https://github.com/trailofbits/x-audit-blockstack-core/blob/9ec42e356a00db3895e00799146f60016820e6e7/src/chainstate/burn/db/processing.rs#L172

https://github.com/trailofbits/x-audit-blockstack-core/blob/0c6c1d9dd8e47e79643c1edea6dc95de404ed262/src/chainstate/stacks/db/blocks.rs#L1057