Browse Source

add test for preserving order

pull/8094/head
William Banfield 3 years ago
parent
commit
11edd067e7
No known key found for this signature in database GPG Key ID: EFAD3442BF29E3AC
2 changed files with 41 additions and 2 deletions
  1. +3
    -2
      types/tx.go
  2. +38
    -0
      types/tx_test.go

+ 3
- 2
types/tx.go View File

@ -243,8 +243,9 @@ func sortedCopy(txs Txs) Txs {
// Both lists must be sorted. // Both lists must be sorted.
func containsAnyTxs(a, b []Tx) (int, bool) { func containsAnyTxs(a, b []Tx) (int, bool) {
aix, bix := 0, 0 aix, bix := 0, 0
nextA:
for ; aix < len(a); aix++ { for ; aix < len(a); aix++ {
LOOP:
for bix < len(b) { for bix < len(b) {
switch bytes.Compare(b[bix], a[aix]) { switch bytes.Compare(b[bix], a[aix]) {
case 0: case 0:
@ -259,7 +260,7 @@ func containsAnyTxs(a, b []Tx) (int, bool) {
return -1, false return -1, false
} }
case 1: case 1:
break LOOP
continue nextA
} }
} }
} }


+ 38
- 0
types/tx_test.go View File

@ -158,4 +158,42 @@ func TestValidateTxRecordSet(t *testing.T) {
err := txrSet.Validate(100, []Tx{}) err := txrSet.Validate(100, []Tx{})
require.Error(t, err) require.Error(t, err)
}) })
t.Run("TxRecordSet preserves order", func(t *testing.T) {
trs := []*abci.TxRecord{
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{100}),
},
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{99}),
},
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{55}),
},
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{12}),
},
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{66}),
},
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{9}),
},
{
Action: abci.TxRecord_ADDED,
Tx: Tx([]byte{17}),
},
}
txrSet := NewTxRecordSet(trs)
err := txrSet.Validate(100, []Tx{})
require.NoError(t, err)
for i, tx := range txrSet.IncludedTxs() {
require.Equal(t, Tx(trs[i].Tx), tx)
}
})
} }

Loading…
Cancel
Save