2016-08-08 19:38:16 +02:00
|
|
|
// Copyright (c) 2015-2016 The btcsuite developers
|
2015-10-22 22:45:13 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-03-22 02:22:08 +01:00
|
|
|
// Provides functions for sorting tx inputs and outputs according to BIP 69
|
|
|
|
// (https://github.com/bitcoin/bips/blob/master/bip-0069.mediawiki)
|
2015-10-23 09:55:56 +02:00
|
|
|
|
2015-10-23 11:09:09 +02:00
|
|
|
package txsort
|
2015-10-22 22:45:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sort"
|
|
|
|
|
2021-09-10 22:30:39 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
|
|
|
"github.com/lbryio/lbcd/wire"
|
2015-10-22 22:45:13 +02:00
|
|
|
)
|
|
|
|
|
2015-10-23 11:42:59 +02:00
|
|
|
// InPlaceSort modifies the passed transaction inputs and outputs to be sorted
|
2016-03-22 02:22:08 +01:00
|
|
|
// based on BIP 69.
|
2015-10-23 11:42:59 +02:00
|
|
|
//
|
|
|
|
// WARNING: This function must NOT be called with published transactions since
|
|
|
|
// it will mutate the transaction if it's not already sorted. This can cause
|
|
|
|
// issues if you mutate a tx in a block, for example, which would invalidate the
|
2021-09-10 22:30:39 +02:00
|
|
|
// block. It could also cause cached hashes, such as in a lbcutil.Tx to become
|
2015-10-23 11:42:59 +02:00
|
|
|
// invalidated.
|
|
|
|
//
|
|
|
|
// The function should only be used if the caller is creating the transaction or
|
|
|
|
// is otherwise 100% positive mutating will not cause adverse affects due to
|
|
|
|
// other dependencies.
|
|
|
|
func InPlaceSort(tx *wire.MsgTx) {
|
|
|
|
sort.Sort(sortableInputSlice(tx.TxIn))
|
|
|
|
sort.Sort(sortableOutputSlice(tx.TxOut))
|
|
|
|
}
|
|
|
|
|
2015-10-23 09:55:56 +02:00
|
|
|
// Sort returns a new transaction with the inputs and outputs sorted based on
|
2016-03-22 02:22:08 +01:00
|
|
|
// BIP 69. The passed transaction is not modified and the new transaction
|
2015-10-23 09:55:56 +02:00
|
|
|
// might have a different hash if any sorting was done.
|
2015-10-23 11:09:09 +02:00
|
|
|
func Sort(tx *wire.MsgTx) *wire.MsgTx {
|
2015-10-22 22:45:13 +02:00
|
|
|
txCopy := tx.Copy()
|
|
|
|
sort.Sort(sortableInputSlice(txCopy.TxIn))
|
|
|
|
sort.Sort(sortableOutputSlice(txCopy.TxOut))
|
|
|
|
return txCopy
|
|
|
|
}
|
|
|
|
|
2015-10-23 09:55:56 +02:00
|
|
|
// IsSorted checks whether tx has inputs and outputs sorted according to BIP
|
2016-03-22 02:22:08 +01:00
|
|
|
// 69.
|
2015-10-23 11:09:09 +02:00
|
|
|
func IsSorted(tx *wire.MsgTx) bool {
|
2015-10-22 22:45:13 +02:00
|
|
|
if !sort.IsSorted(sortableInputSlice(tx.TxIn)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !sort.IsSorted(sortableOutputSlice(tx.TxOut)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type sortableInputSlice []*wire.TxIn
|
|
|
|
type sortableOutputSlice []*wire.TxOut
|
|
|
|
|
2015-10-23 09:55:56 +02:00
|
|
|
// For SortableInputSlice and SortableOutputSlice, three functions are needed
|
2015-10-22 22:45:13 +02:00
|
|
|
// to make it sortable with sort.Sort() -- Len, Less, and Swap
|
2016-03-22 02:22:08 +01:00
|
|
|
// Len and Swap are trivial. Less is BIP 69 specific.
|
2015-10-23 09:55:56 +02:00
|
|
|
func (s sortableInputSlice) Len() int { return len(s) }
|
|
|
|
func (s sortableOutputSlice) Len() int { return len(s) }
|
|
|
|
func (s sortableOutputSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
func (s sortableInputSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
2015-10-22 22:45:13 +02:00
|
|
|
|
|
|
|
// Input comparison function.
|
2015-10-23 09:55:56 +02:00
|
|
|
// First sort based on input hash (reversed / rpc-style), then index.
|
|
|
|
func (s sortableInputSlice) Less(i, j int) bool {
|
|
|
|
// Input hashes are the same, so compare the index.
|
|
|
|
ihash := s[i].PreviousOutPoint.Hash
|
|
|
|
jhash := s[j].PreviousOutPoint.Hash
|
|
|
|
if ihash == jhash {
|
|
|
|
return s[i].PreviousOutPoint.Index < s[j].PreviousOutPoint.Index
|
2015-10-22 22:45:13 +02:00
|
|
|
}
|
2015-10-23 09:55:56 +02:00
|
|
|
|
|
|
|
// At this point, the hashes are not equal, so reverse them to
|
|
|
|
// big-endian and return the result of the comparison.
|
2016-08-08 19:38:16 +02:00
|
|
|
const hashSize = chainhash.HashSize
|
2015-10-23 09:55:56 +02:00
|
|
|
for b := 0; b < hashSize/2; b++ {
|
|
|
|
ihash[b], ihash[hashSize-1-b] = ihash[hashSize-1-b], ihash[b]
|
|
|
|
jhash[b], jhash[hashSize-1-b] = jhash[hashSize-1-b], jhash[b]
|
2015-10-22 22:45:13 +02:00
|
|
|
}
|
2015-10-23 09:55:56 +02:00
|
|
|
return bytes.Compare(ihash[:], jhash[:]) == -1
|
2015-10-22 22:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output comparison function.
|
2015-10-23 09:55:56 +02:00
|
|
|
// First sort based on amount (smallest first), then PkScript.
|
|
|
|
func (s sortableOutputSlice) Less(i, j int) bool {
|
|
|
|
if s[i].Value == s[j].Value {
|
|
|
|
return bytes.Compare(s[i].PkScript, s[j].PkScript) < 0
|
2015-10-22 22:45:13 +02:00
|
|
|
}
|
2015-10-23 09:55:56 +02:00
|
|
|
return s[i].Value < s[j].Value
|
2015-10-22 22:45:13 +02:00
|
|
|
}
|