Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eeba808

Browse files
evan-forbesliamsi
andcommittedSep 21, 2021
fix overwrite bug (#251)
* fix overwrite bug and stop splitting shares of size MsgShareSize * remove ineffectual code * review feedback: better docs Co-authored-by: Ismail Khoffi <[email protected]> * remove uneeded copy and only fix the source of the bug Co-authored-by: Ismail Khoffi <[email protected]> * fix overwrite bug while also being consistent with using NamespacedShares * update to the latest rsmt2d for the nmt wrapper Co-authored-by: Ismail Khoffi <[email protected]>
1 parent 801cfc8 commit eeba808

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed
 

‎types/shares.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ func (m Message) MarshalDelimited() ([]byte, error) {
6262
// appendToShares appends raw data as shares.
6363
// Used for messages.
6464
func appendToShares(shares []NamespacedShare, nid namespace.ID, rawData []byte) []NamespacedShare {
65-
if len(rawData) < consts.MsgShareSize {
65+
if len(rawData) <= consts.MsgShareSize {
6666
rawShare := []byte(append(nid, rawData...))
6767
paddedShare := zeroPadIfNecessary(rawShare, consts.ShareSize)
6868
share := NamespacedShare{paddedShare, nid}
6969
shares = append(shares, share)
70-
} else { // len(rawData) >= MsgShareSize
70+
} else { // len(rawData) > MsgShareSize
7171
shares = append(shares, split(rawData, nid)...)
7272
}
7373
return shares
@@ -102,7 +102,9 @@ func split(rawData []byte, nid namespace.ID) []NamespacedShare {
102102
rawData = rawData[consts.MsgShareSize:]
103103
for len(rawData) > 0 {
104104
shareSizeOrLen := min(consts.MsgShareSize, len(rawData))
105-
rawShare := []byte(append(nid, rawData[:shareSizeOrLen]...))
105+
rawShare := make([]byte, consts.NamespaceSize)
106+
copy(rawShare, nid)
107+
rawShare = append(rawShare, rawData[:shareSizeOrLen]...)
106108
paddedShare := zeroPadIfNecessary(rawShare, consts.ShareSize)
107109
share := NamespacedShare{paddedShare, nid}
108110
shares = append(shares, share)

‎types/shares_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package types
22

33
import (
44
"bytes"
5+
"crypto/rand"
56
"reflect"
7+
"sort"
68
"testing"
79

810
"github.com/celestiaorg/nmt/namespace"
11+
"github.com/stretchr/testify/assert"
912
"github.com/tendermint/tendermint/internal/libs/protoio"
1013
"github.com/tendermint/tendermint/pkg/consts"
1114
)
@@ -176,3 +179,53 @@ func Test_zeroPadIfNecessary(t *testing.T) {
176179
})
177180
}
178181
}
182+
183+
func Test_appendToSharesOverwrite(t *testing.T) {
184+
var shares NamespacedShares
185+
186+
// generate some arbitrary namespaced shares first share that must be split
187+
newShare := generateRandomNamespacedShares(1, consts.MsgShareSize+1)[0]
188+
189+
// make a copy of the portion of the share to check if it's overwritten later
190+
extraCopy := make([]byte, consts.MsgShareSize)
191+
copy(extraCopy, newShare.Share[:consts.MsgShareSize])
192+
193+
// use appendToShares to add our new share
194+
appendToShares(shares, newShare.ID, newShare.Share)
195+
196+
// check if the original share data has been overwritten.
197+
assert.Equal(t, extraCopy, []byte(newShare.Share[:consts.MsgShareSize]))
198+
}
199+
200+
func generateRandomNamespacedShares(count, leafSize int) []NamespacedShare {
201+
shares := generateRandNamespacedRawData(count, consts.NamespaceSize, leafSize)
202+
nsShares := make(NamespacedShares, count)
203+
for i, s := range shares {
204+
nsShares[i] = NamespacedShare{
205+
Share: s[consts.NamespaceSize:],
206+
ID: s[:consts.NamespaceSize],
207+
}
208+
}
209+
return nsShares
210+
}
211+
212+
func generateRandNamespacedRawData(total, nidSize, leafSize int) [][]byte {
213+
data := make([][]byte, total)
214+
for i := 0; i < total; i++ {
215+
nid := make([]byte, nidSize)
216+
rand.Read(nid)
217+
data[i] = nid
218+
}
219+
sortByteArrays(data)
220+
for i := 0; i < total; i++ {
221+
d := make([]byte, leafSize)
222+
rand.Read(d)
223+
data[i] = append(data[i], d...)
224+
}
225+
226+
return data
227+
}
228+
229+
func sortByteArrays(src [][]byte) {
230+
sort.Slice(src, func(i, j int) bool { return bytes.Compare(src[i], src[j]) < 0 })
231+
}

0 commit comments

Comments
 (0)
Please sign in to comment.