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

Spec compliant merge shares #261

Merged
merged 35 commits into from
Apr 6, 2021
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
494db15
start spec compliant share merging
evan-forbes Mar 31, 2021
0d15df4
refactor and finish unit testing
evan-forbes Apr 1, 2021
3f0d808
whoops
evan-forbes Apr 1, 2021
8535122
linter gods
evan-forbes Apr 1, 2021
8bae88b
fix initial changes and use constants
evan-forbes Apr 1, 2021
cbb5d76
use constant
evan-forbes Apr 1, 2021
b523831
more polish
evan-forbes Apr 1, 2021
f0f38a6
docs fix
evan-forbes Apr 1, 2021
014a00e
review feedback: docs and out of range panic protection
evan-forbes Apr 1, 2021
0896a3b
review feedback: add panic protection from empty input
evan-forbes Apr 1, 2021
96eafc7
use constant instead of recalculating `ShareSize`
evan-forbes Apr 1, 2021
c3d897a
don't redeclare existing var
evan-forbes Apr 1, 2021
28f9769
be more explicit with returned nil
evan-forbes Apr 1, 2021
9ff16f5
use constant instead of recalculating `ShareSize`
evan-forbes Apr 1, 2021
39ce26a
review feedback: use consistent capitalization
evan-forbes Apr 1, 2021
8794411
stop accepting reserved namespaces as normal messages
evan-forbes Apr 1, 2021
2b28c59
use a descriptive var name for message length
evan-forbes Apr 1, 2021
d118716
linter and comparison fix
evan-forbes Apr 1, 2021
3ae1f79
reorg tests, add test for parse delimiter, DataFromBlock and fix evid…
evan-forbes Apr 2, 2021
dfe2a07
catch error for linter
evan-forbes Apr 3, 2021
e9f3a2e
update test MakeShares to include length delimiters for the SHARE_RES…
evan-forbes Apr 3, 2021
221b0fb
minor iteration change
evan-forbes Apr 3, 2021
37316e8
refactor share splitting to fix bug
evan-forbes Apr 3, 2021
e3736d6
fix all bugs with third and final refactor
evan-forbes Apr 4, 2021
632bb37
Merge branch 'master' into evan/merge-shares
evan-forbes Apr 4, 2021
3ca2afa
fix conflict
evan-forbes Apr 4, 2021
0f930fd
revert unnecessary changes
evan-forbes Apr 4, 2021
91c3989
review feedback: better docs
evan-forbes Apr 5, 2021
043812d
reivew feedback: add comment for safeLen
evan-forbes Apr 5, 2021
76d7a4b
review feedback: remove unnecessay comments
evan-forbes Apr 5, 2021
1cb4030
review feedback: split up share merging and splitting into their own …
evan-forbes Apr 5, 2021
a88db3b
review feedback: more descriptive var names
evan-forbes Apr 5, 2021
2aad8fd
fix accidental change
evan-forbes Apr 5, 2021
887aa08
add some constant docs
evan-forbes Apr 5, 2021
40a57c1
spelling error
evan-forbes Apr 5, 2021
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
Prev Previous commit
Next Next commit
fix all bugs with third and final refactor
evan-forbes committed Apr 4, 2021

Verified

This commit was signed with the committer’s verified signature.
evan-forbes Evan Forbes
commit e3736d600e55c6aa4e4c8eef88e48e8451a0fdd5
95 changes: 51 additions & 44 deletions types/shares.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package types
import (
"bytes"
"encoding/binary"
"errors"

"github.com/gogo/protobuf/proto"
tmbytes "github.com/lazyledger/lazyledger-core/libs/bytes"
@@ -150,6 +151,7 @@ func mintShare(nid namespace.ID, rawTxs [][]byte) (NamespacedShare, [][]byte) {
// If we don't have room, fill remaining data in the share with as much
// of the tx as possible
remaining := TxShareSize - len(rawData)

rawData = append(rawData, tx[:remaining]...)

// remove the portion of the tx that was added in this share
@@ -359,60 +361,65 @@ func processContiguousShares(shares [][]byte) (txs [][]byte, err error) {
if len(shares) == 0 {
return nil, nil
}
share := shares[0][NamespaceSize+ShareReservedBytes:]
share, txLen, err := parseDelimiter(share)
if err != nil {
return nil, err
}

for i := 0; i < len(shares); i++ {
var newTxs [][]byte
newTxs, share, txLen, err = collectTxsFromShare(share, txLen)
if err != nil {
return nil, err
}

// add the collected txs to the output
txs = append(txs, newTxs...)

// if there is no next share, the work is done
// if not, then we know there are more shares to process
if len(shares) == i+1 {
break
}
ss := newShareStack(shares)
return ss.resolve()
}

nextShare := shares[i+1][NamespaceSize+ShareReservedBytes:]
// shareStack hold variables for peel
type shareStack struct {
shares [][]byte
txLen uint64
txs [][]byte
cursor int
}

// if there is no current share remaining, process the next share
if len(share) == 0 {
share, txLen, err = parseDelimiter(nextShare)
if err != nil {
break
}
continue
}
func newShareStack(shares [][]byte) *shareStack {
return &shareStack{shares: shares}
}

// create the next share by merging the next share with the remaining of
// the current share
share = append(share, nextShare...)
func (ss *shareStack) resolve() ([][]byte, error) {
if len(ss.shares) == 0 {
return nil, nil
}

return txs, err
err := ss.peel(ss.shares[0][NamespaceSize+ShareReservedBytes:], true)
return ss.txs, err
}

func collectTxsFromShare(share []byte, txLen uint64) (txs [][]byte, extra []byte, l uint64, err error) {
for uint64(len(share)) >= txLen {
tx := share[:txLen]
txs = append(txs, tx)

share = share[txLen:]
// peel recursively parses each chunk of data (either a transaction,
// intermediate state root, or evidence) and adds it to the underlying slice of data.
func (ss *shareStack) peel(share []byte, delimited bool) (err error) {
if delimited {
var txLen uint64
share, txLen, err = parseDelimiter(share)
if txLen == 0 || err != nil {
share = nil
break
if err != nil {
return err
}
if txLen == 0 {
return nil
}
ss.txLen = txLen
}
safeLen := len(share) - binary.MaxVarintLen64
if safeLen < 0 {
safeLen = 0
}
if int(ss.txLen) <= safeLen {
ss.txs = append(ss.txs, share[:ss.txLen])
share = share[ss.txLen:]
return ss.peel(share, true)
}
if len(ss.shares) > ss.cursor+1 {
ss.cursor++
share := append(share, ss.shares[ss.cursor][NamespaceSize+ShareReservedBytes:]...)
return ss.peel(share, false)
}
if ss.txLen <= uint64(len(share)) {
ss.txs = append(ss.txs, share[:ss.txLen])
share = share[ss.txLen:]
return ss.peel(share, true)
}
return txs, share, txLen, err
return errors.New("failure to parse block data: transaction length exceeded data length")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this can serve as a kind of blueprint for the pseudo-code that could end up in the spec.


// parseMessages iterates through raw shares and separates the contiguous chunks
37 changes: 35 additions & 2 deletions types/shares_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package types

import (
"bytes"
"context"
"fmt"
"math"
"math/rand"
@@ -239,9 +240,10 @@ func TestDataFromSquare(t *testing.T) {
tc.maxSize,
)

shares := data.ComputeShares().RawShares()
shares := data.ComputeShares()
rawShares := shares.RawShares()

eds, err := rsmt2d.ComputeExtendedDataSquare(shares, rsmt2d.RSGF8, rsmt2d.NewDefaultTree)
eds, err := rsmt2d.ComputeExtendedDataSquare(rawShares, rsmt2d.RSGF8, rsmt2d.NewDefaultTree)
if err != nil {
t.Error(err)
}
@@ -269,6 +271,21 @@ func TestDataFromSquare(t *testing.T) {
}
}

func TestFuzz_DataFromSquare(t *testing.T) {
t.Skip()
// run random shares through processContiguousShares for a minute
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
for {
select {
case <-ctx.Done():
return
default:
TestDataFromSquare(t)
}
}
}

func Test_processContiguousShares(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
@@ -291,6 +308,7 @@ func Test_processContiguousShares(t *testing.T) {
{"single exact size tx", exactTxShareSize, 1},
{"many exact size txs", exactTxShareSize, 10},
}

for _, tc := range tests {
tc := tc

@@ -330,6 +348,21 @@ func Test_processContiguousShares(t *testing.T) {
}
}

func TestFuzz_processContiguousShares(t *testing.T) {
t.Skip()
// run random shares through processContiguousShares for a minute
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
for {
select {
case <-ctx.Done():
return
default:
Test_processContiguousShares(t)
}
}
}

func Test_parseMsgShares(t *testing.T) {
// exactMsgShareSize is the length of message that will fit exactly into a single
// share, accounting for namespace id and the length delimiter prepended to