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
review feedback: docs and out of range panic protection
evan-forbes committed Apr 1, 2021

Verified

This commit was signed with the committer’s verified signature.
evan-forbes Evan Forbes
commit 014a00e0f44e3c7e8c2d9ed8073e1c866c0831e5
22 changes: 16 additions & 6 deletions types/shares.go
Original file line number Diff line number Diff line change
@@ -293,6 +293,8 @@ func parseIsrs(shares [][]byte) (IntermediateStateRoots, error) {

// parseEvd collects all evidence from the shares provided.
func parseEvd(shares [][]byte) (EvidenceData, error) {
// the raw data returned does not have length delimiters or namespaces and
// is ready to be unmarshaled
rawEvd, err := processContiguousShares(shares)
if err != nil {
return EvidenceData{}, err
@@ -332,7 +334,13 @@ func parseMsgs(shares [][]byte) (Messages, error) {
}, nil
}

// processContiguousShares takes raw shares and extracts out transactions,
// intermediate state roots, or evidence. The returned [][]byte do have
// namespaces or length delimiters and are ready to be unmarshalled
func processContiguousShares(shares [][]byte) (txs [][]byte, err error) {
if len(shares) == 0 {
return nil, err
}
share := shares[0][NamespaceSize+ShareReservedBytes:]
share, txLen, err := parseDelimiter(share)
if err != nil {
@@ -341,21 +349,22 @@ func processContiguousShares(shares [][]byte) (txs [][]byte, err error) {

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

txs = append(txs, newTxs...)

// if there is no next share
if len(shares) <= i+1 {
// 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
}

nextShare := shares[i+1][NamespaceSize+ShareReservedBytes:]

// if there is no current share, process the next share
// if there is no current share remaining, process the next share
if len(share) == 0 {
share, txLen, err = parseDelimiter(nextShare)
if err != nil {
@@ -364,14 +373,15 @@ func processContiguousShares(shares [][]byte) (txs [][]byte, err error) {
continue
}

// create the next share by merging the next share with the extra
// create the next share by merging the next share with the remaining of
// the current share
share = append(share, nextShare...)
}

return txs, err
}

func collectTxFromShare(share []byte, txLen uint64) (txs [][]byte, extra []byte, l uint64, err error) {
func collectTxsFromShare(share []byte, txLen uint64) (txs [][]byte, extra []byte, l uint64, err error) {
for uint64(len(share)) >= txLen {
tx := share[:txLen]
if len(tx) == 0 {
4 changes: 2 additions & 2 deletions types/shares_test.go
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ func Test_processContiguousShares(t *testing.T) {
{"single big tx", 1000, 1},
{"many big txs", 1000, 10},
{"single exact size tx", exactTxShareSize, 1},
{"many exact size txs", exactTxShareSize, 2},
{"many exact size txs", exactTxShareSize, 10},
}

for _, tc := range tests {
@@ -239,7 +239,7 @@ func Test_processContiguousShares(t *testing.T) {
}
})

// run the same tests using randomly sized txs with caps of tc.msgSize
// run the same tests using randomly sized txs with caps of tc.txSize
t.Run(fmt.Sprintf("%s randomly sized", tc.name), func(t *testing.T) {
txs := generateRandomlySizedContiguousShares(tc.txCount, tc.txSize)