From ad8190cb7cce04a40f73614f07a0273282f6648b Mon Sep 17 00:00:00 2001 From: Manav Aggarwal Date: Wed, 9 Aug 2023 07:17:08 -0400 Subject: [PATCH] Modify DA Test to use SubmitBlocks with multiple blocks (#1085) ## Overview Closes: #1086 ## Checklist - [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 --- da/celestia/mock/server.go | 28 ++++++++++++++++------------ da/test/da_test.go | 24 ++++++++++++++++-------- types/serialization.go | 27 ++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/da/celestia/mock/server.go b/da/celestia/mock/server.go index 9460b65c250..6bc088d6619 100644 --- a/da/celestia/mock/server.go +++ b/da/celestia/mock/server.go @@ -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{ diff --git a/da/test/da_test.go b/da/test/da_test.go index 9648ed8d7e1..34fac350fef 100644 --- a/da/test/da_test.go +++ b/da/test/da_test.go @@ -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 @@ -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) diff --git a/types/serialization.go b/types/serialization.go index c398ea18850..c5982359540 100644 --- a/types/serialization.go +++ b/types/serialization.go @@ -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() @@ -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 }