mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [NOD-64] Intermediate commit: need check tests on master * [NOD-64] Commit before changing subnetwork IDS to new values * [NOD-64] Fixed tests after changing subnetworks IDs constants * [NOD-64] Extract duplicate code into functions * [NOD-64] Renamed IsAllSubnetworks ==> IncludeAllSubnetworks
125 lines
3.9 KiB
Go
125 lines
3.9 KiB
Go
// Copyright (c) 2015-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txsort_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/daglabs/btcd/util/txsort"
|
|
"github.com/daglabs/btcd/wire"
|
|
)
|
|
|
|
// TestSort ensures the transaction sorting works according to the BIP.
|
|
func TestSort(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
hexFile string
|
|
isSorted bool
|
|
unsortedHash string
|
|
sortedHash string
|
|
}{
|
|
{
|
|
name: "first test case from BIP 69 - sorts inputs only, based on hash",
|
|
hexFile: "bip69-1.hex",
|
|
isSorted: false,
|
|
unsortedHash: "942436554228bcff3af7825cc733f58df686166943da87347f1dfd77ace0f8cf",
|
|
sortedHash: "a730bfdb5efc611dbf6b1cf10b16477f60be647ac95009d9b655f59f09fbe7cb",
|
|
},
|
|
{
|
|
name: "second test case from BIP 69 - already sorted",
|
|
hexFile: "bip69-2.hex",
|
|
isSorted: true,
|
|
unsortedHash: "81812ee14ff0e57582eb1520630812838d8fbf2a278517b7ff89e82463f6a558",
|
|
sortedHash: "81812ee14ff0e57582eb1520630812838d8fbf2a278517b7ff89e82463f6a558",
|
|
},
|
|
{
|
|
name: "block 100001 tx[1] - sorts outputs only, based on amount",
|
|
hexFile: "bip69-3.hex",
|
|
isSorted: false,
|
|
unsortedHash: "73d990869a7e3095ba06cc2c6718b4f210f72f6099643d2c6fe4dd93d32a49cf",
|
|
sortedHash: "5c2b807e2fff4dadea373e2f2a41a6d4808675bc478a3448f76bbbeec6fae310",
|
|
},
|
|
{
|
|
name: "block 100001 tx[2] - sorts both inputs and outputs",
|
|
hexFile: "bip69-4.hex",
|
|
isSorted: false,
|
|
unsortedHash: "cefbcc2b6513392c9296a866e8ca9e3fc832355b454c6897aedbf2159f958fd3",
|
|
sortedHash: "4e3573d75a3aa02aeb3508f8203c52f0154d19ecb0cb0ef2e00ec3fa803baa5d",
|
|
},
|
|
{
|
|
name: "block 100998 tx[6] - sorts outputs only, based on output script",
|
|
hexFile: "bip69-5.hex",
|
|
isSorted: false,
|
|
unsortedHash: "f79ed295b9d8a3c9813cc2b207d267753755503258bc9ac887f2d5495e8b6140",
|
|
sortedHash: "a90123db6e884f821d3228da70e7a810d4797d43379d40b71c8e214c7104c59a",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
// Load and deserialize the test transaction.
|
|
filePath := filepath.Join("testdata", test.hexFile)
|
|
txHexBytes, err := ioutil.ReadFile(filePath)
|
|
if err != nil {
|
|
t.Errorf("ReadFile (%s): failed to read test file: %v",
|
|
test.name, err)
|
|
continue
|
|
}
|
|
txBytes, err := hex.DecodeString(string(txHexBytes))
|
|
if err != nil {
|
|
t.Errorf("DecodeString (%s): failed to decode tx: %v",
|
|
test.name, err)
|
|
continue
|
|
}
|
|
var tx wire.MsgTx
|
|
err = tx.Deserialize(bytes.NewReader(txBytes))
|
|
if err != nil {
|
|
t.Errorf("Deserialize (%s): unexpected error %v",
|
|
test.name, err)
|
|
continue
|
|
}
|
|
|
|
// Ensure the sort order of the original transaction matches the
|
|
// expected value.
|
|
if got := txsort.IsSorted(&tx); got != test.isSorted {
|
|
t.Errorf("IsSorted (%s): sort does not match "+
|
|
"expected - got %v, want %v", test.name, got,
|
|
test.isSorted)
|
|
continue
|
|
}
|
|
|
|
// Sort the transaction and ensure the resulting hash is the
|
|
// expected value.
|
|
sortedTx := txsort.Sort(&tx)
|
|
if got := sortedTx.TxHash().String(); got != test.sortedHash {
|
|
t.Errorf("Sort (%s): sorted hash does not match "+
|
|
"expected - got %v, want %v", test.name, got,
|
|
test.sortedHash)
|
|
continue
|
|
}
|
|
|
|
// Ensure the original transaction is not modified.
|
|
if got := tx.TxHash().String(); got != test.unsortedHash {
|
|
t.Errorf("Sort (%s): unsorted hash does not match "+
|
|
"expected - got %v, want %v", test.name, got,
|
|
test.unsortedHash)
|
|
continue
|
|
}
|
|
|
|
// Now sort the transaction using the mutable version and ensure
|
|
// the resulting hash is the expected value.
|
|
txsort.InPlaceSort(&tx)
|
|
if got := tx.TxHash().String(); got != test.sortedHash {
|
|
t.Errorf("SortMutate (%s): sorted hash does not match "+
|
|
"expected - got %v, want %v", test.name, got,
|
|
test.sortedHash)
|
|
continue
|
|
}
|
|
}
|
|
}
|