Skip to content

Commit

Permalink
fix: consensus msgs serialization (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch authored Nov 12, 2024
1 parent dd734b0 commit 30a84df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
24 changes: 2 additions & 22 deletions block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

proto2 "github.com/gogo/protobuf/proto"
proto "github.com/gogo/protobuf/types"
"go.uber.org/multierr"

abci "github.com/tendermint/tendermint/abci/types"
Expand All @@ -17,6 +16,7 @@ import (

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/types"
protoutils "github.com/dymensionxyz/dymint/utils/proto"
)

// default minimum block max size allowed. not specific reason to set it to 10K, but we need to avoid no transactions can be included in a block.
Expand Down Expand Up @@ -167,7 +167,7 @@ func (e *Executor) CreateBlock(
Txs: toDymintTxs(mempoolTxs),
IntermediateStateRoots: types.IntermediateStateRoots{RawRootsList: nil},
Evidence: types.EvidenceData{Evidence: nil},
ConsensusMessages: fromProtoMsgSliceToAnySlice(e.consensusMsgQueue.Get()...),
ConsensusMessages: protoutils.FromProtoMsgSliceToAnySlice(e.consensusMsgQueue.Get()...),
},
LastCommit: *lastCommit,
}
Expand Down Expand Up @@ -337,23 +337,3 @@ func fromDymintTxs(optiTxs types.Txs) tmtypes.Txs {
}
return txs
}

func fromProtoMsgToAny(msg proto2.Message) *proto.Any {
theType, err := proto2.Marshal(msg)
if err != nil {
return nil
}

return &proto.Any{
TypeUrl: proto2.MessageName(msg),
Value: theType,
}
}

func fromProtoMsgSliceToAnySlice(msgs ...proto2.Message) []*proto.Any {
result := make([]*proto.Any, len(msgs))
for i, msg := range msgs {
result[i] = fromProtoMsgToAny(msg)
}
return result
}
2 changes: 2 additions & 0 deletions types/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (d *Data) ToProto() *pb.Data {
Txs: txsToByteSlices(d.Txs),
IntermediateStateRoots: d.IntermediateStateRoots.RawRootsList,
Evidence: evidenceToProto(d.Evidence),
ConsensusMessages: d.ConsensusMessages,
}
}

Expand All @@ -187,6 +188,7 @@ func (b *Block) FromProto(other *pb.Block) error {
b.Data.Txs = byteSlicesToTxs(other.Data.Txs)
b.Data.IntermediateStateRoots.RawRootsList = other.Data.IntermediateStateRoots
b.Data.Evidence = evidenceFromProto(other.Data.Evidence)
b.Data.ConsensusMessages = other.Data.ConsensusMessages
if other.LastCommit != nil {
err := b.LastCommit.FromProto(other.LastCommit)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions types/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"

"github.com/dymensionxyz/dymint/block"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/types"
pb "github.com/dymensionxyz/dymint/types/pb/dymint"
protoutils "github.com/dymensionxyz/dymint/utils/proto"
)

func TestBlockSerializationRoundTrip(t *testing.T) {
Expand All @@ -31,6 +32,10 @@ func TestBlockSerializationRoundTrip(t *testing.T) {
h = append(h, h1)
}

sequencers := []types.Sequencer{testutil.GenerateSequencer()}
consensusMsgs, err := block.ConsensusMsgsOnSequencerSetUpdate(sequencers)
require.NoError(err)

cases := []struct {
name string
input *types.Block
Expand All @@ -57,7 +62,8 @@ func TestBlockSerializationRoundTrip(t *testing.T) {
Txs: nil,
IntermediateStateRoots: types.IntermediateStateRoots{RawRootsList: [][]byte{{0x1}}},
// TODO(tzdybal): update when we have actual evidence types
Evidence: types.EvidenceData{Evidence: nil},
Evidence: types.EvidenceData{Evidence: nil},
ConsensusMessages: protoutils.FromProtoMsgSliceToAnySlice(consensusMsgs...),
},
LastCommit: types.Commit{
Height: 8,
Expand Down
21 changes: 21 additions & 0 deletions utils/proto/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proto

import (
cosmos "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/gogo/protobuf/proto"
gogo "github.com/gogo/protobuf/types"
)

Expand All @@ -24,3 +25,23 @@ func CosmosToGogo(v *cosmos.Any) *gogo.Any {
Value: v.Value,
}
}

func FromProtoMsgToAny(msg proto.Message) *gogo.Any {
theType, err := proto.Marshal(msg)
if err != nil {
return nil
}

return &gogo.Any{
TypeUrl: proto.MessageName(msg),
Value: theType,
}
}

func FromProtoMsgSliceToAnySlice(msgs ...proto.Message) []*gogo.Any {
result := make([]*gogo.Any, len(msgs))
for i, msg := range msgs {
result[i] = FromProtoMsgToAny(msg)
}
return result
}

0 comments on commit 30a84df

Please sign in to comment.