From 293615ea5f9b873c77659869b9e3fb7c045e391d Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 29 Nov 2019 12:27:15 +0300 Subject: [PATCH] network/consensus: add new block relaying Tell everyone about our new shiny blocks. --- pkg/consensus/consensus.go | 5 +++++ pkg/network/server.go | 14 ++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/consensus/consensus.go b/pkg/consensus/consensus.go index 3c9e87dc6..2895adae3 100644 --- a/pkg/consensus/consensus.go +++ b/pkg/consensus/consensus.go @@ -62,6 +62,9 @@ type Config struct { // Broadcast is a callback which is called to notify server // about new consensus payload to sent. Broadcast func(p *Payload) + // RelayBlock is a callback that is called to notify server + // about the new block that needs to be broadcasted. + RelayBlock func(b *core.Block) // Chain is a core.Blockchainer instance. Chain core.Blockchainer // RequestTx is a callback to which will be called @@ -250,6 +253,8 @@ func (s *service) processBlock(b block.Block) { if err := s.Chain.AddBlock(bb); err != nil { s.log.Warnf("error on add block: %v", err) + } else { + s.Config.RelayBlock(bb) } } diff --git a/pkg/network/server.go b/pkg/network/server.go index 3e6e333b5..7900923b3 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -95,10 +95,11 @@ func NewServer(config ServerConfig, chain core.Blockchainer) *Server { } srv, err := consensus.NewService(consensus.Config{ - Broadcast: s.handleNewPayload, - Chain: chain, - RequestTx: s.requestTx, - Wallet: config.Wallet, + Broadcast: s.handleNewPayload, + RelayBlock: s.relayBlock, + Chain: chain, + RequestTx: s.requestTx, + Wallet: config.Wallet, }) if err != nil { return nil @@ -614,6 +615,11 @@ func (s *Server) relayInventory(t payload.InventoryType, hashes ...util.Uint256) } } +// relayBlock tells all the other connected nodes about the given block. +func (s *Server) relayBlock(b *core.Block) { + s.relayInventory(payload.BlockType, b.Hash()) +} + // RelayTxn a new transaction to the local node and the connected peers. // Reference: the method OnRelay in C#: https://github.com/neo-project/neo/blob/master/neo/Network/P2P/LocalNode.cs#L159 func (s *Server) RelayTxn(t *transaction.Transaction) RelayReason {