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 47f3e55

Browse files
committedAug 22, 2022
wip: start using version
1 parent f4444ed commit 47f3e55

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed
 

‎pkg/prove/proof.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"github.com/tendermint/tendermint/types"
1414
)
1515

16-
// TxInclusion uses the provided block data to progressively generate rows
17-
// of a data square, and then using those shares to creates nmt inclusion proofs
18-
// It is possible that a transaction spans more than one row. In that case, we
19-
// have to return two proofs.
16+
// TxInclusion uses the provided block data to progressively generate rows of a
17+
// data square, and then uses those shares to creates nmt inclusion proofs. It
18+
// is possible that a transaction spans more than one row. In that case, we have
19+
// to return two proofs.
2020
func TxInclusion(codec rsmt2d.Codec, data types.Data, txIndex uint64) (types.TxProof, error) {
21-
// calculate the index of the shares that contain the tx
21+
// calculate the positions of the shares that contain this tx
2222
startPos, endPos, err := txSharePosition(data.Txs, txIndex)
2323
if err != nil {
2424
return types.TxProof{}, err
@@ -90,8 +90,9 @@ func TxInclusion(codec rsmt2d.Codec, data types.Data, txIndex uint64) (types.TxP
9090
}, nil
9191
}
9292

93-
// txSharePosition returns the share that a given transaction is included in.
94-
// returns an error if index is greater than that of the provided txs.
93+
// txSharePosition returns the start and end positions for the shares that
94+
// include the given transaction. Returns an error if txIndex is greater than
95+
// the number of transactions.
9596
func txSharePosition(txs types.Txs, txIndex uint64) (startSharePos, endSharePos uint64, err error) {
9697
if txIndex >= uint64(len(txs)) {
9798
return startSharePos, endSharePos, errors.New("transaction index is greater than the number of txs")
@@ -144,8 +145,8 @@ func genRowShares(codec rsmt2d.Codec, data types.Data, startRow, endRow uint64)
144145
}
145146

146147
// genOrigRowShares progressively generates data square rows for the original
147-
// data square, meaning the rows only half the full square length, as there is
148-
// not erasure data
148+
// data square, meaning the rows only fill half the full square length, as there
149+
// is no erasure data.
149150
func genOrigRowShares(data types.Data, startRow, endRow uint64) [][]byte {
150151
wantLen := (endRow + 1) * data.OriginalSquareSize
151152
startPos := startRow * data.OriginalSquareSize
@@ -168,7 +169,7 @@ func genOrigRowShares(data types.Data, startRow, endRow uint64) [][]byte {
168169
if err != nil {
169170
panic(fmt.Sprintf("app accepted a Message that can not be encoded %#v", m))
170171
}
171-
shares = types.AppendToShares(shares, m.NamespaceID, rawData)
172+
shares = types.AppendToShares(shares, m.NamespaceID, m.Version, rawData)
172173

173174
// return if we have enough shares
174175
if uint64(len(shares)) >= wantLen {

‎types/block.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ func (msgs Messages) SplitIntoShares() NamespacedShares {
11891189
if err != nil {
11901190
panic(fmt.Sprintf("app accepted a Message that can not be encoded %#v", m))
11911191
}
1192-
shares = AppendToShares(shares, m.NamespaceID, rawData)
1192+
shares = AppendToShares(shares, m.NamespaceID, m.Version, rawData)
11931193
}
11941194
return shares
11951195
}
@@ -1215,6 +1215,10 @@ type Message struct {
12151215
// Data is the actual data contained in the message
12161216
// (e.g. a block of a virtual sidechain).
12171217
Data []byte
1218+
1219+
// Version defines the the message format specification version of this
1220+
// message. Version is a number between 0 and 127 (inclusive).
1221+
Version uint8
12181222
}
12191223

12201224
var (
@@ -1226,9 +1230,14 @@ func MessageFromProto(p *tmproto.Message) Message {
12261230
if p == nil {
12271231
return MessageEmpty
12281232
}
1233+
if p.Version > 127 {
1234+
// TODO (throw an error here)
1235+
return MessageEmpty
1236+
}
12291237
return Message{
12301238
NamespaceID: p.NamespaceId,
12311239
Data: p.Data,
1240+
Version: uint8(p.Version),
12321241
}
12331242
}
12341243

@@ -1288,6 +1297,7 @@ func (data *Data) ToProto() tmproto.Data {
12881297
protoMsgs[i] = &tmproto.Message{
12891298
NamespaceId: msg.NamespaceID,
12901299
Data: msg.Data,
1300+
Version: uint32(msg.Version),
12911301
}
12921302
}
12931303
tp.Messages = tmproto.Messages{MessagesList: protoMsgs}
@@ -1319,7 +1329,7 @@ func DataFromProto(dp *tmproto.Data) (Data, error) {
13191329
if len(dp.Messages.MessagesList) > 0 {
13201330
msgs := make([]Message, len(dp.Messages.MessagesList))
13211331
for i, m := range dp.Messages.MessagesList {
1322-
msgs[i] = Message{NamespaceID: m.NamespaceId, Data: m.Data}
1332+
msgs[i] = Message{NamespaceID: m.NamespaceId, Data: m.Data, Version: uint8(m.Version)}
13231333
}
13241334
data.Messages = Messages{MessagesList: msgs}
13251335
} else {

‎types/block_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,12 @@ func TestBlockDataProtobuf(t *testing.T) {
682682
{
683683
NamespaceID: []byte{8, 7, 6, 5, 4, 3, 2, 1},
684684
Data: stdbytes.Repeat([]byte{3, 2, 1, 0}, 100),
685+
Version: uint8(0),
685686
},
686687
{
687688
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
688689
Data: stdbytes.Repeat([]byte{1, 2, 3}, 100),
690+
Version: uint8(0),
689691
},
690692
},
691693
},

‎types/share_splitting.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (msw *MessageShareWriter) Write(msg Message) {
2828
panic(fmt.Sprintf("app accepted a Message that can not be encoded %#v", msg))
2929
}
3030
newShares := make([]NamespacedShare, 0)
31-
newShares = AppendToShares(newShares, msg.NamespaceID, rawMsg)
31+
newShares = AppendToShares(newShares, msg.NamespaceID, msg.Version, rawMsg)
3232
msw.shares = append(msw.shares, newShares)
3333
msw.count += len(newShares)
3434
}
@@ -58,27 +58,33 @@ func (msw *MessageShareWriter) Count() int {
5858
return msw.count
5959
}
6060

61-
// appendToShares appends raw data as shares.
62-
// Used for messages.
63-
func AppendToShares(shares []NamespacedShare, nid namespace.ID, rawData []byte) []NamespacedShare {
61+
// AppendToShares appends raw data as shares. Used for messages.
62+
func AppendToShares(shares []NamespacedShare, nid namespace.ID, version uint8, rawData []byte) []NamespacedShare {
6463
if len(rawData) <= consts.MsgShareSize {
65-
rawShare := append(append(
66-
make([]byte, 0, len(nid)+len(rawData)),
64+
isMessageStart := true
65+
infoReservedByte, err := NewInfoReservedByte(version, isMessageStart)
66+
if err != nil {
67+
// TODO (propogate this error?)
68+
panic(err)
69+
}
70+
rawShare := append(append(append(
71+
make([]byte, 0, len(nid)+consts.InfoReservedBytes+len(rawData)),
6772
nid...),
73+
byte(infoReservedByte)),
6874
rawData...,
6975
)
7076
paddedShare := zeroPadIfNecessary(rawShare, consts.ShareSize)
7177
share := NamespacedShare{paddedShare, nid}
7278
shares = append(shares, share)
7379
} else { // len(rawData) > MsgShareSize
74-
shares = append(shares, splitMessage(rawData, nid)...)
80+
shares = append(shares, splitMessage(rawData, nid, version)...)
7581
}
7682
return shares
7783
}
7884

7985
// splitMessage breaks the data in a message into the minimum number of
8086
// namespaced shares
81-
func splitMessage(rawData []byte, nid namespace.ID) NamespacedShares {
87+
func splitMessage(rawData []byte, nid namespace.ID, version uint8) NamespacedShares {
8288
shares := make([]NamespacedShare, 0)
8389
firstRawShare := append(append(
8490
make([]byte, 0, consts.ShareSize),

‎types/shares.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type NamespacedShare struct {
1515
ID namespace.ID
1616
}
1717

18+
type MessageShare struct {
19+
NamespacedShare
20+
Info byte
21+
}
22+
1823
func (n NamespacedShare) NamespaceID() namespace.ID {
1924
return n.ID
2025
}
@@ -24,7 +29,7 @@ func (n NamespacedShare) Data() []byte {
2429
}
2530

2631
// NamespacedShares is just a list of NamespacedShare elements.
27-
// It can be used to extract the raw raw shares.
32+
// It can be used to extract the raw shares.
2833
type NamespacedShares []NamespacedShare
2934

3035
// RawShares returns the raw shares that can be fed into the erasure coding

0 commit comments

Comments
 (0)
Please sign in to comment.