-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore!: consistent share prefix for tail padding (#1353)
- Loading branch information
Showing
14 changed files
with
243 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package shares | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"github.com/celestiaorg/celestia-app/pkg/appconsts" | ||
"github.com/celestiaorg/nmt/namespace" | ||
) | ||
|
||
// NamespacePaddingShare returns a share that acts as padding. Namespace padding | ||
// shares follow a blob so that the next blob may start at an index that | ||
// conforms to non-interactive default rules. The ns parameter provided should | ||
// be the namespace of the blob that precedes this padding in the data square. | ||
func NamespacePaddingShare(ns namespace.ID) Share { | ||
infoByte, err := NewInfoByte(appconsts.ShareVersionZero, true) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sequenceLen := make([]byte, appconsts.SequenceLenBytes) | ||
binary.BigEndian.PutUint32(sequenceLen, uint32(0)) | ||
|
||
padding := bytes.Repeat([]byte{0}, appconsts.FirstSparseShareContentSize) | ||
|
||
share := make([]byte, 0, appconsts.ShareSize) | ||
share = append(share, ns...) | ||
share = append(share, byte(infoByte)) | ||
share = append(share, sequenceLen...) | ||
share = append(share, padding...) | ||
return share | ||
} | ||
|
||
// NamespacePaddingShares returns n namespace padding shares. | ||
func NamespacePaddingShares(ns namespace.ID, n int) []Share { | ||
shares := make([]Share, n) | ||
for i := 0; i < n; i++ { | ||
shares[i] = NamespacePaddingShare(ns) | ||
} | ||
return shares | ||
} | ||
|
||
// ReservedPaddingShare returns a share that acts as padding. Reserved padding | ||
// shares follow all significant shares in the reserved namespace so that the | ||
// first blob can start at an index that conforms to non-interactive default | ||
// rules. | ||
func ReservedPaddingShare() Share { | ||
return NamespacePaddingShare(appconsts.ReservedPaddingNamespaceID) | ||
} | ||
|
||
// ReservedPaddingShare returns n reserved padding shares. | ||
func ReservedPaddingShares(n int) []Share { | ||
return NamespacePaddingShares(appconsts.ReservedPaddingNamespaceID, n) | ||
} | ||
|
||
// TailPaddingShare is a share that is used to pad a data square to the desired | ||
// square size. Tail padding shares follow the last blob share in the data | ||
// square. | ||
func TailPaddingShare() Share { | ||
return NamespacePaddingShare(appconsts.TailPaddingNamespaceID) | ||
} | ||
|
||
// TailPaddingShares returns n tail padding shares. | ||
func TailPaddingShares(n int) []Share { | ||
return NamespacePaddingShares(appconsts.TailPaddingNamespaceID, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package shares | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/pkg/appconsts" | ||
"github.com/celestiaorg/nmt/namespace" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var nsOne = namespace.ID{1, 1, 1, 1, 1, 1, 1, 1} | ||
|
||
var nsOnePadding, _ = zeroPadIfNecessary([]byte{ | ||
1, 1, 1, 1, 1, 1, 1, 1, // namespace ID | ||
1, // info byte | ||
0, 0, 0, 0, // sequence len | ||
}, appconsts.ShareSize) | ||
|
||
var reservedPadding, _ = zeroPadIfNecessary([]byte{ | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, // namespace ID | ||
1, // info byte | ||
0, 0, 0, 0, // sequence len | ||
}, appconsts.ShareSize) | ||
|
||
var tailPadding, _ = zeroPadIfNecessary([]byte{ | ||
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, // namespace ID | ||
1, // info byte | ||
0, 0, 0, 0, // sequence len | ||
}, appconsts.ShareSize) | ||
|
||
func TestNamespacePaddingShare(t *testing.T) { | ||
got := NamespacePaddingShare(nsOne).ToBytes() | ||
assert.Equal(t, nsOnePadding, got) | ||
} | ||
|
||
func TestNamespacePaddingShares(t *testing.T) { | ||
shares := NamespacePaddingShares(nsOne, 2) | ||
for _, share := range shares { | ||
assert.Equal(t, nsOnePadding, share.ToBytes()) | ||
} | ||
} | ||
|
||
func TestReservedPaddingShare(t *testing.T) { | ||
got := ReservedPaddingShare().ToBytes() | ||
assert.Equal(t, reservedPadding, got) | ||
} | ||
|
||
func TestReservedPaddingShares(t *testing.T) { | ||
shares := ReservedPaddingShares(2) | ||
for _, share := range shares { | ||
assert.Equal(t, reservedPadding, share.ToBytes()) | ||
} | ||
} | ||
|
||
func TestTailPaddingShare(t *testing.T) { | ||
got := TailPaddingShare().ToBytes() | ||
assert.Equal(t, tailPadding, got) | ||
} | ||
|
||
func TestTailPaddingShares(t *testing.T) { | ||
shares := TailPaddingShares(2) | ||
for _, share := range shares { | ||
assert.Equal(t, tailPadding, share.ToBytes()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.