Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set reserved byte correctly #818

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f93266b
wip: write data length to first compact share
rootulp Sep 23, 2022
e9848f2
refactor: extract NumberOfBytesVarint
rootulp Sep 26, 2022
6f879fe
fix: TestCompactShareWriter
rootulp Sep 26, 2022
a15cff4
rename: txLen to unitLen. improve docs
rootulp Sep 26, 2022
1ce5bad
fix: TestCount
rootulp Sep 26, 2022
b287b3f
feat: introduce CompactStartShareContentSize
rootulp Sep 26, 2022
55dd412
fix: all tests cases of TestTxSharePosition but one
rootulp Sep 26, 2022
11027e9
fix: joinByteSlice impl
rootulp Sep 26, 2022
4a97567
Merge branch 'rp/fix-proof-test' into rp/data-length-prefix
rootulp Sep 26, 2022
d88a0c1
bug: some tests fail b/c last byte of tx is not included in tx range
rootulp Sep 26, 2022
d9c507f
rename: ContinuationCompactShareContentSize
rootulp Sep 26, 2022
14d8ccd
Merge branch 'main' into rp/data-length-prefix
rootulp Sep 26, 2022
bbcfd16
chore: more test cases for TestTxShareIndex
rootulp Sep 27, 2022
1229433
discovered bug in compact share splitting
rootulp Sep 27, 2022
753779d
reorder consts to use order in which they are encoded
rootulp Sep 27, 2022
676cee1
remove duplicate unnecessary test
rootulp Sep 27, 2022
1393238
implement failing test for SplitTxs
rootulp Sep 27, 2022
b6a9966
Merge branch 'main' into rp/data-length-prefix
rootulp Sep 28, 2022
ac1ac10
pass TestSplitTxs with one transaction
rootulp Sep 28, 2022
92202a5
add test case for two transactions
rootulp Sep 28, 2022
e35f613
add test names
rootulp Sep 28, 2022
3bc2389
replace hard-coded padding with bytes.Repeat
rootulp Sep 28, 2022
181c118
replace hard-coded padding with bytes.Repeat for test case 2
rootulp Sep 28, 2022
3be79dc
use bytes.Repeat for third test case
rootulp Sep 28, 2022
ea1f7ce
reserve byte should be non-zero
rootulp Sep 28, 2022
28e4606
Merge branch 'main' into rp/data-length-prefix
rootulp Sep 28, 2022
7e86c15
improve test name
rootulp Sep 28, 2022
72713a3
implement failing fourth test case
rootulp Sep 28, 2022
d6c0c9e
fix: test case four
rootulp Sep 28, 2022
9f44876
uncomment proof tests
rootulp Sep 28, 2022
1452acf
remove unused helper, improve comments
rootulp Sep 28, 2022
4f38d3e
use css.isEmpty()
rootulp Sep 28, 2022
4fd8493
remove debug logging
rootulp Sep 28, 2022
5b5d535
spin out txLen rename
rootulp Sep 28, 2022
8361113
revert namespace rename
rootulp Sep 28, 2022
fca6cd5
chore: move `zeroPadIfNecessary` to utils (#811)
rootulp Sep 29, 2022
537d091
Rename `InfoReservedByte` to `InfoByte` (#805)
rootulp Sep 29, 2022
c650400
test: transaction splitting (#813)
rootulp Sep 29, 2022
c02ef12
chore: rename txLen to unitLen (#814)
rootulp Sep 29, 2022
c952795
Merge branch 'main' into rp/data-length-prefix
rootulp Sep 29, 2022
2ee86a4
feat!: stop forcing reserved byte to zero
rootulp Sep 29, 2022
dbe60c7
fix: reserved byte for existing test cases
rootulp Sep 29, 2022
f781496
test: add test case for reserved byte that is not 14
rootulp Sep 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions pkg/prove/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prove

import (
"errors"
"fmt"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/shares"
Expand Down Expand Up @@ -97,20 +98,39 @@ func txSharePosition(txs types.Txs, txIndex uint64) (startSharePos, endSharePos
return startSharePos, endSharePos, errors.New("transaction index is greater than the number of txs")
}

totalLen := 0
prevTxTotalLen := 0
for i := uint64(0); i < txIndex; i++ {
txLen := len(txs[i])
totalLen += (shares.DelimLen(uint64(txLen)) + txLen)
prevTxTotalLen += (shares.DelimLen(uint64(txLen)) + txLen)
}

txLen := len(txs[txIndex])

startSharePos = uint64((totalLen) / appconsts.ContinuationCompactShareContentSize)
endSharePos = uint64((totalLen + txLen + shares.DelimLen(uint64(txLen))) / appconsts.ContinuationCompactShareContentSize)
currentTxLen := len(txs[txIndex])
currentTxTotalLen := shares.DelimLen(uint64(currentTxLen)) + currentTxLen
endOfCurrentTxLen := prevTxTotalLen + currentTxTotalLen

startSharePos = txShareIndex(prevTxTotalLen)
endSharePos = txShareIndex(endOfCurrentTxLen)
fmt.Printf("prevTxTotalLen: %d, endOfCurrentTxLen: %d, startSharePos: %d, endSharePos %d, currentTxTotalLen %d\n", prevTxTotalLen, endOfCurrentTxLen, startSharePos, endSharePos, currentTxTotalLen)
return startSharePos, endSharePos, nil
}

// txShareIndex returns the index of the compact share that would contain
// transactions with totalTxLen
func txShareIndex(totalTxLen int) (index uint64) {
if totalTxLen <= appconsts.FirstCompactShareContentSize {
return 0
}

index++
totalTxLen -= appconsts.FirstCompactShareContentSize

for totalTxLen > appconsts.ContinuationCompactShareContentSize {
index++
totalTxLen -= appconsts.ContinuationCompactShareContentSize
}
return index
}

// genRowShares progessively generates data square rows from block data
func genRowShares(codec rsmt2d.Codec, data types.Data, startRow, endRow uint64) ([][][]byte, error) {
if endRow > data.OriginalSquareSize {
Expand Down
47 changes: 45 additions & 2 deletions pkg/prove/proof_test.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/shares/compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestFuzz_processCompactShares(t *testing.T) {

func Test_processCompactShares(t *testing.T) {
// exactTxShareSize is the length of tx that will fit exactly into a single
// share, accounting for namespace id and the length delimiter prepended to
// share, accounting for the tx length delimiter prepended to
// each tx. Note that the length delimiter can be 1 to 10 bytes (varint) but
// this test assumes it is 1 byte.
const exactTxShareSize = appconsts.ContinuationCompactShareContentSize - 1
Expand All @@ -81,7 +81,7 @@ func Test_processCompactShares(t *testing.T) {
{"single big tx", appconsts.ContinuationCompactShareContentSize * 4, 1},
{"many big txs", appconsts.ContinuationCompactShareContentSize * 4, 10},
{"single exact size tx", exactTxShareSize, 1},
{"many exact size txs", exactTxShareSize, 10},
{"many exact size txs", exactTxShareSize, 100},
}

for _, tc := range tests {
Expand Down
5 changes: 3 additions & 2 deletions pkg/shares/parse_compact_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

// parseCompactShares takes raw shares and extracts out transactions,
// intermediate state roots, or evidence. The returned [][]byte do not have
// namespaces, info bytes, or length delimiters and are ready to be unmarshalled
// namespaces, info bytes, data length delimiter, or unit length
// delimiters and are ready to be unmarshalled
func parseCompactShares(shares [][]byte) (data [][]byte, err error) {
if len(shares) == 0 {
return nil, nil
Expand Down Expand Up @@ -44,7 +45,7 @@ func (ss *shareStack) resolve() ([][]byte, error) {
if !infoByte.IsMessageStart() {
return nil, errors.New("first share is not a message start")
}
err = ss.peel(ss.shares[0][appconsts.NamespaceSize+appconsts.ShareInfoBytes+appconsts.CompactShareReservedBytes:], true)
err = ss.peel(ss.shares[0][appconsts.NamespaceSize+appconsts.ShareInfoBytes+appconsts.FirstCompactShareDataLengthBytes+appconsts.CompactShareReservedBytes:], true)
return ss.data, err
}

Expand Down
67 changes: 46 additions & 21 deletions pkg/shares/share_splitting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func TestSplitTxs(t *testing.T) {
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
0x0, // reserved byte
0x1, // info byte
0x2, 0x0, 0x0, 0x0, // 1 byte (unit) + 1 byte (unit length) = 2 bytes message length
14, // reserved byte
0x1, // unit length of first transaction
0xa, // data of first transaction
}, bytes.Repeat([]byte{0}, 244)...), // padding
}, bytes.Repeat([]byte{0}, 240)...), // padding
},
},
{
Expand All @@ -39,25 +40,27 @@ func TestSplitTxs(t *testing.T) {
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
0x0, // reserved byte
0x1, // info byte
0x4, 0x0, 0x0, 0x0, // 2 bytes (first transaction) + 2 bytes (second transaction) = 4 bytes message length
14, // 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
}, bytes.Repeat([]byte{0}, 238)...), // padding
},
},
{
name: "one large tx that spans two shares",
txs: coretypes.Txs{bytes.Repeat([]byte{0xC}, 245)},
txs: coretypes.Txs{bytes.Repeat([]byte{0xC}, 241)},
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
0x1, // info byte
243, 1, 0, 0, // 241 (unit) + 2 (unit length) = 243 message length
14, // reserved byte
241, 1, // unit length of first transaction is 241
}, bytes.Repeat([]byte{0xc}, 240)...), // data of first transaction
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x0, // info byte
Expand All @@ -68,31 +71,53 @@ func TestSplitTxs(t *testing.T) {
},
{
name: "one small tx then one large tx that spans two shares",
txs: coretypes.Txs{coretypes.Tx{0xd}, bytes.Repeat([]byte{0xe}, 243)},
txs: coretypes.Txs{coretypes.Tx{0xd}, bytes.Repeat([]byte{0xe}, 241)},
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
0x1, // info byte
245, 1, 0, 0, // 2 bytes (first transaction) + 243 bytes (second transaction) = 245 bytes message length
14, // reserved byte
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
241, 1, // unit length of second transaction is 241
}, bytes.Repeat([]byte{0xe}, 238)...), // data of first transaction
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x0, // info byte
0x0, // reserved byte
0xe, 0xe, 0xe, // continuation data of second transaction
}, bytes.Repeat([]byte{0x0}, 243)...), // padding
},
},
{
name: "one large tx that spans two shares then one small tx",
txs: coretypes.Txs{bytes.Repeat([]byte{0xe}, 241), coretypes.Tx{0xd}},
want: [][]uint8{
append([]uint8{
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, // namespace id
0x1, // info byte
245, 1, 0, 0, // 243 bytes (first transaction) + 2 bytes (second transaction) = 245 bytes message length
14, // reserved byte
241, 1, // unit length of first transaction is 241
}, bytes.Repeat([]byte{0xe}, 240)...), // 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
11, // reserved byte
0xe, // continuation data of first transaction
1, // unit length of second transaction
0xd, // data of second transaction
}, bytes.Repeat([]byte{0x0}, 243)...), // 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)
t.Errorf("SplitTxs()\n got %#v\n want %#v", got, tt.want)
}
})
}
}
} //
Loading