Skip to content

Commit

Permalink
chore!: rename blockdata.Messages -> blockdata.Blobs (#879)
Browse files Browse the repository at this point in the history
* chore!: rename Messages -> blobs

* fix: adjust tests now that there is less overhead

* review: feedback

Co-authored-by: Rootul P <[email protected]>

* chore; docs

Co-authored-by: Rootul P <[email protected]>
  • Loading branch information
evan-forbes and rootulp authored Nov 11, 2022
1 parent 58146a5 commit b7a7c1a
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 475 deletions.
2 changes: 1 addition & 1 deletion blockchain/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestBlockchainMessageVectors(t *testing.T) {
BlockRequest: &bcproto.BlockRequest{Height: math.MaxInt64}}},
"0a0a08ffffffffffffffff7f"},
{"BlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_BlockResponse{
BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, "1a95010a92010a5b0a02080b1803220b088092b8c398feffffff012a0212003a20c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf96a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85512330a0b48656c6c6f20576f726c641a0022003220c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf9"},
BlockResponse: &bcproto.BlockResponse{Block: bpb}}}, "1a93010a90010a5b0a02080b1803220b088092b8c398feffffff012a0212003a20c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf96a20e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85512310a0b48656c6c6f20576f726c641a003220c4da88e876062aa1543400d50d0eaa0dac88096057949cfb7bca7f3a48c04bf9"},
{"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
NoBlockResponse: &bcproto.NoBlockResponse{Height: 1}}}, "12020801"},
{"NoBlockResponseMessage", &bcproto.Message{Sum: &bcproto.Message_NoBlockResponse{
Expand Down
4 changes: 2 additions & 2 deletions mempool/v0/clist_mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func TestReapMaxBytesMaxGas(t *testing.T) {
{20, 10, 10, 0},
{20, 28, 10, 1}, // account for overhead in Data{}
{20, 240, 5, 5},
{20, 240, -1, 10},
{20, 240, -1, 12},
{20, 240, 10, 10},
{20, 240, 15, 10},
{20, 240, 15, 12},
{20, 20000, -1, 20},
{20, 20000, 5, 5},
{20, 20000, 30, 20},
Expand Down
2 changes: 1 addition & 1 deletion mempool/v1/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
ensurePrioritized(reapedTxs)
require.Equal(t, len(tTxs), txmp.Size())
require.Equal(t, int64(5690), txmp.SizeBytes())
require.Len(t, reapedTxs, 23)
require.Len(t, reapedTxs, 24)
}

func TestTxMempool_ReapMaxTxs(t *testing.T) {
Expand Down
578 changes: 203 additions & 375 deletions proto/tendermint/types/types.pb.go

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions proto/tendermint/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ message Data {
repeated bytes txs = 1;

// field number 2 is reserved for intermediate state roots
EvidenceList evidence = 3 [(gogoproto.nullable) = false];
Messages messages = 4 [(gogoproto.nullable) = false];
uint64 square_size = 5;
bytes hash = 6;
EvidenceList evidence = 3 [(gogoproto.nullable) = false];
repeated Blob blobs = 4 [(gogoproto.nullable) = false];
uint64 square_size = 5;
bytes hash = 6;
}

// DuplicateVoteEvidence contains evidence of a validator signed two conflicting votes.
Expand Down Expand Up @@ -125,11 +125,9 @@ message EvidenceList {
repeated Evidence evidence = 1 [(gogoproto.nullable) = false];
}

message Messages {
repeated Message messages_list = 1;
}

message Message {
// Blob defines a chunk of data that is attributed to a namespace and under
// usual circumstances, ends up published on-chain.
message Blob {
bytes namespace_id = 1;
bytes data = 2;
}
Expand Down
6 changes: 2 additions & 4 deletions state/test/factory/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ func MakeBlock(state sm.State, height int64, c *types.Commit) *types.Block {
return block
}

func MakeData(txs []types.Tx, evd []types.Evidence, msgs []types.Message) types.Data {
func MakeData(txs []types.Tx, evd []types.Evidence, blobs []types.Blob) types.Data {
return types.Data{
Txs: txs,
Evidence: types.EvidenceData{
Evidence: evd,
},
Messages: types.Messages{
MessagesList: msgs,
},
Blobs: blobs,
}
}

Expand Down
4 changes: 2 additions & 2 deletions state/tx_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestTxFilter(t *testing.T) {
tx types.Tx
isErr bool
}{
{types.Tx(tmrand.Bytes(2149)), false},
{types.Tx(tmrand.Bytes(2152)), true},
{types.Tx(tmrand.Bytes(2151)), false},
{types.Tx(tmrand.Bytes(2154)), true},
{types.Tx(tmrand.Bytes(3000)), true},
}

Expand Down
6 changes: 2 additions & 4 deletions test/factory/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ func MakeTenTxs(height int64) (txs []types.Tx) {
return MakeTxs(height, 10)
}

func MakeData(txs []types.Tx, evd []types.Evidence, msgs []types.Message) types.Data {
func MakeData(txs []types.Tx, evd []types.Evidence, blobs []types.Blob) types.Data {
return types.Data{
Txs: txs,
Evidence: types.EvidenceData{
Evidence: evd,
},
Messages: types.Messages{
MessagesList: msgs,
},
Blobs: blobs,
}
}
90 changes: 40 additions & 50 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,13 +1013,12 @@ type Data struct {

Evidence EvidenceData `json:"evidence"`

// The messages included in this block.
// TODO: how do messages end up here? (abci) app <-> ll-core?
// A simple approach could be: include them in the Tx above and
// have a mechanism to split them out somehow? Probably better to include
// them only when necessary (before proposing the block) as messages do not
// really need to be processed by tendermint
Messages Messages `json:"msgs"`
// The messages included in this block. TODO: how do messages end up here?
// (abci) app <-> ll-core? A simple approach could be: include them in the
// Tx above and have a mechanism to split them out somehow? Probably better
// to include them only when necessary (before proposing the block) as blobs
// do not really need to be processed by tendermint
Blobs []Blob `json:"blobs"`

// SquareSize is the size of the square after splitting all the block data
// into shares. The erasure data is discarded after generation, and keeping this
Expand Down Expand Up @@ -1048,41 +1047,37 @@ func (data *Data) Hash() tmbytes.HexBytes {
return data.hash
}

type Messages struct {
MessagesList []Message `json:"msgs"`
}

// ByNamespace implements sort.Interface for Message
type ByNamespace []Message
// ByNamespace implements sort.Interface for Blob
type Blobs []Blob

func (s ByNamespace) Len() int {
return len(s)
func (b Blobs) Len() int {
return len(b)
}

func (s ByNamespace) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
func (b Blobs) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}

func (s ByNamespace) Less(i, j int) bool {
func (b Blobs) Less(i, j int) bool {
// The following comparison is `<` and not `<=` because bytes.Compare returns 0 for if a == b.
// We want this comparison to return `false` if a == b because:
// If both Less(i, j) and Less(j, i) are false,
// then the elements at index i and j are considered equal.
// See https://pkg.go.dev/sort#Interface
return bytes.Compare(s[i].NamespaceID, s[j].NamespaceID) < 0
return bytes.Compare(b[i].NamespaceID, b[j].NamespaceID) < 0
}

// SortMessages sorts messages by ascending namespace id
func (msgs *Messages) SortMessages() {
sort.Sort(ByNamespace(msgs.MessagesList))
func (b *Blobs) SortMessages() {
sort.Sort(b)
}

// IsSorted returns whether the messages are sorted by namespace id
func (msgs *Messages) IsSorted() bool {
return sort.IsSorted(ByNamespace(msgs.MessagesList))
func (b *Blobs) IsSorted() bool {
return sort.IsSorted(b)
}

type Message struct {
type Blob struct {
// NamespaceID defines the namespace of this message, i.e. the
// namespace it will use in the namespaced Merkle tree.
NamespaceID namespace.ID
Expand All @@ -1092,32 +1087,27 @@ type Message struct {
Data []byte
}

var (
MessageEmpty = Message{}
MessagesEmpty = Messages{}
)

func MessageFromProto(p *tmproto.Message) Message {
func BlobFromProto(p *tmproto.Blob) Blob {
if p == nil {
return MessageEmpty
return Blob{}
}
return Message{
return Blob{
NamespaceID: p.NamespaceId,
Data: p.Data,
}
}

func MessagesFromProto(p *tmproto.Messages) Messages {
func BlobsFromProto(p []*tmproto.Blob) []Blob {
if p == nil {
return MessagesEmpty
return []Blob{}
}

msgs := make([]Message, 0, len(p.MessagesList))
blobs := make([]Blob, 0, len(p))

for i := 0; i < len(p.MessagesList); i++ {
msgs = append(msgs, MessageFromProto(p.MessagesList[i]))
for i := 0; i < len(p); i++ {
blobs = append(blobs, BlobFromProto(p[i]))
}
return Messages{MessagesList: msgs}
return blobs
}

// StringIndented returns an indented string representation of the transactions.
Expand Down Expand Up @@ -1158,14 +1148,14 @@ func (data *Data) ToProto() tmproto.Data {
}
tp.Evidence = *pevd

protoMsgs := make([]*tmproto.Message, len(data.Messages.MessagesList))
for i, msg := range data.Messages.MessagesList {
protoMsgs[i] = &tmproto.Message{
NamespaceId: msg.NamespaceID,
Data: msg.Data,
protoBlobs := make([]tmproto.Blob, len(data.Blobs))
for i, b := range data.Blobs {
protoBlobs[i] = tmproto.Blob{
NamespaceId: b.NamespaceID,
Data: b.Data,
}
}
tp.Messages = tmproto.Messages{MessagesList: protoMsgs}
tp.Blobs = protoBlobs
tp.SquareSize = data.SquareSize

tp.Hash = data.hash
Expand All @@ -1191,14 +1181,14 @@ func DataFromProto(dp *tmproto.Data) (Data, error) {
data.Txs = Txs{}
}

if len(dp.Messages.MessagesList) > 0 {
msgs := make([]Message, len(dp.Messages.MessagesList))
for i, m := range dp.Messages.MessagesList {
msgs[i] = Message{NamespaceID: m.NamespaceId, Data: m.Data}
if len(dp.Blobs) > 0 {
blobs := make([]Blob, len(dp.Blobs))
for i, m := range dp.Blobs {
blobs[i] = Blob{NamespaceID: m.NamespaceId, Data: m.Data}
}
data.Messages = Messages{MessagesList: msgs}
data.Blobs = blobs
} else {
data.Messages = Messages{}
data.Blobs = []Blob{}
}

evdData := new(EvidenceData)
Expand Down
35 changes: 18 additions & 17 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,10 @@ func TestBlockProtoBuf(t *testing.T) {

func TestBlockDataProtobuf(t *testing.T) {
type test struct {
name string
txs Txs
evd EvidenceData
msgs []Message
name string
txs Txs
evd EvidenceData
blobs []Blob
}
tests := []test{
{
Expand All @@ -671,7 +671,7 @@ func TestBlockDataProtobuf(t *testing.T) {
name: "everything",
txs: Txs([]Tx{stdbytes.Repeat([]byte{1}, 200)}),
evd: EvidenceData{Evidence: EvidenceList([]Evidence{})},
msgs: []Message{
blobs: []Blob{
{
NamespaceID: []byte{8, 7, 6, 5, 4, 3, 2, 1},
Data: stdbytes.Repeat([]byte{3, 2, 1, 0}, 100),
Expand All @@ -685,7 +685,7 @@ func TestBlockDataProtobuf(t *testing.T) {
}

for _, tt := range tests {
d := Data{Txs: tt.txs, Evidence: tt.evd, Messages: Messages{MessagesList: tt.msgs}}
d := Data{Txs: tt.txs, Evidence: tt.evd, Blobs: tt.blobs}
firstHash := d.Hash()
pd := d.ToProto()
d2, err := DataFromProto(&pd)
Expand Down Expand Up @@ -855,7 +855,7 @@ func TestBlockIDEquals(t *testing.T) {
}

func TestMessagesIsSorted(t *testing.T) {
sortedMessages := Messages{MessagesList: []Message{
sortedBlobs := []Blob{
{
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
Data: stdbytes.Repeat([]byte{1}, 100),
Expand All @@ -864,8 +864,8 @@ func TestMessagesIsSorted(t *testing.T) {
NamespaceID: []byte{8, 7, 6, 5, 4, 3, 2, 1},
Data: stdbytes.Repeat([]byte{2}, 100),
},
}}
sameNamespaceMessages := Messages{MessagesList: []Message{
}
sameNamespacedBlobs := []Blob{
{
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
Data: stdbytes.Repeat([]byte{1}, 100),
Expand All @@ -874,8 +874,8 @@ func TestMessagesIsSorted(t *testing.T) {
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
Data: stdbytes.Repeat([]byte{2}, 100),
},
}}
unsortedMessages := Messages{MessagesList: []Message{
}
unsortedBlobs := []Blob{
{
NamespaceID: []byte{8, 7, 6, 5, 4, 3, 2, 1},
Data: stdbytes.Repeat([]byte{1}, 100),
Expand All @@ -884,23 +884,24 @@ func TestMessagesIsSorted(t *testing.T) {
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
Data: stdbytes.Repeat([]byte{2}, 100),
},
}}
}

type testCase struct {
descripton string
messages Messages
blobs []Blob
want bool
}

tests := []testCase{
{"sorted messages", sortedMessages, true},
{"same namespace messages", sameNamespaceMessages, true},
{"unsorted messages", unsortedMessages, false},
{"sorted blobs", sortedBlobs, true},
{"same namespace blobs", sameNamespacedBlobs, true},
{"unsorted blobs", unsortedBlobs, false},
}

for _, tc := range tests {
t.Run(tc.descripton, func(t *testing.T) {
assert.Equal(t, tc.want, tc.messages.IsSorted())
bs := Blobs(tc.blobs)
assert.Equal(t, tc.want, bs.IsSorted())
})
}
}
9 changes: 5 additions & 4 deletions types/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ func MakeVote(
return vote, nil
}

func makeData(txs []Tx, evd []Evidence, msgs []Message) Data {
func makeData(txs []Tx, evd []Evidence, blobs []Blob) Data {
if blobs == nil {
blobs = []Blob{}
}
return Data{
Txs: txs,
Evidence: EvidenceData{
Evidence: evd,
},
Messages: Messages{
MessagesList: msgs,
},
Blobs: blobs,
}
}
10 changes: 4 additions & 6 deletions types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ func TestUnwrapMalleatedTx(t *testing.T) {
Evidence: EvidenceData{
Evidence: nil,
},
Messages: Messages{
MessagesList: []Message{
{
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9},
},
Blobs: []Blob{
{
NamespaceID: []byte{1, 2, 3, 4, 5, 6, 7, 8},
Data: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9},
},
},
}
Expand Down

0 comments on commit b7a7c1a

Please sign in to comment.