network/consensus: add new block relaying

Tell everyone about our new shiny blocks.
This commit is contained in:
Roman Khimov 2019-11-29 12:27:15 +03:00
parent 4d286dcfeb
commit 293615ea5f
2 changed files with 15 additions and 4 deletions

View file

@ -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)
}
}

View file

@ -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 {