Skip to content

Commit

Permalink
Fix bug in decoding missingdatakey
Browse files Browse the repository at this point in the history
FAB-13240 #done

Change-Id: I7ec5191a20743c99e992e7ce4b3c2a6117087144
Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi committed Dec 12, 2018
1 parent 5bcbbb5 commit e485f77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/ledger/pvtdatastorage/kv_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func decodeMissingDataKey(keyBytes []byte) *missingDataKey {
return key
}

splittedKey := bytes.Split(keyBytes[1:], []byte{nilByte})
splittedKey := bytes.SplitN(keyBytes[1:], []byte{nilByte}, 3) //encoded bytes for blknum may contain empty bytes
key.ns = string(splittedKey[0])
key.coll = string(splittedKey[1])
key.blkNum, _ = util.DecodeReverseOrderVarUint64(splittedKey[2])
Expand Down
38 changes: 38 additions & 0 deletions core/ledger/pvtdatastorage/kv_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package pvtdatastorage

import (
"bytes"
math "math"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -78,3 +79,40 @@ func TestEligibleMissingdataRange(t *testing.T) {
assert.Equal(t, bytes.Compare(keyOfPreviousBlock, endKey), 1)
}
}

func TestEncodeDecodeMissingdataKey(t *testing.T) {
for i := 0; i < 1000; i++ {
testEncodeDecodeMissingdataKey(t, uint64(i))
}
testEncodeDecodeMissingdataKey(t, math.MaxUint64) // corner case
}

func testEncodeDecodeMissingdataKey(t *testing.T, blkNum uint64) {
key := &missingDataKey{
nsCollBlk: nsCollBlk{
ns: "ns",
coll: "coll",
blkNum: blkNum,
},
}

t.Run("ineligibileKey",
func(t *testing.T) {
key.isEligible = false
decodedKey := decodeMissingDataKey(
encodeMissingDataKey(key),
)
assert.Equal(t, key, decodedKey)
},
)

t.Run("ineligibileKey",
func(t *testing.T) {
key.isEligible = true
decodedKey := decodeMissingDataKey(
encodeMissingDataKey(key),
)
assert.Equal(t, key, decodedKey)
},
)
}

0 comments on commit e485f77

Please sign in to comment.