Skip to content
Merged
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)
Comment thread
nashqueue marked this conversation as resolved.
}

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