Skip to content

Commit

Permalink
Modify DA Test to use SubmitBlocks with multiple blocks (cosmos#1085)
Browse files Browse the repository at this point in the history
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview
Closes: cosmos#1086 

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords

---------

Co-authored-by: Ganesha Upadhyaya <gupadhyaya@Ganeshas-MacBook-Pro-2.local>
  • Loading branch information
Manav-Aggarwal and Ganesha Upadhyaya authored Aug 9, 2023
1 parent 58ab158 commit ad8190c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
28 changes: 16 additions & 12 deletions da/celestia/mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,24 @@ func (s *Server) rpc(w http.ResponseWriter, r *http.Request) {
s.writeError(w, errors.New("expected 3 params: fee (uint64), gaslimit (uint64), data (base64 string)"))
return
}
block := types.Block{}
blockBase64 := params[2].([]interface{})[0].(map[string]interface{})["data"].(string)
blockData, err := base64.StdEncoding.DecodeString(blockBase64)
if err != nil {
s.writeError(w, err)
return
}
err = block.UnmarshalBinary(blockData)
if err != nil {
s.writeError(w, err)
return

blocks := make([]*types.Block, len(params[2].([]interface{})))
for i, data := range params[2].([]interface{}) {
blockBase64 := data.(map[string]interface{})["data"].(string)
blockData, err := base64.StdEncoding.DecodeString(blockBase64)
if err != nil {
s.writeError(w, err)
return
}
blocks[i] = new(types.Block)
err = blocks[i].UnmarshalBinary(blockData)
if err != nil {
s.writeError(w, err)
return
}
}

res := s.mock.SubmitBlocks(r.Context(), []*types.Block{&block})
res := s.mock.SubmitBlocks(r.Context(), blocks)
resp := &response{
Jsonrpc: "2.0",
Result: &sdk.TxResponse{
Expand Down
24 changes: 16 additions & 8 deletions da/test/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,23 @@ func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) {

retriever := dalc.(da.BlockRetriever)
countAtHeight := make(map[uint64]int)
blocks := make(map[*types.Block]uint64)

for i := uint64(0); i < 100; i++ {
b := getRandomBlock(i, rand.Int()%20) //nolint:gosec
resp := dalc.SubmitBlocks(ctx, []*types.Block{b})
blockToDAHeight := make(map[*types.Block]uint64)
numBatches := uint64(10)
blocksSubmittedPerBatch := 10

for i := uint64(0); i < numBatches; i++ {
blocks := make([]*types.Block, blocksSubmittedPerBatch)
for j := 0; j < len(blocks); j++ {
blocks[j] = getRandomBlock(i*numBatches+uint64(j), rand.Int()%20) //nolint:gosec
}
resp := dalc.SubmitBlocks(ctx, blocks)
assert.Equal(da.StatusSuccess, resp.Code, resp.Message)
time.Sleep(time.Duration(rand.Int63() % mockDaBlockTime.Milliseconds())) //nolint:gosec

countAtHeight[resp.DAHeight]++
blocks[b] = resp.DAHeight
for _, b := range blocks {
blockToDAHeight[b] = resp.DAHeight
countAtHeight[resp.DAHeight]++
}
}

// wait a bit more than mockDaBlockTime, so mock can "produce" last blocks
Expand All @@ -181,10 +188,11 @@ func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) {
ret := retriever.RetrieveBlocks(ctx, h)
assert.Equal(da.StatusSuccess, ret.Code, ret.Message)
require.NotEmpty(ret.Blocks, h)
assert.Equal(cnt%blocksSubmittedPerBatch, 0)
assert.Len(ret.Blocks, cnt, h)
}

for b, h := range blocks {
for b, h := range blockToDAHeight {
ret := retriever.RetrieveBlocks(ctx, h)
assert.Equal(da.StatusSuccess, ret.Code, h)
require.NotEmpty(ret.Blocks, h)
Expand Down
27 changes: 24 additions & 3 deletions types/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func (d *Data) MarshalBinary() ([]byte, error) {
return d.ToProto().Marshal()
}

// UnmarshalBinary decodes binary form of Data into object.
func (d *Data) UnmarshalBinary(data []byte) error {
var pData pb.Data
err := pData.Unmarshal(data)
if err != nil {
return err
}
err = d.FromProto(&pData)
return err
}

// MarshalBinary encodes Commit into binary form and returns it.
func (c *Commit) MarshalBinary() ([]byte, error) {
return c.ToProto().Marshal()
Expand Down Expand Up @@ -192,10 +203,20 @@ func (b *Block) FromProto(other *pb.Block) error {
if err != nil {
return err
}
b.Data.Txs = byteSlicesToTxs(other.Data.Txs)
b.Data.IntermediateStateRoots.RawRootsList = other.Data.IntermediateStateRoots
err = b.Data.FromProto(other.Data)
if err != nil {
return err
}

return nil
}

// FromProto fills the Data with data from its protobuf representation
func (d *Data) FromProto(other *pb.Data) error {
d.Txs = byteSlicesToTxs(other.Txs)
d.IntermediateStateRoots.RawRootsList = other.IntermediateStateRoots
// Note: Temporarily remove Evidence #896
// b.Data.Evidence = evidenceFromProto(other.Data.Evidence)
// d.Evidence = evidenceFromProto(other.Evidence)

return nil
}
Expand Down

0 comments on commit ad8190c

Please sign in to comment.