Skip to content

Commit

Permalink
test: transaction splitting (#813)
Browse files Browse the repository at this point in the history
Define unit tests for transaction splitting. These should be helpful
when we tackle celestiaorg/celestia-app#802
  • Loading branch information
rootulp authored Sep 29, 2022
1 parent 959d42d commit ee4ab3b
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions pkg/shares/share_splitting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package shares

import (
"bytes"
"reflect"
"testing"

coretypes "github.com/tendermint/tendermint/types"
)

func TestSplitTxs(t *testing.T) {
type testCase struct {
name string
txs coretypes.Txs
want [][]byte
}
testCases := []testCase{
{
name: "empty txs",
txs: coretypes.Txs{},
want: [][]byte{},
},
{
name: "one small tx",
txs: coretypes.Txs{coretypes.Tx{0xa}},
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
0x0, // reserved byte
0x1, // unit length of first transaction
0xa, // data of first transaction
}, bytes.Repeat([]byte{0}, 244)...), // padding
},
},
{
name: "two small txs",
txs: coretypes.Txs{coretypes.Tx{0xa}, coretypes.Tx{0xb}},
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
0x0, // reserved byte
0x1, // unit length of first transaction
0xa, // data of first transaction
0x1, // unit length of second transaction
0xb, // data of second transaction
}, bytes.Repeat([]byte{0}, 242)...), // padding
},
},
{
name: "one large tx that spans two shares",
txs: coretypes.Txs{bytes.Repeat([]byte{0xC}, 245)},
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
0x0, // BUG reserved byte should be non-zero see https://github.com/celestiaorg/celestia-app/issues/802
245, 1, // unit length of first transaction is 245
}, bytes.Repeat([]byte{0xc}, 244)...), // data of first transaction
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x0, // info byte
0x0, // reserved byte
0xc, // continuation data of first transaction
}, bytes.Repeat([]byte{0x0}, 245)...), // padding
},
},
{
name: "one small tx then one large tx that spans two shares",
txs: coretypes.Txs{coretypes.Tx{0xd}, bytes.Repeat([]byte{0xe}, 243)},
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
0x0, // BUG reserved byte should be non-zero see https://github.com/celestiaorg/celestia-app/issues/802
1, // unit length of first transaction
0xd, // data of first transaction
243, 1, // unit length of second transaction is 243
}, bytes.Repeat([]byte{0xe}, 242)...), // data of first transaction
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x0, // info byte
0x0, // reserved byte
0xe, // continuation data of second transaction
}, bytes.Repeat([]byte{0x0}, 245)...), // padding
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
got := SplitTxs(tt.txs)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SplitTxs(%#v) got %#v, want %#v", tt.txs, got, tt.want)
}
})
}
}

0 comments on commit ee4ab3b

Please sign in to comment.