From 2f9db23a6952181f021bea9f74ae9e62b8ab9ea6 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Fri, 9 Aug 2024 09:57:33 -0400 Subject: [PATCH 1/2] p2pblockevent renamed --- block/p2p.go | 4 +- p2p/block_sync.go | 10 +- p2p/block_sync_test.go | 2 +- p2p/client.go | 4 +- p2p/p2p_block.go | 74 ------- p2p/pb/gossiped_block.pb.go | 387 --------------------------------- p2p/validator.go | 2 +- p2p/validator_test.go | 2 +- proto/p2p/gossiped_block.proto | 11 - 9 files changed, 12 insertions(+), 484 deletions(-) delete mode 100644 p2p/p2p_block.go delete mode 100644 p2p/pb/gossiped_block.pb.go delete mode 100644 proto/p2p/gossiped_block.proto diff --git a/block/p2p.go b/block/p2p.go index 7de670b11..2522a4099 100644 --- a/block/p2p.go +++ b/block/p2p.go @@ -11,7 +11,7 @@ import ( // onReceivedBlock receives a block received event from P2P, saves the block to a cache and tries to apply the blocks from the cache. func (m *Manager) onReceivedBlock(event pubsub.Message) { - eventData, ok := event.Data().(p2p.P2PBlockEvent) + eventData, ok := event.Data().(p2p.BlockData) if !ok { m.logger.Error("onReceivedBlock", "err", "wrong event data received") return @@ -64,7 +64,7 @@ func (m *Manager) onReceivedBlock(event pubsub.Message) { // gossipBlock sends created blocks by the sequencer to full-nodes using P2P gossipSub func (m *Manager) gossipBlock(ctx context.Context, block types.Block, commit types.Commit) error { m.logger.Info("Gossipping block", "height", block.Header.Height) - gossipedBlock := p2p.P2PBlockEvent{Block: block, Commit: commit} + gossipedBlock := p2p.BlockData{Block: block, Commit: commit} gossipedBlockBytes, err := gossipedBlock.MarshalBinary() if err != nil { return fmt.Errorf("marshal binary: %w: %w", err, ErrNonRecoverable) diff --git a/p2p/block_sync.go b/p2p/block_sync.go index e562fcfda..e7bda4bf2 100644 --- a/p2p/block_sync.go +++ b/p2p/block_sync.go @@ -39,7 +39,7 @@ type BlockSync struct { logger types.Logger } -type BlockSyncMessageHandler func(block *P2PBlockEvent) +type BlockSyncMessageHandler func(block *BlockData) // SetupBlockSync initializes all services required to provide and retrieve block data in the P2P network. func SetupBlockSync(ctx context.Context, h host.Host, store datastore.Datastore, logger types.Logger) *BlockSync { @@ -99,14 +99,14 @@ func (blocksync *BlockSync) SaveBlock(ctx context.Context, block []byte) (cid.Ci } // LoadBlock retrieves the blocks (from the local blockstore or the network) using the DAGService to discover all data chunks that are part of the same block. -func (blocksync *BlockSync) LoadBlock(ctx context.Context, cid cid.Cid) (P2PBlockEvent, error) { +func (blocksync *BlockSync) LoadBlock(ctx context.Context, cid cid.Cid) (BlockData, error) { blockBytes, err := blocksync.dsrv.LoadBlock(ctx, cid) if err != nil { - return P2PBlockEvent{}, err + return BlockData{}, err } - var block P2PBlockEvent + var block BlockData if err := block.UnmarshalBinary(blockBytes); err != nil { - return P2PBlockEvent{}, fmt.Errorf("deserialize blocksync block %w", err) + return BlockData{}, fmt.Errorf("deserialize blocksync block %w", err) } return block, nil } diff --git a/p2p/block_sync_test.go b/p2p/block_sync_test.go index 3baf01cef..6b61f2ebf 100644 --- a/p2p/block_sync_test.go +++ b/p2p/block_sync_test.go @@ -33,7 +33,7 @@ func TestBlockSync(t *testing.T) { commits, err := testutil.GenerateCommits(blocks, manager.LocalKey) require.NoError(t, err) - gossipedBlock := p2p.P2PBlockEvent{Block: *blocks[0], Commit: *commits[0]} + gossipedBlock := p2p.BlockData{Block: *blocks[0], Commit: *commits[0]} gossipedBlockbytes, err := gossipedBlock.MarshalBinary() require.NoError(t, err) diff --git a/p2p/client.go b/p2p/client.go index ed08f8565..e257f4523 100644 --- a/p2p/client.go +++ b/p2p/client.go @@ -499,7 +499,7 @@ func (c *Client) NewTxValidator() GossipValidator { } // blockSyncReceived is called on reception of new block via block-sync protocol -func (c *Client) blockSyncReceived(block *P2PBlockEvent) { +func (c *Client) blockSyncReceived(block *BlockData) { err := c.localPubsubServer.PublishWithEvents(context.Background(), *block, map[string][]string{EventTypeKey: {EventNewBlockSyncBlock}}) if err != nil { c.logger.Error("Publishing event.", "err", err) @@ -510,7 +510,7 @@ func (c *Client) blockSyncReceived(block *P2PBlockEvent) { // blockSyncReceived is called on reception of new block via gossip protocol func (c *Client) blockGossipReceived(ctx context.Context, block []byte) { - var gossipedBlock P2PBlockEvent + var gossipedBlock BlockData if err := gossipedBlock.UnmarshalBinary(block); err != nil { c.logger.Error("Deserialize gossiped block", "error", err) } diff --git a/p2p/p2p_block.go b/p2p/p2p_block.go deleted file mode 100644 index ac814d51c..000000000 --- a/p2p/p2p_block.go +++ /dev/null @@ -1,74 +0,0 @@ -package p2p - -import ( - "github.com/dymensionxyz/dymint/p2p/pb" - "github.com/dymensionxyz/dymint/types" - tmtypes "github.com/tendermint/tendermint/types" -) - -/* -------------------------------------------------------------------------- */ -/* Event Data */ -/* -------------------------------------------------------------------------- */ - -// P2PBlockEvent defines the struct of the event data for the Block sent via P2P -type P2PBlockEvent struct { - // Block is the block that was gossiped - Block types.Block - // Commit is the commit that was gossiped - Commit types.Commit -} - -// MarshalBinary encodes GossipedBlock into binary form and returns it. -func (e *P2PBlockEvent) MarshalBinary() ([]byte, error) { - return e.ToProto().Marshal() -} - -// UnmarshalBinary decodes binary form of GossipedBlock into object. -func (e *P2PBlockEvent) UnmarshalBinary(data []byte) error { - var pbGossipedBlock pb.GossipedBlock - err := pbGossipedBlock.Unmarshal(data) - if err != nil { - return err - } - err = e.FromProto(&pbGossipedBlock) - return err -} - -// ToProto converts Data into protobuf representation and returns it. -func (e *P2PBlockEvent) ToProto() *pb.GossipedBlock { - return &pb.GossipedBlock{ - Block: e.Block.ToProto(), - Commit: e.Commit.ToProto(), - } -} - -// FromProto fills P2PBlock with data from its protobuf representation. -func (e *P2PBlockEvent) FromProto(other *pb.GossipedBlock) error { - if err := e.Block.FromProto(other.Block); err != nil { - return err - } - if err := e.Commit.FromProto(other.Commit); err != nil { - return err - } - return nil -} - -// Validate run basic validation on the p2p block -func (e *P2PBlockEvent) Validate(proposer *types.Sequencer) error { - if err := e.Block.ValidateBasic(); err != nil { - return err - } - if err := e.Commit.ValidateBasic(); err != nil { - return err - } - if err := e.Commit.ValidateWithHeader(proposer, &e.Block.Header); err != nil { - return err - } - abciData := tmtypes.Data{ - Txs: types.ToABCIBlockDataTxs(&e.Block.Data), - } - if e.Block.Header.DataHash != [32]byte(abciData.Hash()) { - return types.ErrInvalidHeaderDataHash - } - return nil -} diff --git a/p2p/pb/gossiped_block.pb.go b/p2p/pb/gossiped_block.pb.go deleted file mode 100644 index ebcd12548..000000000 --- a/p2p/pb/gossiped_block.pb.go +++ /dev/null @@ -1,387 +0,0 @@ -// Code generated by protoc-gen-gogo. DO NOT EDIT. -// source: p2p/gossiped_block.proto - -package pb - -import ( - fmt "fmt" - dymint "github.com/dymensionxyz/dymint/types/pb/dymint" - proto "github.com/gogo/protobuf/proto" - io "io" - math "math" - math_bits "math/bits" -) - -// Reference imports to suppress errors if they are not otherwise used. -var _ = proto.Marshal -var _ = fmt.Errorf -var _ = math.Inf - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the proto package it is being compiled against. -// A compilation error at this line likely means your copy of the -// proto package needs to be updated. -const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package - -type GossipedBlock struct { - Block *dymint.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` - Commit *dymint.Commit `protobuf:"bytes,2,opt,name=commit,proto3" json:"commit,omitempty"` -} - -func (m *GossipedBlock) Reset() { *m = GossipedBlock{} } -func (m *GossipedBlock) String() string { return proto.CompactTextString(m) } -func (*GossipedBlock) ProtoMessage() {} -func (*GossipedBlock) Descriptor() ([]byte, []int) { - return fileDescriptor_e39a70b849e02fbf, []int{0} -} -func (m *GossipedBlock) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *GossipedBlock) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_GossipedBlock.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *GossipedBlock) XXX_Merge(src proto.Message) { - xxx_messageInfo_GossipedBlock.Merge(m, src) -} -func (m *GossipedBlock) XXX_Size() int { - return m.Size() -} -func (m *GossipedBlock) XXX_DiscardUnknown() { - xxx_messageInfo_GossipedBlock.DiscardUnknown(m) -} - -var xxx_messageInfo_GossipedBlock proto.InternalMessageInfo - -func (m *GossipedBlock) GetBlock() *dymint.Block { - if m != nil { - return m.Block - } - return nil -} - -func (m *GossipedBlock) GetCommit() *dymint.Commit { - if m != nil { - return m.Commit - } - return nil -} - -func init() { - proto.RegisterType((*GossipedBlock)(nil), "p2p.events.GossipedBlock") -} - -func init() { proto.RegisterFile("p2p/gossiped_block.proto", fileDescriptor_e39a70b849e02fbf) } - -var fileDescriptor_e39a70b849e02fbf = []byte{ - // 197 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x28, 0x30, 0x2a, 0xd0, - 0x4f, 0xcf, 0x2f, 0x2e, 0xce, 0x2c, 0x48, 0x4d, 0x89, 0x4f, 0xca, 0xc9, 0x4f, 0xce, 0xd6, 0x2b, - 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x2a, 0x30, 0x2a, 0xd0, 0x4b, 0x2d, 0x4b, 0xcd, 0x2b, 0x29, - 0x96, 0x92, 0x2c, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x4f, 0xa9, 0xcc, 0xcd, 0xcc, 0x2b, 0x81, 0x52, - 0x10, 0x65, 0x4a, 0x31, 0x5c, 0xbc, 0xee, 0x50, 0xed, 0x4e, 0x20, 0xdd, 0x42, 0xca, 0x5c, 0xac, - 0x60, 0x63, 0x24, 0x18, 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x78, 0xf5, 0xa0, 0xca, 0xc1, 0xb2, 0x41, - 0x10, 0x39, 0x21, 0x35, 0x2e, 0xb6, 0xe4, 0xfc, 0xdc, 0xdc, 0xcc, 0x12, 0x09, 0x26, 0xb0, 0x2a, - 0x3e, 0x98, 0x2a, 0x67, 0xb0, 0x68, 0x10, 0x54, 0xd6, 0xc9, 0xfe, 0xc4, 0x23, 0x39, 0xc6, 0x0b, - 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, - 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0x54, 0xd3, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, - 0x41, 0x0e, 0x4a, 0xcd, 0x2b, 0xce, 0xcc, 0xcf, 0xab, 0xa8, 0xac, 0x82, 0x39, 0x12, 0xe4, 0xaf, - 0x82, 0xa4, 0x24, 0x36, 0xb0, 0x2b, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xf3, 0x0e, 0xf1, - 0x19, 0xe8, 0x00, 0x00, 0x00, -} - -func (m *GossipedBlock) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *GossipedBlock) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *GossipedBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if m.Commit != nil { - { - size, err := m.Commit.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintGossipedBlock(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - if m.Block != nil { - { - size, err := m.Block.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintGossipedBlock(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func encodeVarintGossipedBlock(dAtA []byte, offset int, v uint64) int { - offset -= sovGossipedBlock(v) - base := offset - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ - } - dAtA[offset] = uint8(v) - return base -} -func (m *GossipedBlock) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - if m.Block != nil { - l = m.Block.Size() - n += 1 + l + sovGossipedBlock(uint64(l)) - } - if m.Commit != nil { - l = m.Commit.Size() - n += 1 + l + sovGossipedBlock(uint64(l)) - } - return n -} - -func sovGossipedBlock(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 -} -func sozGossipedBlock(x uint64) (n int) { - return sovGossipedBlock(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (m *GossipedBlock) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGossipedBlock - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: GossipedBlock: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: GossipedBlock: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Block", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGossipedBlock - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthGossipedBlock - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthGossipedBlock - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Block == nil { - m.Block = &dymint.Block{} - } - if err := m.Block.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Commit", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGossipedBlock - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthGossipedBlock - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthGossipedBlock - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.Commit == nil { - m.Commit = &dymint.Commit{} - } - if err := m.Commit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipGossipedBlock(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthGossipedBlock - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func skipGossipedBlock(dAtA []byte) (n int, err error) { - l := len(dAtA) - iNdEx := 0 - depth := 0 - for iNdEx < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowGossipedBlock - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - wireType := int(wire & 0x7) - switch wireType { - case 0: - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowGossipedBlock - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - iNdEx++ - if dAtA[iNdEx-1] < 0x80 { - break - } - } - case 1: - iNdEx += 8 - case 2: - var length int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowGossipedBlock - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - length |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if length < 0 { - return 0, ErrInvalidLengthGossipedBlock - } - iNdEx += length - case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupGossipedBlock - } - depth-- - case 5: - iNdEx += 4 - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - if iNdEx < 0 { - return 0, ErrInvalidLengthGossipedBlock - } - if depth == 0 { - return iNdEx, nil - } - } - return 0, io.ErrUnexpectedEOF -} - -var ( - ErrInvalidLengthGossipedBlock = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowGossipedBlock = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupGossipedBlock = fmt.Errorf("proto: unexpected end of group") -) diff --git a/p2p/validator.go b/p2p/validator.go index 0b1362b6a..cb8ac890f 100644 --- a/p2p/validator.go +++ b/p2p/validator.go @@ -71,7 +71,7 @@ func (v *Validator) TxValidator(mp mempool.Mempool, mpoolIDS *nodemempool.Mempoo // BlockValidator runs basic checks on the gossiped block func (v *Validator) BlockValidator() GossipValidator { return func(blockMsg *GossipMessage) bool { - var gossipedBlock P2PBlockEvent + var gossipedBlock BlockData if err := gossipedBlock.UnmarshalBinary(blockMsg.Data); err != nil { v.logger.Error("Deserialize gossiped block.", "error", err) return false diff --git a/p2p/validator_test.go b/p2p/validator_test.go index d025c1314..ef8abf804 100644 --- a/p2p/validator_test.go +++ b/p2p/validator_test.go @@ -168,7 +168,7 @@ func TestValidator_BlockValidator(t *testing.T) { } // Create gossiped block - gossipedBlock := p2p.P2PBlockEvent{Block: *block, Commit: *commit} + gossipedBlock := p2p.BlockData{Block: *block, Commit: *commit} gossipedBlockBytes, err := gossipedBlock.MarshalBinary() require.NoError(t, err) blockMsg := &p2p.GossipMessage{ diff --git a/proto/p2p/gossiped_block.proto b/proto/p2p/gossiped_block.proto deleted file mode 100644 index e006b60e2..000000000 --- a/proto/p2p/gossiped_block.proto +++ /dev/null @@ -1,11 +0,0 @@ -syntax = "proto3"; -package p2p.events; - -option go_package = "github.com/dymensionxyz/dymint/p2p/pb"; - -import "types/dymint/dymint.proto"; - -message GossipedBlock { - dymint.Block block = 1; - dymint.Commit commit = 2; -} From 06231db6e876fc63a8101e7e8191bfb0cdb65932 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Fri, 9 Aug 2024 10:04:34 -0400 Subject: [PATCH 2/2] comment improved --- p2p/block.go | 74 ++++++++ p2p/pb/blockdata.pb.go | 387 ++++++++++++++++++++++++++++++++++++++ proto/p2p/blockdata.proto | 11 ++ 3 files changed, 472 insertions(+) create mode 100644 p2p/block.go create mode 100644 p2p/pb/blockdata.pb.go create mode 100644 proto/p2p/blockdata.proto diff --git a/p2p/block.go b/p2p/block.go new file mode 100644 index 000000000..099dcad53 --- /dev/null +++ b/p2p/block.go @@ -0,0 +1,74 @@ +package p2p + +import ( + "github.com/dymensionxyz/dymint/p2p/pb" + "github.com/dymensionxyz/dymint/types" + tmtypes "github.com/tendermint/tendermint/types" +) + +/* -------------------------------------------------------------------------- */ +/* Event Data */ +/* -------------------------------------------------------------------------- */ + +// BlockData defines the struct of the data for each block sent via P2P +type BlockData struct { + // Block is the block that was gossiped + Block types.Block + // Commit is the commit that was gossiped + Commit types.Commit +} + +// MarshalBinary encodes BlockData into binary form and returns it. +func (b *BlockData) MarshalBinary() ([]byte, error) { + return b.ToProto().Marshal() +} + +// UnmarshalBinary decodes binary form of p2p received block into object. +func (b *BlockData) UnmarshalBinary(data []byte) error { + var pbBlock pb.BlockData + err := pbBlock.Unmarshal(data) + if err != nil { + return err + } + err = b.FromProto(&pbBlock) + return err +} + +// ToProto converts Data into protobuf representation and returns it. +func (b *BlockData) ToProto() *pb.BlockData { + return &pb.BlockData{ + Block: b.Block.ToProto(), + Commit: b.Commit.ToProto(), + } +} + +// FromProto fills BlockData with data from its protobuf representation. +func (b *BlockData) FromProto(other *pb.BlockData) error { + if err := b.Block.FromProto(other.Block); err != nil { + return err + } + if err := b.Commit.FromProto(other.Commit); err != nil { + return err + } + return nil +} + +// Validate run basic validation on the p2p block received +func (b *BlockData) Validate(proposer *types.Sequencer) error { + if err := b.Block.ValidateBasic(); err != nil { + return err + } + if err := b.Commit.ValidateBasic(); err != nil { + return err + } + if err := b.Commit.ValidateWithHeader(proposer, &b.Block.Header); err != nil { + return err + } + abciData := tmtypes.Data{ + Txs: types.ToABCIBlockDataTxs(&b.Block.Data), + } + if b.Block.Header.DataHash != [32]byte(abciData.Hash()) { + return types.ErrInvalidHeaderDataHash + } + return nil +} diff --git a/p2p/pb/blockdata.pb.go b/p2p/pb/blockdata.pb.go new file mode 100644 index 000000000..23f78739c --- /dev/null +++ b/p2p/pb/blockdata.pb.go @@ -0,0 +1,387 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: p2p/blockdata.proto + +package pb + +import ( + fmt "fmt" + dymint "github.com/dymensionxyz/dymint/types/pb/dymint" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +type BlockData struct { + Block *dymint.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` + Commit *dymint.Commit `protobuf:"bytes,2,opt,name=commit,proto3" json:"commit,omitempty"` +} + +func (m *BlockData) Reset() { *m = BlockData{} } +func (m *BlockData) String() string { return proto.CompactTextString(m) } +func (*BlockData) ProtoMessage() {} +func (*BlockData) Descriptor() ([]byte, []int) { + return fileDescriptor_3cee9514a66baf30, []int{0} +} +func (m *BlockData) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *BlockData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_BlockData.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *BlockData) XXX_Merge(src proto.Message) { + xxx_messageInfo_BlockData.Merge(m, src) +} +func (m *BlockData) XXX_Size() int { + return m.Size() +} +func (m *BlockData) XXX_DiscardUnknown() { + xxx_messageInfo_BlockData.DiscardUnknown(m) +} + +var xxx_messageInfo_BlockData proto.InternalMessageInfo + +func (m *BlockData) GetBlock() *dymint.Block { + if m != nil { + return m.Block + } + return nil +} + +func (m *BlockData) GetCommit() *dymint.Commit { + if m != nil { + return m.Commit + } + return nil +} + +func init() { + proto.RegisterType((*BlockData)(nil), "p2p.events.BlockData") +} + +func init() { proto.RegisterFile("p2p/blockdata.proto", fileDescriptor_3cee9514a66baf30) } + +var fileDescriptor_3cee9514a66baf30 = []byte{ + // 193 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x30, 0x2a, 0xd0, + 0x4f, 0xca, 0xc9, 0x4f, 0xce, 0x4e, 0x49, 0x2c, 0x49, 0xd4, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, + 0xe2, 0x2a, 0x30, 0x2a, 0xd0, 0x4b, 0x2d, 0x4b, 0xcd, 0x2b, 0x29, 0x96, 0x92, 0x2c, 0xa9, 0x2c, + 0x48, 0x2d, 0xd6, 0x4f, 0xa9, 0xcc, 0xcd, 0xcc, 0x2b, 0x81, 0x52, 0x10, 0x65, 0x4a, 0x11, 0x5c, + 0x9c, 0x4e, 0x20, 0x9d, 0x2e, 0x89, 0x25, 0x89, 0x42, 0xca, 0x5c, 0xac, 0x60, 0x63, 0x24, 0x18, + 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x78, 0xf5, 0xa0, 0x4a, 0xc1, 0x2a, 0x82, 0x20, 0x72, 0x42, 0x6a, + 0x5c, 0x6c, 0xc9, 0xf9, 0xb9, 0xb9, 0x99, 0x25, 0x12, 0x4c, 0x60, 0x55, 0x7c, 0x30, 0x55, 0xce, + 0x60, 0xd1, 0x20, 0xa8, 0xac, 0x93, 0xfd, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, + 0x78, 0x24, 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, + 0x44, 0xa9, 0xa6, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0x82, 0x1c, 0x93, 0x9a, + 0x57, 0x9c, 0x99, 0x9f, 0x57, 0x51, 0x59, 0x05, 0x73, 0x20, 0xc8, 0x3b, 0x05, 0x49, 0x49, 0x6c, + 0x60, 0x17, 0x1a, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x81, 0x13, 0x29, 0x7e, 0xdf, 0x00, 0x00, + 0x00, +} + +func (m *BlockData) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *BlockData) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *BlockData) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Commit != nil { + { + size, err := m.Commit.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintBlockdata(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + if m.Block != nil { + { + size, err := m.Block.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintBlockdata(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintBlockdata(dAtA []byte, offset int, v uint64) int { + offset -= sovBlockdata(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *BlockData) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Block != nil { + l = m.Block.Size() + n += 1 + l + sovBlockdata(uint64(l)) + } + if m.Commit != nil { + l = m.Commit.Size() + n += 1 + l + sovBlockdata(uint64(l)) + } + return n +} + +func sovBlockdata(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozBlockdata(x uint64) (n int) { + return sovBlockdata(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *BlockData) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBlockdata + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: BlockData: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: BlockData: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Block", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBlockdata + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthBlockdata + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthBlockdata + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Block == nil { + m.Block = &dymint.Block{} + } + if err := m.Block.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Commit", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowBlockdata + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthBlockdata + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthBlockdata + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Commit == nil { + m.Commit = &dymint.Commit{} + } + if err := m.Commit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipBlockdata(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthBlockdata + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipBlockdata(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowBlockdata + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowBlockdata + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowBlockdata + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthBlockdata + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupBlockdata + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthBlockdata + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthBlockdata = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowBlockdata = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupBlockdata = fmt.Errorf("proto: unexpected end of group") +) diff --git a/proto/p2p/blockdata.proto b/proto/p2p/blockdata.proto new file mode 100644 index 000000000..53480612a --- /dev/null +++ b/proto/p2p/blockdata.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; +package p2p.events; + +option go_package = "github.com/dymensionxyz/dymint/p2p/pb"; + +import "types/dymint/dymint.proto"; + +message BlockData { + dymint.Block block = 1; + dymint.Commit commit = 2; +}