Skip to content

Commit

Permalink
refactor(p2p): rename p2p block event (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
srene authored Aug 12, 2024
1 parent d3b4311 commit 603c160
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 157 deletions.
4 changes: 2 additions & 2 deletions block/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions p2p/block.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 5 additions & 5 deletions p2p/block_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/block_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions p2p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
74 changes: 0 additions & 74 deletions p2p/p2p_block.go

This file was deleted.

Loading

0 comments on commit 603c160

Please sign in to comment.