mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-14 10:06:40 +00:00

* [NOD-1191] Convert wire protocol to 100% protobuf * [NOD-1191] Simplify wire interface and remove redundant messages * [NOD-1191] Map all proto to wire conversions * [NOD-1203] Create netadapter outside of protocol manager * [NOD-1191] Fix nil errors * [NOD-1191] Fix comments * [NOD-1191] Add converter interface * [NOD-1191] Add missing GetBlockLocator message * [NOD-1191] Change message names that starts with 'get' to 'request' * [NOD-1191] Change message commands values * [NOD-1191] Remove redundant methods * [NOD-1191] Rename message constructors * [NOD-1191] Change message commands to use iota * [NOD-1191] Add missing outputs to protobuf conversion * [NOD-1191] Make block header a required field * [NOD-1191] Rename variables * [NOD-1212] Fix test names * [NOD-1191] Rename flow names * [NOD-1191] Fix infinite loop
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package flowcontext
|
|
|
|
import (
|
|
"sync/atomic"
|
|
|
|
"github.com/kaspanet/kaspad/blockdag"
|
|
"github.com/kaspanet/kaspad/protocol/flows/blockrelay"
|
|
"github.com/kaspanet/kaspad/util"
|
|
"github.com/kaspanet/kaspad/util/daghash"
|
|
"github.com/kaspanet/kaspad/wire"
|
|
)
|
|
|
|
// OnNewBlock updates the mempool after a new block arrival, and
|
|
// relays newly unorphaned transactions and possibly rebroadcast
|
|
// manually added transactions when not in IBD.
|
|
func (f *FlowContext) OnNewBlock(block *util.Block) error {
|
|
transactionsAcceptedToMempool, err := f.txPool.HandleNewBlock(block)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// TODO(libp2p) Notify transactionsAcceptedToMempool to RPC
|
|
|
|
return f.broadcastTransactionsAfterBlockAdded(block, transactionsAcceptedToMempool)
|
|
}
|
|
|
|
func (f *FlowContext) broadcastTransactionsAfterBlockAdded(block *util.Block, transactionsAcceptedToMempool []*util.Tx) error {
|
|
f.updateTransactionsToRebroadcast(block)
|
|
|
|
// Don't relay transactions when in IBD.
|
|
if atomic.LoadUint32(&f.isInIBD) != 0 {
|
|
return nil
|
|
}
|
|
|
|
var txIDsToRebroadcast []*daghash.TxID
|
|
if f.shouldRebroadcastTransactions() {
|
|
txIDsToRebroadcast = f.txIDsToRebroadcast()
|
|
}
|
|
|
|
txIDsToBroadcast := make([]*daghash.TxID, len(transactionsAcceptedToMempool)+len(txIDsToRebroadcast))
|
|
for i, tx := range transactionsAcceptedToMempool {
|
|
txIDsToBroadcast[i] = tx.ID()
|
|
}
|
|
|
|
copy(txIDsToBroadcast[len(transactionsAcceptedToMempool):], txIDsToBroadcast)
|
|
|
|
if len(txIDsToBroadcast) == 0 {
|
|
return nil
|
|
}
|
|
if len(txIDsToBroadcast) > wire.MaxInvPerTxInvMsg {
|
|
txIDsToBroadcast = txIDsToBroadcast[:wire.MaxInvPerTxInvMsg]
|
|
}
|
|
inv := wire.NewMsgInvTransaction(txIDsToBroadcast)
|
|
return f.Broadcast(inv)
|
|
}
|
|
|
|
// SharedRequestedBlocks returns a *blockrelay.SharedRequestedBlocks for sharing
|
|
// data about requested blocks between different peers.
|
|
func (f *FlowContext) SharedRequestedBlocks() *blockrelay.SharedRequestedBlocks {
|
|
return f.sharedRequestedBlocks
|
|
}
|
|
|
|
// AddBlock adds the given block to the DAG and propagates it.
|
|
func (f *FlowContext) AddBlock(block *util.Block, flags blockdag.BehaviorFlags) error {
|
|
_, _, err := f.DAG().ProcessBlock(block, flags)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return f.Broadcast(wire.NewMsgInvBlock(block.Hash()))
|
|
}
|