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

Rename appconsts to use Compact and Sparse terminology #710

Merged
merged 5 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions app/prepare_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (app *App) estimateSquareSize(data *core.Data) uint64 {
for _, tx := range data.Txs {
txBytes += len(tx) + types.DelimLen(uint64(len(tx)))
}
txShareEstimate := txBytes / appconsts.TxShareSize
txShareEstimate := txBytes / appconsts.CompactShareContentSize
if txBytes > 0 {
txShareEstimate++ // add one to round up
}
Expand All @@ -60,7 +60,7 @@ func (app *App) estimateSquareSize(data *core.Data) uint64 {
for _, evd := range data.Evidence.Evidence {
evdBytes += evd.Size() + types.DelimLen(uint64(evd.Size()))
}
evdShareEstimate := evdBytes / appconsts.TxShareSize
evdShareEstimate := evdBytes / appconsts.CompactShareContentSize
if evdBytes > 0 {
evdShareEstimate++ // add one to round up
}
Expand Down
6 changes: 3 additions & 3 deletions app/split_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (sqwr *shareSplitter) hasRoomForBoth(tx, msg []byte) bool {

txBytesTaken := types.DelimLen(uint64(len(tx))) + len(tx)

maxTxSharesTaken := ((txBytesTaken - availableBytes) / appconsts.TxShareSize) + 1 // plus one because we have to add at least one share
maxTxSharesTaken := ((txBytesTaken - availableBytes) / appconsts.CompactShareContentSize) + 1 // plus one because we have to add at least one share

maxMsgSharesTaken := types.MsgSharesUsed(len(msg))

Expand All @@ -241,7 +241,7 @@ func (sqwr *shareSplitter) hasRoomForTx(tx []byte) bool {
return true
}

maxSharesTaken := ((bytesTaken - availableBytes) / appconsts.TxShareSize) + 1 // plus one because we have to add at least one share
maxSharesTaken := ((bytesTaken - availableBytes) / appconsts.CompactShareContentSize) + 1 // plus one because we have to add at least one share

return currentShareCount+maxSharesTaken <= sqwr.maxShareCount
}
Expand All @@ -255,7 +255,7 @@ func (sqwr *shareSplitter) shareCount() (count, availableTxBytes int) {
func (sqwr *shareSplitter) export() [][]byte {
count, availableBytes := sqwr.shareCount()
// increment the count if there are any pending tx bytes
if availableBytes < appconsts.TxShareSize {
if availableBytes < appconsts.CompactShareContentSize {
count++
}
shares := make([][]byte, sqwr.maxShareCount)
Expand Down
29 changes: 19 additions & 10 deletions pkg/appconsts/appconsts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ import (
"github.com/tendermint/tendermint/pkg/consts"
)

// These constants are sourced from:
// These constants were originally sourced from:
rootulp marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/celestiaorg/celestia-specs/blob/master/src/specs/consensus.md#constants
const (
// ShareSize is the size of a share (in bytes).
// See: https://github.com/celestiaorg/celestia-specs/blob/master/src/specs/consensus.md#constants
// ShareSize is the size of a share in bytes.
ShareSize = 256

// NamespaceSize is the namespace size in bytes.
NamespaceSize = 8

// ShareReservedBytes is the reserved bytes for contiguous appends.
ShareReservedBytes = 1
// See https://github.com/celestiaorg/celestia-app/pull/660#discussion_r958603307
// for the motivation behind `CompactShare` and `SparseShare` terminology.

// TxShareSize is the number of bytes usable for tx/evidence/ISR shares.
TxShareSize = ShareSize - NamespaceSize - ShareReservedBytes
// MsgShareSize is the number of bytes usable for message shares.
MsgShareSize = ShareSize - NamespaceSize
// CompactShareReservedBytes is the number of bytes reserved for the location of
// the first unit (transaction, ISR, evidence) in a compact share.
CompactShareReservedBytes = 1

// CompactShareContentSize is the number of bytes usable for data in a compact
// (i.e. transactions, ISRs, evidence) share.
CompactShareContentSize = ShareSize - NamespaceSize - CompactShareReservedBytes
// SparseShareContentSize is the number of bytes usable for data in a sparse (i.e.
// message) share.
SparseShareContentSize = ShareSize - NamespaceSize

// MaxSquareSize is the maximum number of
// rows/columns of the original data shares in square layout.
Expand Down Expand Up @@ -80,5 +85,9 @@ var (
// DataCommitmentBlocksLimit is the limit to the number of blocks we can generate a data commitment for.
DataCommitmentBlocksLimit = consts.DataCommitmentBlocksLimit

NameSpacedPaddedShareBytes = bytes.Repeat([]byte{0}, MsgShareSize)
// NameSpacedPaddedShareBytes are the raw bytes that are used in the contents
// of a NameSpacedPaddedShare. A NameSpacedPaddedShare follows a message so
// that the next message starts at an index that conforms to non-interactive
// defaults.
NameSpacedPaddedShareBytes = bytes.Repeat([]byte{0}, SparseShareContentSize)
)
4 changes: 2 additions & 2 deletions pkg/prove/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func txSharePosition(txs types.Txs, txIndex uint64) (startSharePos, endSharePos

txLen := len(txs[txIndex])

startSharePos = uint64((totalLen) / appconsts.TxShareSize)
endSharePos = uint64((totalLen + txLen + delimLen(txLen)) / appconsts.TxShareSize)
startSharePos = uint64((totalLen) / appconsts.CompactShareContentSize)
endSharePos = uint64((totalLen + txLen + delimLen(txLen)) / appconsts.CompactShareContentSize)

return startSharePos, endSharePos, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/prove/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestTxInclusion(t *testing.T) {
overlappingRowsBlockData := types.Data{
Txs: types.ToTxs(
[][]byte{
tmrand.Bytes(appconsts.TxShareSize*overlappingSquareSize + 1),
tmrand.Bytes(appconsts.CompactShareContentSize*overlappingSquareSize + 1),
tmrand.Bytes(10000),
},
),
Expand All @@ -39,7 +39,7 @@ func TestTxInclusion(t *testing.T) {
overlappingRowsBlockDataWithMessages := types.Data{
Txs: types.ToTxs(
[][]byte{
tmrand.Bytes(appconsts.TxShareSize*overlappingSquareSize + 1),
tmrand.Bytes(appconsts.CompactShareContentSize*overlappingSquareSize + 1),
tmrand.Bytes(10000),
},
),
Expand Down
2 changes: 1 addition & 1 deletion pkg/shares/compact_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Test_processCompactShares(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
// each tx
const exactTxShareSize = appconsts.TxShareSize - 1
const exactTxShareSize = appconsts.CompactShareContentSize - 1

type test struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions pkg/shares/parse_compact_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (ss *shareStack) resolve() ([][]byte, error) {
if len(ss.shares) == 0 {
return nil, nil
}
err := ss.peel(ss.shares[0][appconsts.NamespaceSize+appconsts.ShareReservedBytes:], true)
err := ss.peel(ss.shares[0][appconsts.NamespaceSize+appconsts.CompactShareReservedBytes:], true)
return ss.data, err
}

Expand Down Expand Up @@ -70,7 +70,7 @@ func (ss *shareStack) peel(share []byte, delimited bool) (err error) {
// add the next share to the current share to continue merging if possible
if len(ss.shares) > ss.cursor+1 {
ss.cursor++
share := append(share, ss.shares[ss.cursor][appconsts.NamespaceSize+appconsts.ShareReservedBytes:]...)
share := append(share, ss.shares[ss.cursor][appconsts.NamespaceSize+appconsts.CompactShareReservedBytes:]...)
return ss.peel(share, false)
}
// collect any remaining data
Expand Down
2 changes: 1 addition & 1 deletion pkg/shares/parse_sparse_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func parseSparseShares(shares [][]byte) ([]coretypes.Message, error) {
}
// iterate through all the shares and parse out each msg
for i := 0; i < len(shares); i++ {
dataLen = len(currentMsg.Data) + appconsts.MsgShareSize
dataLen = len(currentMsg.Data) + appconsts.SparseShareContentSize
switch {
case isNewMessage:
nextMsgChunk, nextMsgLen, err := ParseDelimiter(shares[i][appconsts.NamespaceSize:])
Expand Down
4 changes: 2 additions & 2 deletions pkg/shares/sparse_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_parseSparseShares(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
// each message
const exactMsgShareSize = appconsts.MsgShareSize - 2
const exactMsgShareSize = appconsts.SparseShareContentSize - 2

type test struct {
name string
Expand All @@ -30,7 +30,7 @@ func Test_parseSparseShares(t *testing.T) {
{"single big msg", 1000, 1},
{"many big msgs", 1000, 10},
{"single exact size msg", exactMsgShareSize, 1},
{"many exact size msgs", appconsts.MsgShareSize, 10},
{"many exact size msgs", appconsts.SparseShareContentSize, 10},
}

for _, tc := range tests {
Expand Down
8 changes: 4 additions & 4 deletions pkg/shares/split_compact_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (css *CompactShareSplitter) WriteBytes(rawData []byte) {
txCursor = len(rawData)

// add the share reserved bytes to the new pending share
pendingCursor := len(rawData) + appconsts.NamespaceSize + appconsts.ShareReservedBytes
pendingCursor := len(rawData) + appconsts.NamespaceSize + appconsts.CompactShareReservedBytes
var reservedByte byte
if pendingCursor >= appconsts.ShareSize {
// the share reserve byte is zero when some compactly written
Expand Down Expand Up @@ -126,7 +126,7 @@ func (css *CompactShareSplitter) Export() NamespacedShares {
lastShare := css.shares[len(css.shares)-1]
rawLastShare := lastShare.Data()

for i := 0; i < appconsts.ShareReservedBytes; i++ {
for i := 0; i < appconsts.CompactShareReservedBytes; i++ {
// here we force the last share reserved byte to be zero to avoid any
// confusion for light clients parsing these shares, as the rest of the
// data after transaction is padding. See
Expand All @@ -147,7 +147,7 @@ func (css *CompactShareSplitter) Count() (count, availableBytes int) {
if len(css.pendingShare.Share) > appconsts.NamespaceSize {
return len(css.shares), 0
}
availableBytes = appconsts.TxShareSize - (len(css.pendingShare.Share) - appconsts.NamespaceSize)
availableBytes = appconsts.CompactShareContentSize - (len(css.pendingShare.Share) - appconsts.NamespaceSize)
return len(css.shares), availableBytes
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func namespacedPaddedShares(ns []byte, count int) []NamespacedShare {
shares[i] = NamespacedShare{
Share: append(append(
make([]byte, 0, appconsts.ShareSize), ns...),
make([]byte, appconsts.MsgShareSize)...),
make([]byte, appconsts.SparseShareContentSize)...),
ID: ns,
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/shares/split_sparse_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (sss *SparseShareSplitter) Count() int {
// AppendToShares appends raw data as shares.
// Used for messages.
func AppendToShares(shares []NamespacedShare, nid namespace.ID, rawData []byte) []NamespacedShare {
if len(rawData) <= appconsts.MsgShareSize {
if len(rawData) <= appconsts.SparseShareContentSize {
rawShare := append(append(
make([]byte, 0, len(nid)+len(rawData)),
nid...),
Expand All @@ -114,12 +114,12 @@ func splitMessage(rawData []byte, nid namespace.ID) NamespacedShares {
firstRawShare := append(append(
make([]byte, 0, appconsts.ShareSize),
nid...),
rawData[:appconsts.MsgShareSize]...,
rawData[:appconsts.SparseShareContentSize]...,
)
shares = append(shares, NamespacedShare{firstRawShare, nid})
rawData = rawData[appconsts.MsgShareSize:]
rawData = rawData[appconsts.SparseShareContentSize:]
for len(rawData) > 0 {
shareSizeOrLen := min(appconsts.MsgShareSize, len(rawData))
shareSizeOrLen := min(appconsts.SparseShareContentSize, len(rawData))
rawShare := append(append(
make([]byte, 0, appconsts.ShareSize),
nid...),
Expand Down
4 changes: 2 additions & 2 deletions pkg/shares/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func DelimLen(x uint64) int {
func MsgSharesUsed(msgSize int) int {
// add the delimiter to the message size
msgSize = DelimLen(uint64(msgSize)) + msgSize
shareCount := msgSize / appconsts.MsgShareSize
shareCount := msgSize / appconsts.SparseShareContentSize
// increment the share count if the message overflows the last counted share
if msgSize%appconsts.MsgShareSize != 0 {
if msgSize%appconsts.SparseShareContentSize != 0 {
shareCount++
}
return shareCount
Expand Down
8 changes: 4 additions & 4 deletions pkg/wrapper/nmt_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestRootErasuredNamespacedMerkleTree(t *testing.T) {
// the case, because the ErasuredNamespacedMerkleTree should add namespaces
// to the second half of the tree
size := 8
data := generateRandNamespacedRawData(size, appconsts.NamespaceSize, appconsts.MsgShareSize)
data := generateRandNamespacedRawData(size, appconsts.NamespaceSize, appconsts.SparseShareContentSize)
n := NewErasuredNamespacedMerkleTree(uint64(size))
tree := n.Constructor()
nmtTree := nmt.New(sha256.New())
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestExtendedDataSquare(t *testing.T) {
raw := generateRandNamespacedRawData(
squareSize*squareSize,
appconsts.NamespaceSize,
appconsts.MsgShareSize,
appconsts.SparseShareContentSize,
)

tree := NewErasuredNamespacedMerkleTree(uint64(squareSize))
Expand All @@ -111,7 +111,7 @@ func TestExtendedDataSquare(t *testing.T) {
func TestErasuredNamespacedMerkleTree(t *testing.T) {
// check that the Tree() returns exact underlying nmt tree
size := 8
data := generateRandNamespacedRawData(size, appconsts.NamespaceSize, appconsts.MsgShareSize)
data := generateRandNamespacedRawData(size, appconsts.NamespaceSize, appconsts.SparseShareContentSize)
n := NewErasuredNamespacedMerkleTree(uint64(size))
tree := n.Constructor()

Expand All @@ -129,7 +129,7 @@ func generateErasuredData(t *testing.T, numLeaves int, codec rsmt2d.Codec) [][]b
raw := generateRandNamespacedRawData(
numLeaves,
appconsts.NamespaceSize,
appconsts.MsgShareSize,
appconsts.SparseShareContentSize,
)
erasuredData, err := codec.Encode(raw)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions x/payment/types/payfordata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ func TestSignMalleatedTxs(t *testing.T) {
{
name: "single share",
ns: []byte{1, 1, 1, 1, 1, 1, 1, 1},
msg: bytes.Repeat([]byte{1}, appconsts.MsgShareSize),
msg: bytes.Repeat([]byte{1}, appconsts.SparseShareContentSize),
ss: []uint64{2, 4, 8, 16},
options: []TxBuilderOption{SetGasLimit(2000000)},
},
{
name: "12 shares",
ns: []byte{1, 1, 1, 1, 1, 1, 1, 2},
msg: bytes.Repeat([]byte{2}, (appconsts.MsgShareSize*12)-4), // subtract a few bytes for the delimiter
msg: bytes.Repeat([]byte{2}, (appconsts.SparseShareContentSize*12)-4), // subtract a few bytes for the delimiter
ss: []uint64{4, 8, 16, 64},
options: []TxBuilderOption{
SetGasLimit(123456789),
Expand Down Expand Up @@ -276,21 +276,21 @@ func TestProcessMessage(t *testing.T) {
{
name: "single share square size 2",
ns: []byte{1, 1, 1, 1, 1, 1, 1, 1},
msg: bytes.Repeat([]byte{1}, totalMsgSize(appconsts.MsgShareSize)),
msg: bytes.Repeat([]byte{1}, totalMsgSize(appconsts.SparseShareContentSize)),
ss: 2,
modify: dontModify,
},
{
name: "15 shares square size 4",
ns: []byte{1, 1, 1, 1, 1, 1, 1, 2},
msg: bytes.Repeat([]byte{2}, totalMsgSize(appconsts.MsgShareSize*15)),
msg: bytes.Repeat([]byte{2}, totalMsgSize(appconsts.SparseShareContentSize*15)),
ss: 4,
modify: dontModify,
},
{
name: "incorrect square size",
ns: []byte{1, 1, 1, 1, 1, 1, 1, 2},
msg: bytes.Repeat([]byte{2}, totalMsgSize(appconsts.MsgShareSize*15)),
msg: bytes.Repeat([]byte{2}, totalMsgSize(appconsts.SparseShareContentSize*15)),
ss: 4,
modify: func(wpfd *MsgWirePayForData) *MsgWirePayForData {
wpfd.MessageShareCommitment[0].K = 99999
Expand Down Expand Up @@ -448,7 +448,7 @@ func validMsgPayForData(t *testing.T) *MsgPayForData {
kb := generateKeyring(t, "test")
signer := NewKeyringSigner(kb, "test", "chain-id")
ns := []byte{1, 1, 1, 1, 1, 1, 1, 2}
msg := bytes.Repeat([]byte{2}, totalMsgSize(appconsts.MsgShareSize*15))
msg := bytes.Repeat([]byte{2}, totalMsgSize(appconsts.SparseShareContentSize*15))
ss := uint64(4)

wpfd, err := NewWirePayForData(ns, msg, ss)
Expand Down
4 changes: 2 additions & 2 deletions x/payment/types/square_sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func AllSquareSizes(msgSize int) []uint64 {
func MsgSharesUsed(msgSize int) int {
// add the delimiter to the message size
msgSize = DelimLen(uint64(msgSize)) + msgSize
shareCount := msgSize / appconsts.MsgShareSize
shareCount := msgSize / appconsts.SparseShareContentSize
// increment the share count if the message overflows the last counted share
if msgSize%appconsts.MsgShareSize != 0 {
if msgSize%appconsts.SparseShareContentSize != 0 {
shareCount++
}
return shareCount
Expand Down